1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
– |
|
5 |
– |
/* Copyright (c) 1989 Regents of the University of California */ |
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 "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 |
+ |
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[] = "%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 |
+ |
int rev = 0; /* invert normal directions? */ |
44 |
|
|
45 |
|
char *modname, *surfname; |
46 |
|
|
47 |
< |
double funvalue(), l_hermite(), l_bezier(), l_bspline(), argument(); |
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 |
< |
main(argc, argv) |
73 |
< |
int argc; |
74 |
< |
char *argv[]; |
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 |
|
{ |
49 |
– |
extern long eclock; |
85 |
|
POINT *row0, *row1, *row2, *rp; |
86 |
|
int i, j, m, n; |
87 |
|
char stmp[256]; |
88 |
|
|
89 |
< |
varset("PI", PI); |
90 |
< |
funset("hermite", 5, l_hermite); |
91 |
< |
funset("bezier", 5, l_bezier); |
92 |
< |
funset("bspline", 5, l_bspline); |
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; |
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 |
< |
fcompile(argv[++i]); |
104 |
< |
else if (!strcmp(argv[i], "-s")) |
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 |
< |
sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]); |
122 |
< |
scompile(stmp, NULL, 0); |
76 |
< |
sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]); |
77 |
< |
scompile(stmp, NULL, 0); |
78 |
< |
sprintf(stmp, "%s(s,t)=%s;", ZNAME, argv[5]); |
79 |
< |
scompile(stmp, NULL, 0); |
80 |
< |
m = atoi(argv[6]); |
81 |
< |
n = atoi(argv[7]); |
121 |
> |
m = eval(argv[6]) + .5; |
122 |
> |
n = eval(argv[7]) + .5; |
123 |
|
if (m <= 0 || n <= 0) |
124 |
|
goto userror; |
125 |
< |
|
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)); |
152 |
|
} |
153 |
|
row0++; row1++; row2++; |
154 |
|
/* print header */ |
155 |
< |
printhead(argc, argv); |
156 |
< |
eclock = 0; |
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 */ |
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 ((i+j) & 1) |
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 |
194 |
|
} |
195 |
|
} |
196 |
|
|
197 |
< |
quit(0); |
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][-e expr][-f file]\n"); |
202 |
< |
quit(1); |
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 |
< |
putsquare(p0, p1, p2, p3) /* put out a square */ |
207 |
< |
POINT *p0, *p1, *p2, *p3; |
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 |
< |
fvsum(v1, p1->p, p0->p, -1.0); |
370 |
< |
fvsum(v2, p2->p, p0->p, -1.0); |
371 |
< |
fcross(vc1, v1, v2); |
372 |
< |
ok1 = normalize(vc1) != 0.0; |
373 |
< |
fvsum(v1, p2->p, p3->p, -1.0); |
374 |
< |
fvsum(v2, p1->p, p3->p, -1.0); |
375 |
< |
fcross(vc2, v1, v2); |
376 |
< |
ok2 = normalize(vc2) != 0.0; |
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 |
|
|
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", texname); |
158 |
< |
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]); |
431 |
|
if (ok1) { |
432 |
|
printf("\n%s ", modname); |
433 |
|
if (axis != -1) { |
434 |
< |
printf("texfunc %s\n", texname); |
181 |
< |
printf(tsargs); |
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]); |
449 |
|
if (ok2) { |
450 |
|
printf("\n%s ", modname); |
451 |
|
if (axis != -1) { |
452 |
< |
printf("texfunc %s\n", texname); |
200 |
< |
printf(tsargs); |
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]); |
467 |
|
} |
468 |
|
|
469 |
|
|
470 |
< |
comprow(s, row, siz) /* compute row of values */ |
471 |
< |
double s; |
472 |
< |
register POINT *row; |
473 |
< |
int siz; |
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 |
< |
register int i; |
479 |
> |
int checkvalid; |
480 |
> |
int i; |
481 |
|
|
482 |
|
if (smooth) { |
483 |
|
i = -1; /* compute one past each end */ |
489 |
|
end = siz; |
490 |
|
} |
491 |
|
st[0] = s; |
492 |
+ |
checkvalid = (fundefined(VNAME) == 2); |
493 |
|
while (i <= end) { |
494 |
|
st[1] = (double)i/siz; |
495 |
< |
row[i].p[0] = funvalue(XNAME, 2, st); |
496 |
< |
row[i].p[1] = funvalue(YNAME, 2, st); |
497 |
< |
row[i].p[2] = funvalue(ZNAME, 2, st); |
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 |
< |
compnorms(r0, r1, r2, siz) /* compute row of averaged normals */ |
513 |
< |
register POINT *r0, *r1, *r2; |
514 |
< |
int siz; |
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; |
252 |
– |
register int i; |
521 |
|
|
522 |
|
if (!smooth) /* not needed if no smoothing */ |
523 |
|
return; |
524 |
< |
/* compute middle points */ |
524 |
> |
/* compute row 1 normals */ |
525 |
|
while (siz-- >= 0) { |
526 |
< |
fvsum(v1, r2[0].p, r0[0].p, -1.0); |
527 |
< |
fvsum(v2, r1[1].p, r1[-1].p, -1.0); |
528 |
< |
fcross(r1[0].n, v1, v2); |
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(resmat, p0, p1, p2, p3) /* compute normal interpolation */ |
561 |
< |
register FVECT resmat[4]; |
562 |
< |
POINT *p0, *p1, *p2, *p3; |
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 |
< |
register int ax; |
571 |
> |
int ax; |
572 |
|
MAT4 eqnmat; |
573 |
|
FVECT v1; |
574 |
< |
register int i, j; |
574 |
> |
int i, j; |
575 |
|
|
576 |
|
if (!smooth) /* no interpolation if no smoothing */ |
577 |
|
return(-1); |
600 |
|
eqnmat[3][2] = p3->p[v]; |
601 |
|
eqnmat[3][3] = 1.0; |
602 |
|
/* invert matrix (solve system) */ |
603 |
< |
if (!invmat(eqnmat, eqnmat)) |
603 |
> |
if (!invmat4(eqnmat, eqnmat)) |
604 |
|
return(-1); /* no solution */ |
605 |
|
/* compute result matrix */ |
606 |
|
for (j = 0; j < 4; j++) |
616 |
|
} |
617 |
|
|
618 |
|
|
323 |
– |
/* |
324 |
– |
* invmat - computes the inverse of mat into inverse. Returns 1 |
325 |
– |
* if there exists an inverse, 0 otherwise. It uses Gaussian Elimination |
326 |
– |
* method. |
327 |
– |
*/ |
328 |
– |
|
329 |
– |
invmat(inverse,mat) |
330 |
– |
MAT4 inverse, mat; |
331 |
– |
{ |
332 |
– |
#define SWAP(a,b,t) (t=a,a=b,b=t) |
333 |
– |
|
334 |
– |
MAT4 m4tmp; |
335 |
– |
register int i,j,k; |
336 |
– |
register double temp; |
337 |
– |
|
338 |
– |
copymat4(m4tmp, mat); |
339 |
– |
/* set inverse to identity */ |
340 |
– |
for (i = 0; i < 4; i++) |
341 |
– |
for (j = 0; j < 4; j++) |
342 |
– |
inverse[i][j] = i==j ? 1.0 : 0.0; |
343 |
– |
|
344 |
– |
for(i = 0; i < 4; i++) { |
345 |
– |
/* Look for row with largest pivot and swap rows */ |
346 |
– |
temp = FTINY; j = -1; |
347 |
– |
for(k = i; k < 4; k++) |
348 |
– |
if(ABS(m4tmp[k][i]) > temp) { |
349 |
– |
temp = ABS(m4tmp[k][i]); |
350 |
– |
j = k; |
351 |
– |
} |
352 |
– |
if(j == -1) /* No replacing row -> no inverse */ |
353 |
– |
return(0); |
354 |
– |
if (j != i) |
355 |
– |
for(k = 0; k < 4; k++) { |
356 |
– |
SWAP(m4tmp[i][k],m4tmp[j][k],temp); |
357 |
– |
SWAP(inverse[i][k],inverse[j][k],temp); |
358 |
– |
} |
359 |
– |
|
360 |
– |
temp = m4tmp[i][i]; |
361 |
– |
for(k = 0; k < 4; k++) { |
362 |
– |
m4tmp[i][k] /= temp; |
363 |
– |
inverse[i][k] /= temp; |
364 |
– |
} |
365 |
– |
for(j = 0; j < 4; j++) { |
366 |
– |
if(j != i) { |
367 |
– |
temp = m4tmp[j][i]; |
368 |
– |
for(k = 0; k < 4; k++) { |
369 |
– |
m4tmp[j][k] -= m4tmp[i][k]*temp; |
370 |
– |
inverse[j][k] -= inverse[i][k]*temp; |
371 |
– |
} |
372 |
– |
} |
373 |
– |
} |
374 |
– |
} |
375 |
– |
return(1); |
376 |
– |
|
377 |
– |
#undef SWAP |
378 |
– |
} |
379 |
– |
|
380 |
– |
|
381 |
– |
eputs(msg) |
382 |
– |
char *msg; |
383 |
– |
{ |
384 |
– |
fputs(msg, stderr); |
385 |
– |
} |
386 |
– |
|
387 |
– |
|
388 |
– |
wputs(msg) |
389 |
– |
char *msg; |
390 |
– |
{ |
391 |
– |
eputs(msg); |
392 |
– |
} |
393 |
– |
|
394 |
– |
|
395 |
– |
quit(code) |
396 |
– |
{ |
397 |
– |
exit(code); |
398 |
– |
} |
399 |
– |
|
400 |
– |
|
401 |
– |
printhead(ac, av) /* print command header */ |
402 |
– |
register int ac; |
403 |
– |
register char **av; |
404 |
– |
{ |
405 |
– |
putchar('#'); |
406 |
– |
while (ac--) { |
407 |
– |
putchar(' '); |
408 |
– |
fputs(*av++, stdout); |
409 |
– |
} |
410 |
– |
putchar('\n'); |
411 |
– |
} |
412 |
– |
|
413 |
– |
|
619 |
|
double |
620 |
< |
l_hermite() |
620 |
> |
l_hermite(char *nm) |
621 |
|
{ |
622 |
|
double t; |
623 |
|
|
630 |
|
|
631 |
|
|
632 |
|
double |
633 |
< |
l_bezier() |
633 |
> |
l_bezier(char *nm) |
634 |
|
{ |
635 |
|
double t; |
636 |
|
|
643 |
|
|
644 |
|
|
645 |
|
double |
646 |
< |
l_bspline() |
646 |
> |
l_bspline(char *nm) |
647 |
|
{ |
648 |
|
double t; |
649 |
|
|