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

Comparing ray/src/gen/replmarks.c (file contents):
Revision 2.4 by gregl, Fri Jan 16 10:48:23 1998 UTC vs.
Revision 2.23 by greg, Sat Jun 7 05:09:45 2025 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1992 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Replace markers in Radiance scene description with objects or instances.
6   *
7   *      Created:        17 Feb 1991     Greg Ward
8   */
9  
10 < #include <stdio.h>
10 > #include <stdlib.h>
11   #include <ctype.h>
12   #include <math.h>
13 + #include <stdio.h>
14  
15 + #include "platform.h"
16 + #include "rtio.h"
17   #include "fvect.h"
18  
19   #ifdef  M_PI
# Line 22 | Line 22 | static char SCCSid[] = "$SunId$ LBL";
22   #define  PI             3.14159265358979323846
23   #endif
24  
25 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
26
25   #define  MAXVERT        6       /* maximum number of vertices for markers */
26 < #define  MAXMARK        32      /* maximum number of markers */
26 > #define  MAXMARK        128     /* maximum number of markers */
27  
28 < #ifdef  DCL_ATOF
29 < extern double  atof();
30 < #endif
28 > #define  USE_XFORM      1       /* use !xform inline command */
29 > #define  USE_INSTANCE   2       /* use instance primitive */
30 > #define  USE_MESH       3       /* use mesh primitive */
31  
32   typedef struct {
33          short   beg, end;               /* beginning and ending vertex */
# Line 41 | Line 39 | struct mrkr {
39          double  mscale;                 /* scale by this to get unit */
40          char    *modin;                 /* input modifier indicating marker */
41          char    *objname;               /* output object file or octree */
42 <        int     doxform;                /* true if xform, false if instance */
43 < }  marker[MAXMARK];             /* array of markers */
42 >        int     usetype;                /* one of USE_* above */
43 > }  marker[MAXMARK+1];           /* array of markers */
44   int     nmarkers = 0;           /* number of markers */
45  
46   int     expand;                 /* expand commands? */
47  
48 < char    *progname;
48 > static void convert(char *name, FILE *fin);
49 > static void cvcomm(char *fname, FILE *fin);
50 > static void cvobject(char *fname, FILE *fin);
51 > static void replace(char *fname, struct mrkr *m, char *mark, FILE *fin);
52 > static int edgecmp(const void *e1, const void *e2);
53 > static int buildxf(char *xf, double markscale, FILE *fin);
54 > static int addrot(char *xf, FVECT xp, FVECT yp, FVECT zp);
55  
56  
57 < main(argc, argv)
58 < int     argc;
59 < char    *argv[];
57 > int
58 > main(
59 >        int     argc,
60 >        char    *argv[]
61 > )
62   {
63          FILE    *fp;
64          int     i, j;
65  
66 <        progname = argv[0];
66 >        fixargv0(argv[0]);              /* sets global progname */
67          i = 1;
68          while (i < argc && argv[i][0] == '-') {
69                  do {
70                          switch (argv[i][1]) {
71                          case 'i':
72 <                                marker[nmarkers].doxform = 0;
72 >                                marker[nmarkers].usetype = USE_INSTANCE;
73                                  marker[nmarkers].objname = argv[++i];
74                                  break;
75 +                        case 'I':
76 +                                marker[nmarkers].usetype = USE_MESH;
77 +                                marker[nmarkers].objname = argv[++i];
78 +                                break;
79                          case 'x':
80 <                                marker[nmarkers].doxform = 1;
80 >                                marker[nmarkers].usetype = USE_XFORM;
81                                  marker[nmarkers].objname = argv[++i];
82                                  break;
83                          case 'e':
# Line 87 | Line 97 | char   *argv[];
97                  } while (argv[i][0] == '-');
98                  if (marker[nmarkers].objname == NULL)
99                          goto userr;
100 +                if (nmarkers >= MAXMARK) {
101 +                        fprintf(stderr, "%s: too many markers\n", progname);
102 +                        return 1;
103 +                }
104                  marker[nmarkers++].modin = argv[i++];
91                if (nmarkers >= MAXMARK)
92                        break;
105                  marker[nmarkers].mscale = marker[nmarkers-1].mscale;
106          }
107          if (nmarkers == 0)
# Line 112 | Line 124 | char   *argv[];
124                          convert(argv[i], fp);
125                          fclose(fp);
126                  }
127 <        exit(0);
127 >        return 0;
128   userr:
129          fprintf(stderr,
130 < "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n",
130 > "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n",
131                  progname);
132 <        exit(1);
132 >        return 1;
133   }
134  
135  
136 < convert(name, fin)              /* replace marks in a stream */
137 < char    *name;
138 < register FILE   *fin;
136 > void
137 > convert(                /* replace marks in a stream */
138 >        char    *name,
139 >        FILE    *fin
140 > )
141   {
142 <        register int    c;
142 >        int     c;
143  
144          while ((c = getc(fin)) != EOF) {
145                  if (isspace(c))                         /* blank */
# Line 148 | Line 162 | register FILE  *fin;
162   }
163  
164  
165 < cvcomm(fname, fin)              /* convert a command */
166 < char    *fname;
167 < FILE    *fin;
165 > void
166 > cvcomm(         /* convert a command */
167 >        char    *fname,
168 >        FILE    *fin
169 > )
170   {
171 <        FILE    *pin, *popen();
172 <        char    buf[512], *fgetline();
171 >        FILE    *pin;
172 >        char    buf[512];
173  
174          fgetline(buf, sizeof(buf), fin);
175          if (expand) {
# Line 164 | Line 180 | FILE   *fin;
180                          exit(1);
181                  }
182                  convert(buf, pin);
183 <                pclose(pin);
183 >                if (pclose(pin) != 0)
184 >                        fprintf(stderr,
185 >                        "%s: (%s): warning - bad status from \"%s\"\n",
186 >                                        progname, fname, buf);
187          } else
188                  printf("\n%s\n", buf);
189   }
190  
191  
192 < cvobject(fname, fin)            /* convert an object */
193 < char    *fname;
194 < FILE    *fin;
192 > void
193 > cvobject(               /* convert an object */
194 >        char    *fname,
195 >        FILE    *fin
196 > )
197   {
198          char    buf[128], typ[16], nam[128];
199          int     i, n;
200 <        register int    j;
200 >        int     j;
201  
202 <        if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
202 >        if (fgetword(buf, sizeof(buf), fin) == NULL ||
203 >                        fgetword(typ, sizeof(typ), fin) == NULL ||
204 >                        fgetword(nam, sizeof(nam), fin) == NULL)
205                  goto readerr;
206          if (!strcmp(typ, "polygon"))
207                  for (j = 0; j < nmarkers; j++)
# Line 186 | Line 209 | FILE   *fin;
209                                  replace(fname, &marker[j], nam, fin);
210                                  return;
211                          }
212 <        printf("\n%s %s %s\n", buf, typ, nam);
212 >        putchar('\n'); fputword(buf, stdout);
213 >        printf(" %s ", typ);
214 >        fputword(nam, stdout); putchar('\n');
215          if (!strcmp(typ, "alias")) {            /* alias special case */
216 <                if (fscanf(fin, "%s", buf) != 1)
216 >                if (fgetword(buf, sizeof(buf), fin) == NULL)
217                          goto readerr;
218 <                printf("\t%s\n", buf);
218 >                putchar('\t'); fputword(buf, stdout); putchar('\n');
219                  return;
220          }
221          for (i = 0; i < 3; i++) {               /* pass along arguments */
# Line 198 | Line 223 | FILE   *fin;
223                          goto readerr;
224                  printf("%d", n);
225                  for (j = 0; j < n; j++) {
226 <                        if (fscanf(fin, "%s", buf) != 1)
226 >                        if (fgetword(buf, sizeof(buf), fin) == NULL)
227                                  goto readerr;
228                          if (j%3 == 0)
229                                  putchar('\n');
230 <                        printf("\t%s", buf);
230 >                        putchar('\t');
231 >                        fputword(buf, stdout);
232                  }
233                  putchar('\n');
234          }
# Line 213 | Line 239 | readerr:
239   }
240  
241  
242 < replace(fname, m, mark, fin)            /* replace marker */
243 < char    *fname;
244 < register struct mrkr    *m;
245 < char    *mark;
246 < FILE    *fin;
242 > void
243 > replace(                /* replace marker */
244 >        char    *fname,
245 >        struct mrkr     *m,
246 >        char    *mark,
247 >        FILE    *fin
248 > )
249   {
250          int     n;
251          char    buf[256];
252  
253 <        if (m->doxform) {
254 <                sprintf(buf, "xform -e -n %s", mark);
253 >        buf[0] = '\0';                  /* bug fix thanks to schorsch */
254 >        if (m->usetype == USE_XFORM) {
255 >                sprintf(buf, "xform -n %s", mark);
256                  if (m->modout != NULL)
257                          sprintf(buf+strlen(buf), " -m %s", m->modout);
258                  if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
# Line 237 | Line 266 | FILE   *fin;
266          } else {
267                  if ((n = buildxf(buf, m->mscale, fin)) < 0)
268                          goto badxf;
269 <                printf("\n%s instance %s\n",
270 <                                m->modout==NULL?"void":m->modout, mark);
269 >                printf("\n%s %s %s\n",
270 >                                m->modout==NULL?"void":m->modout,
271 >                                m->usetype==USE_INSTANCE?"instance":"mesh",
272 >                                mark);
273                  printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
274          }
275          return;
# Line 249 | Line 280 | badxf:
280   }
281  
282  
283 < edgecmp(e1, e2)                 /* compare two edges, descending order */
284 < EDGE    *e1, *e2;
283 > int
284 > edgecmp(                        /* compare two edges, descending order */
285 >        const void *e1,
286 >        const void *e2
287 > )
288   {
289 <        if (e1->len2 > e2->len2)
289 >        if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2)
290                  return(-1);
291 <        if (e1->len2 < e2->len2)
291 >        if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2)
292                  return(1);
293          return(0);
294   }
295  
296  
297   int
298 < buildxf(xf, markscale, fin)             /* build transform for marker */
299 < char    *xf;
300 < double  markscale;
301 < FILE    *fin;
298 > buildxf(                /* build transform for marker */
299 >        char    *xf,
300 >        double  markscale,
301 >        FILE    *fin
302 > )
303   {
304          static FVECT    vlist[MAXVERT];
305          static EDGE     elist[MAXVERT];
306          FVECT   xvec, yvec, zvec;
307          double  xlen;
308          int     n;
309 <        register int    i;
309 >        int     i;
310          /*
311           * Read and sort vectors:  longest is hypotenuse,
312           *      second longest is x' axis,
# Line 329 | Line 364 | FILE   *fin;
364          if (markscale > 0.0) {          /* add scale factor */
365                  sprintf(xf, " -s %f", xlen*markscale);
366                  n += 2;
367 <                xf += strlen(xf);
367 >                while (*xf) ++xf;
368          }
369                                          /* add rotation */
370          n += addrot(xf, xvec, yvec, zvec);
371 <        xf += strlen(xf);
371 >        while (*xf) ++xf;
372                                          /* add translation */
373          n += 4;
374          sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
# Line 342 | Line 377 | FILE   *fin;
377   }
378  
379  
380 < addrot(xf, xp, yp, zp)          /* compute rotation (x,y,z) => (xp,yp,zp) */
381 < char    *xf;
382 < FVECT   xp, yp, zp;
380 > int
381 > addrot(         /* compute rotation (x,y,z) => (xp,yp,zp) */
382 >        char    *xf,
383 >        FVECT xp,
384 >        FVECT yp,
385 >        FVECT zp
386 > )
387   {
388          int     n;
389          double  theta;
390  
391 +        if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
392 +                /* Special case for X' along Z-axis */
393 +                theta = -atan2(yp[0], yp[1]);
394 +                sprintf(xf, " -ry %f -rz %f",
395 +                                xp[2] < 0.0 ? 90.0 : -90.0,
396 +                                theta*(180./PI));
397 +                return(4);
398 +        }
399          n = 0;
400          theta = atan2(yp[2], zp[2]);
401 <        if (!FEQ(theta,0.0)) {
401 >        if (!FABSEQ(theta,0.0)) {
402                  sprintf(xf, " -rx %f", theta*(180./PI));
403 <                xf += strlen(xf);
403 >                while (*xf) ++xf;
404                  n += 2;
405          }
406 <        theta = asin(-xp[2]);
407 <        if (!FEQ(theta,0.0)) {
406 >        theta = Asin(-xp[2]);
407 >        if (!FABSEQ(theta,0.0)) {
408                  sprintf(xf, " -ry %f", theta*(180./PI));
409 <                xf += strlen(xf);
409 >                while (*xf) ++xf;
410                  n += 2;
411          }
412          theta = atan2(xp[1], xp[0]);
413 <        if (!FEQ(theta,0.0)) {
413 >        if (!FABSEQ(theta,0.0)) {
414                  sprintf(xf, " -rz %f", theta*(180./PI));
415 <                /* xf += strlen(xf); */
415 >                /* while (*xf) ++xf; */
416                  n += 2;
417          }
418          return(n);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines