| 19 |  |  | 
| 20 |  | #include  "random.h" | 
| 21 |  |  | 
| 22 | + | #define  MINSAMPLES     5               /* minimum number of pretest samples */ | 
| 23 | + | #define  STESTMAX       30              /* maximum seeks per sample */ | 
| 24 |  |  | 
| 23 | – | #define  DISKTFRAC      0.25            /* disk area pretest fraction */ | 
| 25 |  |  | 
| 26 |  | double  getdisk(); | 
| 27 |  |  | 
| 104 |  | for (i = 0; i < vsmat->nproj; i++) | 
| 105 |  | if ((*vsmat->vproj)(proj, o, &source[sn], i)) | 
| 106 |  | if ((ns = makevsrc(o, sn, proj)) >= 0) { | 
| 107 | + | source[ns].sa.sv.pn = i; | 
| 108 |  | #ifdef DEBUG | 
| 109 |  | virtverb(ns, stderr); | 
| 110 |  | #endif | 
| 119 |  | register int  sn; | 
| 120 |  | MAT4  pm; | 
| 121 |  | { | 
| 122 | < | FVECT  nsloc, nsnorm, ocent; | 
| 123 | < | double  maxrad2; | 
| 122 | > | FVECT  nsloc, nsnorm, ocent, v; | 
| 123 | > | double  maxrad2, d; | 
| 124 |  | int  nsflags; | 
| 123 | – | double  d1; | 
| 125 |  | SPOT  theirspot, ourspot; | 
| 126 |  | register int  i; | 
| 127 |  |  | 
| 135 |  | if (source[sn].sflags & SPROX) | 
| 136 |  | return(-1);             /* should never get here! */ | 
| 137 |  | multv3(nsloc, source[sn].sloc, pm); | 
| 138 | + | normalize(nsloc); | 
| 139 |  | VCOPY(ourspot.aim, ocent); | 
| 140 |  | ourspot.siz = PI*maxrad2; | 
| 141 |  | ourspot.flen = 0.; | 
| 142 |  | if (source[sn].sflags & SSPOT) { | 
| 141 | – | copystruct(&theirspot, source[sn].sl.s); | 
| 143 |  | multp3(theirspot.aim, source[sn].sl.s->aim, pm); | 
| 144 | + | d = sqrt(dist2(ourspot.aim, theirspot.aim)); | 
| 145 | + | d = sqrt(source[sn].sl.s->siz/PI) + d*source[sn].ss; | 
| 146 | + | theirspot.siz = PI*d*d; | 
| 147 | + | ourspot.flen = theirspot.flen = source[sn].sl.s->flen; | 
| 148 | + | d = ourspot.siz; | 
| 149 |  | if (!commonbeam(&ourspot, &theirspot, nsloc)) | 
| 150 | < | return(-1);             /* no overlap */ | 
| 150 | > | return(-1);     /* no overlap */ | 
| 151 | > | if (ourspot.siz < d-FTINY) {    /* it shrunk */ | 
| 152 | > | d = beamdisk(v, op, &ourspot, nsloc); | 
| 153 | > | if (d <= FTINY) | 
| 154 | > | return(-1); | 
| 155 | > | if (d < maxrad2) { | 
| 156 | > | maxrad2 = d; | 
| 157 | > | VCOPY(ocent, v); | 
| 158 | > | } | 
| 159 | > | } | 
| 160 |  | } | 
| 161 |  | } else {                                /* local source */ | 
| 162 |  | multp3(nsloc, source[sn].sloc, pm); | 
| 163 |  | for (i = 0; i < 3; i++) | 
| 164 |  | ourspot.aim[i] = ocent[i] - nsloc[i]; | 
| 165 | < | if ((d1 = normalize(ourspot.aim)) == 0.) | 
| 165 | > | if ((d = normalize(ourspot.aim)) == 0.) | 
| 166 |  | return(-1);             /* at source!! */ | 
| 167 | < | if (source[sn].sflags & SPROX && d1 > source[sn].sl.prox) | 
| 167 | > | if (source[sn].sflags & SPROX && d > source[sn].sl.prox) | 
| 168 |  | return(-1);             /* too far away */ | 
| 154 | – | ourspot.siz = 2.*PI*(1. - d1/sqrt(d1*d1+maxrad2)); | 
| 169 |  | ourspot.flen = 0.; | 
| 170 | + | d = (sqrt(maxrad2) + source[sn].ss) / d; | 
| 171 | + | if (d < 1.-FTINY) | 
| 172 | + | ourspot.siz = 2.*PI*(1. - sqrt(1.-d*d)); | 
| 173 | + | else | 
| 174 | + | nsflags &= ~SSPOT; | 
| 175 |  | if (source[sn].sflags & SSPOT) { | 
| 176 |  | copystruct(&theirspot, source[sn].sl.s); | 
| 177 |  | multv3(theirspot.aim, source[sn].sl.s->aim, pm); | 
| 178 | < | if (!commonspot(&ourspot, &theirspot, nsloc)) | 
| 179 | < | return(-1);     /* no overlap */ | 
| 180 | < | ourspot.flen = theirspot.flen; | 
| 178 | > | normalize(theirspot.aim); | 
| 179 | > | if (nsflags & SSPOT) { | 
| 180 | > | ourspot.flen = theirspot.flen; | 
| 181 | > | d = ourspot.siz; | 
| 182 | > | if (!commonspot(&ourspot, &theirspot, nsloc)) | 
| 183 | > | return(-1);     /* no overlap */ | 
| 184 | > | } else { | 
| 185 | > | nsflags |= SSPOT; | 
| 186 | > | copystruct(&ourspot, &theirspot); | 
| 187 | > | d = 2.*ourspot.siz; | 
| 188 | > | } | 
| 189 | > | if (ourspot.siz < d-FTINY) {    /* it shrunk */ | 
| 190 | > | d = spotdisk(v, op, &ourspot, nsloc); | 
| 191 | > | if (d <= FTINY) | 
| 192 | > | return(-1); | 
| 193 | > | if (d < maxrad2) { | 
| 194 | > | maxrad2 = d; | 
| 195 | > | VCOPY(ocent, v); | 
| 196 | > | } | 
| 197 | > | } | 
| 198 |  | } | 
| 199 |  | if (source[sn].sflags & SFLAT) {        /* behind source? */ | 
| 200 |  | multv3(nsnorm, source[sn].snorm, pm); | 
| 201 | < | if (checkspot(&ourspot, nsnorm) < 0) | 
| 201 | > | normalize(nsnorm); | 
| 202 | > | if (!checkspot(&ourspot, nsnorm)) | 
| 203 |  | return(-1); | 
| 204 |  | } | 
| 205 |  | } | 
| 215 |  | if (nsflags & SFLAT) | 
| 216 |  | VCOPY(source[i].snorm, nsnorm); | 
| 217 |  | source[i].ss = source[sn].ss; source[i].ss2 = source[sn].ss2; | 
| 218 | < | if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) | 
| 219 | < | goto memerr; | 
| 220 | < | copystruct(source[i].sl.s, &ourspot); | 
| 218 | > | if (nsflags & SSPOT) { | 
| 219 | > | if ((source[i].sl.s = (SPOT *)malloc(sizeof(SPOT))) == NULL) | 
| 220 | > | goto memerr; | 
| 221 | > | copystruct(source[i].sl.s, &ourspot); | 
| 222 | > | } | 
| 223 |  | if (nsflags & SPROX) | 
| 224 |  | source[i].sl.prox = source[sn].sl.prox; | 
| 225 | < | source[i].sa.svnext = sn; | 
| 225 | > | source[i].sa.sv.sn = sn; | 
| 226 |  | source[i].so = op; | 
| 227 |  | return(i); | 
| 228 |  | memerr: | 
| 239 |  | double  rad2, roffs, offs, d, rd, rdoto; | 
| 240 |  | FVECT  rnrm, nrm; | 
| 241 |  | /* first, use object getdisk function */ | 
| 242 | < | rad2 = (*sfun[op->otype].of->getdisk)(oc, op); | 
| 242 | > | rad2 = getmaxdisk(oc, op); | 
| 243 |  | if (!(source[sn].sflags & SVIRTUAL)) | 
| 244 |  | return(rad2);           /* all done for normal source */ | 
| 245 |  | /* check for correct side of relay surface */ | 
| 246 | < | roffs = (*sfun[source[sn].so->otype].of->getpleq)(rnrm, source[sn].so); | 
| 246 | > | roffs = getplaneq(rnrm, source[sn].so); | 
| 247 |  | rd = DOT(rnrm, source[sn].sloc);        /* source projection */ | 
| 248 |  | if (!(source[sn].sflags & SDISTANT)) | 
| 249 |  | rd -= roffs; | 
| 251 |  | if ((d > 0.) ^ (rd > 0.)) | 
| 252 |  | return(rad2);           /* OK if opposite sides */ | 
| 253 |  | if (d*d >= rad2) | 
| 254 | < | return(.0);             /* no relay is possible */ | 
| 254 | > | return(0.);             /* no relay is possible */ | 
| 255 |  | /* we need a closer look */ | 
| 256 | < | offs = (*sfun[op->otype].of->getpleq)(nrm, op); | 
| 256 | > | offs = getplaneq(nrm, op); | 
| 257 |  | rdoto = DOT(rnrm, nrm); | 
| 258 |  | if (d*d >= rad2*(1.-rdoto*rdoto)) | 
| 259 |  | return(0.);             /* disk entirely on projection side */ | 
| 274 |  | FVECT  onorm; | 
| 275 |  | FVECT  offsdir; | 
| 276 |  | double  or, d; | 
| 277 | < | int  nok, nhit; | 
| 277 | > | int  infront; | 
| 278 | > | int  stestlim, ssn; | 
| 279 | > | int  nhit, nok; | 
| 280 |  | register int  i, n; | 
| 281 |  | /* return if pretesting disabled */ | 
| 282 |  | if (vspretest <= 0) | 
| 283 |  | return(f); | 
| 284 |  | /* get surface normal */ | 
| 285 | < | (*sfun[o->otype].of->getpleq)(onorm, o); | 
| 285 | > | getplaneq(onorm, o); | 
| 286 |  | /* set number of rays to sample */ | 
| 287 | < | if (source[sn].sflags & SDISTANT) | 
| 287 | > | if (source[sn].sflags & SDISTANT) { | 
| 288 |  | n = (2./3.*PI*PI)*or2/(thescene.cusize*thescene.cusize)* | 
| 289 |  | vspretest + .5; | 
| 290 | < | else | 
| 291 | < | n = or2/dist2(oc,source[sn].sloc)*vspretest + .5; | 
| 292 | < | if (n < 1) n = 1; | 
| 293 | < | /* limit tests to central region */ | 
| 294 | < | or = DISKTFRAC*sqrt(or2); | 
| 290 | > | infront = DOT(onorm, source[sn].sloc) > 0.; | 
| 291 | > | } else { | 
| 292 | > | for (i = 0; i < 3; i++) | 
| 293 | > | offsdir[i] = source[sn].sloc[i] - oc[i]; | 
| 294 | > | n = or2/DOT(offsdir,offsdir)*vspretest + .5; | 
| 295 | > | infront = DOT(onorm, offsdir) > 0.; | 
| 296 | > | } | 
| 297 | > | if (n < MINSAMPLES) n = MINSAMPLES; | 
| 298 | > | #ifdef DEBUG | 
| 299 | > | fprintf(stderr, "pretesting source %d in object %s with %d rays\n", | 
| 300 | > | sn, o->oname, n); | 
| 301 | > | #endif | 
| 302 |  | /* sample */ | 
| 303 | + | or = sqrt(or2); | 
| 304 | + | stestlim = n*STESTMAX; | 
| 305 | + | ssn = 0; | 
| 306 |  | nhit = nok = 0; | 
| 307 |  | while (n-- > 0) { | 
| 308 | < | samplendx++; | 
| 309 | < | /* | 
| 310 | < | * We're being real sloppy with our sample locations here. | 
| 311 | < | */ | 
| 312 | < | for (i = 0; i < 3; i++) | 
| 313 | < | offsdir[i] = or*(1. - 2.*urand(931*i+5821+n)); | 
| 314 | < | d = DOT(offsdir,onorm); | 
| 315 | < | for (i = 0; i < 3; i++) | 
| 316 | < | sr.rorg[i] = oc[i] + (1.-d)*offsdir[i]; | 
| 308 | > | /* get sample point */ | 
| 309 | > | do { | 
| 310 | > | if (ssn >= stestlim) { | 
| 311 | > | #ifdef DEBUG | 
| 312 | > | fprintf(stderr, "\ttoo hard to hit\n"); | 
| 313 | > | #endif | 
| 314 | > | return(f);      /* too small a target! */ | 
| 315 | > | } | 
| 316 | > | for (i = 0; i < 3; i++) | 
| 317 | > | offsdir[i] = or*(1. - | 
| 318 | > | 2.*urand(urind(931*i+5827,ssn))); | 
| 319 | > | ssn++; | 
| 320 | > | for (i = 0; i < 3; i++) | 
| 321 | > | sr.rorg[i] = oc[i] + offsdir[i]; | 
| 322 | > | d = DOT(offsdir,onorm); | 
| 323 | > | if (infront) | 
| 324 | > | for (i = 0; i < 3; i++) { | 
| 325 | > | sr.rorg[i] -= (d-.0001)*onorm[i]; | 
| 326 | > | sr.rdir[i] = -onorm[i]; | 
| 327 | > | } | 
| 328 | > | else | 
| 329 | > | for (i = 0; i < 3; i++) { | 
| 330 | > | sr.rorg[i] -= (d+.0001)*onorm[i]; | 
| 331 | > | sr.rdir[i] = onorm[i]; | 
| 332 | > | } | 
| 333 | > | rayorigin(&sr, NULL, PRIMARY, 1.0); | 
| 334 | > | } while (!(*ofun[o->otype].funp)(o, &sr)); | 
| 335 |  | /* check against source */ | 
| 336 | < | if (srcray(&sr, NULL, sn) == 0.0) | 
| 336 | > | samplendx++; | 
| 337 | > | if (srcray(&sr, NULL, sn) == 0.) | 
| 338 |  | continue; | 
| 339 |  | sr.revf = srcvalue; | 
| 340 |  | rayvalue(&sr); | 
| 342 |  | continue; | 
| 343 |  | nok++; | 
| 344 |  | /* check against obstructions */ | 
| 345 | < | srcray(&sr, NULL, sn); | 
| 345 | > | rayclear(&sr); | 
| 346 | > | sr.revf = raytrace; | 
| 347 |  | rayvalue(&sr); | 
| 348 | < | if (bright(sr.rcol) <= FTINY) | 
| 349 | < | continue; | 
| 350 | < | nhit++; | 
| 348 | > | if (bright(sr.rcol) > FTINY) | 
| 349 | > | nhit++; | 
| 350 | > | if (nhit > 0 && nhit < nok) { | 
| 351 | > | #ifdef DEBUG | 
| 352 | > | fprintf(stderr, "\tpartially occluded\n"); | 
| 353 | > | #endif | 
| 354 | > | return(f);              /* need to shadow test */ | 
| 355 | > | } | 
| 356 |  | } | 
| 357 | < | /* interpret results */ | 
| 358 | < | if (nhit == 0) | 
| 357 | > | if (nhit == 0) { | 
| 358 | > | #ifdef DEBUG | 
| 359 | > | fprintf(stderr, "\t0%% hit rate\n"); | 
| 360 | > | #endif | 
| 361 |  | return(f | SSKIP);      /* 0% hit rate:  totally occluded */ | 
| 362 | < | if (nhit == nok) | 
| 363 | < | return(f & ~SFOLLOW);   /* 100% hit rate:  no occlusion */ | 
| 364 | < | return(f);              /* no comment */ | 
| 362 | > | } | 
| 363 | > | #ifdef DEBUG | 
| 364 | > | fprintf(stderr, "\t100%% hit rate\n"); | 
| 365 | > | #endif | 
| 366 | > | return(f & ~SFOLLOW);           /* 100% hit rate:  no occlusion */ | 
| 367 |  | } | 
| 368 |  |  | 
| 369 |  |  | 
| 381 |  | fprintf(fp, "\tat (%f,%f,%f)\n", | 
| 382 |  | source[sn].sloc[0], source[sn].sloc[1], source[sn].sloc[2]); | 
| 383 |  | fprintf(fp, "\tlinked to source %d (%s)\n", | 
| 384 | < | source[sn].sa.svnext, source[source[sn].sa.svnext].so->oname); | 
| 384 | > | source[sn].sa.sv.sn, source[source[sn].sa.sv.sn].so->oname); | 
| 385 |  | if (source[sn].sflags & SFOLLOW) | 
| 386 |  | fprintf(fp, "\talways followed\n"); | 
| 387 |  | else |