| 1 | #ifndef lint | 
| 2 | static const char RCSid[] = "$Id: rsensor.c,v 2.14 2014/09/30 23:14:34 greg Exp $"; | 
| 3 | #endif | 
| 4 |  | 
| 5 | /* | 
| 6 | * Compute sensor signal based on spatial sensitivity. | 
| 7 | * | 
| 8 | *      Created Feb 2008 for Architectural Energy Corp. | 
| 9 | */ | 
| 10 |  | 
| 11 | #include "ray.h" | 
| 12 | #include "source.h" | 
| 13 | #include "view.h" | 
| 14 | #include "random.h" | 
| 15 |  | 
| 16 | #define DEGREE          (PI/180.) | 
| 17 |  | 
| 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] */ | 
| 22 | extern int      nowarn;         /* don't report warnings? */ | 
| 23 |  | 
| 24 | /* current sensor's perspective */ | 
| 25 | VIEW            ourview =  {VT_ANG,{0.,0.,0.},{0.,0.,1.},{1.,0.,0.}, | 
| 26 | 1.,180.,180.,0.,0.,0.,0., | 
| 27 | {0.,0.,0.},{0.,0.,0.},0.,0.}; | 
| 28 |  | 
| 29 | unsigned long   nsamps = 10000; /* desired number of initial samples */ | 
| 30 | unsigned long   nssamps = 9000; /* number of super-samples */ | 
| 31 | int             ndsamps = 32;   /* number of direct samples */ | 
| 32 | int             nprocs = 1;     /* number of rendering processes */ | 
| 33 |  | 
| 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 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 */ | 
| 42 |  | 
| 43 | #define s_theta(t)      sensor[(t+1)*(sntp[1]+1)] | 
| 44 | #define s_phi(p)        sensor[(p)+1] | 
| 45 | #define s_val(t,p)      sensor[(p)+1+(t+1)*(sntp[1]+1)] | 
| 46 |  | 
| 47 | static void     comp_sensor(char *sfile); | 
| 48 |  | 
| 49 | static void | 
| 50 | over_options()                  /* overriding options */ | 
| 51 | { | 
| 52 | directvis = (ndsamps <= 0); | 
| 53 | do_irrad = 0; | 
| 54 | } | 
| 55 |  | 
| 56 | static void | 
| 57 | print_defaults()                /* print out default parameters */ | 
| 58 | { | 
| 59 | over_options(); | 
| 60 | printf("-n %-9d\t\t\t# number of processes\n", nprocs); | 
| 61 | printf("-rd %-9ld\t\t\t# ray directions\n", nsamps); | 
| 62 | /* printf("-rs %-9ld\t\t\t# ray super-samples\n", nssamps); */ | 
| 63 | printf("-dn %-9d\t\t\t# direct number of samples\n", ndsamps); | 
| 64 | printf("-vp %f %f %f\t# view point\n", | 
| 65 | ourview.vp[0], ourview.vp[1], ourview.vp[2]); | 
| 66 | printf("-vd %f %f %f\t# view direction\n", | 
| 67 | ourview.vdir[0], ourview.vdir[1], ourview.vdir[2]); | 
| 68 | printf("-vu %f %f %f\t# view up\n", | 
| 69 | ourview.vup[0], ourview.vup[1], ourview.vup[2]); | 
| 70 | printf("-vo %f\t\t\t# view fore clipping distance\n", ourview.vfore); | 
| 71 | print_rdefaults(); | 
| 72 | } | 
| 73 |  | 
| 74 |  | 
| 75 | void | 
| 76 | quit(ec)                        /* make sure exit is called */ | 
| 77 | int     ec; | 
| 78 | { | 
| 79 | if (ray_pnprocs > 0)    /* close children if any */ | 
| 80 | ray_pclose(0); | 
| 81 | exit(ec); | 
| 82 | } | 
| 83 |  | 
| 84 |  | 
| 85 | int | 
| 86 | main( | 
| 87 | int     argc, | 
| 88 | char    *argv[] | 
| 89 | ) | 
| 90 | { | 
| 91 | int     doheader = 1; | 
| 92 | int     optwarn = 0; | 
| 93 | int     i, rval; | 
| 94 |  | 
| 95 | progname = argv[0]; | 
| 96 | /* set up rendering defaults */ | 
| 97 | rand_samp = 1; | 
| 98 | dstrsrc = 0.65; | 
| 99 | srcsizerat = 0.1; | 
| 100 | directrelay = 3; | 
| 101 | ambounce = 1; | 
| 102 | maxdepth = -10; | 
| 103 | /* get options from command line */ | 
| 104 | for (i = 1; i < argc; i++) { | 
| 105 | while ((rval = expandarg(&argc, &argv, i)) > 0) | 
| 106 | ; | 
| 107 | if (rval < 0) { | 
| 108 | sprintf(errmsg, "cannot expand '%s'", argv[i]); | 
| 109 | error(SYSTEM, errmsg); | 
| 110 | } | 
| 111 | if (argv[i][0] != '-') { | 
| 112 | if (i >= argc-1) | 
| 113 | break;          /* final octree argument */ | 
| 114 | if (!ray_pnprocs) { | 
| 115 | over_options(); | 
| 116 | if (doheader) { /* print header */ | 
| 117 | newheader("RADIANCE", stdout); | 
| 118 | printargs(argc, argv, stdout); | 
| 119 | fputformat("ascii", stdout); | 
| 120 | putchar('\n'); | 
| 121 | } | 
| 122 | /* start process(es) */ | 
| 123 | if (strcmp(argv[argc-1], ".")) | 
| 124 | ray_pinit(argv[argc-1], nprocs); | 
| 125 | } | 
| 126 | comp_sensor(argv[i]);   /* process a sensor file */ | 
| 127 | continue; | 
| 128 | } | 
| 129 | if (argv[i][1] == 'r') {        /* sampling options */ | 
| 130 | if (argv[i][2] == 'd') | 
| 131 | nsamps = atol(argv[++i]); | 
| 132 | else if (argv[i][2] == 's') | 
| 133 | nssamps = atol(argv[++i]); | 
| 134 | else { | 
| 135 | sprintf(errmsg, "bad option at '%s'", argv[i]); | 
| 136 | error(USER, errmsg); | 
| 137 | } | 
| 138 | continue; | 
| 139 | } | 
| 140 | /* direct component samples */ | 
| 141 | if (argv[i][1] == 'd' && argv[i][2] == 'n') { | 
| 142 | ndsamps = atoi(argv[++i]); | 
| 143 | continue; | 
| 144 | } | 
| 145 | if (argv[i][1] == 'v') {        /* next sensor view */ | 
| 146 | if (argv[i][2] == 'f') { | 
| 147 | rval = viewfile(argv[++i], &ourview, NULL); | 
| 148 | if (rval < 0) { | 
| 149 | sprintf(errmsg, | 
| 150 | "cannot open view file \"%s\"", | 
| 151 | argv[i]); | 
| 152 | error(SYSTEM, errmsg); | 
| 153 | } else if (rval == 0) { | 
| 154 | sprintf(errmsg, | 
| 155 | "bad view file \"%s\"", | 
| 156 | argv[i]); | 
| 157 | error(USER, errmsg); | 
| 158 | } | 
| 159 | continue; | 
| 160 | } | 
| 161 | rval = getviewopt(&ourview, argc-i, argv+i); | 
| 162 | if (rval >= 0) { | 
| 163 | i += rval; | 
| 164 | continue; | 
| 165 | } | 
| 166 | sprintf(errmsg, "bad view option at '%s'", argv[i]); | 
| 167 | error(USER, errmsg); | 
| 168 | } | 
| 169 | if (!strcmp(argv[i], "-w")) {   /* toggle warnings */ | 
| 170 | nowarn = !nowarn; | 
| 171 | continue; | 
| 172 | } | 
| 173 | if (ray_pnprocs) { | 
| 174 | if (!optwarn++) | 
| 175 | error(WARNING, | 
| 176 | "rendering options should appear before first sensor"); | 
| 177 | } else if (!strcmp(argv[i], "-defaults")) { | 
| 178 | print_defaults(); | 
| 179 | return(0); | 
| 180 | } | 
| 181 | if (argv[i][1] == 'h') {        /* header toggle */ | 
| 182 | doheader = !doheader; | 
| 183 | continue; | 
| 184 | } | 
| 185 | if (!strcmp(argv[i], "-n")) {   /* number of processes */ | 
| 186 | nprocs = atoi(argv[++i]); | 
| 187 | if (nprocs <= 0) | 
| 188 | error(USER, "illegal number of processes"); | 
| 189 | continue; | 
| 190 | } | 
| 191 | rval = getrenderopt(argc-i, argv+i); | 
| 192 | if (rval < 0) { | 
| 193 | sprintf(errmsg, "bad render option at '%s'", argv[i]); | 
| 194 | error(USER, errmsg); | 
| 195 | } | 
| 196 | i += rval; | 
| 197 | } | 
| 198 | if (sensor == NULL) | 
| 199 | error(USER, i<argc ? "missing sensor file" : "missing octree"); | 
| 200 | quit(0); | 
| 201 | } | 
| 202 |  | 
| 203 | /* Load sensor sensitivities (first row and column are angles) */ | 
| 204 | static float * | 
| 205 | load_sensor( | 
| 206 | int     ntp[2], | 
| 207 | char    *sfile | 
| 208 | ) | 
| 209 | { | 
| 210 | int     warnedneg; | 
| 211 | char    linebuf[8192]; | 
| 212 | int     nelem = 1000; | 
| 213 | float   *sarr = (float *)malloc(sizeof(float)*nelem); | 
| 214 | FILE    *fp; | 
| 215 | char    *cp; | 
| 216 | int     i; | 
| 217 |  | 
| 218 | fp = frlibopen(sfile); | 
| 219 | if (fp == NULL) { | 
| 220 | sprintf(errmsg, "cannot open sensor file '%s'", sfile); | 
| 221 | error(SYSTEM, errmsg); | 
| 222 | } | 
| 223 | fgets(linebuf, sizeof(linebuf), fp); | 
| 224 | if (!strncmp(linebuf, "Elevation ", 10)) | 
| 225 | fgets(linebuf, sizeof(linebuf), fp); | 
| 226 | /* get phi values */ | 
| 227 | sarr[0] = .0f; | 
| 228 | if (strncmp(linebuf, "degrees", 7)) { | 
| 229 | sprintf(errmsg, "Missing 'degrees' in sensor file '%s'", sfile); | 
| 230 | error(USER, errmsg); | 
| 231 | } | 
| 232 | cp = sskip(linebuf); | 
| 233 | ntp[1] = 0; | 
| 234 | for ( ; ; ) { | 
| 235 | sarr[ntp[1]+1] = atof(cp); | 
| 236 | cp = fskip(cp); | 
| 237 | if (cp == NULL) | 
| 238 | break; | 
| 239 | if (ntp[1] > 1 && sarr[ntp[1]+1] <= sarr[ntp[1]]) { | 
| 240 | sprintf(errmsg, | 
| 241 | "Phi values not monotinically increasing in sensor file '%s'", | 
| 242 | sfile); | 
| 243 | error(USER, errmsg); | 
| 244 | } | 
| 245 | ++ntp[1]; | 
| 246 | } | 
| 247 | warnedneg = 0; | 
| 248 | ntp[0] = 0;                             /* get thetas + data */ | 
| 249 | while (fgets(linebuf, sizeof(linebuf), fp) != NULL) { | 
| 250 | ++ntp[0]; | 
| 251 | if ((ntp[0]+1)*(ntp[1]+1) > nelem) { | 
| 252 | nelem += (nelem>>2) + ntp[1]; | 
| 253 | sarr = (float *)realloc((void *)sarr, | 
| 254 | sizeof(float)*nelem); | 
| 255 | if (sarr == NULL) | 
| 256 | error(SYSTEM, "out of memory in load_sensor()"); | 
| 257 | } | 
| 258 | cp = linebuf; | 
| 259 | i = ntp[0]*(ntp[1]+1); | 
| 260 | for ( ; ; ) { | 
| 261 | sarr[i] = atof(cp); | 
| 262 | cp = fskip(cp); | 
| 263 | if (cp == NULL) | 
| 264 | break; | 
| 265 | if (i && sarr[i] < .0) { | 
| 266 | if (!warnedneg++) { | 
| 267 | sprintf(errmsg, | 
| 268 | "Negative value(s) in sensor file '%s' (ignored)\n", sfile); | 
| 269 | error(WARNING, errmsg); | 
| 270 | } | 
| 271 | sarr[i] = .0; | 
| 272 | } | 
| 273 | ++i; | 
| 274 | } | 
| 275 | if (i == ntp[0]*(ntp[1]+1)) | 
| 276 | break; | 
| 277 | if (ntp[0] > 1 && sarr[ntp[0]*(ntp[1]+1)] <= | 
| 278 | sarr[(ntp[0]-1)*(ntp[1]+1)]) { | 
| 279 | sprintf(errmsg, | 
| 280 | "Theta values not monotinically increasing in sensor file '%s'", | 
| 281 | sfile); | 
| 282 | error(USER, errmsg); | 
| 283 | } | 
| 284 | if (i != (ntp[0]+1)*(ntp[1]+1)) { | 
| 285 | sprintf(errmsg, | 
| 286 | "bad column count near line %d in sensor file '%s'", | 
| 287 | ntp[0]+1, sfile); | 
| 288 | error(USER, errmsg); | 
| 289 | } | 
| 290 | } | 
| 291 | nelem = i; | 
| 292 | fclose(fp); | 
| 293 | errmsg[0] = '\0';                       /* sanity checks */ | 
| 294 | if (ntp[0] <= 0) | 
| 295 | sprintf(errmsg, "no data in sensor file '%s'", sfile); | 
| 296 | else if (fabs(sarr[ntp[1]+1]) > FTINY) | 
| 297 | sprintf(errmsg, "minimum theta must be 0 in sensor file '%s'", | 
| 298 | sfile); | 
| 299 | else if (fabs(sarr[1]) > FTINY) | 
| 300 | sprintf(errmsg, "minimum phi must be 0 in sensor file '%s'", | 
| 301 | sfile); | 
| 302 | else if (sarr[ntp[1]] <= FTINY) | 
| 303 | sprintf(errmsg, | 
| 304 | "maximum phi must be positive in sensor file '%s'", | 
| 305 | sfile); | 
| 306 | else if (sarr[ntp[0]*(ntp[1]+1)] <= FTINY) | 
| 307 | sprintf(errmsg, | 
| 308 | "maximum theta must be positive in sensor file '%s'", | 
| 309 | sfile); | 
| 310 | if (errmsg[0]) | 
| 311 | error(USER, errmsg); | 
| 312 | return((float *)realloc((void *)sarr, sizeof(float)*nelem)); | 
| 313 | } | 
| 314 |  | 
| 315 | /* Initialize probability table */ | 
| 316 | static void | 
| 317 | init_ptable( | 
| 318 | char    *sfile | 
| 319 | ) | 
| 320 | { | 
| 321 | int     samptot = nsamps; | 
| 322 | float   *rowp, *rowp1; | 
| 323 | double  rowsum[MAXNT], rowomega[MAXNT]; | 
| 324 | double  thdiv[MAXNT+1], phdiv[MAXNP+1]; | 
| 325 | double  tsize, psize; | 
| 326 | double  prob, frac, frac1; | 
| 327 | int     i, j, t, p; | 
| 328 | /* free old table */ | 
| 329 | if (sensor != NULL) | 
| 330 | free((void *)sensor); | 
| 331 | if (pvals != NULL) | 
| 332 | free((void *)pvals); | 
| 333 | if (sfile == NULL || !*sfile) { | 
| 334 | sensor = NULL; | 
| 335 | sntp[0] = sntp[1] = 0; | 
| 336 | pvals = NULL; | 
| 337 | ntheta = nphi = 0; | 
| 338 | return; | 
| 339 | } | 
| 340 | /* load sensor table */ | 
| 341 | sensor = load_sensor(sntp, sfile); | 
| 342 | if (sntp[0] > MAXNT) { | 
| 343 | sprintf(errmsg, "Too many theta rows in sensor file '%s'", | 
| 344 | sfile); | 
| 345 | error(INTERNAL, errmsg); | 
| 346 | } | 
| 347 | if (sntp[1] > MAXNP) { | 
| 348 | sprintf(errmsg, "Too many phi columns in sensor file '%s'", | 
| 349 | sfile); | 
| 350 | error(INTERNAL, errmsg); | 
| 351 | } | 
| 352 | /* compute boundary angles */ | 
| 353 | maxtheta = DEGREE*(1.5f*s_theta(sntp[0]-1) - 0.5f*s_theta(sntp[0]-2)); | 
| 354 | if (maxtheta > PI) | 
| 355 | maxtheta = PI; | 
| 356 | thdiv[0] = .0; | 
| 357 | for (t = 1; t < sntp[0]; t++) | 
| 358 | thdiv[t] = DEGREE/2.*(s_theta(t-1) + s_theta(t)); | 
| 359 | thdiv[sntp[0]] = maxtheta; | 
| 360 | phdiv[0] = DEGREE*(1.5f*s_phi(0) - 0.5f*s_phi(1)); | 
| 361 | for (p = 1; p < sntp[1]; p++) | 
| 362 | phdiv[p] = DEGREE/2.*(s_phi(p-1) + s_phi(p)); | 
| 363 | phdiv[sntp[1]] = DEGREE*(1.5f*s_phi(sntp[1]-1) - 0.5f*s_phi(sntp[1]-2)); | 
| 364 | /* size our table */ | 
| 365 | tsize = 1. - cos(maxtheta); | 
| 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); | 
| 370 | if (ntheta > MAXNT) | 
| 371 | ntheta = MAXNT; | 
| 372 | nphi = samptot/ntheta; | 
| 373 | pvals = (float *)malloc(sizeof(float)*(ntheta+1)*(nphi+1)); | 
| 374 | if (pvals == NULL) | 
| 375 | error(SYSTEM, "out of memory in init_ptable()"); | 
| 376 | gscale = .0;                    /* compute our inverse table */ | 
| 377 | for (i = 0; i < sntp[0]; i++) { | 
| 378 | rowp = &s_val(i,0); | 
| 379 | rowsum[i] = 1e-20; | 
| 380 | for (j = 0; j < sntp[1]; j++) | 
| 381 | rowsum[i] += *rowp++; | 
| 382 | rowomega[i] = cos(thdiv[i]) - cos(thdiv[i+1]); | 
| 383 | rowomega[i] *= 2.*PI / (double)sntp[1]; | 
| 384 | gscale += rowsum[i] * rowomega[i]; | 
| 385 | } | 
| 386 | if (gscale <= FTINY) { | 
| 387 | sprintf(errmsg, "Sensor values sum to zero in file '%s'", sfile); | 
| 388 | error(USER, errmsg); | 
| 389 | } | 
| 390 | for (i = 0; i < ntheta; i++) { | 
| 391 | prob = (double)i / (double)ntheta; | 
| 392 | for (t = 0; t < sntp[0]; t++) | 
| 393 | if ((prob -= rowsum[t]*rowomega[t]/gscale) <= .0) | 
| 394 | break; | 
| 395 | if (t >= sntp[0]) | 
| 396 | error(INTERNAL, "code error 1 in init_ptable()"); | 
| 397 | frac = 1. + prob/(rowsum[t]*rowomega[t]/gscale); | 
| 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 < sntp[0]-1) & (!t | (frac >= 0.5))) { | 
| 402 | frac -= 0.5; | 
| 403 | } else { | 
| 404 | frac += 0.5; | 
| 405 | --t; | 
| 406 | } | 
| 407 | pvals[i*(nphi+1)] = phdiv[0]; | 
| 408 | for (j = 1; j < nphi; j++) { | 
| 409 | prob = (double)j / (double)nphi; | 
| 410 | rowp = &s_val(t,0); | 
| 411 | rowp1 = &s_val(t+1,0); | 
| 412 | for (p = 0; p < sntp[1]; p++) | 
| 413 | if ((prob -= (1.-frac)*rowp[p]/rowsum[t] + | 
| 414 | frac*rowp1[p]/rowsum[t+1]) <= .0) | 
| 415 | break; | 
| 416 | if (p >= sntp[1]) {     /* should never happen? */ | 
| 417 | p = sntp[1] - 1; | 
| 418 | prob = .5; | 
| 419 | } | 
| 420 | frac1 = 1. + prob/((1.-frac)*rowp[p]/rowsum[t] | 
| 421 | + frac*rowp1[p]/rowsum[t+1]); | 
| 422 | pvals[i*(nphi+1) + j] = (1.-frac1)*phdiv[p] + | 
| 423 | frac1*phdiv[p+1]; | 
| 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 | } | 
| 433 |  | 
| 434 | /* Get normalized direction from random variables in [0,1) range */ | 
| 435 | static void | 
| 436 | get_direc( | 
| 437 | FVECT dvec, | 
| 438 | double  x, | 
| 439 | double  y | 
| 440 | ) | 
| 441 | { | 
| 442 | double  xfrac = x*ntheta; | 
| 443 | int     tndx = (int)xfrac; | 
| 444 | double  yfrac = y*nphi; | 
| 445 | int     pndx = (int)yfrac; | 
| 446 | double  rad, phi; | 
| 447 | FVECT   dv; | 
| 448 | int     i; | 
| 449 |  | 
| 450 | xfrac -= (double)tndx; | 
| 451 | yfrac -= (double)pndx; | 
| 452 | pndx += tndx*(nphi+1); | 
| 453 |  | 
| 454 | dv[2] = 1. - ((1.-xfrac)*tvals[tndx] + xfrac*tvals[tndx+1]); | 
| 455 | rad = sqrt(1. - dv[2]*dv[2]); | 
| 456 | phi = (1.-yfrac)*pvals[pndx] + yfrac*pvals[pndx+1]; | 
| 457 | dv[0] = -rad*sin(phi); | 
| 458 | dv[1] = rad*cos(phi); | 
| 459 | for (i = 3; i--; ) | 
| 460 | dvec[i] = dv[0]*ourview.hvec[i] + | 
| 461 | dv[1]*ourview.vvec[i] + | 
| 462 | dv[2]*ourview.vdir[i] ; | 
| 463 | } | 
| 464 |  | 
| 465 | /* Get sensor value in the specified direction (normalized) */ | 
| 466 | static float | 
| 467 | sens_val( | 
| 468 | FVECT   dvec | 
| 469 | ) | 
| 470 | { | 
| 471 | FVECT   dv; | 
| 472 | float   theta, phi; | 
| 473 | int     t, p; | 
| 474 |  | 
| 475 | dv[2] = DOT(dvec, ourview.vdir); | 
| 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 = 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./(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)) | 
| 491 | --t; | 
| 492 | while (p+1 < sntp[1] && phi >= s_phi(p+1)) | 
| 493 | ++p; | 
| 494 | while (p-1 >= 0 && phi <= s_phi(p-1)) | 
| 495 | --p; | 
| 496 | return(s_val(t,p)); | 
| 497 | } | 
| 498 |  | 
| 499 | /* Print origin and direction */ | 
| 500 | static void | 
| 501 | print_ray( | 
| 502 | FVECT rorg, | 
| 503 | FVECT rdir | 
| 504 | ) | 
| 505 | { | 
| 506 | printf("%.6g %.6g %.6g %.8f %.8f %.8f\n", | 
| 507 | rorg[0], rorg[1], rorg[2], | 
| 508 | rdir[0], rdir[1], rdir[2]); | 
| 509 | } | 
| 510 |  | 
| 511 | /* Compute sensor output */ | 
| 512 | static void | 
| 513 | comp_sensor( | 
| 514 | char *sfile | 
| 515 | ) | 
| 516 | { | 
| 517 | int     ndirs = dstrsrc > FTINY ? ndsamps : | 
| 518 | ndsamps > 0 ? 1 : 0; | 
| 519 | char    *err; | 
| 520 | int     nt, np; | 
| 521 | COLOR   vsum; | 
| 522 | RAY     rr; | 
| 523 | double  sf; | 
| 524 | int     i, j; | 
| 525 | /* set view */ | 
| 526 | ourview.type = VT_ANG; | 
| 527 | ourview.horiz = ourview.vert = 180.; | 
| 528 | ourview.hoff = ourview.voff = .0; | 
| 529 | err = setview(&ourview); | 
| 530 | if (err != NULL) | 
| 531 | error(USER, err); | 
| 532 | /* assign probability table */ | 
| 533 | init_ptable(sfile); | 
| 534 | /* stratified MC sampling */ | 
| 535 | setcolor(vsum, .0f, .0f, .0f); | 
| 536 | nt = (int)(sqrt((double)nsamps*ntheta/nphi) + .5); | 
| 537 | np = nsamps/nt; | 
| 538 | sf = gscale/nsamps; | 
| 539 | for (i = 0; i < nt; i++) | 
| 540 | for (j = 0; j < np; j++) { | 
| 541 | VCOPY(rr.rorg, ourview.vp); | 
| 542 | get_direc(rr.rdir, (i+frandom())/nt, (j+frandom())/np); | 
| 543 | if (ourview.vfore > FTINY) | 
| 544 | VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); | 
| 545 | if (!ray_pnprocs) { | 
| 546 | print_ray(rr.rorg, rr.rdir); | 
| 547 | continue; | 
| 548 | } | 
| 549 | rr.rmax = .0; | 
| 550 | rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL); | 
| 551 | scalecolor(rr.rcoef, sf); | 
| 552 | if (ray_pqueue(&rr) == 1) | 
| 553 | addcolor(vsum, rr.rcol); | 
| 554 | } | 
| 555 | /* remaining rays pure MC */ | 
| 556 | for (i = nsamps - nt*np; i-- > 0; ) { | 
| 557 | VCOPY(rr.rorg, ourview.vp); | 
| 558 | get_direc(rr.rdir, frandom(), frandom()); | 
| 559 | if (ourview.vfore > FTINY) | 
| 560 | VSUM(rr.rorg, rr.rorg, rr.rdir, ourview.vfore); | 
| 561 | if (!ray_pnprocs) { | 
| 562 | print_ray(rr.rorg, rr.rdir); | 
| 563 | continue; | 
| 564 | } | 
| 565 | rr.rmax = .0; | 
| 566 | rayorigin(&rr, PRIMARY|SPECULAR, NULL, NULL); | 
| 567 | scalecolor(rr.rcoef, sf); | 
| 568 | if (ray_pqueue(&rr) == 1) | 
| 569 | addcolor(vsum, rr.rcol); | 
| 570 | } | 
| 571 | if (!ray_pnprocs)                       /* just printing rays */ | 
| 572 | return; | 
| 573 | /* scale partial result */ | 
| 574 | scalecolor(vsum, sf); | 
| 575 | /* add direct component */ | 
| 576 | for (i = ndirs; i-- > 0; ) { | 
| 577 | SRCINDEX        si; | 
| 578 | initsrcindex(&si); | 
| 579 | while (srcray(&rr, NULL, &si)) { | 
| 580 | sf = sens_val(rr.rdir); | 
| 581 | if (sf <= FTINY) | 
| 582 | continue; | 
| 583 | sf *= si.dom/ndirs; | 
| 584 | scalecolor(rr.rcoef, sf); | 
| 585 | if (ray_pqueue(&rr) == 1) { | 
| 586 | multcolor(rr.rcol, rr.rcoef); | 
| 587 | addcolor(vsum, rr.rcol); | 
| 588 | } | 
| 589 | } | 
| 590 | } | 
| 591 | /* finish our calculation */ | 
| 592 | while (ray_presult(&rr, 0) > 0) { | 
| 593 | multcolor(rr.rcol, rr.rcoef); | 
| 594 | addcolor(vsum, rr.rcol); | 
| 595 | } | 
| 596 | /* print our result */ | 
| 597 | printf("%.4e %.4e %.4e\n", colval(vsum,RED), | 
| 598 | colval(vsum,GRN), colval(vsum,BLU)); | 
| 599 | } |