ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/cv/thf2rad.c
Revision: 2.6
Committed: Sat Feb 22 02:07:23 2003 UTC (21 years ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.5: +2 -8 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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