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






	(*******************************  AJ300  *)

Needs["Splines`"]

caroliniana[1]={{8.500,19.5000},{10.500,20.0000},{12.500,20.5000},{14.500,22.0000},{16.500,23.0000},{18.500,24.5000},{20.500,25.0000},{22.500,28.0000},{24.500,29.5000},{26.500,31.5000},{28.500,35.0000},{30.500,37.5000},{32.500,38.5000},{34.500,41.5000},{36.500,43.5000},{38.500,45.5000},{40.500,48.5000},{42.500,51.0000},{44.500,52.5000},{46.500,56.0000},{48.500,59.0000},{50.500,60.0000},{52.500,62.0000},{54.500,63.5000},{56.500,63.5000},{58.500,64.5000},{60.500,65.5000},{62.500,66.0000},{64.500,66.0000},{66.500,65.5000},{68.500,64.0000},{70.500,63.0000},{72.500,62.0000},{74.500,59.0000},{76.500,56.0000},{78.500,53.5000},{80.500,51.0000},{82.500,49.0000},{84.500,46.0000},{86.500,45.0000},{88.500,43.5000},{90.500,42.0000},{92.500,41.5000},{94.500,40.0000},{96.500,39.5000},{98.500,38.5000},{100.500,36.5000},{102.500,35.0000},{104.500,33.5000},{106.500,31.0000},{108.500,29.5000},{110.500,28.0000},{112.500,27.0000},{114.500,25.0000},{116.500,23.0000},{118.500,22.0000},{120.500,19.5000},{122.500,17.0000},{124.500,14.5000},{126.500,13.5000},{128.500,12.0000},{130.500,9.5000},{132.500,7.5000},{134.500,7.0000},{136.500,6.5000},{138.500,5.5000},{140.500,4.5000},{142.500,3.5000}}
caroliniana[2]={{8.500,32.5000},{10.500,33.5000},{12.500,33.5000},{14.500,33.5000},{16.500,34.0000},{18.500,34.5000},{20.500,34.5000},{22.500,35.0000},{24.500,36.0000},{26.500,37.0000},{28.500,38.0000},{30.500,39.5000},{32.500,41.0000},{34.500,42.0000},{36.500,43.5000},{38.500,45.0000},{40.500,46.5000},{42.500,48.0000},{44.500,49.0000},{46.500,50.0000},{48.500,52.0000},{50.500,54.0000},{52.500,57.0000},{54.500,58.0000},{56.500,59.5000},{58.500,61.0000},{60.500,62.0000},{62.500,62.0000},{64.500,62.5000},{66.500,63.0000},{68.500,63.0000},{70.500,63.0000},{72.500,63.5000},{74.500,62.5000},{76.500,62.5000},{78.500,61.0000},{80.500,60.5000},{82.500,59.0000},{84.500,57.5000},{86.500,57.0000},{88.500,55.0000},{90.500,54.0000},{92.500,53.5000},{94.500,52.5000},{96.500,50.5000},{98.500,48.0000},{100.500,47.5000},{102.500,46.5000},{104.500,46.0000},{106.500,45.5000},{108.500,45.0000},{110.500,43.5000},{112.500,43.0000},{114.500,42.0000},{116.500,41.5000},{118.500,41.0000},{120.500,40.5000},{122.500,39.5000},{124.500,39.0000},{126.500,37.5000},{128.500,36.5000},{130.500,36.0000},{132.500,35.0000},{134.500,33.5000},{136.500,33.0000},{138.500,31.0000},{140.500,30.5000},{142.500,30.5000},{144.500,30.0000},{146.500,29.5000},{148.500,29.5000}}
caroliniana[3]={{2.500,27.5000},{4.500,28.5000},{6.500,28.5000},{8.500,28.5000},{10.500,29.0000},{12.500,31.0000},{14.500,33.0000},{16.500,35.0000},{18.500,37.0000},{20.500,40.0000},{22.500,42.0000},{24.500,44.0000},{26.500,46.0000},{28.500,49.0000},{30.500,51.0000},{32.500,53.0000},{34.500,55.5000},{36.500,58.0000},{38.500,60.5000},{40.500,62.5000},{42.500,64.0000},{44.500,65.5000},{46.500,66.0000},{48.500,68.0000},{50.500,70.0000},{52.500,72.0000},{54.500,75.0000},{56.500,77.0000},{58.500,80.0000},{60.500,80.5000},{62.500,81.0000},{64.500,82.0000},{66.500,82.5000},{68.500,82.5000},{70.500,82.5000},{72.500,82.5000},{74.500,82.0000},{76.500,80.0000},{78.500,79.0000},{80.500,77.0000},{82.500,75.0000},{84.500,73.0000},{86.500,71.0000},{88.500,69.0000},{90.500,66.5000},{92.500,63.0000},{94.500,61.0000},{96.500,59.0000},{98.500,57.5000},{100.500,54.5000},{102.500,51.5000},{104.500,49.0000},{106.500,47.5000},{108.500,46.0000},{110.500,44.0000},{112.500,43.5000}}
caroliniana[4]={{0.500,24.0000},{2.500,25.5000},{4.500,25.5000},{6.500,26.0000},{8.500,27.0000},{10.500,29.0000},{12.500,30.0000},{14.500,32.0000},{16.500,33.0000},{18.500,35.0000},{20.500,37.0000},{22.500,39.5000},{24.500,43.0000},{26.500,45.0000},{28.500,47.0000},{30.500,49.0000},{32.500,52.0000},{34.500,56.0000},{36.500,58.0000},{38.500,63.5000},{40.500,69.5000},{42.500,73.5000},{44.500,77.0000},{46.500,79.0000},{48.500,81.0000},{50.500,82.5000},{52.500,82.5000},{54.500,82.5000},{56.500,82.5000},{58.500,82.0000},{60.500,80.5000},{62.500,18.5000},{62.500,79.0000},{64.500,77.0000},{66.500,73.5000},{68.500,71.0000},{70.500,64.5000},{72.500,60.5000},{74.500,57.5000},{76.500,54.0000},{78.500,51.0000},{80.500,49.0000},{82.500,44.5000},{84.500,41.5000},{86.500,39.0000},{88.500,36.0000},{90.500,34.0000},{92.500,32.5000},{94.500,30.0000},{96.500,28.0000},{98.500,27.0000},{100.500,25.0000},{102.500,24.5000},{104.500,23.0000},{106.500,21.0000},{108.500,19.5000},{110.500,19.5000},{112.500,19.5000},{114.500,18.5000}}
caroliniana[5]={{0.500,76.5000},{2.500,76.5000},{4.500,76.5000},{6.500,77.5000},{8.500,79.0000},{10.500,79.5000},{12.500,80.0000},{14.500,81.0000},{16.500,82.0000},{18.500,83.5000},{20.500,84.5000},{22.500,85.0000},{24.500,87.0000},{26.500,88.0000},{28.500,89.0000},{30.500,90.5000},{32.500,91.5000},{34.500,92.5000},{36.500,94.0000},{38.500,96.5000},{40.500,97.5000},{42.500,99.5000},{44.500,102.5000},{46.500,105.0000},{48.500,106.0000},{50.500,107.5000},{52.500,110.0000},{54.500,111.0000},{56.500,114.0000},{58.500,116.5000},{60.500,119.0000},{62.500,124.0000},{64.500,128.0000},{66.500,129.5000},{68.500,129.5000},{70.500,130.0000},{72.500,130.5000},{74.500,130.0000},{76.500,128.5000},{78.500,125.0000},{80.500,123.0000},{82.500,122.0000},{84.500,117.5000},{86.500,115.5000},{88.500,113.5000},{90.500,112.0000},{92.500,110.0000},{94.500,105.5000},{96.500,102.5000},{98.500,98.5000},{100.500,95.5000},{102.500,93.0000},{104.500,92.0000},{106.500,89.5000},{108.500,87.0000},{110.500,84.5000},{112.500,82.0000},{114.500,81.0000},{116.500,80.5000},{118.500,79.5000},{120.500,78.5000},{122.500,77.5000},{124.500,77.5000},{126.500,76.5000},{128.500,76.0000},{130.500,76.0000},{132.500,76.5000},{134.500,76.5000},{136.500,76.5000},{138.500,77.0000},{140.500,77.5000},{142.500,78.0000},{144.500,80.0000},{146.500,81.0000}}
caroliniana[6]={{0.500,57.5000},{2.500,57.5000},{4.500,57.5000},{6.500,57.5000},{8.500,57.5000},{10.500,58.5000},{12.500,58.5000},{14.500,59.5000},{16.500,60.0000},{18.500,60.5000},{20.500,62.0000},{22.500,64.0000},{24.500,65.0000},{26.500,67.0000},{28.500,70.0000},{30.500,72.0000},{32.500,74.0000},{34.500,76.0000},{36.500,78.5000},{38.500,81.5000},{40.500,84.0000},{42.500,87.0000},{44.500,91.0000},{46.500,95.0000},{48.500,99.0000},{50.500,103.5000},{52.500,106.0000},{54.500,108.0000},{56.500,109.0000},{58.500,109.5000},{60.500,109.5000},{62.500,108.5000},{64.500,108.0000},{66.500,106.5000},{68.500,104.0000},{70.500,100.5000},{72.500,95.5000},{74.500,89.5000},{76.500,84.5000},{78.500,81.5000},{80.500,79.0000},{82.500,77.5000},{84.500,75.5000},{86.500,72.5000},{88.500,71.0000},{90.500,69.0000},{92.500,67.0000},{94.500,66.0000},{96.500,65.0000},{98.500,63.5000},{100.500,62.0000},{102.500,60.5000},{104.500,59.0000},{106.500,58.5000},{108.500,58.5000},{110.500,58.5000},{112.500,58.0000},{114.500,57.5000},{116.500,58.0000},{118.500,58.5000},{120.500,58.5000},{122.500,58.5000}}

(*Poner aqui la familia*)
var=caroliniana;
(* el número de curvas estudiadas/semillas de la familia*)
m=6;
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[NMinValue[{curbe[j,t],0.30<t<0.7},t]];
vmed[j]=Abs[NIntegrate[curbe[j,t],{t,0.3,0.7}]/0.4];
dibcurbe[j]=Plot[{curbe[j,t],-vmed[j]},{t,0,1},PlotRange->{-0.2,0.1},DisplayFunction->Identity,PlotStyle->{RGBColor[0,1,0],{Dotted,Dashed}}];
total=GraphicsGrid[{
(*{spli,pp},*)
{dibez[j],dibcurbe[j]}},Spacings->0.3];
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,6}]];
Print["Valores medios de curvatura de cada ", var ," en [0.3,0.7]:  ", Table[vmed[j],{j,1,6}]];

Show[Table[dibcurbe[j],{j,1,6}]]

Table[NMinValue[{curbe[j,t],0.25<t<0.75},t],{j,1,6}]

dibcurbe[j],{j,1,6}

Table[dibcurbe[j],{j,1,6}]

curbe[j,t_]
dibcurbe[j]