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