1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
2.19 |
static const char RCSid[] = "$Id: replmarks.c,v 2.18 2021/02/12 00:53:56 greg Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Replace markers in Radiance scene description with objects or instances. |
6 |
|
|
* |
7 |
|
|
* Created: 17 Feb 1991 Greg Ward |
8 |
|
|
*/ |
9 |
|
|
|
10 |
greg |
2.5 |
#include <stdlib.h> |
11 |
greg |
1.1 |
#include <ctype.h> |
12 |
|
|
#include <math.h> |
13 |
schorsch |
2.7 |
#include <stdio.h> |
14 |
greg |
1.1 |
|
15 |
schorsch |
2.7 |
#include "platform.h" |
16 |
schorsch |
2.9 |
#include "rtio.h" |
17 |
greg |
2.17 |
#include "paths.h" |
18 |
greg |
1.1 |
#include "fvect.h" |
19 |
|
|
|
20 |
|
|
#ifdef M_PI |
21 |
gregl |
2.4 |
#define PI ((double)M_PI) |
22 |
greg |
1.1 |
#else |
23 |
|
|
#define PI 3.14159265358979323846 |
24 |
|
|
#endif |
25 |
|
|
|
26 |
|
|
#define MAXVERT 6 /* maximum number of vertices for markers */ |
27 |
greg |
2.13 |
#define MAXMARK 128 /* maximum number of markers */ |
28 |
greg |
1.1 |
|
29 |
greg |
2.11 |
#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 |
greg |
1.1 |
typedef struct { |
34 |
|
|
short beg, end; /* beginning and ending vertex */ |
35 |
|
|
float len2; /* length squared */ |
36 |
|
|
} EDGE; /* a marker edge */ |
37 |
|
|
|
38 |
greg |
2.2 |
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 |
greg |
2.11 |
int usetype; /* one of USE_* above */ |
44 |
greg |
2.13 |
} marker[MAXMARK+1]; /* array of markers */ |
45 |
greg |
2.2 |
int nmarkers = 0; /* number of markers */ |
46 |
greg |
1.1 |
|
47 |
greg |
2.2 |
int expand; /* expand commands? */ |
48 |
greg |
1.1 |
|
49 |
|
|
char *progname; |
50 |
|
|
|
51 |
schorsch |
2.9 |
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 |
greg |
1.1 |
|
59 |
schorsch |
2.9 |
|
60 |
|
|
int |
61 |
|
|
main( |
62 |
|
|
int argc, |
63 |
|
|
char *argv[] |
64 |
|
|
) |
65 |
greg |
1.1 |
{ |
66 |
|
|
FILE *fp; |
67 |
|
|
int i, j; |
68 |
|
|
|
69 |
|
|
progname = argv[0]; |
70 |
greg |
2.2 |
i = 1; |
71 |
|
|
while (i < argc && argv[i][0] == '-') { |
72 |
|
|
do { |
73 |
|
|
switch (argv[i][1]) { |
74 |
|
|
case 'i': |
75 |
greg |
2.11 |
marker[nmarkers].usetype = USE_INSTANCE; |
76 |
|
|
marker[nmarkers].objname = argv[++i]; |
77 |
|
|
break; |
78 |
|
|
case 'I': |
79 |
|
|
marker[nmarkers].usetype = USE_MESH; |
80 |
greg |
2.2 |
marker[nmarkers].objname = argv[++i]; |
81 |
|
|
break; |
82 |
|
|
case 'x': |
83 |
greg |
2.11 |
marker[nmarkers].usetype = USE_XFORM; |
84 |
greg |
2.2 |
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 |
greg |
2.13 |
if (nmarkers >= MAXMARK) { |
104 |
|
|
fprintf(stderr, "%s: too many markers\n", progname); |
105 |
|
|
return 1; |
106 |
|
|
} |
107 |
greg |
2.2 |
marker[nmarkers++].modin = argv[i++]; |
108 |
|
|
marker[nmarkers].mscale = marker[nmarkers-1].mscale; |
109 |
|
|
} |
110 |
|
|
if (nmarkers == 0) |
111 |
greg |
1.1 |
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 |
schorsch |
2.9 |
return 0; |
131 |
greg |
1.1 |
userr: |
132 |
|
|
fprintf(stderr, |
133 |
greg |
2.11 |
"Usage: %s [-e][-s size][-m modout] {-x objfile|-i octree|-I mesh} modname .. [file ..]\n", |
134 |
greg |
1.1 |
progname); |
135 |
schorsch |
2.9 |
return 1; |
136 |
greg |
1.1 |
} |
137 |
|
|
|
138 |
|
|
|
139 |
schorsch |
2.9 |
void |
140 |
|
|
convert( /* replace marks in a stream */ |
141 |
|
|
char *name, |
142 |
|
|
register FILE *fin |
143 |
|
|
) |
144 |
greg |
1.1 |
{ |
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 |
schorsch |
2.9 |
void |
169 |
|
|
cvcomm( /* convert a command */ |
170 |
|
|
char *fname, |
171 |
|
|
FILE *fin |
172 |
|
|
) |
173 |
greg |
1.1 |
{ |
174 |
schorsch |
2.8 |
FILE *pin; |
175 |
greg |
1.1 |
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 |
greg |
2.19 |
if (pclose(pin) != 0) { |
187 |
|
|
fprintf(stderr, |
188 |
|
|
"%s: (%s): bad status from \"%s\"\n", |
189 |
|
|
progname, fname, buf); |
190 |
|
|
exit(1); |
191 |
|
|
} |
192 |
greg |
1.1 |
} else |
193 |
|
|
printf("\n%s\n", buf); |
194 |
|
|
} |
195 |
|
|
|
196 |
|
|
|
197 |
schorsch |
2.9 |
void |
198 |
|
|
cvobject( /* convert an object */ |
199 |
|
|
char *fname, |
200 |
|
|
FILE *fin |
201 |
|
|
) |
202 |
greg |
1.1 |
{ |
203 |
greg |
2.6 |
extern char *fgetword(); |
204 |
greg |
1.1 |
char buf[128], typ[16], nam[128]; |
205 |
greg |
2.2 |
int i, n; |
206 |
|
|
register int j; |
207 |
greg |
1.1 |
|
208 |
greg |
2.14 |
if (fgetword(buf, sizeof(buf), fin) == NULL || |
209 |
|
|
fgetword(typ, sizeof(typ), fin) == NULL || |
210 |
|
|
fgetword(nam, sizeof(nam), fin) == NULL) |
211 |
greg |
1.1 |
goto readerr; |
212 |
greg |
2.2 |
if (!strcmp(typ, "polygon")) |
213 |
|
|
for (j = 0; j < nmarkers; j++) |
214 |
|
|
if (!strcmp(buf, marker[j].modin)) { |
215 |
|
|
replace(fname, &marker[j], nam, fin); |
216 |
|
|
return; |
217 |
|
|
} |
218 |
greg |
2.14 |
putchar('\n'); fputword(buf, stdout); |
219 |
|
|
printf(" %s ", typ); |
220 |
|
|
fputword(nam, stdout); putchar('\n'); |
221 |
greg |
1.1 |
if (!strcmp(typ, "alias")) { /* alias special case */ |
222 |
greg |
2.14 |
if (fgetword(buf, sizeof(buf), fin) == NULL) |
223 |
greg |
1.1 |
goto readerr; |
224 |
greg |
2.14 |
putchar('\t'); fputword(buf, stdout); putchar('\n'); |
225 |
greg |
1.1 |
return; |
226 |
|
|
} |
227 |
|
|
for (i = 0; i < 3; i++) { /* pass along arguments */ |
228 |
|
|
if (fscanf(fin, "%d", &n) != 1) |
229 |
|
|
goto readerr; |
230 |
|
|
printf("%d", n); |
231 |
|
|
for (j = 0; j < n; j++) { |
232 |
greg |
2.5 |
if (fgetword(buf, sizeof(buf), fin) == NULL) |
233 |
greg |
1.1 |
goto readerr; |
234 |
|
|
if (j%3 == 0) |
235 |
|
|
putchar('\n'); |
236 |
greg |
2.5 |
putchar('\t'); |
237 |
|
|
fputword(buf, stdout); |
238 |
greg |
1.1 |
} |
239 |
|
|
putchar('\n'); |
240 |
|
|
} |
241 |
|
|
return; |
242 |
|
|
readerr: |
243 |
|
|
fprintf(stderr, "%s: (%s): read error for %s \"%s\"\n", |
244 |
|
|
progname, fname, typ, nam); |
245 |
|
|
} |
246 |
|
|
|
247 |
|
|
|
248 |
schorsch |
2.9 |
void |
249 |
|
|
replace( /* replace marker */ |
250 |
|
|
char *fname, |
251 |
|
|
register struct mrkr *m, |
252 |
|
|
char *mark, |
253 |
|
|
FILE *fin |
254 |
|
|
) |
255 |
greg |
1.1 |
{ |
256 |
|
|
int n; |
257 |
|
|
char buf[256]; |
258 |
|
|
|
259 |
greg |
2.5 |
buf[0] = '\0'; /* bug fix thanks to schorsch */ |
260 |
greg |
2.11 |
if (m->usetype == USE_XFORM) { |
261 |
greg |
2.10 |
sprintf(buf, "xform -n %s", mark); |
262 |
greg |
2.2 |
if (m->modout != NULL) |
263 |
|
|
sprintf(buf+strlen(buf), " -m %s", m->modout); |
264 |
|
|
if (buildxf(buf+strlen(buf), m->mscale, fin) < 0) |
265 |
greg |
1.1 |
goto badxf; |
266 |
greg |
2.2 |
sprintf(buf+strlen(buf), " %s", m->objname); |
267 |
greg |
1.1 |
if (expand) { |
268 |
|
|
fflush(stdout); |
269 |
|
|
system(buf); |
270 |
|
|
} else |
271 |
|
|
printf("\n!%s\n", buf); |
272 |
|
|
} else { |
273 |
greg |
2.2 |
if ((n = buildxf(buf, m->mscale, fin)) < 0) |
274 |
greg |
1.1 |
goto badxf; |
275 |
greg |
2.11 |
printf("\n%s %s %s\n", |
276 |
|
|
m->modout==NULL?"void":m->modout, |
277 |
|
|
m->usetype==USE_INSTANCE?"instance":"mesh", |
278 |
|
|
mark); |
279 |
greg |
2.2 |
printf("%d %s%s\n0\n0\n", n+1, m->objname, buf); |
280 |
greg |
1.1 |
} |
281 |
|
|
return; |
282 |
|
|
badxf: |
283 |
|
|
fprintf(stderr, "%s: (%s): bad arguments for marker \"%s\"\n", |
284 |
|
|
progname, fname, mark); |
285 |
|
|
exit(1); |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
|
289 |
schorsch |
2.9 |
int |
290 |
|
|
edgecmp( /* compare two edges, descending order */ |
291 |
|
|
const void *e1, |
292 |
|
|
const void *e2 |
293 |
|
|
) |
294 |
greg |
1.1 |
{ |
295 |
schorsch |
2.9 |
if (((EDGE*)e1)->len2 > ((EDGE*)e2)->len2) |
296 |
greg |
1.1 |
return(-1); |
297 |
schorsch |
2.9 |
if (((EDGE*)e1)->len2 < ((EDGE*)e2)->len2) |
298 |
greg |
1.1 |
return(1); |
299 |
|
|
return(0); |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
|
303 |
|
|
int |
304 |
schorsch |
2.9 |
buildxf( /* build transform for marker */ |
305 |
|
|
register char *xf, |
306 |
|
|
double markscale, |
307 |
|
|
FILE *fin |
308 |
|
|
) |
309 |
greg |
1.1 |
{ |
310 |
|
|
static FVECT vlist[MAXVERT]; |
311 |
|
|
static EDGE elist[MAXVERT]; |
312 |
|
|
FVECT xvec, yvec, zvec; |
313 |
|
|
double xlen; |
314 |
|
|
int n; |
315 |
|
|
register int i; |
316 |
|
|
/* |
317 |
|
|
* Read and sort vectors: longest is hypotenuse, |
318 |
|
|
* second longest is x' axis, |
319 |
|
|
* third longest is y' axis (approximate), |
320 |
|
|
* other vectors are ignored. |
321 |
|
|
* It is an error if the x' and y' axes do |
322 |
|
|
* not share a common vertex (their origin). |
323 |
|
|
*/ |
324 |
|
|
if (fscanf(fin, "%*d %*d %d", &n) != 1) |
325 |
|
|
return(-1); |
326 |
|
|
if (n%3 != 0) |
327 |
|
|
return(-1); |
328 |
|
|
n /= 3; |
329 |
|
|
if (n < 3 || n > MAXVERT) |
330 |
|
|
return(-1); |
331 |
|
|
/* sort edges in descending order */ |
332 |
|
|
for (i = 0; i < n; i++) { |
333 |
|
|
if (fscanf(fin, "%lf %lf %lf", &vlist[i][0], |
334 |
|
|
&vlist[i][1], &vlist[i][2]) != 3) |
335 |
|
|
return(-1); |
336 |
|
|
if (i) { |
337 |
|
|
elist[i].beg = i-1; |
338 |
|
|
elist[i].end = i; |
339 |
|
|
elist[i].len2 = dist2(vlist[i-1],vlist[i]); |
340 |
|
|
} |
341 |
|
|
} |
342 |
|
|
elist[0].beg = n-1; |
343 |
|
|
elist[0].end = 0; |
344 |
|
|
elist[0].len2 = dist2(vlist[n-1],vlist[0]); |
345 |
|
|
qsort(elist, n, sizeof(EDGE), edgecmp); |
346 |
|
|
/* find x' and y' */ |
347 |
|
|
if (elist[1].end == elist[2].beg || elist[1].end == elist[2].end) { |
348 |
|
|
i = elist[1].beg; |
349 |
|
|
elist[1].beg = elist[1].end; |
350 |
|
|
elist[1].end = i; |
351 |
|
|
} |
352 |
|
|
if (elist[2].end == elist[1].beg) { |
353 |
|
|
i = elist[2].beg; |
354 |
|
|
elist[2].beg = elist[2].end; |
355 |
|
|
elist[2].end = i; |
356 |
|
|
} |
357 |
|
|
if (elist[1].beg != elist[2].beg) |
358 |
|
|
return(-1); /* x' and y' not connected! */ |
359 |
|
|
for (i = 0; i < 3; i++) { |
360 |
|
|
xvec[i] = vlist[elist[1].end][i] - vlist[elist[1].beg][i]; |
361 |
|
|
yvec[i] = vlist[elist[2].end][i] - vlist[elist[2].beg][i]; |
362 |
|
|
} |
363 |
|
|
if ((xlen = normalize(xvec)) == 0.0) |
364 |
|
|
return(-1); |
365 |
|
|
fcross(zvec, xvec, yvec); |
366 |
|
|
if (normalize(zvec) == 0.0) |
367 |
|
|
return(-1); |
368 |
|
|
fcross(yvec, zvec, xvec); |
369 |
|
|
n = 0; /* start transformation... */ |
370 |
|
|
if (markscale > 0.0) { /* add scale factor */ |
371 |
|
|
sprintf(xf, " -s %f", xlen*markscale); |
372 |
|
|
n += 2; |
373 |
greg |
2.5 |
while (*xf) ++xf; |
374 |
greg |
1.1 |
} |
375 |
|
|
/* add rotation */ |
376 |
|
|
n += addrot(xf, xvec, yvec, zvec); |
377 |
greg |
2.5 |
while (*xf) ++xf; |
378 |
greg |
1.1 |
/* add translation */ |
379 |
|
|
n += 4; |
380 |
|
|
sprintf(xf, " -t %f %f %f", vlist[elist[1].beg][0], |
381 |
|
|
vlist[elist[1].beg][1], vlist[elist[1].beg][2]); |
382 |
|
|
return(n); /* all done */ |
383 |
|
|
} |
384 |
|
|
|
385 |
|
|
|
386 |
schorsch |
2.9 |
int |
387 |
|
|
addrot( /* compute rotation (x,y,z) => (xp,yp,zp) */ |
388 |
|
|
register char *xf, |
389 |
|
|
FVECT xp, |
390 |
|
|
FVECT yp, |
391 |
|
|
FVECT zp |
392 |
|
|
) |
393 |
greg |
1.1 |
{ |
394 |
greg |
1.2 |
int n; |
395 |
|
|
double theta; |
396 |
greg |
1.1 |
|
397 |
greg |
2.15 |
if (yp[2]*yp[2] + zp[2]*zp[2] < 2.*FTINY*FTINY) { |
398 |
|
|
/* Special case for X' along Z-axis */ |
399 |
|
|
theta = -atan2(yp[0], yp[1]); |
400 |
|
|
sprintf(xf, " -ry %f -rz %f", |
401 |
|
|
xp[2] < 0.0 ? 90.0 : -90.0, |
402 |
|
|
theta*(180./PI)); |
403 |
|
|
return(4); |
404 |
|
|
} |
405 |
greg |
1.2 |
n = 0; |
406 |
|
|
theta = atan2(yp[2], zp[2]); |
407 |
greg |
2.18 |
if (!FABSEQ(theta,0.0)) { |
408 |
greg |
1.2 |
sprintf(xf, " -rx %f", theta*(180./PI)); |
409 |
greg |
2.5 |
while (*xf) ++xf; |
410 |
greg |
1.2 |
n += 2; |
411 |
|
|
} |
412 |
greg |
2.16 |
theta = Asin(-xp[2]); |
413 |
greg |
2.18 |
if (!FABSEQ(theta,0.0)) { |
414 |
greg |
1.2 |
sprintf(xf, " -ry %f", theta*(180./PI)); |
415 |
greg |
2.5 |
while (*xf) ++xf; |
416 |
greg |
1.2 |
n += 2; |
417 |
|
|
} |
418 |
|
|
theta = atan2(xp[1], xp[0]); |
419 |
greg |
2.18 |
if (!FABSEQ(theta,0.0)) { |
420 |
greg |
1.2 |
sprintf(xf, " -rz %f", theta*(180./PI)); |
421 |
greg |
2.5 |
/* while (*xf) ++xf; */ |
422 |
greg |
1.2 |
n += 2; |
423 |
|
|
} |
424 |
|
|
return(n); |
425 |
greg |
1.1 |
} |