ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/thf2rad.c
Revision: 2.2
Committed: Thu Dec 19 15:05:54 1991 UTC (32 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +4 -1 lines
Log Message:
removed atof declaration for NeXT

File Contents

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