|
![](/i/fill.gif) |
"W?odzimierz ABX Skiba" <abx### [at] babilon org> wrote in message
news:d091eu092vqvbnn84edd8h1hv63t6bd27h@4ax.com...
> void Compute_Min_Max_B_Spline(DBL p0,p1,p2,p3,*minp,*maxp)
> {
> DBL a,b,c,delta;
>
> // first derivative of spline equation is
> // 1/2*(-p0+3*p1-3*p2+p3)*t^2+(p0-2*p1+p2)*t-1/2*p0+1/2*p2
>
> a=1/2*(-p0+3*p1-3*p2+p3);
> b=p0-2*p1+p2;
> c=(-p0+p2)/2;
> d=b*b-4*a*c;
> if ((d<0)||(a==0)) // for unexpected errors return the biggest bbox
> {
> t1=0;
> t2=1;
> }
> else
> {
> // parameter for extremas
> t1=(-b-sqrt(d))/(2*a);
> t2=(-b+sqrt(d))/(2*a);
> // parameter should be clipped to visible part of segment
> t1=min(max(t1,0),1);
> t2=min(max(t2,0),1);
> }
> t1 = Compute_B_Spline_Value(t1);
> t2 = Compute_B_Spline_Value(t2);
> minp = min( t1, t2 );
> maxp = max( t1, t2 );
> }
>
> DBL Compute_Catmull_Rom_Spline_Value(DBL t)
> {
> // should be best to code this using already defined Catmull_Rom_Matrix
> return
((2*p1)+(-p0+p2)*t+(2*p0-5*p1+4*p2-p3)*t^2+(-p0+3*p1-3*p2+p3)*t^3)/2;
> }
>
> void Compute_Min_Max_Catmull_Rom_Spline(DBL p0,p1,p2,p3,*minp,*maxp)
> {
> DBL a,b,c,delta;
>
> // first derivative of spline equation is
> // -1/2*p0+1/2*p2+(2*p0-5*p1+4*p2-p3)*t+3/2*(-p0+3*p1-3*p2+p3)*t^2
>
> a=3*(-p0+3*p1-3*p2+p3)/2;
> b=2*p0-5*p1+4*p2-p3;
> c=(-p0+p2)/2;
> d=b*b-4*a*c;
> if ((d<0)||(a==0)) // for unexpected errors return the biggest bbox
> {
> t1=0;
> t2=1;
> }
> else
> {
> // parameter for extremas
> t1=(-b-sqrt(d))/(2*a);
> t2=(-b+sqrt(d))/(2*a);
> // parameter should be clipped to visible part of segment
> t1=min(max(t1,0),1);
> t2=min(max(t2,0),1);
> }
> t1 = Compute_Catmull_Rom_Spline_Value(t1);
> t2 = Compute_Catmull_Rom_Spline_Value(t2);
> minp = min( t1, t2 );
> maxp = max( t1, t2 );
> }
Two remarks:
1) the only difference between procedures Compute_Min_Max_B_Spline and
Compute_Min_Max_Catmull_Rom_Spline is their polynomial coefficients a,b,c,d,
maybe it's better to put this common part in a separate procedure;
2) the curve can possibly reach its min/max at its ends, when t=0 and t=1.
Regards,
Gleb
Post a reply to this message
|
![](/i/fill.gif) |