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.16 by greg, Thu Apr 23 23:54:08 2015 UTC

# Line 15 | Line 15 | static const char RCSid[] = "$Id$";
15  
16   #define DEGREE          (PI/180.)
17  
18 < #define MAXNT           180     /* maximum number of theta divisions */
18 > #define MAXNT           181     /* 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 = 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 188 | Line 207 | load_sensor(
207          char    *sfile
208   )
209   {
210 +        int     warnedneg;
211          char    linebuf[8192];
212 +        int     last_pos_val = 0;
213          int     nelem = 1000;
214          float   *sarr = (float *)malloc(sizeof(float)*nelem);
215          FILE    *fp;
# Line 216 | Line 237 | load_sensor(
237                  cp = fskip(cp);
238                  if (cp == NULL)
239                          break;
240 +                if (ntp[1] > 1 && sarr[ntp[1]+1] <= sarr[ntp[1]]) {
241 +                        sprintf(errmsg,
242 +                "Phi values not monotinically increasing in sensor file '%s'",
243 +                                        sfile);
244 +                        error(USER, errmsg);
245 +                }
246                  ++ntp[1];
247          }
248 +        warnedneg = 0;
249          ntp[0] = 0;                             /* get thetas + data */
250          while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
251                  ++ntp[0];
# Line 235 | Line 263 | load_sensor(
263                          cp = fskip(cp);
264                          if (cp == NULL)
265                                  break;
266 +                        if (i && sarr[i] < .0) {
267 +                                if (!warnedneg++) {
268 +                                        sprintf(errmsg,
269 +                "Negative value(s) in sensor file '%s' (ignored)\n", sfile);
270 +                                        error(WARNING, errmsg);
271 +                                }
272 +                                sarr[i] = .0;
273 +                        } else if (i > ntp[0]*(ntp[1]+1) && sarr[i] > FTINY)
274 +                                last_pos_val = i;
275                          ++i;
276                  }
277                  if (i == ntp[0]*(ntp[1]+1))
278                          break;
279 +                if (ntp[0] > 1 && sarr[ntp[0]*(ntp[1]+1)] <=
280 +                                        sarr[(ntp[0]-1)*(ntp[1]+1)]) {
281 +                        sprintf(errmsg,
282 +                "Theta values not monotinically increasing in sensor file '%s'",
283 +                                        sfile);
284 +                        error(USER, errmsg);
285 +                }
286                  if (i != (ntp[0]+1)*(ntp[1]+1)) {
287                          sprintf(errmsg,
288                          "bad column count near line %d in sensor file '%s'",
# Line 246 | Line 290 | load_sensor(
290                          error(USER, errmsg);
291                  }
292          }
293 <        nelem = i;
293 >                                                /* truncate zero region */
294 >        ntp[0] = (last_pos_val + ntp[1])/(ntp[1]+1) - 1;
295 >        nelem = (ntp[0]+1)*(ntp[1]+1);
296          fclose(fp);
297          errmsg[0] = '\0';                       /* sanity checks */
298 +        if (!last_pos_val)
299 +                sprintf(errmsg, "no positive sensor values in file '%s'", sfile);
300          if (ntp[0] <= 0)
301                  sprintf(errmsg, "no data in sensor file '%s'", sfile);
302          else if (fabs(sarr[ntp[1]+1]) > FTINY)
# Line 263 | Line 311 | load_sensor(
311                                  sfile);
312          else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY)
313                  sprintf(errmsg,
314 <                        "maximum theta must be positive in sensor file '%s'",
315 <                                sfile);
314 >                        "maximum theta (%f) must be positive in sensor file '%s'",
315 >                                sarr[ntp[0]*(ntp[1]+1)], sfile);
316          if (errmsg[0])
317                  error(USER, errmsg);
318          return((float *)realloc((void *)sarr, sizeof(float)*nelem));
# Line 308 | Line 356 | init_ptable(
356                  error(INTERNAL, errmsg);
357          }
358                                          /* compute boundary angles */
359 <        maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2);
359 >        maxtheta = DEGREE*(1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2));
360 >        if (maxtheta > PI)
361 >                maxtheta = PI;
362          thdiv[0] = .0;
363          for (t = 1; t < sntp[0]; t++)
364                  thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t));
365 <        thdiv[sntp[0]] = maxtheta*DEGREE;
366 <        phdiv[0] = .0;
365 >        thdiv[sntp[0]] = maxtheta;
366 >        phdiv[0] = DEGREE*(1.5f*s_phi(0) - 0.5f*s_phi(1));
367          for (p = 1; p < sntp[1]; p++)
368                  phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p));
369 <        phdiv[sntp[1]] = 2.*PI;
369 >        phdiv[sntp[1]] = DEGREE*(1.5f*s_phi(sntp[1]-1) - 0.5f*s_phi(sntp[1]-2));
370                                          /* size our table */
371 <        tsize = 1. - cos(maxtheta*DEGREE);
372 <        psize = PI*tsize/(maxtheta*DEGREE);
371 >        tsize = 1. - cos(maxtheta);
372 >        psize = PI*tsize/maxtheta;
373          if (sntp[0]*sntp[1] < samptot)  /* don't overdo resolution */
374                  samptot = sntp[0]*sntp[1];
375          ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5);
376          if (ntheta > MAXNT)
377                  ntheta = MAXNT;
378          nphi = samptot/ntheta;
379 <        pvals = (float *)malloc(sizeof(float)*ntheta*(nphi+1));
379 >        pvals = (float *)malloc(sizeof(float)*(ntheta+1)*(nphi+1));
380          if (pvals == NULL)
381                  error(SYSTEM, "out of memory in init_ptable()");
382          gscale = .0;                    /* compute our inverse table */
383          for (i = 0; i < sntp[0]; i++) {
384                  rowp = &s_val(i,0);
385 <                rowsum[i] = 0.;
385 >                rowsum[i] = 1e-20;
386                  for (j = 0; j < sntp[1]; j++)
387                          rowsum[i] += *rowp++;
388                  rowomega[i] = cos(thdiv[i]) - cos(thdiv[i+1]);
389                  rowomega[i] *= 2.*PI / (double)sntp[1];
390                  gscale += rowsum[i] * rowomega[i];
391          }
392 +        if (gscale <= FTINY) {
393 +                sprintf(errmsg, "Sensor values sum to zero in file '%s'", sfile);
394 +                error(USER, errmsg);
395 +        }
396          for (i = 0; i < ntheta; i++) {
397                  prob = (double)i / (double)ntheta;
398                  for (t = 0; t < sntp[0]; t++)
# Line 350 | Line 404 | init_ptable(
404                  tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) +
405                                                  frac*cos(thdiv[t+1]) );
406                                  /* offset b/c sensor values are centered */
407 <                if (t <= 0 || frac > 0.5)
407 >                if ((t < sntp[0]-1) & (!t | (frac >= 0.5))) {
408                          frac -= 0.5;
409 <                else if (t >= sntp[0]-1 || frac < 0.5) {
409 >                } else {
410                          frac += 0.5;
411                          --t;
412                  }
413 <                pvals[i*(nphi+1)] = .0f;
413 >                pvals[i*(nphi+1)] = phdiv[0];
414                  for (j = 1; j < nphi; j++) {
415                          prob = (double)j / (double)nphi;
416                          rowp = &s_val(t,0);
417                          rowp1 = &s_val(t+1,0);
418 <                        for (p = 0; p < sntp[1]; p++) {
418 >                        for (p = 0; p < sntp[1]; p++)
419                                  if ((prob -= (1.-frac)*rowp[p]/rowsum[t] +
420                                              frac*rowp1[p]/rowsum[t+1]) <= .0)
421                                          break;
422 <                                if (p >= sntp[1])
423 <                                        error(INTERNAL,
424 <                                            "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];
422 >                        if (p >= sntp[1]) {     /* should never happen? */
423 >                                p = sntp[1] - 1;
424 >                                prob = .5;
425                          }
426 +                        frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t]
427 +                                        + frac*rowp1[p]/rowsum[t+1]);
428 +                        pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] +
429 +                                                frac1*phdiv[p+1];
430                  }
431 <                pvals[i*(nphi+1) + nphi] = (float)(2.*PI);
431 >                pvals[i*(nphi+1) + nphi] = phdiv[sntp[1]];
432          }
433 +                                                /* duplicate final row */
434 +        memcpy(pvals+ntheta*(nphi+1), pvals+(ntheta-1)*(nphi+1),
435 +                                sizeof(*pvals)*(nphi+1));
436          tvals[0] = .0f;
437          tvals[ntheta] = (float)tsize;
438   }
# Line 428 | Line 479 | sens_val(
479          int     t, p;
480          
481          dv[2] = DOT(dvec, ourview.vdir);
482 <        theta = (float)((1./DEGREE) * acos(dv[2]));
482 >        theta = acos(dv[2]);
483          if (theta >= maxtheta)
484                  return(.0f);
485          dv[0] = DOT(dvec, ourview.hvec);
486          dv[1] = DOT(dvec, ourview.vvec);
487 <        phi = (float)((1./DEGREE) * atan2(-dv[0], dv[1]));
488 <        while (phi < .0f) phi += 360.f;
487 >        phi = atan2(-dv[0], dv[1]);
488 >        while (phi < .0f) phi += (float)(2.*PI);
489          t = (int)(theta/maxtheta * sntp[0]);
490 <        p = (int)(phi*(1./360.) * sntp[1]);
490 >        p = (int)(phi*(1./(2.*PI)) * sntp[1]);
491                          /* hack for non-uniform sensor grid */
492 +        theta *= (float)(1./DEGREE);
493 +        phi *= (float)(1./DEGREE);
494          while (t+1 < sntp[0] && theta >= s_theta(t+1))
495                  ++t;
496          while (t-1 >= 0 && theta <= s_theta(t-1))
# Line 449 | Line 502 | sens_val(
502          return(s_val(t,p));
503   }
504  
505 + /* Print origin and direction */
506 + static void
507 + print_ray(
508 +        FVECT rorg,
509 +        FVECT rdir
510 + )
511 + {
512 +        printf("%.6g %.6g %.6g %.8f %.8f %.8f\n",
513 +                        rorg[0], rorg[1], rorg[2],
514 +                        rdir[0], rdir[1], rdir[2]);
515 + }
516 +
517   /* Compute sensor output */
518   static void
519   comp_sensor(
# Line 461 | Line 526 | comp_sensor(
526          int     nt, np;
527          COLOR   vsum;
528          RAY     rr;
529 +        double  sf;
530          int     i, j;
531                                                  /* set view */
532          ourview.type = VT_ANG;
# Line 471 | Line 537 | comp_sensor(
537                  error(USER, err);
538                                                  /* assign probability table */
539          init_ptable(sfile);
540 <                                                /* do Monte Carlo sampling */
540 >                                                /* stratified MC sampling */
541          setcolor(vsum, .0f, .0f, .0f);
542          nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5);
543          np = nsamps/nt;
544 <        VCOPY(rr.rorg, ourview.vp);
479 <        rr.rmax = .0;
544 >        sf = gscale/nsamps;
545          for (i = 0; i < nt; i++)
546                  for (j = 0; j < np; j++) {
547 +                        VCOPY(rr.rorg, ourview.vp);
548                          get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np);
549 <                        rayorigin(&rr, PRIMARY, NULL, NULL);
549 >                        if (ourview.vfore > FTINY)
550 >                                VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore);
551 >                        if (!ray_pnprocs) {
552 >                                print_ray(rr.rorg, rr.rdir);
553 >                                continue;
554 >                        }
555 >                        rr.rmax = .0;
556 >                        rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
557 >                        scalecolor(rr.rcoef, sf);
558                          if (ray_pqueue(&rr) == 1)
559                                  addcolor(vsum, rr.rcol);
560                  }
561 <                                                /* finish MC calculation */
562 <        while (ray_presult(&rr, 0) > 0)
563 <                addcolor(vsum, rr.rcol);
564 <        scalecolor(vsum, gscale/(nt*np));
565 <                                                /* compute direct component */
561 >                                                /* remaining rays pure MC */
562 >        for (i = nsamps - nt*np; i-- > 0; ) {
563 >                VCOPY(rr.rorg, ourview.vp);
564 >                get_direc(rr.rdir, frandom(), frandom());
565 >                if (ourview.vfore > FTINY)
566 >                        VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore);
567 >                if (!ray_pnprocs) {
568 >                        print_ray(rr.rorg, rr.rdir);
569 >                        continue;
570 >                }
571 >                rr.rmax = .0;
572 >                rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
573 >                scalecolor(rr.rcoef, sf);
574 >                if (ray_pqueue(&rr) == 1)
575 >                        addcolor(vsum, rr.rcol);
576 >        }
577 >        if (!ray_pnprocs)                       /* just printing rays */
578 >                return;
579 >                                                /* scale partial result */
580 >        scalecolor(vsum, sf);
581 >                                                /* add direct component */
582          for (i = ndirs; i-- > 0; ) {
583                  SRCINDEX        si;
584                  initsrcindex(&si);
585                  while (srcray(&rr, NULL, &si)) {
586 <                        double  d = sens_val(rr.rdir);
587 <                        if (d <= FTINY)
586 >                        sf = sens_val(rr.rdir);
587 >                        if (sf <= FTINY)
588                                  continue;
589 <                        d *= si.dom/ndirs;
590 <                        scalecolor(rr.rcoef, d);
589 >                        sf *= si.dom/ndirs;
590 >                        scalecolor(rr.rcoef, sf);
591                          if (ray_pqueue(&rr) == 1) {
592                                  multcolor(rr.rcol, rr.rcoef);
593                                  addcolor(vsum, rr.rcol);
594                          }
595                  }
596          }
597 <                                                /* finish direct calculation */
597 >                                                /* finish our calculation */
598          while (ray_presult(&rr, 0) > 0) {
599                  multcolor(rr.rcol, rr.rcoef);
600                  addcolor(vsum, rr.rcol);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines