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






	(*******************************  S. frivaldskiana 1 to 6  *)

Needs["Splines`"]

frivaldskiana[1]={{0.3394,8.4861},{1.6972,8.4861},{3.0550,8.4861},{4.4128,8.4861},{5.7705,8.4861},{7.1283,9.1650},{8.4861,9.8439},{9.8439,10.8622},{11.2016,11.5411},{12.5594,12.5594},{13.9172,13.5777},{15.2749,14.9355},{16.6327,16.2933},{17.9905,16.9722},{19.3483,17.6511},{20.7060,18.3299},{22.0638,18.6694},{23.4216,19.3483},{24.7794,19.3483},{26.1371,19.6877},{27.4949,20.0272},{28.8527,20.0272},{30.2105,20.3666},{31.5682,20.7060},{32.9260,20.7060},{34.2838,20.7060},{35.6415,20.7060},{36.9993,20.7060},{38.3571,20.7060},{39.7149,20.7060},{41.0726,20.7060},{42.4304,20.7060},{43.7882,20.3666},{45.1460,20.0272},{46.5037,20.0272},{47.8615,19.3483},{49.2193,19.0088},{50.5771,18.6694},{51.9348,17.9905},{53.2926,17.6511},{54.6504,17.3116},{56.0081,16.6327},{57.3659,15.9538},{58.7237,15.2749},{60.0815,15.2749},{61.4392,14.5961},{62.7970,13.9172},{64.1548,13.5777},{65.5126,13.2383},{66.8703,12.5594},{68.2281,12.5594},{69.5859,12.5594},{70.9436,12.5594},{72.3014,12.5594},{73.6592,12.5594}}
frivaldskiana[2]={{0.3394,11.2016},{1.6972,11.2016},{3.0550,11.5411},{4.4128,11.8805},{5.7705,11.8805},{7.1283,12.5594},{8.4861,12.5594},{9.8439,13.2383},{11.2016,13.9172},{12.5594,14.5961},{13.9172,15.2749},{15.2749,15.9538},{16.6327,16.9722},{17.9905,17.6511},{19.3483,18.3299},{20.7060,19.0088},{22.0638,19.6877},{23.4216,20.0272},{24.7794,20.7060},{26.1371,20.7060},{27.4949,21.0455},{28.8527,21.3849},{30.2105,21.3849},{31.5682,21.3849},{32.9260,21.3849},{34.2838,21.3849},{35.6415,21.3849},{36.9993,21.3849},{38.3571,21.3849},{39.7149,21.3849},{41.0726,21.3849},{42.4304,21.3849},{43.7882,20.7060},{45.1460,20.7060},{46.5037,20.7060},{47.8615,20.3666},{49.2193,19.6877},{50.5771,19.0088},{51.9348,18.3299},{53.2926,17.6511},{54.6504,17.3116},{56.0081,16.6327},{57.3659,15.9538},{58.7237,15.9538},{60.0815,15.2749},{61.4392,14.5961},{62.7970,13.9172},{64.1548,13.2383},{65.5126,12.5594},{66.8703,12.2200},{68.2281,11.8805},{69.5859,11.2016},{70.9436,11.2016},{72.3014,10.5227},{73.6592,10.5227},{75.0170,10.5227},{76.3747,10.5227},{77.7325,11.2016},{79.0903,11.8805},{80.4481,12.2200}}
frivaldskiana[3]={{0.3394,18.6694},{1.6972,18.6694},{3.0550,19.3483},{4.4128,19.6877},{5.7705,20.3666},{7.1283,21.0455},{8.4861,21.3849},{9.8439,22.4033},{11.2016,23.0821},{12.5594,23.7610},{13.9172,24.7794},{15.2749,25.4582},{16.6327,26.1371},{17.9905,26.4766},{19.3483,26.8160},{20.7060,27.4949},{22.0638,27.8344},{23.4216,28.5132},{24.7794,29.1921},{26.1371,29.5316},{27.4949,30.2105},{28.8527,30.2105},{30.2105,30.8893},{31.5682,31.2288},{32.9260,31.5682},{34.2838,31.9077},{35.6415,32.2471},{36.9993,32.9260},{38.3571,32.9260},{39.7149,32.9260},{41.0726,32.9260},{42.4304,32.9260},{43.7882,32.9260},{45.1460,32.9260},{46.5037,32.2471},{47.8615,32.2471},{49.2193,31.9077},{50.5771,31.5682},{51.9348,31.5682},{53.2926,30.8893},{54.6504,30.5499},{56.0081,30.2105},{57.3659,29.5316},{58.7237,28.8527},{60.0815,28.5132},{61.4392,27.8344},{62.7970,27.4949},{64.1548,26.4766},{65.5126,25.7977},{66.8703,25.1188},{68.2281,23.7610},{69.5859,23.0821},{70.9436,21.7244},{72.3014,20.7060},{73.6592,20.0272},{75.0170,19.3483},{76.3747,18.6694},{77.7325,18.3299},{79.0903,17.9905},{80.4481,17.9905},{81.8058,17.9905}}
frivaldskiana[4]={{0.3394,14.25662},{1.6972,13.91718},{3.0550,13.91718},{4.4128,14.25662},{5.7705,14.59606},{7.1283,14.59606},{8.4861,15.27495},{9.8439,15.61439},{11.2016,15.95384},{12.5594,16.63272},{13.9172,16.97217},{15.2749,17.31161},{16.6327,17.65105},{17.9905,17.99050},{19.3483,18.32994},{20.7060,18.66938},{22.0638,19.34827},{23.4216,19.34827},{24.7794,20.02715},{26.1371,20.02715},{27.4949,20.36660},{28.8527,20.70604},{30.2105,20.70604},{31.5682,20.70604},{32.9260,20.70604},{34.2838,20.70604},{35.6415,20.70604},{36.9993,20.70604},{38.3571,20.70604},{39.7149,20.70604},{41.0726,20.36660},{42.4304,20.02715},{43.7882,20.02715},{45.1460,19.68771},{46.5037,19.34827},{47.8615,19.00883},{49.2193,18.66938},{50.5771,17.99050},{51.9348,17.31161},{53.2926,16.97217},{54.6504,16.29328},{56.0081,15.61439},{57.3659,15.27495},{58.7237,14.59606},{60.0815,13.91718},{61.4392,13.23829},{62.7970,12.89885},{64.1548,12.55940},{65.5126,12.21996},{66.8703,11.88052},{68.2281,11.88052}}
frivaldskiana[5]={{0.3394,19.0088},{1.6972,18.6694},{3.0550,18.6694},{4.4128,18.6694},{5.7705,18.6694},{7.1283,19.3483},{8.4861,19.3483},{9.8439,20.0272},{11.2016,20.3666},{12.5594,20.7060},{13.9172,21.0455},{15.2749,21.7244},{16.6327,22.4033},{17.9905,23.0821},{19.3483,23.7610},{20.7060,24.4399},{22.0638,25.1188},{23.4216,25.4582},{24.7794,25.7977},{26.1371,26.4766},{27.4949,26.8160},{28.8527,27.1555},{30.2105,27.4949},{31.5682,27.4949},{32.9260,27.8344},{34.2838,28.1738},{35.6415,28.1738},{36.9993,28.1738},{38.3571,28.1738},{39.7149,28.1738},{41.0726,28.1738},{42.4304,28.1738},{43.7882,28.1738},{45.1460,28.1738},{46.5037,27.4949},{47.8615,27.4949},{49.2193,27.4949},{50.5771,27.1555},{51.9348,26.8160},{53.2926,26.4766},{54.6504,26.1371},{56.0081,25.4582},{57.3659,25.1188},{58.7237,24.4399},{60.0815,23.7610},{61.4392,23.4216},{62.7970,22.7427},{64.1548,22.0638},{65.5126,21.7244},{66.8703,21.3849},{68.2281,20.7060},{69.5859,20.3666},{70.9436,20.0272},{72.3014,19.3483},{73.6592,18.6694},{75.0170,17.9905},{76.3747,17.3116},{77.7325,16.9722},{79.0903,16.6327},{80.4481,16.6327},{81.8058,16.6327},{83.1636,16.6327},{84.5214,16.6327},{85.8792,16.6327},{87.2369,17.3116}}
frivaldskiana[6]={{0.3394,9.8439},{1.6972,9.8439},{3.0550,9.1650},{4.4128,9.1650},{5.7705,9.1650},{7.1283,9.1650},{8.4861,9.1650},{9.8439,9.8439},{11.2016,9.8439},{12.5594,10.1833},{13.9172,10.5227},{15.2749,10.8622},{16.6327,11.8805},{17.9905,12.5594},{19.3483,13.2383},{20.7060,13.9172},{22.0638,14.5961},{23.4216,15.2749},{24.7794,15.9538},{26.1371,16.6327},{27.4949,17.3116},{28.8527,17.6511},{30.2105,17.9905},{31.5682,18.6694},{32.9260,18.6694},{34.2838,19.3483},{35.6415,19.3483},{36.9993,19.3483},{38.3571,19.6877},{39.7149,20.0272},{41.0726,20.0272},{42.4304,20.0272},{43.7882,20.0272},{45.1460,20.0272},{46.5037,20.0272},{47.8615,20.0272},{49.2193,20.0272},{50.5771,19.3483},{51.9348,19.3483},{53.2926,18.6694},{54.6504,18.6694},{56.0081,17.9905},{57.3659,17.3116},{58.7237,16.6327},{60.0815,16.2933},{61.4392,15.6144},{62.7970,14.9355},{64.1548,13.9172},{65.5126,13.2383},{66.8703,12.5594},{68.2281,11.8805},{69.5859,11.2016},{70.9436,10.5227},{72.3014,9.8439},{73.6592,9.1650},{75.0170,8.4861},{76.3747,7.8072},{77.7325,7.1283},{79.0903,7.1283},{80.4481,6.4494},{81.8058,6.4494},{83.1636,6.4494},{84.5214,6.4494},{85.8792,6.7889},{87.2369,7.1283}}

(*Poner aqui la familia*)
var=frivaldskiana;
(* 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.1,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]