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 2.1 by greg, Tue Nov 12 17:04:51 1991 UTC vs.
Revision 2.14 by greg, Wed Dec 28 18:35:42 2005 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
# Line 25 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
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        128     /* maximum number of markers */
30  
31 + #define  USE_XFORM      1       /* use !xform inline command */
32 + #define  USE_INSTANCE   2       /* use instance primitive */
33 + #define  USE_MESH       3       /* use mesh primitive */
34 +
35   typedef struct {
36          short   beg, end;               /* beginning and ending vertex */
37          float   len2;                   /* length squared */
38   }  EDGE;                        /* a marker edge */
39  
40 < int     expand = 0;             /* expand commands? */
40 > struct mrkr {
41 >        char    *modout;                /* output modifier */
42 >        double  mscale;                 /* scale by this to get unit */
43 >        char    *modin;                 /* input modifier indicating marker */
44 >        char    *objname;               /* output object file or octree */
45 >        int     usetype;                /* one of USE_* above */
46 > }  marker[MAXMARK+1];           /* array of markers */
47 > int     nmarkers = 0;           /* number of markers */
48  
49 < char    *modout = NULL;         /* output modifier (for instances) */
49 > int     expand;                 /* expand commands? */
50  
38 double  markscale = 0.0;        /* scale markers by this to get unit */
39
40 char    *modin = NULL;          /* input modifier indicating marker */
41
42 char    *objname = NULL;        /* output object file (octree if instance) */
43 int     doxform;                /* true if xform, false if instance */
44
51   char    *progname;
52  
53 + static void convert(char *name, FILE *fin);
54 + static void cvcomm(char *fname, FILE *fin);
55 + static void cvobject(char *fname, FILE *fin);
56 + static void replace(char *fname, struct mrkr *m, char *mark, FILE *fin);
57 + static int edgecmp(const void *e1, const void *e2);
58 + static int buildxf(char *xf, double markscale, FILE *fin);
59 + static int addrot(char *xf, FVECT xp, FVECT yp, FVECT zp);
60  
61 < main(argc, argv)
62 < int     argc;
63 < char    *argv[];
61 >
62 > int
63 > main(
64 >        int     argc,
65 >        char    *argv[]
66 > )
67   {
68          FILE    *fp;
69          int     i, j;
70  
71          progname = argv[0];
72 <        for (i = 1; i < argc && argv[i][0] == '-'; i++)
73 <                switch (argv[i][1]) {
74 <                case 'i':
75 <                        doxform = 0;
76 <                        objname = argv[++i];
77 <                        break;
78 <                case 'x':
79 <                        doxform = 1;
80 <                        objname = argv[++i];
81 <                        break;
82 <                case 'e':
83 <                        expand = !expand;
84 <                        break;
85 <                case 'm':
86 <                        modout = argv[++i];
87 <                        break;
88 <                case 's':
89 <                        markscale = atof(argv[++i]);
90 <                        break;
91 <                default:
72 >        i = 1;
73 >        while (i < argc && argv[i][0] == '-') {
74 >                do {
75 >                        switch (argv[i][1]) {
76 >                        case 'i':
77 >                                marker[nmarkers].usetype = USE_INSTANCE;
78 >                                marker[nmarkers].objname = argv[++i];
79 >                                break;
80 >                        case 'I':
81 >                                marker[nmarkers].usetype = USE_MESH;
82 >                                marker[nmarkers].objname = argv[++i];
83 >                                break;
84 >                        case 'x':
85 >                                marker[nmarkers].usetype = USE_XFORM;
86 >                                marker[nmarkers].objname = argv[++i];
87 >                                break;
88 >                        case 'e':
89 >                                expand = 1;
90 >                                break;
91 >                        case 'm':
92 >                                marker[nmarkers].modout = argv[++i];
93 >                                break;
94 >                        case 's':
95 >                                marker[nmarkers].mscale = atof(argv[++i]);
96 >                                break;
97 >                        default:
98 >                                goto userr;
99 >                        }
100 >                        if (++i >= argc)
101 >                                goto userr;
102 >                } while (argv[i][0] == '-');
103 >                if (marker[nmarkers].objname == NULL)
104                          goto userr;
105 +                if (nmarkers >= MAXMARK) {
106 +                        fprintf(stderr, "%s: too many markers\n", progname);
107 +                        return 1;
108                  }
109 <        if (i < argc)
110 <                modin = argv[i++];
111 <        if (objname == NULL || modin == NULL)
109 >                marker[nmarkers++].modin = argv[i++];
110 >                marker[nmarkers].mscale = marker[nmarkers-1].mscale;
111 >        }
112 >        if (nmarkers == 0)
113                  goto userr;
114                                          /* simple header */
115          putchar('#');
# Line 97 | Line 129 | char   *argv[];
129                          convert(argv[i], fp);
130                          fclose(fp);
131                  }
132 <        exit(0);
132 >        return 0;
133   userr:
134          fprintf(stderr,
135 < "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname [file ..]\n",
135 > "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n",
136                  progname);
137 <        exit(1);
137 >        return 1;
138   }
139  
140  
141 < convert(name, fin)              /* replace marks in a stream */
142 < char    *name;
143 < register FILE   *fin;
141 > void
142 > convert(                /* replace marks in a stream */
143 >        char    *name,
144 >        register FILE   *fin
145 > )
146   {
147          register int    c;
148  
# Line 133 | Line 167 | register FILE  *fin;
167   }
168  
169  
170 < cvcomm(fname, fin)              /* convert a command */
171 < char    *fname;
172 < FILE    *fin;
170 > void
171 > cvcomm(         /* convert a command */
172 >        char    *fname,
173 >        FILE    *fin
174 > )
175   {
176 <        FILE    *pin, *popen();
176 >        FILE    *pin;
177          char    buf[512], *fgetline();
178  
179          fgetline(buf, sizeof(buf), fin);
# Line 155 | Line 191 | FILE   *fin;
191   }
192  
193  
194 < cvobject(fname, fin)            /* convert an object */
195 < char    *fname;
196 < FILE    *fin;
194 > void
195 > cvobject(               /* convert an object */
196 >        char    *fname,
197 >        FILE    *fin
198 > )
199   {
200 +        extern char     *fgetword();
201          char    buf[128], typ[16], nam[128];
202 <        int     i, j, n;
202 >        int     i, n;
203 >        register int    j;
204  
205 <        if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
205 >        if (fgetword(buf, sizeof(buf), fin) == NULL ||
206 >                        fgetword(typ, sizeof(typ), fin) == NULL ||
207 >                        fgetword(nam, sizeof(nam), fin) == NULL)
208                  goto readerr;
209 <        if (!strcmp(buf, modin) && !strcmp(typ, "polygon")) {
210 <                replace(fname, nam, fin);
211 <                return;
212 <        }
213 <        printf("\n%s %s %s\n", buf, typ, nam);
209 >        if (!strcmp(typ, "polygon"))
210 >                for (j = 0; j < nmarkers; j++)
211 >                        if (!strcmp(buf, marker[j].modin)) {
212 >                                replace(fname, &marker[j], nam, fin);
213 >                                return;
214 >                        }
215 >        putchar('\n'); fputword(buf, stdout);
216 >        printf(" %s ", typ);
217 >        fputword(nam, stdout); putchar('\n');
218          if (!strcmp(typ, "alias")) {            /* alias special case */
219 <                if (fscanf(fin, "%s", buf) != 1)
219 >                if (fgetword(buf, sizeof(buf), fin) == NULL)
220                          goto readerr;
221 <                printf("\t%s\n", buf);
221 >                putchar('\t'); fputword(buf, stdout); putchar('\n');
222                  return;
223          }
224          for (i = 0; i < 3; i++) {               /* pass along arguments */
# Line 180 | Line 226 | FILE   *fin;
226                          goto readerr;
227                  printf("%d", n);
228                  for (j = 0; j < n; j++) {
229 <                        if (fscanf(fin, "%s", buf) != 1)
229 >                        if (fgetword(buf, sizeof(buf), fin) == NULL)
230                                  goto readerr;
231                          if (j%3 == 0)
232                                  putchar('\n');
233 <                        printf("\t%s", buf);
233 >                        putchar('\t');
234 >                        fputword(buf, stdout);
235                  }
236                  putchar('\n');
237          }
# Line 195 | Line 242 | readerr:
242   }
243  
244  
245 < replace(fname, mark, fin)               /* replace marker */
246 < char    *fname, *mark;
247 < FILE    *fin;
245 > void
246 > replace(                /* replace marker */
247 >        char    *fname,
248 >        register struct mrkr    *m,
249 >        char    *mark,
250 >        FILE    *fin
251 > )
252   {
253          int     n;
254          char    buf[256];
255  
256 <        if (doxform) {
257 <                sprintf(buf, "xform -e -n %s", mark);
258 <                if (modout != NULL)
259 <                        sprintf(buf+strlen(buf), " -m %s", modout);
260 <                if (buildxf(buf+strlen(buf), fin) < 0)
256 >        buf[0] = '\0';                  /* bug fix thanks to schorsch */
257 >        if (m->usetype == USE_XFORM) {
258 >                sprintf(buf, "xform -n %s", mark);
259 >                if (m->modout != NULL)
260 >                        sprintf(buf+strlen(buf), " -m %s", m->modout);
261 >                if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
262                          goto badxf;
263 <                sprintf(buf+strlen(buf), " %s", objname);
263 >                sprintf(buf+strlen(buf), " %s", m->objname);
264                  if (expand) {
265                          fflush(stdout);
266                          system(buf);
267                  } else
268                          printf("\n!%s\n", buf);
269          } else {
270 <                if ((n = buildxf(buf, fin)) < 0)
270 >                if ((n = buildxf(buf, m->mscale, fin)) < 0)
271                          goto badxf;
272 <                printf("\n%s instance %s\n", modout==NULL?"void":modout, mark);
273 <                printf("%d %s%s\n0\n0\n", n+1, objname, buf);
272 >                printf("\n%s %s %s\n",
273 >                                m->modout==NULL?"void":m->modout,
274 >                                m->usetype==USE_INSTANCE?"instance":"mesh",
275 >                                mark);
276 >                printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
277          }
278          return;
279   badxf:
# Line 228 | Line 283 | badxf:
283   }
284  
285  
286 < edgecmp(e1, e2)                 /* compare two edges, descending order */
287 < EDGE    *e1, *e2;
286 > int
287 > edgecmp(                        /* compare two edges, descending order */
288 >        const void *e1,
289 >        const void *e2
290 > )
291   {
292 <        if (e1->len2 > e2->len2)
292 >        if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2)
293                  return(-1);
294 <        if (e1->len2 < e2->len2)
294 >        if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2)
295                  return(1);
296          return(0);
297   }
298  
299  
300   int
301 < buildxf(xf, fin)                /* build transform for marker */
302 < char    *xf;
303 < FILE    *fin;
301 > buildxf(                /* build transform for marker */
302 >        register char   *xf,
303 >        double  markscale,
304 >        FILE    *fin
305 > )
306   {
307          static FVECT    vlist[MAXVERT];
308          static EDGE     elist[MAXVERT];
# Line 307 | Line 367 | FILE   *fin;
367          if (markscale > 0.0) {          /* add scale factor */
368                  sprintf(xf, " -s %f", xlen*markscale);
369                  n += 2;
370 <                xf += strlen(xf);
370 >                while (*xf) ++xf;
371          }
372                                          /* add rotation */
373          n += addrot(xf, xvec, yvec, zvec);
374 <        xf += strlen(xf);
374 >        while (*xf) ++xf;
375                                          /* add translation */
376          n += 4;
377          sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
# Line 320 | Line 380 | FILE   *fin;
380   }
381  
382  
383 < addrot(xf, xp, yp, zp)          /* compute rotation (x,y,z) => (xp,yp,zp) */
384 < char    *xf;
385 < FVECT   xp, yp, zp;
383 > int
384 > addrot(         /* compute rotation (x,y,z) => (xp,yp,zp) */
385 >        register char   *xf,
386 >        FVECT xp,
387 >        FVECT yp,
388 >        FVECT zp
389 > )
390   {
391          int     n;
392          double  theta;
# Line 331 | Line 395 | FVECT  xp, yp, zp;
395          theta = atan2(yp[2], zp[2]);
396          if (!FEQ(theta,0.0)) {
397                  sprintf(xf, " -rx %f", theta*(180./PI));
398 <                xf += strlen(xf);
398 >                while (*xf) ++xf;
399                  n += 2;
400          }
401          theta = asin(-xp[2]);
402          if (!FEQ(theta,0.0)) {
403                  sprintf(xf, " -ry %f", theta*(180./PI));
404 <                xf += strlen(xf);
404 >                while (*xf) ++xf;
405                  n += 2;
406          }
407          theta = atan2(xp[1], xp[0]);
408          if (!FEQ(theta,0.0)) {
409                  sprintf(xf, " -rz %f", theta*(180./PI));
410 <                /* xf += strlen(xf); */
410 >                /* while (*xf) ++xf; */
411                  n += 2;
412          }
413          return(n);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines