(*  The following are examples of curvature analysis  in seeds of  Vitis*)






	(*******************************   Cordecabrito*)

Needs["Splines`"]

Cordecabrito[1]={{1.35109,5.68072},{1.4125,5.40436},{1.47392,5.06659},{1.53533,4.79023},{1.59674,4.57528},{1.65816,4.39104},{1.71957,4.23751},{1.78098,4.08398},{1.8424,3.93044},{1.90381,3.80762},{1.96522,3.7155},{2.02664,3.62338},{2.08805,3.53126},{2.14946,3.46985},{2.21088,3.37773},{2.27229,3.2549},{2.3337,3.13207},{2.39511,3.00925},{2.45653,2.88642},{2.51794,2.76359},{2.57935,2.51794},{2.64077,2.18017},{2.70218,1.96522},{2.76359,1.50462},{2.82501,1.3818},{2.88642,1.28968},{2.94783,1.16685},{3.00925,1.04402},{3.07066,0.9519},{3.13207,0.9212},{3.19349,0.89049},{3.2549,0.89049},{3.31631,0.9212},{3.37773,0.9212},{3.43914,0.9212},{3.50055,0.9212},{3.56197,0.9519},{3.62338,0.98261},{3.68479,1.01332},{3.74621,1.10544},{3.80762,1.22826},{3.86903,1.35109},{3.93044,1.53533},{3.99186,2.18017},{4.05327,2.48723},{4.11468,3.07066},{4.1761,3.19349},{4.23751,3.31631},{4.29892,3.43914},{4.36034,3.56197},{4.42175,3.68479},{4.48316,3.80762},{4.54458,3.93044},{4.60599,4.05327},{4.6674,4.1761},{4.72882,4.36034},{4.79023,4.57528},{4.85164,4.82094},{4.91306,5.15871},{4.97447,5.77284},{4.91306,6.35627},{4.85164,6.60192},{4.79023,6.78616},{4.72882,6.93969},{4.6674,7.03181},{4.60599,7.09322},{4.54458,7.15464},{4.48316,7.21605},{4.42175,7.27746},{4.36034,7.33888},{4.29892,7.36958},{4.23751,7.40029},{4.1761,7.431},{4.11468,7.4617},{4.05327,7.49241},{3.99186,7.49241},{3.93044,7.49241},{3.86903,7.49241},{3.80762,7.4617},{3.74621,7.431},{3.68479,7.40029},{3.62338,7.36958},{3.56197,7.36958},{3.50055,7.33888},{3.43914,7.27746},{3.37773,7.24676},{3.31631,7.24676},{3.2549,7.24676},{3.19349,7.21605},{3.13207,7.18534},{3.07066,7.21605},{3.00925,7.24676},{2.94783,7.27746},{2.88642,7.30817},{2.82501,7.33888},{2.76359,7.36958},{2.70218,7.36958},{2.64077,7.36958},{2.57935,7.36958},{2.51794,7.36958},{2.45653,7.36958},{2.39511,7.36958},{2.3337,7.36958},{2.27229,7.36958},{2.21088,7.33888},{2.14946,7.30817},{2.08805,7.30817},{2.02664,7.27746},{1.96522,7.21605},{1.90381,7.15464},{1.8424,7.09322},{1.78098,7.0011},{1.71957,6.90898},{1.65816,6.81686},{1.59674,6.69404},{1.53533,6.57121},{1.47392,6.35627},{1.4125,5.98779}}
Cordecabrito[2]={{1.78098,6.26415},{1.8424,5.80355},{1.90381,5.46577},{1.96522,5.15871},{2.02664,4.97447},{2.08805,4.82094},{2.14946,4.6674},{2.21088,4.54458},{2.27229,4.39104},{2.3337,4.23751},{2.39511,4.14539},{2.45653,4.05327},{2.51794,3.93044},{2.57935,3.80762},{2.64077,3.68479},{2.70218,3.56197},{2.76359,3.46985},{2.82501,3.34702},{2.88642,3.19349},{2.94783,3.03995},{3.00925,2.88642},{3.07066,2.73289},{3.13207,2.48723},{3.19349,2.27229},{3.2549,2.02664},{3.31631,1.81169},{3.37773,1.75028},{3.43914,1.68886},{3.50055,1.62745},{3.56197,1.56604},{3.62338,1.53533},{3.68479,1.53533},{3.74621,1.53533},{3.80762,1.56604},{3.86903,1.62745},{3.93044,1.68886},{3.99186,1.71957},{4.05327,1.71957},{4.11468,1.71957},{4.1761,1.78098},{4.23751,1.90381},{4.29892,2.21088},{4.36034,2.61006},{4.42175,2.82501},{4.48316,2.94783},{4.54458,3.07066},{4.60599,3.19349},{4.6674,3.37773},{4.72882,3.62338},{4.79023,3.83833},{4.85164,3.99186},{4.91306,4.14539},{4.97447,4.29892},{5.03588,4.42175},{5.0973,4.54458},{5.15871,4.6674},{5.22012,4.79023},{5.28154,4.91306},{5.34295,5.06659},{5.40436,5.22012},{5.46577,5.40436},{5.52719,5.68072},{5.5886,6.38697},{5.52719,7.06252},{5.46577,7.24676},{5.40436,7.36958},{5.34295,7.4617},{5.28154,7.52312},{5.22012,7.61524},{5.15871,7.70736},{5.0973,7.76877},{5.03588,7.83018},{4.97447,7.8916},{4.91306,7.9223},{4.85164,7.95301},{4.79023,7.98372},{4.72882,8.01442},{4.6674,8.04513},{4.60599,8.04513},{4.54458,8.07584},{4.48316,8.10654},{4.42175,8.10654},{4.36034,8.07584},{4.29892,8.04513},{4.23751,8.04513},{4.1761,8.01442},{4.11468,7.98372},{4.05327,7.95301},{3.99186,7.8916},{3.93044,7.86089},{3.86903,7.83018},{3.80762,7.79948},{3.74621,7.79948},{3.68479,7.79948},{3.62338,7.79948},{3.56197,7.83018},{3.50055,7.86089},{3.43914,7.86089},{3.37773,7.8916},{3.31631,7.95301},{3.2549,7.98372},{3.19349,8.01442},{3.13207,8.04513},{3.07066,8.04513},{3.00925,8.04513},{2.94783,8.04513},{2.88642,8.04513},{2.82501,8.04513},{2.76359,8.04513},{2.70218,8.01442},{2.64077,7.98372},{2.57935,7.98372},{2.51794,7.95301},{2.45653,7.8916},{2.39511,7.83018},{2.3337,7.76877},{2.27229,7.70736},{2.21088,7.64594},{2.14946,7.55382},{2.08805,7.431},{2.02664,7.30817},{1.96522,7.15464},{1.90381,6.9704},{1.8424,6.72475}}
Cordecabrito[3]={{1.59674,6.14132},{1.65816,5.71143},{1.71957,5.40436},{1.78098,5.15871},{1.8424,4.97447},{1.90381,4.79023},{1.96522,4.60599},{2.02664,4.48316},{2.08805,4.36034},{2.14946,4.23751},{2.21088,4.11468},{2.27229,4.02256},{2.3337,3.93044},{2.39511,3.83833},{2.45653,3.77691},{2.51794,3.68479},{2.57935,3.56197},{2.64077,3.40843},{2.70218,3.22419},{2.76359,3.03995},{2.82501,2.85571},{2.88642,2.64077},{2.94783,2.36441},{3.00925,2.08805},{3.07066,1.8424},{3.13207,1.65816},{3.19349,1.56604},{3.2549,1.50462},{3.31631,1.44321},{3.37773,1.4125},{3.43914,1.4125},{3.50055,1.4125},{3.56197,1.4125},{3.62338,1.44321},{3.68479,1.47392},{3.74621,1.47392},{3.80762,1.50462},{3.86903,1.59674},{3.93044,1.68886},{3.99186,1.78098},{4.05327,1.96522},{4.11468,2.30299},{4.1761,2.67147},{4.23751,2.94783},{4.29892,3.16278},{4.36034,3.34702},{4.42175,3.53126},{4.48316,3.68479},{4.54458,3.80762},{4.60599,3.89974},{4.6674,3.99186},{4.72882,4.08398},{4.79023,4.14539},{4.85164,4.23751},{4.91306,4.36034},{4.97447,4.45246},{5.03588,4.57528},{5.0973,4.72882},{5.15871,4.88235},{5.22012,5.06659},{5.28154,5.25083},{5.34295,5.43507},{5.40436,5.83425},{5.46577,6.60192},{5.40436,7.15464},{5.34295,7.30817},{5.28154,7.40029},{5.22012,7.49241},{5.15871,7.58453},{5.0973,7.64594},{5.03588,7.73806},{4.97447,7.83018},{4.91306,7.8916},{4.85164,7.95301},{4.79023,7.98372},{4.72882,8.01442},{4.6674,8.07584},{4.60599,8.10654},{4.54458,8.10654},{4.48316,8.13725},{4.42175,8.16796},{4.36034,8.16796},{4.29892,8.16796},{4.23751,8.19866},{4.1761,8.22937},{4.11468,8.22937},{4.05327,8.22937},{3.99186,8.19866},{3.93044,8.16796},{3.86903,8.13725},{3.80762,8.10654},{3.74621,8.10654},{3.68479,8.07584},{3.62338,8.04513},{3.56197,8.04513},{3.50055,8.07584},{3.43914,8.10654},{3.37773,8.10654},{3.31631,8.10654},{3.2549,8.10654},{3.19349,8.13725},{3.13207,8.19866},{3.07066,8.22937},{3.00925,8.22937},{2.94783,8.19866},{2.88642,8.16796},{2.82501,8.16796},{2.76359,8.13725},{2.70218,8.10654},{2.64077,8.10654},{2.57935,8.07584},{2.51794,8.04513},{2.45653,8.01442},{2.39511,7.95301},{2.3337,7.9223},{2.27229,7.8916},{2.21088,7.83018},{2.14946,7.73806},{2.08805,7.64594},{2.02664,7.58453},{1.96522,7.52312},{1.90381,7.431},{1.8424,7.30817},{1.78098,7.15464},{1.71957,6.93969},{1.65816,6.60192}}

