1 |
/* Copyright (c) 1990 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* Copyright (c) 1988 Regents of the University of California */ |
8 |
|
9 |
/* |
10 |
* thf2rad - convert GDS things file to Radiance scene description. |
11 |
* |
12 |
* 12/21/88 |
13 |
*/ |
14 |
|
15 |
#include <stdio.h> |
16 |
#include <math.h> |
17 |
|
18 |
#define MAXSTR 128 /* maximum string or id length */ |
19 |
#define MAXPTS 2048 /* maximum points per object */ |
20 |
|
21 |
typedef struct { |
22 |
double x, y, z; |
23 |
} POINT; |
24 |
|
25 |
double rad = 0.0; /* line radius */ |
26 |
|
27 |
|
28 |
main(argc, argv) |
29 |
int argc; |
30 |
char *argv[]; |
31 |
{ |
32 |
char *name = NULL; |
33 |
int an; |
34 |
|
35 |
for (an = 1; an < argc; an++) |
36 |
if (argv[an][0] == '-') |
37 |
switch (argv[an][1]) { |
38 |
case 'n': |
39 |
name = argv[++an]; |
40 |
break; |
41 |
case 'r': |
42 |
rad = atof(argv[++an]); |
43 |
break; |
44 |
default: |
45 |
fprintf(stderr, |
46 |
"Usage: %s [-n name][-r rad] [file ..]\n", |
47 |
argv[0]); |
48 |
exit(1); |
49 |
} |
50 |
else |
51 |
break; |
52 |
header(argc, argv); |
53 |
if (an < argc) |
54 |
for ( ; an < argc; an++) |
55 |
thf2rad(name, argv[an]); |
56 |
else |
57 |
thf2rad(name, NULL); |
58 |
exit(0); |
59 |
} |
60 |
|
61 |
|
62 |
thf2rad(name, file) /* convert things file */ |
63 |
char *name, *file; |
64 |
{ |
65 |
char nambuf[MAXSTR]; |
66 |
register char *cp; |
67 |
FILE *fp; |
68 |
int on; |
69 |
/* open file and hack name */ |
70 |
if (file == NULL) { |
71 |
fp = stdin; |
72 |
file = "<stdin>"; |
73 |
if (name == NULL) |
74 |
name = "thing"; |
75 |
} else { |
76 |
if ((fp = fopen(file, "r")) == NULL) { |
77 |
fprintf(stderr, "%s: cannot open\n", file); |
78 |
exit(1); |
79 |
} |
80 |
if (name == NULL) |
81 |
name = file; |
82 |
} |
83 |
for (cp = nambuf; *cp = *name++; cp++) |
84 |
; |
85 |
/* get objects from file */ |
86 |
on = 0; |
87 |
while (cvobject(nambuf, file, fp)) |
88 |
sprintf(cp, ".%d", ++on); |
89 |
} |
90 |
|
91 |
|
92 |
cvobject(name, file, fp) /* convert next object in things file */ |
93 |
char *name, *file; |
94 |
FILE *fp; |
95 |
{ |
96 |
static POINT parr[MAXPTS]; |
97 |
static unsigned char haspt[MAXPTS/8]; |
98 |
char sbuf[MAXSTR], nambuf[MAXSTR]; |
99 |
int npts, n, nv, p1, p2; |
100 |
register int i; |
101 |
/* get points */ |
102 |
if (fscanf(fp, "%s", sbuf) != 1) |
103 |
return(0); |
104 |
if (strcmp(sbuf, "YIN")) |
105 |
readerr(file, "not a GDS things file"); |
106 |
if (fscanf(fp, "%d", &npts) != 1 || npts < 0 || npts > MAXPTS) |
107 |
readerr(file, "bad number of points"); |
108 |
for (i = 0; i < npts; i++) |
109 |
if (fscanf(fp, "%lf %lf %lf", |
110 |
&parr[i].x, &parr[i].y, &parr[i].z) != 3) |
111 |
readerr(file, "bad point"); |
112 |
for (i = (npts+7)/8; i >= 0; i--) |
113 |
haspt[i] = 0; |
114 |
/* get lines */ |
115 |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
116 |
readerr(file, "bad number of lines"); |
117 |
for (i = 0; i < n; i++) { |
118 |
if (fscanf(fp, "%d %d %s", &p1, &p2, sbuf) != 3) |
119 |
readerr(file, "error reading vector"); |
120 |
if (--p1 < 0 || p1 >= npts || --p2 < 0 || p2 >= npts) |
121 |
readerr(file, "bad point in vector"); |
122 |
if (!(haspt[p1>>3] & 1<<(p1&7))) { |
123 |
sprintf(nambuf, "%s.l%d.s0", name, i+1); |
124 |
point(sbuf, nambuf, &parr[p1]); |
125 |
haspt[p1>>3] |= 1<<(p1&7); |
126 |
} |
127 |
sprintf(nambuf, "%s.l%d.c", name, i+1); |
128 |
line(sbuf, nambuf, &parr[p1], &parr[p2]); |
129 |
if (!(haspt[p2>>3] & 1<<(p2&7))) { |
130 |
sprintf(nambuf, "%s.l%d.s1", name, i+1); |
131 |
point(sbuf, nambuf, &parr[p2]); |
132 |
haspt[p2>>3] |= 1<<(p2&7); |
133 |
} |
134 |
} |
135 |
/* get surfaces */ |
136 |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
137 |
readerr(file, "bad number of surfaces"); |
138 |
for (i = 0; i < n; i++) { |
139 |
if (fscanf(fp, "%d %s %d", &p1, sbuf, &nv) != 3) |
140 |
readerr(file, "error reading surface"); |
141 |
if (p1 != 1 || nv < 0) |
142 |
readerr(file, "bad surface type (has hole?)"); |
143 |
if (nv < 3) { |
144 |
while (nv--) |
145 |
fscanf(fp, "%*d"); |
146 |
continue; |
147 |
} |
148 |
printf("\n%s polygon %s.p%d\n", sbuf, name, i+1); |
149 |
printf("0\n0\n%d\n", 3*nv); |
150 |
while (nv--) { |
151 |
if (fscanf(fp, "%d", &p1) != 1) |
152 |
readerr(file, "error reading surface vertex"); |
153 |
loc(&parr[p1-1]); |
154 |
} |
155 |
} |
156 |
return(1); |
157 |
} |
158 |
|
159 |
|
160 |
header(ac, av) /* print header */ |
161 |
int ac; |
162 |
char **av; |
163 |
{ |
164 |
putchar('#'); |
165 |
while (ac--) { |
166 |
putchar(' '); |
167 |
fputs(*av++, stdout); |
168 |
} |
169 |
putchar('\n'); |
170 |
} |
171 |
|
172 |
|
173 |
point(mat, nam, pos) /* print point */ |
174 |
char *mat, *nam; |
175 |
POINT *pos; |
176 |
{ |
177 |
if (rad <= 0.0) |
178 |
return; |
179 |
printf("\n%s sphere %s\n", mat, nam); |
180 |
printf("0\n0\n4\n"); |
181 |
loc(pos); |
182 |
printf("\t%g\n", rad); |
183 |
} |
184 |
|
185 |
|
186 |
line(mat, nam, p1, p2) /* print line */ |
187 |
char *mat, *nam; |
188 |
POINT *p1, *p2; |
189 |
{ |
190 |
if (rad <= 0.0) |
191 |
return; |
192 |
printf("\n%s cylinder %s\n", mat, nam); |
193 |
printf("0\n0\n7\n"); |
194 |
loc(p1); |
195 |
loc(p2); |
196 |
printf("\t%g\n", rad); |
197 |
} |
198 |
|
199 |
|
200 |
loc(p) /* print location */ |
201 |
register POINT *p; |
202 |
{ |
203 |
printf("\t%14.8g\t%14.8g\t%14.8g\n", p->x, p->y, p->z); |
204 |
} |
205 |
|
206 |
|
207 |
readerr(file, err) /* print read error and exit */ |
208 |
char *file, *err; |
209 |
{ |
210 |
fprintf(stderr, "%s: %s\n", file, err); |
211 |
exit(1); |
212 |
} |