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.9 by greg, Tue May 17 19:34:36 2011 UTC vs.
Revision 2.20 by greg, Fri Jul 24 17:09:33 2020 UTC

# Line 9 | Line 9 | static const char RCSid[] = "$Id$";
9   */
10  
11   #include "ray.h"
12 + #include "platform.h"
13   #include "source.h"
14   #include "view.h"
15   #include "random.h"
16  
17   #define DEGREE          (PI/180.)
18  
19 < #define MAXNT           180     /* maximum number of theta divisions */
19 > #define MAXNT           181     /* maximum number of theta divisions */
20   #define MAXNP           360     /* maximum number of phi divisions */
21  
22   extern char     *progname;      /* global argv[0] */
# Line 77 | Line 78 | quit(ec)                       /* make sure exit is called */
78   int     ec;
79   {
80          if (ray_pnprocs > 0)    /* close children if any */
81 <                ray_pclose(0);          
81 >                ray_pclose(0);
82 >        else if (ray_pnprocs < 0)
83 >                _exit(ec);      /* avoid flush in child */
84          exit(ec);
85   }
86  
# Line 207 | Line 210 | load_sensor(
210          char    *sfile
211   )
212   {
213 +        int     warnedneg;
214          char    linebuf[8192];
215 +        int     last_pos_val = 0;
216          int     nelem = 1000;
217          float   *sarr = (float *)malloc(sizeof(float)*nelem);
218          FILE    *fp;
# Line 235 | Line 240 | load_sensor(
240                  cp = fskip(cp);
241                  if (cp == NULL)
242                          break;
243 <                if (ntp[1] > 1 && sarr[ntp[1]+1] <= sarr[ntp[1]]) {
243 >                if (ntp[1] > 1 && sarr[ntp[1]+1] <= sarr[ntp[1]]+FTINY) {
244                          sprintf(errmsg,
245                  "Phi values not monotinically increasing in sensor file '%s'",
246                                          sfile);
# Line 243 | Line 248 | load_sensor(
248                  }
249                  ++ntp[1];
250          }
251 +        warnedneg = 0;
252          ntp[0] = 0;                             /* get thetas + data */
253          while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
254                  ++ntp[0];
# Line 260 | Line 266 | load_sensor(
266                          cp = fskip(cp);
267                          if (cp == NULL)
268                                  break;
269 +                        if (sarr[i] < .0) {
270 +                                if (!warnedneg++) {
271 +                                        sprintf(errmsg,
272 +                "Negative value(s) in sensor file '%s' (ignored)\n", sfile);
273 +                                        error(WARNING, errmsg);
274 +                                }
275 +                                sarr[i] = .0;
276 +                        } else if (sarr[i] > FTINY && i > ntp[0]*(ntp[1]+1))
277 +                                last_pos_val = i;
278                          ++i;
279                  }
280 <                if (i == ntp[0]*(ntp[1]+1))
280 >                if (i == ntp[0]*(ntp[1]+1))     /* empty line? */
281                          break;
282                  if (ntp[0] > 1 && sarr[ntp[0]*(ntp[1]+1)] <=
283                                          sarr[(ntp[0]-1)*(ntp[1]+1)]) {
# Line 278 | Line 293 | load_sensor(
293                          error(USER, errmsg);
294                  }
295          }
296 <        nelem = i;
296 >                                                /* truncate zero region */
297 >        ntp[0] = (last_pos_val + ntp[1])/(ntp[1]+1) - 1;
298 >        nelem = (ntp[0]+1)*(ntp[1]+1);
299          fclose(fp);
300          errmsg[0] = '\0';                       /* sanity checks */
301 <        if (ntp[0] <= 0)
302 <                sprintf(errmsg, "no data in sensor file '%s'", sfile);
301 >        if (!last_pos_val)
302 >                sprintf(errmsg, "no positive sensor values in file '%s'", sfile);
303          else if (fabs(sarr[ntp[1]+1]) > FTINY)
304                  sprintf(errmsg, "minimum theta must be 0 in sensor file '%s'",
305                                  sfile);
306          else if (fabs(sarr[1]) > FTINY)
307                  sprintf(errmsg, "minimum phi must be 0 in sensor file '%s'",
308                                  sfile);
309 <        else if (sarr[ntp[1]] <= FTINY)
309 >        else if (sarr[ntp[1]] < 270.-FTINY)
310                  sprintf(errmsg,
311 <                        "maximum phi must be positive in sensor file '%s'",
311 >                        "maximum phi must be 270 or greater in sensor file '%s'",
312                                  sfile);
313 <        else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY)
313 >        else if (sarr[ntp[1]] >= 360.-FTINY)
314                  sprintf(errmsg,
315 <                        "maximum theta must be positive in sensor file '%s'",
315 >                        "maximum phi must be less than 360 in sensor file '%s'",
316                                  sfile);
317          if (errmsg[0])
318                  error(USER, errmsg);
# Line 356 | Line 373 | init_ptable(
373          psize = PI*tsize/maxtheta;
374          if (sntp[0]*sntp[1] < samptot)  /* don't overdo resolution */
375                  samptot = sntp[0]*sntp[1];
376 <        ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5);
376 >        ntheta = (int)(sqrt((double)samptot*tsize/psize)*sntp[0]/sntp[1]) + 1;
377          if (ntheta > MAXNT)
378                  ntheta = MAXNT;
379          nphi = samptot/ntheta;
# Line 388 | Line 405 | init_ptable(
405                  tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) +
406                                                  frac*cos(thdiv[t+1]) );
407                                  /* offset b/c sensor values are centered */
408 <                if (t <= 0 || frac > 0.5)
408 >                if ((t < sntp[0]-1) & (!t | (frac >= 0.5))) {
409                          frac -= 0.5;
410 <                else if (t >= sntp[0]-1 || frac < 0.5) {
410 >                } else {
411                          frac += 0.5;
412                          --t;
413                  }
# Line 403 | Line 420 | init_ptable(
420                                  if ((prob -= (1.-frac)*rowp[p]/rowsum[t] +
421                                              frac*rowp1[p]/rowsum[t+1]) <= .0)
422                                          break;
423 <                        if (p >= sntp[1]) {
423 >                        if (p >= sntp[1]) {     /* should never happen? */
424                                  p = sntp[1] - 1;
425                                  prob = .5;
426                          }
# Line 414 | Line 431 | init_ptable(
431                  }
432                  pvals[i*(nphi+1) + nphi] = phdiv[sntp[1]];
433          }
434 +                                                /* duplicate final row */
435 +        memcpy(pvals+ntheta*(nphi+1), pvals+(ntheta-1)*(nphi+1),
436 +                                sizeof(*pvals)*(nphi+1));
437          tvals[0] = .0f;
438          tvals[ntheta] = (float)tsize;
439   }
# Line 460 | Line 480 | sens_val(
480          int     t, p;
481          
482          dv[2] = DOT(dvec, ourview.vdir);
483 <        theta = (float)((1./DEGREE) * acos(dv[2]));
483 >        theta = acos(dv[2]);
484          if (theta >= maxtheta)
485                  return(.0f);
486          dv[0] = DOT(dvec, ourview.hvec);
487          dv[1] = DOT(dvec, ourview.vvec);
488 <        phi = (float)((1./DEGREE) * atan2(-dv[0], dv[1]));
489 <        while (phi < .0f) phi += 360.f;
488 >        phi = atan2(-dv[0], dv[1]);
489 >        while (phi < .0f) phi += (float)(2.*PI);
490          t = (int)(theta/maxtheta * sntp[0]);
491 <        p = (int)(phi*(1./360.) * sntp[1]);
491 >        p = (int)(phi*(1./(2.*PI)) * sntp[1]);
492                          /* hack for non-uniform sensor grid */
493 +        theta *= (float)(1./DEGREE);
494 +        phi *= (float)(1./DEGREE);
495          while (t+1 < sntp[0] && theta >= s_theta(t+1))
496                  ++t;
497          while (t-1 >= 0 && theta <= s_theta(t-1))
# Line 532 | Line 554 | comp_sensor(
554                                  continue;
555                          }
556                          rr.rmax = .0;
557 <                        rayorigin(&rr, PRIMARY, NULL, NULL);
557 >                        rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
558                          scalecolor(rr.rcoef, sf);
559                          if (ray_pqueue(&rr) == 1)
560                                  addcolor(vsum, rr.rcol);
# Line 548 | Line 570 | comp_sensor(
570                          continue;
571                  }
572                  rr.rmax = .0;
573 <                rayorigin(&rr, PRIMARY, NULL, NULL);
573 >                rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
574                  scalecolor(rr.rcoef, sf);
575                  if (ray_pqueue(&rr) == 1)
576                          addcolor(vsum, rr.rcol);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines