File: spline.cpp

package info (click to toggle)
freespace2 3.7.4%2Brepack-1.1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye
  • size: 22,268 kB
  • sloc: cpp: 393,535; ansic: 4,106; makefile: 1,091; xml: 181; sh: 137
file content (308 lines) | stat: -rw-r--r-- 6,118 bytes parent folder | download | duplicates (4)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/ 



#include "globalincs/alphacolors.h"
#include "graphics/2d.h"
#include "math/spline.h"
#include "render/3d.h"



// -------------------------------------------------------------------------------------------------
// SPLINE DEFINES/VARS
//


// -------------------------------------------------------------------------------------------------
// SPLINE FUNCTIONS
//

static float bez_fact_lookup[13] = {
	1.0f,          // 0!
	1.0f,          // 1!
	2.0f,          // 2!
	6.0f,          // 3!
	24.0f,         // 4!
	120.0f,        // 5!
	720.0f,        // 6!
	5040.0f,       // 7!
	40320.0f,      // 8!
	362880.0f,     // 9!
	3628800.0f,    // 10!
	39916800.0f,   // 11!
	479001600.0f,  // 12!
};

// Limited Factorial
static float bez_fact(int n)
{
	Assert((n >= 0) && (n <= 12));

	return bez_fact_lookup[n];
}

// bez constructor
bez_spline::bez_spline()
{
	int idx;

	// zero all points
	for(idx=0; idx<MAX_BEZ_PTS; idx++){
		pts[idx] = vmd_zero_vector;		
	}
	num_pts = 0;
}

// bez constructor
bez_spline::bez_spline(int _num_pts, vec3d *_pts[MAX_BEZ_PTS])
{
	bez_set_points(_num_pts, _pts);	
}

// set control points
void bez_spline::bez_set_points(int _num_pts, vec3d *_pts[MAX_BEZ_PTS])
{
	int idx;

	// store the points
	num_pts = _num_pts;
	for(idx=0; idx<_num_pts; idx++){
		Assert(_pts[idx] != NULL);
		if(_pts[idx] != NULL){
			pts[idx] = *_pts[idx];
		}
	}
}

// blend function
#define COMB(_n, _k)		(bez_fact(_n) / (bez_fact(_k) * bez_fact(_n - _k)))
float bez_spline::BEZ(int k, int n, float u)
{
	float a = (float)COMB(n, k);
	float b = (float)pow(u, (float)k);
	float c = (float)pow(1.0f - u, (float)(n - k));

	return a * b * c;
}

// get a point on the curve
void bez_spline::bez_get_point(vec3d *out, float u)
{	
	int idx;
	float bez_val;

	Assert(out != NULL);
	if(out == NULL){
		return;
	}

	// calc
	out->xyz.x = 0.0f;
	out->xyz.y = 0.0f;
	out->xyz.z = 0.0f;
	for(idx=0; idx<num_pts; idx++){
		// bez val
		bez_val = BEZ(idx, num_pts-1, u);

		// x component
		out->xyz.x += pts[idx].xyz.x * bez_val;

		// y component
		out->xyz.y += pts[idx].xyz.y * bez_val;

		// z component
		out->xyz.z += pts[idx].xyz.z * bez_val;
	}
}	

// render a bezier
void bez_spline::bez_render(int divs, color *c)
{
	float inc;
	int idx;
	vertex a, b;
	vec3d pt;

	// bleh
	if(divs <= 0){
		return;
	}
	inc = 1.0f / (float)divs;

	// draw in red
	gr_set_color_fast(c);

	// draw that many divisions
	bez_get_point(&pt, 0.0f);
	g3_rotate_vertex(&a, &pt);
	for(idx=1; idx<=divs; idx++){
		// second point
		bez_get_point(&pt, (float)idx * inc);
		g3_rotate_vertex(&b, &pt);

		// draw the line
		g3_draw_line(&a, &b);

		// store b
		a = b;
	}

	// draw the control points
	gr_set_color_fast(&Color_bright_green);
	for(idx=0; idx<num_pts; idx++){
		g3_draw_sphere_ez(&pts[idx], 0.75f);
	}
}


// --------------------------------------------------------------------------
// HERMITE splines

// constructor
herm_spline::herm_spline()
{
	int idx;

	// zero all points
	for(idx=0; idx<MAX_HERM_PTS; idx++){
		pts[idx] = vmd_zero_vector;
		d_pts[idx] = vmd_zero_vector;
	}
	num_pts = 0;
}

