1 |
+ |
#ifndef lint |
2 |
+ |
static const char RCSid[] = "$Id$"; |
3 |
+ |
#endif |
4 |
|
/* |
5 |
|
================================================================== |
6 |
|
Photon map data structures and kd-tree handling |
11 |
|
supported by the Swiss National Science Foundation (SNSF, #147053) |
12 |
|
================================================================== |
13 |
|
|
11 |
– |
$Id$ |
14 |
|
*/ |
15 |
|
|
16 |
|
|
21 |
|
#include "otypes.h" |
22 |
|
#include "source.h" |
23 |
|
#include "rcontrib.h" |
24 |
+ |
#include "random.h" |
25 |
|
|
26 |
|
|
27 |
|
|
69 |
|
const PhotonPrimary* addPhotonPrimary (PhotonMap *pmap, const RAY *ray) |
70 |
|
{ |
71 |
|
PhotonPrimary *prim = NULL; |
72 |
+ |
FVECT dvec; |
73 |
|
|
74 |
|
if (!pmap || !ray) |
75 |
|
return NULL; |
102 |
|
prim -> srcIdx = -1; |
103 |
|
|
104 |
|
/* Reverse incident direction to point to light source */ |
105 |
< |
prim -> dir [0] = -ray -> rdir [0]; |
106 |
< |
prim -> dir [1] = -ray -> rdir [1]; |
107 |
< |
prim -> dir [2] = -ray -> rdir [2]; |
105 |
> |
dvec [0] = -ray -> rdir [0]; |
106 |
> |
dvec [1] = -ray -> rdir [1]; |
107 |
> |
dvec [2] = -ray -> rdir [2]; |
108 |
> |
prim -> dir = encodedir(dvec); |
109 |
|
|
110 |
< |
VCOPY(prim -> org, ray -> rorg); |
110 |
> |
VCOPY(prim -> pos, ray -> rop); |
111 |
|
|
112 |
|
return prim; |
113 |
|
} |
232 |
|
} |
233 |
|
|
234 |
|
/* Reject photon if normal faces away (ignored for volume photons) */ |
235 |
< |
if (norm && DOT(norm, p -> norm) <= 0) |
235 |
> |
if (norm && DOT(norm, p -> norm) <= 0.5 * frandom()) |
236 |
|
return; |
237 |
|
|
238 |
|
if (isContribPmap(pmap) && pmap -> srcContrib) { |
243 |
|
if (srcIdx < 0 || srcIdx >= nsources) |
244 |
|
error(INTERNAL, "invalid light source index in photon map"); |
245 |
|
|
246 |
< |
srcMod = objptr(source [srcIdx].so -> omod); |
246 |
> |
srcMod = findmaterial(source [srcIdx].so); |
247 |
|
|
248 |
|
/* Reject photon if contributions from light source which emitted it |
249 |
|
* are not sought */ |
314 |
|
/* Threshold below which we assume increasing max radius won't help */ |
315 |
|
#define PMAP_SHORT_LOOKUP_THRESH 1 |
316 |
|
|
317 |
+ |
/* Coefficient for adaptive maximum search radius */ |
318 |
+ |
#define PMAP_MAXDIST_COEFF 100 |
319 |
+ |
|
320 |
+ |
|
321 |
|
void findPhotons (PhotonMap* pmap, const RAY* ray) |
322 |
|
{ |
323 |
|
float pos [3], norm [3]; |
339 |
|
pmap -> minError = FHUGE; |
340 |
|
pmap -> maxError = -FHUGE; |
341 |
|
pmap -> rmsError = 0; |
342 |
< |
#ifdef PMAP_MAXDIST_ABS |
343 |
< |
/* Treat maxDistCoeff as an *absolute* and *fixed* max search radius. |
335 |
< |
Primarily intended for debugging; FOR ZE ECKSPURTZ ONLY! */ |
336 |
< |
pmap -> maxDist0 = pmap -> maxDistLimit = maxDistCoeff; |
337 |
< |
#else |
338 |
< |
/* Maximum search radius limit based on avg photon distance to |
339 |
< |
* centre of gravity */ |
342 |
> |
/* SQUARED max search radius limit is based on avg photon distance to |
343 |
> |
* centre of gravity, unless fixed by user (maxDistFix > 0) */ |
344 |
|
pmap -> maxDist0 = pmap -> maxDistLimit = |
345 |
< |
maxDistCoeff * pmap -> squeueSize * pmap -> CoGdist / |
346 |
< |
pmap -> heapSize; |
347 |
< |
#endif |
345 |
> |
maxDistFix > 0 ? maxDistFix * maxDistFix |
346 |
> |
: PMAP_MAXDIST_COEFF * pmap -> squeueSize * |
347 |
> |
pmap -> CoGdist / pmap -> heapSize; |
348 |
|
} |
349 |
|
|
350 |
|
do { |
364 |
|
nearestNeighbours(pmap, pos, norm, 1); |
365 |
|
} |
366 |
|
|
367 |
< |
#ifndef PMAP_MAXDIST_ABS |
367 |
> |
if (pmap -> maxDist < FTINY) { |
368 |
> |
sprintf(errmsg, "itsy bitsy teeny weeny photon search radius %e", |
369 |
> |
sqrt(pmap -> maxDist)); |
370 |
> |
error(WARNING, errmsg); |
371 |
> |
} |
372 |
> |
|
373 |
|
if (pmap -> squeueEnd < pmap -> squeueSize * pmap -> gatherTolerance) { |
374 |
|
/* Short lookup; too few photons found */ |
375 |
|
if (pmap -> squeueEnd > PMAP_SHORT_LOOKUP_THRESH) { |
377 |
|
* PMAP_SHORT_LOOKUP_THRESH photons under the assumption there |
378 |
|
* really are no photons in the vicinity, and increasing the max |
379 |
|
* search radius therefore won't help */ |
380 |
< |
#ifdef PMAP_LOOKUP_WARN |
380 |
> |
#ifdef PMAP_LOOKUP_WARN |
381 |
|
sprintf(errmsg, |
382 |
|
"%d/%d %s photons found at (%.2f,%.2f,%.2f) on %s", |
383 |
|
pmap -> squeueEnd, pmap -> squeueSize, |
384 |
|
pmapName [pmap -> type], pos [0], pos [1], pos [2], |
385 |
|
ray -> ro ? ray -> ro -> oname : "<null>"); |
386 |
|
error(WARNING, errmsg); |
387 |
< |
#endif |
387 |
> |
#endif |
388 |
|
|
389 |
+ |
/* Bail out after warning if maxDist is fixed */ |
390 |
+ |
if (maxDistFix > 0) |
391 |
+ |
return; |
392 |
+ |
|
393 |
|
if (pmap -> maxDist0 < pmap -> maxDistLimit) { |
394 |
|
/* Increase max search radius if below limit & redo search */ |
395 |
|
pmap -> maxDist0 *= PMAP_MAXDIST_INC; |
396 |
< |
#ifdef PMAP_LOOKUP_REDO |
396 |
> |
#ifdef PMAP_LOOKUP_REDO |
397 |
|
redo = 1; |
398 |
< |
#endif |
399 |
< |
|
387 |
< |
#ifdef PMAP_LOOKUP_WARN |
398 |
> |
#endif |
399 |
> |
#ifdef PMAP_LOOKUP_WARN |
400 |
|
sprintf(errmsg, |
401 |
|
redo ? "restarting photon lookup with max radius %.1e" |
402 |
|
: "max photon lookup radius adjusted to %.1e", |
403 |
< |
pmap -> maxDist0); |
403 |
> |
sqrt(pmap -> maxDist0)); |
404 |
|
error(WARNING, errmsg); |
405 |
< |
#endif |
405 |
> |
#endif |
406 |
|
} |
407 |
< |
#ifdef PMAP_LOOKUP_REDO |
407 |
> |
#ifdef PMAP_LOOKUP_REDO |
408 |
|
else { |
409 |
|
sprintf(errmsg, "max photon lookup radius clamped to %.1e", |
410 |
< |
pmap -> maxDist0); |
410 |
> |
sqrt(pmap -> maxDist0)); |
411 |
|
error(WARNING, errmsg); |
412 |
|
} |
413 |
< |
#endif |
413 |
> |
#endif |
414 |
|
} |
415 |
|
|
416 |
|
/* Reset successful lookup counter */ |
417 |
|
pmap -> numLookups = 0; |
418 |
|
} |
419 |
|
else { |
420 |
+ |
/* Bail out after warning if maxDist is fixed */ |
421 |
+ |
if (maxDistFix > 0) |
422 |
+ |
return; |
423 |
+ |
|
424 |
|
/* Increment successful lookup counter and reduce max search radius if |
425 |
|
* wraparound */ |
426 |
|
pmap -> numLookups = (pmap -> numLookups + 1) % PMAP_MAXDIST_CNT; |
429 |
|
|
430 |
|
redo = 0; |
431 |
|
} |
432 |
< |
#endif |
432 |
> |
|
433 |
|
} while (redo); |
434 |
|
} |
435 |
|
|
469 |
|
dv [2] = pos [2] - p -> pos [2]; |
470 |
|
d2 = DOT(dv, dv); |
471 |
|
|
472 |
< |
if (d2 < pmap -> maxDist && DOT(norm, p -> norm) > 0) { |
472 |
> |
if (d2 < pmap -> maxDist && DOT(norm, p -> norm) > 0.5 * frandom()) { |
473 |
|
/* Closest photon so far with similar normal */ |
474 |
|
pmap -> maxDist = d2; |
475 |
|
*photon = p; |