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 1.2 by greg, Mon Feb 18 13:45:12 1991 UTC vs.
Revision 2.18 by greg, Fri Feb 12 00:53:56 2021 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 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 "paths.h"
18   #include "fvect.h"
19  
20   #ifdef  M_PI
21 < #define  PI             M_PI
21 > #define  PI             ((double)M_PI)
22   #else
23   #define  PI             3.14159265358979323846
24   #endif
25  
25 #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
26
26   #define  MAXVERT        6       /* maximum number of vertices for 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 */
36   }  EDGE;                        /* a marker edge */
37  
38 < int     expand = 0;             /* expand commands? */
38 > struct mrkr {
39 >        char    *modout;                /* output modifier */
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     usetype;                /* one of USE_* above */
44 > }  marker[MAXMARK+1];           /* array of markers */
45 > int     nmarkers = 0;           /* number of markers */
46  
47 < char    *modout = NULL;         /* output modifier (for instances) */
47 > int     expand;                 /* expand commands? */
48  
38 double  markscale = 0.0;        /* scale markers by this to get unit */
39
40 char    *modin = NULL;          /* input modifier indicating marker */
41
42 char    *objname = NULL;        /* output object file (octree if instance) */
43 int     doxform;                /* true if xform, false if instance */
44
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;
68  
69          progname = argv[0];
70 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
71 <                switch (argv[i][1]) {
72 <                case 'i':
73 <                        doxform = 0;
74 <                        objname = argv[++i];
75 <                        break;
76 <                case 'x':
77 <                        doxform = 1;
78 <                        objname = argv[++i];
79 <                        break;
80 <                case 'e':
81 <                        expand = !expand;
82 <                        break;
83 <                case 'm':
84 <                        modout = argv[++i];
85 <                        break;
86 <                case 's':
87 <                        markscale = atof(argv[++i]);
88 <                        break;
89 <                default:
70 >        i = 1;
71 >        while (i < argc && argv[i][0] == '-') {
72 >                do {
73 >                        switch (argv[i][1]) {
74 >                        case 'i':
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].usetype = USE_XFORM;
84 >                                marker[nmarkers].objname = argv[++i];
85 >                                break;
86 >                        case 'e':
87 >                                expand = 1;
88 >                                break;
89 >                        case 'm':
90 >                                marker[nmarkers].modout = argv[++i];
91 >                                break;
92 >                        case 's':
93 >                                marker[nmarkers].mscale = atof(argv[++i]);
94 >                                break;
95 >                        default:
96 >                                goto userr;
97 >                        }
98 >                        if (++i >= argc)
99 >                                goto userr;
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 <        if (i < argc)
108 <                modin = argv[i++];
109 <        if (objname == NULL || modin == NULL)
107 >                marker[nmarkers++].modin = argv[i++];
108 >                marker[nmarkers].mscale = marker[nmarkers-1].mscale;
109 >        }
110 >        if (nmarkers == 0)
111                  goto userr;
112                                          /* simple header */
113          putchar('#');
# Line 97 | 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 133 | 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 155 | Line 189 | FILE   *fin;
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 +        extern char     *fgetword();
199          char    buf[128], typ[16], nam[128];
200 <        int     i, j, n;
200 >        int     i, n;
201 >        register int    j;
202  
203 <        if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
203 >        if (fgetword(buf, sizeof(buf), fin) == NULL ||
204 >                        fgetword(typ, sizeof(typ), fin) == NULL ||
205 >                        fgetword(nam, sizeof(nam), fin) == NULL)
206                  goto readerr;
207 <        if (!strcmp(buf, modin) && !strcmp(typ, "polygon")) {
208 <                replace(fname, nam, fin);
209 <                return;
210 <        }
211 <        printf("\n%s %s %s\n", buf, typ, nam);
207 >        if (!strcmp(typ, "polygon"))
208 >                for (j = 0; j < nmarkers; j++)
209 >                        if (!strcmp(buf, marker[j].modin)) {
210 >                                replace(fname, &marker[j], nam, fin);
211 >                                return;
212 >                        }
213 >        putchar('\n'); fputword(buf, stdout);
214 >        printf(" %s ", typ);
215 >        fputword(nam, stdout); putchar('\n');
216          if (!strcmp(typ, "alias")) {            /* alias special case */
217 <                if (fscanf(fin, "%s", buf) != 1)
217 >                if (fgetword(buf, sizeof(buf), fin) == NULL)
218                          goto readerr;
219 <                printf("\t%s\n", buf);
219 >                putchar('\t'); fputword(buf, stdout); putchar('\n');
220                  return;
221          }
222          for (i = 0; i < 3; i++) {               /* pass along arguments */
# Line 180 | Line 224 | FILE   *fin;
224                          goto readerr;
225                  printf("%d", n);
226                  for (j = 0; j < n; j++) {
227 <                        if (fscanf(fin, "%s", buf) != 1)
227 >                        if (fgetword(buf, sizeof(buf), fin) == NULL)
228                                  goto readerr;
229                          if (j%3 == 0)
230                                  putchar('\n');
231 <                        printf("\t%s", buf);
231 >                        putchar('\t');
232 >                        fputword(buf, stdout);
233                  }
234                  putchar('\n');
235          }
# Line 195 | Line 240 | readerr:
240   }
241  
242  
243 < replace(fname, mark, fin)               /* replace marker */
244 < char    *fname, *mark;
245 < FILE    *fin;
243 > void
244 > replace(                /* replace marker */
245 >        char    *fname,
246 >        register struct mrkr    *m,
247 >        char    *mark,
248 >        FILE    *fin
249 > )
250   {
251          int     n;
252          char    buf[256];
253  
254 <        if (doxform) {
255 <                sprintf(buf, "xform -e -n %s", mark);
256 <                if (modout != NULL)
257 <                        sprintf(buf+strlen(buf), " -m %s", modout);
258 <                if (buildxf(buf+strlen(buf), fin) < 0)
254 >        buf[0] = '\0';                  /* bug fix thanks to schorsch */
255 >        if (m->usetype == USE_XFORM) {
256 >                sprintf(buf, "xform -n %s", mark);
257 >                if (m->modout != NULL)
258 >                        sprintf(buf+strlen(buf), " -m %s", m->modout);
259 >                if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
260                          goto badxf;
261 <                sprintf(buf+strlen(buf), " %s", objname);
261 >                sprintf(buf+strlen(buf), " %s", m->objname);
262                  if (expand) {
263                          fflush(stdout);
264                          system(buf);
265                  } else
266                          printf("\n!%s\n", buf);
267          } else {
268 <                if ((n = buildxf(buf, fin)) < 0)
268 >                if ((n = buildxf(buf, m->mscale, fin)) < 0)
269                          goto badxf;
270 <                printf("\n%s instance %s\n", modout==NULL?"void":modout, mark);
271 <                printf("%d %s%s\n0\n0\n", n+1, objname, buf);
270 >                printf("\n%s %s %s\n",
271 >                                m->modout==NULL?"void":m->modout,
272 >                                m->usetype==USE_INSTANCE?"instance":"mesh",
273 >                                mark);
274 >                printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
275          }
276          return;
277   badxf:
# Line 228 | Line 281 | badxf:
281   }
282  
283  
284 < edgecmp(e1, e2)                 /* compare two edges, descending order */
285 < EDGE    *e1, *e2;
284 > int
285 > edgecmp(                        /* compare two edges, descending order */
286 >        const void *e1,
287 >        const void *e2
288 > )
289   {
290 <        if (e1->len2 > e2->len2)
290 >        if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2)
291                  return(-1);
292 <        if (e1->len2 < e2->len2)
292 >        if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2)
293                  return(1);
294          return(0);
295   }
296  
297  
298   int
299 < buildxf(xf, fin)                /* build transform for marker */
300 < char    *xf;
301 < FILE    *fin;
299 > buildxf(                /* build transform for marker */
300 >        register char   *xf,
301 >        double  markscale,
302 >        FILE    *fin
303 > )
304   {
305          static FVECT    vlist[MAXVERT];
306          static EDGE     elist[MAXVERT];
# Line 307 | Line 365 | FILE   *fin;
365          if (markscale > 0.0) {          /* add scale factor */
366                  sprintf(xf, " -s %f", xlen*markscale);
367                  n += 2;
368 <                xf += strlen(xf);
368 >                while (*xf) ++xf;
369          }
370                                          /* add rotation */
371          n += addrot(xf, xvec, yvec, zvec);
372 <        xf += strlen(xf);
372 >        while (*xf) ++xf;
373                                          /* add translation */
374          n += 4;
375          sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
# Line 320 | Line 378 | FILE   *fin;
378   }
379  
380  
381 < addrot(xf, xp, yp, zp)          /* compute rotation (x,y,z) => (xp,yp,zp) */
382 < char    *xf;
383 < FVECT   xp, yp, zp;
381 > int
382 > addrot(         /* compute rotation (x,y,z) => (xp,yp,zp) */
383 >        register char   *xf,
384 >        FVECT xp,
385 >        FVECT yp,
386 >        FVECT zp
387 > )
388   {
389          int     n;
390          double  theta;
391  
392 +        if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) {
393 +                /* Special case for X' along Z-axis */
394 +                theta = -atan2(yp[0], yp[1]);
395 +                sprintf(xf, " -ry %f -rz %f",
396 +                                xp[2] < 0.0 ? 90.0 : -90.0,
397 +                                theta*(180./PI));
398 +                return(4);
399 +        }
400          n = 0;
401          theta = atan2(yp[2], zp[2]);
402 <        if (!FEQ(theta,0.0)) {
402 >        if (!FABSEQ(theta,0.0)) {
403                  sprintf(xf, " -rx %f", theta*(180./PI));
404 <                xf += strlen(xf);
404 >                while (*xf) ++xf;
405                  n += 2;
406          }
407 <        theta = asin(-xp[2]);
408 <        if (!FEQ(theta,0.0)) {
407 >        theta = Asin(-xp[2]);
408 >        if (!FABSEQ(theta,0.0)) {
409                  sprintf(xf, " -ry %f", theta*(180./PI));
410 <                xf += strlen(xf);
410 >                while (*xf) ++xf;
411                  n += 2;
412          }
413          theta = atan2(xp[1], xp[0]);
414 <        if (!FEQ(theta,0.0)) {
414 >        if (!FABSEQ(theta,0.0)) {
415                  sprintf(xf, " -rz %f", theta*(180./PI));
416 <                /* xf += strlen(xf); */
416 >                /* while (*xf) ++xf; */
417                  n += 2;
418          }
419          return(n);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines