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.5 by greg, Sat Feb 22 02:07:24 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 158 | Line 169 | char   *fname;
169   FILE    *fin;
170   {
171          char    buf[128], typ[16], nam[128];
172 <        int     i, j, n;
172 >        int     i, n;
173 >        register int    j;
174  
175          if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
176                  goto readerr;
177 <        if (!strcmp(buf, modin) && !strcmp(typ, "polygon")) {
178 <                replace(fname, nam, fin);
179 <                return;
180 <        }
177 >        if (!strcmp(typ, "polygon"))
178 >                for (j = 0; j < nmarkers; j++)
179 >                        if (!strcmp(buf, marker[j].modin)) {
180 >                                replace(fname, &marker[j], nam, fin);
181 >                                return;
182 >                        }
183          printf("\n%s %s %s\n", buf, typ, nam);
184          if (!strcmp(typ, "alias")) {            /* alias special case */
185                  if (fscanf(fin, "%s", buf) != 1)
# Line 178 | Line 192 | FILE   *fin;
192                          goto readerr;
193                  printf("%d", n);
194                  for (j = 0; j < n; j++) {
195 <                        if (fscanf(fin, "%s", buf) != 1)
195 >                        if (fgetword(buf, sizeof(buf), fin) == NULL)
196                                  goto readerr;
197                          if (j%3 == 0)
198                                  putchar('\n');
199 <                        printf("\t%s", buf);
199 >                        putchar('\t');
200 >                        fputword(buf, stdout);
201                  }
202                  putchar('\n');
203          }
# Line 193 | Line 208 | readerr:
208   }
209  
210  
211 < replace(fname, mark, fin)               /* replace marker */
212 < char    *fname, *mark;
211 > replace(fname, m, mark, fin)            /* replace marker */
212 > char    *fname;
213 > register struct mrkr    *m;
214 > char    *mark;
215   FILE    *fin;
216   {
217          int     n;
218          char    buf[256];
219  
220 <        if (doxform) {
220 >        buf[0] = '\0';                  /* bug fix thanks to schorsch */
221 >        if (m->doxform) {
222                  sprintf(buf, "xform -e -n %s", mark);
223 <                if (buildxf(buf+strlen(buf), fin) < 0)
223 >                if (m->modout != NULL)
224 >                        sprintf(buf+strlen(buf), " -m %s", m->modout);
225 >                if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
226                          goto badxf;
227 <                sprintf(buf+strlen(buf), " %s", objname);
227 >                sprintf(buf+strlen(buf), " %s", m->objname);
228                  if (expand) {
229                          fflush(stdout);
230                          system(buf);
231                  } else
232                          printf("\n!%s\n", buf);
233          } else {
234 <                if ((n = buildxf(buf, fin)) < 0)
234 >                if ((n = buildxf(buf, m->mscale, fin)) < 0)
235                          goto badxf;
236 <                printf("\n%s instance %s\n", modout, mark);
237 <                printf("%d %s%s\n", n+1, objname, buf);
238 <                printf("\n0\n0\n");
236 >                printf("\n%s instance %s\n",
237 >                                m->modout==NULL?"void":m->modout, mark);
238 >                printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
239          }
240          return;
241   badxf:
# Line 237 | Line 257 | EDGE   *e1, *e2;
257  
258  
259   int
260 < buildxf(xf, fin)                /* build transform for marker */
261 < char    *xf;
260 > buildxf(xf, markscale, fin)             /* build transform for marker */
261 > register char   *xf;
262 > double  markscale;
263   FILE    *fin;
264   {
265          static FVECT    vlist[MAXVERT];
# Line 304 | Line 325 | FILE   *fin;
325          if (markscale > 0.0) {          /* add scale factor */
326                  sprintf(xf, " -s %f", xlen*markscale);
327                  n += 2;
328 <                xf += strlen(xf);
328 >                while (*xf) ++xf;
329          }
330                                          /* add rotation */
331          n += addrot(xf, xvec, yvec, zvec);
332 <        xf += strlen(xf);
332 >        while (*xf) ++xf;
333                                          /* add translation */
334          n += 4;
335          sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
# Line 318 | Line 339 | FILE   *fin;
339  
340  
341   addrot(xf, xp, yp, zp)          /* compute rotation (x,y,z) => (xp,yp,zp) */
342 < char    *xf;
342 > register char   *xf;
343   FVECT   xp, yp, zp;
344   {
345 <        double  tx, ty, tz;
345 >        int     n;
346 >        double  theta;
347  
348 <        tx = atan2(yp[2], zp[2]);
349 <        ty = asin(-xp[2]);
350 <        tz = atan2(xp[1], xp[0]);
351 <        sprintf(xf, " -rx %f -ry %f -rz %f", tx*(180./PI),
352 <                        ty*(180./PI), tz*(180./PI));
353 <        return(6);
348 >        n = 0;
349 >        theta = atan2(yp[2], zp[2]);
350 >        if (!FEQ(theta,0.0)) {
351 >                sprintf(xf, " -rx %f", theta*(180./PI));
352 >                while (*xf) ++xf;
353 >                n += 2;
354 >        }
355 >        theta = asin(-xp[2]);
356 >        if (!FEQ(theta,0.0)) {
357 >                sprintf(xf, " -ry %f", theta*(180./PI));
358 >                while (*xf) ++xf;
359 >                n += 2;
360 >        }
361 >        theta = atan2(xp[1], xp[0]);
362 >        if (!FEQ(theta,0.0)) {
363 >                sprintf(xf, " -rz %f", theta*(180./PI));
364 >                /* while (*xf) ++xf; */
365 >                n += 2;
366 >        }
367 >        return(n);
368   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines