ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rsensor.c
Revision: 2.6
Committed: Sat Dec 13 00:44:05 2008 UTC (15 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.5: +27 -13 lines
Log Message:
Improved sampling method very slightly

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rsensor.c,v 2.5 2008/04/11 22:06:04 greg Exp $";
3 #endif
4
5 /*
6 * Compute sensor signal based on spatial sensitivity.
7 *
8 * Created Feb 2008 for Architectural Energy Corp.
9 */
10
11 #include "ray.h"
12 #include "source.h"
13 #include "view.h"
14 #include "random.h"
15
16 #define DEGREE (PI/180.)
17
18 #define MAXNT 180 /* maximum number of theta divisions */
19 #define MAXNP 360 /* maximum number of phi divisions */
20
21 extern char *progname; /* global argv[0] */
22 extern int nowarn; /* don't report warnings? */
23
24 /* current sensor's perspective */
25 VIEW ourview = {VT_ANG,{0.,0.,0.},{0.,0.,1.},{1.,0.,0.},
26 1.,180.,180.,0.,0.,0.,0.,
27 {0.,0.,0.},{0.,0.,0.},0.,0.};
28
29 unsigned long nsamps = 10000; /* desired number of initial samples */
30 unsigned long nssamps = 9000; /* number of super-samples */
31 int ndsamps = 32; /* number of direct samples */
32 int nprocs = 1; /* number of rendering processes */
33
34 float *sensor = NULL; /* current sensor data */
35 int sntp[2]; /* number of sensor theta and phi angles */
36 float maxtheta; /* maximum theta value for this sensor */
37 float tvals[MAXNT+1]; /* theta values (1-D table of 1-cos(t)) */
38 float *pvals = NULL; /* phi values (2-D table in radians) */
39 int ntheta = 0; /* polar angle divisions */
40 int nphi = 0; /* azimuthal angle divisions */
41 double gscale = 1.; /* global scaling value */
42
43 #define s_theta(t) sensor[(t+1)*(sntp[1]+1)]
44 #define s_phi(p) sensor[(p)+1]
45 #define s_val(t,p) sensor[(p)+1+(t+1)*(sntp[1]+1)]
46
47 static void comp_sensor(char *sfile);
48
49 static void
50 over_options() /* overriding options */
51 {
52 directvis = (ndsamps <= 0);
53 do_irrad = 0;
54 }
55
56 static void
57 print_defaults() /* print out default parameters */
58 {
59 over_options();
60 printf("-n %-9d\t\t\t# number of processes\n", nprocs);
61 printf("-rd %-9ld\t\t\t# ray directions\n", nsamps);
62 /* printf("-rs %-9ld\t\t\t# ray super-samples\n", nssamps); */
63 printf("-dn %-9d\t\t\t# direct number of samples\n", ndsamps);
64 printf("-vp %f %f %f\t# view point\n",
65 ourview.vp[0], ourview.vp[1], ourview.vp[2]);
66 printf("-vd %f %f %f\t# view direction\n",
67 ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]);
68 printf("-vu %f %f %f\t# view up\n",
69 ourview.vup[0], ourview.vup[1], ourview.vup[2]);
70 printf("-vo %f\t\t\t# view fore clipping distance\n", ourview.vfore);
71 print_rdefaults();
72 }
73
74 int
75 main(
76 int argc,
77 char *argv[]
78 )
79 {
80 int doheader = 1;
81 int optwarn = 0;
82 int i, rval;
83
84 progname = argv[0];
85 /* set up rendering defaults */
86 rand_samp = 1;
87 dstrsrc = 0.65;
88 srcsizerat = 0.1;
89 directrelay = 3;
90 ambounce = 1;
91 maxdepth = -10;
92 /* get options from command line */
93 for (i = 1; i < argc; i++) {
94 while ((rval = expandarg(&argc, &argv, i)) > 0)
95 ;
96 if (rval < 0) {
97 sprintf(errmsg, "cannot expand '%s'", argv[i]);
98 error(SYSTEM, errmsg);
99 }
100 if (argv[i][0] != '-') {
101 if (i >= argc-1)
102 break; /* final octree argument */
103 if (!ray_pnprocs) {
104 over_options();
105 if (doheader) { /* print header */
106 newheader("RADIANCE", stdout);
107 printargs(argc, argv, stdout);
108 fputformat("ascii", stdout);
109 putchar('\n');
110 }
111 /* start process(es) */
112 ray_pinit(argv[argc-1], nprocs);
113 }
114 comp_sensor(argv[i]); /* process a sensor file */
115 continue;
116 }
117 if (argv[i][1] == 'r') { /* sampling options */
118 if (argv[i][2] == 'd')
119 nsamps = atol(argv[++i]);
120 else if (argv[i][2] == 's')
121 nssamps = atol(argv[++i]);
122 else {
123 sprintf(errmsg, "bad option at '%s'", argv[i]);
124 error(USER, errmsg);
125 }
126 continue;
127 }
128 /* direct component samples */
129 if (argv[i][1] == 'd' && argv[i][2] == 'n') {
130 ndsamps = atoi(argv[++i]);
131 continue;
132 }
133 if (argv[i][1] == 'v') { /* next sensor view */
134 if (argv[i][2] == 'f') {
135 rval = viewfile(argv[++i], &ourview, NULL);
136 if (rval < 0) {
137 sprintf(errmsg,
138 "cannot open view file \"%s\"",
139 argv[i]);
140 error(SYSTEM, errmsg);
141 } else if (rval == 0) {
142 sprintf(errmsg,
143 "bad view file \"%s\"",
144 argv[i]);
145 error(USER, errmsg);
146 }
147 continue;
148 }
149 rval = getviewopt(&ourview, argc-i, argv+i);
150 if (rval >= 0) {
151 i += rval;
152 continue;
153 }
154 sprintf(errmsg, "bad view option at '%s'", argv[i]);
155 error(USER, errmsg);
156 }
157 if (!strcmp(argv[i], "-w")) { /* toggle warnings */
158 nowarn = !nowarn;
159 continue;
160 }
161 if (ray_pnprocs) {
162 if (!optwarn++)
163 error(WARNING,
164 "rendering options should appear before first sensor");
165 } else if (!strcmp(argv[i], "-defaults")) {
166 print_defaults();
167 return(0);
168 }
169 if (argv[i][1] == 'h') { /* header toggle */
170 doheader = !doheader;
171 continue;
172 }
173 if (!strcmp(argv[i], "-n")) { /* number of processes */
174 nprocs = atoi(argv[++i]);
175 if (nprocs <= 0)
176 error(USER, "illegal number of processes");
177 continue;
178 }
179 rval = getrenderopt(argc-i, argv+i);
180 if (rval < 0) {
181 sprintf(errmsg, "bad render option at '%s'", argv[i]);
182 error(USER, errmsg);
183 }
184 i += rval;
185 }
186 if (!ray_pnprocs)
187 error(USER, i<argc ? "missing sensor file" : "missing octree");
188 quit(0);
189 }
190
191 /* Load sensor sensitivities (first row and column are angles) */
192 static float *
193 load_sensor(
194 int ntp[2],
195 char *sfile
196 )
197 {
198 char linebuf[8192];
199 int nelem = 1000;
200 float *sarr = (float *)malloc(sizeof(float)*nelem);
201 FILE *fp;
202 char *cp;
203 int i;
204
205 fp = frlibopen(sfile);
206 if (fp == NULL) {
207 sprintf(errmsg, "cannot open sensor file '%s'", sfile);
208 error(SYSTEM, errmsg);
209 }
210 fgets(linebuf, sizeof(linebuf), fp);
211 if (!strncmp(linebuf, "Elevation ", 10))
212 fgets(linebuf, sizeof(linebuf), fp);
213 /* get phi values */
214 sarr[0] = .0f;
215 if (strncmp(linebuf, "degrees", 7)) {
216 sprintf(errmsg, "Missing 'degrees' in sensor file '%s'", sfile);
217 error(USER, errmsg);
218 }
219 cp = sskip(linebuf);
220 ntp[1] = 0;
221 for ( ; ; ) {
222 sarr[ntp[1]+1] = atof(cp);
223 cp = fskip(cp);
224 if (cp == NULL)
225 break;
226 ++ntp[1];
227 }
228 ntp[0] = 0; /* get thetas + data */
229 while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
230 ++ntp[0];
231 if ((ntp[0]+1)*(ntp[1]+1) > nelem) {
232 nelem += (nelem>>2) + ntp[1];
233 sarr = (float *)realloc((void *)sarr,
234 sizeof(float)*nelem);
235 if (sarr == NULL)
236 error(SYSTEM, "out of memory in load_sensor()");
237 }
238 cp = linebuf;
239 i = ntp[0]*(ntp[1]+1);
240 for ( ; ; ) {
241 sarr[i] = atof(cp);
242 cp = fskip(cp);
243 if (cp == NULL)
244 break;
245 ++i;
246 }
247 if (i == ntp[0]*(ntp[1]+1))
248 break;
249 if (i != (ntp[0]+1)*(ntp[1]+1)) {
250 sprintf(errmsg,
251 "bad column count near line %d in sensor file '%s'",
252 ntp[0]+1, sfile);
253 error(USER, errmsg);
254 }
255 }
256 nelem = i;
257 fclose(fp);
258 errmsg[0] = '\0'; /* sanity checks */
259 if (ntp[0] <= 0)
260 sprintf(errmsg, "no data in sensor file '%s'", sfile);
261 else if (fabs(sarr[ntp[1]+1]) > FTINY)
262 sprintf(errmsg, "minimum theta must be 0 in sensor file '%s'",
263 sfile);
264 else if (fabs(sarr[1]) > FTINY)
265 sprintf(errmsg, "minimum phi must be 0 in sensor file '%s'",
266 sfile);
267 else if (sarr[ntp[1]] <= FTINY)
268 sprintf(errmsg,
269 "maximum phi must be positive in sensor file '%s'",
270 sfile);
271 else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY)
272 sprintf(errmsg,
273 "maximum theta must be positive in sensor file '%s'",
274 sfile);
275 if (errmsg[0])
276 error(USER, errmsg);
277 return((float *)realloc((void *)sarr, sizeof(float)*nelem));
278 }
279
280 /* Initialize probability table */
281 static void
282 init_ptable(
283 char *sfile
284 )
285 {
286 int samptot = nsamps;
287 float *rowp, *rowp1;
288 double rowsum[MAXNT], rowomega[MAXNT];
289 double thdiv[MAXNT+1], phdiv[MAXNP+1];
290 double tsize, psize;
291 double prob, frac, frac1;
292 int i, j, t, p;
293 /* free old table */
294 if (sensor != NULL)
295 free((void *)sensor);
296 if (pvals != NULL)
297 free((void *)pvals);
298 if (sfile == NULL || !*sfile) {
299 sensor = NULL;
300 sntp[0] = sntp[1] = 0;
301 pvals = NULL;
302 ntheta = nphi = 0;
303 return;
304 }
305 /* load sensor table */
306 sensor = load_sensor(sntp, sfile);
307 if (sntp[0] > MAXNT) {
308 sprintf(errmsg, "Too many theta rows in sensor file '%s'",
309 sfile);
310 error(INTERNAL, errmsg);
311 }
312 if (sntp[1] > MAXNP) {
313 sprintf(errmsg, "Too many phi columns in sensor file '%s'",
314 sfile);
315 error(INTERNAL, errmsg);
316 }
317 /* compute boundary angles */
318 maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2);
319 thdiv[0] = .0;
320 for (t = 1; t < sntp[0]; t++)
321 thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t));
322 thdiv[sntp[0]] = maxtheta*DEGREE;
323 phdiv[0] = .0;
324 for (p = 1; p < sntp[1]; p++)
325 phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p));
326 phdiv[sntp[1]] = 2.*PI;
327 /* size our table */
328 tsize = 1. - cos(maxtheta*DEGREE);
329 psize = PI*tsize/(maxtheta*DEGREE);
330 if (sntp[0]*sntp[1] < samptot) /* don't overdo resolution */
331 samptot = sntp[0]*sntp[1];
332 ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5);
333 if (ntheta > MAXNT)
334 ntheta = MAXNT;
335 nphi = samptot/ntheta;
336 pvals = (float *)malloc(sizeof(float)*ntheta*(nphi+1));
337 if (pvals == NULL)
338 error(SYSTEM, "out of memory in init_ptable()");
339 gscale = .0; /* compute our inverse table */
340 for (i = 0; i < sntp[0]; i++) {
341 rowp = &s_val(i,0);
342 rowsum[i] = 0.;
343 for (j = 0; j < sntp[1]; j++)
344 rowsum[i] += *rowp++;
345 rowomega[i] = cos(thdiv[i]) - cos(thdiv[i+1]);
346 rowomega[i] *= 2.*PI / (double)sntp[1];
347 gscale += rowsum[i] * rowomega[i];
348 }
349 for (i = 0; i < ntheta; i++) {
350 prob = (double)i / (double)ntheta;
351 for (t = 0; t < sntp[0]; t++)
352 if ((prob -= rowsum[t]*rowomega[t]/gscale) <= .0)
353 break;
354 if (t >= sntp[0])
355 error(INTERNAL, "code error 1 in init_ptable()");
356 frac = 1. + prob/(rowsum[t]*rowomega[t]/gscale);
357 tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) +
358 frac*cos(thdiv[t+1]) );
359 /* offset b/c sensor values are centered */
360 if (t <= 0 || frac > 0.5)
361 frac -= 0.5;
362 else if (t >= sntp[0]-1 || frac < 0.5) {
363 frac += 0.5;
364 --t;
365 }
366 pvals[i*(nphi+1)] = .0f;
367 for (j = 1; j < nphi; j++) {
368 prob = (double)j / (double)nphi;
369 rowp = &s_val(t,0);
370 rowp1 = &s_val(t+1,0);
371 for (p = 0; p < sntp[1]; p++) {
372 if ((prob -= (1.-frac)*rowp[p]/rowsum[t] +
373 frac*rowp1[p]/rowsum[t+1]) <= .0)
374 break;
375 if (p >= sntp[1])
376 error(INTERNAL,
377 "code error 2 in init_ptable()");
378 frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t]
379 + frac*rowp1[p]/rowsum[t+1]);
380 pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] +
381 frac1*phdiv[p+1];
382 }
383 }
384 pvals[i*(nphi+1) + nphi] = (float)(2.*PI);
385 }
386 tvals[0] = .0f;
387 tvals[ntheta] = (float)tsize;
388 }
389
390 /* Get normalized direction from random variables in [0,1) range */
391 static void
392 get_direc(
393 FVECT dvec,
394 double x,
395 double y
396 )
397 {
398 double xfrac = x*ntheta;
399 int tndx = (int)xfrac;
400 double yfrac = y*nphi;
401 int pndx = (int)yfrac;
402 double rad, phi;
403 FVECT dv;
404 int i;
405
406 xfrac -= (double)tndx;
407 yfrac -= (double)pndx;
408 pndx += tndx*(nphi+1);
409
410 dv[2] = 1. - ((1.-xfrac)*tvals[tndx] + xfrac*tvals[tndx+1]);
411 rad = sqrt(1. - dv[2]*dv[2]);
412 phi = (1.-yfrac)*pvals[pndx] + yfrac*pvals[pndx+1];
413 dv[0] = -rad*sin(phi);
414 dv[1] = rad*cos(phi);
415 for (i = 3; i--; )
416 dvec[i] = dv[0]*ourview.hvec[i] +
417 dv[1]*ourview.vvec[i] +
418 dv[2]*ourview.vdir[i] ;
419 }
420
421 /* Get sensor value in the specified direction (normalized) */
422 static float
423 sens_val(
424 FVECT dvec
425 )
426 {
427 FVECT dv;
428 float theta, phi;
429 int t, p;
430
431 dv[2] = DOT(dvec, ourview.vdir);
432 theta = (float)((1./DEGREE) * acos(dv[2]));
433 if (theta >= maxtheta)
434 return(.0f);
435 dv[0] = DOT(dvec, ourview.hvec);
436 dv[1] = DOT(dvec, ourview.vvec);
437 phi = (float)((1./DEGREE) * atan2(-dv[0], dv[1]));
438 while (phi < .0f) phi += 360.f;
439 t = (int)(theta/maxtheta * sntp[0]);
440 p = (int)(phi*(1./360.) * sntp[1]);
441 /* hack for non-uniform sensor grid */
442 while (t+1 < sntp[0] && theta >= s_theta(t+1))
443 ++t;
444 while (t-1 >= 0 && theta <= s_theta(t-1))
445 --t;
446 while (p+1 < sntp[1] && phi >= s_phi(p+1))
447 ++p;
448 while (p-1 >= 0 && phi <= s_phi(p-1))
449 --p;
450 return(s_val(t,p));
451 }
452
453 /* Compute sensor output */
454 static void
455 comp_sensor(
456 char *sfile
457 )
458 {
459 int ndirs = dstrsrc > FTINY ? ndsamps :
460 ndsamps > 0 ? 1 : 0;
461 char *err;
462 int nt, np;
463 COLOR vsum;
464 RAY rr;
465 double sf;
466 int i, j;
467 /* set view */
468 ourview.type = VT_ANG;
469 ourview.horiz = ourview.vert = 180.;
470 ourview.hoff = ourview.voff = .0;
471 err = setview(&ourview);
472 if (err != NULL)
473 error(USER, err);
474 /* assign probability table */
475 init_ptable(sfile);
476 /* stratified MC sampling */
477 setcolor(vsum, .0f, .0f, .0f);
478 nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5);
479 np = nsamps/nt;
480 sf = gscale/nsamps;
481 for (i = 0; i < nt; i++)
482 for (j = 0; j < np; j++) {
483 VCOPY(rr.rorg, ourview.vp);
484 get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np);
485 if (ourview.vfore > FTINY)
486 VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore);
487 rr.rmax = .0;
488 rayorigin(&rr, PRIMARY, NULL, NULL);
489 scalecolor(rr.rcoef, sf);
490 if (ray_pqueue(&rr) == 1)
491 addcolor(vsum, rr.rcol);
492 }
493 /* remaining rays pure MC */
494 for (i = nsamps - nt*np; i-- > 0; ) {
495 VCOPY(rr.rorg, ourview.vp);
496 get_direc(rr.rdir, frandom(), frandom());
497 if (ourview.vfore > FTINY)
498 VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore);
499 rr.rmax = .0;
500 rayorigin(&rr, PRIMARY, NULL, NULL);
501 scalecolor(rr.rcoef, sf);
502 if (ray_pqueue(&rr) == 1)
503 addcolor(vsum, rr.rcol);
504 }
505 /* scale partial result */
506 scalecolor(vsum, sf);
507 /* add direct component */
508 for (i = ndirs; i-- > 0; ) {
509 SRCINDEX si;
510 initsrcindex(&si);
511 while (srcray(&rr, NULL, &si)) {
512 sf = sens_val(rr.rdir);
513 if (sf <= FTINY)
514 continue;
515 sf *= si.dom/ndirs;
516 scalecolor(rr.rcoef, sf);
517 if (ray_pqueue(&rr) == 1) {
518 multcolor(rr.rcol, rr.rcoef);
519 addcolor(vsum, rr.rcol);
520 }
521 }
522 }
523 /* finish our calculation */
524 while (ray_presult(&rr, 0) > 0) {
525 multcolor(rr.rcol, rr.rcoef);
526 addcolor(vsum, rr.rcol);
527 }
528 /* print our result */
529 printf("%.4e %.4e %.4e\n", colval(vsum,RED),
530 colval(vsum,GRN), colval(vsum,BLU));
531 }