1 |
– |
/* Copyright (c) 1989 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* gensurf.c - program to generate functional surfaces |
6 |
|
* |
11 |
|
* rule applied to (s,t). |
12 |
|
* |
13 |
|
* 4/3/87 |
14 |
+ |
* |
15 |
+ |
* 4/16/02 Added conditional vertex output |
16 |
|
*/ |
17 |
|
|
18 |
< |
#include <stdio.h> |
20 |
< |
#include "fvect.h" |
18 |
> |
#include "standard.h" |
19 |
|
|
20 |
< |
#define XNAME "X_" /* x function name */ |
21 |
< |
#define YNAME "Y_" /* y function name */ |
22 |
< |
#define ZNAME "Z_" /* z function name */ |
20 |
> |
#include "paths.h" |
21 |
> |
#include "resolu.h" |
22 |
> |
#include "rterror.h" |
23 |
> |
#include "calcomp.h" |
24 |
|
|
25 |
< |
#define PI 3.14159265358979323846 |
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 |
< |
#define FTINY 1e-7 |
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[] = "%15.9g %15.9g %15.9g\n"; |
38 |
< |
char tsargs[] = "4 surf_dx surf_dy surf_dz surf.cal\n"; |
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 |
|
|
44 |
|
char *modname, *surfname; |
45 |
|
|
46 |
< |
double funvalue(), l_hermite(), argument(); |
46 |
> |
/* recorded data flags */ |
47 |
> |
#define HASBORDER 01 |
48 |
> |
#define TRIPLETS 02 |
49 |
> |
/* a data structure */ |
50 |
> |
struct { |
51 |
> |
int flags; /* data type */ |
52 |
> |
short m, n; /* number of s and t values */ |
53 |
> |
RREAL *data; /* the data itself, s major sort */ |
54 |
> |
} datarec; /* our recorded data */ |
55 |
|
|
56 |
+ |
/* XXX this is redundant with rt/noise3.c, should go to a library */ |
57 |
+ |
double l_hermite(), l_bezier(), l_bspline(), l_dataval(); |
58 |
+ |
|
59 |
|
typedef struct { |
60 |
+ |
int valid; /* point is valid (vertex number) */ |
61 |
+ |
int nvalid; /* normal is valid */ |
62 |
|
FVECT p; /* vertex position */ |
63 |
|
FVECT n; /* average normal */ |
64 |
+ |
RREAL uv[2]; /* (u,v) position */ |
65 |
|
} POINT; |
66 |
|
|
67 |
+ |
int nverts = 0; /* vertex output count */ |
68 |
+ |
int nnorms = 0; /* normal output count */ |
69 |
|
|
70 |
+ |
void loaddata(char *file, int m, int n, int pointsize); |
71 |
+ |
double l_dataval(char *nam); |
72 |
+ |
void putobjrow(POINT *rp, int n); |
73 |
+ |
void putobjvert(POINT *p); |
74 |
+ |
void putsquare(POINT *p0, POINT *p1, POINT *p2, POINT *p3); |
75 |
+ |
void comprow(double s, POINT *row, int siz); |
76 |
+ |
void compnorms(POINT *r0, POINT *r1, POINT *r2, int siz); |
77 |
+ |
int norminterp(FVECT resmat[4], POINT *p0, POINT *p1, POINT *p2, POINT *p3); |
78 |
+ |
|
79 |
+ |
|
80 |
+ |
int |
81 |
|
main(argc, argv) |
82 |
|
int argc; |
83 |
|
char *argv[]; |
86 |
|
int i, j, m, n; |
87 |
|
char stmp[256]; |
88 |
|
|
89 |
< |
varset("PI", PI); |
90 |
< |
funset("hermite", 5, l_hermite); |
89 |
> |
varset("PI", ':', PI); |
90 |
> |
funset("hermite", 5, ':', l_hermite); |
91 |
> |
funset("bezier", 5, ':', l_bezier); |
92 |
> |
funset("bspline", 5, ':', l_bspline); |
93 |
|
|
94 |
|
if (argc < 8) |
95 |
|
goto userror; |
96 |
|
|
97 |
|
for (i = 8; i < argc; i++) |
98 |
|
if (!strcmp(argv[i], "-e")) |
99 |
< |
scompile(NULL, argv[++i]); |
99 |
> |
scompile(argv[++i], NULL, 0); |
100 |
|
else if (!strcmp(argv[i], "-f")) |
101 |
|
fcompile(argv[++i]); |
102 |
|
else if (!strcmp(argv[i], "-s")) |
103 |
|
smooth++; |
104 |
+ |
else if (!strcmp(argv[i], "-o")) |
105 |
+ |
objout++; |
106 |
|
else |
107 |
|
goto userror; |
108 |
|
|
109 |
|
modname = argv[1]; |
110 |
|
surfname = argv[2]; |
76 |
– |
sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]); |
77 |
– |
scompile(NULL, stmp); |
78 |
– |
sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]); |
79 |
– |
scompile(NULL, stmp); |
80 |
– |
sprintf(stmp, "%s(s,t)=%s;", ZNAME, argv[5]); |
81 |
– |
scompile(NULL, stmp); |
111 |
|
m = atoi(argv[6]); |
112 |
|
n = atoi(argv[7]); |
113 |
|
if (m <= 0 || n <= 0) |
114 |
|
goto userror; |
115 |
< |
|
115 |
> |
if (!strcmp(argv[5], "-") || access(argv[5], 4) == 0) { /* file? */ |
116 |
> |
funset(ZNAME, 2, ':', l_dataval); |
117 |
> |
if (!strcmp(argv[5],argv[3]) && !strcmp(argv[5],argv[4])) { |
118 |
> |
loaddata(argv[5], m, n, 3); |
119 |
> |
funset(XNAME, 2, ':', l_dataval); |
120 |
> |
funset(YNAME, 2, ':', l_dataval); |
121 |
> |
} else { |
122 |
> |
loaddata(argv[5], m, n, 1); |
123 |
> |
sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]); |
124 |
> |
scompile(stmp, NULL, 0); |
125 |
> |
sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]); |
126 |
> |
scompile(stmp, NULL, 0); |
127 |
> |
} |
128 |
> |
} else { |
129 |
> |
sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]); |
130 |
> |
scompile(stmp, NULL, 0); |
131 |
> |
sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]); |
132 |
> |
scompile(stmp, NULL, 0); |
133 |
> |
sprintf(stmp, "%s(s,t)=%s;", ZNAME, argv[5]); |
134 |
> |
scompile(stmp, NULL, 0); |
135 |
> |
} |
136 |
|
row0 = (POINT *)malloc((n+3)*sizeof(POINT)); |
137 |
|
row1 = (POINT *)malloc((n+3)*sizeof(POINT)); |
138 |
|
row2 = (POINT *)malloc((n+3)*sizeof(POINT)); |
142 |
|
} |
143 |
|
row0++; row1++; row2++; |
144 |
|
/* print header */ |
145 |
< |
printhead(argc, argv); |
145 |
> |
fputs("# ", stdout); |
146 |
> |
printargs(argc, argv, stdout); |
147 |
> |
eclock = 0; |
148 |
|
/* initialize */ |
149 |
|
comprow(-1.0/m, row0, n); |
150 |
|
comprow(0.0, row1, n); |
151 |
|
comprow(1.0/m, row2, n); |
152 |
|
compnorms(row0, row1, row2, n); |
153 |
+ |
if (objout) { |
154 |
+ |
printf("\nusemtl %s\n\n", modname); |
155 |
+ |
putobjrow(row1, n); |
156 |
+ |
} |
157 |
|
/* for each row */ |
158 |
|
for (i = 0; i < m; i++) { |
159 |
|
/* compute next row */ |
163 |
|
row2 = rp; |
164 |
|
comprow((double)(i+2)/m, row2, n); |
165 |
|
compnorms(row0, row1, row2, n); |
166 |
+ |
if (objout) |
167 |
+ |
putobjrow(row1, n); |
168 |
|
|
169 |
|
for (j = 0; j < n; j++) { |
170 |
+ |
int orient = (j & 1); |
171 |
|
/* put polygons */ |
172 |
< |
if ((i+j) & 1) |
172 |
> |
if (!(row0[j].valid && row1[j+1].valid)) |
173 |
> |
orient = 1; |
174 |
> |
else if (!(row1[j].valid && row0[j+1].valid)) |
175 |
> |
orient = 0; |
176 |
> |
if (orient) |
177 |
|
putsquare(&row0[j], &row1[j], |
178 |
|
&row0[j+1], &row1[j+1]); |
179 |
|
else |
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
< |
quit(0); |
185 |
> |
return 0; |
186 |
|
|
187 |
|
userror: |
188 |
|
fprintf(stderr, "Usage: %s material name ", argv[0]); |
189 |
< |
fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-e expr][-f file]\n"); |
190 |
< |
quit(1); |
189 |
> |
fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-o][-e expr][-f file]\n"); |
190 |
> |
return 1; |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
< |
putsquare(p0, p1, p2, p3) /* put out a square */ |
195 |
< |
POINT *p0, *p1, *p2, *p3; |
194 |
> |
void |
195 |
> |
loaddata( /* load point data from file */ |
196 |
> |
char *file, |
197 |
> |
int m, |
198 |
> |
int n, |
199 |
> |
int pointsize |
200 |
> |
) |
201 |
|
{ |
202 |
+ |
FILE *fp; |
203 |
+ |
char word[64]; |
204 |
+ |
int size; |
205 |
+ |
RREAL *dp; |
206 |
+ |
|
207 |
+ |
datarec.flags = HASBORDER; /* assume border values */ |
208 |
+ |
datarec.m = m+1; |
209 |
+ |
datarec.n = n+1; |
210 |
+ |
size = datarec.m*datarec.n*pointsize; |
211 |
+ |
if (pointsize == 3) |
212 |
+ |
datarec.flags |= TRIPLETS; |
213 |
+ |
dp = (RREAL *)malloc(size*sizeof(RREAL)); |
214 |
+ |
if ((datarec.data = dp) == NULL) { |
215 |
+ |
fputs("Out of memory\n", stderr); |
216 |
+ |
exit(1); |
217 |
+ |
} |
218 |
+ |
if (!strcmp(file, "-")) { |
219 |
+ |
file = "<stdin>"; |
220 |
+ |
fp = stdin; |
221 |
+ |
} else if ((fp = fopen(file, "r")) == NULL) { |
222 |
+ |
fputs(file, stderr); |
223 |
+ |
fputs(": cannot open\n", stderr); |
224 |
+ |
exit(1); |
225 |
+ |
} |
226 |
+ |
while (size > 0 && fgetword(word, sizeof(word), fp) != NULL) { |
227 |
+ |
if (!isflt(word)) { |
228 |
+ |
fprintf(stderr, "%s: garbled data value: %s\n", |
229 |
+ |
file, word); |
230 |
+ |
exit(1); |
231 |
+ |
} |
232 |
+ |
*dp++ = atof(word); |
233 |
+ |
size--; |
234 |
+ |
} |
235 |
+ |
if (size == (m+n+1)*pointsize) { /* no border after all */ |
236 |
+ |
dp = (RREAL *)realloc(datarec.data, |
237 |
+ |
m*n*pointsize*sizeof(RREAL)); |
238 |
+ |
if (dp != NULL) |
239 |
+ |
datarec.data = dp; |
240 |
+ |
datarec.flags &= ~HASBORDER; |
241 |
+ |
datarec.m = m; |
242 |
+ |
datarec.n = n; |
243 |
+ |
size = 0; |
244 |
+ |
} |
245 |
+ |
if (datarec.m < 2 || datarec.n < 2 || size != 0 || |
246 |
+ |
fgetword(word, sizeof(word), fp) != NULL) { |
247 |
+ |
fputs(file, stderr); |
248 |
+ |
fputs(": bad number of data points\n", stderr); |
249 |
+ |
exit(1); |
250 |
+ |
} |
251 |
+ |
fclose(fp); |
252 |
+ |
} |
253 |
+ |
|
254 |
+ |
|
255 |
+ |
double |
256 |
+ |
l_dataval( /* return recorded data value */ |
257 |
+ |
char *nam |
258 |
+ |
) |
259 |
+ |
{ |
260 |
+ |
double u, v; |
261 |
+ |
int i, j; |
262 |
+ |
RREAL *dp; |
263 |
+ |
double d00, d01, d10, d11; |
264 |
+ |
/* compute coordinates */ |
265 |
+ |
u = argument(1); v = argument(2); |
266 |
+ |
if (datarec.flags & HASBORDER) { |
267 |
+ |
i = u *= datarec.m-1; |
268 |
+ |
j = v *= datarec.n-1; |
269 |
+ |
} else { |
270 |
+ |
i = u = u*datarec.m - .5; |
271 |
+ |
j = v = v*datarec.n - .5; |
272 |
+ |
} |
273 |
+ |
if (i < 0) i = 0; |
274 |
+ |
else if (i > datarec.m-2) i = datarec.m-2; |
275 |
+ |
if (j < 0) j = 0; |
276 |
+ |
else if (j > datarec.n-2) j = datarec.n-2; |
277 |
+ |
/* compute value */ |
278 |
+ |
if (datarec.flags & TRIPLETS) { |
279 |
+ |
dp = datarec.data + 3*(j*datarec.m + i); |
280 |
+ |
if (nam == ZNAME) |
281 |
+ |
dp += 2; |
282 |
+ |
else if (nam == YNAME) |
283 |
+ |
dp++; |
284 |
+ |
d00 = dp[0]; d01 = dp[3]; |
285 |
+ |
dp += 3*datarec.m; |
286 |
+ |
d10 = dp[0]; d11 = dp[3]; |
287 |
+ |
} else { |
288 |
+ |
dp = datarec.data + j*datarec.m + i; |
289 |
+ |
d00 = dp[0]; d01 = dp[1]; |
290 |
+ |
dp += datarec.m; |
291 |
+ |
d10 = dp[0]; d11 = dp[1]; |
292 |
+ |
} |
293 |
+ |
/* bilinear interpolation */ |
294 |
+ |
return((j+1-v)*((i+1-u)*d00+(u-i)*d01)+(v-j)*((i+1-u)*d10+(u-i)*d11)); |
295 |
+ |
} |
296 |
+ |
|
297 |
+ |
|
298 |
+ |
void |
299 |
+ |
putobjrow( /* output vertex row to .OBJ */ |
300 |
+ |
POINT *rp, |
301 |
+ |
int n |
302 |
+ |
) |
303 |
+ |
{ |
304 |
+ |
for ( ; n-- >= 0; rp++) { |
305 |
+ |
if (!rp->valid) |
306 |
+ |
continue; |
307 |
+ |
fputs("v ", stdout); |
308 |
+ |
pvect(rp->p); |
309 |
+ |
if (smooth && !ZEROVECT(rp->n)) { |
310 |
+ |
printf("\tvn %.9g %.9g %.9g\n", |
311 |
+ |
rp->n[0], rp->n[1], rp->n[2]); |
312 |
+ |
rp->nvalid = ++nnorms; |
313 |
+ |
} else |
314 |
+ |
rp->nvalid = 0; |
315 |
+ |
printf("\tvt %.9g %.9g\n", rp->uv[0], rp->uv[1]); |
316 |
+ |
rp->valid = ++nverts; |
317 |
+ |
} |
318 |
+ |
} |
319 |
+ |
|
320 |
+ |
|
321 |
+ |
void |
322 |
+ |
putobjvert( /* put out OBJ vertex index triplet */ |
323 |
+ |
POINT *p |
324 |
+ |
) |
325 |
+ |
{ |
326 |
+ |
int pti = p->valid ? p->valid-nverts-1 : 0; |
327 |
+ |
int ni = p->nvalid ? p->nvalid-nnorms-1 : 0; |
328 |
+ |
|
329 |
+ |
printf(" %d/%d/%d", pti, pti, ni); |
330 |
+ |
} |
331 |
+ |
|
332 |
+ |
|
333 |
+ |
void |
334 |
+ |
putsquare( /* put out a square */ |
335 |
+ |
POINT *p0, |
336 |
+ |
POINT *p1, |
337 |
+ |
POINT *p2, |
338 |
+ |
POINT *p3 |
339 |
+ |
) |
340 |
+ |
{ |
341 |
|
static int nout = 0; |
342 |
|
FVECT norm[4]; |
343 |
|
int axis; |
344 |
|
FVECT v1, v2, vc1, vc2; |
345 |
|
int ok1, ok2; |
346 |
|
/* compute exact normals */ |
347 |
< |
fvsum(v1, p1->p, p0->p, -1.0); |
348 |
< |
fvsum(v2, p2->p, p0->p, -1.0); |
349 |
< |
fcross(vc1, v1, v2); |
350 |
< |
ok1 = normalize(vc1) != 0.0; |
351 |
< |
fvsum(v1, p2->p, p3->p, -1.0); |
352 |
< |
fvsum(v2, p1->p, p3->p, -1.0); |
353 |
< |
fcross(vc2, v1, v2); |
354 |
< |
ok2 = normalize(vc2) != 0.0; |
347 |
> |
ok1 = (p0->valid && p1->valid && p2->valid); |
348 |
> |
if (ok1) { |
349 |
> |
VSUB(v1, p1->p, p0->p); |
350 |
> |
VSUB(v2, p2->p, p0->p); |
351 |
> |
fcross(vc1, v1, v2); |
352 |
> |
ok1 = (normalize(vc1) != 0.0); |
353 |
> |
} |
354 |
> |
ok2 = (p1->valid && p2->valid && p3->valid); |
355 |
> |
if (ok2) { |
356 |
> |
VSUB(v1, p2->p, p3->p); |
357 |
> |
VSUB(v2, p1->p, p3->p); |
358 |
> |
fcross(vc2, v1, v2); |
359 |
> |
ok2 = (normalize(vc2) != 0.0); |
360 |
> |
} |
361 |
|
if (!(ok1 | ok2)) |
362 |
|
return; |
363 |
+ |
if (objout) { /* output .OBJ faces */ |
364 |
+ |
if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) { |
365 |
+ |
putc('f', stdout); |
366 |
+ |
putobjvert(p0); putobjvert(p1); |
367 |
+ |
putobjvert(p3); putobjvert(p2); |
368 |
+ |
putc('\n', stdout); |
369 |
+ |
return; |
370 |
+ |
} |
371 |
+ |
if (ok1) { |
372 |
+ |
putc('f', stdout); |
373 |
+ |
putobjvert(p0); putobjvert(p1); putobjvert(p2); |
374 |
+ |
putc('\n', stdout); |
375 |
+ |
} |
376 |
+ |
if (ok2) { |
377 |
+ |
putc('f', stdout); |
378 |
+ |
putobjvert(p2); putobjvert(p1); putobjvert(p3); |
379 |
+ |
putc('\n', stdout); |
380 |
+ |
} |
381 |
+ |
return; |
382 |
+ |
} |
383 |
|
/* compute normal interpolation */ |
384 |
|
axis = norminterp(norm, p0, p1, p2, p3); |
385 |
|
|
387 |
|
if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) { |
388 |
|
printf("\n%s ", modname); |
389 |
|
if (axis != -1) { |
390 |
< |
printf("texfunc %s\n", texname); |
159 |
< |
printf(tsargs); |
390 |
> |
printf("texfunc %s\n%s\n", texname, tsargs); |
391 |
|
printf("0\n13\t%d\n", axis); |
392 |
|
pvect(norm[0]); |
393 |
|
pvect(norm[1]); |
409 |
|
if (ok1) { |
410 |
|
printf("\n%s ", modname); |
411 |
|
if (axis != -1) { |
412 |
< |
printf("texfunc %s\n", texname); |
182 |
< |
printf(tsargs); |
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]); |
427 |
|
if (ok2) { |
428 |
|
printf("\n%s ", modname); |
429 |
|
if (axis != -1) { |
430 |
< |
printf("texfunc %s\n", texname); |
201 |
< |
printf(tsargs); |
430 |
> |
printf("texfunc %s\n%s\n", texname, tsargs); |
431 |
|
printf("0\n13\t%d\n", axis); |
432 |
|
pvect(norm[0]); |
433 |
|
pvect(norm[1]); |
445 |
|
} |
446 |
|
|
447 |
|
|
448 |
< |
comprow(s, row, siz) /* compute row of values */ |
449 |
< |
double s; |
450 |
< |
register POINT *row; |
451 |
< |
int siz; |
448 |
> |
void |
449 |
> |
comprow( /* compute row of values */ |
450 |
> |
double s, |
451 |
> |
POINT *row, |
452 |
> |
int siz |
453 |
> |
) |
454 |
|
{ |
455 |
|
double st[2]; |
456 |
< |
register int i; |
457 |
< |
/* compute one past each end */ |
456 |
> |
int end; |
457 |
> |
int checkvalid; |
458 |
> |
int i; |
459 |
> |
|
460 |
> |
if (smooth) { |
461 |
> |
i = -1; /* compute one past each end */ |
462 |
> |
end = siz+1; |
463 |
> |
} else { |
464 |
> |
if (s < -FTINY || s > 1.0+FTINY) |
465 |
> |
return; |
466 |
> |
i = 0; |
467 |
> |
end = siz; |
468 |
> |
} |
469 |
|
st[0] = s; |
470 |
< |
for (i = -1; i <= siz+1; i++) { |
470 |
> |
checkvalid = (fundefined(VNAME) == 2); |
471 |
> |
while (i <= end) { |
472 |
|
st[1] = (double)i/siz; |
473 |
< |
row[i].p[0] = funvalue(XNAME, 2, st); |
474 |
< |
row[i].p[1] = funvalue(YNAME, 2, st); |
475 |
< |
row[i].p[2] = funvalue(ZNAME, 2, st); |
473 |
> |
if (checkvalid && funvalue(VNAME, 2, st) <= 0.0) { |
474 |
> |
row[i].valid = 0; |
475 |
> |
row[i].p[0] = row[i].p[1] = row[i].p[2] = 0.0; |
476 |
> |
row[i].uv[0] = row[i].uv[1] = 0.0; |
477 |
> |
} else { |
478 |
> |
row[i].valid = 1; |
479 |
> |
row[i].p[0] = funvalue(XNAME, 2, st); |
480 |
> |
row[i].p[1] = funvalue(YNAME, 2, st); |
481 |
> |
row[i].p[2] = funvalue(ZNAME, 2, st); |
482 |
> |
row[i].uv[0] = st[0]; |
483 |
> |
row[i].uv[1] = st[1]; |
484 |
> |
} |
485 |
> |
i++; |
486 |
|
} |
487 |
|
} |
488 |
|
|
489 |
|
|
490 |
< |
compnorms(r0, r1, r2, siz) /* compute row of averaged normals */ |
491 |
< |
register POINT *r0, *r1, *r2; |
492 |
< |
int siz; |
490 |
> |
void |
491 |
> |
compnorms( /* compute row of averaged normals */ |
492 |
> |
POINT *r0, |
493 |
> |
POINT *r1, |
494 |
> |
POINT *r2, |
495 |
> |
int siz |
496 |
> |
) |
497 |
|
{ |
498 |
< |
FVECT v1, v2, vc; |
242 |
< |
register int i; |
498 |
> |
FVECT v1, v2; |
499 |
|
|
500 |
|
if (!smooth) /* not needed if no smoothing */ |
501 |
|
return; |
502 |
< |
/* compute middle points */ |
502 |
> |
/* compute row 1 normals */ |
503 |
|
while (siz-- >= 0) { |
504 |
< |
fvsum(v1, r2[0].p, r1[0].p, -1.0); |
505 |
< |
fvsum(v2, r1[1].p, r1[0].p, -1.0); |
504 |
> |
if (!r1[0].valid) |
505 |
> |
continue; |
506 |
> |
if (!r0[0].valid) { |
507 |
> |
if (!r2[0].valid) { |
508 |
> |
r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0; |
509 |
> |
continue; |
510 |
> |
} |
511 |
> |
fvsum(v1, r2[0].p, r1[0].p, -1.0); |
512 |
> |
} else if (!r2[0].valid) |
513 |
> |
fvsum(v1, r1[0].p, r0[0].p, -1.0); |
514 |
> |
else |
515 |
> |
fvsum(v1, r2[0].p, r0[0].p, -1.0); |
516 |
> |
if (!r1[-1].valid) { |
517 |
> |
if (!r1[1].valid) { |
518 |
> |
r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0; |
519 |
> |
continue; |
520 |
> |
} |
521 |
> |
fvsum(v2, r1[1].p, r1[0].p, -1.0); |
522 |
> |
} else if (!r1[1].valid) |
523 |
> |
fvsum(v2, r1[0].p, r1[-1].p, -1.0); |
524 |
> |
else |
525 |
> |
fvsum(v2, r1[1].p, r1[-1].p, -1.0); |
526 |
|
fcross(r1[0].n, v1, v2); |
251 |
– |
fvsum(v1, r0[0].p, r1[0].p, -1.0); |
252 |
– |
fcross(vc, v2, v1); |
253 |
– |
fvsum(r1[0].n, r1[0].n, vc, 1.0); |
254 |
– |
fvsum(v2, r1[-1].p, r1[0].p, -1.0); |
255 |
– |
fcross(vc, v1, v2); |
256 |
– |
fvsum(r1[0].n, r1[0].n, vc, 1.0); |
257 |
– |
fvsum(v1, r2[0].p, r1[0].p, -1.0); |
258 |
– |
fcross(vc, v2, v1); |
259 |
– |
fvsum(r1[0].n, r1[0].n, vc, 1.0); |
527 |
|
normalize(r1[0].n); |
528 |
|
r0++; r1++; r2++; |
529 |
|
} |
531 |
|
|
532 |
|
|
533 |
|
int |
534 |
< |
norminterp(resmat, p0, p1, p2, p3) /* compute normal interpolation */ |
535 |
< |
register FVECT resmat[4]; |
536 |
< |
POINT *p0, *p1, *p2, *p3; |
534 |
> |
norminterp( /* compute normal interpolation */ |
535 |
> |
FVECT resmat[4], |
536 |
> |
POINT *p0, |
537 |
> |
POINT *p1, |
538 |
> |
POINT *p2, |
539 |
> |
POINT *p3 |
540 |
> |
) |
541 |
|
{ |
542 |
|
#define u ((ax+1)%3) |
543 |
|
#define v ((ax+2)%3) |
544 |
|
|
545 |
< |
register int ax; |
546 |
< |
double eqnmat[4][4]; |
545 |
> |
int ax; |
546 |
> |
MAT4 eqnmat; |
547 |
|
FVECT v1; |
548 |
< |
register int i, j; |
548 |
> |
int i, j; |
549 |
|
|
550 |
|
if (!smooth) /* no interpolation if no smoothing */ |
551 |
|
return(-1); |
574 |
|
eqnmat[3][2] = p3->p[v]; |
575 |
|
eqnmat[3][3] = 1.0; |
576 |
|
/* invert matrix (solve system) */ |
577 |
< |
if (!invmat(eqnmat, eqnmat)) |
577 |
> |
if (!invmat4(eqnmat, eqnmat)) |
578 |
|
return(-1); /* no solution */ |
579 |
|
/* compute result matrix */ |
580 |
|
for (j = 0; j < 4; j++) |
590 |
|
} |
591 |
|
|
592 |
|
|
593 |
< |
/* |
594 |
< |
* invmat - computes the inverse of mat into inverse. Returns 1 |
324 |
< |
* if there exists an inverse, 0 otherwise. It uses Gaussian Elimination |
325 |
< |
* method. |
326 |
< |
*/ |
327 |
< |
|
328 |
< |
invmat(inverse,mat) |
329 |
< |
double mat[4][4],inverse[4][4]; |
593 |
> |
double |
594 |
> |
l_hermite(char *nm) |
595 |
|
{ |
596 |
< |
#define SWAP(a,b,t) (t=a,a=b,b=t) |
597 |
< |
|
598 |
< |
double m4tmp[4][4]; |
599 |
< |
register int i,j,k; |
600 |
< |
register double temp; |
601 |
< |
|
602 |
< |
bcopy(mat, m4tmp, sizeof(m4tmp)); |
338 |
< |
/* set inverse to identity */ |
339 |
< |
for (i = 0; i < 4; i++) |
340 |
< |
for (j = 0; j < 4; j++) |
341 |
< |
inverse[i][j] = i==j ? 1.0 : 0.0; |
342 |
< |
|
343 |
< |
for(i = 0; i < 4; i++) { |
344 |
< |
/* Look for raw with largest pivot and swap raws */ |
345 |
< |
temp = FTINY; j = -1; |
346 |
< |
for(k = i; k < 4; k++) |
347 |
< |
if(ABS(m4tmp[k][i]) > temp) { |
348 |
< |
temp = ABS(m4tmp[k][i]); |
349 |
< |
j = k; |
350 |
< |
} |
351 |
< |
if(j == -1) /* No replacing raw -> no inverse */ |
352 |
< |
return(0); |
353 |
< |
if (j != i) |
354 |
< |
for(k = 0; k < 4; k++) { |
355 |
< |
SWAP(m4tmp[i][k],m4tmp[j][k],temp); |
356 |
< |
SWAP(inverse[i][k],inverse[j][k],temp); |
357 |
< |
} |
358 |
< |
|
359 |
< |
temp = m4tmp[i][i]; |
360 |
< |
for(k = 0; k < 4; k++) { |
361 |
< |
m4tmp[i][k] /= temp; |
362 |
< |
inverse[i][k] /= temp; |
363 |
< |
} |
364 |
< |
for(j = 0; j < 4; j++) { |
365 |
< |
if(j != i) { |
366 |
< |
temp = m4tmp[j][i]; |
367 |
< |
for(k = 0; k < 4; k++) { |
368 |
< |
m4tmp[j][k] -= m4tmp[i][k]*temp; |
369 |
< |
inverse[j][k] -= inverse[i][k]*temp; |
370 |
< |
} |
371 |
< |
} |
372 |
< |
} |
373 |
< |
} |
374 |
< |
return(1); |
375 |
< |
|
376 |
< |
#undef SWAP |
596 |
> |
double t; |
597 |
> |
|
598 |
> |
t = argument(5); |
599 |
> |
return( argument(1)*((2.0*t-3.0)*t*t+1.0) + |
600 |
> |
argument(2)*(-2.0*t+3.0)*t*t + |
601 |
> |
argument(3)*((t-2.0)*t+1.0)*t + |
602 |
> |
argument(4)*(t-1.0)*t*t ); |
603 |
|
} |
604 |
|
|
605 |
|
|
606 |
< |
eputs(msg) |
607 |
< |
char *msg; |
606 |
> |
double |
607 |
> |
l_bezier(char *nm) |
608 |
|
{ |
609 |
< |
fputs(msg, stderr); |
384 |
< |
} |
609 |
> |
double t; |
610 |
|
|
611 |
< |
|
612 |
< |
wputs(msg) |
613 |
< |
char *msg; |
614 |
< |
{ |
615 |
< |
eputs(msg); |
611 |
> |
t = argument(5); |
612 |
> |
return( argument(1) * (1.+t*(-3.+t*(3.-t))) + |
613 |
> |
argument(2) * 3.*t*(1.+t*(-2.+t)) + |
614 |
> |
argument(3) * 3.*t*t*(1.-t) + |
615 |
> |
argument(4) * t*t*t ); |
616 |
|
} |
617 |
|
|
618 |
|
|
394 |
– |
quit(code) |
395 |
– |
{ |
396 |
– |
exit(code); |
397 |
– |
} |
398 |
– |
|
399 |
– |
|
400 |
– |
printhead(ac, av) /* print command header */ |
401 |
– |
register int ac; |
402 |
– |
register char **av; |
403 |
– |
{ |
404 |
– |
putchar('#'); |
405 |
– |
while (ac--) { |
406 |
– |
putchar(' '); |
407 |
– |
fputs(*av++, stdout); |
408 |
– |
} |
409 |
– |
putchar('\n'); |
410 |
– |
} |
411 |
– |
|
412 |
– |
|
619 |
|
double |
620 |
< |
l_hermite() |
620 |
> |
l_bspline(char *nm) |
621 |
|
{ |
622 |
|
double t; |
623 |
< |
|
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 ); |
625 |
> |
return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) + |
626 |
> |
argument(2) * (2./3.+t*t*(-1.+1./2.*t)) + |
627 |
> |
argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) + |
628 |
> |
argument(4) * (1./6.*t*t*t) ); |
629 |
|
} |