ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/genrhenv.c
Revision: 3.5
Committed: Fri Jun 20 00:25:49 2003 UTC (21 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 3.4: +2 -2 lines
Log Message:
Changed instances of "int4" to "int32" and "int2" to "int16"

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: genrhenv.c,v 3.4 2003/05/29 16:26:21 greg Exp $";
3 #endif
4 /*
5 * Create a closed environment from a holodeck section
6 */
7
8 #include "holo.h"
9
10 #define ourhp (hdlist[0])
11
12 char *progname; /* global argv[0] */
13 char *prefix; /* file name prefix */
14
15 HDBEAMI *gabmi; /* beam index array */
16
17
18 main(argc, argv)
19 int argc;
20 char *argv[];
21 {
22 int sect;
23 register int n;
24
25 progname = argv[0];
26 if (argc < 3 | argc > 4)
27 goto userr;
28 prefix = argv[1];
29 sect = argc==3 ? 0 : atoi(argv[3]);
30 openholo(argv[2], sect);
31 mkcalfile(argv[2], sect);
32 n = ourhp->grid[0] * ourhp->grid[1];
33 if (ourhp->grid[0] * ourhp->grid[2] > n)
34 n = ourhp->grid[0] * ourhp->grid[2];
35 if (ourhp->grid[1] * ourhp->grid[2] > n)
36 n = ourhp->grid[1] * ourhp->grid[2];
37 gabmi = (HDBEAMI *)malloc(n*sizeof(HDBEAMI));
38 if (gabmi == NULL)
39 error(SYSTEM, "out of memory in main");
40 while (n--)
41 gabmi[n].h = ourhp;
42 fputs("# ", stdout);
43 printargs(argc, argv, stdout);
44 hdcachesize = 0;
45 for (n = 0; n < 6; n++)
46 mkwall(argv[2], sect, n);
47 quit(0);
48 userr:
49 fprintf(stderr, "Usage: %s prefix input.hdk [section]\n", progname);
50 exit(1);
51 }
52
53
54 openholo(fname, sect) /* open holodeck section for input */
55 char *fname;
56 int sect;
57 {
58 FILE *fp;
59 int fd;
60 int32 nextloc;
61 int n;
62 /* open holodeck file */
63 if ((fp = fopen(fname, "r")) == NULL) {
64 sprintf(errmsg, "cannot open \"%s\"", fname);
65 error(SYSTEM, errmsg);
66 }
67 /* check header and magic number */
68 if (checkheader(fp, HOLOFMT, NULL) < 0 || getw(fp) != HOLOMAGIC) {
69 sprintf(errmsg, "file \"%s\" not in holodeck format", fname);
70 error(USER, errmsg);
71 }
72 fd = dup(fileno(fp)); /* dup file handle */
73 nextloc = ftell(fp); /* get stdio position */
74 fclose(fp); /* done with stdio */
75 for (n = 0; nextloc > 0L; n++) { /* get the indicated section */
76 lseek(fd, (off_t)nextloc, 0);
77 read(fd, (char *)&nextloc, sizeof(nextloc));
78 if (n == sect) {
79 hdinit(fd, NULL);
80 return;
81 }
82 }
83 error(USER, "holodeck section not found");
84 }
85
86
87 mkwall(hdk, sect, wn) /* make map for indicated wall */
88 char *hdk;
89 int sect;
90 int wn;
91 {
92 static char cname[3][6] = {"Red", "Green", "Blue"};
93 char fnbuf[128];
94 FILE *cdat[3];
95 int st = 0;
96 int wo, i, j;
97 /* loop through each opposing wall */
98 for (wo = 0; wo < 6; wo++) {
99 if (wo == wn) continue;
100 /* create data files */
101 sprintf(fnbuf, "%s_%dr%d.dat", prefix, wn, wo);
102 if ((cdat[0] = fopen(fnbuf, "w")) == NULL)
103 goto openerr;
104 sprintf(fnbuf, "%s_%dg%d.dat", prefix, wn, wo);
105 if ((cdat[1] = fopen(fnbuf, "w")) == NULL)
106 goto openerr;
107 sprintf(fnbuf, "%s_%db%d.dat", prefix, wn, wo);
108 if ((cdat[2] = fopen(fnbuf, "w")) == NULL)
109 goto openerr;
110 /* write dimensions */
111 for (i = 0; i < 3; i++) {
112 fprintf(cdat[i],
113 "# %s channel for wall %d -> wall %d of section %d in holodeck \"%s\"\n",
114 cname[i], wo, wn, sect, hdk);
115 fprintf(cdat[i],
116 "4\n0.5 %.1f %d\n0.5 %.1f %d\n0.5 %.1f %d\n0.5 %.1f %d\n",
117 ourhp->grid[hdwg0[wn]]-.5, ourhp->grid[hdwg0[wn]],
118 ourhp->grid[hdwg1[wn]]-.5, ourhp->grid[hdwg1[wn]],
119 ourhp->grid[hdwg0[wo]]-.5, ourhp->grid[hdwg0[wo]],
120 ourhp->grid[hdwg1[wo]]-.5, ourhp->grid[hdwg1[wo]]);
121 }
122 /* run each grid cell */
123 for (i = 0; i < ourhp->grid[hdwg0[wn]]; i++)
124 for (j = 0; j < ourhp->grid[hdwg1[wn]]; j++)
125 dogrid(wn, i, j, wo, cdat);
126 /* close files */
127 if (fclose(cdat[0]) == EOF | fclose(cdat[1]) == EOF |
128 fclose(cdat[2]) == EOF)
129 error(SYSTEM, "write error in mkwall");
130 /* put out pattern */
131 printf("\n%s colordata wallmap\n", st++ ? "wallmap" : "void");
132 printf("11 f f f %s_%dr%d.dat %s_%dg%d.dat %s_%db%d.dat\n",
133 prefix,wn,wo, prefix,wn,wo, prefix,wn,wo);
134 printf("\t%s.cal nu nv ou ov\n0\n2 %d %d\n", prefix, wn, wo);
135 }
136 /* put out polygon */
137 printf("\nwallmap glow wallglow\n0\n0\n4 1 1 1 0\n");
138 printf("\nwallglow polygon wall%d\n0\n0\n12\n", wn);
139 wallverts(wn);
140 return;
141 openerr:
142 sprintf(errmsg, "cannot open \"%d\" for writing", fnbuf);
143 error(SYSTEM, errmsg);
144 }
145
146
147 wallverts(wn) /* print out vertices for wall wn */
148 int wn;
149 {
150 int rev, i0, i1;
151 FVECT corn, vt;
152
153 if ((rev = wn & 1))
154 VSUM(corn, ourhp->orig, ourhp->xv[wn>>1], 1.);
155 else
156 VCOPY(corn, ourhp->orig);
157 fcross(vt, ourhp->xv[i0=hdwg0[wn]], ourhp->xv[i1=hdwg1[wn]]);
158 if (DOT(vt, ourhp->wg[wn>>1]) < 0.)
159 rev ^= 1;
160 if (rev) {
161 i0 = hdwg1[wn]; i1 = hdwg0[wn];
162 }
163 printf("\t%.10e %.10e %.10e\n", corn[0], corn[1], corn[2]);
164 printf("\t%.10e %.10e %.10e\n", corn[0] + ourhp->xv[i0][0],
165 corn[1] + ourhp->xv[i0][1], corn[2] + ourhp->xv[i0][2]);
166 printf("\t%.10e %.10e %.10e\n",
167 corn[0] + ourhp->xv[i0][0] + ourhp->xv[i1][0],
168 corn[1] + ourhp->xv[i0][1] + ourhp->xv[i1][1],
169 corn[2] + ourhp->xv[i0][2] + ourhp->xv[i1][2]);
170 printf("\t%.10e %.10e %.10e\n", corn[0] + ourhp->xv[i1][0],
171 corn[1] + ourhp->xv[i1][1], corn[2] + ourhp->xv[i1][2]);
172 }
173
174
175 dogrid(wn, ci, cj, wo, dfp) /* compute and write grid cell data */
176 int wn, ci, cj, wo;
177 FILE *dfp[3];
178 {
179 GCOORD gc[2];
180 COLOR cavg;
181 register int n;
182
183 gc[1].w = wn; gc[1].i[0] = ci; gc[1].i[1] = cj;
184 gc[0].w = wo;
185 n = 0; /* load beams in optimal order */
186 for (gc[0].i[0] = ourhp->grid[hdwg0[gc[0].w]]; gc[0].i[0]--; )
187 for (gc[0].i[1] = ourhp->grid[hdwg1[gc[0].w]]; gc[0].i[1]--; )
188 gabmi[n++].b = hdbindex(ourhp, gc);
189 hdloadbeams(gabmi, n, NULL);
190 /* run beams in regular order */
191 fprintf(dfp[0], "\n# Begin grid cell (%d,%d)\n", ci, cj);
192 fprintf(dfp[1], "\n# Begin grid cell (%d,%d)\n", ci, cj);
193 fprintf(dfp[2], "\n# Begin grid cell (%d,%d)\n", ci, cj);
194 for (gc[0].i[0] = 0; gc[0].i[0] < ourhp->grid[hdwg0[gc[0].w]];
195 gc[0].i[0]++) {
196 for (gc[0].i[1] = 0; gc[0].i[1] < ourhp->grid[hdwg1[gc[0].w]];
197 gc[0].i[1]++) {
198 avgbeam(cavg, gc);
199 fprintf(dfp[0], " %.2e", colval(cavg,RED));
200 fprintf(dfp[1], " %.2e", colval(cavg,GRN));
201 fprintf(dfp[2], " %.2e", colval(cavg,BLU));
202 }
203 fputc('\n', dfp[0]);
204 fputc('\n', dfp[1]);
205 fputc('\n', dfp[2]);
206 }
207 hdflush(NULL); /* flush cache */
208 }
209
210
211 avgbeam(cavg, gc) /* compute average beam color */
212 COLOR cavg;
213 GCOORD gc[2];
214 {
215 static COLOR crun = BLKCOLOR;
216 static int ncrun = 0;
217 register BEAM *bp;
218 COLOR cray;
219 unsigned beamlen;
220 int ls[2][3];
221 FVECT gp[2], wp[2];
222 double d;
223 int n;
224 register int i;
225
226 bp = hdgetbeam(ourhp, hdbindex(ourhp, gc));
227 if (bp == NULL || !bp->nrm) { /* use running average */
228 copycolor(cavg, crun);
229 if (ncrun > 1) {
230 d = 1./ncrun;
231 scalecolor(cavg, d);
232 }
233 } else { /* use beam average */
234 hdlseg(ls, ourhp, gc); /* get beam length */
235 for (i = 3; i--; ) {
236 gp[0][i] = ls[0][i] + .5;
237 gp[1][i] = ls[1][i] + .5;
238 }
239 hdworld(wp[0], ourhp, gp[0]);
240 hdworld(wp[1], ourhp, gp[1]);
241 d = sqrt(dist2(wp[0], wp[1]));
242 beamlen = hdcode(ourhp, d);
243 setcolor(cavg, 0., 0., 0.);
244 n = 0;
245 for (i = bp->nrm; i--; ) {
246 if (hdbray(bp)[i].d < beamlen)
247 continue; /* inside section */
248 colr_color(cray, hdbray(bp)[i].v);
249 addcolor(cavg, cray);
250 n++;
251 }
252 if (n > 1) {
253 d = 1./n;
254 scalecolor(cavg, d);
255 }
256 if (n) {
257 addcolor(crun, cavg);
258 ncrun++;
259 }
260 }
261 }
262
263
264 mkcalfile(hdk, sect) /* create .cal file */
265 char *hdk;
266 int sect;
267 {
268 char fname[128];
269 FILE *fp;
270 register int i;
271
272 sprintf(fname, "%s.cal", prefix);
273 if ((fp = fopen(fname, "w")) == NULL) {
274 sprintf(errmsg, "cannot open \"%s\" for writing", fname);
275 error(SYSTEM, errmsg);
276 }
277 fprintf(fp,
278 "{\n\tCoordinate calculation for section %d of holodeck \"%s\"\n}\n",
279 sect, hdk);
280
281 fprintf(fp, "\n{ Constants }\n");
282 fprintf(fp, "hox : %.7e; hoy : %.7e; hoz : %.7e;\n",
283 ourhp->orig[0], ourhp->orig[1], ourhp->orig[2]);
284 fprintf(fp, "grid(i) : select(i+1, %d, %d, %d);\n",
285 ourhp->grid[0], ourhp->grid[1], ourhp->grid[2]);
286 fprintf(fp, "hwgx(i) : select(i+1, %.7e, %.7e, %.7e);\n",
287 ourhp->wg[0][0], ourhp->wg[1][0], ourhp->wg[2][0]);
288 fprintf(fp, "hwgy(i) : select(i+1, %.7e, %.7e, %.7e);\n",
289 ourhp->wg[0][1], ourhp->wg[1][1], ourhp->wg[2][1]);
290 fprintf(fp, "hwgz(i) : select(i+1, %.7e, %.7e, %.7e);\n",
291 ourhp->wg[0][2], ourhp->wg[1][2], ourhp->wg[2][2]);
292 fprintf(fp, "hwo(i) : select(i+1, %.7e, %.7e, %.7e,\n",
293 ourhp->wo[0], ourhp->wo[1], ourhp->wo[2]);
294 fprintf(fp, "\t\t%.7e, %.7e, %.7e);\n",
295 ourhp->wo[3], ourhp->wo[4], ourhp->wo[5]);
296 fprintf(fp,
297 "wgp(i,x,y,z) : (x-hox)*hwgx(i) + (y-hoy)*hwgy(i) + (z-hoz)*hwgz(i);\n");
298 fprintf(fp, "wg0(w) : select(.5*w+.75, 1, 2, 0);\n");
299 fprintf(fp, "wg1(w) : select(.5*w+.75, 2, 0, 1);\n");
300
301 fprintf(fp, "\n{ Variables }\n");
302 fprintf(fp, "nw = arg(1); ow = arg(2);\n");
303 fprintf(fp, "oax = .5*ow-.25;\n");
304 fprintf(fp, "ng0 = wg0(nw); ng1 = wg1(nw);\n");
305 fprintf(fp, "og0 = wg0(ow); og1 = wg1(ow);\n");
306 fprintf(fp,
307 "odenom = Dx*hwgx(oax) + Dy*hwgy(oax) + Dz*hwgz(oax);\n");
308 fprintf(fp, "odist = if(and(FTINY-odenom, FTINY+odenom), -1,\n");
309 fprintf(fp,
310 "\t\t(Px*hwgx(oax) + Py*hwgy(oax) + Pz*hwgz(oax) - hwo(ow))/odenom);\n");
311 fprintf(fp,
312 "opx = Px - odist*Dx; opy = Py - odist*Dy; opz = Pz - odist*Dz;\n");
313 fprintf(fp, "nu = wgp(ng0,Px,Py,Pz);\n");
314 fprintf(fp, "nv = wgp(ng1,Px,Py,Pz);\n");
315 fprintf(fp, "ou = wgp(og0,opx,opy,opz);\n");
316 fprintf(fp, "ov = wgp(og1,opx,opy,opz);\n");
317 fprintf(fp, "f(v) = if(and(and(ou-FTINY,grid(og0)-ou-FTINY),\n");
318 fprintf(fp, "\t\tand(ov-FTINY,grid(og1)-ov-FTINY)), if(v,v,0), 1);\n");
319 fclose(fp);
320 }
321
322
323 void
324 eputs(s) /* put error message to stderr */
325 register char *s;
326 {
327 static int midline = 0;
328
329 if (!*s)
330 return;
331 if (!midline++) { /* prepend line with program name */
332 fputs(progname, stderr);
333 fputs(": ", stderr);
334 }
335 fputs(s, stderr);
336 if (s[strlen(s)-1] == '\n') {
337 fflush(stderr);
338 midline = 0;
339 }
340 }