(*Poner aqui la familia*)
var=Cordecabrito
(* el número de curvas estudiadas/semillas de la familia*)
m=3;
For[j=1,j≤m,j++,
puntos=var[j];
(* pru es el spline cubico
d es el vector que contiene la información de pru 
n es el número de trozos del spline, es decir, el número de puntos menos 1
*)
pru=SplineFit[puntos,Cubic];
d=InputForm[pru];
n=IntegerPart[d[[1,2,2]]];
spli=ParametricPlot[pru[t],{t,0,n},PlotRange->All, DisplayFunction->Identity];
(* Con los coeficientes se calculan los polinomios cúbicos que dan el spline. Habrá n polinomios para la primera coordenada (X) y otros tantos para la segunda
*)
For[i=0,i<n,i++,
SX[i,t_]=d[[1,4,i+1,1,1]]+d[[1,4,i+1,1,2]]*(t-i)+d[[1,4,i+1,1,3]]*(t-i)^2+d[[1,4,i+1,1,4]]*(t-i)^3];
For[i=0,i<n,i++,SY[i,t_]=d[[1,4,i+1,2,1]]+d[[1,4,i+1,2,2]]*(t-i)+d[[1,4,i+1,2,3]]*(t-i)^2+d[[1,4,i+1,2,4]]*(t-i)^3];
(* Derivada primera y segunda de cada trozo del spline
*)
For[i=0,i<n,i++,DX[i,t_]=d[[1,4,i+1,1,2]]+2*d[[1,4,i+1,1,3]]*(t-i)+3*d[[1,4,i+1,1,4]]*(t-i)^2];
For[i=0,i<n,i++,DY[i,t_]=d[[1,4,i+1,2,2]]+2*d[[1,4,i+1,2,3]]*(t-i)+3*d[[1,4,i+1,2,4]]*(t-i)^2];
For[i=0,i<n,i++,DDX[i,t_]=2*d[[1,4,i+1,1,3]]+6*d[[1,4,i+1,1,4]]*(t-i)];
For[i=0,i<n,i++,DDY[i,t_]=2*d[[1,4,i+1,2,3]]+6*d[[1,4,i+1,2,4]]*(t-i)];
(*Cálculo de la curvatura a trozos
*)
For[i=0,i<n,i++,cur[i,t_]=(DX[i,t]DDY[i,t]-DDX[i,t]DY[i,t])/((DX[i,t])^2+(DY[i,t])^2)^(3/2)];
(* Dibujos de los trozos de curvatura 
*)
For[i=0,i<n,i++,dib[i]=Plot[cur[i,t],{t,i,i+1},DisplayFunction->Identity]];
pp=dib[0];
For[i=1,i<n,i++,pp=Show[pp,dib[i],PlotRange->All]];
(* bez es el spline de Bezier
b es el vector que contiene la información de bez
nb es el número de trozos del spline, es decir, el número de puntos menos 1 (Como nb=n, no se define nuevo)
*)
bez=SplineFit[puntos,Bezier];
b=InputForm[bez];
(*Calculo de las funciones de Bezier para la coordenada X y la Y; la curva es la imagen del intervalo[0,1]*)
For[i=1,i≤n+1,i++,BX[t_]=bez[[4,1,1]]/.#1->t];
For[i=1,i≤n+1,i++,BY[t_]=bez[[4,1,2]]/.#1->t];
dibez[j]=ParametricPlot[{BX[t],BY[t]},{t,0,1},PlotRange->All,DisplayFunction->Identity];
curbe[j,t_]=(BX'[t]BY''[t]-BX''[t]BY'[t])/((BX'[t])^2+(BY'[t])^2)^(3/2);
cur[j]=Abs[NMaxValue[SetPrecision[{curbe[j,t],0.1<t<1},50],t,WorkingPrecision->8]];
vmed[j]=Abs[NIntegrate[curbe[j,t],{t,0.2,0.8}]];
dibcurbe[j]=Plot[{curbe[j,t],vmed[j]},{t,0,1},PlotRange->{-4,4},DisplayFunction->Identity,PlotStyle->{RGBColor[0,1,0],{Dotted,Dashed}}];
total=GraphicsGrid[{
(*{spli,pp},*)
{Print[""],dibez[j],dibcurbe[j]}},Spacings->0.5];
Print[var,j, ":", "Máximo valor de la curvatura (en valor absoluto)",cur[j]];
Print[total]
]
Print["Curvaturas de cada ", var ,":  ", Table[cur[j],{j,1,3}]];
Print["Valores medios de curvatura de cada ", var ," en [0.1,0.9]:  ", Table[vmed[j],{j,1,3}]];