I know how to do bicubic patch from 16 control points ( i.e. 4x4 grid) like B-spline, Catmull–Rom, Besier ...
However, I would like rather something like 2D/3D analogy to Hermite spline described by 4 control points (p00,p01,p10,p11
) and 4 normals (n00,n01,n10,n11
) at that points.
I tried to program it ( first doing 2x 1D interpolation along u
and than interpolating result along v
).
I got stuck when I realized that I don't know how to use surface normals instead of derivatives used in 1D Hermite spline.
If I naively just plug normals instead of derivatives I get nonsense results. See the C++ code here ( I took just the relevatn parts ):
// basis function
template <class TYPE>
inline void spline_hermite_basis( TYPE x, TYPE& c0, TYPE& c1, TYPE& d0, TYPE& d1 ){
TYPE x2 = x*x;
TYPE x3 = x*x2;
c0 = 2*x3 - 3*x2 + 1;
c1 = -2*x3 + 3*x2 ;
d0 = x3 - 2*x2 + x;
d1 = x3 - x2 ;
};
// derivative of basis functions
template <class TYPE>
inline void dspline_hermite_basis( TYPE x, TYPE& c0, TYPE& c1, TYPE& d0, TYPE& d1 ){
TYPE x2 = x*x;
c0 = 6*x2 - 6*x ;
c1 = -6*x2 + 6*x ;
d0 = 3*x2 - 4*x + 1;
d1 = 3*x2 - 2*x ;
};
void cubicPatch_point( double u, double v,
const Vec3d& p00, const Vec3d& p01, const Vec3d& p10, const Vec3d& p11,
const Vec3d& n00, const Vec3d& n01, const Vec3d& n10, const Vec3d& n11,
Vec3d& p, Vec3d& n
){
double cc0,cc1,cd0,cd1;
double dc0,dc1,dd0,dd1;
// interpolation along u
spline_hermite_basis<double>( u, cc0, cc1, cd0, cd1 );
dspline_hermite_basis<double>( u, dc0, dc1, dd0, dd1 );
Vec3d p0u,p1u,n0u,n1u;
p0u.set_mul( p00, cc0 ); p0u.add_mul( p01, cc1 ); p0u.add_mul( n00, cd0 ); p0u.add_mul( n01, cd1 ); // p0u = cc0*p00 + cc1*p01 + cd0*n00 + cd1*n01;
n0u.set_mul( p00, dc0 ); n0u.add_mul( p01, dc1 ); n0u.add_mul( n00, dd0 ); n0u.add_mul( n01, dd1 ); // n0u = dc0*p00 + dc1*p01 + dd0*n00 + dd1*n01;
p1u.set_mul( p10, cc0 ); p1u.add_mul( p11, cc1 ); p1u.add_mul( n10, cd0 ); p1u.add_mul( n11, cd1 ); // p1u = cc0*p10 + cc1*p11 + cd0*n10 + cd1*n11;
n1u.set_mul( p10, dc0 ); n1u.add_mul( p11, dc1 ); n1u.add_mul( n10, dd0 ); n1u.add_mul( n11, dd1 ); // n1u = dc0*p10 + dc1*p11 + dd0*n10 + dd1*n11;
// interpolation along v
spline_hermite_basis<double>( v, cc0, cc1, cd0, cd1 );
dspline_hermite_basis<double>( v, dc0, dc1, dd0, dd1 );
p.set_mul( p0u, cc0 ); p.add_mul( p1u, cc1 ); p.add_mul( n0u, cd0 ); p.add_mul( n1u, cd1 ); // p = cc0*p0u + cc1*p1u + cd0*n0u + cd1*n1u;
n.set_mul( p0u, dc0 ); n.add_mul( p1u, dc1 ); n.add_mul( n0u, dd0 ); n.add_mul( n1u, dd1 ); // n = dc0*p0u + dc1*p1u + dd0*n0u + dd1*n1u;
}