ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rsensor.c
(Generate patch)

Comparing ray/src/util/rsensor.c (file contents):
Revision 2.2 by greg, Thu Feb 21 20:18:25 2008 UTC vs.
Revision 2.9 by greg, Tue May 17 19:34:36 2011 UTC

# Line 22 | Line 22 | extern char    *progname;      /* global argv[0] */
22   extern int      nowarn;         /* don't report warnings? */
23  
24                                  /* current sensor's perspective */
25 < VIEW            ourview = STDVIEW;
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 */
# Line 32 | Line 34 | int            nprocs = 1;     /* number of rendering processes
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) */
37 > 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   int             ntheta = 0;     /* polar angle divisions */
40   int             nphi = 0;       /* azimuthal angle divisions */
41   double          gscale = 1.;    /* global scaling value */
# Line 45 | Line 47 | double         gscale = 1.;    /* global scaling value */
47   static void     comp_sensor(char *sfile);
48  
49   static void
50 < print_defaults()
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); */
# Line 61 | Line 71 | print_defaults()
71          print_rdefaults();
72   }
73  
74 +
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   int
86   main(
87          int     argc,
# Line 68 | Line 89 | main(
89   )
90   {
91          int     doheader = 1;
92 +        int     optwarn = 0;
93          int     i, rval;
94  
95          progname = argv[0];
96                                  /* set up rendering defaults */
97          rand_samp = 1;
98 <        dstrsrc = 0.5;
98 >        dstrsrc = 0.65;
99          srcsizerat = 0.1;
100          directrelay = 3;
101          ambounce = 1;
102          maxdepth = -10;
81                                /* just asking defaults? */
82        if (argc == 2 && !strcmp(argv[1], "-defaults")) {
83                print_defaults();
84                return(0);
85        }
86                                /* check octree */
87        if (argc < 2 || argv[argc-1][0] == '-')
88                error(USER, "missing octree argument");
103                                  /* get options from command line */
104 <        for (i = 1; i < argc-1; i++) {
104 >        for (i = 1; i < argc; i++) {
105                  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 <                if (argv[i][0] != '-') {        /* process a sensor file */
111 >                if (argv[i][0] != '-') {
112 >                        if (i >= argc-1)
113 >                                break;          /* final octree argument */
114                          if (!ray_pnprocs) {
115 <                                                /* overriding options */
100 <                                directvis = (ndsamps <= 0);
101 <                                do_irrad = 0;
115 >                                over_options();
116                                  if (doheader) { /* print header */
117 +                                        newheader("RADIANCE", stdout);
118                                          printargs(argc, argv, stdout);
119                                          fputformat("ascii", stdout);
120                                          putchar('\n');
121                                  }
122                                                  /* start process(es) */
123 <                                ray_pinit(argv[argc-1], nprocs);
123 >                                if (strcmp(argv[argc-1], "."))
124 >                                        ray_pinit(argv[argc-1], nprocs);
125                          }
126 <                        comp_sensor(argv[i]);
126 >                        comp_sensor(argv[i]);   /* process a sensor file */
127                          continue;
128                  }
129                  if (argv[i][1] == 'r') {        /* sampling options */
# Line 150 | Line 166 | main(
166                          sprintf(errmsg, "bad view option at '%s'", argv[i]);
167                          error(USER, errmsg);
168                  }
169 <                if (!strcmp(argv[i], "-w")) {   /* turn off warnings */
170 <                        nowarn = 1;
169 >                if (!strcmp(argv[i], "-w")) {   /* toggle warnings */
170 >                        nowarn = !nowarn;
171                          continue;
172                  }
173                  if (ray_pnprocs) {
174 <                        error(WARNING,
174 >                        if (!optwarn++)
175 >                                error(WARNING,
176                          "rendering options should appear before first sensor");
177                  } else if (!strcmp(argv[i], "-defaults")) {
178                          print_defaults();
# Line 178 | Line 195 | main(
195                  }
196                  i += rval;
197          }
198 +        if (sensor == NULL)
199 +                error(USER, i<argc ? "missing sensor file" : "missing octree");
200          quit(0);
201   }
202  
# Line 216 | Line 235 | load_sensor(
235                  cp = fskip(cp);
236                  if (cp == NULL)
237                          break;
238 +                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                  ++ntp[1];
245          }
246          ntp[0] = 0;                             /* get thetas + data */
# Line 239 | Line 264 | load_sensor(
264                  }
265                  if (i == ntp[0]*(ntp[1]+1))
266                          break;
267 +                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                  if (i != (ntp[0]+1)*(ntp[1]+1)) {
275                          sprintf(errmsg,
276                          "bad column count near line %d in sensor file '%s'",
# Line 308 | Line 340 | init_ptable(
340                  error(INTERNAL, errmsg);
341          }
342                                          /* compute boundary angles */
343 <        maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2);
343 >        maxtheta = DEGREE*(1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2));
344 >        if (maxtheta > PI)
345 >                maxtheta = PI;
346          thdiv[0] = .0;
347          for (t = 1; t < sntp[0]; t++)
348                  thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t));
349 <        thdiv[sntp[0]] = maxtheta*DEGREE;
350 <        phdiv[0] = .0;
349 >        thdiv[sntp[0]] = maxtheta;
350 >        phdiv[0] = DEGREE*(1.5f*s_phi(0) - 0.5f*s_phi(1));
351          for (p = 1; p < sntp[1]; p++)
352                  phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p));
353 <        phdiv[sntp[1]] = 2.*PI;
353 >        phdiv[sntp[1]] = DEGREE*(1.5f*s_phi(sntp[1]-1) - 0.5f*s_phi(sntp[1]-2));
354                                          /* size our table */
355 <        tsize = 1. - cos(maxtheta*DEGREE);
356 <        psize = PI*tsize/(maxtheta*DEGREE);
355 >        tsize = 1. - cos(maxtheta);
356 >        psize = PI*tsize/maxtheta;
357          if (sntp[0]*sntp[1] < samptot)  /* don't overdo resolution */
358                  samptot = sntp[0]*sntp[1];
359          ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5);
360          if (ntheta > MAXNT)
361                  ntheta = MAXNT;
362          nphi = samptot/ntheta;
363 <        pvals = (float *)malloc(sizeof(float)*ntheta*(nphi+1));
363 >        pvals = (float *)malloc(sizeof(float)*(ntheta+1)*(nphi+1));
364          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                  rowp = &s_val(i,0);
369 <                rowsum[i] = 0.;
369 >                rowsum[i] = 1e-20;
370                  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 +        if (gscale <= FTINY) {
377 +                sprintf(errmsg, "Sensor values sum to zero in file '%s'", sfile);
378 +                error(USER, errmsg);
379 +        }
380          for (i = 0; i < ntheta; i++) {
381                  prob = (double)i / (double)ntheta;
382                  for (t = 0; t < sntp[0]; t++)
# Line 356 | Line 394 | init_ptable(
394                          frac += 0.5;
395                          --t;
396                  }
397 <                pvals[i*(nphi+1)] = .0f;
397 >                pvals[i*(nphi+1)] = phdiv[0];
398                  for (j = 1; j < nphi; j++) {
399                          prob = (double)j / (double)nphi;
400                          rowp = &s_val(t,0);
401                          rowp1 = &s_val(t+1,0);
402 <                        for (p = 0; p < sntp[1]; p++) {
402 >                        for (p = 0; p < sntp[1]; p++)
403                                  if ((prob -= (1.-frac)*rowp[p]/rowsum[t] +
404                                              frac*rowp1[p]/rowsum[t+1]) <= .0)
405                                          break;
406 <                                if (p >= sntp[1])
407 <                                        error(INTERNAL,
408 <                                            "code error 2 in init_ptable()");
371 <                                frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t]
372 <                                                + frac*rowp1[p]/rowsum[t+1]);
373 <                                if (p <= 0 || frac1 > 0.5)
374 <                                        frac1 -= 0.5;
375 <                                else if (p >= sntp[1]-1 || frac1 < 0.5) {
376 <                                        frac1 += 0.5;
377 <                                        --p;
378 <                                }
379 <                                pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] +
380 <                                                        frac1*phdiv[p+1];
406 >                        if (p >= sntp[1]) {
407 >                                p = sntp[1] - 1;
408 >                                prob = .5;
409                          }
410 +                        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                  }
415 <                pvals[i*(nphi+1) + nphi] = (float)(2.*PI);
415 >                pvals[i*(nphi+1) + nphi] = phdiv[sntp[1]];
416          }
417          tvals[0] = .0f;
418          tvals[ntheta] = (float)tsize;
# Line 449 | Line 481 | sens_val(
481          return(s_val(t,p));
482   }
483  
484 + /* 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   /* Compute sensor output */
497   static void
498   comp_sensor(
# Line 461 | Line 505 | comp_sensor(
505          int     nt, np;
506          COLOR   vsum;
507          RAY     rr;
508 +        double  sf;
509          int     i, j;
510                                                  /* set view */
511          ourview.type = VT_ANG;
# Line 471 | Line 516 | comp_sensor(
516                  error(USER, err);
517                                                  /* assign probability table */
518          init_ptable(sfile);
519 <                                                /* do Monte Carlo sampling */
519 >                                                /* stratified MC sampling */
520          setcolor(vsum, .0f, .0f, .0f);
521          nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5);
522          np = nsamps/nt;
523 <        VCOPY(rr.rorg, ourview.vp);
479 <        rr.rmax = .0;
523 >        sf = gscale/nsamps;
524          for (i = 0; i < nt; i++)
525                  for (j = 0; j < np; j++) {
526 +                        VCOPY(rr.rorg, ourview.vp);
527                          get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np);
528 +                        if (ourview.vfore > FTINY)
529 +                                VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore);
530 +                        if (!ray_pnprocs) {
531 +                                print_ray(rr.rorg, rr.rdir);
532 +                                continue;
533 +                        }
534 +                        rr.rmax = .0;
535                          rayorigin(&rr, PRIMARY, NULL, NULL);
536 +                        scalecolor(rr.rcoef, sf);
537                          if (ray_pqueue(&rr) == 1)
538                                  addcolor(vsum, rr.rcol);
539                  }
540 <                                                /* finish MC calculation */
541 <        while (ray_presult(&rr, 0) > 0)
542 <                addcolor(vsum, rr.rcol);
543 <        scalecolor(vsum, gscale/(nt*np));
544 <                                                /* compute direct component */
540 >                                                /* 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 >                if (!ray_pnprocs) {
547 >                        print_ray(rr.rorg, rr.rdir);
548 >                        continue;
549 >                }
550 >                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 >        if (!ray_pnprocs)                       /* just printing rays */
557 >                return;
558 >                                                /* scale partial result */
559 >        scalecolor(vsum, sf);
560 >                                                /* add direct component */
561          for (i = ndirs; i-- > 0; ) {
562                  SRCINDEX        si;
563                  initsrcindex(&si);
564                  while (srcray(&rr, NULL, &si)) {
565 <                        double  d = sens_val(rr.rdir);
566 <                        if (d <= FTINY)
565 >                        sf = sens_val(rr.rdir);
566 >                        if (sf <= FTINY)
567                                  continue;
568 <                        d *= si.dom/ndirs;
569 <                        scalecolor(rr.rcoef, d);
568 >                        sf *= si.dom/ndirs;
569 >                        scalecolor(rr.rcoef, sf);
570                          if (ray_pqueue(&rr) == 1) {
571                                  multcolor(rr.rcol, rr.rcoef);
572                                  addcolor(vsum, rr.rcol);
573                          }
574                  }
575          }
576 <                                                /* finish direct calculation */
576 >                                                /* finish our calculation */
577          while (ray_presult(&rr, 0) > 0) {
578                  multcolor(rr.rcol, rr.rcoef);
579                  addcolor(vsum, rr.rcol);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines