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. |
14 |
|
|
15 |
|
#include "platform.h" |
16 |
|
#include "rtio.h" |
17 |
– |
#include "rtprocess.h" |
17 |
|
#include "fvect.h" |
18 |
|
|
19 |
|
#ifdef M_PI |
22 |
|
#define PI 3.14159265358979323846 |
23 |
|
#endif |
24 |
|
|
26 |
– |
#define FEQ(a,b) ((a)-(b) <= 1e-7 && (b)-(a) <= 1e-7) |
27 |
– |
|
25 |
|
#define MAXVERT 6 /* maximum number of vertices for markers */ |
26 |
< |
#define MAXMARK 32 /* maximum number of markers */ |
26 |
> |
#define MAXMARK 128 /* maximum number of markers */ |
27 |
|
|
28 |
+ |
#define USE_XFORM 1 /* use !xform inline command */ |
29 |
+ |
#define USE_INSTANCE 2 /* use instance primitive */ |
30 |
+ |
#define USE_MESH 3 /* use mesh primitive */ |
31 |
+ |
|
32 |
|
typedef struct { |
33 |
|
short beg, end; /* beginning and ending vertex */ |
34 |
|
float len2; /* length squared */ |
39 |
|
double mscale; /* scale by this to get unit */ |
40 |
|
char *modin; /* input modifier indicating marker */ |
41 |
|
char *objname; /* output object file or octree */ |
42 |
< |
int doxform; /* true if xform, false if instance */ |
43 |
< |
} marker[MAXMARK]; /* array of markers */ |
42 |
> |
int usetype; /* one of USE_* above */ |
43 |
> |
} marker[MAXMARK+1]; /* array of markers */ |
44 |
|
int nmarkers = 0; /* number of markers */ |
45 |
|
|
46 |
|
int expand; /* expand commands? */ |
47 |
|
|
47 |
– |
char *progname; |
48 |
– |
|
48 |
|
static void convert(char *name, FILE *fin); |
49 |
|
static void cvcomm(char *fname, FILE *fin); |
50 |
|
static void cvobject(char *fname, FILE *fin); |
63 |
|
FILE *fp; |
64 |
|
int i, j; |
65 |
|
|
66 |
< |
progname = argv[0]; |
66 |
> |
fixargv0(argv[0]); /* sets global progname */ |
67 |
|
i = 1; |
68 |
|
while (i < argc && argv[i][0] == '-') { |
69 |
|
do { |
70 |
|
switch (argv[i][1]) { |
71 |
|
case 'i': |
72 |
< |
marker[nmarkers].doxform = 0; |
72 |
> |
marker[nmarkers].usetype = USE_INSTANCE; |
73 |
|
marker[nmarkers].objname = argv[++i]; |
74 |
|
break; |
75 |
+ |
case 'I': |
76 |
+ |
marker[nmarkers].usetype = USE_MESH; |
77 |
+ |
marker[nmarkers].objname = argv[++i]; |
78 |
+ |
break; |
79 |
|
case 'x': |
80 |
< |
marker[nmarkers].doxform = 1; |
80 |
> |
marker[nmarkers].usetype = USE_XFORM; |
81 |
|
marker[nmarkers].objname = argv[++i]; |
82 |
|
break; |
83 |
|
case 'e': |
97 |
|
} while (argv[i][0] == '-'); |
98 |
|
if (marker[nmarkers].objname == NULL) |
99 |
|
goto userr; |
100 |
+ |
if (nmarkers >= MAXMARK) { |
101 |
+ |
fprintf(stderr, "%s: too many markers\n", progname); |
102 |
+ |
return 1; |
103 |
+ |
} |
104 |
|
marker[nmarkers++].modin = argv[i++]; |
98 |
– |
if (nmarkers >= MAXMARK) |
99 |
– |
break; |
105 |
|
marker[nmarkers].mscale = marker[nmarkers-1].mscale; |
106 |
|
} |
107 |
|
if (nmarkers == 0) |
127 |
|
return 0; |
128 |
|
userr: |
129 |
|
fprintf(stderr, |
130 |
< |
"Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree} modname .. [file ..]\n", |
130 |
> |
"Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n", |
131 |
|
progname); |
132 |
|
return 1; |
133 |
|
} |
136 |
|
void |
137 |
|
convert( /* replace marks in a stream */ |
138 |
|
char *name, |
139 |
< |
register FILE *fin |
139 |
> |
FILE *fin |
140 |
|
) |
141 |
|
{ |
142 |
< |
register int c; |
142 |
> |
int c; |
143 |
|
|
144 |
|
while ((c = getc(fin)) != EOF) { |
145 |
|
if (isspace(c)) /* blank */ |
169 |
|
) |
170 |
|
{ |
171 |
|
FILE *pin; |
172 |
< |
char buf[512], *fgetline(); |
172 |
> |
char buf[512]; |
173 |
|
|
174 |
|
fgetline(buf, sizeof(buf), fin); |
175 |
|
if (expand) { |
180 |
|
exit(1); |
181 |
|
} |
182 |
|
convert(buf, pin); |
183 |
< |
pclose(pin); |
183 |
> |
if (pclose(pin) != 0) |
184 |
> |
fprintf(stderr, |
185 |
> |
"%s: (%s): warning - bad status from \"%s\"\n", |
186 |
> |
progname, fname, buf); |
187 |
|
} else |
188 |
|
printf("\n%s\n", buf); |
189 |
|
} |
195 |
|
FILE *fin |
196 |
|
) |
197 |
|
{ |
190 |
– |
extern char *fgetword(); |
198 |
|
char buf[128], typ[16], nam[128]; |
199 |
|
int i, n; |
200 |
< |
register int j; |
200 |
> |
int j; |
201 |
|
|
202 |
< |
if (fscanf(fin, "%s %s %s", buf, typ, nam) != 3) |
202 |
> |
if (fgetword(buf, sizeof(buf), fin) == NULL || |
203 |
> |
fgetword(typ, sizeof(typ), fin) == NULL || |
204 |
> |
fgetword(nam, sizeof(nam), fin) == NULL) |
205 |
|
goto readerr; |
206 |
|
if (!strcmp(typ, "polygon")) |
207 |
|
for (j = 0; j < nmarkers; j++) |
209 |
|
replace(fname, &marker[j], nam, fin); |
210 |
|
return; |
211 |
|
} |
212 |
< |
printf("\n%s %s %s\n", buf, typ, nam); |
212 |
> |
putchar('\n'); fputword(buf, stdout); |
213 |
> |
printf(" %s ", typ); |
214 |
> |
fputword(nam, stdout); putchar('\n'); |
215 |
|
if (!strcmp(typ, "alias")) { /* alias special case */ |
216 |
< |
if (fscanf(fin, "%s", buf) != 1) |
216 |
> |
if (fgetword(buf, sizeof(buf), fin) == NULL) |
217 |
|
goto readerr; |
218 |
< |
printf("\t%s\n", buf); |
218 |
> |
putchar('\t'); fputword(buf, stdout); putchar('\n'); |
219 |
|
return; |
220 |
|
} |
221 |
|
for (i = 0; i < 3; i++) { /* pass along arguments */ |
242 |
|
void |
243 |
|
replace( /* replace marker */ |
244 |
|
char *fname, |
245 |
< |
register struct mrkr *m, |
245 |
> |
struct mrkr *m, |
246 |
|
char *mark, |
247 |
|
FILE *fin |
248 |
|
) |
251 |
|
char buf[256]; |
252 |
|
|
253 |
|
buf[0] = '\0'; /* bug fix thanks to schorsch */ |
254 |
< |
if (m->doxform) { |
255 |
< |
sprintf(buf, "xform -e -n %s", mark); |
254 |
> |
if (m->usetype == USE_XFORM) { |
255 |
> |
sprintf(buf, "xform -n %s", mark); |
256 |
|
if (m->modout != NULL) |
257 |
|
sprintf(buf+strlen(buf), " -m %s", m->modout); |
258 |
|
if (buildxf(buf+strlen(buf), m->mscale, fin) < 0) |
266 |
|
} else { |
267 |
|
if ((n = buildxf(buf, m->mscale, fin)) < 0) |
268 |
|
goto badxf; |
269 |
< |
printf("\n%s instance %s\n", |
270 |
< |
m->modout==NULL?"void":m->modout, mark); |
269 |
> |
printf("\n%s %s %s\n", |
270 |
> |
m->modout==NULL?"void":m->modout, |
271 |
> |
m->usetype==USE_INSTANCE?"instance":"mesh", |
272 |
> |
mark); |
273 |
|
printf("%d %s%s\n0\n0\n", n+1, m->objname, buf); |
274 |
|
} |
275 |
|
return; |
296 |
|
|
297 |
|
int |
298 |
|
buildxf( /* build transform for marker */ |
299 |
< |
register char *xf, |
299 |
> |
char *xf, |
300 |
|
double markscale, |
301 |
|
FILE *fin |
302 |
|
) |
306 |
|
FVECT xvec, yvec, zvec; |
307 |
|
double xlen; |
308 |
|
int n; |
309 |
< |
register int i; |
309 |
> |
int i; |
310 |
|
/* |
311 |
|
* Read and sort vectors: longest is hypotenuse, |
312 |
|
* second longest is x' axis, |
379 |
|
|
380 |
|
int |
381 |
|
addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */ |
382 |
< |
register char *xf, |
382 |
> |
char *xf, |
383 |
|
FVECT xp, |
384 |
|
FVECT yp, |
385 |
|
FVECT zp |
388 |
|
int n; |
389 |
|
double theta; |
390 |
|
|
391 |
+ |
if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) { |
392 |
+ |
/* Special case for X' along Z-axis */ |
393 |
+ |
theta = -atan2(yp[0], yp[1]); |
394 |
+ |
sprintf(xf, " -ry %f -rz %f", |
395 |
+ |
xp[2] < 0.0 ? 90.0 : -90.0, |
396 |
+ |
theta*(180./PI)); |
397 |
+ |
return(4); |
398 |
+ |
} |
399 |
|
n = 0; |
400 |
|
theta = atan2(yp[2], zp[2]); |
401 |
< |
if (!FEQ(theta,0.0)) { |
401 |
> |
if (!FABSEQ(theta,0.0)) { |
402 |
|
sprintf(xf, " -rx %f", theta*(180./PI)); |
403 |
|
while (*xf) ++xf; |
404 |
|
n += 2; |
405 |
|
} |
406 |
< |
theta = asin(-xp[2]); |
407 |
< |
if (!FEQ(theta,0.0)) { |
406 |
> |
theta = Asin(-xp[2]); |
407 |
> |
if (!FABSEQ(theta,0.0)) { |
408 |
|
sprintf(xf, " -ry %f", theta*(180./PI)); |
409 |
|
while (*xf) ++xf; |
410 |
|
n += 2; |
411 |
|
} |
412 |
|
theta = atan2(xp[1], xp[0]); |
413 |
< |
if (!FEQ(theta,0.0)) { |
413 |
> |
if (!FABSEQ(theta,0.0)) { |
414 |
|
sprintf(xf, " -rz %f", theta*(180./PI)); |
415 |
|
/* while (*xf) ++xf; */ |
416 |
|
n += 2; |