| 1 |
/* spline.c: spline and spline list (represented as arrays) manipulation. */ |
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
|
| 5 |
#include "mallocvar.h" |
| 6 |
|
| 7 |
#include "message.h" |
| 8 |
#include "point.h" |
| 9 |
#include "spline.h" |
| 10 |
#include "vector.h" |
| 11 |
|
| 12 |
/* Print a spline in human-readable form. */ |
| 13 |
|
| 14 |
void |
| 15 |
print_spline (FILE *f, spline_type s) |
| 16 |
{ |
| 17 |
assert(SPLINE_DEGREE (s) == LINEARTYPE || SPLINE_DEGREE (s) == CUBICTYPE); |
| 18 |
|
| 19 |
if (SPLINE_DEGREE (s) == LINEARTYPE) |
| 20 |
fprintf (f, "(%.3f,%.3f)--(%.3f,%.3f).\n", |
| 21 |
BEG_POINT (s).x, BEG_POINT (s).y, |
| 22 |
END_POINT (s).x, END_POINT (s).y); |
| 23 |
|
| 24 |
else if (SPLINE_DEGREE (s) == CUBICTYPE) |
| 25 |
fprintf (f, "(%.3f,%.3f)..ctrls(%.3f,%.3f)&(%.3f,%.3f)..(%.3f,%.3f).\n", |
| 26 |
BEG_POINT (s).x, BEG_POINT (s).y, |
| 27 |
CONTROL1 (s).x, CONTROL1 (s).y, |
| 28 |
CONTROL2 (s).x, CONTROL2 (s).y, |
| 29 |
END_POINT (s).x, END_POINT (s).y); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
/* Evaluate the spline S at a given T value. This is an implementation |
| 35 |
of de Casteljau's algorithm. See Schneider's thesis, p.37. |
| 36 |
The variable names are taken from there. */ |
| 37 |
|
| 38 |
Point |
| 39 |
evaluate_spline (spline_type s, float t) |
| 40 |
{ |
| 41 |
spline_type V[4]; /* We need degree+1 splines, but assert degree <= 3. */ |
| 42 |
signed i, j; |
| 43 |
float one_minus_t = (float) 1.0 - t; |
| 44 |
polynomial_degree degree = SPLINE_DEGREE (s); |
| 45 |
|
| 46 |
for (i = 0; i <= degree; i++) |
| 47 |
{ |
| 48 |
V[0].v[i].x = s.v[i].x; |
| 49 |
V[0].v[i].y = s.v[i].y; |
| 50 |
V[0].v[i].z = s.v[i].z; |
| 51 |
} |
| 52 |
|
| 53 |
for (j = 1; j <= degree; j++) |
| 54 |
for (i = 0; i <= degree - j; i++) |
| 55 |
{ |
| 56 |
Point t1 = point_scaled(V[j - 1].v[i], one_minus_t); |
| 57 |
Point t2 = point_scaled(V[j - 1].v[i + 1], t); |
| 58 |
Point temp = point_sum(t1, t2); |
| 59 |
V[j].v[i].x = temp.x; |
| 60 |
V[j].v[i].y = temp.y; |
| 61 |
V[j].v[i].z = temp.z; |
| 62 |
} |
| 63 |
|
| 64 |
return V[degree].v[0]; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
/* Return a new, empty, spline list. */ |
| 70 |
|
| 71 |
spline_list_type * |
| 72 |
new_spline_list (void) |
| 73 |
{ |
| 74 |
spline_list_type *answer; |
| 75 |
|
| 76 |
MALLOCVAR(answer); |
| 77 |
*answer = empty_spline_list(); |
| 78 |
return answer; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
spline_list_type |
| 84 |
empty_spline_list (void) |
| 85 |
{ |
| 86 |
spline_list_type answer; |
| 87 |
SPLINE_LIST_DATA (answer) = NULL; |
| 88 |
SPLINE_LIST_LENGTH (answer) = 0; |
| 89 |
return answer; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
/* Return a new spline list with SPLINE as the first element. */ |
| 95 |
|
| 96 |
spline_list_type * |
| 97 |
new_spline_list_with_spline (spline_type spline) |
| 98 |
{ |
| 99 |
spline_list_type *answer; |
| 100 |
|
| 101 |
answer = new_spline_list(); |
| 102 |
MALLOCVAR(SPLINE_LIST_DATA(*answer)); |
| 103 |
SPLINE_LIST_ELT (*answer, 0) = spline; |
| 104 |
SPLINE_LIST_LENGTH (*answer) = 1; |
| 105 |
|
| 106 |
return answer; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
/* Free the storage in a spline list. We don't have to free the |
| 112 |
elements, since they are arrays in automatic storage. And we don't |
| 113 |
want to free the list if it was empty. */ |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
void |
| 118 |
free_spline_list(spline_list_type spline_list) { |
| 119 |
|
| 120 |
if (SPLINE_LIST_DATA(spline_list) != NULL) |
| 121 |
free(SPLINE_LIST_DATA(spline_list)); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
/* Append the spline S to the list SPLINE_LIST. */ |
| 127 |
|
| 128 |
void |
| 129 |
append_spline (spline_list_type *l, spline_type s) |
| 130 |
{ |
| 131 |
assert (l != NULL); |
| 132 |
|
| 133 |
SPLINE_LIST_LENGTH (*l)++; |
| 134 |
REALLOCARRAY(SPLINE_LIST_DATA(*l), SPLINE_LIST_LENGTH(*l)); |
| 135 |
LAST_SPLINE_LIST_ELT (*l) = s; |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
/* Tack the elements in the list S2 onto the end of S1. |
| 141 |
S2 is not changed. */ |
| 142 |
|
| 143 |
void |
| 144 |
concat_spline_lists (spline_list_type *s1, spline_list_type s2) |
| 145 |
{ |
| 146 |
unsigned this_spline; |
| 147 |
unsigned new_length; |
| 148 |
|
| 149 |
assert (s1 != NULL); |
| 150 |
|
| 151 |
new_length = SPLINE_LIST_LENGTH (*s1) + SPLINE_LIST_LENGTH (s2); |
| 152 |
|
| 153 |
REALLOCARRAY_NOFAIL(SPLINE_LIST_DATA(*s1), new_length); |
| 154 |
|
| 155 |
for (this_spline = 0; this_spline < SPLINE_LIST_LENGTH (s2); this_spline++) |
| 156 |
SPLINE_LIST_ELT (*s1, SPLINE_LIST_LENGTH (*s1)++) |
| 157 |
= SPLINE_LIST_ELT (s2, this_spline); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
/* Return a new, empty, spline list array. */ |
| 163 |
|
| 164 |
spline_list_array_type |
| 165 |
new_spline_list_array (void) |
| 166 |
{ |
| 167 |
spline_list_array_type answer; |
| 168 |
|
| 169 |
SPLINE_LIST_ARRAY_DATA (answer) = NULL; |
| 170 |
SPLINE_LIST_ARRAY_LENGTH (answer) = 0; |
| 171 |
|
| 172 |
return answer; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/* Free the storage in a spline list array. We don't |
| 178 |
want to free the list if it is empty. */ |
| 179 |
void |
| 180 |
free_spline_list_array (spline_list_array_type *spline_list_array) |
| 181 |
{ |
| 182 |
unsigned this_list; |
| 183 |
|
| 184 |
for (this_list = 0; |
| 185 |
this_list < SPLINE_LIST_ARRAY_LENGTH (*spline_list_array); |
| 186 |
this_list++) |
| 187 |
free_spline_list (SPLINE_LIST_ARRAY_ELT (*spline_list_array, this_list)); |
| 188 |
|
| 189 |
if (SPLINE_LIST_ARRAY_DATA (*spline_list_array) != NULL) |
| 190 |
free (SPLINE_LIST_ARRAY_DATA (*spline_list_array)); |
| 191 |
|
| 192 |
flush_log_output (); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
/* Append the spline S to the list SPLINE_LIST_ARRAY. */ |
| 198 |
|
| 199 |
void |
| 200 |
append_spline_list (spline_list_array_type *l, spline_list_type s) |
| 201 |
{ |
| 202 |
SPLINE_LIST_ARRAY_LENGTH (*l)++; |
| 203 |
REALLOCARRAY_NOFAIL(SPLINE_LIST_ARRAY_DATA(*l), |
| 204 |
SPLINE_LIST_ARRAY_LENGTH(*l)); |
| 205 |
LAST_SPLINE_LIST_ARRAY_ELT (*l) = s; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|