1 |
|
#ifndef lint |
2 |
< |
static const char RCSid[] = "$Id"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
|
* Replace markers in Radiance scene description with objects or instances. |
13 |
|
#include <stdio.h> |
14 |
|
|
15 |
|
#include "platform.h" |
16 |
+ |
#include "rtio.h" |
17 |
+ |
#include "paths.h" |
18 |
|
#include "fvect.h" |
19 |
|
|
20 |
|
#ifdef M_PI |
23 |
|
#define PI 3.14159265358979323846 |
24 |
|
#endif |
25 |
|
|
24 |
– |
#define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7) |
25 |
– |
|
26 |
|
#define MAXVERT 6 /* maximum number of vertices for markers */ |
27 |
< |
#define MAXMARK 32 /* maximum number of markers */ |
27 |
> |
#define MAXMARK 128 /* maximum number of markers */ |
28 |
|
|
29 |
+ |
#define USE_XFORM 1 /* use !xform inline command */ |
30 |
+ |
#define USE_INSTANCE 2 /* use instance primitive */ |
31 |
+ |
#define USE_MESH 3 /* use mesh primitive */ |
32 |
+ |
|
33 |
|
typedef struct { |
34 |
|
short beg, end; /* beginning and ending vertex */ |
35 |
|
float len2; /* length squared */ |
40 |
|
double mscale; /* scale by this to get unit */ |
41 |
|
char *modin; /* input modifier indicating marker */ |
42 |
|
char *objname; /* output object file or octree */ |
43 |
< |
int doxform; /* true if xform, false if instance */ |
44 |
< |
} marker[MAXMARK]; /* array of markers */ |
43 |
> |
int usetype; /* one of USE_* above */ |
44 |
> |
} marker[MAXMARK+1]; /* array of markers */ |
45 |
|
int nmarkers = 0; /* number of markers */ |
46 |
|
|
47 |
|
int expand; /* expand commands? */ |
48 |
|
|
49 |
|
char *progname; |
50 |
|
|
51 |
+ |
static void convert(char *name, FILE *fin); |
52 |
+ |
static void cvcomm(char *fname, FILE *fin); |
53 |
+ |
static void cvobject(char *fname, FILE *fin); |
54 |
+ |
static void replace(char *fname, struct mrkr *m, char *mark, FILE *fin); |
55 |
+ |
static int edgecmp(const void *e1, const void *e2); |
56 |
+ |
static int buildxf(char *xf, double markscale, FILE *fin); |
57 |
+ |
static int addrot(char *xf, FVECT xp, FVECT yp, FVECT zp); |
58 |
|
|
59 |
< |
main(argc, argv) |
60 |
< |
int argc; |
61 |
< |
char *argv[]; |
59 |
> |
|
60 |
> |
int |
61 |
> |
main( |
62 |
> |
int argc, |
63 |
> |
char *argv[] |
64 |
> |
) |
65 |
|
{ |
66 |
|
FILE *fp; |
67 |
|
int i, j; |
72 |
|
do { |
73 |
|
switch (argv[i][1]) { |
74 |
|
case 'i': |
75 |
< |
marker[nmarkers].doxform = 0; |
75 |
> |
marker[nmarkers].usetype = USE_INSTANCE; |
76 |
|
marker[nmarkers].objname = argv[++i]; |
77 |
|
break; |
78 |
+ |
case 'I': |
79 |
+ |
marker[nmarkers].usetype = USE_MESH; |
80 |
+ |
marker[nmarkers].objname = argv[++i]; |
81 |
+ |
break; |
82 |
|
case 'x': |
83 |
< |
marker[nmarkers].doxform = 1; |
83 |
> |
marker[nmarkers].usetype = USE_XFORM; |
84 |
|
marker[nmarkers].objname = argv[++i]; |
85 |
|
break; |
86 |
|
case 'e': |
100 |
|
} while (argv[i][0] == '-'); |
101 |
|
if (marker[nmarkers].objname == NULL) |
102 |
|
goto userr; |
103 |
+ |
if (nmarkers >= MAXMARK) { |
104 |
+ |
fprintf(stderr, "%s: too many markers\n", progname); |
105 |
+ |
return 1; |
106 |
+ |
} |
107 |
|
marker[nmarkers++].modin = argv[i++]; |
86 |
– |
if (nmarkers >= MAXMARK) |
87 |
– |
break; |
108 |
|
marker[nmarkers].mscale = marker[nmarkers-1].mscale; |
109 |
|
} |
110 |
|
if (nmarkers == 0) |
127 |
|
convert(argv[i], fp); |
128 |
|
fclose(fp); |
129 |
|
} |
130 |
< |
exit(0); |
130 |
> |
return 0; |
131 |
|
userr: |
132 |
|
fprintf(stderr, |
133 |
< |
"Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n", |
133 |
> |
"Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n", |
134 |
|
progname); |
135 |
< |
exit(1); |
135 |
> |
return 1; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
< |
convert(name, fin) /* replace marks in a stream */ |
140 |
< |
char *name; |
141 |
< |
register FILE *fin; |
139 |
> |
void |
140 |
> |
convert( /* replace marks in a stream */ |
141 |
> |
char *name, |
142 |
> |
FILE *fin |
143 |
> |
) |
144 |
|
{ |
145 |
< |
register int c; |
145 |
> |
int c; |
146 |
|
|
147 |
|
while ((c = getc(fin)) != EOF) { |
148 |
|
if (isspace(c)) /* blank */ |
165 |
|
} |
166 |
|
|
167 |
|
|
168 |
< |
cvcomm(fname, fin) /* convert a command */ |
169 |
< |
char *fname; |
170 |
< |
FILE *fin; |
168 |
> |
void |
169 |
> |
cvcomm( /* convert a command */ |
170 |
> |
char *fname, |
171 |
> |
FILE *fin |
172 |
> |
) |
173 |
|
{ |
174 |
< |
FILE *pin, *popen(); |
175 |
< |
char buf[512], *fgetline(); |
174 |
> |
FILE *pin; |
175 |
> |
char buf[512]; |
176 |
|
|
177 |
|
fgetline(buf, sizeof(buf), fin); |
178 |
|
if (expand) { |
183 |
|
exit(1); |
184 |
|
} |
185 |
|
convert(buf, pin); |
186 |
< |
pclose(pin); |
186 |
> |
if (pclose(pin) != 0) |
187 |
> |
fprintf(stderr, |
188 |
> |
"%s: (%s): warning - bad status from \"%s\"\n", |
189 |
> |
progname, fname, buf); |
190 |
|
} else |
191 |
|
printf("\n%s\n", buf); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
< |
cvobject(fname, fin) /* convert an object */ |
196 |
< |
char *fname; |
197 |
< |
FILE *fin; |
195 |
> |
void |
196 |
> |
cvobject( /* convert an object */ |
197 |
> |
char *fname, |
198 |
> |
FILE *fin |
199 |
> |
) |
200 |
|
{ |
172 |
– |
extern char *fgetword(); |
201 |
|
char buf[128], typ[16], nam[128]; |
202 |
|
int i, n; |
203 |
< |
register int j; |
203 |
> |
int j; |
204 |
|
|
205 |
< |
if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3) |
205 |
> |
if (fgetword(buf, sizeof(buf), fin) == NULL || |
206 |
> |
fgetword(typ, sizeof(typ), fin) == NULL || |
207 |
> |
fgetword(nam, sizeof(nam), fin) == NULL) |
208 |
|
goto readerr; |
209 |
|
if (!strcmp(typ, "polygon")) |
210 |
|
for (j = 0; j < nmarkers; j++) |
212 |
|
replace(fname, &marker[j], nam, fin); |
213 |
|
return; |
214 |
|
} |
215 |
< |
printf("\n%s %s %s\n", buf, typ, nam); |
215 |
> |
putchar('\n'); fputword(buf, stdout); |
216 |
> |
printf(" %s ", typ); |
217 |
> |
fputword(nam, stdout); putchar('\n'); |
218 |
|
if (!strcmp(typ, "alias")) { /* alias special case */ |
219 |
< |
if (fscanf(fin, "%s", buf) != 1) |
219 |
> |
if (fgetword(buf, sizeof(buf), fin) == NULL) |
220 |
|
goto readerr; |
221 |
< |
printf("\t%s\n", buf); |
221 |
> |
putchar('\t'); fputword(buf, stdout); putchar('\n'); |
222 |
|
return; |
223 |
|
} |
224 |
|
for (i = 0; i < 3; i++) { /* pass along arguments */ |
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
< |
replace(fname, m, mark, fin) /* replace marker */ |
246 |
< |
char *fname; |
247 |
< |
register struct mrkr *m; |
248 |
< |
char *mark; |
249 |
< |
FILE *fin; |
245 |
> |
void |
246 |
> |
replace( /* replace marker */ |
247 |
> |
char *fname, |
248 |
> |
struct mrkr *m, |
249 |
> |
char *mark, |
250 |
> |
FILE *fin |
251 |
> |
) |
252 |
|
{ |
253 |
|
int n; |
254 |
|
char buf[256]; |
255 |
|
|
256 |
|
buf[0] = '\0'; /* bug fix thanks to schorsch */ |
257 |
< |
if (m->doxform) { |
258 |
< |
sprintf(buf, "xform -e -n %s", mark); |
257 |
> |
if (m->usetype == USE_XFORM) { |
258 |
> |
sprintf(buf, "xform -n %s", mark); |
259 |
|
if (m->modout != NULL) |
260 |
|
sprintf(buf+strlen(buf), " -m %s", m->modout); |
261 |
|
if (buildxf(buf+strlen(buf), m->mscale, fin) < 0) |
269 |
|
} else { |
270 |
|
if ((n = buildxf(buf, m->mscale, fin)) < 0) |
271 |
|
goto badxf; |
272 |
< |
printf("\n%s instance %s\n", |
273 |
< |
m->modout==NULL?"void":m->modout, mark); |
272 |
> |
printf("\n%s %s %s\n", |
273 |
> |
m->modout==NULL?"void":m->modout, |
274 |
> |
m->usetype==USE_INSTANCE?"instance":"mesh", |
275 |
> |
mark); |
276 |
|
printf("%d %s%s\n0\n0\n", n+1, m->objname, buf); |
277 |
|
} |
278 |
|
return; |
283 |
|
} |
284 |
|
|
285 |
|
|
286 |
< |
edgecmp(e1, e2) /* compare two edges, descending order */ |
287 |
< |
EDGE *e1, *e2; |
286 |
> |
int |
287 |
> |
edgecmp( /* compare two edges, descending order */ |
288 |
> |
const void *e1, |
289 |
> |
const void *e2 |
290 |
> |
) |
291 |
|
{ |
292 |
< |
if (e1->len2 > e2->len2) |
292 |
> |
if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2) |
293 |
|
return(-1); |
294 |
< |
if (e1->len2 < e2->len2) |
294 |
> |
if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2) |
295 |
|
return(1); |
296 |
|
return(0); |
297 |
|
} |
298 |
|
|
299 |
|
|
300 |
|
int |
301 |
< |
buildxf(xf, markscale, fin) /* build transform for marker */ |
302 |
< |
register char *xf; |
303 |
< |
double markscale; |
304 |
< |
FILE *fin; |
301 |
> |
buildxf( /* build transform for marker */ |
302 |
> |
char *xf, |
303 |
> |
double markscale, |
304 |
> |
FILE *fin |
305 |
> |
) |
306 |
|
{ |
307 |
|
static FVECT vlist[MAXVERT]; |
308 |
|
static EDGE elist[MAXVERT]; |
309 |
|
FVECT xvec, yvec, zvec; |
310 |
|
double xlen; |
311 |
|
int n; |
312 |
< |
register int i; |
312 |
> |
int i; |
313 |
|
/* |
314 |
|
* Read and sort vectors: longest is hypotenuse, |
315 |
|
* second longest is x' axis, |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
< |
addrot(xf, xp, yp, zp) /* compute rotation (x,y,z) => (xp,yp,zp) */ |
384 |
< |
register char *xf; |
385 |
< |
FVECT xp, yp, zp; |
383 |
> |
int |
384 |
> |
addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */ |
385 |
> |
char *xf, |
386 |
> |
FVECT xp, |
387 |
> |
FVECT yp, |
388 |
> |
FVECT zp |
389 |
> |
) |
390 |
|
{ |
391 |
|
int n; |
392 |
|
double theta; |
393 |
|
|
394 |
+ |
if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) { |
395 |
+ |
/* Special case for X' along Z-axis */ |
396 |
+ |
theta = -atan2(yp[0], yp[1]); |
397 |
+ |
sprintf(xf, " -ry %f -rz %f", |
398 |
+ |
xp[2] < 0.0 ? 90.0 : -90.0, |
399 |
+ |
theta*(180./PI)); |
400 |
+ |
return(4); |
401 |
+ |
} |
402 |
|
n = 0; |
403 |
|
theta = atan2(yp[2], zp[2]); |
404 |
< |
if (!FEQ(theta,0.0)) { |
404 |
> |
if (!FABSEQ(theta,0.0)) { |
405 |
|
sprintf(xf, " -rx %f", theta*(180./PI)); |
406 |
|
while (*xf) ++xf; |
407 |
|
n += 2; |
408 |
|
} |
409 |
< |
theta = asin(-xp[2]); |
410 |
< |
if (!FEQ(theta,0.0)) { |
409 |
> |
theta = Asin(-xp[2]); |
410 |
> |
if (!FABSEQ(theta,0.0)) { |
411 |
|
sprintf(xf, " -ry %f", theta*(180./PI)); |
412 |
|
while (*xf) ++xf; |
413 |
|
n += 2; |
414 |
|
} |
415 |
|
theta = atan2(xp[1], xp[0]); |
416 |
< |
if (!FEQ(theta,0.0)) { |
416 |
> |
if (!FABSEQ(theta,0.0)) { |
417 |
|
sprintf(xf, " -rz %f", theta*(180./PI)); |
418 |
|
/* while (*xf) ++xf; */ |
419 |
|
n += 2; |