ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/replmarks.c
Revision: 2.4
Committed: Fri Jan 16 10:48:23 1998 UTC (26 years, 3 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 2.3: +1 -1 lines
Log Message:
changed M_PI definition to ward off long double problems

File Contents

# Content
1 /* Copyright (c) 1992 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * Replace markers in Radiance scene description with objects or instances.
9 *
10 * Created: 17 Feb 1991 Greg Ward
11 */
12
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <math.h>
16
17 #include "fvect.h"
18
19 #ifdef M_PI
20 #define PI ((double)M_PI)
21 #else
22 #define PI 3.14159265358979323846
23 #endif
24
25 #define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7)
26
27 #define MAXVERT 6 /* maximum number of vertices for markers */
28 #define MAXMARK 32 /* maximum number of markers */
29
30 #ifdef DCL_ATOF
31 extern double atof();
32 #endif
33
34 typedef struct {
35 short beg, end; /* beginning and ending vertex */
36 float len2; /* length squared */
37 } EDGE; /* a marker edge */
38
39 struct mrkr {
40 char *modout; /* output modifier */
41 double mscale; /* scale by this to get unit */
42 char *modin; /* input modifier indicating marker */
43 char *objname; /* output object file or octree */
44 int doxform; /* true if xform, false if instance */
45 } marker[MAXMARK]; /* array of markers */
46 int nmarkers = 0; /* number of markers */
47
48 int expand; /* expand commands? */
49
50 char *progname;
51
52
53 main(argc, argv)
54 int argc;
55 char *argv[];
56 {
57 FILE *fp;
58 int i, j;
59
60 progname = argv[0];
61 i = 1;
62 while (i < argc && argv[i][0] == '-') {
63 do {
64 switch (argv[i][1]) {
65 case 'i':
66 marker[nmarkers].doxform = 0;
67 marker[nmarkers].objname = argv[++i];
68 break;
69 case 'x':
70 marker[nmarkers].doxform = 1;
71 marker[nmarkers].objname = argv[++i];
72 break;
73 case 'e':
74 expand = 1;
75 break;
76 case 'm':
77 marker[nmarkers].modout = argv[++i];
78 break;
79 case 's':
80 marker[nmarkers].mscale = atof(argv[++i]);
81 break;
82 default:
83 goto userr;
84 }
85 if (++i >= argc)
86 goto userr;
87 } while (argv[i][0] == '-');
88 if (marker[nmarkers].objname == NULL)
89 goto userr;
90 marker[nmarkers++].modin = argv[i++];
91 if (nmarkers >= MAXMARK)
92 break;
93 marker[nmarkers].mscale = marker[nmarkers-1].mscale;
94 }
95 if (nmarkers == 0)
96 goto userr;
97 /* simple header */
98 putchar('#');
99 for (j = 0; j < i; j++) {
100 putchar(' ');
101 fputs(argv[j], stdout);
102 }
103 putchar('\n');
104 if (i == argc)
105 convert("<stdin>", stdin);
106 else
107 for ( ; i < argc; i++) {
108 if ((fp = fopen(argv[i], "r")) == NULL) {
109 perror(argv[i]);
110 exit(1);
111 }
112 convert(argv[i], fp);
113 fclose(fp);
114 }
115 exit(0);
116 userr:
117 fprintf(stderr,
118 "Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n",
119 progname);
120 exit(1);
121 }
122
123
124 convert(name, fin) /* replace marks in a stream */
125 char *name;
126 register FILE *fin;
127 {
128 register int c;
129
130 while ((c = getc(fin)) != EOF) {
131 if (isspace(c)) /* blank */
132 continue;
133 if (c == '#') { /* comment */
134 putchar(c);
135 do {
136 if ((c = getc(fin)) == EOF)
137 return;
138 putchar(c);
139 } while (c != '\n');
140 } else if (c == '!') { /* command */
141 ungetc(c, fin);
142 cvcomm(name, fin);
143 } else { /* object */
144 ungetc(c, fin);
145 cvobject(name, fin);
146 }
147 }
148 }
149
150
151 cvcomm(fname, fin) /* convert a command */
152 char *fname;
153 FILE *fin;
154 {
155 FILE *pin, *popen();
156 char buf[512], *fgetline();
157
158 fgetline(buf, sizeof(buf), fin);
159 if (expand) {
160 if ((pin = popen(buf+1, "r")) == NULL) {
161 fprintf(stderr,
162 "%s: (%s): cannot execute \"%s\"\n",
163 progname, fname, buf);
164 exit(1);
165 }
166 convert(buf, pin);
167 pclose(pin);
168 } else
169 printf("\n%s\n", buf);
170 }
171
172
173 cvobject(fname, fin) /* convert an object */
174 char *fname;
175 FILE *fin;
176 {
177 char buf[128], typ[16], nam[128];
178 int i, n;
179 register int j;
180
181 if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3)
182 goto readerr;
183 if (!strcmp(typ, "polygon"))
184 for (j = 0; j < nmarkers; j++)
185 if (!strcmp(buf, marker[j].modin)) {
186 replace(fname, &marker[j], nam, fin);
187 return;
188 }
189 printf("\n%s %s %s\n", buf, typ, nam);
190 if (!strcmp(typ, "alias")) { /* alias special case */
191 if (fscanf(fin, "%s", buf) != 1)
192 goto readerr;
193 printf("\t%s\n", buf);
194 return;
195 }
196 for (i = 0; i < 3; i++) { /* pass along arguments */
197 if (fscanf(fin, "%d", &n) != 1)
198 goto readerr;
199 printf("%d", n);
200 for (j = 0; j < n; j++) {
201 if (fscanf(fin, "%s", buf) != 1)
202 goto readerr;
203 if (j%3 == 0)
204 putchar('\n');
205 printf("\t%s", buf);
206 }
207 putchar('\n');
208 }
209 return;
210 readerr:
211 fprintf(stderr, "%s: (%s): read error for %s \"%s\"\n",
212 progname, fname, typ, nam);
213 }
214
215
216 replace(fname, m, mark, fin) /* replace marker */
217 char *fname;
218 register struct mrkr *m;
219 char *mark;
220 FILE *fin;
221 {
222 int n;
223 char buf[256];
224
225 if (m->doxform) {
226 sprintf(buf, "xform -e -n %s", mark);
227 if (m->modout != NULL)
228 sprintf(buf+strlen(buf), " -m %s", m->modout);
229 if (buildxf(buf+strlen(buf), m->mscale, fin) < 0)
230 goto badxf;
231 sprintf(buf+strlen(buf), " %s", m->objname);
232 if (expand) {
233 fflush(stdout);
234 system(buf);
235 } else
236 printf("\n!%s\n", buf);
237 } else {
238 if ((n = buildxf(buf, m->mscale, fin)) < 0)
239 goto badxf;
240 printf("\n%s instance %s\n",
241 m->modout==NULL?"void":m->modout, mark);
242 printf("%d %s%s\n0\n0\n", n+1, m->objname, buf);
243 }
244 return;
245 badxf:
246 fprintf(stderr, "%s: (%s): bad arguments for marker \"%s\"\n",
247 progname, fname, mark);
248 exit(1);
249 }
250
251
252 edgecmp(e1, e2) /* compare two edges, descending order */
253 EDGE *e1, *e2;
254 {
255 if (e1->len2 > e2->len2)
256 return(-1);
257 if (e1->len2 < e2->len2)
258 return(1);
259 return(0);
260 }
261
262
263 int
264 buildxf(xf, markscale, fin) /* build transform for marker */
265 char *xf;
266 double markscale;
267 FILE *fin;
268 {
269 static FVECT vlist[MAXVERT];
270 static EDGE elist[MAXVERT];
271 FVECT xvec, yvec, zvec;
272 double xlen;
273 int n;
274 register int i;
275 /*
276 * Read and sort vectors: longest is hypotenuse,
277 * second longest is x' axis,
278 * third longest is y' axis (approximate),
279 * other vectors are ignored.
280 * It is an error if the x' and y' axes do
281 * not share a common vertex (their origin).
282 */
283 if (fscanf(fin, "%*d %*d %d", &n) != 1)
284 return(-1);
285 if (n%3 != 0)
286 return(-1);
287 n /= 3;
288 if (n < 3 || n > MAXVERT)
289 return(-1);
290 /* sort edges in descending order */
291 for (i = 0; i < n; i++) {
292 if (fscanf(fin, "%lf %lf %lf", &vlist[i][0],
293 &vlist[i][1], &vlist[i][2]) != 3)
294 return(-1);
295 if (i) {
296 elist[i].beg = i-1;
297 elist[i].end = i;
298 elist[i].len2 = dist2(vlist[i-1],vlist[i]);
299 }
300 }
301 elist[0].beg = n-1;
302 elist[0].end = 0;
303 elist[0].len2 = dist2(vlist[n-1],vlist[0]);
304 qsort(elist, n, sizeof(EDGE), edgecmp);
305 /* find x' and y' */
306 if (elist[1].end == elist[2].beg || elist[1].end == elist[2].end) {
307 i = elist[1].beg;
308 elist[1].beg = elist[1].end;
309 elist[1].end = i;
310 }
311 if (elist[2].end == elist[1].beg) {
312 i = elist[2].beg;
313 elist[2].beg = elist[2].end;
314 elist[2].end = i;
315 }
316 if (elist[1].beg != elist[2].beg)
317 return(-1); /* x' and y' not connected! */
318 for (i = 0; i < 3; i++) {
319 xvec[i] = vlist[elist[1].end][i] - vlist[elist[1].beg][i];
320 yvec[i] = vlist[elist[2].end][i] - vlist[elist[2].beg][i];
321 }
322 if ((xlen = normalize(xvec)) == 0.0)
323 return(-1);
324 fcross(zvec, xvec, yvec);
325 if (normalize(zvec) == 0.0)
326 return(-1);
327 fcross(yvec, zvec, xvec);
328 n = 0; /* start transformation... */
329 if (markscale > 0.0) { /* add scale factor */
330 sprintf(xf, " -s %f", xlen*markscale);
331 n += 2;
332 xf += strlen(xf);
333 }
334 /* add rotation */
335 n += addrot(xf, xvec, yvec, zvec);
336 xf += strlen(xf);
337 /* add translation */
338 n += 4;
339 sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0],
340 vlist[elist[1].beg][1], vlist[elist[1].beg][2]);
341 return(n); /* all done */
342 }
343
344
345 addrot(xf, xp, yp, zp) /* compute rotation (x,y,z) => (xp,yp,zp) */
346 char *xf;
347 FVECT xp, yp, zp;
348 {
349 int n;
350 double theta;
351
352 n = 0;
353 theta = atan2(yp[2], zp[2]);
354 if (!FEQ(theta,0.0)) {
355 sprintf(xf, " -rx %f", theta*(180./PI));
356 xf += strlen(xf);
357 n += 2;
358 }
359 theta = asin(-xp[2]);
360 if (!FEQ(theta,0.0)) {
361 sprintf(xf, " -ry %f", theta*(180./PI));
362 xf += strlen(xf);
363 n += 2;
364 }
365 theta = atan2(xp[1], xp[0]);
366 if (!FEQ(theta,0.0)) {
367 sprintf(xf, " -rz %f", theta*(180./PI));
368 /* xf += strlen(xf); */
369 n += 2;
370 }
371 return(n);
372 }