45 |
|
#include "pmapkdt.c" |
46 |
|
#endif |
47 |
|
|
48 |
+ |
/* Ambient include/exclude set (from ambient.c) */ |
49 |
+ |
#ifndef MAXASET |
50 |
+ |
#define MAXASET 4095 |
51 |
+ |
#endif |
52 |
+ |
extern OBJECT ambset [MAXASET+1]; |
53 |
|
|
54 |
+ |
/* Callback to print photon attributes acc. to user defined format */ |
55 |
+ |
int (*printPhoton)(RAY *r, Photon *p, PhotonMap *pm); |
56 |
|
|
57 |
+ |
|
58 |
+ |
|
59 |
|
void initPhotonMap (PhotonMap *pmap, PhotonMapType t) |
60 |
|
/* Init photon map 'n' stuff... */ |
61 |
|
{ |
205 |
|
|
206 |
|
int newPhoton (PhotonMap* pmap, const RAY* ray) |
207 |
|
{ |
208 |
< |
unsigned i, inROI = 0; |
208 |
> |
unsigned i; |
209 |
|
Photon photon; |
210 |
|
COLOR photonFlux; |
211 |
|
|
217 |
|
if (ray -> robj > -1 && islight(objptr(ray -> ro -> omod) -> otype)) |
218 |
|
return -1; |
219 |
|
|
220 |
< |
/* Store photon if within a region of interest (for ze Ecksperts!) */ |
221 |
< |
if (!pmapNumROI || !pmapROI) |
222 |
< |
inROI = 1; |
223 |
< |
else { |
220 |
> |
/* Ignore photon if modifier in/outside exclude/include set */ |
221 |
> |
if (ambincl != -1 && ray -> ro && |
222 |
> |
ambincl != inset(ambset, ray -> ro -> omod)) |
223 |
> |
return -1; |
224 |
> |
|
225 |
> |
if (pmapNumROI && pmapROI) { |
226 |
> |
unsigned inROI = 0; |
227 |
> |
|
228 |
> |
/* Store photon if within a region of interest (for ze Ecksperts!) */ |
229 |
|
for (i = 0; !inROI && i < pmapNumROI; i++) |
230 |
|
inROI = (ray -> rop [0] >= pmapROI [i].min [0] && |
231 |
|
ray -> rop [0] <= pmapROI [i].max [0] && |
233 |
|
ray -> rop [1] <= pmapROI [i].max [1] && |
234 |
|
ray -> rop [2] >= pmapROI [i].min [2] && |
235 |
|
ray -> rop [2] <= pmapROI [i].max [2]); |
236 |
+ |
if (!inROI) |
237 |
+ |
return -1; |
238 |
|
} |
239 |
< |
|
240 |
< |
if (inROI) { |
241 |
< |
/* Adjust flux according to distribution ratio and ray weight */ |
242 |
< |
copycolor(photonFlux, ray -> rcol); |
243 |
< |
scalecolor(photonFlux, |
244 |
< |
ray -> rweight / (pmap -> distribRatio ? pmap -> distribRatio |
245 |
< |
: 1)); |
246 |
< |
setPhotonFlux(&photon, photonFlux); |
247 |
< |
|
248 |
< |
/* Set photon position and flags */ |
249 |
< |
VCOPY(photon.pos, ray -> rop); |
250 |
< |
photon.flags = 0; |
251 |
< |
photon.caustic = PMAP_CAUSTICRAY(ray); |
239 |
> |
|
240 |
> |
/* Adjust flux according to distribution ratio and ray weight */ |
241 |
> |
copycolor(photonFlux, ray -> rcol); |
242 |
> |
#if 0 |
243 |
> |
/* Factored out ray -> rweight as deprecated (?) for pmap, and infact |
244 |
> |
erroneously attenuates volume photon flux based on extinction, |
245 |
> |
which is already factored in by photonParticipate() */ |
246 |
> |
scalecolor(photonFlux, |
247 |
> |
ray -> rweight / (pmap -> distribRatio ? pmap -> distribRatio |
248 |
> |
: 1)); |
249 |
> |
#else |
250 |
> |
scalecolor(photonFlux, |
251 |
> |
1.0 / (pmap -> distribRatio ? pmap -> distribRatio : 1)); |
252 |
> |
#endif |
253 |
> |
setPhotonFlux(&photon, photonFlux); |
254 |
|
|
255 |
< |
/* Set contrib photon's primary ray and subprocess index (the latter |
256 |
< |
* to linearise the primary ray indices after photon distribution is |
257 |
< |
* complete). Also set primary ray's source index, thereby marking it |
258 |
< |
* as used. */ |
241 |
< |
if (isContribPmap(pmap)) { |
242 |
< |
photon.primary = pmap -> numPrimary; |
243 |
< |
photon.proc = PMAP_GETRAYPROC(ray); |
244 |
< |
pmap -> lastPrimary.srcIdx = ray -> rsrc; |
245 |
< |
} |
246 |
< |
else photon.primary = 0; |
247 |
< |
|
248 |
< |
/* Set normal */ |
249 |
< |
for (i = 0; i <= 2; i++) |
250 |
< |
photon.norm [i] = 127.0 * (isVolumePmap(pmap) ? ray -> rdir [i] |
251 |
< |
: ray -> ron [i]); |
255 |
> |
/* Set photon position and flags */ |
256 |
> |
VCOPY(photon.pos, ray -> rop); |
257 |
> |
photon.flags = 0; |
258 |
> |
photon.caustic = PMAP_CAUSTICRAY(ray); |
259 |
|
|
260 |
< |
if (!pmap -> heapBuf) { |
261 |
< |
/* Lazily allocate heap buffa */ |
260 |
> |
/* Set contrib photon's primary ray and subprocess index (the latter |
261 |
> |
* to linearise the primary ray indices after photon distribution is |
262 |
> |
* complete). Also set primary ray's source index, thereby marking it |
263 |
> |
* as used. */ |
264 |
> |
if (isContribPmap(pmap)) { |
265 |
> |
photon.primary = pmap -> numPrimary; |
266 |
> |
photon.proc = PMAP_GETRAYPROC(ray); |
267 |
> |
pmap -> lastPrimary.srcIdx = ray -> rsrc; |
268 |
> |
} |
269 |
> |
else photon.primary = 0; |
270 |
> |
|
271 |
> |
/* Set normal */ |
272 |
> |
for (i = 0; i <= 2; i++) |
273 |
> |
photon.norm [i] = 127.0 * (isVolumePmap(pmap) ? ray -> rdir [i] |
274 |
> |
: ray -> ron [i]); |
275 |
> |
|
276 |
> |
if (!pmap -> heapBuf) { |
277 |
> |
/* Lazily allocate heap buffa */ |
278 |
|
#if NIX |
279 |
< |
/* Randomise buffa size to temporally decorellate flushes in |
280 |
< |
* multiprocessing mode */ |
281 |
< |
srandom(randSeed + getpid()); |
282 |
< |
pmap -> heapBufSize = PMAP_HEAPBUFSIZE * (0.5 + frandom()); |
279 |
> |
/* Randomise buffa size to temporally decorellate flushes in |
280 |
> |
* multiprocessing mode */ |
281 |
> |
srandom(randSeed + getpid()); |
282 |
> |
pmap -> heapBufSize = PMAP_HEAPBUFSIZE * (0.5 + frandom()); |
283 |
|
#else |
284 |
< |
/* Randomisation disabled for single processes on WIN; also useful |
285 |
< |
* for reproducability during debugging */ |
286 |
< |
pmap -> heapBufSize = PMAP_HEAPBUFSIZE; |
284 |
> |
/* Randomisation disabled for single processes on WIN; also useful |
285 |
> |
* for reproducability during debugging */ |
286 |
> |
pmap -> heapBufSize = PMAP_HEAPBUFSIZE; |
287 |
|
#endif |
288 |
< |
if (!(pmap -> heapBuf = calloc(pmap -> heapBufSize, sizeof(Photon)))) |
289 |
< |
error(SYSTEM, "failed heap buffer allocation in newPhoton"); |
290 |
< |
pmap -> heapBufLen = 0; |
291 |
< |
} |
288 |
> |
if (!(pmap -> heapBuf = calloc(pmap -> heapBufSize, sizeof(Photon)))) |
289 |
> |
error(SYSTEM, "failed heap buffer allocation in newPhoton"); |
290 |
> |
pmap -> heapBufLen = 0; |
291 |
> |
} |
292 |
|
|
293 |
< |
/* Photon initialised; now append to heap buffa */ |
294 |
< |
memcpy(pmap -> heapBuf + pmap -> heapBufLen, &photon, sizeof(Photon)); |
295 |
< |
|
296 |
< |
if (++pmap -> heapBufLen >= pmap -> heapBufSize) |
297 |
< |
/* Heap buffa full, flush to heap file */ |
298 |
< |
flushPhotonHeap(pmap); |
293 |
> |
/* Photon initialised; now append to heap buffa */ |
294 |
> |
memcpy(pmap -> heapBuf + pmap -> heapBufLen, &photon, sizeof(Photon)); |
295 |
> |
|
296 |
> |
if (++pmap -> heapBufLen >= pmap -> heapBufSize) |
297 |
> |
/* Heap buffa full, flush to heap file */ |
298 |
> |
flushPhotonHeap(pmap); |
299 |
|
|
300 |
< |
pmap -> numPhotons++; |
301 |
< |
} |
300 |
> |
pmap -> numPhotons++; |
301 |
> |
|
302 |
> |
/* Print photon attributes */ |
303 |
> |
if (printPhoton) |
304 |
> |
/* Non-const kludge */ |
305 |
> |
printPhoton((RAY*)ray, &photon, pmap); |
306 |
|
|
307 |
|
return 0; |
308 |
|
} |
399 |
|
/* Scale photon's flux (hitherto normalised to 1 over RGB); in |
400 |
|
* case of a contrib photon map, this is done per light source, |
401 |
|
* and photonFlux is assumed to be an array */ |
402 |
< |
getPhotonFlux(p, flux); |
402 |
> |
getPhotonFlux(p, flux); |
403 |
|
|
404 |
|
if (photonFlux) { |
405 |
|
scalecolor(flux, photonFlux [isContribPmap(pmap) ? |
517 |
|
|
518 |
|
/* Search position is ray -> rorg for volume photons, since we have no |
519 |
|
intersection point. Normals are ignored -- these are incident |
520 |
< |
directions). */ |
520 |
> |
directions). */ |
521 |
> |
/* NOTE: status returned by XXX_FindPhotons() is currently ignored; |
522 |
> |
if no photons are found, an empty queue is returned under the |
523 |
> |
assumption all photons are too distant to contribute significant |
524 |
> |
flux. */ |
525 |
|
if (isVolumePmap(pmap)) { |
526 |
|
#ifdef PMAP_OOC |
527 |
|
OOC_FindPhotons(pmap, ray -> rorg, NULL); |
611 |
|
|
612 |
|
|
613 |
|
|
614 |
< |
void find1Photon (PhotonMap *pmap, const RAY* ray, Photon *photon) |
614 |
> |
Photon *find1Photon (PhotonMap *pmap, const RAY* ray, Photon *photon) |
615 |
|
{ |
616 |
< |
pmap -> maxDist2 = thescene.cusize; /* ? */ |
616 |
> |
/* Init (squared) search radius to avg photon dist to centre of gravity */ |
617 |
> |
float maxDist2_0 = pmap -> CoGdist; |
618 |
> |
int found = 0; |
619 |
> |
#ifdef PMAP_LOOKUP_REDO |
620 |
> |
#define REDO 1 |
621 |
> |
#else |
622 |
> |
#define REDO 0 |
623 |
> |
#endif |
624 |
|
|
625 |
+ |
do { |
626 |
+ |
pmap -> maxDist2 = maxDist2_0; |
627 |
|
#ifdef PMAP_OOC |
628 |
< |
OOC_Find1Photon(pmap, ray -> rop, ray -> ron, photon); |
628 |
> |
found = OOC_Find1Photon(pmap, ray -> rop, ray -> ron, photon); |
629 |
|
#else |
630 |
< |
kdT_Find1Photon(pmap, ray -> rop, ray -> ron, photon); |
631 |
< |
#endif |
630 |
> |
found = kdT_Find1Photon(pmap, ray -> rop, ray -> ron, photon); |
631 |
> |
#endif |
632 |
> |
if (found < 0) { |
633 |
> |
/* Expand search radius to retry */ |
634 |
> |
maxDist2_0 *= 2; |
635 |
> |
#ifdef PMAP_LOOKUP_WARN |
636 |
> |
sprintf(errmsg, "failed 1-NN photon lookup" |
637 |
> |
#ifdef PMAP_LOOKUP_REDO |
638 |
> |
", retrying with search radius %.2f", maxDist2_0 |
639 |
> |
#endif |
640 |
> |
); |
641 |
> |
error(WARNING, errmsg); |
642 |
> |
#endif |
643 |
> |
} |
644 |
> |
} while (REDO && found < 0); |
645 |
> |
|
646 |
> |
/* Return photon buffer containing valid photon, else NULL */ |
647 |
> |
return found < 0 ? NULL : photon; |
648 |
|
} |
649 |
|
|
650 |
|
|