スプライン
Vd3A bezier (const Vd3& q0,const Vd3& q1,const Vd3& q2,const Vd3& q3,const long div_c)
{
Vd3A vd3a ;
for (long index=0 ; index<=div_c ; index++) {
double t = 1./div_c*index ;
double b0 = (1.-t)*(1.-t)*(1.-t) ;
double b1 = 3.* t *(1.-t)*(1.-t) ;
double b2 = 3.*(1.-t)*t*t ;
double b3 = t*t*t ;
double px = (b0*q0.x) + (b1*q1.x) + (b2*q2.x) + (b3*q3.x) ;
double py = (b0*q0.y) + (b1*q1.y) + (b2*q2.y) + (b3*q3.y) ;
vd3a.push_back(Vd3(px,py,0)) ;
}
return vd3a ;
}
Vd3A b_spline (const Vd3& q0,const Vd3& q1,const Vd3& q2,const Vd3& q3,const long div_c)
{
Vd3A vd3a ;
for (long index=0 ; index<=div_c ; index++) {
double t = 1./div_c*index ;
double n0 = 1./6 * (1.-t)*(1.-t)*(1.-t) ;
double n1 = 1./2 * t*t*t - t*t + 2./3 ;
double n2 =-1./2 * t*t*t + 1./2 * t*t + 1./2 *t + 1./6 ;
double n3 = 1./6 * t*t*t ;
double px = (n0*q0.x) + (n1*q1.x) + (n2*q2.x) + (n3*q3.x) ;
double py = (n0*q0.y) + (n1*q1.y) + (n2*q2.y) + (n3*q3.y) ;
vd3a.push_back(Vd3(px,py,0)) ;
}
return vd3a ;
}
q0 から q3 までを与えての座標の生成部分
{
#define div_n 5
{
vd3_b = ::bezier(q0,q1,q2,q3,div_n+11) ;
}
{
Vd3A vd_01 = ::b_spline(q0,q0,q1,q2,div_n) ;
Vd3A vd_12 = ::b_spline(q0,q1,q2,q3,div_n) ;
Vd3A vd_23 = ::b_spline(q1,q2,q3,q3,div_n) ;
vd3_n.insert(vd3_n.end(),vd_01.begin(),vd_01.end()) ;
vd3_n.insert(vd3_n.end(),vd_12.begin(),vd_12.end()) ;
vd3_n.insert(vd3_n.end(),vd_23.begin(),vd_23.end()) ;
}
}
カーディナルスプライン
Vd3A cardinal (const Vd3& q0,const Vd3& q1,const Vd3& q2,const Vd3& q3,const long div_c,double av=0.5)
{
Vd3A vd3a ;
for (long index=0 ; index<=div_c ; index++) {
double t = 1./div_c*index ;
double c0 = 2. * t*t*t - 3.*t*t + 1. ;
double c1 =-2. * t*t*t + 3.*t*t ;
double c2 = t*t*t - 2. * t*t + t ;
double c3 = t*t*t - t*t ;
Vd3 d1 = (q2-q0)*av ;
Vd3 d2 = (q3-q1)*av ;
double px = (c0*q1.x) + (c1*q2.x) + (c2*d1.x) + (c3*d2.x) ;
double py = (c0*q1.y) + (c1*q2.y) + (c2*d1.y) + (c3*d2.y) ;
vd3a.push_back(Vd3(px,py,0)) ;
}
return vd3a ;
}
{
Vd3A vd_01 = ::cardinal(q0,q0,q1,q2,div_n*5) ;
Vd3A vd_12 = ::cardinal(q0,q1,q2,q3,div_n*5) ;
Vd3A vd_23 = ::cardinal(q1,q2,q3,q3,div_n*5) ;
vd3_c.insert(vd3_c.end(),vd_01.begin(),vd_01.end()) ;
vd3_c.insert(vd3_c.end(),vd_12.begin(),vd_12.end()) ;
vd3_c.insert(vd3_c.end(),vd_23.begin(),vd_23.end()) ;
}
参考にさせてもらった所
http://afsoft.jp/cad/cad/024.html