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.8 by greg, Sun Sep 26 15:41:46 2010 UTC vs.
Revision 2.18 by greg, Tue Jun 9 21:34:15 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 34 | 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 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 235 | 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]]+FTINY) {
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 254 | 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)]) {
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 265 | 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);
303          else if (fabs(sarr[1]) > FTINY)
304                  sprintf(errmsg, "minimum phi must be 0 in sensor file '%s'",
305                                  sfile);
306 <        else if (sarr[ntp[1]] <= FTINY)
306 >        else if (sarr[ntp[1]] < 270.-FTINY)
307                  sprintf(errmsg,
308 <                        "maximum phi must be positive in sensor file '%s'",
308 >                        "maximum phi must be 270 or greater in sensor file '%s'",
309                                  sfile);
310 <        else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY)
310 >        else if (sarr[ntp[1]] >= 360.-FTINY)
311                  sprintf(errmsg,
312 <                        "maximum theta must be positive in sensor file '%s'",
312 >                        "maximum phi must be less than 360 in sensor file '%s'",
313                                  sfile);
314          if (errmsg[0])
315                  error(USER, errmsg);
# Line 327 | Line 354 | init_ptable(
354                  error(INTERNAL, errmsg);
355          }
356                                          /* compute boundary angles */
357 <        maxtheta = 1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2);
357 >        maxtheta = DEGREE*(1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2));
358 >        if (maxtheta > PI)
359 >                maxtheta = PI;
360          thdiv[0] = .0;
361          for (t = 1; t < sntp[0]; t++)
362                  thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t));
363 <        thdiv[sntp[0]] = maxtheta*DEGREE;
364 <        phdiv[0] = .0;
363 >        thdiv[sntp[0]] = maxtheta;
364 >        phdiv[0] = DEGREE*(1.5f*s_phi(0) - 0.5f*s_phi(1));
365          for (p = 1; p < sntp[1]; p++)
366                  phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p));
367 <        phdiv[sntp[1]] = 2.*PI;
367 >        phdiv[sntp[1]] = DEGREE*(1.5f*s_phi(sntp[1]-1) - 0.5f*s_phi(sntp[1]-2));
368                                          /* size our table */
369 <        tsize = 1. - cos(maxtheta*DEGREE);
370 <        psize = PI*tsize/(maxtheta*DEGREE);
369 >        tsize = 1. - cos(maxtheta);
370 >        psize = PI*tsize/maxtheta;
371          if (sntp[0]*sntp[1] < samptot)  /* don't overdo resolution */
372                  samptot = sntp[0]*sntp[1];
373 <        ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5);
373 >        ntheta = (int)(sqrt((double)samptot*tsize/psize)*sntp[0]/sntp[1]) + 1;
374          if (ntheta > MAXNT)
375                  ntheta = MAXNT;
376          nphi = samptot/ntheta;
377 <        pvals = (float *)malloc(sizeof(float)*ntheta*(nphi+1));
377 >        pvals = (float *)malloc(sizeof(float)*(ntheta+1)*(nphi+1));
378          if (pvals == NULL)
379                  error(SYSTEM, "out of memory in init_ptable()");
380          gscale = .0;                    /* compute our inverse table */
381          for (i = 0; i < sntp[0]; i++) {
382                  rowp = &s_val(i,0);
383 <                rowsum[i] = 0.;
383 >                rowsum[i] = 1e-20;
384                  for (j = 0; j < sntp[1]; j++)
385                          rowsum[i] += *rowp++;
386                  rowomega[i] = cos(thdiv[i]) - cos(thdiv[i+1]);
387                  rowomega[i] *= 2.*PI / (double)sntp[1];
388                  gscale += rowsum[i] * rowomega[i];
389          }
390 +        if (gscale <= FTINY) {
391 +                sprintf(errmsg, "Sensor values sum to zero in file '%s'", sfile);
392 +                error(USER, errmsg);
393 +        }
394          for (i = 0; i < ntheta; i++) {
395                  prob = (double)i / (double)ntheta;
396                  for (t = 0; t < sntp[0]; t++)
# Line 369 | Line 402 | init_ptable(
402                  tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) +
403                                                  frac*cos(thdiv[t+1]) );
404                                  /* offset b/c sensor values are centered */
405 <                if (t <= 0 || frac > 0.5)
405 >                if ((t < sntp[0]-1) & (!t | (frac >= 0.5))) {
406                          frac -= 0.5;
407 <                else if (t >= sntp[0]-1 || frac < 0.5) {
407 >                } else {
408                          frac += 0.5;
409                          --t;
410                  }
411 <                pvals[i*(nphi+1)] = .0f;
411 >                pvals[i*(nphi+1)] = phdiv[0];
412                  for (j = 1; j < nphi; j++) {
413                          prob = (double)j / (double)nphi;
414                          rowp = &s_val(t,0);
415                          rowp1 = &s_val(t+1,0);
416 <                        for (p = 0; p < sntp[1]; p++) {
416 >                        for (p = 0; p < sntp[1]; p++)
417                                  if ((prob -= (1.-frac)*rowp[p]/rowsum[t] +
418                                              frac*rowp1[p]/rowsum[t+1]) <= .0)
419                                          break;
420 <                                if (p >= sntp[1])
421 <                                        error(INTERNAL,
422 <                                            "code error 2 in init_ptable()");
390 <                                frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t]
391 <                                                + frac*rowp1[p]/rowsum[t+1]);
392 <                                pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] +
393 <                                                        frac1*phdiv[p+1];
420 >                        if (p >= sntp[1]) {     /* should never happen? */
421 >                                p = sntp[1] - 1;
422 >                                prob = .5;
423                          }
424 +                        frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t]
425 +                                        + frac*rowp1[p]/rowsum[t+1]);
426 +                        pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] +
427 +                                                frac1*phdiv[p+1];
428                  }
429 <                pvals[i*(nphi+1) + nphi] = (float)(2.*PI);
429 >                pvals[i*(nphi+1) + nphi] = phdiv[sntp[1]];
430          }
431 +                                                /* duplicate final row */
432 +        memcpy(pvals+ntheta*(nphi+1), pvals+(ntheta-1)*(nphi+1),
433 +                                sizeof(*pvals)*(nphi+1));
434          tvals[0] = .0f;
435          tvals[ntheta] = (float)tsize;
436   }
# Line 441 | Line 477 | sens_val(
477          int     t, p;
478          
479          dv[2] = DOT(dvec, ourview.vdir);
480 <        theta = (float)((1./DEGREE) * acos(dv[2]));
480 >        theta = acos(dv[2]);
481          if (theta >= maxtheta)
482                  return(.0f);
483          dv[0] = DOT(dvec, ourview.hvec);
484          dv[1] = DOT(dvec, ourview.vvec);
485 <        phi = (float)((1./DEGREE) * atan2(-dv[0], dv[1]));
486 <        while (phi < .0f) phi += 360.f;
485 >        phi = atan2(-dv[0], dv[1]);
486 >        while (phi < .0f) phi += (float)(2.*PI);
487          t = (int)(theta/maxtheta * sntp[0]);
488 <        p = (int)(phi*(1./360.) * sntp[1]);
488 >        p = (int)(phi*(1./(2.*PI)) * sntp[1]);
489                          /* hack for non-uniform sensor grid */
490 +        theta *= (float)(1./DEGREE);
491 +        phi *= (float)(1./DEGREE);
492          while (t+1 < sntp[0] && theta >= s_theta(t+1))
493                  ++t;
494          while (t-1 >= 0 && theta <= s_theta(t-1))
# Line 513 | Line 551 | comp_sensor(
551                                  continue;
552                          }
553                          rr.rmax = .0;
554 <                        rayorigin(&rr, PRIMARY, NULL, NULL);
554 >                        rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
555                          scalecolor(rr.rcoef, sf);
556                          if (ray_pqueue(&rr) == 1)
557                                  addcolor(vsum, rr.rcol);
# Line 529 | Line 567 | comp_sensor(
567                          continue;
568                  }
569                  rr.rmax = .0;
570 <                rayorigin(&rr, PRIMARY, NULL, NULL);
570 >                rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL);
571                  scalecolor(rr.rcoef, sf);
572                  if (ray_pqueue(&rr) == 1)
573                          addcolor(vsum, rr.rcol);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines