1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: gensurf.c,v 2.32 2025/04/18 23:02:53 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* gensurf.c - program to generate functional surfaces |
6 |
* |
7 |
* Parametric functions x(s,t), y(s,t) and z(s,t) |
8 |
* specify the surface, which is tesselated into an m by n |
9 |
* array of paired triangles. |
10 |
* The surface normal is defined by the right hand |
11 |
* rule applied to (s,t). |
12 |
* |
13 |
* 4/3/87 |
14 |
* |
15 |
* 4/16/02 Added conditional vertex output |
16 |
*/ |
17 |
|
18 |
#include "standard.h" |
19 |
|
20 |
#include "paths.h" |
21 |
#include "resolu.h" |
22 |
#include "rterror.h" |
23 |
#include "calcomp.h" |
24 |
|
25 |
char XNAME[] = "X`SYS"; /* x function name */ |
26 |
char YNAME[] = "Y`SYS"; /* y function name */ |
27 |
char ZNAME[] = "Z`SYS"; /* z function name */ |
28 |
|
29 |
char VNAME[] = "valid"; /* valid vertex name */ |
30 |
|
31 |
#define ABS(x) ((x)>=0 ? (x) : -(x)) |
32 |
|
33 |
#define ZEROVECT(v) (DOT(v,v) <= FTINY*FTINY) |
34 |
|
35 |
#define pvect(p) printf(vformat, (p)[0], (p)[1], (p)[2]) |
36 |
|
37 |
char vformat[] = "%18.12g %18.12g %18.12g\n"; |
38 |
char tsargs[] = "4 surf_dx surf_dy surf_dz surf.cal"; |
39 |
char texname[] = "Phong"; |
40 |
|
41 |
int smooth = 0; /* apply smoothing? */ |
42 |
int objout = 0; /* output .OBJ format? */ |
43 |
int rev = 0; /* invert normal directions? */ |
44 |
|
45 |
char *modname, *surfname; |
46 |
|
47 |
/* recorded data flags */ |
48 |
#define HASBORDER 01 |
49 |
#define TRIPLETS 02 |
50 |
/* a data structure */ |
51 |
struct { |
52 |
int flags; /* data type */ |
53 |
short m, n; /* number of s and t values */ |
54 |
RREAL *data; /* the data itself, s major sort */ |
55 |
} datarec; /* our recorded data */ |
56 |
|
57 |
/* XXX this is redundant with rt/noise3.c, should go to a library */ |
58 |
double l_hermite(char *), l_bezier(char *), |
59 |
l_bspline(char *), l_dataval(char *); |
60 |
|
61 |
typedef struct { |
62 |
int valid; /* point is valid (vertex number) */ |
63 |
int nvalid; /* normal is valid (normal number) */ |
64 |
FVECT p; /* vertex position */ |
65 |
FVECT n; /* average normal */ |
66 |
RREAL uv[2]; /* (u,v) position */ |
67 |
} POINT; |
68 |
|
69 |
int nverts = 0; /* vertex output count */ |
70 |
int nnorms = 0; /* normal output count */ |
71 |
|
72 |
void loaddata(char *file, int m, int n, int pointsize); |
73 |
double l_dataval(char *nam); |
74 |
void putobjrow(POINT *rp, int n); |
75 |
void putobjvert(POINT *p); |
76 |
void putsquare(POINT *p0, POINT *p1, POINT *p2, POINT *p3); |
77 |
void comprow(double s, POINT *row, int siz); |
78 |
void compnorms(POINT *r0, POINT *r1, POINT *r2, int siz); |
79 |
int norminterp(FVECT resmat[4], POINT *p0, POINT *p1, POINT *p2, POINT *p3); |
80 |
|
81 |
|
82 |
int |
83 |
main(int argc, char *argv[]) |
84 |
{ |
85 |
POINT *row0, *row1, *row2, *rp; |
86 |
int i, j, m, n; |
87 |
char stmp[256]; |
88 |
|
89 |
esupport |= E_VARIABLE|E_FUNCTION|E_RCONST; |
90 |
esupport &= ~(E_OUTCHAN|E_INCHAN); |
91 |
varset("PI", ':', PI); |
92 |
funset("hermite", 5, ':', l_hermite); |
93 |
funset("bezier", 5, ':', l_bezier); |
94 |
funset("bspline", 5, ':', l_bspline); |
95 |
|
96 |
if (argc < 8) |
97 |
goto userror; |
98 |
|
99 |
for (i = 8; i < argc; i++) |
100 |
if (!strcmp(argv[i], "-e")) |
101 |
scompile(argv[++i], NULL, 0); |
102 |
else if (!strcmp(argv[i], "-f")) { |
103 |
char *fpath = getpath(argv[++i], getrlibpath(), 0); |
104 |
if (fpath == NULL) { |
105 |
fprintf(stderr, "%s: cannot find file '%s'\n", |
106 |
argv[0], argv[i]); |
107 |
quit(1); |
108 |
} |
109 |
fcompile(fpath); |
110 |
} else if (!strcmp(argv[i], "-s")) |
111 |
smooth++; |
112 |
else if (!strcmp(argv[i], "-o")) |
113 |
objout++; |
114 |
else if (!strcmp(argv[i], "-i")) |
115 |
rev = 1; |
116 |
else |
117 |
goto userror; |
118 |
|
119 |
modname = argv[1]; |
120 |
surfname = argv[2]; |
121 |
m = eval(argv[6]) + .5; |
122 |
n = eval(argv[7]) + .5; |
123 |
if (m <= 0 || n <= 0) |
124 |
goto userror; |
125 |
if (!strcmp(argv[5], "-") || access(argv[5], 4) == 0) { /* file? */ |
126 |
funset(ZNAME, 2, ':', l_dataval); |
127 |
if (!strcmp(argv[5],argv[3]) && !strcmp(argv[5],argv[4])) { |
128 |
loaddata(argv[5], m, n, 3); |
129 |
funset(XNAME, 2, ':', l_dataval); |
130 |
funset(YNAME, 2, ':', l_dataval); |
131 |
} else { |
132 |
loaddata(argv[5], m, n, 1); |
133 |
sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]); |
134 |
scompile(stmp, NULL, 0); |
135 |
sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]); |
136 |
scompile(stmp, NULL, 0); |
137 |
} |
138 |
} else { |
139 |
sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]); |
140 |
scompile(stmp, NULL, 0); |
141 |
sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]); |
142 |
scompile(stmp, NULL, 0); |
143 |
sprintf(stmp, "%s(s,t)=%s;", ZNAME, argv[5]); |
144 |
scompile(stmp, NULL, 0); |
145 |
} |
146 |
row0 = (POINT *)malloc((n+3)*sizeof(POINT)); |
147 |
row1 = (POINT *)malloc((n+3)*sizeof(POINT)); |
148 |
row2 = (POINT *)malloc((n+3)*sizeof(POINT)); |
149 |
if (row0 == NULL || row1 == NULL || row2 == NULL) { |
150 |
fprintf(stderr, "%s: out of memory\n", argv[0]); |
151 |
quit(1); |
152 |
} |
153 |
row0++; row1++; row2++; |
154 |
/* print header */ |
155 |
fputs("# ", stdout); |
156 |
printargs(argc, argv, stdout); |
157 |
doptimize(1); |
158 |
eclock++; |
159 |
/* initialize */ |
160 |
comprow(-1.0/m, row0, n); |
161 |
comprow(0.0, row1, n); |
162 |
comprow(1.0/m, row2, n); |
163 |
compnorms(row0, row1, row2, n); |
164 |
if (objout) { |
165 |
printf("\nusemtl %s\n\n", modname); |
166 |
printf("o %s\n\n", surfname); |
167 |
putobjrow(row1, n); |
168 |
} |
169 |
/* for each row */ |
170 |
for (i = 0; i < m; i++) { |
171 |
/* compute next row */ |
172 |
rp = row0; |
173 |
row0 = row1; |
174 |
row1 = row2; |
175 |
row2 = rp; |
176 |
comprow((double)(i+2)/m, row2, n); |
177 |
compnorms(row0, row1, row2, n); |
178 |
if (objout) |
179 |
putobjrow(row1, n); |
180 |
|
181 |
for (j = 0; j < n; j++) { |
182 |
int orient = (j & 1); |
183 |
/* put polygons */ |
184 |
if (!(row0[j].valid && row1[j+1].valid)) |
185 |
orient = 1; |
186 |
else if (!(row1[j].valid && row0[j+1].valid)) |
187 |
orient = 0; |
188 |
if (orient) |
189 |
putsquare(&row0[j], &row1[j], |
190 |
&row0[j+1], &row1[j+1]); |
191 |
else |
192 |
putsquare(&row1[j], &row1[j+1], |
193 |
&row0[j], &row0[j+1]); |
194 |
} |
195 |
} |
196 |
|
197 |
return 0; |
198 |
|
199 |
userror: |
200 |
fprintf(stderr, "Usage: %s material name ", argv[0]); |
201 |
fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-o][-e expr][-f file]\n"); |
202 |
return 1; |
203 |
} |
204 |
|
205 |
|
206 |
void |
207 |
loaddata( /* load point data from file */ |
208 |
char *file, |
209 |
int m, |
210 |
int n, |
211 |
int pointsize |
212 |
) |
213 |
{ |
214 |
FILE *fp; |
215 |
char word[64]; |
216 |
int size; |
217 |
RREAL *dp; |
218 |
|
219 |
datarec.flags = HASBORDER; /* assume border values */ |
220 |
datarec.m = m+1; |
221 |
datarec.n = n+1; |
222 |
size = datarec.m*datarec.n*pointsize; |
223 |
if (pointsize == 3) |
224 |
datarec.flags |= TRIPLETS; |
225 |
dp = (RREAL *)malloc(size*sizeof(RREAL)); |
226 |
if ((datarec.data = dp) == NULL) { |
227 |
fputs("Out of memory\n", stderr); |
228 |
exit(1); |
229 |
} |
230 |
if (!strcmp(file, "-")) { |
231 |
file = "<stdin>"; |
232 |
fp = stdin; |
233 |
} else if ((fp = fopen(file, "r")) == NULL) { |
234 |
fputs(file, stderr); |
235 |
fputs(": cannot open\n", stderr); |
236 |
exit(1); |
237 |
} |
238 |
while (size > 0 && fgetword(word, sizeof(word), fp) != NULL) { |
239 |
if (!isflt(word)) { |
240 |
fprintf(stderr, "%s: garbled data value: %s\n", |
241 |
file, word); |
242 |
exit(1); |
243 |
} |
244 |
*dp++ = atof(word); |
245 |
size--; |
246 |
} |
247 |
if (size == (m+n+1)*pointsize) { /* no border after all */ |
248 |
dp = (RREAL *)realloc(datarec.data, |
249 |
m*n*pointsize*sizeof(RREAL)); |
250 |
if (dp != NULL) |
251 |
datarec.data = dp; |
252 |
datarec.flags &= ~HASBORDER; |
253 |
datarec.m = m; |
254 |
datarec.n = n; |
255 |
size = 0; |
256 |
} |
257 |
if (datarec.m < 2 || datarec.n < 2 || size != 0 || |
258 |
fgetword(word, sizeof(word), fp) != NULL) { |
259 |
fputs(file, stderr); |
260 |
fputs(": bad number of data points\n", stderr); |
261 |
exit(1); |
262 |
} |
263 |
fclose(fp); |
264 |
} |
265 |
|
266 |
|
267 |
double |
268 |
l_dataval( /* return recorded data value */ |
269 |
char *nam |
270 |
) |
271 |
{ |
272 |
double u, v; |
273 |
int i, j; |
274 |
RREAL *dp; |
275 |
double d00, d01, d10, d11; |
276 |
/* compute coordinates */ |
277 |
u = argument(1); v = argument(2); |
278 |
if (datarec.flags & HASBORDER) { |
279 |
i = u *= datarec.m-1; |
280 |
j = v *= datarec.n-1; |
281 |
} else { |
282 |
i = u = u*datarec.m - .5; |
283 |
j = v = v*datarec.n - .5; |
284 |
} |
285 |
if (i < 0) i = 0; |
286 |
else if (i > datarec.m-2) i = datarec.m-2; |
287 |
if (j < 0) j = 0; |
288 |
else if (j > datarec.n-2) j = datarec.n-2; |
289 |
/* compute value */ |
290 |
if (datarec.flags & TRIPLETS) { |
291 |
dp = datarec.data + 3*(j*datarec.m + i); |
292 |
if (nam == ZNAME) |
293 |
dp += 2; |
294 |
else if (nam == YNAME) |
295 |
dp++; |
296 |
d00 = dp[0]; d01 = dp[3]; |
297 |
dp += 3*datarec.m; |
298 |
d10 = dp[0]; d11 = dp[3]; |
299 |
} else { |
300 |
dp = datarec.data + j*datarec.m + i; |
301 |
d00 = dp[0]; d01 = dp[1]; |
302 |
dp += datarec.m; |
303 |
d10 = dp[0]; d11 = dp[1]; |
304 |
} |
305 |
/* bilinear interpolation */ |
306 |
return((j+1-v)*((i+1-u)*d00+(u-i)*d01)+(v-j)*((i+1-u)*d10+(u-i)*d11)); |
307 |
} |
308 |
|
309 |
|
310 |
void |
311 |
putobjrow( /* output vertex row to .OBJ */ |
312 |
POINT *rp, |
313 |
int n |
314 |
) |
315 |
{ |
316 |
static FVECT prevNorm; |
317 |
|
318 |
for ( ; n-- >= 0; rp++) { |
319 |
if (!rp->valid) |
320 |
continue; |
321 |
fputs("v ", stdout); |
322 |
pvect(rp->p); |
323 |
rp->valid = ++nverts; |
324 |
printf("\tvt %.9g %.9g\n", rp->uv[0], rp->uv[1]); |
325 |
if (!smooth || ZEROVECT(rp->n)) |
326 |
rp->nvalid = 0; |
327 |
else if (VABSEQ(rp->n, prevNorm)) |
328 |
rp->nvalid = nnorms; |
329 |
else { |
330 |
printf("\tvn %.9g %.9g %.9g\n", |
331 |
rp->n[0], rp->n[1], rp->n[2]); |
332 |
rp->nvalid = ++nnorms; |
333 |
VCOPY(prevNorm, rp->n); |
334 |
} |
335 |
} |
336 |
} |
337 |
|
338 |
|
339 |
void |
340 |
putobjvert( /* put out OBJ vertex index triplet */ |
341 |
POINT *p |
342 |
) |
343 |
{ |
344 |
int pti = p->valid ? p->valid-nverts-1 : 0; |
345 |
int ni = p->nvalid ? p->nvalid-nnorms-1 : 0; |
346 |
|
347 |
printf(" %d/%d/%d", pti, pti, ni); |
348 |
} |
349 |
|
350 |
|
351 |
void |
352 |
putsquare( /* put out a square */ |
353 |
POINT *p0, |
354 |
POINT *p1, |
355 |
POINT *p2, |
356 |
POINT *p3 |
357 |
) |
358 |
{ |
359 |
static int nout = 0; |
360 |
FVECT norm[4]; |
361 |
int axis; |
362 |
FVECT v1, v2, vc1, vc2; |
363 |
int ok1, ok2; |
364 |
|
365 |
if (rev) { /* reverse normals? */ |
366 |
POINT *pt = p1; p1 = p2; p2 = pt; |
367 |
} |
368 |
/* compute exact normals */ |
369 |
ok1 = (p0->valid && p1->valid && p2->valid); |
370 |
if (ok1) { |
371 |
VSUB(v1, p1->p, p0->p); |
372 |
VSUB(v2, p2->p, p0->p); |
373 |
fcross(vc1, v1, v2); |
374 |
ok1 = (normalize(vc1) != 0.0); |
375 |
} |
376 |
ok2 = (p1->valid && p2->valid && p3->valid); |
377 |
if (ok2) { |
378 |
VSUB(v1, p2->p, p3->p); |
379 |
VSUB(v2, p1->p, p3->p); |
380 |
fcross(vc2, v1, v2); |
381 |
ok2 = (normalize(vc2) != 0.0); |
382 |
} |
383 |
if (!(ok1 | ok2)) |
384 |
return; |
385 |
if (objout) { /* output .OBJ faces */ |
386 |
if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) { |
387 |
putc('f', stdout); |
388 |
putobjvert(p0); putobjvert(p1); |
389 |
putobjvert(p3); putobjvert(p2); |
390 |
putc('\n', stdout); |
391 |
return; |
392 |
} |
393 |
if (ok1) { |
394 |
putc('f', stdout); |
395 |
putobjvert(p0); putobjvert(p1); putobjvert(p2); |
396 |
putc('\n', stdout); |
397 |
} |
398 |
if (ok2) { |
399 |
putc('f', stdout); |
400 |
putobjvert(p2); putobjvert(p1); putobjvert(p3); |
401 |
putc('\n', stdout); |
402 |
} |
403 |
return; |
404 |
} |
405 |
/* compute normal interpolation */ |
406 |
axis = norminterp(norm, p0, p1, p2, p3); |
407 |
|
408 |
/* put out quadrilateral? */ |
409 |
if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) { |
410 |
printf("\n%s ", modname); |
411 |
if (axis != -1) { |
412 |
printf("texfunc %s\n%s\n", texname, tsargs); |
413 |
printf("0\n13\t%d\n", axis); |
414 |
pvect(norm[0]); |
415 |
pvect(norm[1]); |
416 |
pvect(norm[2]); |
417 |
fvsum(v1, norm[3], vc1, -0.5); |
418 |
fvsum(v1, v1, vc2, -0.5); |
419 |
pvect(v1); |
420 |
printf("\n%s ", texname); |
421 |
} |
422 |
printf("polygon %s.%d\n", surfname, ++nout); |
423 |
printf("0\n0\n12\n"); |
424 |
pvect(p0->p); |
425 |
pvect(p1->p); |
426 |
pvect(p3->p); |
427 |
pvect(p2->p); |
428 |
return; |
429 |
} |
430 |
/* put out triangles? */ |
431 |
if (ok1) { |
432 |
printf("\n%s ", modname); |
433 |
if (axis != -1) { |
434 |
printf("texfunc %s\n%s\n", texname, tsargs); |
435 |
printf("0\n13\t%d\n", axis); |
436 |
pvect(norm[0]); |
437 |
pvect(norm[1]); |
438 |
pvect(norm[2]); |
439 |
fvsum(v1, norm[3], vc1, -1.0); |
440 |
pvect(v1); |
441 |
printf("\n%s ", texname); |
442 |
} |
443 |
printf("polygon %s.%d\n", surfname, ++nout); |
444 |
printf("0\n0\n9\n"); |
445 |
pvect(p0->p); |
446 |
pvect(p1->p); |
447 |
pvect(p2->p); |
448 |
} |
449 |
if (ok2) { |
450 |
printf("\n%s ", modname); |
451 |
if (axis != -1) { |
452 |
printf("texfunc %s\n%s\n", texname, tsargs); |
453 |
printf("0\n13\t%d\n", axis); |
454 |
pvect(norm[0]); |
455 |
pvect(norm[1]); |
456 |
pvect(norm[2]); |
457 |
fvsum(v2, norm[3], vc2, -1.0); |
458 |
pvect(v2); |
459 |
printf("\n%s ", texname); |
460 |
} |
461 |
printf("polygon %s.%d\n", surfname, ++nout); |
462 |
printf("0\n0\n9\n"); |
463 |
pvect(p2->p); |
464 |
pvect(p1->p); |
465 |
pvect(p3->p); |
466 |
} |
467 |
} |
468 |
|
469 |
|
470 |
void |
471 |
comprow( /* compute row of values */ |
472 |
double s, |
473 |
POINT *row, |
474 |
int siz |
475 |
) |
476 |
{ |
477 |
double st[2]; |
478 |
int end; |
479 |
int checkvalid; |
480 |
int i; |
481 |
|
482 |
if (smooth) { |
483 |
i = -1; /* compute one past each end */ |
484 |
end = siz+1; |
485 |
} else { |
486 |
if (s < -FTINY || s > 1.0+FTINY) |
487 |
return; |
488 |
i = 0; |
489 |
end = siz; |
490 |
} |
491 |
st[0] = s; |
492 |
checkvalid = (fundefined(VNAME) == 2); |
493 |
while (i <= end) { |
494 |
st[1] = (double)i/siz; |
495 |
if (checkvalid && funvalue(VNAME, 2, st) <= 0.0) { |
496 |
row[i].valid = 0; |
497 |
row[i].p[0] = row[i].p[1] = row[i].p[2] = 0.0; |
498 |
row[i].uv[0] = row[i].uv[1] = 0.0; |
499 |
} else { |
500 |
row[i].valid = 1; |
501 |
row[i].p[0] = funvalue(XNAME, 2, st); |
502 |
row[i].p[1] = funvalue(YNAME, 2, st); |
503 |
row[i].p[2] = funvalue(ZNAME, 2, st); |
504 |
row[i].uv[0] = st[0]; |
505 |
row[i].uv[1] = st[1]; |
506 |
} |
507 |
i++; |
508 |
} |
509 |
} |
510 |
|
511 |
|
512 |
void |
513 |
compnorms( /* compute row of averaged normals */ |
514 |
POINT *r0, |
515 |
POINT *r1, |
516 |
POINT *r2, |
517 |
int siz |
518 |
) |
519 |
{ |
520 |
FVECT v1, v2; |
521 |
|
522 |
if (!smooth) /* not needed if no smoothing */ |
523 |
return; |
524 |
/* compute row 1 normals */ |
525 |
while (siz-- >= 0) { |
526 |
if (!r1[0].valid) |
527 |
goto skip; |
528 |
if (!r0[0].valid) { |
529 |
if (!r2[0].valid) { |
530 |
r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0; |
531 |
goto skip; |
532 |
} |
533 |
fvsum(v1, r2[0].p, r1[0].p, -1.0); |
534 |
} else if (!r2[0].valid) |
535 |
fvsum(v1, r1[0].p, r0[0].p, -1.0); |
536 |
else |
537 |
fvsum(v1, r2[0].p, r0[0].p, -1.0); |
538 |
if (!r1[-1].valid) { |
539 |
if (!r1[1].valid) { |
540 |
r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0; |
541 |
goto skip; |
542 |
} |
543 |
fvsum(v2, r1[1].p, r1[0].p, -1.0); |
544 |
} else if (!r1[1].valid) |
545 |
fvsum(v2, r1[0].p, r1[-1].p, -1.0); |
546 |
else |
547 |
fvsum(v2, r1[1].p, r1[-1].p, -1.0); |
548 |
if (rev) |
549 |
fcross(r1[0].n, v2, v1); |
550 |
else |
551 |
fcross(r1[0].n, v1, v2); |
552 |
normalize(r1[0].n); |
553 |
skip: |
554 |
r0++; r1++; r2++; |
555 |
} |
556 |
} |
557 |
|
558 |
|
559 |
int |
560 |
norminterp( /* compute normal interpolation */ |
561 |
FVECT resmat[4], |
562 |
POINT *p0, |
563 |
POINT *p1, |
564 |
POINT *p2, |
565 |
POINT *p3 |
566 |
) |
567 |
{ |
568 |
#define u ((ax+1)%3) |
569 |
#define v ((ax+2)%3) |
570 |
|
571 |
int ax; |
572 |
MAT4 eqnmat; |
573 |
FVECT v1; |
574 |
int i, j; |
575 |
|
576 |
if (!smooth) /* no interpolation if no smoothing */ |
577 |
return(-1); |
578 |
/* find dominant axis */ |
579 |
VCOPY(v1, p0->n); |
580 |
fvsum(v1, v1, p1->n, 1.0); |
581 |
fvsum(v1, v1, p2->n, 1.0); |
582 |
fvsum(v1, v1, p3->n, 1.0); |
583 |
ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1; |
584 |
ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2; |
585 |
/* assign equation matrix */ |
586 |
eqnmat[0][0] = p0->p[u]*p0->p[v]; |
587 |
eqnmat[0][1] = p0->p[u]; |
588 |
eqnmat[0][2] = p0->p[v]; |
589 |
eqnmat[0][3] = 1.0; |
590 |
eqnmat[1][0] = p1->p[u]*p1->p[v]; |
591 |
eqnmat[1][1] = p1->p[u]; |
592 |
eqnmat[1][2] = p1->p[v]; |
593 |
eqnmat[1][3] = 1.0; |
594 |
eqnmat[2][0] = p2->p[u]*p2->p[v]; |
595 |
eqnmat[2][1] = p2->p[u]; |
596 |
eqnmat[2][2] = p2->p[v]; |
597 |
eqnmat[2][3] = 1.0; |
598 |
eqnmat[3][0] = p3->p[u]*p3->p[v]; |
599 |
eqnmat[3][1] = p3->p[u]; |
600 |
eqnmat[3][2] = p3->p[v]; |
601 |
eqnmat[3][3] = 1.0; |
602 |
/* invert matrix (solve system) */ |
603 |
if (!invmat4(eqnmat, eqnmat)) |
604 |
return(-1); /* no solution */ |
605 |
/* compute result matrix */ |
606 |
for (j = 0; j < 4; j++) |
607 |
for (i = 0; i < 3; i++) |
608 |
resmat[j][i] = eqnmat[j][0]*p0->n[i] + |
609 |
eqnmat[j][1]*p1->n[i] + |
610 |
eqnmat[j][2]*p2->n[i] + |
611 |
eqnmat[j][3]*p3->n[i]; |
612 |
return(ax); |
613 |
|
614 |
#undef u |
615 |
#undef v |
616 |
} |
617 |
|
618 |
|
619 |
double |
620 |
l_hermite(char *nm) |
621 |
{ |
622 |
double t; |
623 |
|
624 |
t = argument(5); |
625 |
return( argument(1)*((2.0*t-3.0)*t*t+1.0) + |
626 |
argument(2)*(-2.0*t+3.0)*t*t + |
627 |
argument(3)*((t-2.0)*t+1.0)*t + |
628 |
argument(4)*(t-1.0)*t*t ); |
629 |
} |
630 |
|
631 |
|
632 |
double |
633 |
l_bezier(char *nm) |
634 |
{ |
635 |
double t; |
636 |
|
637 |
t = argument(5); |
638 |
return( argument(1) * (1.+t*(-3.+t*(3.-t))) + |
639 |
argument(2) * 3.*t*(1.+t*(-2.+t)) + |
640 |
argument(3) * 3.*t*t*(1.-t) + |
641 |
argument(4) * t*t*t ); |
642 |
} |
643 |
|
644 |
|
645 |
double |
646 |
l_bspline(char *nm) |
647 |
{ |
648 |
double t; |
649 |
|
650 |
t = argument(5); |
651 |
return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) + |
652 |
argument(2) * (2./3.+t*t*(-1.+1./2.*t)) + |
653 |
argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) + |
654 |
argument(4) * (1./6.*t*t*t) ); |
655 |
} |