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