| Bsoft 2.0.5 Bernard's software package

spline.cpp
Go to the documentation of this file.
1
9 #include "Vector3.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
15 // Internal function prototype
16 Vector3<double> vector3_catmull_rom_interpolate(Vector3<double>* pnt, double t);
17
33 Vector3<double>* vector3_catmull_rom_spline(long ncoord, Vector3<double>* coords, long& nspline)
34 {
35 if ( ncoord < 1 ) return NULL;
36
37 long i, ns;
38 double d, s, t;
39 Vector3<double> dx;
40
41 // Calculate the length of the spline curve in voxels
42 d = 0;
43 for ( i=1; i<ncoord; i++ )
44 d += (coords[i] - coords[i-1]).length();
45 nspline = (long) (d+1);
46
47 Vector3<double>* spline = new Vector3<double>[nspline];
48
49 // Calculate the spline curve by interpolation
50 ns = 0;
51 t = 0;
52 for ( i=1; i<ncoord; i++ ) {
53 dx = coords[i] - coords[i-1];
54 d = dx.normalize();
55 if ( i==1 || i==ncoord-1 ) {
56 for ( s=t; s<d; s+=1 ) {
57 spline[ns] = (dx * s) + coords[i-1];
58 ns++;
59 }
60 t = s - d;
61 } else {
62 for ( s=t; s<d; s+=1 ) {
63 spline[ns] = vector3_catmull_rom_interpolate(&coords[i-2], s/d);
64 ns++;
65 }
66 t = s - d;
67 }
68 }
69
70 return spline;
71 }
72
73 double catmull_rom_interpolate(double* pnt, double t)
74 {
75 double s = 1.0 - t;
76 double t2 = t * t;
77 double t3 = t2 * t;
78 double intpnt;
79
80 intpnt = 0.5 * ( -t*s*s*pnt[0] + ( 2 - 5*t2 + 3*t3 ) * pnt[1] +
81 t * ( 1 + 4*t - 3*t2 ) * pnt[2] - t2*s*pnt[3] );
82
83 return intpnt;
84 }
85
86 Vector3<double> vector3_catmull_rom_interpolate(Vector3<double>* pnt, double t)
87 {
88 double s = 1.0 - t;
89 double t2 = t * t;
90 double t3 = t2 * t;
91 double c0 = -t*s*s;
92 double c1 = 2 - 5*t2 + 3*t3;
93 double c2 = t * ( 1 + 4*t - 3*t2 );
94 double c3 = t2*s;
95 Vector3<double> intpnt;
96
97 intpnt[0] = 0.5 * ( c0*pnt[0][0] + c1*pnt[1][0] + c2*pnt[2][0] - c3*pnt[3][0] );
98 intpnt[1] = 0.5 * ( c0*pnt[0][1] + c1*pnt[1][1] + c2*pnt[2][1] - c3*pnt[3][1] );
99 intpnt[2] = 0.5 * ( c0*pnt[0][2] + c1*pnt[1][2] + c2*pnt[2][2] - c3*pnt[3][2] );
100
101 return intpnt;
102 }
103
Vector3::normalize
double normalize()
Definition: Vector3.h:236
Vector3::length
double length()
Definition: Vector3.h:222
utilities.h
Header file for general utilities.
catmull_rom_interpolate
double catmull_rom_interpolate(double *pnt, double t)
Definition: spline.cpp:73
verbose
int verbose
vector3_catmull_rom_spline
Vector3< double > * vector3_catmull_rom_spline(long ncoord, Vector3< double > *coords, long &nspline)
Calculates a 2D/3D spline curve using the Catmull-Rom algorithm.
Definition: spline.cpp:33
Vector3< double >
Vector3.h
Class for 3-value vectors.
vector3_catmull_rom_interpolate
Vector3< double > vector3_catmull_rom_interpolate(Vector3< double > *pnt, double t)
Definition: spline.cpp:86