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