ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/gensurf.c
(Generate patch)

Comparing ray/src/gen/gensurf.c (file contents):
Revision 2.6 by greg, Sat Feb 22 02:07:23 2003 UTC vs.
Revision 2.28 by greg, Sat Nov 14 00:29:51 2020 UTC

# Line 1 | Line 1
1   #ifndef lint
2 < static const char       RCSid[] = "$Id$";
2 > static const char RCSid[] = "$Id$";
3   #endif
4   /*
5   *  gensurf.c - program to generate functional surfaces
# Line 17 | Line 17 | static const char      RCSid[] = "$Id$";
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 */
# Line 25 | Line 30 | 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  
# Line 42 | Line 50 | char  *modname, *surfname;
50   struct {
51          int     flags;                  /* data type */
52          short   m, n;                   /* number of s and t values */
53 <        FLOAT   *data;                  /* the data itself, s major sort */
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();
49 extern double  funvalue(), argument();
58  
59   typedef struct {
60 <        int  valid;     /* point is valid */
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 < main(argc, argv)
71 < int  argc;
72 < char  *argv[];
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(int argc, char *argv[])
82   {
62        extern long     eclock;
83          POINT  *row0, *row1, *row2, *rp;
84          int  i, j, m, n;
85          char  stmp[256];
86  
87 +        esupport |= E_VARIABLE|E_FUNCTION|E_RCONST;
88 +        esupport &= ~(E_OUTCHAN|E_INCHAN);
89          varset("PI", ':', PI);
90          funset("hermite", 5, ':', l_hermite);
91          funset("bezier", 5, ':', l_bezier);
# Line 75 | Line 97 | char  *argv[];
97          for (i = 8; i < argc; i++)
98                  if (!strcmp(argv[i], "-e"))
99                          scompile(argv[++i], NULL, 0);
100 <                else if (!strcmp(argv[i], "-f"))
101 <                        fcompile(argv[++i]);
102 <                else if (!strcmp(argv[i], "-s"))
100 >                else if (!strcmp(argv[i], "-f")) {
101 >                        char  *fpath = getpath(argv[++i], getrlibpath(), 0);
102 >                        if (fpath == NULL) {
103 >                                fprintf(stderr, "%s: cannot find file '%s'\n",
104 >                                                argv[0], argv[i]);
105 >                                quit(1);
106 >                        }
107 >                        fcompile(fpath);
108 >                } else if (!strcmp(argv[i], "-s"))
109                          smooth++;
110 +                else if (!strcmp(argv[i], "-o"))
111 +                        objout++;
112                  else
113                          goto userror;
114  
115          modname = argv[1];
116          surfname = argv[2];
117 <        m = atoi(argv[6]);
118 <        n = atoi(argv[7]);
117 >        m = eval(argv[6]) + .5;
118 >        n = eval(argv[7]) + .5;
119          if (m <= 0 || n <= 0)
120                  goto userror;
121          if (!strcmp(argv[5], "-") || access(argv[5], 4) == 0) { /* file? */
# Line 118 | Line 148 | char  *argv[];
148          }
149          row0++; row1++; row2++;
150                                                  /* print header */
151 <        printhead(argc, argv);
151 >        fputs("# ", stdout);
152 >        printargs(argc, argv, stdout);
153          eclock = 0;
154                                                  /* initialize */
155          comprow(-1.0/m, row0, n);
156          comprow(0.0, row1, n);
157          comprow(1.0/m, row2, n);
158          compnorms(row0, row1, row2, n);
159 +        if (objout) {
160 +                printf("\nusemtl %s\n\n", modname);
161 +                printf("o %s\n\n", surfname);
162 +                putobjrow(row1, n);
163 +        }
164                                                  /* for each row */
165          for (i = 0; i < m; i++) {
166                                                  /* compute next row */
# Line 134 | Line 170 | char  *argv[];
170                  row2 = rp;
171                  comprow((double)(i+2)/m, row2, n);
172                  compnorms(row0, row1, row2, n);
173 +                if (objout)
174 +                        putobjrow(row1, n);
175  
176                  for (j = 0; j < n; j++) {
177                          int  orient = (j & 1);
178                                                          /* put polygons */
179 <                        if (!(row0[j].valid & row1[j+1].valid))
179 >                        if (!(row0[j].valid && row1[j+1].valid))
180                                  orient = 1;
181 <                        else if (!(row1[j].valid & row0[j+1].valid))
181 >                        else if (!(row1[j].valid && row0[j+1].valid))
182                                  orient = 0;
183                          if (orient)
184                                  putsquare(&row0[j], &row1[j],
# Line 151 | Line 189 | char  *argv[];
189                  }
190          }
191  
192 <        quit(0);
192 >        return 0;
193  
194   userror:
195          fprintf(stderr, "Usage: %s material name ", argv[0]);
196 <        fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-e expr][-f file]\n");
197 <        quit(1);
196 >        fprintf(stderr, "x(s,t) y(s,t) z(s,t) m n [-s][-o][-e expr][-f file]\n");
197 >        return 1;
198   }
199  
200  
201 < loaddata(file, m, n, pointsize)         /* load point data from file */
202 < char  *file;
203 < int  m, n;
204 < int  pointsize;
201 > void
202 > loaddata(               /* load point data from file */
203 >        char  *file,
204 >        int  m,
205 >        int  n,
206 >        int  pointsize
207 > )
208   {
209          FILE  *fp;
210          char  word[64];
211 <        register int  size;
212 <        register FLOAT  *dp;
211 >        int  size;
212 >        RREAL  *dp;
213  
214          datarec.flags = HASBORDER;              /* assume border values */
215          datarec.m = m+1;
# Line 176 | Line 217 | int  pointsize;
217          size = datarec.m*datarec.n*pointsize;
218          if (pointsize == 3)
219                  datarec.flags |= TRIPLETS;
220 <        dp = (FLOAT *)malloc(size*sizeof(FLOAT));
220 >        dp = (RREAL *)malloc(size*sizeof(RREAL));
221          if ((datarec.data = dp) == NULL) {
222                  fputs("Out of memory\n", stderr);
223                  exit(1);
# Line 199 | Line 240 | int  pointsize;
240                  size--;
241          }
242          if (size == (m+n+1)*pointsize) {        /* no border after all */
243 <                dp = (FLOAT *)realloc((char *)datarec.data,
244 <                                m*n*pointsize*sizeof(FLOAT));
243 >                dp = (RREAL *)realloc(datarec.data,
244 >                                m*n*pointsize*sizeof(RREAL));
245                  if (dp != NULL)
246                          datarec.data = dp;
247                  datarec.flags &= ~HASBORDER;
# Line 219 | Line 260 | int  pointsize;
260  
261  
262   double
263 < l_dataval(nam)                          /* return recorded data value */
264 < char  *nam;
263 > l_dataval(                              /* return recorded data value */
264 >        char  *nam
265 > )
266   {
267          double  u, v;
268 <        register int  i, j;
269 <        register FLOAT  *dp;
268 >        int  i, j;
269 >        RREAL  *dp;
270          double  d00, d01, d10, d11;
271                                                  /* compute coordinates */
272          u = argument(1); v = argument(2);
# Line 260 | Line 302 | char  *nam;
302   }
303  
304  
305 < putsquare(p0, p1, p2, p3)               /* put out a square */
306 < POINT  *p0, *p1, *p2, *p3;
305 > void
306 > putobjrow(                      /* output vertex row to .OBJ */
307 >        POINT  *rp,
308 >        int  n
309 > )
310   {
311 +        for ( ; n-- >= 0; rp++) {
312 +                if (!rp->valid)
313 +                        continue;
314 +                fputs("v ", stdout);
315 +                pvect(rp->p);
316 +                if (smooth && !ZEROVECT(rp->n)) {
317 +                        printf("\tvn %.9g %.9g %.9g\n",
318 +                                        rp->n[0], rp->n[1], rp->n[2]);
319 +                        rp->nvalid = ++nnorms;
320 +                } else
321 +                        rp->nvalid = 0;
322 +                printf("\tvt %.9g %.9g\n", rp->uv[0], rp->uv[1]);
323 +                rp->valid = ++nverts;
324 +        }
325 + }
326 +
327 +
328 + void
329 + putobjvert(             /* put out OBJ vertex index triplet */
330 +        POINT *p
331 + )
332 + {
333 +        int     pti = p->valid ? p->valid-nverts-1 : 0;
334 +        int     ni = p->nvalid ? p->nvalid-nnorms-1 : 0;
335 +        
336 +        printf(" %d/%d/%d", pti, pti, ni);
337 + }
338 +
339 +
340 + void
341 + putsquare(              /* put out a square */
342 +        POINT *p0,
343 +        POINT *p1,
344 +        POINT *p2,
345 +        POINT *p3
346 + )
347 + {
348          static int  nout = 0;
349          FVECT  norm[4];
350          int  axis;
351          FVECT  v1, v2, vc1, vc2;
352          int  ok1, ok2;
353                                          /* compute exact normals */
354 <        ok1 = (p0->valid & p1->valid & p2->valid);
354 >        ok1 = (p0->valid && p1->valid && p2->valid);
355          if (ok1) {
356 <                fvsum(v1, p1->p, p0->p, -1.0);
357 <                fvsum(v2, p2->p, p0->p, -1.0);
356 >                VSUB(v1, p1->p, p0->p);
357 >                VSUB(v2, p2->p, p0->p);
358                  fcross(vc1, v1, v2);
359                  ok1 = (normalize(vc1) != 0.0);
360          }
361 <        ok2 = (p1->valid & p2->valid & p3->valid);
361 >        ok2 = (p1->valid && p2->valid && p3->valid);
362          if (ok2) {
363 <                fvsum(v1, p2->p, p3->p, -1.0);
364 <                fvsum(v2, p1->p, p3->p, -1.0);
363 >                VSUB(v1, p2->p, p3->p);
364 >                VSUB(v2, p1->p, p3->p);
365                  fcross(vc2, v1, v2);
366                  ok2 = (normalize(vc2) != 0.0);
367          }
368          if (!(ok1 | ok2))
369                  return;
370 +        if (objout) {                   /* output .OBJ faces */
371 +                if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
372 +                        putc('f', stdout);
373 +                        putobjvert(p0); putobjvert(p1);
374 +                        putobjvert(p3); putobjvert(p2);
375 +                        putc('\n', stdout);
376 +                        return;
377 +                }
378 +                if (ok1) {
379 +                        putc('f', stdout);
380 +                        putobjvert(p0); putobjvert(p1); putobjvert(p2);
381 +                        putc('\n', stdout);
382 +                }
383 +                if (ok2) {
384 +                        putc('f', stdout);
385 +                        putobjvert(p2); putobjvert(p1); putobjvert(p3);
386 +                        putc('\n', stdout);
387 +                }
388 +                return;
389 +        }
390                                          /* compute normal interpolation */
391          axis = norminterp(norm, p0, p1, p2, p3);
392  
# Line 292 | Line 394 | POINT  *p0, *p1, *p2, *p3;
394          if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
395                  printf("\n%s ", modname);
396                  if (axis != -1) {
397 <                        printf("texfunc %s\n", texname);
296 <                        printf(tsargs);
397 >                        printf("texfunc %s\n%s\n", texname, tsargs);
398                          printf("0\n13\t%d\n", axis);
399                          pvect(norm[0]);
400                          pvect(norm[1]);
# Line 315 | Line 416 | POINT  *p0, *p1, *p2, *p3;
416          if (ok1) {
417                  printf("\n%s ", modname);
418                  if (axis != -1) {
419 <                        printf("texfunc %s\n", texname);
319 <                        printf(tsargs);
419 >                        printf("texfunc %s\n%s\n", texname, tsargs);
420                          printf("0\n13\t%d\n", axis);
421                          pvect(norm[0]);
422                          pvect(norm[1]);
# Line 334 | Line 434 | POINT  *p0, *p1, *p2, *p3;
434          if (ok2) {
435                  printf("\n%s ", modname);
436                  if (axis != -1) {
437 <                        printf("texfunc %s\n", texname);
338 <                        printf(tsargs);
437 >                        printf("texfunc %s\n%s\n", texname, tsargs);
438                          printf("0\n13\t%d\n", axis);
439                          pvect(norm[0]);
440                          pvect(norm[1]);
# Line 353 | Line 452 | POINT  *p0, *p1, *p2, *p3;
452   }
453  
454  
455 < comprow(s, row, siz)                    /* compute row of values */
456 < double  s;
457 < register POINT  *row;
458 < int  siz;
455 > void
456 > comprow(                        /* compute row of values */
457 >        double  s,
458 >        POINT  *row,
459 >        int  siz
460 > )
461   {
462          double  st[2];
463          int  end;
464          int  checkvalid;
465 <        register int  i;
465 >        int  i;
466          
467          if (smooth) {
468                  i = -1;                 /* compute one past each end */
# Line 379 | Line 480 | int  siz;
480                  if (checkvalid && funvalue(VNAME, 2, st) <= 0.0) {
481                          row[i].valid = 0;
482                          row[i].p[0] = row[i].p[1] = row[i].p[2] = 0.0;
483 +                        row[i].uv[0] = row[i].uv[1] = 0.0;
484                  } else {
485                          row[i].valid = 1;
486                          row[i].p[0] = funvalue(XNAME, 2, st);
487                          row[i].p[1] = funvalue(YNAME, 2, st);
488                          row[i].p[2] = funvalue(ZNAME, 2, st);
489 +                        row[i].uv[0] = st[0];
490 +                        row[i].uv[1] = st[1];
491                  }
492                  i++;
493          }
494   }
495  
496  
497 < compnorms(r0, r1, r2, siz)              /* compute row of averaged normals */
498 < register POINT  *r0, *r1, *r2;
499 < int  siz;
497 > void
498 > compnorms(              /* compute row of averaged normals */
499 >        POINT  *r0,
500 >        POINT  *r1,
501 >        POINT  *r2,
502 >        int  siz
503 > )
504   {
505          FVECT  v1, v2;
506  
# Line 401 | Line 509 | int  siz;
509                                          /* compute row 1 normals */
510          while (siz-- >= 0) {
511                  if (!r1[0].valid)
512 <                        continue;
512 >                        goto skip;
513                  if (!r0[0].valid) {
514                          if (!r2[0].valid) {
515                                  r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0;
516 <                                continue;
516 >                                goto skip;
517                          }
518                          fvsum(v1, r2[0].p, r1[0].p, -1.0);
519                  } else if (!r2[0].valid)
# Line 415 | Line 523 | int  siz;
523                  if (!r1[-1].valid) {
524                          if (!r1[1].valid) {
525                                  r1[0].n[0] = r1[0].n[1] = r1[0].n[2] = 0.0;
526 <                                continue;
526 >                                goto skip;
527                          }
528                          fvsum(v2, r1[1].p, r1[0].p, -1.0);
529                  } else if (!r1[1].valid)
# Line 424 | Line 532 | int  siz;
532                          fvsum(v2, r1[1].p, r1[-1].p, -1.0);
533                  fcross(r1[0].n, v1, v2);
534                  normalize(r1[0].n);
535 +        skip:
536                  r0++; r1++; r2++;
537          }
538   }
539  
540  
541   int
542 < norminterp(resmat, p0, p1, p2, p3)      /* compute normal interpolation */
543 < register FVECT  resmat[4];
544 < POINT  *p0, *p1, *p2, *p3;
542 > norminterp(     /* compute normal interpolation */
543 >        FVECT  resmat[4],
544 >        POINT  *p0,
545 >        POINT  *p1,
546 >        POINT  *p2,
547 >        POINT  *p3
548 > )
549   {
550   #define u  ((ax+1)%3)
551   #define v  ((ax+2)%3)
552  
553 <        register int  ax;
553 >        int  ax;
554          MAT4  eqnmat;
555          FVECT  v1;
556 <        register int  i, j;
556 >        int  i, j;
557  
558          if (!smooth)                    /* no interpolation if no smoothing */
559                  return(-1);
# Line 485 | Line 598 | POINT  *p0, *p1, *p2, *p3;
598   }
599  
600  
488 void
489 eputs(msg)
490 char  *msg;
491 {
492        fputs(msg, stderr);
493 }
494
495
496 void
497 wputs(msg)
498 char  *msg;
499 {
500        eputs(msg);
501 }
502
503
504 void
505 quit(code)
506 int  code;
507 {
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
601   double
602 < l_hermite()                    
602 > l_hermite(char *nm)
603   {
604          double  t;
605          
# Line 536 | Line 612 | l_hermite()                    
612  
613  
614   double
615 < l_bezier()
615 > l_bezier(char *nm)
616   {
617          double  t;
618  
# Line 549 | Line 625 | l_bezier()
625  
626  
627   double
628 < l_bspline()
628 > l_bspline(char *nm)
629   {
630          double  t;
631  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines