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