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.31 by greg, Tue Jan 21 01:56:27 2025 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 + int  rev = 0;                   /* invert normal directions? */
44  
45   char  *modname, *surfname;
46  
# Line 42 | Line 51 | char  *modname, *surfname;
51   struct {
52          int     flags;                  /* data type */
53          short   m, n;                   /* number of s and t values */
54 <        FLOAT   *data;                  /* the data itself, s major sort */
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();
49 extern double  funvalue(), argument();
59  
60   typedef struct {
61 <        int  valid;     /* point is valid */
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 < main(argc, argv)
72 < int  argc;
73 < char  *argv[];
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   {
62        extern long     eclock;
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);
# Line 75 | Line 98 | char  *argv[];
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 <                        fcompile(argv[++i]);
103 <                else if (!strcmp(argv[i], "-s"))
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 = atoi(argv[6]);
121 <        n = atoi(argv[7]);
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? */
# Line 118 | Line 151 | char  *argv[];
151          }
152          row0++; row1++; row2++;
153                                                  /* print header */
154 <        printhead(argc, argv);
155 <        eclock = 0;
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 */
# Line 134 | Line 173 | char  *argv[];
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))
182 >                        if (!(row0[j].valid && row1[j+1].valid))
183                                  orient = 1;
184 <                        else if (!(row1[j].valid & row0[j+1].valid))
184 >                        else if (!(row1[j].valid && row0[j+1].valid))
185                                  orient = 0;
186                          if (orient)
187                                  putsquare(&row0[j], &row1[j],
# Line 151 | Line 192 | char  *argv[];
192                  }
193          }
194  
195 <        quit(0);
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][-e expr][-f file]\n");
200 <        quit(1);
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 < loaddata(file, m, n, pointsize)         /* load point data from file */
205 < char  *file;
206 < int  m, n;
207 < int  pointsize;
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 <        register int  size;
215 <        register FLOAT  *dp;
214 >        int  size;
215 >        RREAL  *dp;
216  
217          datarec.flags = HASBORDER;              /* assume border values */
218          datarec.m = m+1;
# Line 176 | Line 220 | int  pointsize;
220          size = datarec.m*datarec.n*pointsize;
221          if (pointsize == 3)
222                  datarec.flags |= TRIPLETS;
223 <        dp = (FLOAT *)malloc(size*sizeof(FLOAT));
223 >        dp = (RREAL *)malloc(size*sizeof(RREAL));
224          if ((datarec.data = dp) == NULL) {
225                  fputs("Out of memory\n", stderr);
226                  exit(1);
# Line 199 | Line 243 | int  pointsize;
243                  size--;
244          }
245          if (size == (m+n+1)*pointsize) {        /* no border after all */
246 <                dp = (FLOAT *)realloc((char *)datarec.data,
247 <                                m*n*pointsize*sizeof(FLOAT));
246 >                dp = (RREAL *)realloc(datarec.data,
247 >                                m*n*pointsize*sizeof(RREAL));
248                  if (dp != NULL)
249                          datarec.data = dp;
250                  datarec.flags &= ~HASBORDER;
# Line 219 | Line 263 | int  pointsize;
263  
264  
265   double
266 < l_dataval(nam)                          /* return recorded data value */
267 < char  *nam;
266 > l_dataval(                              /* return recorded data value */
267 >        char  *nam
268 > )
269   {
270          double  u, v;
271 <        register int  i, j;
272 <        register FLOAT  *dp;
271 >        int  i, j;
272 >        RREAL  *dp;
273          double  d00, d01, d10, d11;
274                                                  /* compute coordinates */
275          u = argument(1); v = argument(2);
# Line 260 | Line 305 | char  *nam;
305   }
306  
307  
308 < putsquare(p0, p1, p2, p3)               /* put out a square */
309 < POINT  *p0, *p1, *p2, *p3;
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);
367 >        ok1 = (p0->valid && p1->valid && p2->valid);
368          if (ok1) {
369 <                fvsum(v1, p1->p, p0->p, -1.0);
370 <                fvsum(v2, p2->p, p0->p, -1.0);
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);
374 >        ok2 = (p1->valid && p2->valid && p3->valid);
375          if (ok2) {
376 <                fvsum(v1, p2->p, p3->p, -1.0);
377 <                fvsum(v2, p1->p, p3->p, -1.0);
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  
# Line 292 | Line 407 | POINT  *p0, *p1, *p2, *p3;
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", texname);
296 <                        printf(tsargs);
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]);
# Line 315 | Line 429 | POINT  *p0, *p1, *p2, *p3;
429          if (ok1) {
430                  printf("\n%s ", modname);
431                  if (axis != -1) {
432 <                        printf("texfunc %s\n", texname);
319 <                        printf(tsargs);
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]);
# Line 334 | Line 447 | POINT  *p0, *p1, *p2, *p3;
447          if (ok2) {
448                  printf("\n%s ", modname);
449                  if (axis != -1) {
450 <                        printf("texfunc %s\n", texname);
338 <                        printf(tsargs);
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]);
# Line 353 | Line 465 | POINT  *p0, *p1, *p2, *p3;
465   }
466  
467  
468 < comprow(s, row, siz)                    /* compute row of values */
469 < double  s;
470 < register POINT  *row;
471 < int  siz;
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 <        register int  i;
478 >        int  i;
479          
480          if (smooth) {
481                  i = -1;                 /* compute one past each end */
# Line 379 | Line 493 | int  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 < compnorms(r0, r1, r2, siz)              /* compute row of averaged normals */
511 < register POINT  *r0, *r1, *r2;
512 < int  siz;
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  
# Line 401 | Line 522 | int  siz;
522                                          /* compute row 1 normals */
523          while (siz-- >= 0) {
524                  if (!r1[0].valid)
525 <                        continue;
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 <                                continue;
529 >                                goto skip;
530                          }
531                          fvsum(v1, r2[0].p, r1[0].p, -1.0);
532                  } else if (!r2[0].valid)
# Line 415 | Line 536 | int  siz;
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 <                                continue;
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 <                fcross(r1[0].n, v1, v2);
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(resmat, p0, p1, p2, p3)      /* compute normal interpolation */
559 < register FVECT  resmat[4];
560 < POINT  *p0, *p1, *p2, *p3;
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 <        register int  ax;
569 >        int  ax;
570          MAT4  eqnmat;
571          FVECT  v1;
572 <        register int  i, j;
572 >        int  i, j;
573  
574          if (!smooth)                    /* no interpolation if no smoothing */
575                  return(-1);
# Line 485 | Line 614 | POINT  *p0, *p1, *p2, *p3;
614   }
615  
616  
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
617   double
618 < l_hermite()                    
618 > l_hermite(char *nm)
619   {
620          double  t;
621          
# Line 536 | Line 628 | l_hermite()                    
628  
629  
630   double
631 < l_bezier()
631 > l_bezier(char *nm)
632   {
633          double  t;
634  
# Line 549 | Line 641 | l_bezier()
641  
642  
643   double
644 < l_bspline()
644 > l_bspline(char *nm)
645   {
646          double  t;
647  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines