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.9 by greg, Wed Mar 12 04:59:04 2003 UTC vs.
Revision 2.28 by greg, Sat Nov 14 00:29:51 2020 UTC

# 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 29 | Line 34 | char  VNAME[] =        "valid";                /* valid vertex name */
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? */
# Line 45 | 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();
52 extern double  funvalue(), argument();
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 <        FLOAT  uv[2];   /* (u,v) position */
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   {
66        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 79 | 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++;
# Line 90 | Line 114 | char  *argv[];
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 124 | 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);
# Line 133 | Line 158 | char  *argv[];
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 */
# Line 163 | 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 188 | 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 211 | 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 231 | 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 272 | Line 302 | char  *nam;
302   }
303  
304  
305 < putobjrow(rp, n)                        /* output vertex row to .OBJ */
306 < register POINT  *rp;
307 < int  n;
305 > void
306 > putobjrow(                      /* output vertex row to .OBJ */
307 >        POINT  *rp,
308 >        int  n
309 > )
310   {
279        static int      nverts = 0;
280
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))
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 < putsquare(p0, p1, p2, p3)               /* put out a square */
329 < POINT  *p0, *p1, *p2, *p3;
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;
# Line 318 | Line 368 | POINT  *p0, *p1, *p2, *p3;
368          if (!(ok1 | ok2))
369                  return;
370          if (objout) {                   /* output .OBJ faces */
321                int     p0n=0, p1n=0, p2n=0, p3n=0;
322                if (smooth) {
323                        if (!ZEROVECT(p0->n))
324                                p0n = p0->valid;
325                        if (!ZEROVECT(p1->n))
326                                p1n = p1->valid;
327                        if (!ZEROVECT(p2->n))
328                                p2n = p2->valid;
329                        if (!ZEROVECT(p3->n))
330                                p3n = p3->valid;
331                }
371                  if (ok1 & ok2 && fdot(vc1,vc2) >= 1.0-FTINY*FTINY) {
372 <                        printf("f %d/%d/%d %d/%d/%d %d/%d/%d %d/%d/%d\n",
373 <                                        p0->valid, p0->valid, p0n,
374 <                                        p1->valid, p1->valid, p1n,
375 <                                        p3->valid, p3->valid, p3n,
337 <                                        p2->valid, p2->valid, p2n);
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 <                        printf("f %d/%d/%d %d/%d/%d %d/%d/%d\n",
380 <                                        p0->valid, p0->valid, p0n,
381 <                                        p1->valid, p1->valid, p1n,
382 <                                        p2->valid, p2->valid, p2n);
383 <                if (ok2)
384 <                        printf("f %d/%d/%d %d/%d/%d %d/%d/%d\n",
385 <                                        p2->valid, p2->valid, p2n,
386 <                                        p1->valid, p1->valid, p1n,
387 <                                        p3->valid, p3->valid, p3n);
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 */
# Line 356 | 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);
360 <                        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 379 | 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);
383 <                        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 398 | 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);
402 <                        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 417 | 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 457 | Line 494 | int  siz;
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 468 | 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 482 | 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 491 | 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 552 | Line 598 | POINT  *p0, *p1, *p2, *p3;
598   }
599  
600  
555 void
556 eputs(msg)
557 char  *msg;
558 {
559        fputs(msg, stderr);
560 }
561
562
563 void
564 wputs(msg)
565 char  *msg;
566 {
567        eputs(msg);
568 }
569
570
571 void
572 quit(code)
573 int  code;
574 {
575        exit(code);
576 }
577
578
579 printhead(ac, av)               /* print command header */
580 register int  ac;
581 register char  **av;
582 {
583        putchar('#');
584        while (ac--) {
585                putchar(' ');
586                fputs(*av++, stdout);
587        }
588        putchar('\n');
589 }
590
591
601   double
602 < l_hermite()                    
602 > l_hermite(char *nm)
603   {
604          double  t;
605          
# Line 603 | Line 612 | l_hermite()                    
612  
613  
614   double
615 < l_bezier()
615 > l_bezier(char *nm)
616   {
617          double  t;
618  
# Line 616 | 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