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.1 by greg, Sun Feb 17 22:36:30 1991 UTC vs.
Revision 2.8 by schorsch, Mon Oct 27 10:27:25 2003 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 "rtprocess.h"
17   #include "fvect.h"
18  
19   #ifdef  M_PI
20 < #define  PI             M_PI
20 > #define  PI             ((double)M_PI)
21   #else
22   #define  PI             3.14159265358979323846
23   #endif
24  
25 + #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
26 +
27   #define  MAXVERT        6       /* maximum number of vertices for markers */
28 + #define  MAXMARK        32      /* maximum number of markers */
29  
30   typedef struct {
31          short   beg, end;               /* beginning and ending vertex */
32          float   len2;                   /* length squared */
33   }  EDGE;                        /* a marker edge */
34  
35 < int     expand = 0;             /* expand commands? */
35 > struct mrkr {
36 >        char    *modout;                /* output modifier */
37 >        double  mscale;                 /* scale by this to get unit */
38 >        char    *modin;                 /* input modifier indicating marker */
39 >        char    *objname;               /* output object file or octree */
40 >        int     doxform;                /* true if xform, false if instance */
41 > }  marker[MAXMARK];             /* array of markers */
42 > int     nmarkers = 0;           /* number of markers */
43  
44 < char    *modout = "void";       /* output modifier (for instances) */
44 > int     expand;                 /* expand commands? */
45  
36 double  markscale = 0.0;        /* scale markers by this to get unit */
37
38 char    *modin = NULL;          /* input modifier indicating marker */
39
40 char    *objname = NULL;        /* output object file (octree if instance) */
41 int     doxform;                /* true if xform, false if instance */
42
46   char    *progname;
47  
48  
# Line 51 | Line 54 | char   *argv[];
54          int     i, j;
55  
56          progname = argv[0];
57 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
58 <                switch (argv[i][1]) {
59 <                case 'i':
60 <                        doxform = 0;
61 <                        objname = argv[++i];
62 <                        break;
63 <                case 'x':
64 <                        doxform = 1;
65 <                        objname = argv[++i];
66 <                        break;
67 <                case 'e':
68 <                        expand = !expand;
69 <                        break;
70 <                case 'm':
71 <                        modout = argv[++i];
72 <                        break;
73 <                case 's':
74 <                        markscale = atof(argv[++i]);
75 <                        break;
76 <                default:
57 >        i = 1;
58 >        while (i < argc && argv[i][0] == '-') {
59 >                do {
60 >                        switch (argv[i][1]) {
61 >                        case 'i':
62 >                                marker[nmarkers].doxform = 0;
63 >                                marker[nmarkers].objname = argv[++i];
64 >                                break;
65 >                        case 'x':
66 >                                marker[nmarkers].doxform = 1;
67 >                                marker[nmarkers].objname = argv[++i];
68 >                                break;
69 >                        case 'e':
70 >                                expand = 1;
71 >                                break;
72 >                        case 'm':
73 >                                marker[nmarkers].modout = argv[++i];
74 >                                break;
75 >                        case 's':
76 >                                marker[nmarkers].mscale = atof(argv[++i]);
77 >                                break;
78 >                        default:
79 >                                goto userr;
80 >                        }
81 >                        if (++i >= argc)
82 >                                goto userr;
83 >                } while (argv[i][0] == '-');
84 >                if (marker[nmarkers].objname == NULL)
85                          goto userr;
86 <                }
87 <        if (i < argc)
88 <                modin = argv[i++];
89 <        if (objname == NULL || modin == NULL)
86 >                marker[nmarkers++].modin = argv[i++];
87 >                if (nmarkers >= MAXMARK)
88 >                        break;
89 >                marker[nmarkers].mscale = marker[nmarkers-1].mscale;
90 >        }
91 >        if (nmarkers == 0)
92                  goto userr;
93                                          /* simple header */
94          putchar('#');
# Line 98 | Line 111 | char   *argv[];
111          exit(0);
112   userr:
113          fprintf(stderr,
114 < "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname [file ..]\n",
114 > "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n",
115                  progname);
116          exit(1);
117   }
# Line 135 | Line 148 | cvcomm(fname, fin)             /* convert a command */
148   char    *fname;
149   FILE    *fin;
150   {
151 <        FILE    *pin, *popen();
151 >        FILE    *pin;
152          char    buf[512], *fgetline();
153  
154          fgetline(buf, sizeof(buf), fin);
# Line 157 | Line 170 | cvobject(fname, fin)           /* convert an object */
170   char    *fname;
171   FILE    *fin;
172   {
173 +        extern char     *fgetword();
174          char    buf[128], typ[16], nam[128];
175 <        int     i, j, n;
175 >        int     i, n;
176 >        register int    j;
177  
178          if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
179                  goto readerr;
180 <        if (!strcmp(buf, modin) && !strcmp(typ, "polygon")) {
181 <                replace(fname, nam, fin);
182 <                return;
183 <        }
180 >        if (!strcmp(typ, "polygon"))
181 >                for (j = 0; j < nmarkers; j++)
182 >                        if (!strcmp(buf, marker[j].modin)) {
183 >                                replace(fname, &marker[j], nam, fin);
184 >                                return;
185 >                        }
186          printf("\n%s %s %s\n", buf, typ, nam);
187          if (!strcmp(typ, "alias")) {            /* alias special case */
188                  if (fscanf(fin, "%s", buf) != 1)
# Line 178 | Line 195 | FILE   *fin;
195                          goto readerr;
196                  printf("%d", n);
197                  for (j = 0; j < n; j++) {
198 <                        if (fscanf(fin, "%s", buf) != 1)
198 >                        if (fgetword(buf, sizeof(buf), fin) == NULL)
199                                  goto readerr;
200                          if (j%3 == 0)
201                                  putchar('\n');
202 <                        printf("\t%s", buf);
202 >                        putchar('\t');
203 >                        fputword(buf, stdout);
204                  }
205                  putchar('\n');
206          }
# Line 193 | Line 211 | readerr:
211   }
212  
213  
214 < replace(fname, mark, fin)               /* replace marker */
215 < char    *fname, *mark;
214 > replace(fname, m, mark, fin)            /* replace marker */
215 > char    *fname;
216 > register struct mrkr    *m;
217 > char    *mark;
218   FILE    *fin;
219   {
220          int     n;
221          char    buf[256];
222  
223 <        if (doxform) {
223 >        buf[0] = '\0';                  /* bug fix thanks to schorsch */
224 >        if (m->doxform) {
225                  sprintf(buf, "xform -e -n %s", mark);
226 <                if (buildxf(buf+strlen(buf), fin) < 0)
226 >                if (m->modout != NULL)
227 >                        sprintf(buf+strlen(buf), " -m %s", m->modout);
228 >                if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
229                          goto badxf;
230 <                sprintf(buf+strlen(buf), " %s", objname);
230 >                sprintf(buf+strlen(buf), " %s", m->objname);
231                  if (expand) {
232                          fflush(stdout);
233                          system(buf);
234                  } else
235                          printf("\n!%s\n", buf);
236          } else {
237 <                if ((n = buildxf(buf, fin)) < 0)
237 >                if ((n = buildxf(buf, m->mscale, fin)) < 0)
238                          goto badxf;
239 <                printf("\n%s instance %s\n", modout, mark);
240 <                printf("%d %s%s\n", n+1, objname, buf);
241 <                printf("\n0\n0\n");
239 >                printf("\n%s instance %s\n",
240 >                                m->modout==NULL?"void":m->modout, mark);
241 >                printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
242          }
243          return;
244   badxf:
# Line 237 | Line 260 | EDGE   *e1, *e2;
260  
261  
262   int
263 < buildxf(xf, fin)                /* build transform for marker */
264 < char    *xf;
263 > buildxf(xf, markscale, fin)             /* build transform for marker */
264 > register char   *xf;
265 > double  markscale;
266   FILE    *fin;
267   {
268          static FVECT    vlist[MAXVERT];
# Line 304 | Line 328 | FILE   *fin;
328          if (markscale > 0.0) {          /* add scale factor */
329                  sprintf(xf, " -s %f", xlen*markscale);
330                  n += 2;
331 <                xf += strlen(xf);
331 >                while (*xf) ++xf;
332          }
333                                          /* add rotation */
334          n += addrot(xf, xvec, yvec, zvec);
335 <        xf += strlen(xf);
335 >        while (*xf) ++xf;
336                                          /* add translation */
337          n += 4;
338          sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
# Line 318 | Line 342 | FILE   *fin;
342  
343  
344   addrot(xf, xp, yp, zp)          /* compute rotation (x,y,z) => (xp,yp,zp) */
345 < char    *xf;
345 > register char   *xf;
346   FVECT   xp, yp, zp;
347   {
348 <        double  tx, ty, tz;
348 >        int     n;
349 >        double  theta;
350  
351 <        tx = atan2(yp[2], zp[2]);
352 <        ty = asin(-xp[2]);
353 <        tz = atan2(xp[1], xp[0]);
354 <        sprintf(xf, " -rx %f -ry %f -rz %f", tx*(180./PI),
355 <                        ty*(180./PI), tz*(180./PI));
356 <        return(6);
351 >        n = 0;
352 >        theta = atan2(yp[2], zp[2]);
353 >        if (!FEQ(theta,0.0)) {
354 >                sprintf(xf, " -rx %f", theta*(180./PI));
355 >                while (*xf) ++xf;
356 >                n += 2;
357 >        }
358 >        theta = asin(-xp[2]);
359 >        if (!FEQ(theta,0.0)) {
360 >                sprintf(xf, " -ry %f", theta*(180./PI));
361 >                while (*xf) ++xf;
362 >                n += 2;
363 >        }
364 >        theta = atan2(xp[1], xp[0]);
365 >        if (!FEQ(theta,0.0)) {
366 >                sprintf(xf, " -rz %f", theta*(180./PI));
367 >                /* while (*xf) ++xf; */
368 >                n += 2;
369 >        }
370 >        return(n);
371   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines