1 |
+ |
#ifndef lint |
2 |
+ |
static const char RCSid[] = "$Id$"; |
3 |
+ |
#endif |
4 |
+ |
|
5 |
|
/* |
6 |
< |
================================================================== |
7 |
< |
Photon map data structures and kd-tree handling |
6 |
> |
========================================================================= |
7 |
> |
Photon map types and interface to nearest neighbour lookups in underlying |
8 |
> |
point cloud data structure. |
9 |
> |
|
10 |
> |
The default data structure is an in-core kd-tree (see pmapkdt.{h,c}). |
11 |
> |
This can be overriden with the PMAP_OOC compiletime switch, which enables |
12 |
> |
an out-of-core octree (see oococt.{h,c}). |
13 |
|
|
14 |
|
Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) |
15 |
|
(c) Fraunhofer Institute for Solar Energy Systems, |
16 |
|
(c) Lucerne University of Applied Sciences and Arts, |
17 |
< |
supported by the Swiss National Science Foundation (SNSF, #147053) |
18 |
< |
================================================================== |
17 |
> |
supported by the Swiss National Science Foundation (SNSF, #147053) |
18 |
> |
========================================================================== |
19 |
|
|
20 |
|
$Id$ |
21 |
|
*/ |
22 |
|
|
23 |
|
|
24 |
|
|
25 |
< |
#include "pmap.h" |
25 |
> |
#include "pmapdata.h" |
26 |
|
#include "pmaprand.h" |
27 |
|
#include "pmapmat.h" |
28 |
|
#include "otypes.h" |
29 |
|
#include "source.h" |
30 |
|
#include "rcontrib.h" |
31 |
+ |
#include "random.h" |
32 |
|
|
33 |
|
|
34 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
+ |
/* Include routines to handle underlying point cloud data structure */ |
42 |
+ |
#ifdef PMAP_OOC |
43 |
+ |
#include "pmapooc.c" |
44 |
+ |
#else |
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 |
|
{ |
62 |
|
if (!pmap) |
63 |
|
return; |
64 |
|
|
65 |
< |
pmap -> heapSize = pmap -> heapEnd = 0; |
38 |
< |
pmap -> heap = NULL; |
39 |
< |
pmap -> squeue = NULL; |
65 |
> |
pmap -> numPhotons = 0; |
66 |
|
pmap -> biasCompHist = NULL; |
67 |
|
pmap -> maxPos [0] = pmap -> maxPos [1] = pmap -> maxPos [2] = -FHUGE; |
68 |
|
pmap -> minPos [0] = pmap -> minPos [1] = pmap -> minPos [2] = FHUGE; |
72 |
|
pmap -> numDensity = 0; |
73 |
|
pmap -> distribRatio = 1; |
74 |
|
pmap -> type = t; |
75 |
+ |
pmap -> squeue.node = NULL; |
76 |
+ |
pmap -> squeue.len = 0; |
77 |
|
|
78 |
|
/* Init local RNG state */ |
79 |
|
pmap -> randState [0] = 10243; |
80 |
|
pmap -> randState [1] = 39829; |
81 |
|
pmap -> randState [2] = 9433; |
54 |
– |
/* pmapSeed(25999, pmap -> randState); */ |
82 |
|
pmapSeed(randSeed, pmap -> randState); |
83 |
|
|
84 |
|
/* Set up type-specific photon lookup callback */ |
85 |
|
pmap -> lookup = pmapLookup [t]; |
86 |
|
|
87 |
< |
pmap -> primary = NULL; |
88 |
< |
pmap -> primarySize = pmap -> primaryEnd = 0; |
87 |
> |
/* Mark primary photon ray as unused */ |
88 |
> |
pmap -> lastPrimary.srcIdx = -1; |
89 |
> |
pmap -> numPrimary = 0; |
90 |
> |
pmap -> primaries = NULL; |
91 |
> |
|
92 |
> |
/* Init storage */ |
93 |
> |
pmap -> heap = NULL; |
94 |
> |
pmap -> heapBuf = NULL; |
95 |
> |
pmap -> heapBufLen = 0; |
96 |
> |
#ifdef PMAP_OOC |
97 |
> |
OOC_Null(&pmap -> store); |
98 |
> |
#else |
99 |
> |
kdT_Null(&pmap -> store); |
100 |
> |
#endif |
101 |
|
} |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
< |
const PhotonPrimary* addPhotonPrimary (PhotonMap *pmap, const RAY *ray) |
105 |
> |
void initPhotonHeap (PhotonMap *pmap) |
106 |
|
{ |
107 |
< |
PhotonPrimary *prim = NULL; |
107 |
> |
int fdFlags; |
108 |
|
|
109 |
< |
if (!pmap || !ray) |
110 |
< |
return NULL; |
109 |
> |
if (!pmap) |
110 |
> |
error(INTERNAL, "undefined photon map in initPhotonHeap"); |
111 |
|
|
112 |
< |
/* Check if last primary ray has spawned photons (srcIdx >= 0, see |
113 |
< |
* addPhoton()), in which case we keep it and allocate a new one; |
114 |
< |
* otherwise we overwrite the unused entry */ |
115 |
< |
if (pmap -> primary && pmap -> primary [pmap -> primaryEnd].srcIdx >= 0) |
116 |
< |
pmap -> primaryEnd++; |
117 |
< |
|
118 |
< |
if (!pmap -> primarySize || pmap -> primaryEnd >= pmap -> primarySize) { |
119 |
< |
/* Allocate/enlarge array */ |
120 |
< |
pmap -> primarySize += pmap -> heapSizeInc; |
121 |
< |
|
83 |
< |
/* Counter wraparound? */ |
84 |
< |
if (pmap -> primarySize < pmap -> heapSizeInc) |
85 |
< |
error(INTERNAL, "photon primary overflow"); |
86 |
< |
|
87 |
< |
pmap -> primary = (PhotonPrimary *)realloc(pmap -> primary, |
88 |
< |
sizeof(PhotonPrimary) * |
89 |
< |
pmap -> primarySize); |
90 |
< |
if (!pmap -> primary) |
91 |
< |
error(USER, "can't allocate photon primaries"); |
112 |
> |
if (!pmap -> heap) { |
113 |
> |
/* Open heap file */ |
114 |
> |
mktemp(strcpy(pmap -> heapFname, PMAP_TMPFNAME)); |
115 |
> |
if (!(pmap -> heap = fopen(pmap -> heapFname, "w+b"))) |
116 |
> |
error(SYSTEM, "failed opening heap file in initPhotonHeap"); |
117 |
> |
|
118 |
> |
#ifdef F_SETFL /* XXX is there an alternate needed for Windows? */ |
119 |
> |
fdFlags = fcntl(fileno(pmap -> heap), F_GETFL); |
120 |
> |
fcntl(fileno(pmap -> heap), F_SETFL, fdFlags | O_APPEND); |
121 |
> |
#endif/* ftruncate(fileno(pmap -> heap), 0); */ |
122 |
|
} |
123 |
+ |
} |
124 |
+ |
|
125 |
+ |
|
126 |
+ |
|
127 |
+ |
void flushPhotonHeap (PhotonMap *pmap) |
128 |
+ |
{ |
129 |
+ |
int fd; |
130 |
+ |
const unsigned long len = pmap -> heapBufLen * sizeof(Photon); |
131 |
|
|
132 |
< |
prim = pmap -> primary + pmap -> primaryEnd; |
132 |
> |
if (!pmap) |
133 |
> |
error(INTERNAL, "undefined photon map in flushPhotonHeap"); |
134 |
> |
|
135 |
> |
if (!pmap -> heap || !pmap -> heapBuf) { |
136 |
> |
/* Silently ignore undefined heap |
137 |
> |
error(INTERNAL, "undefined heap in flushPhotonHeap"); */ |
138 |
> |
return; |
139 |
> |
} |
140 |
> |
|
141 |
> |
/* Atomically seek and write block to heap */ |
142 |
> |
/* !!! Unbuffered I/O via pwrite() avoids potential race conditions |
143 |
> |
* !!! and buffer corruption which can occur with lseek()/fseek() |
144 |
> |
* !!! followed by write()/fwrite(). */ |
145 |
> |
fd = fileno(pmap -> heap); |
146 |
|
|
147 |
< |
/* Mark unused with negative source index until path spawns a photon (see |
148 |
< |
* addPhoton()) */ |
149 |
< |
prim -> srcIdx = -1; |
150 |
< |
|
151 |
< |
/* Reverse incident direction to point to light source */ |
101 |
< |
prim -> dir [0] = -ray -> rdir [0]; |
102 |
< |
prim -> dir [1] = -ray -> rdir [1]; |
103 |
< |
prim -> dir [2] = -ray -> rdir [2]; |
147 |
> |
#ifdef DEBUG_PMAP |
148 |
> |
sprintf(errmsg, "Proc %d: flushing %ld photons from pos %ld\n", getpid(), |
149 |
> |
pmap -> heapBufLen, lseek(fd, 0, SEEK_END) / sizeof(Photon)); |
150 |
> |
eputs(errmsg); |
151 |
> |
#endif |
152 |
|
|
153 |
< |
VCOPY(prim -> pos, ray -> rop); |
153 |
> |
/*if (pwrite(fd, pmap -> heapBuf, len, lseek(fd, 0, SEEK_END)) != len) */ |
154 |
> |
if (write(fd, pmap -> heapBuf, len) != len) |
155 |
> |
error(SYSTEM, "failed append to heap file in flushPhotonHeap"); |
156 |
> |
|
157 |
> |
#if NIX |
158 |
> |
if (fsync(fd)) |
159 |
> |
error(SYSTEM, "failed fsync in flushPhotonHeap"); |
160 |
> |
#endif |
161 |
> |
|
162 |
> |
pmap -> heapBufLen = 0; |
163 |
> |
} |
164 |
> |
|
165 |
> |
|
166 |
> |
|
167 |
> |
#ifdef DEBUG_PMAP |
168 |
> |
static int checkPhotonHeap (FILE *file) |
169 |
> |
/* Check heap for nonsensical or duplicate photons */ |
170 |
> |
{ |
171 |
> |
Photon p, lastp; |
172 |
> |
int i, dup; |
173 |
|
|
174 |
< |
return prim; |
174 |
> |
rewind(file); |
175 |
> |
memset(&lastp, 0, sizeof(lastp)); |
176 |
> |
|
177 |
> |
while (fread(&p, sizeof(p), 1, file)) { |
178 |
> |
dup = 1; |
179 |
> |
|
180 |
> |
for (i = 0; i <= 2; i++) { |
181 |
> |
if (p.pos [i] < thescene.cuorg [i] || |
182 |
> |
p.pos [i] > thescene.cuorg [i] + thescene.cusize) { |
183 |
> |
|
184 |
> |
sprintf(errmsg, "corrupt photon in heap at [%f, %f, %f]\n", |
185 |
> |
p.pos [0], p.pos [1], p.pos [2]); |
186 |
> |
error(WARNING, errmsg); |
187 |
> |
} |
188 |
> |
|
189 |
> |
dup &= p.pos [i] == lastp.pos [i]; |
190 |
> |
} |
191 |
> |
|
192 |
> |
if (dup) { |
193 |
> |
sprintf(errmsg, |
194 |
> |
"consecutive duplicate photon in heap at [%f, %f, %f]\n", |
195 |
> |
p.pos [0], p.pos [1], p.pos [2]); |
196 |
> |
error(WARNING, errmsg); |
197 |
> |
} |
198 |
> |
} |
199 |
> |
|
200 |
> |
return 0; |
201 |
|
} |
202 |
+ |
#endif |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
< |
const Photon* addPhoton (PhotonMap* pmap, const RAY* ray) |
206 |
> |
int newPhoton (PhotonMap* pmap, const RAY* ray) |
207 |
|
{ |
208 |
|
unsigned i; |
209 |
< |
Photon* photon = NULL; |
209 |
> |
Photon photon; |
210 |
|
COLOR photonFlux; |
211 |
|
|
212 |
|
/* Account for distribution ratio */ |
213 |
|
if (!pmap || pmapRandom(pmap -> randState) > pmap -> distribRatio) |
214 |
< |
return NULL; |
214 |
> |
return -1; |
215 |
|
|
216 |
|
/* Don't store on sources */ |
217 |
|
if (ray -> robj > -1 && islight(objptr(ray -> ro -> omod) -> otype)) |
218 |
< |
return NULL; |
218 |
> |
return -1; |
219 |
|
|
220 |
< |
#if 0 |
221 |
< |
if (inContribPmap(pmap)) { |
222 |
< |
/* Adding contribution photon */ |
223 |
< |
if (ray -> parent && ray -> parent -> rtype & PRIMARY) |
224 |
< |
/* Add primary photon ray if parent is primary; by putting this |
225 |
< |
* here and checking the ray's immediate parent, we only add |
226 |
< |
* primaries that actually contribute photons, and we only add them |
133 |
< |
* once. */ |
134 |
< |
addPhotonPrimary(pmap, ray -> parent); |
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 |
< |
/* Save index to primary ray (remains unchanged if primary already in |
229 |
< |
* array) */ |
230 |
< |
primary = pmap -> primaryEnd; |
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] && |
232 |
> |
ray -> rop [1] >= pmapROI [i].min [1] && |
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 |
|
} |
140 |
– |
#endif |
239 |
|
|
240 |
< |
#ifdef PMAP_ROI |
241 |
< |
/* Store photon if within region of interest -- for egg-spurtz only! */ |
242 |
< |
if (ray -> rop [0] >= pmapROI [0] && ray -> rop [0] <= pmapROI [1] && |
243 |
< |
ray -> rop [1] >= pmapROI [2] && ray -> rop [1] <= pmapROI [3] && |
244 |
< |
ray -> rop [2] >= pmapROI [4] && ray -> rop [2] <= pmapROI [5]) |
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 |
< |
{ |
149 |
< |
if (pmap -> heapEnd >= pmap -> heapSize) { |
150 |
< |
/* Enlarge heap */ |
151 |
< |
pmap -> heapSize += pmap -> heapSizeInc; |
152 |
< |
|
153 |
< |
/* Counter wraparound? */ |
154 |
< |
if (pmap -> heapSize < pmap -> heapSizeInc) |
155 |
< |
error(INTERNAL, "photon heap overflow"); |
156 |
< |
|
157 |
< |
pmap -> heap = (Photon *)realloc(pmap -> heap, |
158 |
< |
sizeof(Photon) * pmap -> heapSize); |
159 |
< |
if (!pmap -> heap) |
160 |
< |
error(USER, "can't allocate photon heap"); |
161 |
< |
} |
253 |
> |
setPhotonFlux(&photon, photonFlux); |
254 |
|
|
255 |
< |
photon = pmap -> heap + pmap -> heapEnd++; |
255 |
> |
/* Set photon position and flags */ |
256 |
> |
VCOPY(photon.pos, ray -> rop); |
257 |
> |
photon.flags = 0; |
258 |
> |
photon.caustic = PMAP_CAUSTICRAY(ray); |
259 |
|
|
260 |
< |
/* Adjust flux according to distribution ratio and ray weight */ |
261 |
< |
copycolor(photonFlux, ray -> rcol); |
262 |
< |
scalecolor(photonFlux, |
263 |
< |
ray -> rweight / (pmap -> distribRatio ? pmap -> distribRatio |
264 |
< |
: 1)); |
265 |
< |
setPhotonFlux(photon, photonFlux); |
266 |
< |
|
267 |
< |
/* Set photon position and flags */ |
268 |
< |
VCOPY(photon -> pos, ray -> rop); |
269 |
< |
photon -> flags = PMAP_CAUSTICRAY(ray) ? PMAP_CAUST_BIT : 0; |
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 |
< |
/* Set primary ray index and mark as used for contrib photons */ |
277 |
< |
if (isContribPmap(pmap)) { |
278 |
< |
photon -> primary = pmap -> primaryEnd; |
279 |
< |
pmap -> primary [pmap -> primaryEnd].srcIdx = ray -> rsrc; |
280 |
< |
} |
281 |
< |
else photon -> primary = 0; |
282 |
< |
|
283 |
< |
/* Update min and max positions & set normal */ |
284 |
< |
for (i = 0; i <= 2; i++) { |
285 |
< |
if (photon -> pos [i] < pmap -> minPos [i]) |
286 |
< |
pmap -> minPos [i] = photon -> pos [i]; |
287 |
< |
if (photon -> pos [i] > pmap -> maxPos [i]) |
288 |
< |
pmap -> maxPos [i] = photon -> pos [i]; |
289 |
< |
photon -> norm [i] = 127.0 * (isVolumePmap(pmap) ? ray -> rdir [i] |
290 |
< |
: ray -> ron [i]); |
191 |
< |
} |
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()); |
283 |
> |
#else |
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 |
|
} |
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); |
299 |
+ |
|
300 |
+ |
pmap -> numPhotons++; |
301 |
+ |
|
302 |
+ |
/* Print photon attributes */ |
303 |
+ |
if (printPhoton) |
304 |
+ |
/* Non-const kludge */ |
305 |
+ |
printPhoton((RAY*)ray, &photon, pmap); |
306 |
|
|
307 |
< |
return photon; |
307 |
> |
return 0; |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
< |
static void nearestNeighbours (PhotonMap* pmap, const float pos [3], |
313 |
< |
const float norm [3], unsigned long node) |
201 |
< |
/* Recursive part of findPhotons(..). |
202 |
< |
Note that all heap and priority queue index handling is 1-based, but |
203 |
< |
accesses to the arrays are 0-based! */ |
312 |
> |
void buildPhotonMap (PhotonMap *pmap, double *photonFlux, |
313 |
> |
PhotonPrimaryIdx *primaryOfs, unsigned nproc) |
314 |
|
{ |
315 |
< |
Photon* p = &pmap -> heap [node - 1]; |
316 |
< |
unsigned i, j; |
317 |
< |
/* Signed distance to current photon's splitting plane */ |
318 |
< |
float d = pos [photonDiscr(*p)] - p -> pos [photonDiscr(*p)], |
319 |
< |
d2 = d * d; |
320 |
< |
PhotonSQNode* sq = pmap -> squeue; |
321 |
< |
const unsigned sqSize = pmap -> squeueSize; |
322 |
< |
float dv [3]; |
315 |
> |
unsigned long n, nCheck = 0; |
316 |
> |
unsigned i; |
317 |
> |
Photon *p; |
318 |
> |
COLOR flux; |
319 |
> |
char nuHeapFname [sizeof(PMAP_TMPFNAME)]; |
320 |
> |
FILE *nuHeap; |
321 |
> |
/* Need double here to reduce summation errors */ |
322 |
> |
double avgFlux [3] = {0, 0, 0}, CoG [3] = {0, 0, 0}, CoGdist = 0; |
323 |
> |
FVECT d; |
324 |
|
|
325 |
< |
/* Search subtree closer to pos first; exclude other subtree if the |
326 |
< |
distance to the splitting plane is greater than maxDist */ |
216 |
< |
if (d < 0) { |
217 |
< |
if (node << 1 <= pmap -> heapSize) |
218 |
< |
nearestNeighbours(pmap, pos, norm, node << 1); |
219 |
< |
if (d2 < pmap -> maxDist && node << 1 < pmap -> heapSize) |
220 |
< |
nearestNeighbours(pmap, pos, norm, (node << 1) + 1); |
221 |
< |
} |
222 |
< |
else { |
223 |
< |
if (node << 1 < pmap -> heapSize) |
224 |
< |
nearestNeighbours(pmap, pos, norm, (node << 1) + 1); |
225 |
< |
if (d2 < pmap -> maxDist && node << 1 <= pmap -> heapSize) |
226 |
< |
nearestNeighbours(pmap, pos, norm, node << 1); |
227 |
< |
} |
228 |
< |
|
229 |
< |
/* Reject photon if normal faces away (ignored for volume photons) */ |
230 |
< |
if (norm && DOT(norm, p -> norm) <= 0) |
231 |
< |
return; |
325 |
> |
if (!pmap) |
326 |
> |
error(INTERNAL, "undefined photon map in buildPhotonMap"); |
327 |
|
|
328 |
< |
if (isContribPmap(pmap) && pmap -> srcContrib) { |
329 |
< |
/* Lookup in contribution photon map */ |
330 |
< |
OBJREC *srcMod; |
331 |
< |
const int srcIdx = photonSrcIdx(pmap, p); |
332 |
< |
|
333 |
< |
if (srcIdx < 0 || srcIdx >= nsources) |
334 |
< |
error(INTERNAL, "invalid light source index in photon map"); |
240 |
< |
|
241 |
< |
srcMod = objptr(source [srcIdx].so -> omod); |
328 |
> |
/* Get number of photons from heapfile size */ |
329 |
> |
if (fseek(pmap -> heap, 0, SEEK_END) < 0) |
330 |
> |
error(SYSTEM, "failed seek to end of photon heap in buildPhotonMap"); |
331 |
> |
pmap -> numPhotons = ftell(pmap -> heap) / sizeof(Photon); |
332 |
> |
|
333 |
> |
if (!pmap -> numPhotons) |
334 |
> |
error(INTERNAL, "empty photon map in buildPhotonMap"); |
335 |
|
|
336 |
< |
/* Reject photon if contributions from light source which emitted it |
337 |
< |
* are not sought */ |
245 |
< |
if (!lu_find(pmap -> srcContrib, srcMod -> oname) -> data) |
246 |
< |
return; |
336 |
> |
if (!pmap -> heap) |
337 |
> |
error(INTERNAL, "no heap in buildPhotonMap"); |
338 |
|
|
339 |
< |
/* Reject non-caustic photon if lookup for caustic contribs */ |
340 |
< |
if (pmap -> lookupFlags & PMAP_CAUST_BIT & ~p -> flags) |
341 |
< |
return; |
339 |
> |
#ifdef DEBUG_PMAP |
340 |
> |
eputs("Checking photon heap consistency...\n"); |
341 |
> |
checkPhotonHeap(pmap -> heap); |
342 |
> |
|
343 |
> |
sprintf(errmsg, "Heap contains %ld photons\n", pmap -> numPhotons); |
344 |
> |
eputs(errmsg); |
345 |
> |
#endif |
346 |
> |
|
347 |
> |
/* Allocate heap buffa */ |
348 |
> |
if (!pmap -> heapBuf) { |
349 |
> |
pmap -> heapBufSize = PMAP_HEAPBUFSIZE; |
350 |
> |
pmap -> heapBuf = calloc(pmap -> heapBufSize, sizeof(Photon)); |
351 |
> |
if (!pmap -> heapBuf) |
352 |
> |
error(SYSTEM, "failed to allocate postprocessed photon heap in" |
353 |
> |
"buildPhotonMap"); |
354 |
|
} |
355 |
+ |
|
356 |
+ |
/* We REALLY don't need yet another @%&*! heap just to hold the scaled |
357 |
+ |
* photons, but can't think of a quicker fix... */ |
358 |
+ |
mktemp(strcpy(nuHeapFname, PMAP_TMPFNAME)); |
359 |
+ |
if (!(nuHeap = fopen(nuHeapFname, "w+b"))) |
360 |
+ |
error(SYSTEM, "failed to open postprocessed photon heap in " |
361 |
+ |
"buildPhotonMap"); |
362 |
+ |
|
363 |
+ |
rewind(pmap -> heap); |
364 |
+ |
|
365 |
+ |
#ifdef DEBUG_PMAP |
366 |
+ |
eputs("Postprocessing photons...\n"); |
367 |
+ |
#endif |
368 |
|
|
369 |
< |
/* Squared distance to current photon */ |
370 |
< |
dv [0] = pos [0] - p -> pos [0]; |
371 |
< |
dv [1] = pos [1] - p -> pos [1]; |
372 |
< |
dv [2] = pos [2] - p -> pos [2]; |
373 |
< |
d2 = DOT(dv, dv); |
369 |
> |
while (!feof(pmap -> heap)) { |
370 |
> |
#ifdef DEBUG_PMAP |
371 |
> |
printf("Reading %lu at %lu... ", pmap -> heapBufSize, ftell(pmap->heap)); |
372 |
> |
#endif |
373 |
> |
pmap -> heapBufLen = fread(pmap -> heapBuf, sizeof(Photon), |
374 |
> |
pmap -> heapBufSize, pmap -> heap); |
375 |
> |
#ifdef DEBUG_PMAP |
376 |
> |
printf("Got %lu\n", pmap -> heapBufLen); |
377 |
> |
#endif |
378 |
|
|
379 |
< |
/* Accept photon if closer than current max dist & add to priority queue */ |
380 |
< |
if (d2 < pmap -> maxDist) { |
381 |
< |
if (pmap -> squeueEnd < sqSize) { |
382 |
< |
/* Priority queue not full; append photon and restore heap */ |
383 |
< |
i = ++pmap -> squeueEnd; |
379 |
> |
if (ferror(pmap -> heap)) |
380 |
> |
error(SYSTEM, "failed to read photon heap in buildPhotonMap"); |
381 |
> |
|
382 |
> |
for (n = pmap -> heapBufLen, p = pmap -> heapBuf; n; n--, p++) { |
383 |
> |
/* Update min and max pos and set photon flux */ |
384 |
> |
for (i = 0; i <= 2; i++) { |
385 |
> |
if (p -> pos [i] < pmap -> minPos [i]) |
386 |
> |
pmap -> minPos [i] = p -> pos [i]; |
387 |
> |
else if (p -> pos [i] > pmap -> maxPos [i]) |
388 |
> |
pmap -> maxPos [i] = p -> pos [i]; |
389 |
> |
|
390 |
> |
/* Update centre of gravity with photon position */ |
391 |
> |
CoG [i] += p -> pos [i]; |
392 |
> |
} |
393 |
|
|
394 |
< |
while (i > 1 && sq [(i >> 1) - 1].dist <= d2) { |
395 |
< |
sq [i - 1].photon = sq [(i >> 1) - 1].photon; |
396 |
< |
sq [i - 1].dist = sq [(i >> 1) - 1].dist; |
397 |
< |
i >>= 1; |
269 |
< |
} |
394 |
> |
if (primaryOfs) |
395 |
> |
/* Linearise photon primary index from subprocess index using the |
396 |
> |
* per-subprocess offsets in primaryOfs */ |
397 |
> |
p -> primary += primaryOfs [p -> proc]; |
398 |
|
|
399 |
< |
sq [--i].photon = p; |
400 |
< |
sq [i].dist = d2; |
401 |
< |
/* Update maxDist if we've just filled the queue */ |
402 |
< |
if (pmap -> squeueEnd >= pmap -> squeueSize) |
403 |
< |
pmap -> maxDist = sq [0].dist; |
404 |
< |
} |
405 |
< |
else { |
406 |
< |
/* Priority queue full; replace maximum, restore heap, and |
407 |
< |
update maxDist */ |
280 |
< |
i = 1; |
281 |
< |
|
282 |
< |
while (i <= sqSize >> 1) { |
283 |
< |
j = i << 1; |
284 |
< |
if (j < sqSize && sq [j - 1].dist < sq [j].dist) |
285 |
< |
j++; |
286 |
< |
if (d2 >= sq [j - 1].dist) |
287 |
< |
break; |
288 |
< |
sq [i - 1].photon = sq [j - 1].photon; |
289 |
< |
sq [i - 1].dist = sq [j - 1].dist; |
290 |
< |
i = j; |
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); |
403 |
> |
|
404 |
> |
if (photonFlux) { |
405 |
> |
scalecolor(flux, photonFlux [isContribPmap(pmap) ? |
406 |
> |
photonSrcIdx(pmap, p) : 0]); |
407 |
> |
setPhotonFlux(p, flux); |
408 |
|
} |
409 |
< |
|
410 |
< |
sq [--i].photon = p; |
411 |
< |
sq [i].dist = d2; |
295 |
< |
pmap -> maxDist = sq [0].dist; |
409 |
> |
|
410 |
> |
/* Update average photon flux; need a double here */ |
411 |
> |
addcolor(avgFlux, flux); |
412 |
|
} |
413 |
+ |
|
414 |
+ |
/* Write modified photons to new heap */ |
415 |
+ |
fwrite(pmap -> heapBuf, sizeof(Photon), pmap -> heapBufLen, nuHeap); |
416 |
+ |
|
417 |
+ |
if (ferror(nuHeap)) |
418 |
+ |
error(SYSTEM, "failed postprocessing photon flux in " |
419 |
+ |
"buildPhotonMap"); |
420 |
+ |
|
421 |
+ |
nCheck += pmap -> heapBufLen; |
422 |
|
} |
423 |
+ |
|
424 |
+ |
#ifdef DEBUG_PMAP |
425 |
+ |
if (nCheck < pmap -> numPhotons) |
426 |
+ |
error(INTERNAL, "truncated photon heap in buildPhotonMap"); |
427 |
+ |
#endif |
428 |
+ |
|
429 |
+ |
/* Finalise average photon flux */ |
430 |
+ |
scalecolor(avgFlux, 1.0 / pmap -> numPhotons); |
431 |
+ |
copycolor(pmap -> photonFlux, avgFlux); |
432 |
+ |
|
433 |
+ |
/* Average photon positions to get centre of gravity */ |
434 |
+ |
for (i = 0; i < 3; i++) |
435 |
+ |
pmap -> CoG [i] = CoG [i] /= pmap -> numPhotons; |
436 |
+ |
|
437 |
+ |
rewind(pmap -> heap); |
438 |
+ |
|
439 |
+ |
/* Compute average photon distance to centre of gravity */ |
440 |
+ |
while (!feof(pmap -> heap)) { |
441 |
+ |
pmap -> heapBufLen = fread(pmap -> heapBuf, sizeof(Photon), |
442 |
+ |
pmap -> heapBufSize, pmap -> heap); |
443 |
+ |
|
444 |
+ |
for (n = pmap -> heapBufLen, p = pmap -> heapBuf; n; n--, p++) { |
445 |
+ |
VSUB(d, p -> pos, CoG); |
446 |
+ |
CoGdist += DOT(d, d); |
447 |
+ |
} |
448 |
+ |
} |
449 |
+ |
|
450 |
+ |
pmap -> CoGdist = CoGdist /= pmap -> numPhotons; |
451 |
+ |
|
452 |
+ |
/* Swap heaps, discarding unscaled photons */ |
453 |
+ |
fclose(pmap -> heap); |
454 |
+ |
unlink(pmap -> heapFname); |
455 |
+ |
pmap -> heap = nuHeap; |
456 |
+ |
strcpy(pmap -> heapFname, nuHeapFname); |
457 |
+ |
|
458 |
+ |
#ifdef PMAP_OOC |
459 |
+ |
OOC_BuildPhotonMap(pmap, nproc); |
460 |
+ |
#else |
461 |
+ |
kdT_BuildPhotonMap(pmap); |
462 |
+ |
#endif |
463 |
+ |
|
464 |
+ |
/* Trash heap and its buffa */ |
465 |
+ |
free(pmap -> heapBuf); |
466 |
+ |
fclose(pmap -> heap); |
467 |
+ |
unlink(pmap -> heapFname); |
468 |
+ |
pmap -> heap = NULL; |
469 |
+ |
pmap -> heapBuf = NULL; |
470 |
|
} |
471 |
|
|
472 |
|
|
481 |
|
/* Threshold below which we assume increasing max radius won't help */ |
482 |
|
#define PMAP_SHORT_LOOKUP_THRESH 1 |
483 |
|
|
484 |
+ |
/* Coefficient for adaptive maximum search radius */ |
485 |
+ |
#define PMAP_MAXDIST_COEFF 100 |
486 |
+ |
|
487 |
|
void findPhotons (PhotonMap* pmap, const RAY* ray) |
488 |
|
{ |
314 |
– |
float pos [3], norm [3]; |
489 |
|
int redo = 0; |
490 |
|
|
491 |
< |
if (!pmap -> squeue) { |
491 |
> |
if (!pmap -> squeue.len) { |
492 |
|
/* Lazy init priority queue */ |
493 |
< |
pmap -> squeueSize = pmap -> maxGather + 1; |
494 |
< |
pmap -> squeue = (PhotonSQNode*)malloc(pmap -> squeueSize * |
495 |
< |
sizeof(PhotonSQNode)); |
496 |
< |
if (!pmap -> squeue) |
497 |
< |
error(USER, "can't allocate photon priority queue"); |
324 |
< |
|
493 |
> |
#ifdef PMAP_OOC |
494 |
> |
OOC_InitFindPhotons(pmap); |
495 |
> |
#else |
496 |
> |
kdT_InitFindPhotons(pmap); |
497 |
> |
#endif |
498 |
|
pmap -> minGathered = pmap -> maxGather; |
499 |
|
pmap -> maxGathered = pmap -> minGather; |
500 |
|
pmap -> totalGathered = 0; |
503 |
|
pmap -> minError = FHUGE; |
504 |
|
pmap -> maxError = -FHUGE; |
505 |
|
pmap -> rmsError = 0; |
506 |
< |
#ifdef PMAP_MAXDIST_ABS |
507 |
< |
/* Treat maxDistCoeff as an *absolute* and *fixed* max search radius. |
508 |
< |
Primarily intended for debugging; FOR ZE ECKSPURTZ ONLY! */ |
509 |
< |
pmap -> maxDist0 = pmap -> maxDistLimit = maxDistCoeff; |
510 |
< |
#else |
511 |
< |
/* Maximum search radius limit based on avg photon distance to |
339 |
< |
* centre of gravity */ |
340 |
< |
pmap -> maxDist0 = pmap -> maxDistLimit = |
341 |
< |
maxDistCoeff * pmap -> squeueSize * pmap -> CoGdist / |
342 |
< |
pmap -> heapSize; |
343 |
< |
#endif |
506 |
> |
/* SQUARED max search radius limit is based on avg photon distance to |
507 |
> |
* centre of gravity, unless fixed by user (maxDistFix > 0) */ |
508 |
> |
pmap -> maxDist0 = pmap -> maxDist2Limit = |
509 |
> |
maxDistFix > 0 ? maxDistFix * maxDistFix |
510 |
> |
: PMAP_MAXDIST_COEFF * pmap -> squeue.len * |
511 |
> |
pmap -> CoGdist / pmap -> numPhotons; |
512 |
|
} |
513 |
|
|
514 |
|
do { |
515 |
< |
pmap -> squeueEnd = 0; |
516 |
< |
pmap -> maxDist = pmap -> maxDist0; |
515 |
> |
pmap -> squeue.tail = 0; |
516 |
> |
pmap -> maxDist2 = pmap -> maxDist0; |
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). */ |
521 |
|
if (isVolumePmap(pmap)) { |
522 |
< |
VCOPY(pos, ray -> rorg); |
523 |
< |
nearestNeighbours(pmap, pos, NULL, 1); |
522 |
> |
#ifdef PMAP_OOC |
523 |
> |
OOC_FindPhotons(pmap, ray -> rorg, NULL); |
524 |
> |
#else |
525 |
> |
kdT_FindPhotons(pmap, ray -> rorg, NULL); |
526 |
> |
#endif |
527 |
|
} |
528 |
|
else { |
529 |
< |
VCOPY(pos, ray -> rop); |
530 |
< |
VCOPY(norm, ray -> ron); |
531 |
< |
nearestNeighbours(pmap, pos, norm, 1); |
529 |
> |
#ifdef PMAP_OOC |
530 |
> |
OOC_FindPhotons(pmap, ray -> rop, ray -> ron); |
531 |
> |
#else |
532 |
> |
kdT_FindPhotons(pmap, ray -> rop, ray -> ron); |
533 |
> |
#endif |
534 |
|
} |
535 |
|
|
536 |
< |
#ifndef PMAP_MAXDIST_ABS |
537 |
< |
if (pmap -> squeueEnd < pmap -> squeueSize * pmap -> gatherTolerance) { |
536 |
> |
#ifdef PMAP_LOOKUP_INFO |
537 |
> |
fprintf(stderr, "%d/%d %s photons found within radius %.3f " |
538 |
> |
"at (%.2f,%.2f,%.2f) on %s\n", pmap -> squeue.tail, |
539 |
> |
pmap -> squeue.len, pmapName [pmap -> type], sqrt(pmap -> maxDist2), |
540 |
> |
ray -> rop [0], ray -> rop [1], ray -> rop [2], |
541 |
> |
ray -> ro ? ray -> ro -> oname : "<null>"); |
542 |
> |
#endif |
543 |
> |
|
544 |
> |
if (pmap -> squeue.tail < pmap -> squeue.len * pmap -> gatherTolerance) { |
545 |
|
/* Short lookup; too few photons found */ |
546 |
< |
if (pmap -> squeueEnd > PMAP_SHORT_LOOKUP_THRESH) { |
546 |
> |
if (pmap -> squeue.tail > PMAP_SHORT_LOOKUP_THRESH) { |
547 |
|
/* Ignore short lookups which return fewer than |
548 |
|
* PMAP_SHORT_LOOKUP_THRESH photons under the assumption there |
549 |
|
* really are no photons in the vicinity, and increasing the max |
550 |
|
* search radius therefore won't help */ |
551 |
< |
#ifdef PMAP_LOOKUP_WARN |
551 |
> |
#ifdef PMAP_LOOKUP_WARN |
552 |
|
sprintf(errmsg, |
553 |
|
"%d/%d %s photons found at (%.2f,%.2f,%.2f) on %s", |
554 |
< |
pmap -> squeueEnd, pmap -> squeueSize, |
555 |
< |
pmapName [pmap -> type], pos [0], pos [1], pos [2], |
554 |
> |
pmap -> squeue.tail, pmap -> squeue.len, |
555 |
> |
pmapName [pmap -> type], |
556 |
> |
ray -> rop [0], ray -> rop [1], ray -> rop [2], |
557 |
|
ray -> ro ? ray -> ro -> oname : "<null>"); |
558 |
|
error(WARNING, errmsg); |
559 |
< |
#endif |
559 |
> |
#endif |
560 |
|
|
561 |
< |
if (pmap -> maxDist0 < pmap -> maxDistLimit) { |
561 |
> |
/* Bail out after warning if maxDist is fixed */ |
562 |
> |
if (maxDistFix > 0) |
563 |
> |
return; |
564 |
> |
|
565 |
> |
if (pmap -> maxDist0 < pmap -> maxDist2Limit) { |
566 |
|
/* Increase max search radius if below limit & redo search */ |
567 |
|
pmap -> maxDist0 *= PMAP_MAXDIST_INC; |
568 |
< |
#ifdef PMAP_LOOKUP_REDO |
568 |
> |
#ifdef PMAP_LOOKUP_REDO |
569 |
|
redo = 1; |
570 |
< |
#endif |
571 |
< |
|
387 |
< |
#ifdef PMAP_LOOKUP_WARN |
570 |
> |
#endif |
571 |
> |
#ifdef PMAP_LOOKUP_WARN |
572 |
|
sprintf(errmsg, |
573 |
|
redo ? "restarting photon lookup with max radius %.1e" |
574 |
|
: "max photon lookup radius adjusted to %.1e", |
575 |
|
pmap -> maxDist0); |
576 |
|
error(WARNING, errmsg); |
577 |
< |
#endif |
577 |
> |
#endif |
578 |
|
} |
579 |
< |
#ifdef PMAP_LOOKUP_REDO |
579 |
> |
#ifdef PMAP_LOOKUP_REDO |
580 |
|
else { |
581 |
|
sprintf(errmsg, "max photon lookup radius clamped to %.1e", |
582 |
|
pmap -> maxDist0); |
583 |
|
error(WARNING, errmsg); |
584 |
|
} |
585 |
< |
#endif |
585 |
> |
#endif |
586 |
|
} |
587 |
|
|
588 |
|
/* Reset successful lookup counter */ |
589 |
|
pmap -> numLookups = 0; |
590 |
|
} |
591 |
|
else { |
592 |
+ |
/* Bail out after warning if maxDist is fixed */ |
593 |
+ |
if (maxDistFix > 0) |
594 |
+ |
return; |
595 |
+ |
|
596 |
|
/* Increment successful lookup counter and reduce max search radius if |
597 |
|
* wraparound */ |
598 |
|
pmap -> numLookups = (pmap -> numLookups + 1) % PMAP_MAXDIST_CNT; |
601 |
|
|
602 |
|
redo = 0; |
603 |
|
} |
604 |
< |
#endif |
604 |
> |
|
605 |
|
} while (redo); |
606 |
|
} |
607 |
|
|
608 |
|
|
609 |
|
|
610 |
< |
static void nearest1Neighbour (PhotonMap *pmap, const float pos [3], |
423 |
< |
const float norm [3], Photon **photon, |
424 |
< |
unsigned long node) |
425 |
< |
/* Recursive part of find1Photon(..). |
426 |
< |
Note that all heap index handling is 1-based, but accesses to the |
427 |
< |
arrays are 0-based! */ |
610 |
> |
void find1Photon (PhotonMap *pmap, const RAY* ray, Photon *photon) |
611 |
|
{ |
612 |
< |
Photon *p = pmap -> heap + node - 1; |
430 |
< |
/* Signed distance to current photon's splitting plane */ |
431 |
< |
float d = pos [photonDiscr(*p)] - p -> pos [photonDiscr(*p)], |
432 |
< |
d2 = d * d; |
433 |
< |
float dv [3]; |
612 |
> |
pmap -> maxDist2 = thescene.cusize; /* ? */ |
613 |
|
|
614 |
< |
/* Search subtree closer to pos first; exclude other subtree if the |
615 |
< |
distance to the splitting plane is greater than maxDist */ |
616 |
< |
if (d < 0) { |
617 |
< |
if (node << 1 <= pmap -> heapSize) |
618 |
< |
nearest1Neighbour(pmap, pos, norm, photon, node << 1); |
440 |
< |
if (d2 < pmap -> maxDist && node << 1 < pmap -> heapSize) |
441 |
< |
nearest1Neighbour(pmap, pos, norm, photon, (node << 1) + 1); |
442 |
< |
} |
443 |
< |
else { |
444 |
< |
if (node << 1 < pmap -> heapSize) |
445 |
< |
nearest1Neighbour(pmap, pos, norm, photon, (node << 1) + 1); |
446 |
< |
if (d2 < pmap -> maxDist && node << 1 <= pmap -> heapSize) |
447 |
< |
nearest1Neighbour(pmap, pos, norm, photon, node << 1); |
448 |
< |
} |
449 |
< |
|
450 |
< |
/* Squared distance to current photon */ |
451 |
< |
dv [0] = pos [0] - p -> pos [0]; |
452 |
< |
dv [1] = pos [1] - p -> pos [1]; |
453 |
< |
dv [2] = pos [2] - p -> pos [2]; |
454 |
< |
d2 = DOT(dv, dv); |
455 |
< |
|
456 |
< |
if (d2 < pmap -> maxDist && DOT(norm, p -> norm) > 0) { |
457 |
< |
/* Closest photon so far with similar normal */ |
458 |
< |
pmap -> maxDist = d2; |
459 |
< |
*photon = p; |
460 |
< |
} |
614 |
> |
#ifdef PMAP_OOC |
615 |
> |
OOC_Find1Photon(pmap, ray -> rop, ray -> ron, photon); |
616 |
> |
#else |
617 |
> |
kdT_Find1Photon(pmap, ray -> rop, ray -> ron, photon); |
618 |
> |
#endif |
619 |
|
} |
620 |
|
|
621 |
|
|
622 |
|
|
623 |
< |
Photon* find1Photon (PhotonMap *pmap, const RAY* ray) |
623 |
> |
void getPhoton (PhotonMap *pmap, PhotonIdx idx, Photon *photon) |
624 |
|
{ |
625 |
< |
float fpos [3], norm [3]; |
626 |
< |
Photon* photon = NULL; |
627 |
< |
|
628 |
< |
VCOPY(fpos, ray -> rop); |
629 |
< |
VCOPY(norm, ray -> ron); |
630 |
< |
pmap -> maxDist = thescene.cusize; |
473 |
< |
nearest1Neighbour(pmap, fpos, norm, &photon, 1); |
474 |
< |
|
475 |
< |
return photon; |
625 |
> |
#ifdef PMAP_OOC |
626 |
> |
if (OOC_GetPhoton(pmap, idx, photon)) |
627 |
> |
#else |
628 |
> |
if (kdT_GetPhoton(pmap, idx, photon)) |
629 |
> |
#endif |
630 |
> |
error(INTERNAL, "failed photon lookup"); |
631 |
|
} |
632 |
|
|
633 |
|
|
634 |
|
|
635 |
< |
static unsigned long medianPartition (const Photon* heap, |
481 |
< |
unsigned long* heapIdx, |
482 |
< |
unsigned long* heapXdi, |
483 |
< |
unsigned long left, |
484 |
< |
unsigned long right, unsigned dim) |
485 |
< |
/* Returns index to median in heap from indices left to right |
486 |
< |
(inclusive) in dimension dim. The heap is partitioned relative to |
487 |
< |
median using a quicksort algorithm. The heap indices in heapIdx are |
488 |
< |
sorted rather than the heap itself. */ |
635 |
> |
Photon *getNearestPhoton (const PhotonSearchQueue *squeue, PhotonIdx idx) |
636 |
|
{ |
637 |
< |
register const float* p; |
638 |
< |
const unsigned long n = right - left + 1; |
639 |
< |
register unsigned long l, r, lg2, n2, m; |
640 |
< |
register unsigned d; |
641 |
< |
|
495 |
< |
/* Round down n to nearest power of 2 */ |
496 |
< |
for (lg2 = 0, n2 = n; n2 > 1; n2 >>= 1, ++lg2); |
497 |
< |
n2 = 1 << lg2; |
498 |
< |
|
499 |
< |
/* Determine median position; this takes into account the fact that |
500 |
< |
only the last level in the heap can be partially empty, and that |
501 |
< |
it fills from left to right */ |
502 |
< |
m = left + ((n - n2) > (n2 >> 1) - 1 ? n2 - 1 : n - (n2 >> 1)); |
503 |
< |
|
504 |
< |
while (right > left) { |
505 |
< |
/* Pivot node */ |
506 |
< |
p = heap [heapIdx [right]].pos; |
507 |
< |
l = left; |
508 |
< |
r = right - 1; |
509 |
< |
|
510 |
< |
/* l & r converge, swapping elements out of order with respect to |
511 |
< |
pivot node. Identical keys are resolved by cycling through |
512 |
< |
dim. The convergence point is then the pivot's position. */ |
513 |
< |
do { |
514 |
< |
while (l <= r) { |
515 |
< |
d = dim; |
516 |
< |
|
517 |
< |
while (heap [heapIdx [l]].pos [d] == p [d]) { |
518 |
< |
d = (d + 1) % 3; |
519 |
< |
|
520 |
< |
if (d == dim) { |
521 |
< |
/* Ignore dupes? */ |
522 |
< |
error(WARNING, "duplicate keys in photon heap"); |
523 |
< |
l++; |
524 |
< |
break; |
525 |
< |
} |
526 |
< |
} |
527 |
< |
|
528 |
< |
if (heap [heapIdx [l]].pos [d] < p [d]) |
529 |
< |
l++; |
530 |
< |
else break; |
531 |
< |
} |
532 |
< |
|
533 |
< |
while (r > l) { |
534 |
< |
d = dim; |
535 |
< |
|
536 |
< |
while (heap [heapIdx [r]].pos [d] == p [d]) { |
537 |
< |
d = (d + 1) % 3; |
538 |
< |
|
539 |
< |
if (d == dim) { |
540 |
< |
/* Ignore dupes? */ |
541 |
< |
error(WARNING, "duplicate keys in photon heap"); |
542 |
< |
r--; |
543 |
< |
break; |
544 |
< |
} |
545 |
< |
} |
546 |
< |
|
547 |
< |
if (heap [heapIdx [r]].pos [d] > p [d]) |
548 |
< |
r--; |
549 |
< |
else break; |
550 |
< |
} |
551 |
< |
|
552 |
< |
/* Swap indices (not the nodes they point to) */ |
553 |
< |
n2 = heapIdx [l]; |
554 |
< |
heapIdx [l] = heapIdx [r]; |
555 |
< |
heapIdx [r] = n2; |
556 |
< |
/* Update reverse indices */ |
557 |
< |
heapXdi [heapIdx [l]] = l; |
558 |
< |
heapXdi [n2] = r; |
559 |
< |
} while (l < r); |
560 |
< |
|
561 |
< |
/* Swap indices of convergence and pivot nodes */ |
562 |
< |
heapIdx [r] = heapIdx [l]; |
563 |
< |
heapIdx [l] = heapIdx [right]; |
564 |
< |
heapIdx [right] = n2; |
565 |
< |
/* Update reverse indices */ |
566 |
< |
heapXdi [heapIdx [r]] = r; |
567 |
< |
heapXdi [heapIdx [l]] = l; |
568 |
< |
heapXdi [n2] = right; |
569 |
< |
if (l >= m) right = l - 1; |
570 |
< |
if (l <= m) left = l + 1; |
571 |
< |
} |
572 |
< |
|
573 |
< |
/* Once left & right have converged at m, we have found the median */ |
574 |
< |
return m; |
637 |
> |
#ifdef PMAP_OOC |
638 |
> |
return OOC_GetNearestPhoton(squeue, idx); |
639 |
> |
#else |
640 |
> |
return kdT_GetNearestPhoton(squeue, idx); |
641 |
> |
#endif |
642 |
|
} |
643 |
|
|
644 |
|
|
645 |
|
|
646 |
< |
void buildHeap (Photon* heap, unsigned long* heapIdx, |
580 |
< |
unsigned long* heapXdi, const float min [3], |
581 |
< |
const float max [3], unsigned long left, |
582 |
< |
unsigned long right, unsigned long root) |
583 |
< |
/* Recursive part of balancePhotons(..). Builds heap from subarray |
584 |
< |
defined by indices left and right. min and max are the minimum resp. |
585 |
< |
maximum photon positions in the array. root is the index of the |
586 |
< |
current subtree's root, which corresponds to the median's 1-based |
587 |
< |
index in the heap. heapIdx are the balanced heap indices. The heap |
588 |
< |
is accessed indirectly through these. heapXdi are the reverse indices |
589 |
< |
from the heap to heapIdx so that heapXdi [heapIdx [i]] = i. */ |
646 |
> |
PhotonIdx firstPhoton (const PhotonMap *pmap) |
647 |
|
{ |
648 |
< |
float maxLeft [3], minRight [3]; |
649 |
< |
Photon rootNode; |
650 |
< |
unsigned d; |
651 |
< |
|
652 |
< |
/* Choose median for dimension with largest spread and partition |
596 |
< |
accordingly */ |
597 |
< |
const float d0 = max [0] - min [0], |
598 |
< |
d1 = max [1] - min [1], |
599 |
< |
d2 = max [2] - min [2]; |
600 |
< |
const unsigned char dim = d0 > d1 ? d0 > d2 ? 0 : 2 |
601 |
< |
: d1 > d2 ? 1 : 2; |
602 |
< |
const unsigned long median = |
603 |
< |
left == right ? left |
604 |
< |
: medianPartition(heap, heapIdx, heapXdi, left, right, dim); |
605 |
< |
|
606 |
< |
/* Place median at root of current subtree. This consists of swapping |
607 |
< |
the median and the root nodes and updating the heap indices */ |
608 |
< |
memcpy(&rootNode, heap + heapIdx [median], sizeof(Photon)); |
609 |
< |
memcpy(heap + heapIdx [median], heap + root - 1, sizeof(Photon)); |
610 |
< |
setPhotonDiscr(rootNode, dim); |
611 |
< |
memcpy(heap + root - 1, &rootNode, sizeof(Photon)); |
612 |
< |
heapIdx [heapXdi [root - 1]] = heapIdx [median]; |
613 |
< |
heapXdi [heapIdx [median]] = heapXdi [root - 1]; |
614 |
< |
heapIdx [median] = root - 1; |
615 |
< |
heapXdi [root - 1] = median; |
616 |
< |
|
617 |
< |
/* Update bounds for left and right subtrees and recurse on them */ |
618 |
< |
for (d = 0; d <= 2; d++) |
619 |
< |
if (d == dim) |
620 |
< |
maxLeft [d] = minRight [d] = rootNode.pos [d]; |
621 |
< |
else { |
622 |
< |
maxLeft [d] = max [d]; |
623 |
< |
minRight [d] = min [d]; |
624 |
< |
} |
625 |
< |
|
626 |
< |
if (left < median) |
627 |
< |
buildHeap(heap, heapIdx, heapXdi, min, maxLeft, |
628 |
< |
left, median - 1, root << 1); |
629 |
< |
|
630 |
< |
if (right > median) |
631 |
< |
buildHeap(heap, heapIdx, heapXdi, minRight, max, |
632 |
< |
median + 1, right, (root << 1) + 1); |
648 |
> |
#ifdef PMAP_OOC |
649 |
> |
return OOC_FirstPhoton(pmap); |
650 |
> |
#else |
651 |
> |
return kdT_FirstPhoton(pmap); |
652 |
> |
#endif |
653 |
|
} |
654 |
|
|
655 |
|
|
656 |
|
|
637 |
– |
void balancePhotons (PhotonMap* pmap, double *photonFlux) |
638 |
– |
{ |
639 |
– |
Photon *heap = pmap -> heap; |
640 |
– |
unsigned long i; |
641 |
– |
unsigned long *heapIdx; /* Photon index array */ |
642 |
– |
unsigned long *heapXdi; /* Reverse index to heapIdx */ |
643 |
– |
unsigned j; |
644 |
– |
COLOR flux; |
645 |
– |
/* Need doubles here to reduce errors from increment */ |
646 |
– |
double avgFlux [3] = {0, 0, 0}, CoG [3] = {0, 0, 0}, CoGdist = 0; |
647 |
– |
FVECT d; |
648 |
– |
|
649 |
– |
if (pmap -> heapEnd) { |
650 |
– |
pmap -> heapSize = pmap -> heapEnd; |
651 |
– |
heapIdx = (unsigned long*)malloc(pmap -> heapSize * |
652 |
– |
sizeof(unsigned long)); |
653 |
– |
heapXdi = (unsigned long*)malloc(pmap -> heapSize * |
654 |
– |
sizeof(unsigned long)); |
655 |
– |
if (!heapIdx || !heapXdi) |
656 |
– |
error(USER, "can't allocate heap index"); |
657 |
– |
|
658 |
– |
for (i = 0; i < pmap -> heapSize; i++) { |
659 |
– |
/* Initialize index arrays */ |
660 |
– |
heapXdi [i] = heapIdx [i] = i; |
661 |
– |
getPhotonFlux(heap + i, flux); |
662 |
– |
|
663 |
– |
/* Scale photon's flux (hitherto normalised to 1 over RGB); in case |
664 |
– |
* of a contrib photon map, this is done per light source, and |
665 |
– |
* photonFlux is assumed to be an array */ |
666 |
– |
if (photonFlux) { |
667 |
– |
scalecolor(flux, photonFlux [isContribPmap(pmap) ? |
668 |
– |
photonSrcIdx(pmap, heap + i) : 0]); |
669 |
– |
setPhotonFlux(heap + i, flux); |
670 |
– |
} |
671 |
– |
|
672 |
– |
/* Need a double here */ |
673 |
– |
addcolor(avgFlux, flux); |
674 |
– |
|
675 |
– |
/* Add photon position to centre of gravity */ |
676 |
– |
for (j = 0; j < 3; j++) |
677 |
– |
CoG [j] += heap [i].pos [j]; |
678 |
– |
} |
679 |
– |
|
680 |
– |
/* Average photon positions to get centre of gravity */ |
681 |
– |
for (j = 0; j < 3; j++) |
682 |
– |
pmap -> CoG [j] = CoG [j] /= pmap -> heapSize; |
683 |
– |
|
684 |
– |
/* Compute average photon distance to CoG */ |
685 |
– |
for (i = 0; i < pmap -> heapSize; i++) { |
686 |
– |
VSUB(d, heap [i].pos, CoG); |
687 |
– |
CoGdist += DOT(d, d); |
688 |
– |
} |
689 |
– |
|
690 |
– |
pmap -> CoGdist = CoGdist /= pmap -> heapSize; |
691 |
– |
|
692 |
– |
/* Average photon flux based on RGBE representation */ |
693 |
– |
scalecolor(avgFlux, 1.0 / pmap -> heapSize); |
694 |
– |
copycolor(pmap -> photonFlux, avgFlux); |
695 |
– |
|
696 |
– |
/* Build kd-tree */ |
697 |
– |
buildHeap(pmap -> heap, heapIdx, heapXdi, pmap -> minPos, |
698 |
– |
pmap -> maxPos, 0, pmap -> heapSize - 1, 1); |
699 |
– |
|
700 |
– |
free(heapIdx); |
701 |
– |
free(heapXdi); |
702 |
– |
} |
703 |
– |
} |
704 |
– |
|
705 |
– |
|
706 |
– |
|
657 |
|
void deletePhotons (PhotonMap* pmap) |
658 |
|
{ |
659 |
< |
free(pmap -> heap); |
660 |
< |
free(pmap -> squeue); |
659 |
> |
#ifdef PMAP_OOC |
660 |
> |
OOC_Delete(&pmap -> store); |
661 |
> |
#else |
662 |
> |
kdT_Delete(&pmap -> store); |
663 |
> |
#endif |
664 |
> |
|
665 |
> |
free(pmap -> squeue.node); |
666 |
|
free(pmap -> biasCompHist); |
667 |
|
|
668 |
< |
pmap -> heapSize = 0; |
669 |
< |
pmap -> minGather = pmap -> maxGather = |
715 |
< |
pmap -> squeueSize = pmap -> squeueEnd = 0; |
668 |
> |
pmap -> numPhotons = pmap -> minGather = pmap -> maxGather = |
669 |
> |
pmap -> squeue.len = pmap -> squeue.tail = 0; |
670 |
|
} |