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