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.17 by greg, Fri Apr 24 19:17:12 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] */
# Line 207 | 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 243 | Line 245 | load_sensor(
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 260 | Line 263 | load_sensor(
263                          cp = fskip(cp);
264                          if (cp == NULL)
265                                  break;
266 +                        if (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 (sarr[i] > FTINY && i > ntp[0]*(ntp[1]+1))
274 +                                last_pos_val = i;
275                          ++i;
276                  }
277 <                if (i == ntp[0]*(ntp[1]+1))
277 >                if (i == ntp[0]*(ntp[1]+1))     /* empty line? */
278                          break;
279                  if (ntp[0] > 1 && sarr[ntp[0]*(ntp[1]+1)] <=
280                                          sarr[(ntp[0]-1)*(ntp[1]+1)]) {
# Line 278 | 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 (ntp[0] <= 0)
299 <                sprintf(errmsg, "no data in sensor file '%s'", sfile);
298 >        if (!last_pos_val)
299 >                sprintf(errmsg, "no positive sensor values in file '%s'", sfile);
300          else if (fabs(sarr[ntp[1]+1]) > FTINY)
301                  sprintf(errmsg, "minimum theta must be 0 in sensor file '%s'",
302                                  sfile);
# Line 293 | Line 307 | load_sensor(
307                  sprintf(errmsg,
308                          "maximum phi must be positive in sensor file '%s'",
309                                  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);
310          if (errmsg[0])
311                  error(USER, errmsg);
312          return((float *)realloc((void *)sarr, sizeof(float)*nelem));
# Line 356 | Line 366 | init_ptable(
366          psize = PI*tsize/maxtheta;
367          if (sntp[0]*sntp[1] < samptot)  /* don't overdo resolution */
368                  samptot = sntp[0]*sntp[1];
369 <        ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5);
369 >        ntheta = (int)(sqrt((double)samptot*tsize/psize)*sntp[0]/sntp[1]) + 1;
370          if (ntheta > MAXNT)
371                  ntheta = MAXNT;
372          nphi = samptot/ntheta;
# Line 388 | Line 398 | init_ptable(
398                  tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) +
399                                                  frac*cos(thdiv[t+1]) );
400                                  /* offset b/c sensor values are centered */
401 <                if (t <= 0 || frac > 0.5)
401 >                if ((t < sntp[0]-1) & (!t | (frac >= 0.5))) {
402                          frac -= 0.5;
403 <                else if (t >= sntp[0]-1 || frac < 0.5) {
403 >                } else {
404                          frac += 0.5;
405                          --t;
406                  }
# Line 403 | Line 413 | init_ptable(
413                                  if ((prob -= (1.-frac)*rowp[p]/rowsum[t] +
414                                              frac*rowp1[p]/rowsum[t+1]) <= .0)
415                                          break;
416 <                        if (p >= sntp[1]) {
416 >                        if (p >= sntp[1]) {     /* should never happen? */
417                                  p = sntp[1] - 1;
418                                  prob = .5;
419                          }
# Line 414 | Line 424 | init_ptable(
424                  }
425                  pvals[i*(nphi+1) + nphi] = phdiv[sntp[1]];
426          }
427 +                                                /* duplicate final row */
428 +        memcpy(pvals+ntheta*(nphi+1), pvals+(ntheta-1)*(nphi+1),
429 +                                sizeof(*pvals)*(nphi+1));
430          tvals[0] = .0f;
431          tvals[ntheta] = (float)tsize;
432   }
# Line 460 | Line 473 | sens_val(
473          int     t, p;
474          
475          dv[2] = DOT(dvec, ourview.vdir);
476 <        theta = (float)((1./DEGREE) * acos(dv[2]));
476 >        theta = acos(dv[2]);
477          if (theta >= maxtheta)
478                  return(.0f);
479          dv[0] = DOT(dvec, ourview.hvec);
480          dv[1] = DOT(dvec, ourview.vvec);
481 <        phi = (float)((1./DEGREE) * atan2(-dv[0], dv[1]));
482 <        while (phi < .0f) phi += 360.f;
481 >        phi = atan2(-dv[0], dv[1]);
482 >        while (phi < .0f) phi += (float)(2.*PI);
483          t = (int)(theta/maxtheta * sntp[0]);
484 <        p = (int)(phi*(1./360.) * sntp[1]);
484 >        p = (int)(phi*(1./(2.*PI)) * sntp[1]);
485                          /* hack for non-uniform sensor grid */
486 +        theta *= (float)(1./DEGREE);
487 +        phi *= (float)(1./DEGREE);
488          while (t+1 < sntp[0] && theta >= s_theta(t+1))
489                  ++t;
490          while (t-1 >= 0 && theta <= s_theta(t-1))
# Line 532 | Line 547 | comp_sensor(
547                                  continue;
548                          }
549                          rr.rmax = .0;
550 <                        rayorigin(&rr, PRIMARY, NULL, NULL);
550 >                        rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
551                          scalecolor(rr.rcoef, sf);
552                          if (ray_pqueue(&rr) == 1)
553                                  addcolor(vsum, rr.rcol);
# Line 548 | Line 563 | comp_sensor(
563                          continue;
564                  }
565                  rr.rmax = .0;
566 <                rayorigin(&rr, PRIMARY, NULL, NULL);
566 >                rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
567                  scalecolor(rr.rcoef, sf);
568                  if (ray_pqueue(&rr) == 1)
569                          addcolor(vsum, rr.rcol);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines