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.6 by greg, Mon Mar 10 17:26:26 2003 UTC vs.
Revision 2.19 by greg, Tue Apr 22 04:45:25 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   * Replace markers in Radiance scene description with objects or instances.
# Line 7 | Line 7 | static const char RCSid[] = "$Id";
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 "paths.h"
18   #include "fvect.h"
19  
20   #ifdef  M_PI
# Line 20 | Line 23 | static const char RCSid[] = "$Id";
23   #define  PI             3.14159265358979323846
24   #endif
25  
23 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
24
26   #define  MAXVERT        6       /* maximum number of vertices for markers */
27 < #define  MAXMARK        32      /* maximum number of markers */
27 > #define  MAXMARK        128     /* maximum number of markers */
28  
29 + #define  USE_XFORM      1       /* use !xform inline command */
30 + #define  USE_INSTANCE   2       /* use instance primitive */
31 + #define  USE_MESH       3       /* use mesh primitive */
32 +
33   typedef struct {
34          short   beg, end;               /* beginning and ending vertex */
35          float   len2;                   /* length squared */
# Line 35 | Line 40 | struct mrkr {
40          double  mscale;                 /* scale by this to get unit */
41          char    *modin;                 /* input modifier indicating marker */
42          char    *objname;               /* output object file or octree */
43 <        int     doxform;                /* true if xform, false if instance */
44 < }  marker[MAXMARK];             /* array of markers */
43 >        int     usetype;                /* one of USE_* above */
44 > }  marker[MAXMARK+1];           /* array of markers */
45   int     nmarkers = 0;           /* number of markers */
46  
47   int     expand;                 /* expand commands? */
48  
49   char    *progname;
50  
51 + static void convert(char *name, FILE *fin);
52 + static void cvcomm(char *fname, FILE *fin);
53 + static void cvobject(char *fname, FILE *fin);
54 + static void replace(char *fname, struct mrkr *m, char *mark, FILE *fin);
55 + static int edgecmp(const void *e1, const void *e2);
56 + static int buildxf(char *xf, double markscale, FILE *fin);
57 + static int addrot(char *xf, FVECT xp, FVECT yp, FVECT zp);
58  
59 < main(argc, argv)
60 < int     argc;
61 < char    *argv[];
59 >
60 > int
61 > main(
62 >        int     argc,
63 >        char    *argv[]
64 > )
65   {
66          FILE    *fp;
67          int     i, j;
# Line 57 | Line 72 | char   *argv[];
72                  do {
73                          switch (argv[i][1]) {
74                          case 'i':
75 <                                marker[nmarkers].doxform = 0;
75 >                                marker[nmarkers].usetype = USE_INSTANCE;
76                                  marker[nmarkers].objname = argv[++i];
77                                  break;
78 +                        case 'I':
79 +                                marker[nmarkers].usetype = USE_MESH;
80 +                                marker[nmarkers].objname = argv[++i];
81 +                                break;
82                          case 'x':
83 <                                marker[nmarkers].doxform = 1;
83 >                                marker[nmarkers].usetype = USE_XFORM;
84                                  marker[nmarkers].objname = argv[++i];
85                                  break;
86                          case 'e':
# Line 81 | Line 100 | char   *argv[];
100                  } while (argv[i][0] == '-');
101                  if (marker[nmarkers].objname == NULL)
102                          goto userr;
103 +                if (nmarkers >= MAXMARK) {
104 +                        fprintf(stderr, "%s: too many markers\n", progname);
105 +                        return 1;
106 +                }
107                  marker[nmarkers++].modin = argv[i++];
85                if (nmarkers >= MAXMARK)
86                        break;
108                  marker[nmarkers].mscale = marker[nmarkers-1].mscale;
109          }
110          if (nmarkers == 0)
# Line 106 | Line 127 | char   *argv[];
127                          convert(argv[i], fp);
128                          fclose(fp);
129                  }
130 <        exit(0);
130 >        return 0;
131   userr:
132          fprintf(stderr,
133 < "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n",
133 > "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n",
134                  progname);
135 <        exit(1);
135 >        return 1;
136   }
137  
138  
139 < convert(name, fin)              /* replace marks in a stream */
140 < char    *name;
141 < register FILE   *fin;
139 > void
140 > convert(                /* replace marks in a stream */
141 >        char    *name,
142 >        register FILE   *fin
143 > )
144   {
145          register int    c;
146  
# Line 142 | Line 165 | register FILE  *fin;
165   }
166  
167  
168 < cvcomm(fname, fin)              /* convert a command */
169 < char    *fname;
170 < FILE    *fin;
168 > void
169 > cvcomm(         /* convert a command */
170 >        char    *fname,
171 >        FILE    *fin
172 > )
173   {
174 <        FILE    *pin, *popen();
174 >        FILE    *pin;
175          char    buf[512], *fgetline();
176  
177          fgetline(buf, sizeof(buf), fin);
# Line 158 | Line 183 | FILE   *fin;
183                          exit(1);
184                  }
185                  convert(buf, pin);
186 <                pclose(pin);
186 >                if (pclose(pin) != 0) {
187 >                        fprintf(stderr,
188 >                        "%s: (%s): bad status from \"%s\"\n",
189 >                                        progname, fname, buf);
190 >                        exit(1);
191 >                }
192          } else
193                  printf("\n%s\n", buf);
194   }
195  
196  
197 < cvobject(fname, fin)            /* convert an object */
198 < char    *fname;
199 < FILE    *fin;
197 > void
198 > cvobject(               /* convert an object */
199 >        char    *fname,
200 >        FILE    *fin
201 > )
202   {
203          extern char     *fgetword();
204          char    buf[128], typ[16], nam[128];
205          int     i, n;
206          register int    j;
207  
208 <        if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
208 >        if (fgetword(buf, sizeof(buf), fin) == NULL ||
209 >                        fgetword(typ, sizeof(typ), fin) == NULL ||
210 >                        fgetword(nam, sizeof(nam), fin) == NULL)
211                  goto readerr;
212          if (!strcmp(typ, "polygon"))
213                  for (j = 0; j < nmarkers; j++)
# Line 181 | Line 215 | FILE   *fin;
215                                  replace(fname, &marker[j], nam, fin);
216                                  return;
217                          }
218 <        printf("\n%s %s %s\n", buf, typ, nam);
218 >        putchar('\n'); fputword(buf, stdout);
219 >        printf(" %s ", typ);
220 >        fputword(nam, stdout); putchar('\n');
221          if (!strcmp(typ, "alias")) {            /* alias special case */
222 <                if (fscanf(fin, "%s", buf) != 1)
222 >                if (fgetword(buf, sizeof(buf), fin) == NULL)
223                          goto readerr;
224 <                printf("\t%s\n", buf);
224 >                putchar('\t'); fputword(buf, stdout); putchar('\n');
225                  return;
226          }
227          for (i = 0; i < 3; i++) {               /* pass along arguments */
# Line 209 | Line 245 | readerr:
245   }
246  
247  
248 < replace(fname, m, mark, fin)            /* replace marker */
249 < char    *fname;
250 < register struct mrkr    *m;
251 < char    *mark;
252 < FILE    *fin;
248 > void
249 > replace(                /* replace marker */
250 >        char    *fname,
251 >        register struct mrkr    *m,
252 >        char    *mark,
253 >        FILE    *fin
254 > )
255   {
256          int     n;
257          char    buf[256];
258  
259          buf[0] = '\0';                  /* bug fix thanks to schorsch */
260 <        if (m->doxform) {
261 <                sprintf(buf, "xform -e -n %s", mark);
260 >        if (m->usetype == USE_XFORM) {
261 >                sprintf(buf, "xform -n %s", mark);
262                  if (m->modout != NULL)
263                          sprintf(buf+strlen(buf), " -m %s", m->modout);
264                  if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
# Line 234 | Line 272 | FILE   *fin;
272          } else {
273                  if ((n = buildxf(buf, m->mscale, fin)) < 0)
274                          goto badxf;
275 <                printf("\n%s instance %s\n",
276 <                                m->modout==NULL?"void":m->modout, mark);
275 >                printf("\n%s %s %s\n",
276 >                                m->modout==NULL?"void":m->modout,
277 >                                m->usetype==USE_INSTANCE?"instance":"mesh",
278 >                                mark);
279                  printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
280          }
281          return;
# Line 246 | Line 286 | badxf:
286   }
287  
288  
289 < edgecmp(e1, e2)                 /* compare two edges, descending order */
290 < EDGE    *e1, *e2;
289 > int
290 > edgecmp(                        /* compare two edges, descending order */
291 >        const void *e1,
292 >        const void *e2
293 > )
294   {
295 <        if (e1->len2 > e2->len2)
295 >        if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2)
296                  return(-1);
297 <        if (e1->len2 < e2->len2)
297 >        if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2)
298                  return(1);
299          return(0);
300   }
301  
302  
303   int
304 < buildxf(xf, markscale, fin)             /* build transform for marker */
305 < register char   *xf;
306 < double  markscale;
307 < FILE    *fin;
304 > buildxf(                /* build transform for marker */
305 >        register char   *xf,
306 >        double  markscale,
307 >        FILE    *fin
308 > )
309   {
310          static FVECT    vlist[MAXVERT];
311          static EDGE     elist[MAXVERT];
# Line 339 | Line 383 | FILE   *fin;
383   }
384  
385  
386 < addrot(xf, xp, yp, zp)          /* compute rotation (x,y,z) => (xp,yp,zp) */
387 < register char   *xf;
388 < FVECT   xp, yp, zp;
386 > int
387 > addrot(         /* compute rotation (x,y,z) => (xp,yp,zp) */
388 >        register char   *xf,
389 >        FVECT xp,
390 >        FVECT yp,
391 >        FVECT zp
392 > )
393   {
394          int     n;
395          double  theta;
396  
397 +        if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
398 +                /* Special case for X' along Z-axis */
399 +                theta = -atan2(yp[0], yp[1]);
400 +                sprintf(xf, " -ry %f -rz %f",
401 +                                xp[2] < 0.0 ? 90.0 : -90.0,
402 +                                theta*(180./PI));
403 +                return(4);
404 +        }
405          n = 0;
406          theta = atan2(yp[2], zp[2]);
407 <        if (!FEQ(theta,0.0)) {
407 >        if (!FABSEQ(theta,0.0)) {
408                  sprintf(xf, " -rx %f", theta*(180./PI));
409                  while (*xf) ++xf;
410                  n += 2;
411          }
412 <        theta = asin(-xp[2]);
413 <        if (!FEQ(theta,0.0)) {
412 >        theta = Asin(-xp[2]);
413 >        if (!FABSEQ(theta,0.0)) {
414                  sprintf(xf, " -ry %f", theta*(180./PI));
415                  while (*xf) ++xf;
416                  n += 2;
417          }
418          theta = atan2(xp[1], xp[0]);
419 <        if (!FEQ(theta,0.0)) {
419 >        if (!FABSEQ(theta,0.0)) {
420                  sprintf(xf, " -rz %f", theta*(180./PI));
421                  /* while (*xf) ++xf; */
422                  n += 2;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines