ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensurf.c
Revision: 2.6
Committed: Sat Feb 22 02:07:23 2003 UTC (21 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +66 -23 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.6 static const char RCSid[] = "$Id$";
3 greg 1.1 #endif
4 greg 1.2 /*
5 greg 1.1 * 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 greg 2.6 *
15     * 4/16/02 Added conditional vertex output
16 greg 1.1 */
17    
18 greg 1.5 #include "standard.h"
19 greg 1.1
20 greg 2.6 char XNAME[] = "X`SYS"; /* x function name */
21     char YNAME[] = "Y`SYS"; /* y function name */
22     char ZNAME[] = "Z`SYS"; /* z function name */
23    
24     char VNAME[] = "valid"; /* valid vertex name */
25 greg 1.1
26 greg 1.4 #define ABS(x) ((x)>=0 ? (x) : -(x))
27    
28 greg 1.3 #define pvect(p) printf(vformat, (p)[0], (p)[1], (p)[2])
29 greg 1.1
30     char vformat[] = "%15.9g %15.9g %15.9g\n";
31 greg 1.3 char tsargs[] = "4 surf_dx surf_dy surf_dz surf.cal\n";
32     char texname[] = "Phong";
33 greg 1.1
34 greg 1.3 int smooth = 0; /* apply smoothing? */
35 greg 1.1
36 greg 1.3 char *modname, *surfname;
37 greg 1.1
38 greg 2.2 /* recorded data flags */
39     #define HASBORDER 01
40     #define TRIPLETS 02
41     /* a data structure */
42     struct {
43     int flags; /* data type */
44     short m, n; /* number of s and t values */
45     FLOAT *data; /* the data itself, s major sort */
46     } datarec; /* our recorded data */
47 greg 1.3
48 greg 2.2 double l_hermite(), l_bezier(), l_bspline(), l_dataval();
49     extern double funvalue(), argument();
50    
51 greg 1.3 typedef struct {
52 greg 2.6 int valid; /* point is valid */
53 greg 1.3 FVECT p; /* vertex position */
54     FVECT n; /* average normal */
55     } POINT;
56    
57    
58 greg 1.1 main(argc, argv)
59     int argc;
60     char *argv[];
61     {
62 greg 1.9 extern long eclock;
63 greg 1.3 POINT *row0, *row1, *row2, *rp;
64 greg 1.1 int i, j, m, n;
65     char stmp[256];
66    
67 greg 1.13 varset("PI", ':', PI);
68 greg 1.14 funset("hermite", 5, ':', l_hermite);
69     funset("bezier", 5, ':', l_bezier);
70     funset("bspline", 5, ':', l_bspline);
71 greg 1.1
72     if (argc < 8)
73     goto userror;
74    
75     for (i = 8; i < argc; i++)
76     if (!strcmp(argv[i], "-e"))
77 greg 1.10 scompile(argv[++i], NULL, 0);
78 greg 1.1 else if (!strcmp(argv[i], "-f"))
79     fcompile(argv[++i]);
80 greg 1.3 else if (!strcmp(argv[i], "-s"))
81     smooth++;
82 greg 1.1 else
83     goto userror;
84    
85 greg 1.3 modname = argv[1];
86     surfname = argv[2];
87 greg 1.1 m = atoi(argv[6]);
88     n = atoi(argv[7]);
89     if (m <= 0 || n <= 0)
90     goto userror;
91 greg 2.2 if (!strcmp(argv[5], "-") || access(argv[5], 4) == 0) { /* file? */
92     funset(ZNAME, 2, ':', l_dataval);
93     if (!strcmp(argv[5],argv[3]) && !strcmp(argv[5],argv[4])) {
94     loaddata(argv[5], m, n, 3);
95     funset(XNAME, 2, ':', l_dataval);
96     funset(YNAME, 2, ':', l_dataval);
97     } else {
98     loaddata(argv[5], m, n, 1);
99     sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]);
100     scompile(stmp, NULL, 0);
101     sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]);
102     scompile(stmp, NULL, 0);
103     }
104     } else {
105     sprintf(stmp, "%s(s,t)=%s;", XNAME, argv[3]);
106     scompile(stmp, NULL, 0);
107     sprintf(stmp, "%s(s,t)=%s;", YNAME, argv[4]);
108     scompile(stmp, NULL, 0);
109     sprintf(stmp, "%s(s,t)=%s;", ZNAME, argv[5]);
110     scompile(stmp, NULL, 0);
111     }
112 greg 1.4 row0 = (POINT *)malloc((n+3)*sizeof(POINT));
113     row1 = (POINT *)malloc((n+3)*sizeof(POINT));
114     row2 = (POINT *)malloc((n+3)*sizeof(POINT));
115 greg 1.3 if (row0 == NULL || row1 == NULL || row2 == NULL) {
116 greg 1.1 fprintf(stderr, "%s: out of memory\n", argv[0]);
117     quit(1);
118     }
119 greg 1.4 row0++; row1++; row2++;
120 greg 1.3 /* print header */
121 greg 1.1 printhead(argc, argv);
122 greg 1.9 eclock = 0;
123 greg 1.4 /* initialize */
124     comprow(-1.0/m, row0, n);
125 greg 1.3 comprow(0.0, row1, n);
126     comprow(1.0/m, row2, n);
127 greg 1.4 compnorms(row0, row1, row2, n);
128 greg 1.3 /* for each row */
129 greg 1.1 for (i = 0; i < m; i++) {
130     /* compute next row */
131 greg 1.3 rp = row0;
132 greg 1.1 row0 = row1;
133 greg 1.3 row1 = row2;
134     row2 = rp;
135 greg 1.4 comprow((double)(i+2)/m, row2, n);
136     compnorms(row0, row1, row2, n);
137 greg 1.1
138     for (j = 0; j < n; j++) {
139 greg 2.6 int orient = (j & 1);
140 greg 1.3 /* put polygons */
141 greg 2.6 if (!(row0[j].valid & row1[j+1].valid))
142     orient = 1;
143     else if (!(row1[j].valid & row0[j+1].valid))
144     orient = 0;
145     if (orient)
146 greg 1.3 putsquare(&row0[j], &row1[j],
147     &row0[j+1], &row1[j+1]);
148     else
149     putsquare(&row1[j], &row1[j+1],
150     &row0[j], &row0[j+1]);
151 greg 1.1 }
152     }
153    
154     quit(0);
155    
156     userror:
157     fprintf(stderr, "Usage: %s material name ", argv[0]);
158 greg 1.3 fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-e expr][-f file]\n");
159 greg 1.1 quit(1);
160 greg 2.2 }
161    
162    
163     loaddata(file, m, n, pointsize) /* load point data from file */
164     char *file;
165     int m, n;
166     int pointsize;
167     {
168     FILE *fp;
169     char word[64];
170     register int size;
171     register FLOAT *dp;
172    
173     datarec.flags = HASBORDER; /* assume border values */
174 greg 2.3 datarec.m = m+1;
175     datarec.n = n+1;
176     size = datarec.m*datarec.n*pointsize;
177 greg 2.2 if (pointsize == 3)
178     datarec.flags |= TRIPLETS;
179     dp = (FLOAT *)malloc(size*sizeof(FLOAT));
180     if ((datarec.data = dp) == NULL) {
181     fputs("Out of memory\n", stderr);
182     exit(1);
183     }
184     if (!strcmp(file, "-")) {
185     file = "<stdin>";
186     fp = stdin;
187     } else if ((fp = fopen(file, "r")) == NULL) {
188     fputs(file, stderr);
189     fputs(": cannot open\n", stderr);
190     exit(1);
191     }
192     while (size > 0 && fgetword(word, sizeof(word), fp) != NULL) {
193     if (!isflt(word)) {
194     fprintf(stderr, "%s: garbled data value: %s\n",
195     file, word);
196     exit(1);
197     }
198     *dp++ = atof(word);
199     size--;
200     }
201     if (size == (m+n+1)*pointsize) { /* no border after all */
202     dp = (FLOAT *)realloc((char *)datarec.data,
203     m*n*pointsize*sizeof(FLOAT));
204     if (dp != NULL)
205     datarec.data = dp;
206     datarec.flags &= ~HASBORDER;
207 greg 2.3 datarec.m = m;
208     datarec.n = n;
209 greg 2.2 size = 0;
210     }
211 greg 2.3 if (datarec.m < 2 || datarec.n < 2 || size != 0 ||
212     fgetword(word, sizeof(word), fp) != NULL) {
213 greg 2.2 fputs(file, stderr);
214     fputs(": bad number of data points\n", stderr);
215     exit(1);
216     }
217     fclose(fp);
218     }
219    
220    
221     double
222     l_dataval(nam) /* return recorded data value */
223     char *nam;
224     {
225     double u, v;
226     register int i, j;
227     register FLOAT *dp;
228     double d00, d01, d10, d11;
229     /* compute coordinates */
230     u = argument(1); v = argument(2);
231     if (datarec.flags & HASBORDER) {
232 greg 2.3 i = u *= datarec.m-1;
233     j = v *= datarec.n-1;
234 greg 2.2 } else {
235 greg 2.3 i = u = u*datarec.m - .5;
236     j = v = v*datarec.n - .5;
237 greg 2.2 }
238     if (i < 0) i = 0;
239     else if (i > datarec.m-2) i = datarec.m-2;
240     if (j < 0) j = 0;
241     else if (j > datarec.n-2) j = datarec.n-2;
242     /* compute value */
243     if (datarec.flags & TRIPLETS) {
244 greg 2.3 dp = datarec.data + 3*(j*datarec.m + i);
245     if (nam == ZNAME)
246     dp += 2;
247     else if (nam == YNAME)
248 greg 2.2 dp++;
249     d00 = dp[0]; d01 = dp[3];
250 greg 2.3 dp += 3*datarec.m;
251 greg 2.2 d10 = dp[0]; d11 = dp[3];
252     } else {
253 greg 2.3 dp = datarec.data + j*datarec.m + i;
254 greg 2.2 d00 = dp[0]; d01 = dp[1];
255 greg 2.3 dp += datarec.m;
256 greg 2.2 d10 = dp[0]; d11 = dp[1];
257     }
258     /* bilinear interpolation */
259     return((j+1-v)*((i+1-u)*d00+(u-i)*d01)+(v-j)*((i+1-u)*d10+(u-i)*d11));
260 greg 1.1 }
261    
262    
263 greg 1.3 putsquare(p0, p1, p2, p3) /* put out a square */
264     POINT *p0, *p1, *p2, *p3;
265     {
266     static int nout = 0;
267     FVECT norm[4];
268     int axis;
269     FVECT v1, v2, vc1, vc2;
270     int ok1, ok2;
271     /* compute exact normals */
272 greg 2.6 ok1 = (p0->valid & p1->valid & p2->valid);
273     if (ok1) {
274     fvsum(v1, p1->p, p0->p, -1.0);
275     fvsum(v2, p2->p, p0->p, -1.0);
276     fcross(vc1, v1, v2);
277     ok1 = (normalize(vc1) != 0.0);
278     }
279     ok2 = (p1->valid & p2->valid & p3->valid);
280     if (ok2) {
281     fvsum(v1, p2->p, p3->p, -1.0);
282     fvsum(v2, p1->p, p3->p, -1.0);
283     fcross(vc2, v1, v2);
284     ok2 = (normalize(vc2) != 0.0);
285     }
286 greg 1.3 if (!(ok1 | ok2))
287     return;
288     /* compute normal interpolation */
289     axis = norminterp(norm, p0, p1, p2, p3);
290    
291     /* put out quadrilateral? */
292     if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
293     printf("\n%s ", modname);
294     if (axis != -1) {
295     printf("texfunc %s\n", texname);
296     printf(tsargs);
297     printf("0\n13\t%d\n", axis);
298     pvect(norm[0]);
299     pvect(norm[1]);
300     pvect(norm[2]);
301     fvsum(v1, norm[3], vc1, -0.5);
302     fvsum(v1, v1, vc2, -0.5);
303     pvect(v1);
304     printf("\n%s ", texname);
305     }
306     printf("polygon %s.%d\n", surfname, ++nout);
307     printf("0\n0\n12\n");
308     pvect(p0->p);
309     pvect(p1->p);
310     pvect(p3->p);
311     pvect(p2->p);
312     return;
313     }
314     /* put out triangles? */
315     if (ok1) {
316     printf("\n%s ", modname);
317     if (axis != -1) {
318     printf("texfunc %s\n", texname);
319     printf(tsargs);
320     printf("0\n13\t%d\n", axis);
321     pvect(norm[0]);
322     pvect(norm[1]);
323     pvect(norm[2]);
324     fvsum(v1, norm[3], vc1, -1.0);
325     pvect(v1);
326     printf("\n%s ", texname);
327     }
328     printf("polygon %s.%d\n", surfname, ++nout);
329     printf("0\n0\n9\n");
330     pvect(p0->p);
331     pvect(p1->p);
332     pvect(p2->p);
333     }
334     if (ok2) {
335     printf("\n%s ", modname);
336     if (axis != -1) {
337     printf("texfunc %s\n", texname);
338     printf(tsargs);
339     printf("0\n13\t%d\n", axis);
340     pvect(norm[0]);
341     pvect(norm[1]);
342     pvect(norm[2]);
343     fvsum(v2, norm[3], vc2, -1.0);
344     pvect(v2);
345     printf("\n%s ", texname);
346     }
347     printf("polygon %s.%d\n", surfname, ++nout);
348     printf("0\n0\n9\n");
349     pvect(p2->p);
350     pvect(p1->p);
351     pvect(p3->p);
352     }
353     }
354    
355    
356 greg 1.1 comprow(s, row, siz) /* compute row of values */
357     double s;
358 greg 1.3 register POINT *row;
359 greg 1.1 int siz;
360     {
361 greg 1.4 double st[2];
362 greg 1.8 int end;
363 greg 2.6 int checkvalid;
364 greg 1.4 register int i;
365 greg 1.8
366     if (smooth) {
367     i = -1; /* compute one past each end */
368     end = siz+1;
369     } else {
370     if (s < -FTINY || s > 1.0+FTINY)
371     return;
372     i = 0;
373     end = siz;
374     }
375 greg 1.1 st[0] = s;
376 greg 2.6 checkvalid = (fundefined(VNAME) == 2);
377 greg 1.8 while (i <= end) {
378 greg 1.4 st[1] = (double)i/siz;
379 greg 2.6 if (checkvalid && funvalue(VNAME, 2, st) <= 0.0) {
380     row[i].valid = 0;
381     row[i].p[0] = row[i].p[1] = row[i].p[2] = 0.0;
382     } else {
383     row[i].valid = 1;
384     row[i].p[0] = funvalue(XNAME, 2, st);
385     row[i].p[1] = funvalue(YNAME, 2, st);
386     row[i].p[2] = funvalue(ZNAME, 2, st);
387     }
388 greg 1.8 i++;
389 greg 1.1 }
390 greg 1.3 }
391    
392    
393     compnorms(r0, r1, r2, siz) /* compute row of averaged normals */
394     register POINT *r0, *r1, *r2;
395     int siz;
396     {
397 greg 1.11 FVECT v1, v2;
398 greg 1.3
399     if (!smooth) /* not needed if no smoothing */
400     return;
401 greg 2.6 /* compute row 1 normals */
402 greg 1.4 while (siz-- >= 0) {
403 greg 2.6 if (!r1[0].valid)
404     continue;
405     if (!r0[0].valid) {
406     if (!r2[0].valid) {
407     r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0;
408     continue;
409     }
410     fvsum(v1, r2[0].p, r1[0].p, -1.0);
411     } else if (!r2[0].valid)
412     fvsum(v1, r1[0].p, r0[0].p, -1.0);
413     else
414     fvsum(v1, r2[0].p, r0[0].p, -1.0);
415     if (!r1[-1].valid) {
416     if (!r1[1].valid) {
417     r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0;
418     continue;
419     }
420     fvsum(v2, r1[1].p, r1[0].p, -1.0);
421     } else if (!r1[1].valid)
422     fvsum(v2, r1[0].p, r1[-1].p, -1.0);
423     else
424     fvsum(v2, r1[1].p, r1[-1].p, -1.0);
425 greg 1.3 fcross(r1[0].n, v1, v2);
426     normalize(r1[0].n);
427     r0++; r1++; r2++;
428     }
429     }
430    
431    
432     int
433     norminterp(resmat, p0, p1, p2, p3) /* compute normal interpolation */
434     register FVECT resmat[4];
435     POINT *p0, *p1, *p2, *p3;
436     {
437     #define u ((ax+1)%3)
438     #define v ((ax+2)%3)
439    
440     register int ax;
441 greg 1.12 MAT4 eqnmat;
442 greg 1.3 FVECT v1;
443     register int i, j;
444    
445     if (!smooth) /* no interpolation if no smoothing */
446     return(-1);
447     /* find dominant axis */
448     VCOPY(v1, p0->n);
449     fvsum(v1, v1, p1->n, 1.0);
450     fvsum(v1, v1, p2->n, 1.0);
451     fvsum(v1, v1, p3->n, 1.0);
452 greg 1.4 ax = ABS(v1[0]) > ABS(v1[1]) ? 0 : 1;
453     ax = ABS(v1[ax]) > ABS(v1[2]) ? ax : 2;
454 greg 1.3 /* assign equation matrix */
455     eqnmat[0][0] = p0->p[u]*p0->p[v];
456     eqnmat[0][1] = p0->p[u];
457     eqnmat[0][2] = p0->p[v];
458     eqnmat[0][3] = 1.0;
459     eqnmat[1][0] = p1->p[u]*p1->p[v];
460     eqnmat[1][1] = p1->p[u];
461     eqnmat[1][2] = p1->p[v];
462     eqnmat[1][3] = 1.0;
463     eqnmat[2][0] = p2->p[u]*p2->p[v];
464     eqnmat[2][1] = p2->p[u];
465     eqnmat[2][2] = p2->p[v];
466     eqnmat[2][3] = 1.0;
467     eqnmat[3][0] = p3->p[u]*p3->p[v];
468     eqnmat[3][1] = p3->p[u];
469     eqnmat[3][2] = p3->p[v];
470     eqnmat[3][3] = 1.0;
471     /* invert matrix (solve system) */
472 greg 2.5 if (!invmat4(eqnmat, eqnmat))
473 greg 1.3 return(-1); /* no solution */
474     /* compute result matrix */
475     for (j = 0; j < 4; j++)
476     for (i = 0; i < 3; i++)
477 greg 1.4 resmat[j][i] = eqnmat[j][0]*p0->n[i] +
478     eqnmat[j][1]*p1->n[i] +
479     eqnmat[j][2]*p2->n[i] +
480     eqnmat[j][3]*p3->n[i];
481 greg 1.3 return(ax);
482    
483     #undef u
484     #undef v
485 greg 1.1 }
486    
487    
488 greg 2.6 void
489 greg 1.1 eputs(msg)
490     char *msg;
491     {
492     fputs(msg, stderr);
493     }
494    
495    
496 greg 2.6 void
497 greg 1.1 wputs(msg)
498     char *msg;
499     {
500     eputs(msg);
501     }
502    
503    
504 greg 2.6 void
505 greg 1.1 quit(code)
506 greg 2.4 int code;
507 greg 1.1 {
508     exit(code);
509     }
510    
511    
512     printhead(ac, av) /* print command header */
513     register int ac;
514     register char **av;
515     {
516     putchar('#');
517     while (ac--) {
518     putchar(' ');
519     fputs(*av++, stdout);
520     }
521     putchar('\n');
522     }
523    
524    
525     double
526     l_hermite()
527     {
528     double t;
529    
530     t = argument(5);
531     return( argument(1)*((2.0*t-3.0)*t*t+1.0) +
532     argument(2)*(-2.0*t+3.0)*t*t +
533     argument(3)*((t-2.0)*t+1.0)*t +
534     argument(4)*(t-1.0)*t*t );
535 greg 1.6 }
536    
537    
538     double
539     l_bezier()
540     {
541     double t;
542    
543     t = argument(5);
544     return( argument(1) * (1.+t*(-3.+t*(3.-t))) +
545     argument(2) * 3.*t*(1.+t*(-2.+t)) +
546     argument(3) * 3.*t*t*(1.-t) +
547     argument(4) * t*t*t );
548 greg 1.7 }
549    
550    
551     double
552     l_bspline()
553     {
554     double t;
555    
556     t = argument(5);
557     return( argument(1) * (1./6.+t*(-1./2.+t*(1./2.-1./6.*t))) +
558     argument(2) * (2./3.+t*t*(-1.+1./2.*t)) +
559     argument(3) * (1./6.+t*(1./2.+t*(1./2.-1./2.*t))) +
560     argument(4) * (1./6.*t*t*t) );
561 greg 1.1 }