ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/genrhenv.c
Revision: 3.1
Committed: Tue Jan 6 15:09:33 1998 UTC (26 years, 9 months ago) by gregl
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

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