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






	(*******************************  mollarcano*)

Needs["Splines`"]

mollarcano[1]={{1.55083,7.10278},{1.61286,6.85465},{1.67489,6.54448},{1.73692,6.26534},{1.79896,6.04822},{1.86099,5.89314},{1.92302,5.73805},{1.98506,5.58297},{2.04709,5.45891},{2.10912,5.33484},{2.17116,5.21077},{2.23319,5.11772},{2.29522,5.05569},{2.35725,4.96264},{2.41929,4.86959},{2.48132,4.80756},{2.54335,4.71451},{2.60539,4.59044},{2.66742,4.40434},{2.72945,4.1252},{2.79149,3.53588},{2.85352,3.07063},{2.91555,3.0086},{2.97759,2.94657},{3.03962,2.88454},{3.10165,2.85352},{3.16368,2.8225},{3.22572,2.76047},{3.28775,2.72945},{3.34978,2.72945},{3.41182,2.72945},{3.47385,2.76047},{3.53588,2.79149},{3.59792,2.79149},{3.65995,2.79149},{3.72198,2.79149},{3.78401,2.8225},{3.84605,2.85352},{3.90808,2.88454},{3.97011,2.94657},{4.03215,3.13267},{4.09418,3.97011},{4.15621,4.28028},{4.21825,4.46638},{4.28028,4.55943},{4.34231,4.62146},{4.40434,4.68349},{4.46638,4.74553},{4.52841,4.80756},{4.59044,4.86959},{4.65248,4.93163},{4.71451,4.99366},{4.77654,5.08671},{4.83858,5.17976},{4.90061,5.24179},{4.96264,5.33484},{5.02467,5.45891},{5.08671,5.58297},{5.14874,5.73805},{5.21077,5.95517},{5.27281,6.2033},{5.33484,6.85465},{5.27281,7.47498},{5.21077,7.66108},{5.14874,7.84718},{5.08671,8.00226},{5.02467,8.09531},{4.96264,8.18836},{4.90061,8.28141},{4.83858,8.34344},{4.77654,8.40547},{4.71451,8.46751},{4.65248,8.52954},{4.59044,8.59157},{4.52841,8.65361},{4.46638,8.68462},{4.40434,8.71564},{4.34231,8.74666},{4.28028,8.74666},{4.21825,8.77767},{4.15621,8.80869},{4.09418,8.80869},{4.03215,8.80869},{3.97011,8.80869},{3.90808,8.80869},{3.84605,8.80869},{3.78401,8.80869},{3.72198,8.77767},{3.65995,8.74666},{3.59792,8.74666},{3.53588,8.71564},{3.47385,8.68462},{3.41182,8.68462},{3.34978,8.68462},{3.28775,8.71564},{3.22572,8.74666},{3.16368,8.74666},{3.10165,8.74666},{3.03962,8.74666},{2.97759,8.74666},{2.91555,8.74666},{2.85352,8.74666},{2.79149,8.74666},{2.72945,8.74666},{2.66742,8.71564},{2.60539,8.68462},{2.54335,8.65361},{2.48132,8.62259},{2.41929,8.62259},{2.35725,8.59157},{2.29522,8.52954},{2.23319,8.49852},{2.17116,8.43649},{2.10912,8.34344},{2.04709,8.28141},{1.98506,8.21938},{1.92302,8.15734},{1.86099,8.03328},{1.79896,7.90921},{1.73692,7.78514},{1.67489,7.59904},{1.61286,7.35091}}
mollarcano[2]={{1.86099,7.5},{1.92302,7.81616},{1.98506,8.15734},{2.04709,8.31242},{2.10912,8.43649},{2.17116,8.52954},{2.23319,8.59157},{2.29522,8.65361},{2.35725,8.74666},{2.41929,8.83971},{2.48132,8.87072},{2.54335,8.90174},{2.60539,8.96377},{2.66742,8.99479},{2.72945,9.0258},{2.79149,9.08784},{2.85352,9.11885},{2.91555,9.14987},{2.97759,9.18089},{3.03962,9.2119},{3.10165,9.24292},{3.16368,9.24292},{3.22572,9.24292},{3.28775,9.24292},{3.34978,9.24292},{3.41182,9.24292},{3.47385,9.24292},{3.53588,9.24292},{3.59792,9.24292},{3.65995,9.2119},{3.72198,9.18089},{3.78401,9.18089},{3.84605,9.18089},{3.90808,9.14987},{3.97011,9.11885},{4.03215,9.11885},{4.09418,9.11885},{4.15621,9.11885},{4.21825,9.11885},{4.28028,9.11885},{4.34231,9.11885},{4.40434,9.11885},{4.46638,9.11885},{4.52841,9.11885},{4.59044,9.11885},{4.65248,9.11885},{4.71451,9.08784},{4.77654,9.05682},{4.83858,9.0258},{4.90061,8.99479},{4.96264,8.99479},{5.02467,8.96377},{5.08671,8.90174},{5.14874,8.80869},{5.21077,8.71564},{5.27281,8.62259},{5.33484,8.52954},{5.39687,8.40547},{5.45891,8.18836},{5.52094,7.87819},{5.58297,7.41295},{5.52094,6.97871},{5.45891,6.69957},{5.39687,6.45143},{5.33484,6.23432},{5.27281,6.07924},{5.21077,5.95517},{5.14874,5.80009},{5.08671,5.61399},{5.02467,5.48992},{4.96264,5.42789},{4.90061,5.36586},{4.83858,5.30382},{4.77654,5.24179},{4.71451,5.17976},{4.65248,5.08671},{4.59044,4.93163},{4.52841,4.65248},{4.46638,4.06316},{4.40434,3.25673},{4.34231,3.1947},{4.28028,3.16368},{4.21825,3.13267},{4.15621,3.10165},{4.09418,3.10165},{4.03215,3.07063},{3.97011,3.03962},{3.90808,3.03962},{3.84605,3.03962},{3.78401,3.07063},{3.72198,3.10165},{3.65995,3.10165},{3.59792,3.10165},{3.53588,3.10165},{3.47385,3.10165},{3.41182,3.10165},{3.34978,3.13267},{3.28775,3.1947},{3.22572,3.65995},{3.16368,4.00113},{3.10165,4.40434},{3.03962,4.62146},{2.97759,4.77654},{2.91555,4.90061},{2.85352,4.99366},{2.79149,5.05569},{2.72945,5.08671},{2.66742,5.11772},{2.60539,5.17976},{2.54335,5.24179},{2.48132,5.30382},{2.41929,5.36586},{2.35725,5.42789},{2.29522,5.52094},{2.23319,5.645},{2.17116,5.76907},{2.10912,5.89314},{2.04709,6.04822},{1.98506,6.23432},{1.92302,6.51347}}
mollarcano[3]={{1.86099,7.07176},{1.92302,6.3894},{1.98506,6.07924},{2.04709,5.86212},{2.10912,5.67602},{2.17116,5.52094},{2.23319,5.42789},{2.29522,5.33484},{2.35725,5.24179},{2.41929,5.14874},{2.48132,5.05569},{2.54335,4.99366},{2.60539,4.93163},{2.66742,4.86959},{2.72945,4.80756},{2.79149,4.71451},{2.85352,4.59044},{2.91555,4.46638},{2.97759,4.28028},{3.03962,3.9391},{3.10165,3.53588},{3.16368,3.31877},{3.22572,3.25673},{3.28775,3.1947},{3.34978,3.13267},{3.41182,3.10165},{3.47385,3.07063},{3.53588,3.03962},{3.59792,3.07063},{3.65995,3.10165},{3.72198,3.10165},{3.78401,3.10165},{3.84605,3.10165},{3.90808,3.10165},{3.97011,3.10165},{4.03215,3.10165},{4.09418,3.10165},{4.15621,3.13267},{4.21825,3.1947},{4.28028,3.22572},{4.34231,3.25673},{4.40434,3.31877},{4.46638,3.87706},{4.52841,4.46638},{4.59044,4.59044},{4.65248,4.71451},{4.71451,4.80756},{4.77654,4.86959},{4.83858,4.93163},{4.90061,4.99366},{4.96264,5.05569},{5.02467,5.14874},{5.08671,5.24179},{5.14874,5.33484},{5.21077,5.45891},{5.27281,5.58297},{5.33484,5.70704},{5.39687,5.86212},{5.45891,6.07924},{5.52094,6.32737},{5.58297,6.66855},{5.645,7.16481},{5.58297,7.59904},{5.52094,7.84718},{5.45891,8.00226},{5.39687,8.12633},{5.33484,8.25039},{5.27281,8.37446},{5.21077,8.46751},{5.14874,8.52954},{5.08671,8.62259},{5.02467,8.71564},{4.96264,8.77767},{4.90061,8.83971},{4.83858,8.90174},{4.77654,8.93275},{4.71451,8.96377},{4.65248,9.0258},{4.59044,9.05682},{4.52841,9.08784},{4.46638,9.11885},{4.40434,9.11885},{4.34231,9.14987},{4.28028,9.14987},{4.21825,9.11885},{4.15621,9.11885},{4.09418,9.11885},{4.03215,9.11885},{3.97011,9.11885},{3.90808,9.11885},{3.84605,9.11885},{3.78401,9.11885},{3.72198,9.11885},{3.65995,9.11885},{3.59792,9.11885},{3.53588,9.11885},{3.47385,9.11885},{3.41182,9.14987},{3.34978,9.18089},{3.28775,9.18089},{3.22572,9.18089},{3.16368,9.2119},{3.10165,9.24292},{3.03962,9.2119},{2.97759,9.18089},{2.91555,9.14987},{2.85352,9.11885},{2.79149,9.08784},{2.72945,9.05682},{2.66742,9.0258},{2.60539,8.96377},{2.54335,8.90174},{2.48132,8.83971},{2.41929,8.74666},{2.35725,8.65361},{2.29522,8.59157},{2.23319,8.52954},{2.17116,8.43649},{2.10912,8.31242},{2.04709,8.15734},{1.98506,7.94023},{1.92302,7.69209}}

(*Poner aqui la familia*)
var=mollarcano;
(* 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[{curbe[j,t],0.2<t<1},t]];
vmed[j]=Abs[NIntegrate[curbe[j,t],{t,0.2,0.8}]];
dibcurbe[j]=Plot[{curbe[j,t],vmed[j]},{t,0,1},PlotRange->{-5,5},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}]];