// constructor
herm_spline::herm_spline(int _num_pts, vec3d *_pts[MAX_HERM_PTS], vec3d *_d_pts[MAX_HERM_PTS])
{	
	herm_set_points(_num_pts, _pts, _d_pts);
}

// set the points
void herm_spline::herm_set_points(int _num_pts, vec3d *_pts[MAX_HERM_PTS], vec3d *_d_pts[MAX_HERM_PTS])
{
	int idx;

	// store the points
	num_pts = _num_pts;
	for(idx=0; idx<_num_pts; idx++){
		Assert(_pts[idx] != NULL);
		if(_pts[idx] != NULL){
			pts[idx] = *_pts[idx];
		}
		Assert(_d_pts[idx] != NULL);
		if(_d_pts[idx] != NULL){
			d_pts[idx] = *_d_pts[idx];
		}
	}
}
	
// get a point on the hermite curve.
void herm_spline::herm_get_point(vec3d *out, float u, int k)
{
	float a = ( (2.0f * u * u * u) - (3.0f * u * u) + 1 );
	float b = ( (-2.0f * u * u * u) + (3.0f * u * u) );
	float c = ( (u * u * u) - (2.0f * u * u) + u );
	float d = ( (u * u * u) - (u * u) );

	vec3d va;
	vm_vec_copy_scale(&va, &pts[k], a);

	vec3d vb;
	vm_vec_copy_scale(&vb, &pts[k+1], b);

	vec3d vc;
	vm_vec_copy_scale(&vc, &d_pts[k], c);

	vec3d vd;
	vm_vec_copy_scale(&vd, &d_pts[k+1], d);

	vm_vec_add(out, &va, &vb);
	vm_vec_add2(out, &vc);
	vm_vec_add2(out, &vd);
}

// the derivative of a point on the hermite curve
void herm_spline::herm_get_deriv(vec3d *deriv, float u, int k)
{
	float a = ( (6.0f * u * u) - (6.0f * u) );
	float b = ( (-6.0f * u * u) + (6.0f * u) );
	float c = ( (3.0f * u * u) - (4.0f * u) + 1 );
	float d = ( (3.0f * u * u) - (2.0f * u) );

	vec3d va;
	vm_vec_copy_scale(&va, &pts[k], a);

	vec3d vb;
	vm_vec_copy_scale(&vb, &pts[k+1], b);

	vec3d vc;
	vm_vec_copy_scale(&vc, &d_pts[k], c);

	vec3d vd;
	vm_vec_copy_scale(&vd, &d_pts[k+1], d);

	vm_vec_add(deriv, &va, &vb);
	vm_vec_add2(deriv, &vc);
	vm_vec_add2(deriv, &vd);
}

// render a bezier
void herm_spline::herm_render(int divs, color *clc)
{
	int idx;
	int s_idx;
	float inc = 1.0f / (float)divs;

	vertex a, b, c;
	vec3d pt, d_pt;

	// draw in red
	gr_set_color_fast(clc);

	// render each section
	for(idx=0; idx<num_pts-1; idx++){
		// render this piece
		herm_get_point(&pt, 0.0f, idx);		
		g3_rotate_vertex(&a, &pt);
		
		// draw the deriv
		herm_get_deriv(&d_pt, 0.0f, idx);
		vm_vec_add2(&d_pt, &pt);
		g3_rotate_vertex(&c, &d_pt);
		g3_draw_line(&a, &c);

		for(s_idx=1; s_idx<divs * 2; s_idx++){
			// second point
			herm_get_point(&pt, (float)s_idx * inc, idx);			
			
			// 2nd point on the line
			g3_rotate_vertex(&b, &pt);

			// draw the line
			g3_draw_line(&a, &b);

			// draw the deriv line
			herm_get_deriv(&d_pt, (float)s_idx * inc, idx);			
			vm_vec_add2(&d_pt, &pt);
			g3_rotate_vertex(&c, &d_pt);
			g3_draw_line(&b, &c);

			// store b
			a = b;
		}		
	}	

	// draw the control points
	gr_set_color_fast(&Color_bright_green);
	for(idx=0; idx<num_pts; idx++){
		g3_draw_sphere_ez(&pts[idx], 0.75f);
	}
}