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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines