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.10 by greg, Fri Jan 2 17:11:40 2004 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 "rtprocess.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  
26 + #define  FEQ(a,b)       ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
27 +
28   #define  MAXVERT        6       /* maximum number of vertices for markers */
29 + #define  MAXMARK        32      /* maximum number of markers */
30  
31   typedef struct {
32          short   beg, end;               /* beginning and ending vertex */
33          float   len2;                   /* length squared */
34   }  EDGE;                        /* a marker edge */
35  
36 < int     expand = 0;             /* expand commands? */
36 > struct mrkr {
37 >        char    *modout;                /* output modifier */
38 >        double  mscale;                 /* scale by this to get unit */
39 >        char    *modin;                 /* input modifier indicating marker */
40 >        char    *objname;               /* output object file or octree */
41 >        int     doxform;                /* true if xform, false if instance */
42 > }  marker[MAXMARK];             /* array of markers */
43 > int     nmarkers = 0;           /* number of markers */
44  
45 < char    *modout = "void";       /* output modifier (for instances) */
45 > int     expand;                 /* expand commands? */
46  
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
47   char    *progname;
48  
49 + static void convert(char *name, FILE *fin);
50 + static void cvcomm(char *fname, FILE *fin);
51 + static void cvobject(char *fname, FILE *fin);
52 + static void replace(char *fname, struct mrkr *m, char *mark, FILE *fin);
53 + static int edgecmp(const void *e1, const void *e2);
54 + static int buildxf(char *xf, double markscale, FILE *fin);
55 + static int addrot(char *xf, FVECT xp, FVECT yp, FVECT zp);
56  
57 < main(argc, argv)
58 < int     argc;
59 < char    *argv[];
57 >
58 > int
59 > main(
60 >        int     argc,
61 >        char    *argv[]
62 > )
63   {
64          FILE    *fp;
65          int     i, j;
66  
67          progname = argv[0];
68 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
69 <                switch (argv[i][1]) {
70 <                case 'i':
71 <                        doxform = 0;
72 <                        objname = argv[++i];
73 <                        break;
74 <                case 'x':
75 <                        doxform = 1;
76 <                        objname = argv[++i];
77 <                        break;
78 <                case 'e':
79 <                        expand = !expand;
80 <                        break;
81 <                case 'm':
82 <                        modout = argv[++i];
83 <                        break;
84 <                case 's':
85 <                        markscale = atof(argv[++i]);
86 <                        break;
87 <                default:
68 >        i = 1;
69 >        while (i < argc && argv[i][0] == '-') {
70 >                do {
71 >                        switch (argv[i][1]) {
72 >                        case 'i':
73 >                                marker[nmarkers].doxform = 0;
74 >                                marker[nmarkers].objname = argv[++i];
75 >                                break;
76 >                        case 'x':
77 >                                marker[nmarkers].doxform = 1;
78 >                                marker[nmarkers].objname = argv[++i];
79 >                                break;
80 >                        case 'e':
81 >                                expand = 1;
82 >                                break;
83 >                        case 'm':
84 >                                marker[nmarkers].modout = argv[++i];
85 >                                break;
86 >                        case 's':
87 >                                marker[nmarkers].mscale = atof(argv[++i]);
88 >                                break;
89 >                        default:
90 >                                goto userr;
91 >                        }
92 >                        if (++i >= argc)
93 >                                goto userr;
94 >                } while (argv[i][0] == '-');
95 >                if (marker[nmarkers].objname == NULL)
96                          goto userr;
97 <                }
98 <        if (i < argc)
99 <                modin = argv[i++];
100 <        if (objname == NULL || modin == NULL)
97 >                marker[nmarkers++].modin = argv[i++];
98 >                if (nmarkers >= MAXMARK)
99 >                        break;
100 >                marker[nmarkers].mscale = marker[nmarkers-1].mscale;
101 >        }
102 >        if (nmarkers == 0)
103                  goto userr;
104                                          /* simple header */
105          putchar('#');
# Line 95 | Line 119 | char   *argv[];
119                          convert(argv[i], fp);
120                          fclose(fp);
121                  }
122 <        exit(0);
122 >        return 0;
123   userr:
124          fprintf(stderr,
125 < "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname [file ..]\n",
125 > "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n",
126                  progname);
127 <        exit(1);
127 >        return 1;
128   }
129  
130  
131 < convert(name, fin)              /* replace marks in a stream */
132 < char    *name;
133 < register FILE   *fin;
131 > void
132 > convert(                /* replace marks in a stream */
133 >        char    *name,
134 >        register FILE   *fin
135 > )
136   {
137          register int    c;
138  
# Line 131 | Line 157 | register FILE  *fin;
157   }
158  
159  
160 < cvcomm(fname, fin)              /* convert a command */
161 < char    *fname;
162 < FILE    *fin;
160 > void
161 > cvcomm(         /* convert a command */
162 >        char    *fname,
163 >        FILE    *fin
164 > )
165   {
166 <        FILE    *pin, *popen();
166 >        FILE    *pin;
167          char    buf[512], *fgetline();
168  
169          fgetline(buf, sizeof(buf), fin);
# Line 153 | Line 181 | FILE   *fin;
181   }
182  
183  
184 < cvobject(fname, fin)            /* convert an object */
185 < char    *fname;
186 < FILE    *fin;
184 > void
185 > cvobject(               /* convert an object */
186 >        char    *fname,
187 >        FILE    *fin
188 > )
189   {
190 +        extern char     *fgetword();
191          char    buf[128], typ[16], nam[128];
192 <        int     i, j, n;
192 >        int     i, n;
193 >        register int    j;
194  
195          if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
196                  goto readerr;
197 <        if (!strcmp(buf, modin) && !strcmp(typ, "polygon")) {
198 <                replace(fname, nam, fin);
199 <                return;
200 <        }
197 >        if (!strcmp(typ, "polygon"))
198 >                for (j = 0; j < nmarkers; j++)
199 >                        if (!strcmp(buf, marker[j].modin)) {
200 >                                replace(fname, &marker[j], nam, fin);
201 >                                return;
202 >                        }
203          printf("\n%s %s %s\n", buf, typ, nam);
204          if (!strcmp(typ, "alias")) {            /* alias special case */
205                  if (fscanf(fin, "%s", buf) != 1)
# Line 178 | Line 212 | FILE   *fin;
212                          goto readerr;
213                  printf("%d", n);
214                  for (j = 0; j < n; j++) {
215 <                        if (fscanf(fin, "%s", buf) != 1)
215 >                        if (fgetword(buf, sizeof(buf), fin) == NULL)
216                                  goto readerr;
217                          if (j%3 == 0)
218                                  putchar('\n');
219 <                        printf("\t%s", buf);
219 >                        putchar('\t');
220 >                        fputword(buf, stdout);
221                  }
222                  putchar('\n');
223          }
# Line 193 | Line 228 | readerr:
228   }
229  
230  
231 < replace(fname, mark, fin)               /* replace marker */
232 < char    *fname, *mark;
233 < FILE    *fin;
231 > void
232 > replace(                /* replace marker */
233 >        char    *fname,
234 >        register struct mrkr    *m,
235 >        char    *mark,
236 >        FILE    *fin
237 > )
238   {
239          int     n;
240          char    buf[256];
241  
242 <        if (doxform) {
243 <                sprintf(buf, "xform -e -n %s", mark);
244 <                if (buildxf(buf+strlen(buf), fin) < 0)
242 >        buf[0] = '\0';                  /* bug fix thanks to schorsch */
243 >        if (m->doxform) {
244 >                sprintf(buf, "xform -n %s", mark);
245 >                if (m->modout != NULL)
246 >                        sprintf(buf+strlen(buf), " -m %s", m->modout);
247 >                if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
248                          goto badxf;
249 <                sprintf(buf+strlen(buf), " %s", objname);
249 >                sprintf(buf+strlen(buf), " %s", m->objname);
250                  if (expand) {
251                          fflush(stdout);
252                          system(buf);
253                  } else
254                          printf("\n!%s\n", buf);
255          } else {
256 <                if ((n = buildxf(buf, fin)) < 0)
256 >                if ((n = buildxf(buf, m->mscale, fin)) < 0)
257                          goto badxf;
258 <                printf("\n%s instance %s\n", modout, mark);
259 <                printf("%d %s%s\n", n+1, objname, buf);
260 <                printf("\n0\n0\n");
258 >                printf("\n%s instance %s\n",
259 >                                m->modout==NULL?"void":m->modout, mark);
260 >                printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
261          }
262          return;
263   badxf:
# Line 225 | Line 267 | badxf:
267   }
268  
269  
270 < edgecmp(e1, e2)                 /* compare two edges, descending order */
271 < EDGE    *e1, *e2;
270 > int
271 > edgecmp(                        /* compare two edges, descending order */
272 >        const void *e1,
273 >        const void *e2
274 > )
275   {
276 <        if (e1->len2 > e2->len2)
276 >        if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2)
277                  return(-1);
278 <        if (e1->len2 < e2->len2)
278 >        if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2)
279                  return(1);
280          return(0);
281   }
282  
283  
284   int
285 < buildxf(xf, fin)                /* build transform for marker */
286 < char    *xf;
287 < FILE    *fin;
285 > buildxf(                /* build transform for marker */
286 >        register char   *xf,
287 >        double  markscale,
288 >        FILE    *fin
289 > )
290   {
291          static FVECT    vlist[MAXVERT];
292          static EDGE     elist[MAXVERT];
# Line 304 | Line 351 | FILE   *fin;
351          if (markscale > 0.0) {          /* add scale factor */
352                  sprintf(xf, " -s %f", xlen*markscale);
353                  n += 2;
354 <                xf += strlen(xf);
354 >                while (*xf) ++xf;
355          }
356                                          /* add rotation */
357          n += addrot(xf, xvec, yvec, zvec);
358 <        xf += strlen(xf);
358 >        while (*xf) ++xf;
359                                          /* add translation */
360          n += 4;
361          sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
# Line 317 | Line 364 | FILE   *fin;
364   }
365  
366  
367 < addrot(xf, xp, yp, zp)          /* compute rotation (x,y,z) => (xp,yp,zp) */
368 < char    *xf;
369 < FVECT   xp, yp, zp;
367 > int
368 > addrot(         /* compute rotation (x,y,z) => (xp,yp,zp) */
369 >        register char   *xf,
370 >        FVECT xp,
371 >        FVECT yp,
372 >        FVECT zp
373 > )
374   {
375 <        double  tx, ty, tz;
375 >        int     n;
376 >        double  theta;
377  
378 <        tx = atan2(yp[2], zp[2]);
379 <        ty = asin(-xp[2]);
380 <        tz = atan2(xp[1], xp[0]);
381 <        sprintf(xf, " -rx %f -ry %f -rz %f", tx*(180./PI),
382 <                        ty*(180./PI), tz*(180./PI));
383 <        return(6);
378 >        n = 0;
379 >        theta = atan2(yp[2], zp[2]);
380 >        if (!FEQ(theta,0.0)) {
381 >                sprintf(xf, " -rx %f", theta*(180./PI));
382 >                while (*xf) ++xf;
383 >                n += 2;
384 >        }
385 >        theta = asin(-xp[2]);
386 >        if (!FEQ(theta,0.0)) {
387 >                sprintf(xf, " -ry %f", theta*(180./PI));
388 >                while (*xf) ++xf;
389 >                n += 2;
390 >        }
391 >        theta = atan2(xp[1], xp[0]);
392 >        if (!FEQ(theta,0.0)) {
393 >                sprintf(xf, " -rz %f", theta*(180./PI));
394 >                /* while (*xf) ++xf; */
395 >                n += 2;
396 >        }
397 >        return(n);
398   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines