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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines