[comp.graphics] bspline program

apollo@ecf.toronto.edu (Vince Pugliese) (11/30/88)

someone requested a bspline program a while back so here is
little piece of code , not very pretty, but it "seems" to work.
have fun with it please report any errors to me.

 here is the code:

/* program to fit a b-spline to an 
arbitrary number of points v.p.*/
#include <math.h>
#include <stdio.h>

#define TRUE -1
#define FALSE 0

typedef
struct
{
  float x,y,z;
}
point;

char file_name[32]; 
point control_points[100];
point spline_points[50000];
int index,run_len,temp_num;
float t,deltat;
int num_points,num;
int i,j;
FILE *handle;

main()
 { 

   
     printf("enter the name of the raw point data\n" );
     scanf("%s",file_name); 



  handle=fopen(file_name,"r"); 
      i= 0;
       while (
            fscanf(handle,"%f %f %f\n",&(control_points[i].x),&(control_points[i].y),&(control_points[i].z)) != EOF) i++;   

            deltat = 0.1;

          for(index=1;index<=(i-3);index++)                 
             for(t=0.0;t<1.0;t=t+deltat) 
             {
            spline_points[num].x=((-0.16667)*t*t*t + (0.5)*t*t + (-0.5)*t + 0.16667)*control_points[index-1].x
                              +((0.5)*t*t*t -t*t + 0.66667)*control_points[index].x
                              +((-0.5)*t*t*t + (0.5)*t*t + (0.5)*t + 0.16667)*control_points[index+1].x
                              +((0.16667)*t*t*t)*control_points[index+2].x ;
          
           spline_points[num].y=((-0.16667)*t*t*t + (0.5)*t*t - (0.5)*t + 0.16667)*control_points[index-1].y
                              +((0.5)*t*t*t -t*t + 0.66667)*control_points[index].y
                              +((-0.5)*t*t*t + (0.5)*t*t + (0.5)*t + 0.16667)*control_points[index+1].y
                              +((0.16667)*t*t*t)*control_points[index+2].y ;

           spline_points[num].z=((-0.16667)*t*t*t + (0.5)*t*t - (0.5)*t + 0.16667)*control_points[index-1].z
                              +((0.5)*t*t*t -t*t + 0.66667)*control_points[index].z
                              +((-0.5)*t*t*t + (0.5)*t*t + (0.5)*t + 0.16667)*control_points[index+1].z
                              +((0.16667)*t*t*t)*control_points[index+2].z ;

               num++;
             }
            printf("here is the spline data\n");
            printf("\n");
          for(j=0;j<num;j++)
         {
            printf("%f    %f   %f\n",spline_points[j].x,spline_points[j].y,spline_points[j].z);
          } 





 
 }

                                                vince pugliese

robbins@istsists.ca (John Robbins) (12/02/88)

	The bspline program presented takes an inputted set of control
points and generates a set of points using a bspline algorithm. It does
not 'fit a b_spline to an an arbitrary number of points'.
	I should be very interested to know of any code that does take a
set of data points and with the minimum amount of operator intervention
(eg specification of order of curve and number of vertices allowed)
generates the vertex coordinates of the b_spline curve that best fits
the data.

		John Robbins	Scintrex Ltd  Concord Ontario Canada

				Robbins@ISTS
 

stuart@rennet.cs.wisc.edu (Stuart Friedberg) (12/05/88)

In article <277@istsists.ca> robbins@istsists.ca (John Robbins) writes:
>	I should be very interested to know of any code that does take a
>set of data points and with the minimum amount of operator intervention

I have used the following source for both open and close cubic
B-splines.  The method also works for cubic rational B-splines.

O. Lozover & K. Preiss,
"Automatic Construction of a Cubic B-Spline Representation for a General Curve"
Computers and Graphics, Vol. 7, No. 2, pp. 149-153, 1983

It is usually a simple task to generate code from a published algorithm
or set of equations.  It is usually hopeless to try to find code that
does what you want, in the languages you want, using the data
representation you want, for the graphics devices you want.

Stu Friedberg  stuart@cs.wisc.edu