ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rsensor.c
Revision: 2.8
Committed: Sun Sep 26 15:41:46 2010 UTC (13 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +26 -3 lines
Log Message:
Added ability to output sample rays rather than compute results

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.8 static const char RCSid[] = "$Id: rsensor.c,v 2.7 2009/12/12 19:01:00 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     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 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     ++ntp[1];
239     }
240     ntp[0] = 0; /* get thetas + data */
241     while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
242     ++ntp[0];
243     if ((ntp[0]+1)*(ntp[1]+1) > nelem) {
244     nelem += (nelem>>2) + ntp[1];
245     sarr = (float *)realloc((void *)sarr,
246     sizeof(float)*nelem);
247     if (sarr == NULL)
248     error(SYSTEM, "out of memory in load_sensor()");
249     }
250     cp = linebuf;
251     i = ntp[0]*(ntp[1]+1);
252     for ( ; ; ) {
253     sarr[i] = atof(cp);
254     cp = fskip(cp);
255     if (cp == NULL)
256     break;
257     ++i;
258     }
259     if (i == ntp[0]*(ntp[1]+1))
260     break;
261     if (i != (ntp[0]+1)*(ntp[1]+1)) {
262     sprintf(errmsg,
263     "bad column count near line %d in sensor file '%s'",
264     ntp[0]+1, sfile);
265     error(USER, errmsg);
266     }
267     }
268     nelem = i;
269     fclose(fp);
270     errmsg[0] = '\0'; /* sanity checks */
271     if (ntp[0] <= 0)
272     sprintf(errmsg, "no data in sensor file '%s'", sfile);
273     else if (fabs(sarr[ntp[1]+1]) > FTINY)
274     sprintf(errmsg, "minimum theta must be 0 in sensor file '%s'",
275     sfile);
276     else if (fabs(sarr[1]) > FTINY)
277     sprintf(errmsg, "minimum phi must be 0 in sensor file '%s'",
278     sfile);
279     else if (sarr[ntp[1]] <= FTINY)
280     sprintf(errmsg,
281     "maximum phi must be positive in sensor file '%s'",
282     sfile);
283     else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY)
284     sprintf(errmsg,
285     "maximum theta must be positive in sensor file '%s'",
286     sfile);
287     if (errmsg[0])
288     error(USER, errmsg);
289     return((float *)realloc((void *)sarr, sizeof(float)*nelem));
290     }
291    
292     /* Initialize probability table */
293     static void
294     init_ptable(
295     char *sfile
296     )
297     {
298     int samptot = nsamps;
299     float *rowp, *rowp1;
300     double rowsum[MAXNT], rowomega[MAXNT];
301     double thdiv[MAXNT+1], phdiv[MAXNP+1];
302     double tsize, psize;
303     double prob, frac, frac1;
304     int i, j, t, p;
305     /* free old table */
306     if (sensor != NULL)
307     free((void *)sensor);
308     if (pvals != NULL)
309     free((void *)pvals);
310     if (sfile == NULL || !*sfile) {
311 greg 2.2 sensor = NULL;
312     sntp[0] = sntp[1] = 0;
313 greg 2.1 pvals = NULL;
314     ntheta = nphi = 0;
315     return;
316     }
317     /* load sensor table */
318     sensor = load_sensor(sntp, sfile);
319     if (sntp[0] > MAXNT) {
320     sprintf(errmsg, "Too many theta rows in sensor file '%s'",
321     sfile);
322     error(INTERNAL, errmsg);
323     }
324     if (sntp[1] > MAXNP) {
325     sprintf(errmsg, "Too many phi columns in sensor file '%s'",
326     sfile);
327     error(INTERNAL, errmsg);
328     }
329     /* compute boundary angles */
330 greg 2.2 maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2);
331 greg 2.1 thdiv[0] = .0;
332     for (t = 1; t < sntp[0]; t++)
333 greg 2.2 thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t));
334 greg 2.1 thdiv[sntp[0]] = maxtheta*DEGREE;
335     phdiv[0] = .0;
336     for (p = 1; p < sntp[1]; p++)
337 greg 2.2 phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p));
338 greg 2.1 phdiv[sntp[1]] = 2.*PI;
339     /* size our table */
340     tsize = 1. - cos(maxtheta*DEGREE);
341 greg 2.2 psize = PI*tsize/(maxtheta*DEGREE);
342 greg 2.1 if (sntp[0]*sntp[1] < samptot) /* don't overdo resolution */
343     samptot = sntp[0]*sntp[1];
344 greg 2.2 ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5);
345 greg 2.1 if (ntheta > MAXNT)
346     ntheta = MAXNT;
347     nphi = samptot/ntheta;
348     pvals = (float *)malloc(sizeof(float)*ntheta*(nphi+1));
349     if (pvals == NULL)
350     error(SYSTEM, "out of memory in init_ptable()");
351     gscale = .0; /* compute our inverse table */
352     for (i = 0; i < sntp[0]; i++) {
353 greg 2.2 rowp = &s_val(i,0);
354 greg 2.1 rowsum[i] = 0.;
355     for (j = 0; j < sntp[1]; j++)
356     rowsum[i] += *rowp++;
357     rowomega[i] = cos(thdiv[i]) - cos(thdiv[i+1]);
358     rowomega[i] *= 2.*PI / (double)sntp[1];
359     gscale += rowsum[i] * rowomega[i];
360     }
361 greg 2.2 for (i = 0; i < ntheta; i++) {
362 greg 2.1 prob = (double)i / (double)ntheta;
363     for (t = 0; t < sntp[0]; t++)
364     if ((prob -= rowsum[t]*rowomega[t]/gscale) <= .0)
365     break;
366     if (t >= sntp[0])
367     error(INTERNAL, "code error 1 in init_ptable()");
368     frac = 1. + prob/(rowsum[t]*rowomega[t]/gscale);
369     tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) +
370     frac*cos(thdiv[t+1]) );
371 greg 2.2 /* offset b/c sensor values are centered */
372     if (t <= 0 || frac > 0.5)
373     frac -= 0.5;
374     else if (t >= sntp[0]-1 || frac < 0.5) {
375     frac += 0.5;
376     --t;
377     }
378 greg 2.1 pvals[i*(nphi+1)] = .0f;
379     for (j = 1; j < nphi; j++) {
380     prob = (double)j / (double)nphi;
381 greg 2.2 rowp = &s_val(t,0);
382     rowp1 = &s_val(t+1,0);
383 greg 2.1 for (p = 0; p < sntp[1]; p++) {
384 greg 2.2 if ((prob -= (1.-frac)*rowp[p]/rowsum[t] +
385     frac*rowp1[p]/rowsum[t+1]) <= .0)
386 greg 2.1 break;
387     if (p >= sntp[1])
388     error(INTERNAL,
389     "code error 2 in init_ptable()");
390 greg 2.2 frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t]
391     + frac*rowp1[p]/rowsum[t+1]);
392 greg 2.1 pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] +
393     frac1*phdiv[p+1];
394     }
395     }
396     pvals[i*(nphi+1) + nphi] = (float)(2.*PI);
397     }
398 greg 2.2 tvals[0] = .0f;
399 greg 2.1 tvals[ntheta] = (float)tsize;
400     }
401    
402     /* Get normalized direction from random variables in [0,1) range */
403     static void
404     get_direc(
405     FVECT dvec,
406     double x,
407     double y
408     )
409     {
410     double xfrac = x*ntheta;
411     int tndx = (int)xfrac;
412     double yfrac = y*nphi;
413     int pndx = (int)yfrac;
414     double rad, phi;
415     FVECT dv;
416     int i;
417    
418     xfrac -= (double)tndx;
419     yfrac -= (double)pndx;
420     pndx += tndx*(nphi+1);
421    
422     dv[2] = 1. - ((1.-xfrac)*tvals[tndx] + xfrac*tvals[tndx+1]);
423     rad = sqrt(1. - dv[2]*dv[2]);
424     phi = (1.-yfrac)*pvals[pndx] + yfrac*pvals[pndx+1];
425     dv[0] = -rad*sin(phi);
426     dv[1] = rad*cos(phi);
427     for (i = 3; i--; )
428     dvec[i] = dv[0]*ourview.hvec[i] +
429     dv[1]*ourview.vvec[i] +
430     dv[2]*ourview.vdir[i] ;
431     }
432    
433     /* Get sensor value in the specified direction (normalized) */
434     static float
435     sens_val(
436     FVECT dvec
437     )
438     {
439     FVECT dv;
440     float theta, phi;
441     int t, p;
442    
443     dv[2] = DOT(dvec, ourview.vdir);
444     theta = (float)((1./DEGREE) * acos(dv[2]));
445     if (theta >= maxtheta)
446     return(.0f);
447     dv[0] = DOT(dvec, ourview.hvec);
448     dv[1] = DOT(dvec, ourview.vvec);
449     phi = (float)((1./DEGREE) * atan2(-dv[0], dv[1]));
450     while (phi < .0f) phi += 360.f;
451     t = (int)(theta/maxtheta * sntp[0]);
452     p = (int)(phi*(1./360.) * sntp[1]);
453     /* hack for non-uniform sensor grid */
454 greg 2.2 while (t+1 < sntp[0] && theta >= s_theta(t+1))
455 greg 2.1 ++t;
456 greg 2.2 while (t-1 >= 0 && theta <= s_theta(t-1))
457 greg 2.1 --t;
458 greg 2.2 while (p+1 < sntp[1] && phi >= s_phi(p+1))
459 greg 2.1 ++p;
460 greg 2.2 while (p-1 >= 0 && phi <= s_phi(p-1))
461 greg 2.1 --p;
462 greg 2.2 return(s_val(t,p));
463 greg 2.1 }
464    
465 greg 2.8 /* Print origin and direction */
466     static void
467     print_ray(
468     FVECT rorg,
469     FVECT rdir
470     )
471     {
472     printf("%.6g %.6g %.6g %.8f %.8f %.8f\n",
473     rorg[0], rorg[1], rorg[2],
474     rdir[0], rdir[1], rdir[2]);
475     }
476    
477 greg 2.1 /* Compute sensor output */
478     static void
479     comp_sensor(
480     char *sfile
481     )
482     {
483     int ndirs = dstrsrc > FTINY ? ndsamps :
484     ndsamps > 0 ? 1 : 0;
485     char *err;
486     int nt, np;
487     COLOR vsum;
488     RAY rr;
489 greg 2.6 double sf;
490 greg 2.1 int i, j;
491     /* set view */
492     ourview.type = VT_ANG;
493     ourview.horiz = ourview.vert = 180.;
494     ourview.hoff = ourview.voff = .0;
495     err = setview(&ourview);
496     if (err != NULL)
497     error(USER, err);
498     /* assign probability table */
499     init_ptable(sfile);
500 greg 2.6 /* stratified MC sampling */
501 greg 2.1 setcolor(vsum, .0f, .0f, .0f);
502     nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5);
503     np = nsamps/nt;
504 greg 2.6 sf = gscale/nsamps;
505 greg 2.1 for (i = 0; i < nt; i++)
506 greg 2.2 for (j = 0; j < np; j++) {
507 greg 2.5 VCOPY(rr.rorg, ourview.vp);
508 greg 2.2 get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np);
509 greg 2.5 if (ourview.vfore > FTINY)
510     VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore);
511 greg 2.8 if (!ray_pnprocs) {
512     print_ray(rr.rorg, rr.rdir);
513     continue;
514     }
515 greg 2.5 rr.rmax = .0;
516 greg 2.1 rayorigin(&rr, PRIMARY, NULL, NULL);
517 greg 2.6 scalecolor(rr.rcoef, sf);
518 greg 2.1 if (ray_pqueue(&rr) == 1)
519     addcolor(vsum, rr.rcol);
520     }
521 greg 2.6 /* remaining rays pure MC */
522     for (i = nsamps - nt*np; i-- > 0; ) {
523     VCOPY(rr.rorg, ourview.vp);
524     get_direc(rr.rdir, frandom(), frandom());
525     if (ourview.vfore > FTINY)
526     VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore);
527 greg 2.8 if (!ray_pnprocs) {
528     print_ray(rr.rorg, rr.rdir);
529     continue;
530     }
531 greg 2.6 rr.rmax = .0;
532     rayorigin(&rr, PRIMARY, NULL, NULL);
533     scalecolor(rr.rcoef, sf);
534     if (ray_pqueue(&rr) == 1)
535     addcolor(vsum, rr.rcol);
536     }
537 greg 2.8 if (!ray_pnprocs) /* just printing rays */
538     return;
539 greg 2.6 /* scale partial result */
540     scalecolor(vsum, sf);
541     /* add direct component */
542 greg 2.1 for (i = ndirs; i-- > 0; ) {
543     SRCINDEX si;
544     initsrcindex(&si);
545     while (srcray(&rr, NULL, &si)) {
546 greg 2.6 sf = sens_val(rr.rdir);
547     if (sf <= FTINY)
548 greg 2.1 continue;
549 greg 2.6 sf *= si.dom/ndirs;
550     scalecolor(rr.rcoef, sf);
551 greg 2.1 if (ray_pqueue(&rr) == 1) {
552     multcolor(rr.rcol, rr.rcoef);
553     addcolor(vsum, rr.rcol);
554     }
555     }
556     }
557 greg 2.6 /* finish our calculation */
558 greg 2.1 while (ray_presult(&rr, 0) > 0) {
559     multcolor(rr.rcol, rr.rcoef);
560     addcolor(vsum, rr.rcol);
561     }
562     /* print our result */
563     printf("%.4e %.4e %.4e\n", colval(vsum,RED),
564     colval(vsum,GRN), colval(vsum,BLU));
565     }