-
Notifications
You must be signed in to change notification settings - Fork 0
/
CubicBspline.cpp
419 lines (339 loc) · 9.27 KB
/
CubicBspline.cpp
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*
* CubicBspline.cpp: Uniform cubic B-splines
*
* (c) 2002 Stephen Chenney, University of Wisconsin at Madison
*/
#include "CubicBspline.h"
#include "GenericException.h"
#include <math.h>
/* Initializes with the given dimension and control points. */
CubicBspline::CubicBspline(const unsigned short dim, const unsigned short num,
float **c_in, const bool l, const float elev)
{
d = dim;
n = num;
z = elev;
Copy_Controls(c_in);
loop = l;
}
/* Destructor. */
CubicBspline::~CubicBspline(void)
{
Delete_Controls();
}
/* Copy operator. */
CubicBspline&
CubicBspline::operator=(const CubicBspline &src)
{
if ( this != &src )
{
Delete_Controls();
d = src.d;
n = src.n;
z = src.z;
Copy_Controls(src.c_pts);
loop = src.loop;
}
return *this;
}
/* Query a control point, putting the value into the given array, pt.
** Throws an exception if the index is out of range. */
void
CubicBspline::C(unsigned short index, float *pt)
{
int i;
if ( index >= n )
throw new GenericException("CubicBspline::C - Index out of range");
for ( i = 0 ; i < d ; i++ )
pt[i] = c_pts[index][i];
}
/* Change a control point at the given position.
** Will throw an exception if the position is out of range. */
void
CubicBspline::Set_Control(const float *pt, const unsigned short posn)
{
int i;
if ( posn >= n )
throw new GenericException(
"CubicBspline::Set_Control - Posn out of range");
for ( i = 0 ; i < d ; i++ )
c_pts[posn][i] = pt[i];
}
/* Add a control point at the end. */
void
CubicBspline::Append_Control(const float *pt)
{
float **c_new = new float*[n + 1];
int i;
// Copy the old points over.
for ( i = 0 ; i < n ; i++ )
c_new[i] = c_pts[i];
// Add the new one.
c_new[n] = new float[d];
for ( i = 0 ; i < d ; i++ )
c_new[n][i] = pt[i];
// Swap old for new.
delete[] c_pts;
c_pts = c_new;
// One more control pt.
n++;
}
/* Add a control point at the given position. */
/* Will throw an exception if the position is beyond the end of
** the existing set of control points. */
void
CubicBspline::Insert_Control(const float *pt, const unsigned short posn)
{
float **c_new = new float*[n + 1];
int i;
if ( posn > n )
throw new GenericException(
"CubicBspline::Insert_Control - Posn out of range");
// Copy some points over.
for ( i = 0 ; i < posn ; i++ )
c_new[i] = c_pts[i];
// Add the new one.
c_new[posn] = new float[d];
for ( i = 0 ; i < d ; i++ )
c_new[posn][i] = pt[i];
// Copy the rest of the points over.
for ( i = posn ; i < n ; i++ )
c_new[i+1] = c_pts[i];
// Swap old for new.
delete[] c_pts;
c_pts = c_new;
// One more control pt.
n++;
}
/* Remove a control point at the given position. */
/* Will throw an exception the position is out of range. */
void
CubicBspline::Delete_Control(const unsigned short posn)
{
int i;
if ( posn >= n )
throw new GenericException(
"CubicBspline::Delete_Control - Posn out of range");
// Get rid of the undesired control point
delete[] c_pts[posn];
// Copy the rest of the points down.
for ( i = posn ; i < n - 1 ; i++ )
c_pts[i] = c_pts[i+1];
// One less control pt.
n--;
}
/* Evaluate the curve at a parameter value and copy the result into
** the given array. Throws an exception if the parameter is out of
** range, unless the curve is a loop. */
void
CubicBspline::Evaluate_Point(const float t, float *pt)
{
int posn;
float u;
float u_sq;
float u_cube;
float basis[4];
int i, j;
posn = (int)floor(t);
if ( posn > n - 4 && ! loop )
{
throw new GenericException(
"CubicBspline::EvaluatePoint - Parameter value out of range");
}
u = t - posn;
u_sq = u * u;
u_cube = u * u_sq;
/* This evaluates the blending functions at the parameter value. */
basis[0] = -u_cube + 3.0f * u_sq - 3.0f * u + 1.0f;
basis[1] = 3.0f * u_cube - 6.0f * u_sq + 4.0f;
basis[2] = -3.0f * u_cube + 3.0f * u_sq + 3.0f * u + 1.0f;
basis[3] = u_cube;
/* Sum up the control points times the basis functions for each dimension.
** j loops over dimension, i loops over control point. */
for ( j = 0 ; j < d ; j++ )
pt[j] = 0.0f;
for ( i = 0 ; i < 4 ; i++ )
{
int index = ( posn + i ) % n;
for ( j = 0 ; j < d ; j++ )
pt[j] += c_pts[index][j] * basis[i];
}
/* Divide through the constant factor. */
for ( j = 0 ; j < d ; j++ )
pt[j] /= 6.0f;
}
void
CubicBspline::Get_Point(const float t, float *pt)
{
Evaluate_Point(t, pt);
pt[2] += z;
}
/* Evaluate the derivative at a parameter value and copy the result into
** the given array. Throws an exception if the parameter is out of
** range, unless the curve is a loop. */
void
CubicBspline::Evaluate_Derivative(const float t, float *deriv)
{
int posn;
float u;
float u_sq;
float basis[4];
int i, j;
posn = (int)floor(t);
if ( posn > n - 4 && ! loop )
{
throw new GenericException(
"CubicBspline::EvaluatePoint - Parameter value out of range");
}
u = t - posn;
u_sq = u * u;
/* Evaluate the derivatives of the blending functions at the parameter
** value. */
basis[0] = -3.0f * u_sq + 6.0f * u - 3.0f;
basis[1] = 9.0f * u_sq - 12.0f * u;
basis[2] = -9.0f * u_sq + 6.0f * u + 3.0f;
basis[3] = 3.0f * u_sq;
/* Now it's just like evaluating a point. */
for ( j = 0 ; j < d ; j++ )
deriv[j] = 0.0f;
for ( i = 0 ; i < 4 ; i++ )
{
int index = ( posn + i ) % n;
for ( j = 0 ; j < d ; j++ )
deriv[j] += c_pts[index][j] * basis[i];
}
for ( j = 0 ; j < d ; j++ )
deriv[j] /= 6.0f;
}
/* Refine the curve, putting the result into the given curve. This
** will correctly account for looped curves. */
void
CubicBspline::Refine(CubicBspline &result)
{
int new_n;
float **new_c;
int i, j, k;
/* Figure out how many new vertices. */
if ( loop )
new_n = n * 2;
else
new_n = n * 2 - 3;
/* This creates two new controls for each existing one, except for the
** first and last control points (unless it loops.) */
new_c = new float*[new_n];
for ( i = 0, k = 0 ; i < new_n ; i+=2, k++ )
{
/* This figures out which control points to average for the new pts. */
int p0 = k % n;
int p1 = ( k + 1 ) % n;
int p2 = ( k + 2 ) % n;
/* Allocate space for the new points. */
new_c[i] = new float[d];
if ( i + 1 < new_n )
new_c[i+1] = new float[d];
/* Compute the new points using the refinement rules. */
for ( j = 0 ; j < d ; j++ )
{
new_c[i][j] = 0.5f * ( c_pts[p0][j] + c_pts[p1][j] );
if ( i + 1 < new_n )
new_c[i+1][j] = 0.125f * ( c_pts[p0][j] + 6.0f * c_pts[p1][j]
+ c_pts[p2][j]);
}
}
/* Get rid of any old control points in the result. */
result.Delete_Controls();
/* Copy everything over. */
result.d = d;
result.n = new_n;
result.c_pts = new_c;
result.loop = loop;
}
/* This function returns true if the curve is locally flat, to within
** tolerance. What it actually does is look at every set of three control
** points in turn, and checks the distance of the middle point from the
** line joining the other two. If the middle point is too far from the line,
** the curve is outside the tolerence. The code is messy because it computes
** certain vectors required for the test, and doesn't abstract them into a
** vector class. Basically all it is doing is a bunch of vector subtractions
** and dot products. */
bool
CubicBspline::Within_Tolerance(const float tolerance)
{
float *p;
float *x2_x1;
float *x3_x1;
float l_13, l_2p, dot;
int i, j;
int m;
int i1, i2, i3;
p = new float[d];
x2_x1 = new float[d];
x3_x1 = new float[d];
m = loop ? n : n - 2;
for ( i = 0 ; i < m ; i++ )
{
i1 = i % n;
i2 = ( i + 1 ) % n;
i3 = ( i + 2 ) % n;
dot = 0.0f;
l_13 = 0.0f;
for ( j = 0 ; j < d ; j++ )
{
x2_x1[j] = c_pts[i2][j] - c_pts[i1][j];
x3_x1[j] = c_pts[i3][j] - c_pts[i1][j];
dot += ( x2_x1[j] * x3_x1[j] );
l_13 += ( x3_x1[j] * x3_x1[j] );
}
if ( l_13 == 0.0f )
continue;
l_2p = 0.0f;
for ( j = 0 ; j < d ; j++ )
{
p[j] = c_pts[i1][j] + dot * x3_x1[j] / l_13;
l_2p += ( c_pts[i2][j] - p[j] ) * ( c_pts[i2][j] - p[j] );
}
if ( l_2p > tolerance * tolerance )
return false;
}
delete[] p;
delete[] x2_x1;
delete[] x3_x1;
return true;
}
/* Refine a curve until it can be approximated with straight lines to within
** the given tolerance. Always does at least one refinement, even if the
** original curve is inside tolerance. */
void
CubicBspline::Refine_Tolerance(CubicBspline &result, const float tolerance)
{
Refine(result);
while ( ! result.Within_Tolerance(tolerance) )
result.Refine(result);
}
/* Copy a set fo control points */
void
CubicBspline::Copy_Controls(float **c_in)
{
int i, j;
if ( c_pts )
Delete_Controls();
c_pts = new float*[n];
for ( i = 0 ; i < n ; i++ )
{
c_pts[n] = new float[d];
for ( j = 0 ; j < d ; j++ )
c_pts[n][d] = c_in[n][d];
}
}
/* Delete a set of control points */
void
CubicBspline::Delete_Controls(void)
{
int i;
if ( ! c_pts )
return;
for ( i = 0 ; i < n ; i++ )
delete[] c_pts[i];
delete[] c_pts;
c_pts = NULL;
}