forked from markjessell/functionNoddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3dSpline.c
110 lines (98 loc) · 2.71 KB
/
3dSpline.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*****************************************************************************
* File: 3dBSplineCurves.c
*
* Purpose: Generates Cubic BSpline curves
*
* © 1989 Mark M. Owen. All rights reserved.
*****************************************************************************
*/
#include "3dSpline.h"
#define BSplineValue(t,t2,t3,CC)\
( ( (CC)->a * (t3)\
+ (CC)->b * (t2)\
+ (CC)->c * (t)\
+ (CC)->d\
) / 6.0\
)
#define BSCOEF(v)\
d1 = (double) (p[i-1].v);\
d13 = d1*3;\
d2 = (double) (p[i ].v);\
d3 = (double) (p[i+1].v);\
d33 = d3*3;\
c.v.a =-d1 +3*d2-d33+(double) (p[i+2].v);\
c.v.b = d13-6*d2+d33;\
c.v.c =-d13 +d33;\
c.v.d = d1 +4*d2+d3;
/*****************************************************************************
*
* Function: BSpline3d(É)
*
* Purpose: Generates a three dimensional Cubic BSpline curve's vertices
* from a supplied set of control points. When sucessfull, the
* number and an array of generated curve vertices are returned
* to the caller.
*
* Returns: MemError() if memory allocation fails
* BS_TOO_FEW_POINTS if < 5 control points are supplied
* noErr if successfull
*
*****************************************************************************
*/
int BSpline3d(nSteps,nPts,p,nP,ppP)
int nSteps; /* number of segments between control points */
int nPts; /* number of input control points */
Point3d _huge *p; /* array of input control points */
int *nP; /* returned number of generated curve vertices */
Point3d _huge **ppP; /* returned array of generated curve vertices */
{
int i,j,k;
BSC c;
FPType t,t2,t3, dt, x,y,z;
FPType d1,d13,d2,d3,d33;
Point3d pt;
/* Generation of a BSpline curve requires at least five control
* points, so verify that at least that many have been supplied.
*/
if (nPts< 5)
return FALSE;
/* Allocate memory for the returned array of generated vertices
*/
*ppP = (Point3d _huge *)xvt_mem_halloc((nSteps+1) * (nPts-3), sizeof(Point3d));
if( !*ppP )
return FALSE;
dt = 1.0 / nSteps;
k = 0;
for (i=1; i<nPts-2; i++) /* 2..N-2 */
{
/* Generate the curve coefficients for x, y, and z in this
* portion of the spline
*/
BSCOEF(x);
BSCOEF(y);
BSCOEF(z);
/* Generate the vertices for this portion of the spline
*/
for (j=0; j<= nSteps; j++)
{
t = j * dt;
t2 = t * t;
t3 = t2 * t;
x = BSplineValue(t,t2,t3,&c.x);
y = BSplineValue(t,t2,t3,&c.y);
z = BSplineValue(t,t2,t3,&c.z);
pt.x = x2fix(&x);
pt.y = x2fix(&y);
pt.z = x2fix(&z);
/* Stash the generated vertex in the return array
*/
(*ppP)[k++] = pt;
}
}
/* return the number of vertices generated
*/
*nP = k;
/* Indicate success
*/
return TRUE;
}