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 "rtio.h" |
17 |
#include "rtprocess.h" |
18 |
#include "fvect.h" |
19 |
|
20 |
#ifdef 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 |
#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 |
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]; /* array of markers */ |
47 |
int nmarkers = 0; /* number of markers */ |
48 |
|
49 |
int expand; /* expand commands? */ |
50 |
|
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 |
|
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 |
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 |
marker[nmarkers++].modin = argv[i++]; |
106 |
if (nmarkers >= MAXMARK) |
107 |
break; |
108 |
marker[nmarkers].mscale = marker[nmarkers-1].mscale; |
109 |
} |
110 |
if (nmarkers == 0) |
111 |
goto userr; |
112 |
/* simple header */ |
113 |
putchar('#'); |
114 |
for (j = 0; j < i; j++) { |
115 |
putchar(' '); |
116 |
fputs(argv[j], stdout); |
117 |
} |
118 |
putchar('\n'); |
119 |
if (i == argc) |
120 |
convert("<stdin>", stdin); |
121 |
else |
122 |
for ( ; i < argc; i++) { |
123 |
if ((fp = fopen(argv[i], "r")) == NULL) { |
124 |
perror(argv[i]); |
125 |
exit(1); |
126 |
} |
127 |
convert(argv[i], fp); |
128 |
fclose(fp); |
129 |
} |
130 |
return 0; |
131 |
userr: |
132 |
fprintf(stderr, |
133 |
"Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n", |
134 |
progname); |
135 |
return 1; |
136 |
} |
137 |
|
138 |
|
139 |
void |
140 |
convert( /* replace marks in a stream */ |
141 |
char *name, |
142 |
register FILE *fin |
143 |
) |
144 |
{ |
145 |
register int c; |
146 |
|
147 |
while ((c = getc(fin)) != EOF) { |
148 |
if (isspace(c)) /* blank */ |
149 |
continue; |
150 |
if (c == '#') { /* comment */ |
151 |
putchar(c); |
152 |
do { |
153 |
if ((c = getc(fin)) == EOF) |
154 |
return; |
155 |
putchar(c); |
156 |
} while (c != '\n'); |
157 |
} else if (c == '!') { /* command */ |
158 |
ungetc(c, fin); |
159 |
cvcomm(name, fin); |
160 |
} else { /* object */ |
161 |
ungetc(c, fin); |
162 |
cvobject(name, fin); |
163 |
} |
164 |
} |
165 |
} |
166 |
|
167 |
|
168 |
void |
169 |
cvcomm( /* convert a command */ |
170 |
char *fname, |
171 |
FILE *fin |
172 |
) |
173 |
{ |
174 |
FILE *pin; |
175 |
char buf[512], *fgetline(); |
176 |
|
177 |
fgetline(buf, sizeof(buf), fin); |
178 |
if (expand) { |
179 |
if ((pin = popen(buf+1, "r")) == NULL) { |
180 |
fprintf(stderr, |
181 |
"%s: (%s): cannot execute \"%s\"\n", |
182 |
progname, fname, buf); |
183 |
exit(1); |
184 |
} |
185 |
convert(buf, pin); |
186 |
pclose(pin); |
187 |
} else |
188 |
printf("\n%s\n", buf); |
189 |
} |
190 |
|
191 |
|
192 |
void |
193 |
cvobject( /* convert an object */ |
194 |
char *fname, |
195 |
FILE *fin |
196 |
) |
197 |
{ |
198 |
extern char *fgetword(); |
199 |
char buf[128], typ[16], nam[128]; |
200 |
int i, n; |
201 |
register int j; |
202 |
|
203 |
if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3) |
204 |
goto readerr; |
205 |
if (!strcmp(typ, "polygon")) |
206 |
for (j = 0; j < nmarkers; j++) |
207 |
if (!strcmp(buf, marker[j].modin)) { |
208 |
replace(fname, &marker[j], nam, fin); |
209 |
return; |
210 |
} |
211 |
printf("\n%s %s %s\n", buf, typ, nam); |
212 |
if (!strcmp(typ, "alias")) { /* alias special case */ |
213 |
if (fscanf(fin, "%s", buf) != 1) |
214 |
goto readerr; |
215 |
printf("\t%s\n", buf); |
216 |
return; |
217 |
} |
218 |
for (i = 0; i < 3; i++) { /* pass along arguments */ |
219 |
if (fscanf(fin, "%d", &n) != 1) |
220 |
goto readerr; |
221 |
printf("%d", n); |
222 |
for (j = 0; j < n; j++) { |
223 |
if (fgetword(buf, sizeof(buf), fin) == NULL) |
224 |
goto readerr; |
225 |
if (j%3 == 0) |
226 |
putchar('\n'); |
227 |
putchar('\t'); |
228 |
fputword(buf, stdout); |
229 |
} |
230 |
putchar('\n'); |
231 |
} |
232 |
return; |
233 |
readerr: |
234 |
fprintf(stderr, "%s: (%s): read error for %s \"%s\"\n", |
235 |
progname, fname, typ, nam); |
236 |
} |
237 |
|
238 |
|
239 |
void |
240 |
replace( /* replace marker */ |
241 |
char *fname, |
242 |
register struct mrkr *m, |
243 |
char *mark, |
244 |
FILE *fin |
245 |
) |
246 |
{ |
247 |
int n; |
248 |
char buf[256]; |
249 |
|
250 |
buf[0] = '\0'; /* bug fix thanks to schorsch */ |
251 |
if (m->usetype == USE_XFORM) { |
252 |
sprintf(buf, "xform -n %s", mark); |
253 |
if (m->modout != NULL) |
254 |
sprintf(buf+strlen(buf), " -m %s", m->modout); |
255 |
if (buildxf(buf+strlen(buf), m->mscale, fin) < 0) |
256 |
goto badxf; |
257 |
sprintf(buf+strlen(buf), " %s", m->objname); |
258 |
if (expand) { |
259 |
fflush(stdout); |
260 |
system(buf); |
261 |
} else |
262 |
printf("\n!%s\n", buf); |
263 |
} else { |
264 |
if ((n = buildxf(buf, m->mscale, fin)) < 0) |
265 |
goto badxf; |
266 |
printf("\n%s %s %s\n", |
267 |
m->modout==NULL?"void":m->modout, |
268 |
m->usetype==USE_INSTANCE?"instance":"mesh", |
269 |
mark); |
270 |
printf("%d %s%s\n0\n0\n", n+1, m->objname, buf); |
271 |
} |
272 |
return; |
273 |
badxf: |
274 |
fprintf(stderr, "%s: (%s): bad arguments for marker \"%s\"\n", |
275 |
progname, fname, mark); |
276 |
exit(1); |
277 |
} |
278 |
|
279 |
|
280 |
int |
281 |
edgecmp( /* compare two edges, descending order */ |
282 |
const void *e1, |
283 |
const void *e2 |
284 |
) |
285 |
{ |
286 |
if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2) |
287 |
return(-1); |
288 |
if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2) |
289 |
return(1); |
290 |
return(0); |
291 |
} |
292 |
|
293 |
|
294 |
int |
295 |
buildxf( /* build transform for marker */ |
296 |
register char *xf, |
297 |
double markscale, |
298 |
FILE *fin |
299 |
) |
300 |
{ |
301 |
static FVECT vlist[MAXVERT]; |
302 |
static EDGE elist[MAXVERT]; |
303 |
FVECT xvec, yvec, zvec; |
304 |
double xlen; |
305 |
int n; |
306 |
register int i; |
307 |
/* |
308 |
* Read and sort vectors: longest is hypotenuse, |
309 |
* second longest is x' axis, |
310 |
* third longest is y' axis (approximate), |
311 |
* other vectors are ignored. |
312 |
* It is an error if the x' and y' axes do |
313 |
* not share a common vertex (their origin). |
314 |
*/ |
315 |
if (fscanf(fin, "%*d %*d %d", &n) != 1) |
316 |
return(-1); |
317 |
if (n%3 != 0) |
318 |
return(-1); |
319 |
n /= 3; |
320 |
if (n < 3 || n > MAXVERT) |
321 |
return(-1); |
322 |
/* sort edges in descending order */ |
323 |
for (i = 0; i < n; i++) { |
324 |
if (fscanf(fin, "%lf %lf %lf", &vlist[i][0], |
325 |
&vlist[i][1], &vlist[i][2]) != 3) |
326 |
return(-1); |
327 |
if (i) { |
328 |
elist[i].beg = i-1; |
329 |
elist[i].end = i; |
330 |
elist[i].len2 = dist2(vlist[i-1],vlist[i]); |
331 |
} |
332 |
} |
333 |
elist[0].beg = n-1; |
334 |
elist[0].end = 0; |
335 |
elist[0].len2 = dist2(vlist[n-1],vlist[0]); |
336 |
qsort(elist, n, sizeof(EDGE), edgecmp); |
337 |
/* find x' and y' */ |
338 |
if (elist[1].end == elist[2].beg || elist[1].end == elist[2].end) { |
339 |
i = elist[1].beg; |
340 |
elist[1].beg = elist[1].end; |
341 |
elist[1].end = i; |
342 |
} |
343 |
if (elist[2].end == elist[1].beg) { |
344 |
i = elist[2].beg; |
345 |
elist[2].beg = elist[2].end; |
346 |
elist[2].end = i; |
347 |
} |
348 |
if (elist[1].beg != elist[2].beg) |
349 |
return(-1); /* x' and y' not connected! */ |
350 |
for (i = 0; i < 3; i++) { |
351 |
xvec[i] = vlist[elist[1].end][i] - vlist[elist[1].beg][i]; |
352 |
yvec[i] = vlist[elist[2].end][i] - vlist[elist[2].beg][i]; |
353 |
} |
354 |
if ((xlen = normalize(xvec)) == 0.0) |
355 |
return(-1); |
356 |
fcross(zvec, xvec, yvec); |
357 |
if (normalize(zvec) == 0.0) |
358 |
return(-1); |
359 |
fcross(yvec, zvec, xvec); |
360 |
n = 0; /* start transformation... */ |
361 |
if (markscale > 0.0) { /* add scale factor */ |
362 |
sprintf(xf, " -s %f", xlen*markscale); |
363 |
n += 2; |
364 |
while (*xf) ++xf; |
365 |
} |
366 |
/* add rotation */ |
367 |
n += addrot(xf, xvec, yvec, zvec); |
368 |
while (*xf) ++xf; |
369 |
/* add translation */ |
370 |
n += 4; |
371 |
sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0], |
372 |
vlist[elist[1].beg][1], vlist[elist[1].beg][2]); |
373 |
return(n); /* all done */ |
374 |
} |
375 |
|
376 |
|
377 |
int |
378 |
addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */ |
379 |
register char *xf, |
380 |
FVECT xp, |
381 |
FVECT yp, |
382 |
FVECT zp |
383 |
) |
384 |
{ |
385 |
int n; |
386 |
double theta; |
387 |
|
388 |
n = 0; |
389 |
theta = atan2(yp[2], zp[2]); |
390 |
if (!FEQ(theta,0.0)) { |
391 |
sprintf(xf, " -rx %f", theta*(180./PI)); |
392 |
while (*xf) ++xf; |
393 |
n += 2; |
394 |
} |
395 |
theta = asin(-xp[2]); |
396 |
if (!FEQ(theta,0.0)) { |
397 |
sprintf(xf, " -ry %f", theta*(180./PI)); |
398 |
while (*xf) ++xf; |
399 |
n += 2; |
400 |
} |
401 |
theta = atan2(xp[1], xp[0]); |
402 |
if (!FEQ(theta,0.0)) { |
403 |
sprintf(xf, " -rz %f", theta*(180./PI)); |
404 |
/* while (*xf) ++xf; */ |
405 |
n += 2; |
406 |
} |
407 |
return(n); |
408 |
} |