--- ray/src/util/rsensor.c 2011/05/19 16:09:38 2.10 +++ ray/src/util/rsensor.c 2023/11/17 20:02:08 2.22 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rsensor.c,v 2.10 2011/05/19 16:09:38 greg Exp $"; +static const char RCSid[] = "$Id: rsensor.c,v 2.22 2023/11/17 20:02:08 greg Exp $"; #endif /* @@ -9,13 +9,14 @@ static const char RCSid[] = "$Id: rsensor.c,v 2.10 201 */ #include "ray.h" +#include "platform.h" #include "source.h" #include "view.h" #include "random.h" #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] */ @@ -26,8 +27,7 @@ VIEW ourview = {VT_ANG,{0.,0.,0.},{0.,0.,1.},{1.,0., 1.,180.,180.,0.,0.,0.,0., {0.,0.,0.},{0.,0.,0.},0.,0.}; -unsigned long nsamps = 10000; /* desired number of initial samples */ -unsigned long nssamps = 9000; /* number of super-samples */ +long nsamps = 10000; /* desired number of initial samples */ int ndsamps = 32; /* number of direct samples */ int nprocs = 1; /* number of rendering processes */ @@ -59,7 +59,6 @@ print_defaults() /* print out default parameters */ over_options(); printf("-n %-9d\t\t\t# number of processes\n", nprocs); printf("-rd %-9ld\t\t\t# ray directions\n", nsamps); - /* printf("-rs %-9ld\t\t\t# ray super-samples\n", nssamps); */ printf("-dn %-9d\t\t\t# direct number of samples\n", ndsamps); printf("-vp %f %f %f\t# view point\n", ourview.vp[0], ourview.vp[1], ourview.vp[2]); @@ -77,7 +76,9 @@ quit(ec) /* make sure exit is called */ int ec; { if (ray_pnprocs > 0) /* close children if any */ - ray_pclose(0); + ray_pclose(0); + else if (ray_pnprocs < 0) + _exit(ec); /* avoid flush in child */ exit(ec); } @@ -129,8 +130,6 @@ main( if (argv[i][1] == 'r') { /* sampling options */ if (argv[i][2] == 'd') nsamps = atol(argv[++i]); - else if (argv[i][2] == 's') - nssamps = atol(argv[++i]); else { sprintf(errmsg, "bad option at '%s'", argv[i]); error(USER, errmsg); @@ -209,6 +208,7 @@ load_sensor( { int warnedneg; char linebuf[8192]; + int last_pos_val = 0; int nelem = 1000; float *sarr = (float *)malloc(sizeof(float)*nelem); FILE *fp; @@ -236,7 +236,7 @@ load_sensor( cp = fskip(cp); if (cp == NULL) break; - if (ntp[1] > 1 && sarr[ntp[1]+1] <= sarr[ntp[1]]) { + if (ntp[1] > 1 && sarr[ntp[1]+1] <= sarr[ntp[1]]+FTINY) { sprintf(errmsg, "Phi values not monotinically increasing in sensor file '%s'", sfile); @@ -262,17 +262,18 @@ load_sensor( cp = fskip(cp); if (cp == NULL) break; - if (i && sarr[i] < .0) { + if (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 (sarr[i] > FTINY && i > ntp[0]*(ntp[1]+1)) + last_pos_val = i; ++i; } - if (i == ntp[0]*(ntp[1]+1)) + if (i == ntp[0]*(ntp[1]+1)) /* empty line? */ break; if (ntp[0] > 1 && sarr[ntp[0]*(ntp[1]+1)] <= sarr[(ntp[0]-1)*(ntp[1]+1)]) { @@ -288,24 +289,26 @@ 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 (ntp[0] <= 0) - sprintf(errmsg, "no data in sensor file '%s'", sfile); + if (!last_pos_val) + sprintf(errmsg, "no positive sensor values in file '%s'", sfile); else if (fabs(sarr[ntp[1]+1]) > FTINY) sprintf(errmsg, "minimum theta must be 0 in sensor file '%s'", sfile); else if (fabs(sarr[1]) > FTINY) sprintf(errmsg, "minimum phi must be 0 in sensor file '%s'", sfile); - else if (sarr[ntp[1]] <= FTINY) + else if (sarr[ntp[1]] < 270.-FTINY) sprintf(errmsg, - "maximum phi must be positive in sensor file '%s'", + "maximum phi must be 270 or greater in sensor file '%s'", sfile); - else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY) + else if (sarr[ntp[1]] >= 360.-FTINY) sprintf(errmsg, - "maximum theta must be positive in sensor file '%s'", + "maximum phi must be less than 360 in sensor file '%s'", sfile); if (errmsg[0]) error(USER, errmsg); @@ -318,7 +321,7 @@ init_ptable( char *sfile ) { - int samptot = nsamps; + long samptot = nsamps; float *rowp, *rowp1; double rowsum[MAXNT], rowomega[MAXNT]; double thdiv[MAXNT+1], phdiv[MAXNP+1]; @@ -366,7 +369,7 @@ init_ptable( psize = PI*tsize/maxtheta; if (sntp[0]*sntp[1] < samptot) /* don't overdo resolution */ samptot = sntp[0]*sntp[1]; - ntheta = (int)(sqrt((double)samptot*tsize/psize) + 0.5); + ntheta = (int)(sqrt((double)samptot*tsize/psize)*sntp[0]/sntp[1]) + 1; if (ntheta > MAXNT) ntheta = MAXNT; nphi = samptot/ntheta; @@ -398,9 +401,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 || (t < sntp[0]-1) & (frac >= 0.5)) + if ((t < sntp[0]-1) & (!t | (frac >= 0.5))) { frac -= 0.5; - else { + } else { frac += 0.5; --t; } @@ -424,6 +427,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; } @@ -470,16 +476,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)) @@ -513,7 +521,8 @@ comp_sensor( ndsamps > 0 ? 1 : 0; char *err; int nt, np; - COLOR vsum; + SCOLOR vsum; + COLOR cres; RAY rr; double sf; int i, j; @@ -527,7 +536,7 @@ comp_sensor( /* assign probability table */ init_ptable(sfile); /* stratified MC sampling */ - setcolor(vsum, .0f, .0f, .0f); + scolorblack(vsum); nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5); np = nsamps/nt; sf = gscale/nsamps; @@ -542,10 +551,10 @@ comp_sensor( continue; } rr.rmax = .0; - rayorigin(&rr, PRIMARY, NULL, NULL); - scalecolor(rr.rcoef, sf); + rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL); + scalescolor(rr.rcoef, sf); if (ray_pqueue(&rr) == 1) - addcolor(vsum, rr.rcol); + saddscolor(vsum, rr.rcol); } /* remaining rays pure MC */ for (i = nsamps - nt*np; i-- > 0; ) { @@ -558,15 +567,15 @@ comp_sensor( continue; } rr.rmax = .0; - rayorigin(&rr, PRIMARY, NULL, NULL); - scalecolor(rr.rcoef, sf); + rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL); + scalescolor(rr.rcoef, sf); if (ray_pqueue(&rr) == 1) - addcolor(vsum, rr.rcol); + saddscolor(vsum, rr.rcol); } if (!ray_pnprocs) /* just printing rays */ return; /* scale partial result */ - scalecolor(vsum, sf); + scalescolor(vsum, sf); /* add direct component */ for (i = ndirs; i-- > 0; ) { SRCINDEX si; @@ -576,19 +585,20 @@ comp_sensor( if (sf <= FTINY) continue; sf *= si.dom/ndirs; - scalecolor(rr.rcoef, sf); + scalescolor(rr.rcoef, sf); if (ray_pqueue(&rr) == 1) { - multcolor(rr.rcol, rr.rcoef); - addcolor(vsum, rr.rcol); + smultscolor(rr.rcol, rr.rcoef); + saddscolor(vsum, rr.rcol); } } } /* finish our calculation */ while (ray_presult(&rr, 0) > 0) { - multcolor(rr.rcol, rr.rcoef); - addcolor(vsum, rr.rcol); + smultscolor(rr.rcol, rr.rcoef); + saddscolor(vsum, rr.rcol); } /* print our result */ - printf("%.4e %.4e %.4e\n", colval(vsum,RED), - colval(vsum,GRN), colval(vsum,BLU)); + scolor_rgb(cres, vsum); + printf("%.4e %.4e %.4e\n", colval(cres,RED), + colval(cres,GRN), colval(cres,BLU)); }