ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/genrhenv.c
Revision: 3.2
Committed: Tue Jan 6 21:48:18 1998 UTC (26 years, 10 months ago) by gregl
Content type: text/plain
Branch: MAIN
Changes since 3.1: +2 -0 lines
Log Message:
added printargs

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