ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapdata.c
(Generate patch)

Comparing ray/src/rt/pmapdata.c (file contents):
Revision 2.4 by greg, Wed May 20 12:58:31 2015 UTC vs.
Revision 2.16 by greg, Wed Sep 28 22:19:18 2016 UTC

# Line 1 | Line 1
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   */
# Line 19 | Line 28
28   #include "otypes.h"
29   #include "source.h"
30   #include "rcontrib.h"
31 + #include "random.h"
32  
33  
34  
# Line 28 | Line 38 | PhotonMap *photonMaps [NUM_PMAP_TYPES] = {
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 +
49 +
50   void initPhotonMap (PhotonMap *pmap, PhotonMapType t)
51   /* Init photon map 'n' stuff... */
52   {
53     if (!pmap)
54        return;
55        
56 <   pmap -> heapSize = pmap -> heapEnd = 0;
38 <   pmap -> heap = NULL;
39 <   pmap -> squeue = NULL;
56 >   pmap -> numPhotons = 0;
57     pmap -> biasCompHist = NULL;
58     pmap -> maxPos [0] = pmap -> maxPos [1] = pmap -> maxPos [2] = -FHUGE;
59     pmap -> minPos [0] = pmap -> minPos [1] = pmap -> minPos [2] = FHUGE;
# Line 46 | Line 63 | void initPhotonMap (PhotonMap *pmap, PhotonMapType t)
63     pmap -> numDensity = 0;
64     pmap -> distribRatio = 1;
65     pmap -> type = t;
66 +   pmap -> squeue.node = NULL;
67 +   pmap -> squeue.len = 0;  
68  
69     /* Init local RNG state */
70     pmap -> randState [0] = 10243;
# Line 57 | Line 76 | void initPhotonMap (PhotonMap *pmap, PhotonMapType t)
76     /* Set up type-specific photon lookup callback */
77     pmap -> lookup = pmapLookup [t];
78  
79 <   pmap -> primary = NULL;
80 <   pmap -> primarySize = pmap -> primaryEnd = 0;
79 >   /* Mark primary photon ray as unused */
80 >   pmap -> lastPrimary.srcIdx = -1;
81 >   pmap -> numPrimary = 0;  
82 >   pmap -> primaries = NULL;
83 >  
84 >   /* Init storage */
85 >   pmap -> heap = NULL;
86 >   pmap -> heapBuf = NULL;
87 >   pmap -> heapBufLen = 0;
88 > #ifdef PMAP_OOC
89 >   OOC_Null(&pmap -> store);
90 > #else
91 >   kdT_Null(&pmap -> store);
92 > #endif
93   }
94  
95  
96  
97 < const PhotonPrimary* addPhotonPrimary (PhotonMap *pmap, const RAY *ray)
97 > void initPhotonHeap (PhotonMap *pmap)
98   {
99 <   PhotonPrimary *prim = NULL;
99 >   int fdFlags;
100    
101 <   if (!pmap || !ray)
102 <      return NULL;
101 >   if (!pmap)
102 >      error(INTERNAL, "undefined photon map in initPhotonHeap");
103        
104 <   /* Check if last primary ray has spawned photons (srcIdx >= 0, see
105 <    * addPhoton()), in which case we keep it and allocate a new one;
106 <    * otherwise we overwrite the unused entry */
107 <   if (pmap -> primary && pmap -> primary [pmap -> primaryEnd].srcIdx >= 0)
108 <      pmap -> primaryEnd++;
109 <      
110 <   if (!pmap -> primarySize || pmap -> primaryEnd >= pmap -> primarySize) {
111 <      /* Allocate/enlarge array */
112 <      pmap -> primarySize += pmap -> heapSizeInc;
82 <      
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");
104 >   if (!pmap -> heap) {
105 >      /* Open heap file */
106 >      if (!(pmap -> heap = tmpfile()))
107 >         error(SYSTEM, "failed opening heap file in initPhotonHeap");
108 > #ifdef F_SETFL  /* XXX is there an alternate needed for Windows? */
109 >      fdFlags = fcntl(fileno(pmap -> heap), F_GETFL);
110 >      fcntl(fileno(pmap -> heap), F_SETFL, fdFlags | O_APPEND);
111 > #endif
112 > /*      ftruncate(fileno(pmap -> heap), 0); */
113     }
114 + }
115 +
116 +
117 +
118 + void flushPhotonHeap (PhotonMap *pmap)
119 + {
120 +   int                  fd;
121 +   const unsigned long  len = pmap -> heapBufLen * sizeof(Photon);
122    
123 <   prim = pmap -> primary + pmap -> primaryEnd;
123 >   if (!pmap)
124 >      error(INTERNAL, "undefined photon map in flushPhotonHeap");
125 >
126 >   if (!pmap -> heap || !pmap -> heapBuf)
127 >      error(INTERNAL, "undefined heap in flushPhotonHeap");
128 >
129 >   /* Atomically seek and write block to heap */
130 >   /* !!! Unbuffered I/O via pwrite() avoids potential race conditions
131 >    * !!! and buffer corruption which can occur with lseek()/fseek()
132 >    * !!! followed by write()/fwrite(). */  
133 >   fd = fileno(pmap -> heap);
134    
135 <   /* Mark unused with negative source index until path spawns a photon (see
136 <    * addPhoton()) */
137 <   prim -> srcIdx = -1;
135 > #ifdef DEBUG_PMAP
136 >   sprintf(errmsg, "Proc %d: flushing %ld photons from pos %ld\n", getpid(),
137 >           pmap -> heapBufLen, lseek(fd, 0, SEEK_END) / sizeof(Photon));
138 >   eputs(errmsg);
139 > #endif
140 >
141 >   /*if (pwrite(fd, pmap -> heapBuf, len, lseek(fd, 0, SEEK_END)) != len) */
142 >   if (write(fd, pmap -> heapBuf, len) != len)
143 >      error(SYSTEM, "failed append to heap file in flushPhotonHeap");
144 >  
145 >   if (fsync(fd))
146 >      error(SYSTEM, "failed fsync in flushPhotonHeap");
147        
148 <   /* Reverse incident direction to point to light source */
149 <   prim -> dir [0] = -ray -> rdir [0];
102 <   prim -> dir [1] = -ray -> rdir [1];
103 <   prim -> dir [2] = -ray -> rdir [2];
148 >   pmap -> heapBufLen = 0;
149 > }
150  
151 <   VCOPY(prim -> pos, ray -> rop);
151 >
152 >
153 > #ifdef DEBUG_OOC
154 > static int checkPhotonHeap (FILE *file)
155 > /* Check heap for nonsensical or duplicate photons */
156 > {
157 >   Photon   p, lastp;
158 >   int      i, dup;
159    
160 <   return prim;
160 >   rewind(file);
161 >   memset(&lastp, 0, sizeof(lastp));
162 >  
163 >   while (fread(&p, sizeof(p), 1, file)) {
164 >      dup = 1;
165 >      
166 >      for (i = 0; i <= 2; i++) {
167 >         if (p.pos [i] < thescene.cuorg [i] ||
168 >             p.pos [i] > thescene.cuorg [i] + thescene.cusize) {
169 >            
170 >            sprintf(errmsg, "corrupt photon in heap at [%f, %f, %f]\n",
171 >                    p.pos [0], p.pos [1], p.pos [2]);
172 >            error(WARNING, errmsg);
173 >         }
174 >        
175 >         dup &= p.pos [i] == lastp.pos [i];
176 >      }
177 >      
178 >      if (dup) {
179 >         sprintf(errmsg,
180 >                 "consecutive duplicate photon in heap at [%f, %f, %f]\n",
181 >                 p.pos [0], p.pos [1], p.pos [2]);
182 >         error(WARNING, errmsg);
183 >      }
184 >   }
185 >
186 >   return 0;
187   }
188 + #endif
189  
190  
191  
192 < const Photon* addPhoton (PhotonMap* pmap, const RAY* ray)
192 > int newPhoton (PhotonMap* pmap, const RAY* ray)
193   {
194     unsigned i;
195 <   Photon* photon = NULL;
195 >   Photon photon;
196     COLOR photonFlux;
197    
198     /* Account for distribution ratio */
199     if (!pmap || pmapRandom(pmap -> randState) > pmap -> distribRatio)
200 <      return NULL;
200 >      return -1;
201        
202     /* Don't store on sources */
203     if (ray -> robj > -1 && islight(objptr(ray -> ro -> omod) -> otype))
204 <      return NULL;
204 >      return -1;
205  
126 #if 0
127   if (inContribPmap(pmap)) {
128      /* Adding contribution photon */
129      if (ray -> parent && ray -> parent -> rtype & PRIMARY)
130         /* Add primary photon ray if parent is primary; by putting this
131          * here and checking the ray's immediate parent, we only add
132          * primaries that actually contribute photons, and we only add them
133          * once.  */
134         addPhotonPrimary(pmap, ray -> parent);
135      
136      /* Save index to primary ray (remains unchanged if primary already in
137       * array) */
138      primary = pmap -> primaryEnd;
139   }
140 #endif
141  
206   #ifdef PMAP_ROI
207 <   /* Store photon if within region of interest -- for egg-spurtz only! */
207 >   /* Store photon if within region of interest -- for Ze Eckspertz only! */
208     if (ray -> rop [0] >= pmapROI [0] && ray -> rop [0] <= pmapROI [1] &&
209         ray -> rop [1] >= pmapROI [2] && ray -> rop [1] <= pmapROI [3] &&
210         ray -> rop [2] >= pmapROI [4] && ray -> rop [2] <= pmapROI [5])
211   #endif
212     {      
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      }
162
163      photon = pmap -> heap + pmap -> heapEnd++;
164
213        /* Adjust flux according to distribution ratio and ray weight */
214        copycolor(photonFlux, ray -> rcol);  
215        scalecolor(photonFlux,
216                   ray -> rweight / (pmap -> distribRatio ? pmap -> distribRatio
217                                                          : 1));
218 <      setPhotonFlux(photon, photonFlux);
218 >      setPhotonFlux(&photon, photonFlux);
219                
220        /* Set photon position and flags */
221 <      VCOPY(photon -> pos, ray -> rop);
222 <      photon -> flags = PMAP_CAUSTICRAY(ray) ? PMAP_CAUST_BIT : 0;
221 >      VCOPY(photon.pos, ray -> rop);
222 >      photon.flags = 0;
223 >      photon.caustic = PMAP_CAUSTICRAY(ray);
224  
225 <      /* Set primary ray index and mark as used for contrib photons */
225 >      /* Set contrib photon's primary ray and subprocess index (the latter
226 >       * to linearise the primary ray indices after photon distribution is
227 >       * complete). Also set primary ray's source index, thereby marking it
228 >       * as used. */
229        if (isContribPmap(pmap)) {
230 <         photon -> primary = pmap -> primaryEnd;
231 <         pmap -> primary [pmap -> primaryEnd].srcIdx = ray -> rsrc;
230 >         photon.primary = pmap -> numPrimary;
231 >         photon.proc = PMAP_GETRAYPROC(ray);
232 >         pmap -> lastPrimary.srcIdx = ray -> rsrc;
233        }
234 <      else photon -> primary = 0;
234 >      else photon.primary = 0;
235        
236 <      /* Update min and max positions & set normal */
237 <      for (i = 0; i <= 2; i++) {
238 <         if (photon -> pos [i] < pmap -> minPos [i])
239 <            pmap -> minPos [i] = photon -> pos [i];
240 <         if (photon -> pos [i] > pmap -> maxPos [i])
241 <            pmap -> maxPos [i] = photon -> pos [i];
242 <         photon -> norm [i] = 127.0 * (isVolumePmap(pmap) ? ray -> rdir [i]
243 <                                                          : ray -> ron [i]);
236 >      /* Set normal */
237 >      for (i = 0; i <= 2; i++)
238 >         photon.norm [i] = 127.0 * (isVolumePmap(pmap) ? ray -> rdir [i]
239 >                                                       : ray -> ron [i]);
240 >
241 >      if (!pmap -> heapBuf) {
242 >         /* Lazily allocate heap buffa */
243 > #if 1        
244 >         /* Randomise buffa size to temporally decorellate buffa flushes */        
245 >         srandom(randSeed + getpid());
246 >         pmap -> heapBufSize = PMAP_HEAPBUFSIZE * (0.5 + frandom());
247 > #else
248 >         /* Randomisation disabled for reproducability during debugging */        
249 >         pmap -> heapBufSize = PMAP_HEAPBUFSIZE;
250 > #endif        
251 >         if (!(pmap -> heapBuf = calloc(pmap -> heapBufSize, sizeof(Photon))))
252 >            error(SYSTEM, "failed heap buffer allocation in newPhoton");
253 >         pmap -> heapBufLen = 0;      
254        }
255 +
256 +      /* Photon initialised; now append to heap buffa */
257 +      memcpy(pmap -> heapBuf + pmap -> heapBufLen, &photon, sizeof(Photon));
258 +                  
259 +      if (++pmap -> heapBufLen >= pmap -> heapBufSize)
260 +         /* Heap buffa full, flush to heap file */
261 +         flushPhotonHeap(pmap);
262 +
263 +      pmap -> numPhotons++;
264     }
265              
266 <   return photon;
266 >   return 0;
267   }
268  
269  
270  
271 < static void nearestNeighbours (PhotonMap* pmap, const float pos [3],
272 <                               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! */
271 > void buildPhotonMap (PhotonMap *pmap, double *photonFlux,
272 >                     PhotonPrimaryIdx *primaryOfs, unsigned nproc)
273   {
274 <   Photon* p = &pmap -> heap [node - 1];
275 <   unsigned i, j;
276 <   /* Signed distance to current photon's splitting plane */
277 <   float d = pos [photonDiscr(*p)] - p -> pos [photonDiscr(*p)],
278 <         d2 = d * d;
279 <   PhotonSQNode* sq = pmap -> squeue;
280 <   const unsigned sqSize = pmap -> squeueSize;
281 <   float dv [3];
274 >   unsigned long  n, nCheck = 0;
275 >   unsigned       i;
276 >   Photon         *p;
277 >   COLOR          flux;
278 >   FILE           *nuHeap;
279 >   /* Need double here to reduce summation errors */
280 >   double         avgFlux [3] = {0, 0, 0}, CoG [3] = {0, 0, 0}, CoGdist = 0;
281 >   FVECT          d;
282    
283 <   /* Search subtree closer to pos first; exclude other subtree if the
284 <      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;
283 >   if (!pmap)
284 >      error(INTERNAL, "undefined photon map in buildPhotonMap");
285        
286 <   if (isContribPmap(pmap) && pmap -> srcContrib) {
287 <      /* Lookup in contribution photon map */
288 <      OBJREC *srcMod;
289 <      const int srcIdx = photonSrcIdx(pmap, p);
290 <      
291 <      if (srcIdx < 0 || srcIdx >= nsources)
239 <         error(INTERNAL, "invalid light source index in photon map");
240 <      
241 <      srcMod = objptr(source [srcIdx].so -> omod);
286 >   /* Get number of photons from heapfile size */
287 >   fseek(pmap -> heap, 0, SEEK_END);      
288 >   pmap -> numPhotons = ftell(pmap -> heap) / sizeof(Photon);
289 >  
290 >   if (!pmap -> numPhotons)
291 >      error(INTERNAL, "empty photon map in buildPhotonMap");  
292  
293 <      /* Reject photon if contributions from light source which emitted it
294 <       * are not sought */
245 <      if (!lu_find(pmap -> srcContrib, srcMod -> oname) -> data)
246 <         return;
293 >   if (!pmap -> heap)
294 >      error(INTERNAL, "no heap in buildPhotonMap");
295  
296 <      /* Reject non-caustic photon if lookup for caustic contribs */
297 <      if (pmap -> lookupFlags & PMAP_CAUST_BIT & ~p -> flags)
298 <         return;
296 > #ifdef DEBUG_PMAP
297 >   eputs("Checking photon heap consistency...\n");
298 >   checkPhotonHeap(pmap -> heap);
299 >  
300 >   sprintf(errmsg, "Heap contains %ld photons\n", pmap -> numPhotons);
301 >   eputs(errmsg);
302 > #endif
303 >    
304 >   /* Allocate heap buffa */
305 >   if (!pmap -> heapBuf) {
306 >      pmap -> heapBufSize = PMAP_HEAPBUFSIZE;
307 >      pmap -> heapBuf = calloc(pmap -> heapBufSize, sizeof(Photon));
308 >      if (!pmap -> heapBuf)
309 >         error(SYSTEM, "failed to allocate postprocessed photon heap in"
310 >               "buildPhotonMap");
311     }
312 +
313 +   /* We REALLY don't need yet another @%&*! heap just to hold the scaled
314 +    * photons, but can't think of a quicker fix... */
315 +   if (!(nuHeap = tmpfile()))
316 +      error(SYSTEM, "failed to open postprocessed photon heap in "
317 +            "buildPhotonMap");
318 +            
319 +   rewind(pmap -> heap);
320 +
321 + #ifdef DEBUG_PMAP
322 +   eputs("Postprocessing photons...\n");
323 + #endif
324    
325 <   /* Squared distance to current photon */
326 <   dv [0] = pos [0] - p -> pos [0];
327 <   dv [1] = pos [1] - p -> pos [1];
328 <   dv [2] = pos [2] - p -> pos [2];
329 <   d2 = DOT(dv, dv);
325 >   while (!feof(pmap -> heap)) {
326 >      pmap -> heapBufLen = fread(pmap -> heapBuf, sizeof(Photon),
327 >                                 PMAP_HEAPBUFSIZE, pmap -> heap);
328 >      
329 >      if (pmap -> heapBufLen) {
330 >         for (n = pmap -> heapBufLen, p = pmap -> heapBuf; n; n--, p++) {
331 >            /* Update min and max pos and set photon flux */
332 >            for (i = 0; i <= 2; i++) {
333 >               if (p -> pos [i] < pmap -> minPos [i])
334 >                  pmap -> minPos [i] = p -> pos [i];
335 >               else if (p -> pos [i] > pmap -> maxPos [i])
336 >                  pmap -> maxPos [i] = p -> pos [i];  
337  
338 <   /* Accept photon if closer than current max dist & add to priority queue */
339 <   if (d2 < pmap -> maxDist) {
340 <      if (pmap -> squeueEnd < sqSize) {
341 <         /* Priority queue not full; append photon and restore heap */
342 <         i = ++pmap -> squeueEnd;
343 <        
344 <         while (i > 1 && sq [(i >> 1) - 1].dist <= d2) {
345 <            sq [i - 1].photon = sq [(i >> 1) - 1].photon;
346 <            sq [i - 1].dist = sq [(i >> 1) - 1].dist;
347 <            i >>= 1;
338 >               /* Update centre of gravity with photon position */                
339 >               CoG [i] += p -> pos [i];                  
340 >            }  
341 >            
342 >            if (primaryOfs)
343 >               /* Linearise photon primary index from subprocess index using the
344 >                * per-subprocess offsets in primaryOfs */
345 >               p -> primary += primaryOfs [p -> proc];
346 >            
347 >            /* Scale photon's flux (hitherto normalised to 1 over RGB); in
348 >             * case of a contrib photon map, this is done per light source,
349 >             * and photonFlux is assumed to be an array */
350 >            getPhotonFlux(p, flux);            
351 >
352 >            if (photonFlux) {
353 >               scalecolor(flux, photonFlux [isContribPmap(pmap) ?
354 >                                               photonSrcIdx(pmap, p) : 0]);
355 >               setPhotonFlux(p, flux);
356 >            }
357 >
358 >            /* Update average photon flux; need a double here */
359 >            addcolor(avgFlux, flux);
360           }
361          
362 <         sq [--i].photon = p;
363 <         sq [i].dist = d2;
364 <         /* Update maxDist if we've just filled the queue */
365 <         if (pmap -> squeueEnd >= pmap -> squeueSize)
366 <            pmap -> maxDist = sq [0].dist;
362 >         /* Write modified photons to new heap */
363 >         fwrite(pmap -> heapBuf, sizeof(Photon), pmap -> heapBufLen, nuHeap);
364 >                
365 >         if (ferror(nuHeap))
366 >            error(SYSTEM, "failed postprocessing photon flux in "
367 >                  "buildPhotonMap");
368        }
369 <      else {
370 <         /* Priority queue full; replace maximum, restore heap, and
279 <            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;
291 <         }
292 <        
293 <         sq [--i].photon = p;
294 <         sq [i].dist = d2;
295 <         pmap -> maxDist = sq [0].dist;
296 <      }
369 >      
370 >      nCheck += pmap -> heapBufLen;
371     }
372 +
373 + #ifdef DEBUG_PMAP
374 +   if (nCheck < pmap -> numPhotons)
375 +      error(INTERNAL, "truncated photon heap in buildPhotonMap");
376 + #endif
377 +  
378 +   /* Finalise average photon flux */
379 +   scalecolor(avgFlux, 1.0 / pmap -> numPhotons);
380 +   copycolor(pmap -> photonFlux, avgFlux);
381 +
382 +   /* Average photon positions to get centre of gravity */
383 +   for (i = 0; i < 3; i++)
384 +      pmap -> CoG [i] = CoG [i] /= pmap -> numPhotons;
385 +      
386 +   rewind(pmap -> heap);
387 +  
388 +   /* Compute average photon distance to centre of gravity */
389 +   while (!feof(pmap -> heap)) {
390 +      pmap -> heapBufLen = fread(pmap -> heapBuf, sizeof(Photon),
391 +                                 PMAP_HEAPBUFSIZE, pmap -> heap);
392 +      
393 +      if (pmap -> heapBufLen)
394 +         for (n = pmap -> heapBufLen, p = pmap -> heapBuf; n; n--, p++) {
395 +            VSUB(d, p -> pos, CoG);
396 +            CoGdist += DOT(d, d);
397 +         }
398 +   }  
399 +
400 +   pmap -> CoGdist = CoGdist /= pmap -> numPhotons;
401 +
402 +   /* Swap heaps */
403 +   fclose(pmap -> heap);
404 +   pmap -> heap = nuHeap;
405 +  
406 + #ifdef PMAP_OOC
407 +   OOC_BuildPhotonMap(pmap, nproc);
408 + #else
409 +   /* kd-tree not parallelised */
410 +   kdT_BuildPhotonMap(pmap);
411 + #endif
412 +
413 +   /* Trash heap and its buffa */
414 +   free(pmap -> heapBuf);
415 +   fclose(pmap -> heap);
416 +   pmap -> heap = NULL;
417 +   pmap -> heapBuf = NULL;
418   }
419  
420  
# Line 309 | Line 429 | static void nearestNeighbours (PhotonMap* pmap, const
429   /* Threshold below which we assume increasing max radius won't help */
430   #define PMAP_SHORT_LOOKUP_THRESH 1
431  
432 + /* Coefficient for adaptive maximum search radius */
433 + #define PMAP_MAXDIST_COEFF 100
434 +
435   void findPhotons (PhotonMap* pmap, const RAY* ray)
436   {
314   float pos [3], norm [3];
437     int redo = 0;
438    
439 <   if (!pmap -> squeue) {
439 >   if (!pmap -> squeue.len) {
440        /* Lazy init priority queue */
441 <      pmap -> squeueSize = pmap -> maxGather + 1;
442 <      pmap -> squeue = (PhotonSQNode*)malloc(pmap -> squeueSize *
443 <                                             sizeof(PhotonSQNode));
444 <      if (!pmap -> squeue)
445 <         error(USER, "can't allocate photon priority queue");
324 <        
441 > #ifdef PMAP_OOC
442 >      OOC_InitFindPhotons(pmap);
443 > #else
444 >      kdT_InitFindPhotons(pmap);      
445 > #endif      
446        pmap -> minGathered = pmap -> maxGather;
447        pmap -> maxGathered = pmap -> minGather;
448        pmap -> totalGathered = 0;
# Line 330 | Line 451 | void findPhotons (PhotonMap* pmap, const RAY* ray)
451        pmap -> minError = FHUGE;
452        pmap -> maxError = -FHUGE;
453        pmap -> rmsError = 0;
454 < #ifdef PMAP_MAXDIST_ABS
455 <      /* Treat maxDistCoeff as an *absolute* and *fixed* max search radius.
456 <         Primarily intended for debugging; FOR ZE ECKSPURTZ ONLY! */
457 <      pmap -> maxDist0 = pmap -> maxDistLimit = maxDistCoeff;
458 < #else
459 <      /* 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      
454 >      /* SQUARED max search radius limit is based on avg photon distance to
455 >       * centre of gravity, unless fixed by user (maxDistFix > 0) */
456 >      pmap -> maxDist0 = pmap -> maxDist2Limit =
457 >         maxDistFix > 0 ? maxDistFix * maxDistFix
458 >                        : PMAP_MAXDIST_COEFF * pmap -> squeue.len *
459 >                          pmap -> CoGdist / pmap -> numPhotons;
460     }
461  
462     do {
463 <      pmap -> squeueEnd = 0;
464 <      pmap -> maxDist = pmap -> maxDist0;
463 >      pmap -> squeue.tail = 0;
464 >      pmap -> maxDist2 = pmap -> maxDist0;
465          
466        /* Search position is ray -> rorg for volume photons, since we have no
467           intersection point. Normals are ignored -- these are incident
468           directions). */  
469        if (isVolumePmap(pmap)) {
470 <         VCOPY(pos, ray -> rorg);
471 <         nearestNeighbours(pmap, pos, NULL, 1);
470 > #ifdef PMAP_OOC
471 >         OOC_FindPhotons(pmap, ray -> rorg, NULL);
472 > #else
473 >         kdT_FindPhotons(pmap, ray -> rorg, NULL);
474 > #endif                  
475        }
476        else {
477 <         VCOPY(pos, ray -> rop);
478 <         VCOPY(norm, ray -> ron);
479 <         nearestNeighbours(pmap, pos, norm, 1);
477 > #ifdef PMAP_OOC
478 >         OOC_FindPhotons(pmap, ray -> rop, ray -> ron);        
479 > #else
480 >         kdT_FindPhotons(pmap, ray -> rop, ray -> ron);
481 > #endif
482        }
483  
484 < #ifndef PMAP_MAXDIST_ABS      
485 <      if (pmap -> squeueEnd < pmap -> squeueSize * pmap -> gatherTolerance) {
484 > #ifdef PMAP_LOOKUP_INFO
485 >      fprintf(stderr, "%d/%d %s photons found within radius %.3f "
486 >              "at (%.2f,%.2f,%.2f) on %s\n", pmap -> squeue.tail,
487 >              pmap -> squeue.len, pmapName [pmap -> type], sqrt(pmap -> maxDist2),
488 >              ray -> rop [0], ray -> rop [1], ray -> rop [2],
489 >              ray -> ro ? ray -> ro -> oname : "<null>");
490 > #endif      
491 >            
492 >      if (pmap -> squeue.tail < pmap -> squeue.len * pmap -> gatherTolerance) {
493           /* Short lookup; too few photons found */
494 <         if (pmap -> squeueEnd > PMAP_SHORT_LOOKUP_THRESH) {
494 >         if (pmap -> squeue.tail > PMAP_SHORT_LOOKUP_THRESH) {
495              /* Ignore short lookups which return fewer than
496               * PMAP_SHORT_LOOKUP_THRESH photons under the assumption there
497               * really are no photons in the vicinity, and increasing the max
498               * search radius therefore won't help */
499 <   #ifdef PMAP_LOOKUP_WARN
499 > #ifdef PMAP_LOOKUP_WARN
500              sprintf(errmsg,
501                      "%d/%d %s photons found at (%.2f,%.2f,%.2f) on %s",
502 <                    pmap -> squeueEnd, pmap -> squeueSize,
503 <                    pmapName [pmap -> type], pos [0], pos [1], pos [2],
502 >                    pmap -> squeue.tail, pmap -> squeue.len,
503 >                    pmapName [pmap -> type],
504 >                    ray -> rop [0], ray -> rop [1], ray -> rop [2],
505                      ray -> ro ? ray -> ro -> oname : "<null>");
506              error(WARNING, errmsg);        
507 <   #endif            
507 > #endif            
508  
509 <            if (pmap -> maxDist0 < pmap -> maxDistLimit) {
509 >            /* Bail out after warning if maxDist is fixed */
510 >            if (maxDistFix > 0)
511 >               return;
512 >            
513 >            if (pmap -> maxDist0 < pmap -> maxDist2Limit) {
514                 /* Increase max search radius if below limit & redo search */
515                 pmap -> maxDist0 *= PMAP_MAXDIST_INC;
516 <   #ifdef PMAP_LOOKUP_REDO
516 > #ifdef PMAP_LOOKUP_REDO
517                 redo = 1;
518 <   #endif              
519 <
387 <   #ifdef PMAP_LOOKUP_WARN
518 > #endif              
519 > #ifdef PMAP_LOOKUP_WARN
520                 sprintf(errmsg,
521                         redo ? "restarting photon lookup with max radius %.1e"
522                              : "max photon lookup radius adjusted to %.1e",
523                         pmap -> maxDist0);
524                 error(WARNING, errmsg);
525 <   #endif
525 > #endif
526              }
527 <   #ifdef PMAP_LOOKUP_REDO
527 > #ifdef PMAP_LOOKUP_REDO
528              else {
529                 sprintf(errmsg, "max photon lookup radius clamped to %.1e",
530                         pmap -> maxDist0);
531                 error(WARNING, errmsg);
532              }
533 <   #endif
533 > #endif
534           }
535          
536           /* Reset successful lookup counter */
537           pmap -> numLookups = 0;
538        }
539        else {
540 +         /* Bail out after warning if maxDist is fixed */
541 +         if (maxDistFix > 0)
542 +            return;
543 +            
544           /* Increment successful lookup counter and reduce max search radius if
545            * wraparound */
546           pmap -> numLookups = (pmap -> numLookups + 1) % PMAP_MAXDIST_CNT;
# Line 413 | Line 549 | void findPhotons (PhotonMap* pmap, const RAY* ray)
549              
550           redo = 0;
551        }
552 < #endif
552 >      
553     } while (redo);
554   }
555  
556  
557  
558 < 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! */
558 > void find1Photon (PhotonMap *pmap, const RAY* ray, Photon *photon)
559   {
560 <   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];
560 >   pmap -> maxDist2 = thescene.cusize;  /* ? */
561    
562 <   /* Search subtree closer to pos first; exclude other subtree if the
563 <      distance to the splitting plane is greater than maxDist */
564 <   if (d < 0) {
565 <      if (node << 1 <= pmap -> heapSize)
566 <         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 <   }
562 > #ifdef PMAP_OOC
563 >   OOC_Find1Photon(pmap, ray -> rop, ray -> ron, photon);
564 > #else
565 >   kdT_Find1Photon(pmap, ray -> rop, ray -> ron, photon);
566 > #endif                  
567   }
568  
569  
570  
571 < Photon* find1Photon (PhotonMap *pmap, const RAY* ray)
571 > void getPhoton (PhotonMap *pmap, PhotonIdx idx, Photon *photon)
572   {
573 <   float fpos [3], norm [3];
574 <   Photon* photon = NULL;
469 <
470 <   VCOPY(fpos, ray -> rop);
471 <   VCOPY(norm, ray -> ron);
472 <   pmap -> maxDist = thescene.cusize;
473 <   nearest1Neighbour(pmap, fpos, norm, &photon, 1);
474 <  
475 <   return photon;
476 < }
477 <
478 <
479 <
480 < 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. */
489 < {
490 <   register const float* p;
491 <   const unsigned long n = right - left + 1;
492 <   register unsigned long l, r, lg2, n2, m;
493 <   register unsigned d;
494 <  
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;
573 > #ifdef PMAP_OOC
574 >   if (OOC_GetPhoton(pmap, idx, photon))
575        
576 <      /* l & r converge, swapping elements out of order with respect to
577 <         pivot node. Identical keys are resolved by cycling through
578 <         dim. The convergence point is then the pivot's position. */
579 <      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;
576 > #else
577 >   if (kdT_GetPhoton(pmap, idx, photon))
578 > #endif
579 >      error(INTERNAL, "failed photon lookup");
580   }
581  
582  
583  
584 < 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. */
584 > Photon *getNearestPhoton (const PhotonSearchQueue *squeue, PhotonIdx idx)
585   {
586 <   float maxLeft [3], minRight [3];
587 <   Photon rootNode;
588 <   unsigned d;
589 <  
590 <   /* 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);
586 > #ifdef PMAP_OOC
587 >   return OOC_GetNearestPhoton(squeue, idx);
588 > #else
589 >   return kdT_GetNearestPhoton(squeue, idx);
590 > #endif
591   }
592  
593  
594  
595 < void balancePhotons (PhotonMap* pmap, double *photonFlux)
595 > PhotonIdx firstPhoton (const PhotonMap *pmap)
596   {
597 <   Photon *heap = pmap -> heap;
598 <   unsigned long i;
599 <   unsigned long *heapIdx;        /* Photon index array */
600 <   unsigned long *heapXdi;        /* Reverse index to heapIdx */
601 <   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 <   }
597 > #ifdef PMAP_OOC
598 >   return OOC_FirstPhoton(pmap);
599 > #else
600 >   return kdT_FirstPhoton(pmap);
601 > #endif
602   }
603  
604  
605  
606   void deletePhotons (PhotonMap* pmap)
607   {
608 <   free(pmap -> heap);
609 <   free(pmap -> squeue);
608 > #ifdef PMAP_OOC
609 >   OOC_Delete(&pmap -> store);
610 > #else
611 >   kdT_Delete(&pmap -> store);
612 > #endif
613 >
614 >   free(pmap -> squeue.node);
615     free(pmap -> biasCompHist);
616    
617 <   pmap -> heapSize = 0;
618 <   pmap -> minGather = pmap -> maxGather =
715 <      pmap -> squeueSize = pmap -> squeueEnd = 0;
617 >   pmap -> numPhotons = pmap -> minGather = pmap -> maxGather =
618 >      pmap -> squeue.len = pmap -> squeue.tail = 0;
619   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines