centripetal parametrization for curve fitting or interpolation

very useful to compute parameter for curve fitting or interpolation
the detail explanation is written here


and here is the code for three dimensional parametric curve


 void GetCentripetal_Param(  
       int ncp,                                                  //number of control point  
       double point[][3],                                        //control points of curve  
       double t[]                                                //array of t parameter (output) 
       )  
 {  
      double *dcp = (double*)malloc(sizeof(double) * (ncp-1));   //CP0-CP1, CP2-CP1, ..., CP[ncp-1][ncp]  
      double *sum = (double*)malloc(sizeof(double) * ncp);  
             
      int i, j;  
      double dcptotal = 0.0;  
      for(i=0; i<ncp-1; i++)  
           dcp[i] = 0.0;  
        
      sum[0] = 0.0;  
      for(i=0; i<ncp-1; i++)  
      {  
        for(j=0; j<3; j++)  
          dcp[i] += (point[i+1][j]-point[i][j]) * (point[i+1][j]-point[i][j]);        
       
        dcp[i] = sqrt(sqrt(dcp[i]));  
        dcptotal += dcp[i];  
        sum[i+1] = dcptotal;  
      }  
      sum[0] = 0.0;  
      double inv_dcptotal = 1 / dcptotal;  
      for(i=0; i<ncp; i++)  
         t[i] = (sum[i] * inv_dcptotal);  
        
      if(dcp) free(dcp);  
      if(sum) free(sum);  
 }