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






	(*******************************  Miguel de Arco*)

Needs["Splines`"]

Migueldearco[1]={{2.30179,7.25703},{2.36573,6.68159},{2.42967,6.42583},{2.49361,6.23402},{2.55754,6.0422},{2.62148,5.88235},{2.68542,5.72251},{2.74936,5.56266},{2.8133,5.40281},{2.87724,5.27494},{2.94118,5.17903},{3.00512,5.05115},{3.06905,4.92327},{3.13299,4.7954},{3.19693,4.66752},{3.26087,4.57161},{3.32481,4.44373},{3.38875,4.31586},{3.45269,4.21995},{3.51662,4.09207},{3.58056,3.96419},{3.6445,3.83632},{3.70844,3.6445},{3.77238,3.42072},{3.83632,3.26087},{3.90026,3.06905},{3.96419,2.84527},{4.02813,2.68542},{4.09207,2.58951},{4.15601,2.52558},{4.21995,2.49361},{4.28389,2.46164},{4.34783,2.42967},{4.41176,2.3977},{4.4757,2.3977},{4.53964,2.42967},{4.60358,2.42967},{4.66752,2.42967},{4.73146,2.42967},{4.7954,2.42967},{4.85933,2.42967},{4.92327,2.42967},{4.98721,2.46164},{5.05115,2.52558},{5.11509,2.71739},{5.17903,3.06905},{5.24297,3.32481},{5.30691,3.48465},{5.37084,3.67647},{5.43478,3.86829},{5.49872,4.0601},{5.56266,4.25192},{5.6266,4.41176},{5.69054,4.53964},{5.75448,4.63555},{5.81841,4.73146},{5.88235,4.85933},{5.94629,4.98721},{6.01023,5.11509},{6.07417,5.27494},{6.13811,5.46675},{6.20205,5.6266},{6.26598,5.78645},{6.32992,5.97826},{6.39386,6.23402},{6.4578,6.68159},{6.52174,7.09719},{6.4578,7.48082},{6.39386,7.86445},{6.32992,8.08823},{6.26598,8.28005},{6.20205,8.40793},{6.13811,8.50384},{6.07417,8.59974},{6.01023,8.63171},{5.94629,8.66368},{5.88235,8.72762},{5.81841,8.75959},{5.75448,8.79156},{5.69054,8.8555},{5.6266,8.88747},{5.56266,8.91944},{5.49872,8.95141},{5.43478,8.95141},{5.37084,8.98338},{5.30691,9.01535},{5.24297,9.01535},{5.17903,9.01535},{5.11509,9.04731},{5.05115,9.07928},{4.98721,9.07928},{4.92327,9.07928},{4.85933,9.04731},{4.7954,9.01535},{4.73146,9.01535},{4.66752,9.01535},{4.60358,9.01535},{4.53964,9.01535},{4.4757,9.01535},{4.41176,8.98338},{4.34783,8.95141},{4.28389,8.95141},{4.21995,8.95141},{4.15601,8.95141},{4.09207,8.98338},{4.02813,9.01535},{3.96419,9.01535},{3.90026,9.01535},{3.83632,9.01535},{3.77238,9.01535},{3.70844,9.01535},{3.6445,9.04731},{3.58056,9.07928},{3.51662,9.07928},{3.45269,9.04731},{3.38875,9.01535},{3.32481,9.01535},{3.26087,9.01535},{3.19693,8.98338},{3.13299,8.95141},{3.06905,8.91944},{3.00512,8.88747},{2.94118,8.8555},{2.87724,8.79156},{2.8133,8.72762},{2.74936,8.66368},{2.68542,8.59974},{2.62148,8.53581},{2.55754,8.4399},{2.49361,8.28005},{2.42967,8.08823},{2.36573,7.83248}}
Migueldearco[2]={{2.87724,7.51279},{2.94118,7.73657},{3.00512,8.34399},{3.06905,8.56777},{3.13299,8.79156},{3.19693,8.95141},{3.26087,9.07928},{3.32481,9.20716},{3.38875,9.30307},{3.45269,9.36701},{3.51662,9.43095},{3.58056,9.46292},{3.6445,9.49488},{3.70844,9.55882},{3.77238,9.59079},{3.83632,9.59079},{3.90026,9.62276},{3.96419,9.65473},{4.02813,9.65473},{4.09207,9.65473},{4.15601,9.65473},{4.21995,9.65473},{4.28389,9.65473},{4.34783,9.65473},{4.41176,9.65473},{4.4757,9.6867},{4.53964,9.71867},{4.60358,9.71867},{4.66752,9.71867},{4.73146,9.75064},{4.7954,9.78261},{4.85933,9.81458},{4.92327,9.84655},{4.98721,9.84655},{5.05115,9.81458},{5.11509,9.78261},{5.17903,9.78261},{5.24297,9.78261},{5.30691,9.78261},{5.37084,9.78261},{5.43478,9.78261},{5.49872,9.75064},{5.56266,9.71867},{5.6266,9.71867},{5.69054,9.6867},{5.75448,9.62276},{5.81841,9.59079},{5.88235,9.59079},{5.94629,9.62276},{6.01023,9.65473},{6.07417,9.65473},{6.13811,9.62276},{6.20205,9.59079},{6.26598,9.59079},{6.32992,9.59079},{6.39386,9.55882},{6.4578,9.52685},{6.52174,9.49488},{6.58568,9.43095},{6.64962,9.39898},{6.71355,9.36701},{6.77749,9.30307},{6.84143,9.23913},{6.90537,9.17519},{6.96931,9.11125},{7.03325,8.98338},{7.09719,8.75959},{7.16113,8.50384},{7.22506,8.05627},{7.289,7.44885},{7.22506,7.03325},{7.16113,6.77749},{7.09719,6.55371},{7.03325,6.39386},{6.96931,6.26598},{6.90537,6.13811},{6.84143,6.01023},{6.77749,5.88235},{6.71355,5.78645},{6.64962,5.69054},{6.58568,5.59463},{6.52174,5.49872},{6.4578,5.37084},{6.39386,5.27494},{6.32992,5.211},{6.26598,5.11509},{6.20205,5.01918},{6.13811,4.92327},{6.07417,4.82737},{6.01023,4.73146},{5.94629,4.53964},{5.88235,4.28389},{5.81841,4.09207},{5.75448,3.54859},{5.69054,3.42072},{5.6266,3.32481},{5.56266,3.2289},{5.49872,3.13299},{5.43478,3.03708},{5.37084,3.00512},{5.30691,2.97315},{5.24297,2.94118},{5.17903,2.90921},{5.11509,2.87724},{5.05115,2.87724},{4.98721,2.87724},{4.92327,2.87724},{4.85933,2.87724},{4.7954,2.87724},{4.73146,2.90921},{4.66752,2.94118},{4.60358,2.97315},{4.53964,3.06905},{4.4757,3.26087},{4.41176,3.48465},{4.34783,3.70844},{4.28389,3.86829},{4.21995,3.96419},{4.15601,4.12404},{4.09207,4.3798},{4.02813,4.63555},{3.96419,4.85933},{3.90026,5.05115},{3.83632,5.17903},{3.77238,5.27494},{3.70844,5.37084},{3.6445,5.46675},{3.58056,5.56266},{3.51662,5.69054},{3.45269,5.78645},{3.38875,5.88235},{3.32481,6.0422},{3.26087,6.26598},{3.19693,6.4578},{3.13299,6.58568},{3.06905,6.77749},{3.00512,7.06522}}
Migueldearco[3]={{3.06905,7.41688},{3.13299,6.71355},{3.19693,6.39386},{3.26087,6.20205},{3.32481,6.07417},{3.38875,5.94629},{3.45269,5.81841},{3.51662,5.69054},{3.58056,5.56266},{3.6445,5.46675},{3.70844,5.37084},{3.77238,5.24297},{3.83632,5.14706},{3.90026,5.05115},{3.96419,4.95524},{4.02813,4.85933},{4.09207,4.76343},{4.15601,4.69949},{4.21995,4.63555},{4.28389,4.53964},{4.34783,4.41176},{4.41176,4.25192},{4.4757,4.0601},{4.53964,3.83632},{4.60358,3.61253},{4.66752,3.45269},{4.73146,3.38875},{4.7954,3.35678},{4.85933,3.32481},{4.92327,3.29284},{4.98721,3.26087},{5.05115,3.2289},{5.11509,3.13299},{5.17903,3.06905},{5.24297,3.10102},{5.30691,3.16496},{5.37084,3.19693},{5.43478,3.19693},{5.49872,3.2289},{5.56266,3.26087},{5.6266,3.26087},{5.69054,3.29284},{5.75448,3.35678},{5.81841,3.61253},{5.88235,3.96419},{5.94629,4.15601},{6.01023,4.31586},{6.07417,4.50767},{6.13811,4.73146},{6.20205,4.95524},{6.26598,5.11509},{6.32992,5.211},{6.39386,5.30691},{6.4578,5.43478},{6.52174,5.53069},{6.58568,5.6266},{6.64962,5.75448},{6.71355,5.88235},{6.77749,6.01023},{6.84143,6.13811},{6.90537,6.29795},{6.96931,6.4578},{7.03325,6.61765},{7.09719,6.8734},{7.16113,7.35294},{7.22506,7.51279},{7.22506,7.80051},{7.16113,8.1202},{7.09719,8.37596},{7.03325,8.53581},{6.96931,8.69565},{6.90537,8.82353},{6.84143,8.95141},{6.77749,9.04731},{6.71355,9.11125},{6.64962,9.17519},{6.58568,9.23913},{6.52174,9.30307},{6.4578,9.33504},{6.39386,9.36701},{6.32992,9.39898},{6.26598,9.43095},{6.20205,9.46292},{6.13811,9.49488},{6.07417,9.52685},{6.01023,9.52685},{5.94629,9.52685},{5.88235,9.52685},{5.81841,9.52685},{5.75448,9.52685},{5.69054,9.52685},{5.6266,9.52685},{5.56266,9.52685},{5.49872,9.52685},{5.43478,9.52685},{5.37084,9.52685},{5.30691,9.49488},{5.24297,9.46292},{5.17903,9.49488},{5.11509,9.49488},{5.05115,9.46292},{4.98721,9.46292},{4.92327,9.46292},{4.85933,9.46292},{4.7954,9.46292},{4.73146,9.46292},{4.66752,9.46292},{4.60358,9.46292},{4.53964,9.46292},{4.4757,9.46292},{4.41176,9.46292},{4.34783,9.46292},{4.28389,9.46292},{4.21995,9.43095},{4.15601,9.39898},{4.09207,9.36701},{4.02813,9.30307},{3.96419,9.2711},{3.90026,9.23913},{3.83632,9.20716},{3.77238,9.17519},{3.70844,9.11125},{3.6445,9.07928},{3.58056,9.04731},{3.51662,8.98338},{3.45269,8.88747},{3.38875,8.75959},{3.32481,8.63171},{3.26087,8.50384},{3.19693,8.31202},{3.13299,8.05627},{3.19693,8.31202}}

(*Poner aqui la familia*)
var=Migueldearco;
(* 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->{-10,10},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}]];