2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
4 * All source code herein is the property of Volition, Inc. You may not sell
5 * or otherwise commercially exploit the source or things you created based on
10 * $Logfile: /Freespace2/code/Math/spline.cpp $
17 * Revision 1.3 2002/06/09 04:41:22 relnev
18 * added copyright header
20 * Revision 1.2 2002/05/03 13:34:33 theoddone33
23 * Revision 1.1.1.1 2002/05/03 03:28:09 root
27 * 3 7/08/99 10:53a Dave
28 * New multiplayer interpolation scheme. Not 100% done yet, but still
29 * better than the old way.
31 * 2 7/06/99 4:24p Dave
32 * Mid-level checkin. Starting on some potentially cool multiplayer
42 #include "alphacolors.h"
45 // -------------------------------------------------------------------------------------------------
46 // SPLINE DEFINES/VARS
50 // -------------------------------------------------------------------------------------------------
54 // integer factorial. =o
61 for(idx=1; idx<=n; idx++){
69 bez_spline::bez_spline()
74 for(idx=0; idx<MAX_BEZ_PTS; idx++){
75 pts[idx] = vmd_zero_vector;
81 bez_spline::bez_spline(int _num_pts, vector *_pts[MAX_BEZ_PTS])
83 bez_set_points(_num_pts, _pts);
87 void bez_spline::bez_set_points(int _num_pts, vector *_pts[MAX_BEZ_PTS])
93 for(idx=0; idx<_num_pts; idx++){
94 Assert(_pts[idx] != NULL);
95 if(_pts[idx] != NULL){
96 pts[idx] = *_pts[idx];
102 #define COMB(_n, _k) ( (float)bez_fact(_n) / (float)(bez_fact(_k) * bez_fact(_n - _k)) )
103 float bez_spline::BEZ(int k, int n, float u)
105 float a = (float)COMB(n, k);
106 float b = (float)pow(u, (float)k);
107 float c = (float)pow(1.0f - u, (float)(n - k));
112 // get a point on the curve
113 void bez_spline::bez_get_point(vector *out, float u)
127 for(idx=0; idx<num_pts; idx++){
129 bez_val = BEZ(idx, num_pts-1, u);
132 out->x += pts[idx].x * bez_val;
135 out->y += pts[idx].y * bez_val;
138 out->z += pts[idx].z * bez_val;
143 void bez_spline::bez_render(int divs, color *c)
154 inc = 1.0f / (float)divs;
157 gr_set_color_fast(c);
159 // draw that many divisions
160 bez_get_point(&pt, 0.0f);
161 g3_rotate_vertex(&a, &pt);
162 for(idx=1; idx<=divs; idx++){
164 bez_get_point(&pt, (float)idx * inc);
165 g3_rotate_vertex(&b, &pt);
168 g3_draw_line(&a, &b);
174 // draw the control points
175 gr_set_color_fast(&Color_bright_green);
176 for(idx=0; idx<num_pts; idx++){
177 g3_draw_sphere_ez(&pts[idx], 0.75f);
182 // --------------------------------------------------------------------------
186 herm_spline::herm_spline()
191 for(idx=0; idx<MAX_HERM_PTS; idx++){
192 pts[idx] = vmd_zero_vector;
193 d_pts[idx] = vmd_zero_vector;
199 herm_spline::herm_spline(int _num_pts, vector *_pts[MAX_HERM_PTS], vector *_d_pts[MAX_HERM_PTS])
201 herm_set_points(_num_pts, _pts, _d_pts);
205 void herm_spline::herm_set_points(int _num_pts, vector *_pts[MAX_HERM_PTS], vector *_d_pts[MAX_HERM_PTS])
211 for(idx=0; idx<_num_pts; idx++){
212 Assert(_pts[idx] != NULL);
213 if(_pts[idx] != NULL){
214 pts[idx] = *_pts[idx];
216 Assert(_d_pts[idx] != NULL);
217 if(_d_pts[idx] != NULL){
218 d_pts[idx] = *_d_pts[idx];
223 // get a point on the hermite curve.
224 void herm_spline::herm_get_point(vector *out, float u, int k)
226 float a = ( (2.0f * u * u * u) - (3.0f * u * u) + 1 );
227 float b = ( (-2.0f * u * u * u) + (3.0f * u * u) );
228 float c = ( (u * u * u) - (2.0f * u * u) + u );
229 float d = ( (u * u * u) - (u * u) );
232 vm_vec_copy_scale(&va, &pts[k], a);
235 vm_vec_copy_scale(&vb, &pts[k+1], b);
238 vm_vec_copy_scale(&vc, &d_pts[k], c);
241 vm_vec_copy_scale(&vd, &d_pts[k+1], d);
243 vm_vec_add(out, &va, &vb);
244 vm_vec_add2(out, &vc);
245 vm_vec_add2(out, &vd);
248 // the derivative of a point on the hermite curve
249 void herm_spline::herm_get_deriv(vector *deriv, float u, int k)
251 float a = ( (6.0f * u * u) - (6.0f * u) );
252 float b = ( (-6.0f * u * u) + (6.0f * u) );
253 float c = ( (3.0f * u * u) - (4.0f * u) + 1 );
254 float d = ( (3.0f * u * u) - (2.0f * u) );
257 vm_vec_copy_scale(&va, &pts[k], a);
260 vm_vec_copy_scale(&vb, &pts[k+1], b);
263 vm_vec_copy_scale(&vc, &d_pts[k], c);
266 vm_vec_copy_scale(&vd, &d_pts[k+1], d);
268 vm_vec_add(deriv, &va, &vb);
269 vm_vec_add2(deriv, &vc);
270 vm_vec_add2(deriv, &vd);
274 void herm_spline::herm_render(int divs, color *clc)
278 float inc = 1.0f / (float)divs;
284 gr_set_color_fast(clc);
286 // render each section
287 for(idx=0; idx<num_pts-1; idx++){
289 herm_get_point(&pt, 0.0f, idx);
290 g3_rotate_vertex(&a, &pt);
293 herm_get_deriv(&d_pt, 0.0f, idx);
294 vm_vec_add2(&d_pt, &pt);
295 g3_rotate_vertex(&c, &d_pt);
296 g3_draw_line(&a, &c);
298 for(s_idx=1; s_idx<divs * 2; s_idx++){
300 herm_get_point(&pt, (float)s_idx * inc, idx);
302 // 2nd point on the line
303 g3_rotate_vertex(&b, &pt);
306 g3_draw_line(&a, &b);
308 // draw the deriv line
309 herm_get_deriv(&d_pt, (float)s_idx * inc, idx);
310 vm_vec_add2(&d_pt, &pt);
311 g3_rotate_vertex(&c, &d_pt);
312 g3_draw_line(&b, &c);
319 // draw the control points
320 gr_set_color_fast(&Color_bright_green);
321 for(idx=0; idx<num_pts; idx++){
322 g3_draw_sphere_ez(&pts[idx], 0.75f);