1 |
/* Copyright (c) 1996 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* xform.c - program to transform object files. |
9 |
* Transformations must preserve aspect ratio. |
10 |
* |
11 |
* 10/19/85 |
12 |
* 11/6/86 Finally added error checking! |
13 |
*/ |
14 |
|
15 |
#include "standard.h" |
16 |
|
17 |
#include "paths.h" |
18 |
|
19 |
#include <ctype.h> |
20 |
|
21 |
#include "object.h" |
22 |
|
23 |
#include "otypes.h" |
24 |
|
25 |
int xac; /* global xform argument count */ |
26 |
char **xav; /* global xform argument pointer */ |
27 |
int xfa; /* start of xf arguments */ |
28 |
|
29 |
XF tot; /* total transformation */ |
30 |
int reverse; /* boolean true if scene mirrored */ |
31 |
|
32 |
int invert; /* boolean true to invert surfaces */ |
33 |
|
34 |
int expand; /* boolean true to expand commands */ |
35 |
|
36 |
char *newmod; /* new modifier for surfaces */ |
37 |
|
38 |
char *idprefix; /* prefix for object identifiers */ |
39 |
|
40 |
#define ALIAS NUMOTYPE /* put alias at end of array */ |
41 |
|
42 |
#define NUMTYPES (NUMOTYPE+1) /* total number of object types */ |
43 |
|
44 |
FUN ofun[NUMTYPES] = INIT_OTYPE; /* default types and actions */ |
45 |
|
46 |
short tinvers[NUMOTYPE]; /* inverse types for surfaces */ |
47 |
|
48 |
int nrept = 1; /* number of array repetitions */ |
49 |
|
50 |
int stdinused = 0; /* stdin has been used by -f option? */ |
51 |
|
52 |
extern char *malloc(), *fgets(), *fgetword(); |
53 |
|
54 |
char mainfn[MAXPATH]; /* main file name */ |
55 |
FILE *mainfp = NULL; /* main file pointer */ |
56 |
|
57 |
#define progname (xav[0]) |
58 |
|
59 |
|
60 |
main(argc, argv) /* get transform options and transform file */ |
61 |
int argc; |
62 |
char *argv[]; |
63 |
{ |
64 |
int mal_prefix = 0; |
65 |
char *fname; |
66 |
int a; |
67 |
/* check for argument list file */ |
68 |
for (a = 1; a < argc; a++) |
69 |
if (!strcmp(argv[a], "-f")) |
70 |
return(doargf(argc, argv, a)); |
71 |
/* check for regular array */ |
72 |
for (a = 1; a < argc; a++) |
73 |
if (!strcmp(argv[a], "-a")) |
74 |
return(doarray(argc, argv, a)); |
75 |
|
76 |
initotypes(); /* initialize */ |
77 |
invert = 0; |
78 |
expand = 1; |
79 |
newmod = NULL; |
80 |
idprefix = NULL; |
81 |
|
82 |
for (a = 1; a < argc; a++) { |
83 |
if (argv[a][0] == '-') |
84 |
switch (argv[a][1]) { |
85 |
case 'm': |
86 |
if (argv[a][2] | a+1 >= argc) |
87 |
break; |
88 |
a++; |
89 |
if (newmod == NULL) |
90 |
newmod = argv[a]; |
91 |
continue; |
92 |
case 'n': |
93 |
if (argv[a][2] | a+1 >= argc) |
94 |
break; |
95 |
a++; |
96 |
if (idprefix == NULL) |
97 |
idprefix = argv[a]; |
98 |
else { |
99 |
register char *newp; |
100 |
newp = (char *)malloc(strlen(idprefix)+ |
101 |
strlen(argv[a])+2); |
102 |
if (newp == NULL) |
103 |
exit(2); |
104 |
sprintf(newp, "%s.%s", |
105 |
idprefix, argv[a]); |
106 |
if (mal_prefix++) |
107 |
free((char *)idprefix); |
108 |
idprefix = newp; |
109 |
} |
110 |
continue; |
111 |
case 'c': |
112 |
if (argv[a][2]) |
113 |
break; |
114 |
expand = 0; |
115 |
continue; |
116 |
case 'e': |
117 |
if (argv[a][2]) |
118 |
break; |
119 |
expand = 1; |
120 |
continue; |
121 |
case 'I': |
122 |
if (argv[a][2]) |
123 |
break; |
124 |
invert = !invert; |
125 |
continue; |
126 |
} |
127 |
break; |
128 |
} |
129 |
|
130 |
xav = argv; |
131 |
xfa = a; |
132 |
|
133 |
a += xf(&tot, argc-a, argv+a); |
134 |
|
135 |
if (reverse = tot.sca < 0.0) |
136 |
tot.sca = -tot.sca; |
137 |
if (invert) |
138 |
reverse = !reverse; |
139 |
|
140 |
if (a < argc && argv[a][0] == '-') { |
141 |
fprintf(stderr, "%s: command line error at '%s'\n", |
142 |
argv[0], argv[a]); |
143 |
exit(1); |
144 |
} |
145 |
|
146 |
xac = a; |
147 |
/* simple header */ |
148 |
putchar('#'); |
149 |
for (a = 0; a < xac; a++) |
150 |
printf(" %s", xav[a]); |
151 |
putchar('\n'); |
152 |
/* transform input */ |
153 |
if (xac == argc) { |
154 |
if (stdinused) { |
155 |
fprintf(stderr, "%s: cannot use stdin more than once\n", |
156 |
argv[0]); |
157 |
exit(1); |
158 |
} |
159 |
openmain(NULL); |
160 |
xform(mainfn, mainfp); |
161 |
} else |
162 |
for (a = xac; a < argc; a++) { |
163 |
openmain(argv[a]); |
164 |
xform(mainfn, mainfp); |
165 |
} |
166 |
|
167 |
if (mal_prefix) |
168 |
free((char *)idprefix); |
169 |
return(0); |
170 |
} |
171 |
|
172 |
|
173 |
doargf(ac, av, fi) /* take argument list from file */ |
174 |
char **av; |
175 |
int ac, fi; |
176 |
{ |
177 |
char *newav[256], **avp; |
178 |
char argbuf[1024]; |
179 |
char newid[128]; |
180 |
char *oldid; |
181 |
register char *cp; |
182 |
FILE *argfp; |
183 |
int n, i, k, newac, err; |
184 |
|
185 |
if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) { |
186 |
fprintf(stderr, "%s: missing file for -f option\n", av[0]); |
187 |
exit(1); |
188 |
} |
189 |
if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') { |
190 |
if (stdinused++) { |
191 |
fprintf(stderr, "%s: cannot use stdin more than once\n", |
192 |
av[0]); |
193 |
exit(1); |
194 |
} |
195 |
argfp = stdin; |
196 |
n = 100; /* we just don't know! */ |
197 |
} else { |
198 |
if ((argfp = fopen(av[fi+1], "r")) == NULL) { |
199 |
fprintf(stderr, "%s: cannot open argument file \"%s\"\n", |
200 |
av[0], av[fi+1]); |
201 |
exit(1); |
202 |
} |
203 |
n = 0; /* count number of lines in file */ |
204 |
while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) |
205 |
n += argbuf[0] != '\n' & argbuf[0] != '#'; |
206 |
if (!n) { |
207 |
fprintf(stderr, "%s: empty argument file \"%s\"\n", |
208 |
av[0], av[fi+1]); |
209 |
exit(1); |
210 |
} |
211 |
nrept *= n; |
212 |
rewind(argfp); |
213 |
} |
214 |
err = 0; k = 0; /* read each arg list and call main */ |
215 |
while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) { |
216 |
if (argbuf[0] == '\n' | argbuf[0] == '#') |
217 |
continue; |
218 |
avp = newav+2; |
219 |
avp[0] = av[0]; |
220 |
for (i = 1; i < fi; i++) |
221 |
avp[i] = av[i]; |
222 |
newac = i; |
223 |
cp = argbuf; /* parse new words */ |
224 |
if (*cp == '!') cp++; |
225 |
if (!strncmp(cp, "xform ", 6)) cp += 6; |
226 |
for ( ; ; ) { |
227 |
while (isspace(*cp)) /* nullify spaces */ |
228 |
*cp++ = '\0'; |
229 |
if (!*cp) /* all done? */ |
230 |
break; |
231 |
avp[newac++] = cp; /* add argument to list */ |
232 |
while (*++cp && !isspace(*cp)) |
233 |
; |
234 |
} |
235 |
for (i = fi+2; i < ac; i++) |
236 |
avp[newac++] = av[i]; |
237 |
avp[newac] = NULL; |
238 |
oldid = NULL; |
239 |
for (i = 2; i < newac; i++) |
240 |
if (!strcmp(avp[i-1], "-n")) { |
241 |
oldid = avp[i]; |
242 |
avp[i] = newid; |
243 |
break; |
244 |
} |
245 |
if (oldid == NULL) { |
246 |
newav[0] = av[0]; |
247 |
newav[1] = "-n"; |
248 |
newav[2] = newid; |
249 |
avp = newav; |
250 |
newac += 2; |
251 |
} |
252 |
if (oldid == NULL) |
253 |
sprintf(newid, "i%d", k); |
254 |
else |
255 |
sprintf(newid, "%s.%d", oldid, k); |
256 |
err |= main(newac, avp); |
257 |
k++; |
258 |
} |
259 |
fclose(argfp); |
260 |
return(err); |
261 |
} |
262 |
|
263 |
|
264 |
doarray(ac, av, ai) /* make array */ |
265 |
char **av; |
266 |
int ac, ai; |
267 |
{ |
268 |
char *newav[256], **avp; |
269 |
char newid[128], repts[32]; |
270 |
char *oldid = NULL; |
271 |
int n, i, err; |
272 |
|
273 |
if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) { |
274 |
fprintf(stderr, "%s: missing count for -a option\n", av[0]); |
275 |
exit(1); |
276 |
} |
277 |
nrept *= n; |
278 |
avp = newav+2; |
279 |
avp[0] = av[0]; |
280 |
for (i = 1; i < ac; i++) |
281 |
if (!strcmp(av[i-1], "-n")) { |
282 |
oldid = av[i]; |
283 |
avp[i] = newid; |
284 |
} else |
285 |
avp[i] = av[i]; |
286 |
avp[ai] = "-i"; |
287 |
avp[ai+1] = repts; |
288 |
avp[i] = NULL; |
289 |
if (oldid == NULL) { |
290 |
newav[0] = av[0]; |
291 |
newav[1] = "-n"; |
292 |
newav[2] = newid; |
293 |
avp = newav; |
294 |
ac += 2; |
295 |
} |
296 |
err = 0; |
297 |
for (i = 0; i < n; i++) { |
298 |
if (oldid == NULL) |
299 |
sprintf(newid, "a%d", i); |
300 |
else |
301 |
sprintf(newid, "%s.%d", oldid, i); |
302 |
sprintf(repts, "%d", i); |
303 |
err |= main(ac, avp); |
304 |
} |
305 |
return(err); |
306 |
} |
307 |
|
308 |
|
309 |
xform(name, fin) /* transform stream by tot.xfm */ |
310 |
char *name; |
311 |
register FILE *fin; |
312 |
{ |
313 |
int nobjs = 0; |
314 |
register int c; |
315 |
|
316 |
while ((c = getc(fin)) != EOF) { |
317 |
if (isspace(c)) /* blank */ |
318 |
continue; |
319 |
if (c == '#') { /* comment */ |
320 |
putchar(c); |
321 |
do { |
322 |
if ((c = getc(fin)) == EOF) |
323 |
return; |
324 |
putchar(c); |
325 |
} while (c != '\n'); |
326 |
} else if (c == '!') { /* command */ |
327 |
ungetc(c, fin); |
328 |
xfcomm(name, fin); |
329 |
nobjs++; |
330 |
} else { /* object */ |
331 |
ungetc(c, fin); |
332 |
xfobject(name, fin); |
333 |
nobjs++; |
334 |
} |
335 |
} |
336 |
if (nobjs == 0) |
337 |
fprintf(stderr, "%s: (%s): warning - empty file\n", |
338 |
progname, name); |
339 |
} |
340 |
|
341 |
|
342 |
xfcomm(fname, fin) /* transform a command */ |
343 |
char *fname; |
344 |
FILE *fin; |
345 |
{ |
346 |
extern FILE *popen(); |
347 |
extern char *fgetline(); |
348 |
FILE *pin; |
349 |
char buf[512]; |
350 |
int i; |
351 |
|
352 |
fgetline(buf, sizeof(buf), fin); |
353 |
if (expand) { |
354 |
if ((pin = popen(buf+1, "r")) == NULL) { |
355 |
fprintf(stderr, "%s: (%s): cannot execute \"%s\"\n", |
356 |
progname, fname, buf); |
357 |
exit(1); |
358 |
} |
359 |
xform(buf, pin); |
360 |
pclose(pin); |
361 |
} else { |
362 |
printf("\n%s", buf); |
363 |
if (xac > 1) { |
364 |
printf(" | %s", xav[0]); |
365 |
for (i = 1; i < xac; i++) |
366 |
printf(" %s", xav[i]); |
367 |
} |
368 |
putchar('\n'); |
369 |
} |
370 |
} |
371 |
|
372 |
|
373 |
xfobject(fname, fin) /* transform an object */ |
374 |
char *fname; |
375 |
FILE *fin; |
376 |
{ |
377 |
extern char *strcpy(); |
378 |
char typ[16], nam[MAXSTR]; |
379 |
int fn; |
380 |
/* modifier and type */ |
381 |
strcpy(typ, "EOF"); |
382 |
fgetword(nam, sizeof(nam), fin); |
383 |
fgetword(typ, sizeof(typ), fin); |
384 |
if ((fn = otype(typ)) < 0) { |
385 |
fprintf(stderr, "%s: (%s): unknown object type \"%s\"\n", |
386 |
progname, fname, typ); |
387 |
exit(1); |
388 |
} |
389 |
if (ismodifier(fn)) |
390 |
printf("\n%s %s ", nam, typ); |
391 |
else |
392 |
printf("\n%s %s ", newmod != NULL ? newmod : nam, |
393 |
invert ? ofun[tinvers[fn]].funame : typ); |
394 |
/* object name */ |
395 |
fgetword(nam, sizeof(nam), fin); |
396 |
if (idprefix == NULL || ismodifier(fn)) |
397 |
printf("%s\n", nam); |
398 |
else |
399 |
printf("%s.%s\n", idprefix, nam); |
400 |
/* transform arguments */ |
401 |
if ((*ofun[fn].funp)(fin) < 0) { |
402 |
fprintf(stderr, "%s: (%s): bad %s \"%s\"\n", |
403 |
progname, fname, ofun[fn].funame, nam); |
404 |
exit(1); |
405 |
} |
406 |
} |
407 |
|
408 |
|
409 |
o_default(fin) /* pass on arguments unchanged */ |
410 |
FILE *fin; |
411 |
{ |
412 |
register int i; |
413 |
FUNARGS fa; |
414 |
|
415 |
if (readfargs(&fa, fin) != 1) |
416 |
return(-1); |
417 |
/* string arguments */ |
418 |
printf("%d", fa.nsargs); |
419 |
for (i = 0; i < fa.nsargs; i++) |
420 |
printf(" %s", fa.sarg[i]); |
421 |
printf("\n"); |
422 |
#ifdef IARGS |
423 |
/* integer arguments */ |
424 |
printf("%d", fa.niargs); |
425 |
for (i = 0; i < fa.niargs; i++) |
426 |
printf(" %d", fa.iarg[i]); |
427 |
printf("\n"); |
428 |
#else |
429 |
printf("0\n"); |
430 |
#endif |
431 |
/* float arguments */ |
432 |
printf("%d", fa.nfargs); |
433 |
for (i = 0; i < fa.nfargs; i++) |
434 |
printf(" %18.12g", fa.farg[i]); |
435 |
printf("\n"); |
436 |
freefargs(&fa); |
437 |
return(0); |
438 |
} |
439 |
|
440 |
|
441 |
addxform(fin) /* add xf arguments to strings */ |
442 |
FILE *fin; |
443 |
{ |
444 |
register int i; |
445 |
int resetarr = 0; |
446 |
FUNARGS fa; |
447 |
|
448 |
if (readfargs(&fa, fin) != 1) |
449 |
return(-1); |
450 |
/* string arguments */ |
451 |
if (xac > xfa && strcmp(xav[xfa], "-i")) |
452 |
resetarr = 2; |
453 |
printf("%d", fa.nsargs + resetarr + xac-xfa); |
454 |
for (i = 0; i < fa.nsargs; i++) |
455 |
printf(" %s", fa.sarg[i]); |
456 |
if (resetarr) |
457 |
printf(" -i 1"); |
458 |
for (i = xfa; i < xac; i++) /* add xf arguments */ |
459 |
printf(" %s", xav[i]); |
460 |
printf("\n"); |
461 |
#ifdef IARGS |
462 |
/* integer arguments */ |
463 |
printf("%d", fa.niargs); |
464 |
for (i = 0; i < fa.niargs; i++) |
465 |
printf(" %d", fa.iarg[i]); |
466 |
printf("\n"); |
467 |
#else |
468 |
printf("0\n"); |
469 |
#endif |
470 |
/* float arguments */ |
471 |
printf("%d", fa.nfargs); |
472 |
for (i = 0; i < fa.nfargs; i++) |
473 |
printf(" %18.12g", fa.farg[i]); |
474 |
printf("\n"); |
475 |
freefargs(&fa); |
476 |
return(0); |
477 |
} |
478 |
|
479 |
|
480 |
int |
481 |
otype(ofname) /* get object function number from its name */ |
482 |
register char *ofname; |
483 |
{ |
484 |
register int i; |
485 |
|
486 |
for (i = 0; i < NUMTYPES; i++) |
487 |
if (!strcmp(ofun[i].funame, ofname)) |
488 |
return(i); |
489 |
|
490 |
return(-1); /* not found */ |
491 |
} |
492 |
|
493 |
|
494 |
alias(fin) /* transfer alias */ |
495 |
FILE *fin; |
496 |
{ |
497 |
char aliasnm[MAXSTR]; |
498 |
|
499 |
if (fgetword(aliasnm, MAXSTR, fin) == NULL) |
500 |
return(-1); |
501 |
printf("\t%s\n", aliasnm); |
502 |
return(0); |
503 |
} |
504 |
|
505 |
|
506 |
m_glow(fin) /* transform arguments for proximity light */ |
507 |
FILE *fin; |
508 |
{ |
509 |
FUNARGS fa; |
510 |
|
511 |
if (readfargs(&fa, fin) != 1) |
512 |
return(-1); |
513 |
if (fa.nsargs != 0 || fa.nfargs != 4) |
514 |
return(-1); |
515 |
printf("0\n0\n4"); |
516 |
printf(" %18.12g %18.12g %18.12g", |
517 |
fa.farg[0], fa.farg[1], fa.farg[2]); |
518 |
printf(" %18.12g\n", fa.farg[3] * tot.sca); |
519 |
freefargs(&fa); |
520 |
return(0); |
521 |
} |
522 |
|
523 |
|
524 |
m_spot(fin) /* transform arguments for spotlight */ |
525 |
FILE *fin; |
526 |
{ |
527 |
FVECT v; |
528 |
FUNARGS fa; |
529 |
|
530 |
if (readfargs(&fa, fin) != 1) |
531 |
return(-1); |
532 |
if (fa.nsargs != 0 || fa.nfargs != 7) |
533 |
return(-1); |
534 |
printf("0\n0\n7"); |
535 |
printf(" %18.12g %18.12g %18.12g %18.12g\n", |
536 |
fa.farg[0], fa.farg[1], fa.farg[2], fa.farg[3]); |
537 |
multv3(v, fa.farg+4, tot.xfm); |
538 |
printf("\t%18.12g %18.12g %18.12g\n", v[0], v[1], v[2]); |
539 |
freefargs(&fa); |
540 |
return(0); |
541 |
} |
542 |
|
543 |
|
544 |
m_mist(fin) /* transform arguments for mist */ |
545 |
FILE *fin; |
546 |
{ |
547 |
FUNARGS fa; |
548 |
int i; |
549 |
|
550 |
if (readfargs(&fa, fin) != 1) |
551 |
return(-1); |
552 |
if (fa.nfargs > 7) |
553 |
return(-1); |
554 |
printf("%d", fa.nsargs); |
555 |
if (idprefix == NULL) |
556 |
for (i = 0; i < fa.nsargs; i++) |
557 |
printf(" %s", fa.sarg[i]); |
558 |
else |
559 |
for (i = 0; i < fa.nsargs; i++) { |
560 |
char sname[256], *sp; |
561 |
register char *cp1, *cp2 = sname; |
562 |
/* add idprefix */ |
563 |
for (sp = fa.sarg[i]; *sp; sp = cp1) { |
564 |
for (cp1 = idprefix; *cp1; ) |
565 |
*cp2++ = *cp1++; |
566 |
*cp2++ = '.'; |
567 |
for (cp1 = sp; *cp1 && |
568 |
(*cp2++ = *cp1++) != '>'; ) |
569 |
; |
570 |
} |
571 |
*cp2 = '\0'; |
572 |
printf(" %s", sname); |
573 |
} |
574 |
printf("\n0\n%d", fa.nfargs); |
575 |
if (fa.nfargs > 2) |
576 |
printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca, |
577 |
fa.farg[1]/tot.sca, fa.farg[2]/tot.sca); |
578 |
for (i = 3; i < fa.nfargs; i++) |
579 |
printf(" %12.6g", fa.farg[i]); |
580 |
printf("\n"); |
581 |
freefargs(&fa); |
582 |
return(0); |
583 |
} |
584 |
|
585 |
|
586 |
m_dielectric(fin) /* transform arguments for dielectric */ |
587 |
FILE *fin; |
588 |
{ |
589 |
FUNARGS fa; |
590 |
|
591 |
if (readfargs(&fa, fin) != 1) |
592 |
return(-1); |
593 |
if (fa.nsargs != 0 || fa.nfargs != 5) |
594 |
return(-1); |
595 |
printf("0\n0\n5"); |
596 |
printf(" %12.6g %12.6g %12.6g", |
597 |
pow(fa.farg[0], 1.0/tot.sca), |
598 |
pow(fa.farg[1], 1.0/tot.sca), |
599 |
pow(fa.farg[2], 1.0/tot.sca)); |
600 |
printf(" %12.6g %12.6g\n", fa.farg[3], fa.farg[4]); |
601 |
freefargs(&fa); |
602 |
return(0); |
603 |
} |
604 |
|
605 |
|
606 |
m_interface(fin) /* transform arguments for interface */ |
607 |
FILE *fin; |
608 |
{ |
609 |
FUNARGS fa; |
610 |
|
611 |
if (readfargs(&fa, fin) != 1) |
612 |
return(-1); |
613 |
if (fa.nsargs != 0 || fa.nfargs != 8) |
614 |
return(-1); |
615 |
printf("0\n0\n8\n"); |
616 |
printf("%12.6g %12.6g %12.6g", |
617 |
pow(fa.farg[0], 1.0/tot.sca), |
618 |
pow(fa.farg[1], 1.0/tot.sca), |
619 |
pow(fa.farg[2], 1.0/tot.sca)); |
620 |
printf(" %12.6g\n", fa.farg[3]); |
621 |
printf("%12.6g %12.6g %12.6g", |
622 |
pow(fa.farg[4], 1.0/tot.sca), |
623 |
pow(fa.farg[5], 1.0/tot.sca), |
624 |
pow(fa.farg[6], 1.0/tot.sca)); |
625 |
printf(" %12.6g\n", fa.farg[7]); |
626 |
freefargs(&fa); |
627 |
return(0); |
628 |
} |
629 |
|
630 |
|
631 |
text(fin) /* transform text arguments */ |
632 |
FILE *fin; |
633 |
{ |
634 |
int i; |
635 |
FVECT v; |
636 |
FUNARGS fa; |
637 |
|
638 |
if (readfargs(&fa, fin) != 1) |
639 |
return(-1); |
640 |
if (fa.nfargs < 9) |
641 |
return(-1); |
642 |
/* string arguments */ |
643 |
printf("%d", fa.nsargs); |
644 |
for (i = 0; i < fa.nsargs; i++) |
645 |
printf(" %s", fa.sarg[i]); |
646 |
printf("\n0\n%d\n", fa.nfargs); |
647 |
/* anchor point */ |
648 |
multp3(v, fa.farg, tot.xfm); |
649 |
printf(" %18.12g %18.12g %18.12g\n", v[0], v[1], v[2]); |
650 |
/* right vector */ |
651 |
multv3(v, fa.farg+3, tot.xfm); |
652 |
printf(" %18.12g %18.12g %18.12g\n", v[0], v[1], v[2]); |
653 |
/* down vector */ |
654 |
multv3(v, fa.farg+6, tot.xfm); |
655 |
printf(" %18.12g %18.12g %18.12g", v[0], v[1], v[2]); |
656 |
/* remaining arguments */ |
657 |
for (i = 9; i < fa.nfargs; i++) { |
658 |
if (i%3 == 0) |
659 |
putchar('\n'); |
660 |
printf(" %18.12g", fa.farg[i]); |
661 |
} |
662 |
putchar('\n'); |
663 |
freefargs(&fa); |
664 |
return(0); |
665 |
} |
666 |
|
667 |
|
668 |
o_source(fin) /* transform source arguments */ |
669 |
FILE *fin; |
670 |
{ |
671 |
FVECT dv; |
672 |
FUNARGS fa; |
673 |
|
674 |
if (readfargs(&fa, fin) != 1) |
675 |
return(-1); |
676 |
if (fa.nsargs != 0 || fa.nfargs != 4) |
677 |
return(-1); |
678 |
/* transform direction vector */ |
679 |
multv3(dv, fa.farg, tot.xfm); |
680 |
/* output */ |
681 |
printf("0\n0\n4"); |
682 |
printf(" %18.12g %18.12g %18.12g %18.12g\n", |
683 |
dv[0], dv[1], dv[2], fa.farg[3]); |
684 |
freefargs(&fa); |
685 |
return(0); |
686 |
} |
687 |
|
688 |
|
689 |
o_sphere(fin) /* transform sphere arguments */ |
690 |
FILE *fin; |
691 |
{ |
692 |
FVECT cent; |
693 |
double rad; |
694 |
FUNARGS fa; |
695 |
|
696 |
if (readfargs(&fa, fin) != 1) |
697 |
return(-1); |
698 |
if (fa.nsargs != 0 || fa.nfargs != 4) |
699 |
return(-1); |
700 |
|
701 |
multp3(cent, fa.farg, tot.xfm); /* transform center */ |
702 |
|
703 |
rad = fa.farg[3] * tot.sca; /* scale radius */ |
704 |
|
705 |
printf("0\n0\n4"); |
706 |
printf(" %18.12g %18.12g %18.12g %18.12g\n", |
707 |
cent[0], cent[1], cent[2], rad); |
708 |
freefargs(&fa); |
709 |
return(0); |
710 |
} |
711 |
|
712 |
|
713 |
o_face(fin) /* transform face arguments */ |
714 |
FILE *fin; |
715 |
{ |
716 |
FVECT p; |
717 |
register int i; |
718 |
FUNARGS fa; |
719 |
|
720 |
if (readfargs(&fa, fin) != 1) |
721 |
return(-1); |
722 |
if (fa.nsargs != 0 || fa.nfargs % 3) |
723 |
return(-1); |
724 |
|
725 |
printf("0\n0\n%d\n", fa.nfargs); |
726 |
|
727 |
for (i = 0; i < fa.nfargs; i += 3) { |
728 |
if (reverse) |
729 |
multp3(p, fa.farg+(fa.nfargs-i-3), tot.xfm); |
730 |
else |
731 |
multp3(p, fa.farg+i, tot.xfm); |
732 |
printf(" %18.12g %18.12g %18.12g\n", p[0], p[1], p[2]); |
733 |
} |
734 |
freefargs(&fa); |
735 |
return(0); |
736 |
} |
737 |
|
738 |
|
739 |
o_cone(fin) /* transform cone and cup arguments */ |
740 |
FILE *fin; |
741 |
{ |
742 |
FVECT p0, p1; |
743 |
double r0, r1; |
744 |
FUNARGS fa; |
745 |
|
746 |
if (readfargs(&fa, fin) != 1) |
747 |
return(-1); |
748 |
if (fa.nsargs != 0 || fa.nfargs != 8) |
749 |
return(-1); |
750 |
|
751 |
printf("0\n0\n8\n"); |
752 |
|
753 |
multp3(p0, fa.farg, tot.xfm); |
754 |
multp3(p1, fa.farg+3, tot.xfm); |
755 |
r0 = fa.farg[6] * tot.sca; |
756 |
r1 = fa.farg[7] * tot.sca; |
757 |
printf(" %18.12g %18.12g %18.12g\n", p0[0], p0[1], p0[2]); |
758 |
printf(" %18.12g %18.12g %18.12g\n", p1[0], p1[1], p1[2]); |
759 |
printf(" %18.12g %18.12g\n", r0, r1); |
760 |
|
761 |
freefargs(&fa); |
762 |
return(0); |
763 |
} |
764 |
|
765 |
|
766 |
o_cylinder(fin) /* transform cylinder and tube arguments */ |
767 |
FILE *fin; |
768 |
{ |
769 |
FVECT p0, p1; |
770 |
double rad; |
771 |
FUNARGS fa; |
772 |
|
773 |
if (readfargs(&fa, fin) != 1) |
774 |
return(-1); |
775 |
if (fa.nsargs != 0 || fa.nfargs != 7) |
776 |
return(-1); |
777 |
|
778 |
printf("0\n0\n7\n"); |
779 |
|
780 |
multp3(p0, fa.farg, tot.xfm); |
781 |
multp3(p1, fa.farg+3, tot.xfm); |
782 |
rad = fa.farg[6] * tot.sca; |
783 |
printf(" %18.12g %18.12g %18.12g\n", p0[0], p0[1], p0[2]); |
784 |
printf(" %18.12g %18.12g %18.12g\n", p1[0], p1[1], p1[2]); |
785 |
printf(" %18.12g\n", rad); |
786 |
freefargs(&fa); |
787 |
return(0); |
788 |
} |
789 |
|
790 |
|
791 |
o_ring(fin) /* transform ring arguments */ |
792 |
FILE *fin; |
793 |
{ |
794 |
FVECT p0, pd; |
795 |
double r0, r1; |
796 |
FUNARGS fa; |
797 |
|
798 |
if (readfargs(&fa, fin) != 1) |
799 |
return(-1); |
800 |
if (fa.nsargs != 0 || fa.nfargs != 8) |
801 |
return(-1); |
802 |
|
803 |
printf("0\n0\n8\n"); |
804 |
|
805 |
multp3(p0, fa.farg, tot.xfm); |
806 |
multv3(pd, fa.farg+3, tot.xfm); |
807 |
if (invert) { |
808 |
pd[0] = -pd[0]; |
809 |
pd[1] = -pd[1]; |
810 |
pd[2] = -pd[2]; |
811 |
} |
812 |
r0 = fa.farg[6] * tot.sca; |
813 |
r1 = fa.farg[7] * tot.sca; |
814 |
printf(" %18.12g %18.12g %18.12g\n", p0[0], p0[1], p0[2]); |
815 |
printf(" %18.12g %18.12g %18.12g\n", pd[0], pd[1], pd[2]); |
816 |
printf(" %18.12g %18.12g\n", r0, r1); |
817 |
freefargs(&fa); |
818 |
return(0); |
819 |
} |
820 |
|
821 |
|
822 |
initotypes() /* initialize ofun[] array */ |
823 |
{ |
824 |
register int i; |
825 |
|
826 |
if (ofun[OBJ_SOURCE].funp == o_source) |
827 |
return; /* done already */ |
828 |
/* alias is additional */ |
829 |
ofun[ALIAS].funame = ALIASID; |
830 |
ofun[ALIAS].flags = 0; |
831 |
/* functions get new transform */ |
832 |
for (i = 0; i < NUMTYPES; i++) |
833 |
if (hasfunc(i)) |
834 |
ofun[i].funp = addxform; |
835 |
/* special cases */ |
836 |
ofun[OBJ_SOURCE].funp = o_source; |
837 |
ofun[OBJ_SPHERE].funp = |
838 |
ofun[OBJ_BUBBLE].funp = o_sphere; |
839 |
ofun[OBJ_FACE].funp = o_face; |
840 |
ofun[OBJ_CONE].funp = |
841 |
ofun[OBJ_CUP].funp = o_cone; |
842 |
ofun[OBJ_CYLINDER].funp = |
843 |
ofun[OBJ_TUBE].funp = o_cylinder; |
844 |
ofun[OBJ_RING].funp = o_ring; |
845 |
ofun[OBJ_INSTANCE].funp = addxform; |
846 |
ofun[MAT_GLOW].funp = m_glow; |
847 |
ofun[MAT_SPOT].funp = m_spot; |
848 |
ofun[MAT_DIELECTRIC].funp = m_dielectric; |
849 |
ofun[MAT_INTERFACE].funp = m_interface; |
850 |
ofun[MAT_MIST].funp = m_mist; |
851 |
ofun[PAT_CTEXT].funp = |
852 |
ofun[PAT_BTEXT].funp = |
853 |
ofun[MIX_TEXT].funp = text; |
854 |
ofun[ALIAS].funp = alias; |
855 |
/* surface inverses */ |
856 |
tinvers[OBJ_FACE] = OBJ_FACE; |
857 |
tinvers[OBJ_SOURCE] = OBJ_SOURCE; |
858 |
tinvers[OBJ_CONE] = OBJ_CUP; |
859 |
tinvers[OBJ_CUP] = OBJ_CONE; |
860 |
tinvers[OBJ_SPHERE] = OBJ_BUBBLE; |
861 |
tinvers[OBJ_BUBBLE] = OBJ_SPHERE; |
862 |
tinvers[OBJ_RING] = OBJ_RING; |
863 |
tinvers[OBJ_CYLINDER] = OBJ_TUBE; |
864 |
tinvers[OBJ_TUBE] = OBJ_CYLINDER; |
865 |
tinvers[OBJ_INSTANCE] = OBJ_INSTANCE; /* oh, well */ |
866 |
} |
867 |
|
868 |
|
869 |
#ifdef OLDXFORM |
870 |
openmain(fname) |
871 |
char *fname; |
872 |
{ |
873 |
if (fname == NULL) { |
874 |
strcpy(mainfn, "standard input"); |
875 |
mainfp = stdin; |
876 |
return; |
877 |
} |
878 |
if (mainfp != NULL) { |
879 |
if (!strcmp(fname, mainfn)) { |
880 |
rewind(mainfp); |
881 |
return; |
882 |
} |
883 |
fclose(mainfp); |
884 |
} |
885 |
if ((mainfp = fopen(fname, "r")) == NULL) { |
886 |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
887 |
progname, fname); |
888 |
exit(1); |
889 |
} |
890 |
strcpy(mainfn, fname); |
891 |
} |
892 |
#else |
893 |
openmain(fname) /* open fname for input, changing to its directory */ |
894 |
char *fname; |
895 |
{ |
896 |
extern FILE *tmpfile(); |
897 |
extern char *getlibpath(), *getpath(); |
898 |
static char origdir[MAXPATH]; |
899 |
static char curfn[MAXPATH]; |
900 |
static int diffdir; |
901 |
register char *fpath; |
902 |
|
903 |
if (fname == NULL) { /* standard input */ |
904 |
if (mainfp == NULL) { |
905 |
register int c; |
906 |
strcpy(mainfn, "standard input"); |
907 |
if (nrept <= 1) { |
908 |
mainfp = stdin; |
909 |
return; /* just read once */ |
910 |
} |
911 |
/* else copy */ |
912 |
if ((mainfp = tmpfile()) == NULL) { |
913 |
fprintf(stderr, |
914 |
"%s: cannot create temporary file\n", |
915 |
progname); |
916 |
exit(1); |
917 |
} |
918 |
while ((c = getc(stdin)) != EOF) |
919 |
putc(c, mainfp); |
920 |
fclose(stdin); |
921 |
} |
922 |
rewind(mainfp); /* rewind copy */ |
923 |
return; |
924 |
} |
925 |
if (mainfp == NULL) { /* first call, initialize */ |
926 |
getwd(origdir); |
927 |
} else if (!strcmp(fname, curfn)) { /* just need to rewind? */ |
928 |
rewind(mainfp); |
929 |
return; |
930 |
} else { /* else close old stream */ |
931 |
fclose(mainfp); |
932 |
if (diffdir) { |
933 |
chdir(origdir); |
934 |
diffdir = 0; |
935 |
} |
936 |
} |
937 |
strcpy(curfn, fname); /* remember file name */ |
938 |
/* get full path */ |
939 |
if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) { |
940 |
fprintf(stderr, "%s: cannot find file \"%s\"\n", |
941 |
progname, fname); |
942 |
exit(1); |
943 |
} |
944 |
if (fpath[0] == '.' && ISDIRSEP(fpath[1])) /* remove leading ./ */ |
945 |
fpath += 2; |
946 |
/* record path name */ |
947 |
strcpy(mainfn, fpath); |
948 |
if (expand) { /* change to local directory */ |
949 |
register char *cp = fpath + strlen(fpath); /* get dir. */ |
950 |
while (cp > fpath) { |
951 |
cp--; |
952 |
if (ISDIRSEP(*cp)) { |
953 |
if (cp == fpath) |
954 |
cp++; /* root special case */ |
955 |
break; |
956 |
} |
957 |
} |
958 |
*cp = '\0'; |
959 |
if (fpath[0]) { /* change to new directory? */ |
960 |
if (chdir(fpath) < 0) { |
961 |
fprintf(stderr, |
962 |
"%s: cannot change directory to \"%s\"\n", |
963 |
progname, fpath); |
964 |
exit(1); |
965 |
} |
966 |
diffdir++; |
967 |
} |
968 |
/* get final path component */ |
969 |
for (fpath = fname+strlen(fname); |
970 |
fpath > fname && !ISDIRSEP(fpath[-1]); fpath--) |
971 |
; |
972 |
} |
973 |
/* open the file */ |
974 |
if ((mainfp = fopen(fpath, "r")) == NULL) { |
975 |
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
976 |
progname, mainfn); |
977 |
exit(1); |
978 |
} |
979 |
} |
980 |
#endif |