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