1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: replmarks.c,v 2.19 2025/04/22 04:45:25 greg Exp $"; |
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 "paths.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 MAXVERT 6 /* maximum number of vertices for 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 */ |
36 |
} EDGE; /* a marker edge */ |
37 |
|
38 |
struct mrkr { |
39 |
char *modout; /* output modifier */ |
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 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 |
|
60 |
int |
61 |
main( |
62 |
int argc, |
63 |
char *argv[] |
64 |
) |
65 |
{ |
66 |
FILE *fp; |
67 |
int i, j; |
68 |
|
69 |
progname = argv[0]; |
70 |
i = 1; |
71 |
while (i < argc && argv[i][0] == '-') { |
72 |
do { |
73 |
switch (argv[i][1]) { |
74 |
case 'i': |
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].usetype = USE_XFORM; |
84 |
marker[nmarkers].objname = argv[++i]; |
85 |
break; |
86 |
case 'e': |
87 |
expand = 1; |
88 |
break; |
89 |
case 'm': |
90 |
marker[nmarkers].modout = argv[++i]; |
91 |
break; |
92 |
case 's': |
93 |
marker[nmarkers].mscale = atof(argv[++i]); |
94 |
break; |
95 |
default: |
96 |
goto userr; |
97 |
} |
98 |
if (++i >= argc) |
99 |
goto userr; |
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++]; |
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 |
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 |
void |
196 |
cvobject( /* convert an object */ |
197 |
char *fname, |
198 |
FILE *fin |
199 |
) |
200 |
{ |
201 |
extern char *fgetword(); |
202 |
char buf[128], typ[16], nam[128]; |
203 |
int i, n; |
204 |
register int j; |
205 |
|
206 |
if (fgetword(buf, sizeof(buf), fin) == NULL || |
207 |
fgetword(typ, sizeof(typ), fin) == NULL || |
208 |
fgetword(nam, sizeof(nam), fin) == NULL) |
209 |
goto readerr; |
210 |
if (!strcmp(typ, "polygon")) |
211 |
for (j = 0; j < nmarkers; j++) |
212 |
if (!strcmp(buf, marker[j].modin)) { |
213 |
replace(fname, &marker[j], nam, fin); |
214 |
return; |
215 |
} |
216 |
putchar('\n'); fputword(buf, stdout); |
217 |
printf(" %s ", typ); |
218 |
fputword(nam, stdout); putchar('\n'); |
219 |
if (!strcmp(typ, "alias")) { /* alias special case */ |
220 |
if (fgetword(buf, sizeof(buf), fin) == NULL) |
221 |
goto readerr; |
222 |
putchar('\t'); fputword(buf, stdout); putchar('\n'); |
223 |
return; |
224 |
} |
225 |
for (i = 0; i < 3; i++) { /* pass along arguments */ |
226 |
if (fscanf(fin, "%d", &n) != 1) |
227 |
goto readerr; |
228 |
printf("%d", n); |
229 |
for (j = 0; j < n; j++) { |
230 |
if (fgetword(buf, sizeof(buf), fin) == NULL) |
231 |
goto readerr; |
232 |
if (j%3 == 0) |
233 |
putchar('\n'); |
234 |
putchar('\t'); |
235 |
fputword(buf, stdout); |
236 |
} |
237 |
putchar('\n'); |
238 |
} |
239 |
return; |
240 |
readerr: |
241 |
fprintf(stderr, "%s: (%s): read error for %s \"%s\"\n", |
242 |
progname, fname, typ, nam); |
243 |
} |
244 |
|
245 |
|
246 |
void |
247 |
replace( /* replace marker */ |
248 |
char *fname, |
249 |
register struct mrkr *m, |
250 |
char *mark, |
251 |
FILE *fin |
252 |
) |
253 |
{ |
254 |
int n; |
255 |
char buf[256]; |
256 |
|
257 |
buf[0] = '\0'; /* bug fix thanks to schorsch */ |
258 |
if (m->usetype == USE_XFORM) { |
259 |
sprintf(buf, "xform -n %s", mark); |
260 |
if (m->modout != NULL) |
261 |
sprintf(buf+strlen(buf), " -m %s", m->modout); |
262 |
if (buildxf(buf+strlen(buf), m->mscale, fin) < 0) |
263 |
goto badxf; |
264 |
sprintf(buf+strlen(buf), " %s", m->objname); |
265 |
if (expand) { |
266 |
fflush(stdout); |
267 |
system(buf); |
268 |
} else |
269 |
printf("\n!%s\n", buf); |
270 |
} else { |
271 |
if ((n = buildxf(buf, m->mscale, fin)) < 0) |
272 |
goto badxf; |
273 |
printf("\n%s %s %s\n", |
274 |
m->modout==NULL?"void":m->modout, |
275 |
m->usetype==USE_INSTANCE?"instance":"mesh", |
276 |
mark); |
277 |
printf("%d %s%s\n0\n0\n", n+1, m->objname, buf); |
278 |
} |
279 |
return; |
280 |
badxf: |
281 |
fprintf(stderr, "%s: (%s): bad arguments for marker \"%s\"\n", |
282 |
progname, fname, mark); |
283 |
exit(1); |
284 |
} |
285 |
|
286 |
|
287 |
int |
288 |
edgecmp( /* compare two edges, descending order */ |
289 |
const void *e1, |
290 |
const void *e2 |
291 |
) |
292 |
{ |
293 |
if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2) |
294 |
return(-1); |
295 |
if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2) |
296 |
return(1); |
297 |
return(0); |
298 |
} |
299 |
|
300 |
|
301 |
int |
302 |
buildxf( /* build transform for marker */ |
303 |
register char *xf, |
304 |
double markscale, |
305 |
FILE *fin |
306 |
) |
307 |
{ |
308 |
static FVECT vlist[MAXVERT]; |
309 |
static EDGE elist[MAXVERT]; |
310 |
FVECT xvec, yvec, zvec; |
311 |
double xlen; |
312 |
int n; |
313 |
register int i; |
314 |
/* |
315 |
* Read and sort vectors: longest is hypotenuse, |
316 |
* second longest is x' axis, |
317 |
* third longest is y' axis (approximate), |
318 |
* other vectors are ignored. |
319 |
* It is an error if the x' and y' axes do |
320 |
* not share a common vertex (their origin). |
321 |
*/ |
322 |
if (fscanf(fin, "%*d %*d %d", &n) != 1) |
323 |
return(-1); |
324 |
if (n%3 != 0) |
325 |
return(-1); |
326 |
n /= 3; |
327 |
if (n < 3 || n > MAXVERT) |
328 |
return(-1); |
329 |
/* sort edges in descending order */ |
330 |
for (i = 0; i < n; i++) { |
331 |
if (fscanf(fin, "%lf %lf %lf", &vlist[i][0], |
332 |
&vlist[i][1], &vlist[i][2]) != 3) |
333 |
return(-1); |
334 |
if (i) { |
335 |
elist[i].beg = i-1; |
336 |
elist[i].end = i; |
337 |
elist[i].len2 = dist2(vlist[i-1],vlist[i]); |
338 |
} |
339 |
} |
340 |
elist[0].beg = n-1; |
341 |
elist[0].end = 0; |
342 |
elist[0].len2 = dist2(vlist[n-1],vlist[0]); |
343 |
qsort(elist, n, sizeof(EDGE), edgecmp); |
344 |
/* find x' and y' */ |
345 |
if (elist[1].end == elist[2].beg || elist[1].end == elist[2].end) { |
346 |
i = elist[1].beg; |
347 |
elist[1].beg = elist[1].end; |
348 |
elist[1].end = i; |
349 |
} |
350 |
if (elist[2].end == elist[1].beg) { |
351 |
i = elist[2].beg; |
352 |
elist[2].beg = elist[2].end; |
353 |
elist[2].end = i; |
354 |
} |
355 |
if (elist[1].beg != elist[2].beg) |
356 |
return(-1); /* x' and y' not connected! */ |
357 |
for (i = 0; i < 3; i++) { |
358 |
xvec[i] = vlist[elist[1].end][i] - vlist[elist[1].beg][i]; |
359 |
yvec[i] = vlist[elist[2].end][i] - vlist[elist[2].beg][i]; |
360 |
} |
361 |
if ((xlen = normalize(xvec)) == 0.0) |
362 |
return(-1); |
363 |
fcross(zvec, xvec, yvec); |
364 |
if (normalize(zvec) == 0.0) |
365 |
return(-1); |
366 |
fcross(yvec, zvec, xvec); |
367 |
n = 0; /* start transformation... */ |
368 |
if (markscale > 0.0) { /* add scale factor */ |
369 |
sprintf(xf, " -s %f", xlen*markscale); |
370 |
n += 2; |
371 |
while (*xf) ++xf; |
372 |
} |
373 |
/* add rotation */ |
374 |
n += addrot(xf, xvec, yvec, zvec); |
375 |
while (*xf) ++xf; |
376 |
/* add translation */ |
377 |
n += 4; |
378 |
sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0], |
379 |
vlist[elist[1].beg][1], vlist[elist[1].beg][2]); |
380 |
return(n); /* all done */ |
381 |
} |
382 |
|
383 |
|
384 |
int |
385 |
addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */ |
386 |
register char *xf, |
387 |
FVECT xp, |
388 |
FVECT yp, |
389 |
FVECT zp |
390 |
) |
391 |
{ |
392 |
int n; |
393 |
double theta; |
394 |
|
395 |
if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) { |
396 |
/* Special case for X' along Z-axis */ |
397 |
theta = -atan2(yp[0], yp[1]); |
398 |
sprintf(xf, " -ry %f -rz %f", |
399 |
xp[2] < 0.0 ? 90.0 : -90.0, |
400 |
theta*(180./PI)); |
401 |
return(4); |
402 |
} |
403 |
n = 0; |
404 |
theta = atan2(yp[2], zp[2]); |
405 |
if (!FABSEQ(theta,0.0)) { |
406 |
sprintf(xf, " -rx %f", theta*(180./PI)); |
407 |
while (*xf) ++xf; |
408 |
n += 2; |
409 |
} |
410 |
theta = Asin(-xp[2]); |
411 |
if (!FABSEQ(theta,0.0)) { |
412 |
sprintf(xf, " -ry %f", theta*(180./PI)); |
413 |
while (*xf) ++xf; |
414 |
n += 2; |
415 |
} |
416 |
theta = atan2(xp[1], xp[0]); |
417 |
if (!FABSEQ(theta,0.0)) { |
418 |
sprintf(xf, " -rz %f", theta*(180./PI)); |
419 |
/* while (*xf) ++xf; */ |
420 |
n += 2; |
421 |
} |
422 |
return(n); |
423 |
} |