2 * $Logfile: /Freespace2/code/Math/spline.cpp $
9 * Revision 1.2 2002/05/03 13:34:33 theoddone33
12 * Revision 1.1.1.1 2002/05/03 03:28:09 root
16 * 3 7/08/99 10:53a Dave
17 * New multiplayer interpolation scheme. Not 100% done yet, but still
18 * better than the old way.
20 * 2 7/06/99 4:24p Dave
21 * Mid-level checkin. Starting on some potentially cool multiplayer
31 #include "alphacolors.h"
34 // -------------------------------------------------------------------------------------------------
35 // SPLINE DEFINES/VARS
39 // -------------------------------------------------------------------------------------------------
43 // integer factorial. =o
50 for(idx=1; idx<=n; idx++){
58 bez_spline::bez_spline()
63 for(idx=0; idx<MAX_BEZ_PTS; idx++){
64 pts[idx] = vmd_zero_vector;
70 bez_spline::bez_spline(int _num_pts, vector *_pts[MAX_BEZ_PTS])
72 bez_set_points(_num_pts, _pts);
76 void bez_spline::bez_set_points(int _num_pts, vector *_pts[MAX_BEZ_PTS])
82 for(idx=0; idx<_num_pts; idx++){
83 Assert(_pts[idx] != NULL);
84 if(_pts[idx] != NULL){
85 pts[idx] = *_pts[idx];
91 #define COMB(_n, _k) ( (float)bez_fact(_n) / (float)(bez_fact(_k) * bez_fact(_n - _k)) )
92 float bez_spline::BEZ(int k, int n, float u)
94 float a = (float)COMB(n, k);
95 float b = (float)pow(u, (float)k);
96 float c = (float)pow(1.0f - u, (float)(n - k));
101 // get a point on the curve
102 void bez_spline::bez_get_point(vector *out, float u)
116 for(idx=0; idx<num_pts; idx++){
118 bez_val = BEZ(idx, num_pts-1, u);
121 out->x += pts[idx].x * bez_val;
124 out->y += pts[idx].y * bez_val;
127 out->z += pts[idx].z * bez_val;
132 void bez_spline::bez_render(int divs, color *c)
143 inc = 1.0f / (float)divs;
146 gr_set_color_fast(c);
148 // draw that many divisions
149 bez_get_point(&pt, 0.0f);
150 g3_rotate_vertex(&a, &pt);
151 for(idx=1; idx<=divs; idx++){
153 bez_get_point(&pt, (float)idx * inc);
154 g3_rotate_vertex(&b, &pt);
157 g3_draw_line(&a, &b);
163 // draw the control points
164 gr_set_color_fast(&Color_bright_green);
165 for(idx=0; idx<num_pts; idx++){
166 g3_draw_sphere_ez(&pts[idx], 0.75f);
171 // --------------------------------------------------------------------------
175 herm_spline::herm_spline()
180 for(idx=0; idx<MAX_HERM_PTS; idx++){
181 pts[idx] = vmd_zero_vector;
182 d_pts[idx] = vmd_zero_vector;
188 herm_spline::herm_spline(int _num_pts, vector *_pts[MAX_HERM_PTS], vector *_d_pts[MAX_HERM_PTS])
190 herm_set_points(_num_pts, _pts, _d_pts);
194 void herm_spline::herm_set_points(int _num_pts, vector *_pts[MAX_HERM_PTS], vector *_d_pts[MAX_HERM_PTS])
200 for(idx=0; idx<_num_pts; idx++){
201 Assert(_pts[idx] != NULL);
202 if(_pts[idx] != NULL){
203 pts[idx] = *_pts[idx];
205 Assert(_d_pts[idx] != NULL);
206 if(_d_pts[idx] != NULL){
207 d_pts[idx] = *_d_pts[idx];
212 // get a point on the hermite curve.
213 void herm_spline::herm_get_point(vector *out, float u, int k)
215 float a = ( (2.0f * u * u * u) - (3.0f * u * u) + 1 );
216 float b = ( (-2.0f * u * u * u) + (3.0f * u * u) );
217 float c = ( (u * u * u) - (2.0f * u * u) + u );
218 float d = ( (u * u * u) - (u * u) );
221 vm_vec_copy_scale(&va, &pts[k], a);
224 vm_vec_copy_scale(&vb, &pts[k+1], b);
227 vm_vec_copy_scale(&vc, &d_pts[k], c);
230 vm_vec_copy_scale(&vd, &d_pts[k+1], d);
232 vm_vec_add(out, &va, &vb);
233 vm_vec_add2(out, &vc);
234 vm_vec_add2(out, &vd);
237 // the derivative of a point on the hermite curve
238 void herm_spline::herm_get_deriv(vector *deriv, float u, int k)
240 float a = ( (6.0f * u * u) - (6.0f * u) );
241 float b = ( (-6.0f * u * u) + (6.0f * u) );
242 float c = ( (3.0f * u * u) - (4.0f * u) + 1 );
243 float d = ( (3.0f * u * u) - (2.0f * u) );
246 vm_vec_copy_scale(&va, &pts[k], a);
249 vm_vec_copy_scale(&vb, &pts[k+1], b);
252 vm_vec_copy_scale(&vc, &d_pts[k], c);
255 vm_vec_copy_scale(&vd, &d_pts[k+1], d);
257 vm_vec_add(deriv, &va, &vb);
258 vm_vec_add2(deriv, &vc);
259 vm_vec_add2(deriv, &vd);
263 void herm_spline::herm_render(int divs, color *clc)
267 float inc = 1.0f / (float)divs;
273 gr_set_color_fast(clc);
275 // render each section
276 for(idx=0; idx<num_pts-1; idx++){
278 herm_get_point(&pt, 0.0f, idx);
279 g3_rotate_vertex(&a, &pt);
282 herm_get_deriv(&d_pt, 0.0f, idx);
283 vm_vec_add2(&d_pt, &pt);
284 g3_rotate_vertex(&c, &d_pt);
285 g3_draw_line(&a, &c);
287 for(s_idx=1; s_idx<divs * 2; s_idx++){
289 herm_get_point(&pt, (float)s_idx * inc, idx);
291 // 2nd point on the line
292 g3_rotate_vertex(&b, &pt);
295 g3_draw_line(&a, &b);
297 // draw the deriv line
298 herm_get_deriv(&d_pt, (float)s_idx * inc, idx);
299 vm_vec_add2(&d_pt, &pt);
300 g3_rotate_vertex(&c, &d_pt);
301 g3_draw_line(&b, &c);
308 // draw the control points
309 gr_set_color_fast(&Color_bright_green);
310 for(idx=0; idx<num_pts; idx++){
311 g3_draw_sphere_ez(&pts[idx], 0.75f);