ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rsensor.c
Revision: 2.9
Committed: Tue May 17 19:34:36 2011 UTC (12 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.8: +40 -21 lines
Log Message:
Fixes pointed out by David G-M

File Contents

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