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.2 by rschregle, Wed Apr 22 15:44:57 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 <       Lucerne University of Applied Sciences & Arts
17 <   ==================================================================  
16 >   (c) Lucerne University of Applied Sciences and Arts,
17 >       supported by the Swiss National Science Foundation (SNSF, #147053)
18 >   ==========================================================================
19    
20     $Id$
21   */
# Line 18 | Line 28
28   #include "otypes.h"
29   #include "source.h"
30   #include "rcontrib.h"
31 + #include "random.h"
32  
33  
34  
# Line 27 | 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;
37 <   pmap -> heap = NULL;
38 <   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 45 | 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 56 | 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;
81 <      
82 <      /* Counter wraparound? */
83 <      if (pmap -> primarySize < pmap -> heapSizeInc)
84 <         error(INTERNAL, "photon primary overflow");
85 <        
86 <      pmap -> primary = (PhotonPrimary *)realloc(pmap -> primary,
87 <                                                 sizeof(PhotonPrimary) *
88 <                                                 pmap -> primarySize);
89 <      if (!pmap -> primary)
90 <         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];
101 <   prim -> dir [1] = -ray -> rdir [1];
102 <   prim -> dir [2] = -ray -> rdir [2];
148 >   pmap -> heapBufLen = 0;
149 > }
150  
151 <   VCOPY(prim -> org, ray -> rorg);
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  
125 #if 0
126   if (inContribPmap(pmap)) {
127      /* Adding contribution photon */
128      if (ray -> parent && ray -> parent -> rtype & PRIMARY)
129         /* Add primary photon ray if parent is primary; by putting this
130          * here and checking the ray's immediate parent, we only add
131          * primaries that actually contribute photons, and we only add them
132          * once.  */
133         addPhotonPrimary(pmap, ray -> parent);
134      
135      /* Save index to primary ray (remains unchanged if primary already in
136       * array) */
137      primary = pmap -> primaryEnd;
138   }
139 #endif
140  
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     {      
148      if (pmap -> heapEnd >= pmap -> heapSize) {
149         /* Enlarge heap */
150         pmap -> heapSize += pmap -> heapSizeInc;
151        
152         /* Counter wraparound? */
153         if (pmap -> heapSize < pmap -> heapSizeInc)
154            error(INTERNAL, "photon heap overflow");
155        
156         pmap -> heap = (Photon *)realloc(pmap -> heap,
157                                          sizeof(Photon) * pmap -> heapSize);
158         if (!pmap -> heap)
159            error(USER, "can't allocate photon heap");
160      }
161
162      photon = pmap -> heap + pmap -> heapEnd++;
163
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)
200 < /* Recursive part of findPhotons(..).
201 <   Note that all heap and priority queue index handling is 1-based, but
202 <   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 */
215 <   if (d < 0) {
216 <      if (node << 1 <= pmap -> heapSize)
217 <         nearestNeighbours(pmap, pos, norm, node << 1);
218 <      if (d2 < pmap -> maxDist && node << 1 < pmap -> heapSize)
219 <         nearestNeighbours(pmap, pos, norm, (node << 1) + 1);
220 <   }
221 <   else {
222 <      if (node << 1 < pmap -> heapSize)
223 <         nearestNeighbours(pmap, pos, norm, (node << 1) + 1);
224 <      if (d2 < pmap -> maxDist && node << 1 <= pmap -> heapSize)
225 <         nearestNeighbours(pmap, pos, norm, node << 1);
226 <   }
227 <
228 <   /* Reject photon if normal faces away (ignored for volume photons) */
229 <   if (norm && DOT(norm, p -> norm) <= 0)
230 <      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)
238 <         error(INTERNAL, "invalid light source index in photon map");
239 <      
240 <      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 */
244 <      if (!lu_find(pmap -> srcContrib, srcMod -> oname) -> data)
245 <         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
278 <            update maxDist */
279 <         i = 1;
280 <        
281 <         while (i <= sqSize >> 1) {
282 <            j = i << 1;
283 <            if (j < sqSize && sq [j - 1].dist < sq [j].dist)
284 <               j++;
285 <            if (d2 >= sq [j - 1].dist)
286 <               break;
287 <            sq [i - 1].photon = sq [j - 1].photon;
288 <            sq [i - 1].dist = sq [j - 1].dist;
289 <            i = j;
290 <         }
291 <        
292 <         sq [--i].photon = p;
293 <         sq [i].dist = d2;
294 <         pmap -> maxDist = sq [0].dist;
295 <      }
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 308 | 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   {
313   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");
323 <        
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 329 | Line 451 | void findPhotons (PhotonMap* pmap, const RAY* ray)
451        pmap -> minError = FHUGE;
452        pmap -> maxError = -FHUGE;
453        pmap -> rmsError = 0;
454 <      /* Maximum search radius limit based on avg photon distance to
455 <       * centre of gravity */
456 <      pmap -> maxDist0 = pmap -> maxDistLimit =
457 <         maxDistCoeff * pmap -> squeueSize * pmap -> CoGdist /
458 <         pmap -> heapSize;
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 <      if (pmap -> squeueEnd < pmap -> squeueSize * pmap -> gatherTolerance) {
483 >
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
# Line 363 | Line 499 | void findPhotons (PhotonMap* pmap, const RAY* ray)
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            
508 <            
509 <            if (pmap -> maxDist0 < pmap -> maxDistLimit) {
508 >
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
517                 redo = 1;
518   #endif              
378
519   #ifdef PMAP_LOOKUP_WARN
520                 sprintf(errmsg,
521                         redo ? "restarting photon lookup with max radius %.1e"
# Line 395 | Line 535 | void findPhotons (PhotonMap* pmap, const RAY* ray)
535          
536           /* Reset successful lookup counter */
537           pmap -> numLookups = 0;
538 <      }  
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 405 | Line 549 | void findPhotons (PhotonMap* pmap, const RAY* ray)
549              
550           redo = 0;
551        }
552 +      
553     } while (redo);
554   }
555  
556  
557  
558 < static void nearest1Neighbour (PhotonMap *pmap, const float pos [3],
414 <                               const float norm [3], Photon **photon,
415 <                               unsigned long node)
416 < /* Recursive part of find1Photon(..).
417 <   Note that all heap index handling is 1-based, but accesses to the
418 <   arrays are 0-based! */
558 > void find1Photon (PhotonMap *pmap, const RAY* ray, Photon *photon)
559   {
560 <   Photon *p = pmap -> heap + node - 1;
421 <   /* Signed distance to current photon's splitting plane */
422 <   float d = pos [photonDiscr(*p)] - p -> pos [photonDiscr(*p)],
423 <         d2 = d * d;
424 <   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);
431 <      if (d2 < pmap -> maxDist && node << 1 < pmap -> heapSize)
432 <         nearest1Neighbour(pmap, pos, norm, photon, (node << 1) + 1);
433 <   }
434 <   else {
435 <      if (node << 1 < pmap -> heapSize)
436 <         nearest1Neighbour(pmap, pos, norm, photon, (node << 1) + 1);
437 <      if (d2 < pmap -> maxDist && node << 1 <= pmap -> heapSize)
438 <         nearest1Neighbour(pmap, pos, norm, photon, node << 1);
439 <   }
440 <  
441 <   /* Squared distance to current photon */
442 <   dv [0] = pos [0] - p -> pos [0];
443 <   dv [1] = pos [1] - p -> pos [1];
444 <   dv [2] = pos [2] - p -> pos [2];
445 <   d2 = DOT(dv, dv);
446 <  
447 <   if (d2 < pmap -> maxDist && DOT(norm, p -> norm) > 0) {
448 <      /* Closest photon so far with similar normal */
449 <      pmap -> maxDist = d2;
450 <      *photon = p;
451 <   }
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;
460 <
461 <   VCOPY(fpos, ray -> rop);
462 <   VCOPY(norm, ray -> ron);
463 <   pmap -> maxDist = thescene.cusize;
464 <   nearest1Neighbour(pmap, fpos, norm, &photon, 1);
465 <  
466 <   return photon;
467 < }
468 <
469 <
470 <
471 < static unsigned long medianPartition (const Photon* heap,
472 <                                      unsigned long* heapIdx,
473 <                                      unsigned long* heapXdi,
474 <                                      unsigned long left,
475 <                                      unsigned long right, unsigned dim)
476 < /* Returns index to median in heap from indices left to right
477 <   (inclusive) in dimension dim. The heap is partitioned relative to
478 <   median using a quicksort algorithm. The heap indices in heapIdx are
479 <   sorted rather than the heap itself. */
480 < {
481 <   register const float* p;
482 <   const unsigned long n = right - left + 1;
483 <   register unsigned long l, r, lg2, n2, m;
484 <   register unsigned d;
485 <  
486 <   /* Round down n to nearest power of 2 */
487 <   for (lg2 = 0, n2 = n; n2 > 1; n2 >>= 1, ++lg2);
488 <   n2 = 1 << lg2;
489 <  
490 <   /* Determine median position; this takes into account the fact that
491 <      only the last level in the heap can be partially empty, and that
492 <      it fills from left to right */
493 <   m = left + ((n - n2) > (n2 >> 1) - 1 ? n2 - 1 : n - (n2 >> 1));
494 <  
495 <   while (right > left) {
496 <      /* Pivot node */
497 <      p = heap [heapIdx [right]].pos;
498 <      l = left;
499 <      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 {
505 <         while (l <= r) {
506 <            d = dim;
507 <            
508 <            while (heap [heapIdx [l]].pos [d] == p [d]) {
509 <               d = (d + 1) % 3;
510 <              
511 <               if (d == dim) {
512 <                  /* Ignore dupes? */
513 <                  error(WARNING, "duplicate keys in photon heap");
514 <                  l++;
515 <                  break;
516 <               }
517 <            }
518 <            
519 <            if (heap [heapIdx [l]].pos [d] < p [d])
520 <               l++;
521 <            else break;
522 <         }
523 <        
524 <         while (r > l) {
525 <            d = dim;
526 <            
527 <            while (heap [heapIdx [r]].pos [d] == p [d]) {
528 <               d = (d + 1) % 3;
529 <              
530 <               if (d == dim) {
531 <                  /* Ignore dupes? */
532 <                  error(WARNING, "duplicate keys in photon heap");
533 <                  r--;
534 <                  break;
535 <               }
536 <            }
537 <            
538 <            if (heap [heapIdx [r]].pos [d] > p [d])
539 <               r--;
540 <            else break;
541 <         }
542 <        
543 <         /* Swap indices (not the nodes they point to) */
544 <         n2 = heapIdx [l];
545 <         heapIdx [l] = heapIdx [r];
546 <         heapIdx [r] = n2;
547 <         /* Update reverse indices */
548 <         heapXdi [heapIdx [l]] = l;
549 <         heapXdi [n2] = r;
550 <      } while (l < r);
551 <      
552 <      /* Swap indices of convergence and pivot nodes */
553 <      heapIdx [r] = heapIdx [l];
554 <      heapIdx [l] = heapIdx [right];
555 <      heapIdx [right] = n2;
556 <      /* Update reverse indices */
557 <      heapXdi [heapIdx [r]] = r;
558 <      heapXdi [heapIdx [l]] = l;
559 <      heapXdi [n2] = right;
560 <      if (l >= m) right = l - 1;
561 <      if (l <= m) left = l + 1;
562 <   }
563 <  
564 <   /* Once left & right have converged at m, we have found the median */
565 <   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,
571 <                unsigned long* heapXdi, const float min [3],
572 <                const float max [3], unsigned long left,
573 <                unsigned long right, unsigned long root)
574 < /* Recursive part of balancePhotons(..). Builds heap from subarray
575 <   defined by indices left and right. min and max are the minimum resp.
576 <   maximum photon positions in the array. root is the index of the
577 <   current subtree's root, which corresponds to the median's 1-based
578 <   index in the heap. heapIdx are the balanced heap indices. The heap
579 <   is accessed indirectly through these. heapXdi are the reverse indices
580 <   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
587 <      accordingly */
588 <   const float d0 = max [0] - min [0],
589 <               d1 = max [1] - min [1],
590 <               d2 = max [2] - min [2];
591 <   const unsigned char dim = d0 > d1 ? d0 > d2 ? 0 : 2
592 <                                     : d1 > d2 ? 1 : 2;
593 <   const unsigned long median =
594 <      left == right ? left
595 <                    : medianPartition(heap, heapIdx, heapXdi, left, right, dim);
596 <  
597 <   /* Place median at root of current subtree. This consists of swapping
598 <      the median and the root nodes and updating the heap indices */
599 <   memcpy(&rootNode, heap + heapIdx [median], sizeof(Photon));
600 <   memcpy(heap + heapIdx [median], heap + root - 1, sizeof(Photon));
601 <   setPhotonDiscr(rootNode, dim);
602 <   memcpy(heap + root - 1, &rootNode, sizeof(Photon));
603 <   heapIdx [heapXdi [root - 1]] = heapIdx [median];
604 <   heapXdi [heapIdx [median]] = heapXdi [root - 1];
605 <   heapIdx [median] = root - 1;
606 <   heapXdi [root - 1] = median;
607 <  
608 <   /* Update bounds for left and right subtrees and recurse on them */
609 <   for (d = 0; d <= 2; d++)
610 <      if (d == dim)
611 <         maxLeft [d] = minRight [d] = rootNode.pos [d];
612 <      else {
613 <         maxLeft [d] = max [d];
614 <         minRight [d] = min [d];
615 <      }
616 <      
617 <   if (left < median)
618 <      buildHeap(heap, heapIdx, heapXdi, min, maxLeft,
619 <                left, median - 1, root << 1);
620 <                
621 <   if (right > median)
622 <      buildHeap(heap, heapIdx, heapXdi, minRight, max,
623 <                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;
635 <   COLOR flux;
636 <   /* Need doubles here to reduce errors from increment */
637 <   double avgFlux [3] = {0, 0, 0}, CoG [3] = {0, 0, 0}, CoGdist = 0;
638 <   FVECT d;
639 <  
640 <   if (pmap -> heapEnd) {
641 <      pmap -> heapSize = pmap -> heapEnd;
642 <      heapIdx = (unsigned long*)malloc(pmap -> heapSize *
643 <                                       sizeof(unsigned long));
644 <      heapXdi = (unsigned long*)malloc(pmap -> heapSize *
645 <                                       sizeof(unsigned long));
646 <      if (!heapIdx || !heapXdi)
647 <         error(USER, "can't allocate heap index");
648 <        
649 <      for (i = 0; i < pmap -> heapSize; i++) {
650 <         /* Initialize index arrays */
651 <         heapXdi [i] = heapIdx [i] = i;
652 <         getPhotonFlux(heap + i, flux);
653 <
654 <         /* Scale photon's flux (hitherto normalised to 1 over RGB); in case
655 <          * of a contrib photon map, this is done per light source, and
656 <          * photonFlux is assumed to be an array */
657 <         if (photonFlux) {
658 <            scalecolor(flux, photonFlux [isContribPmap(pmap) ?
659 <                                         photonSrcIdx(pmap, heap + i) : 0]);
660 <            setPhotonFlux(heap + i, flux);
661 <         }
662 <
663 <         /* Need a double here */
664 <         addcolor(avgFlux, flux);
665 <        
666 <         /* Add photon position to centre of gravity */
667 <         for (j = 0; j < 3; j++)
668 <            CoG [j] += heap [i].pos [j];
669 <      }
670 <      
671 <      /* Average photon positions to get centre of gravity */
672 <      for (j = 0; j < 3; j++)
673 <         pmap -> CoG [j] = CoG [j] /= pmap -> heapSize;
674 <      
675 <      /* Compute average photon distance to CoG */
676 <      for (i = 0; i < pmap -> heapSize; i++) {
677 <         VSUB(d, heap [i].pos, CoG);
678 <         CoGdist += DOT(d, d);
679 <      }
680 <      
681 <      pmap -> CoGdist = CoGdist /= pmap -> heapSize;
682 <      
683 <      /* Average photon flux based on RGBE representation */
684 <      scalecolor(avgFlux, 1.0 / pmap -> heapSize);
685 <      copycolor(pmap -> photonFlux, avgFlux);
686 <      
687 <      /* Build kd-tree */
688 <      buildHeap(pmap -> heap, heapIdx, heapXdi, pmap -> minPos,
689 <                pmap -> maxPos, 0, pmap -> heapSize - 1, 1);
690 <                
691 <      free(heapIdx);
692 <      free(heapXdi);
693 <   }
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 =
706 <      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