--- ray/src/util/rsensor.c 2011/05/17 19:34:36 2.9 +++ ray/src/util/rsensor.c 2015/04/23 23:54:08 2.16 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rsensor.c,v 2.9 2011/05/17 19:34:36 greg Exp $"; +static const char RCSid[] = "$Id: rsensor.c,v 2.16 2015/04/23 23:54:08 greg Exp $"; #endif /* @@ -15,7 +15,7 @@ static const char RCSid[] = "$Id: rsensor.c,v 2.9 2011 #define DEGREE (PI/180.) -#define MAXNT 180 /* maximum number of theta divisions */ +#define MAXNT 181 /* maximum number of theta divisions */ #define MAXNP 360 /* maximum number of phi divisions */ extern char *progname; /* global argv[0] */ @@ -207,7 +207,9 @@ load_sensor( char *sfile ) { + int warnedneg; char linebuf[8192]; + int last_pos_val = 0; int nelem = 1000; float *sarr = (float *)malloc(sizeof(float)*nelem); FILE *fp; @@ -243,6 +245,7 @@ load_sensor( } ++ntp[1]; } + warnedneg = 0; ntp[0] = 0; /* get thetas + data */ while (fgets(linebuf, sizeof(linebuf), fp) != NULL) { ++ntp[0]; @@ -260,6 +263,15 @@ load_sensor( cp = fskip(cp); if (cp == NULL) break; + if (i && sarr[i] < .0) { + if (!warnedneg++) { + sprintf(errmsg, + "Negative value(s) in sensor file '%s' (ignored)\n", sfile); + error(WARNING, errmsg); + } + sarr[i] = .0; + } else if (i > ntp[0]*(ntp[1]+1) && sarr[i] > FTINY) + last_pos_val = i; ++i; } if (i == ntp[0]*(ntp[1]+1)) @@ -278,9 +290,13 @@ load_sensor( error(USER, errmsg); } } - nelem = i; + /* truncate zero region */ + ntp[0] = (last_pos_val + ntp[1])/(ntp[1]+1) - 1; + nelem = (ntp[0]+1)*(ntp[1]+1); fclose(fp); errmsg[0] = '\0'; /* sanity checks */ + if (!last_pos_val) + sprintf(errmsg, "no positive sensor values in file '%s'", sfile); if (ntp[0] <= 0) sprintf(errmsg, "no data in sensor file '%s'", sfile); else if (fabs(sarr[ntp[1]+1]) > FTINY) @@ -295,8 +311,8 @@ load_sensor( sfile); else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY) sprintf(errmsg, - "maximum theta must be positive in sensor file '%s'", - sfile); + "maximum theta (%f) must be positive in sensor file '%s'", + sarr[ntp[0]*(ntp[1]+1)], sfile); if (errmsg[0]) error(USER, errmsg); return((float *)realloc((void *)sarr, sizeof(float)*nelem)); @@ -388,9 +404,9 @@ init_ptable( tvals[i] = 1. - ( (1.-frac)*cos(thdiv[t]) + frac*cos(thdiv[t+1]) ); /* offset b/c sensor values are centered */ - if (t <= 0 || frac > 0.5) + if ((t < sntp[0]-1) & (!t | (frac >= 0.5))) { frac -= 0.5; - else if (t >= sntp[0]-1 || frac < 0.5) { + } else { frac += 0.5; --t; } @@ -403,7 +419,7 @@ init_ptable( if ((prob -= (1.-frac)*rowp[p]/rowsum[t] + frac*rowp1[p]/rowsum[t+1]) <= .0) break; - if (p >= sntp[1]) { + if (p >= sntp[1]) { /* should never happen? */ p = sntp[1] - 1; prob = .5; } @@ -414,6 +430,9 @@ init_ptable( } pvals[i*(nphi+1) + nphi] = phdiv[sntp[1]]; } + /* duplicate final row */ + memcpy(pvals+ntheta*(nphi+1), pvals+(ntheta-1)*(nphi+1), + sizeof(*pvals)*(nphi+1)); tvals[0] = .0f; tvals[ntheta] = (float)tsize; } @@ -460,16 +479,18 @@ sens_val( int t, p; dv[2] = DOT(dvec, ourview.vdir); - theta = (float)((1./DEGREE) * acos(dv[2])); + theta = acos(dv[2]); if (theta >= maxtheta) return(.0f); dv[0] = DOT(dvec, ourview.hvec); dv[1] = DOT(dvec, ourview.vvec); - phi = (float)((1./DEGREE) * atan2(-dv[0], dv[1])); - while (phi < .0f) phi += 360.f; + phi = atan2(-dv[0], dv[1]); + while (phi < .0f) phi += (float)(2.*PI); t = (int)(theta/maxtheta * sntp[0]); - p = (int)(phi*(1./360.) * sntp[1]); + p = (int)(phi*(1./(2.*PI)) * sntp[1]); /* hack for non-uniform sensor grid */ + theta *= (float)(1./DEGREE); + phi *= (float)(1./DEGREE); while (t+1 < sntp[0] && theta >= s_theta(t+1)) ++t; while (t-1 >= 0 && theta <= s_theta(t-1)) @@ -532,7 +553,7 @@ comp_sensor( continue; } rr.rmax = .0; - rayorigin(&rr, PRIMARY, NULL, NULL); + rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL); scalecolor(rr.rcoef, sf); if (ray_pqueue(&rr) == 1) addcolor(vsum, rr.rcol); @@ -548,7 +569,7 @@ comp_sensor( continue; } rr.rmax = .0; - rayorigin(&rr, PRIMARY, NULL, NULL); + rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL); scalecolor(rr.rcoef, sf); if (ray_pqueue(&rr) == 1) addcolor(vsum, rr.rcol);