ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/replmarks.c
Revision: 2.6
Committed: Mon Mar 10 17:26:26 2003 UTC (21 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.5: +2 -1 lines
Log Message:
Compile fixes for Linux

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id";
3 #endif
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>
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 ((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 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 int expand; /* expand commands? */
43
44 char *progname;
45
46
47 main(argc, argv)
48 int argc;
49 char *argv[];
50 {
51 FILE *fp;
52 int i, j;
53
54 progname = argv[0];
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 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('#');
93 for (j = 0; j < i; j++) {
94 putchar(' ');
95 fputs(argv[j], stdout);
96 }
97 putchar('\n');
98 if (i == argc)
99 convert("<stdin>", stdin);
100 else
101 for ( ; i < argc; i++) {
102 if ((fp = fopen(argv[i], "r")) == NULL) {
103 perror(argv[i]);
104 exit(1);
105 }
106 convert(argv[i], fp);
107 fclose(fp);
108 }
109 exit(0);
110 userr:
111 fprintf(stderr,
112 "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n",
113 progname);
114 exit(1);
115 }
116
117
118 convert(name, fin) /* replace marks in a stream */
119 char *name;
120 register FILE *fin;
121 {
122 register int c;
123
124 while ((c = getc(fin)) != EOF) {
125 if (isspace(c)) /* blank */
126 continue;
127 if (c == '#') { /* comment */
128 putchar(c);
129 do {
130 if ((c = getc(fin)) == EOF)
131 return;
132 putchar(c);
133 } while (c != '\n');
134 } else if (c == '!') { /* command */
135 ungetc(c, fin);
136 cvcomm(name, fin);
137 } else { /* object */
138 ungetc(c, fin);
139 cvobject(name, fin);
140 }
141 }
142 }
143
144
145 cvcomm(fname, fin) /* convert a command */
146 char *fname;
147 FILE *fin;
148 {
149 FILE *pin, *popen();
150 char buf[512], *fgetline();
151
152 fgetline(buf, sizeof(buf), fin);
153 if (expand) {
154 if ((pin = popen(buf+1, "r")) == NULL) {
155 fprintf(stderr,
156 "%s: (%s): cannot execute \"%s\"\n",
157 progname, fname, buf);
158 exit(1);
159 }
160 convert(buf, pin);
161 pclose(pin);
162 } else
163 printf("\n%s\n", buf);
164 }
165
166
167 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, n;
174 register int j;
175
176 if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
177 goto readerr;
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)
187 goto readerr;
188 printf("\t%s\n", buf);
189 return;
190 }
191 for (i = 0; i < 3; i++) { /* pass along arguments */
192 if (fscanf(fin, "%d", &n) != 1)
193 goto readerr;
194 printf("%d", n);
195 for (j = 0; j < n; j++) {
196 if (fgetword(buf, sizeof(buf), fin) == NULL)
197 goto readerr;
198 if (j%3 == 0)
199 putchar('\n');
200 putchar('\t');
201 fputword(buf, stdout);
202 }
203 putchar('\n');
204 }
205 return;
206 readerr:
207 fprintf(stderr, "%s: (%s): read error for %s \"%s\"\n",
208 progname, fname, typ, nam);
209 }
210
211
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 buf[0] = '\0'; /* bug fix thanks to schorsch */
222 if (m->doxform) {
223 sprintf(buf, "xform -e -n %s", mark);
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", 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, m->mscale, fin)) < 0)
236 goto badxf;
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:
243 fprintf(stderr, "%s: (%s): bad arguments for marker \"%s\"\n",
244 progname, fname, mark);
245 exit(1);
246 }
247
248
249 edgecmp(e1, e2) /* compare two edges, descending order */
250 EDGE *e1, *e2;
251 {
252 if (e1->len2 > e2->len2)
253 return(-1);
254 if (e1->len2 < e2->len2)
255 return(1);
256 return(0);
257 }
258
259
260 int
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];
267 static EDGE elist[MAXVERT];
268 FVECT xvec, yvec, zvec;
269 double xlen;
270 int n;
271 register int i;
272 /*
273 * Read and sort vectors: longest is hypotenuse,
274 * second longest is x' axis,
275 * third longest is y' axis (approximate),
276 * other vectors are ignored.
277 * It is an error if the x' and y' axes do
278 * not share a common vertex (their origin).
279 */
280 if (fscanf(fin, "%*d %*d %d", &n) != 1)
281 return(-1);
282 if (n%3 != 0)
283 return(-1);
284 n /= 3;
285 if (n < 3 || n > MAXVERT)
286 return(-1);
287 /* sort edges in descending order */
288 for (i = 0; i < n; i++) {
289 if (fscanf(fin, "%lf %lf %lf", &vlist[i][0],
290 &vlist[i][1], &vlist[i][2]) != 3)
291 return(-1);
292 if (i) {
293 elist[i].beg = i-1;
294 elist[i].end = i;
295 elist[i].len2 = dist2(vlist[i-1],vlist[i]);
296 }
297 }
298 elist[0].beg = n-1;
299 elist[0].end = 0;
300 elist[0].len2 = dist2(vlist[n-1],vlist[0]);
301 qsort(elist, n, sizeof(EDGE), edgecmp);
302 /* find x' and y' */
303 if (elist[1].end == elist[2].beg || elist[1].end == elist[2].end) {
304 i = elist[1].beg;
305 elist[1].beg = elist[1].end;
306 elist[1].end = i;
307 }
308 if (elist[2].end == elist[1].beg) {
309 i = elist[2].beg;
310 elist[2].beg = elist[2].end;
311 elist[2].end = i;
312 }
313 if (elist[1].beg != elist[2].beg)
314 return(-1); /* x' and y' not connected! */
315 for (i = 0; i < 3; i++) {
316 xvec[i] = vlist[elist[1].end][i] - vlist[elist[1].beg][i];
317 yvec[i] = vlist[elist[2].end][i] - vlist[elist[2].beg][i];
318 }
319 if ((xlen = normalize(xvec)) == 0.0)
320 return(-1);
321 fcross(zvec, xvec, yvec);
322 if (normalize(zvec) == 0.0)
323 return(-1);
324 fcross(yvec, zvec, xvec);
325 n = 0; /* start transformation... */
326 if (markscale > 0.0) { /* add scale factor */
327 sprintf(xf, " -s %f", xlen*markscale);
328 n += 2;
329 while (*xf) ++xf;
330 }
331 /* add rotation */
332 n += addrot(xf, xvec, yvec, zvec);
333 while (*xf) ++xf;
334 /* add translation */
335 n += 4;
336 sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
337 vlist[elist[1].beg][1], vlist[elist[1].beg][2]);
338 return(n); /* all done */
339 }
340
341
342 addrot(xf, xp, yp, zp) /* compute rotation (x,y,z) => (xp,yp,zp) */
343 register char *xf;
344 FVECT xp, yp, zp;
345 {
346 int n;
347 double theta;
348
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 }