ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmap.c
Revision: 2.2
Committed: Tue Apr 21 19:16:51 2015 UTC (9 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +3 -4 lines
Log Message:
Fixes for Windows and photon map

File Contents

# Content
1 /*
2 ==================================================================
3 Photon map main module
4
5 Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
6 (c) Fraunhofer Institute for Solar Energy Systems,
7 Lucerne University of Applied Sciences & Arts
8 ==================================================================
9
10 $Id: pmap.c,v 2.1 2015/02/24 19:39:26 greg Exp $
11 */
12
13
14
15 #include "pmap.h"
16 #include "pmapmat.h"
17 #include "pmapsrc.h"
18 #include "pmaprand.h"
19 #include "pmapio.h"
20 #include "pmapbias.h"
21 #include "pmapdiag.h"
22 #include "otypes.h"
23 #include <time.h>
24 #include <sys/stat.h>
25
26
27
28 extern char *octname;
29
30 static char PmapRevision [] = "$Revision: 2.1 $";
31
32
33
34 /* Photon map lookup functions per type */
35 void (*pmapLookup [NUM_PMAP_TYPES])(PhotonMap*, RAY*, COLOR) = {
36 photonDensity, photonPreCompDensity, photonDensity, volumePhotonDensity,
37 photonDensity, NULL
38 };
39
40
41
42 void colorNorm (COLOR c)
43 /* Normalise colour channels to average of 1 */
44 {
45 const float avg = colorAvg(c);
46
47 if (!avg)
48 return;
49
50 c [0] /= avg;
51 c [1] /= avg;
52 c [2] /= avg;
53 }
54
55
56
57 void loadPmaps (PhotonMap **pmaps, const PhotonMapParams *parm)
58 {
59 unsigned t;
60 struct stat octstat, pmstat;
61 PhotonMap *pm;
62 PhotonMapType type;
63
64 for (t = 0; t < NUM_PMAP_TYPES; t++)
65 if (setPmapParam(&pm, parm + t)) {
66 /* Check if photon map newer than octree */
67 if (!stat(pm -> fileName, &pmstat) && !stat(octname, &octstat) &&
68 octstat.st_mtime > pmstat.st_mtime) {
69 sprintf(errmsg, "photon map in file %s may be stale",
70 pm -> fileName);
71 error(USER, errmsg);
72 }
73
74 /* Load photon map from file and get its type */
75 if ((type = loadPhotonMap(pm, pm -> fileName)) == PMAP_TYPE_NONE)
76 error(USER, "failed loading photon map");
77
78 /* Assign to appropriate photon map type (deleting previously
79 * loaded photon map of same type if necessary) */
80 if (pmaps [type]) {
81 deletePhotons(pmaps [type]);
82 free(pmaps [type]);
83 }
84 pmaps [type] = pm;
85
86 /* Check for invalid density estimate bandwidth */
87 if (pm -> maxGather > pm -> heapSize) {
88 error(WARNING, "adjusting density estimate bandwidth");
89 pm -> minGather = pm -> maxGather = pm -> heapSize;
90 }
91 }
92 }
93
94
95
96 void savePmaps (const PhotonMap **pmaps, int argc, char **argv)
97 {
98 unsigned t;
99
100 for (t = 0; t < NUM_PMAP_TYPES; t++) {
101 if (pmaps [t])
102 savePhotonMap(pmaps [t], pmaps [t] -> fileName, t, argc, argv);
103 }
104 }
105
106
107
108 void cleanUpPmaps (PhotonMap **pmaps)
109 {
110 unsigned t;
111
112 for (t = 0; t < NUM_PMAP_TYPES; t++) {
113 if (pmaps [t]) {
114 deletePhotons(pmaps [t]);
115 free(pmaps [t]);
116 }
117 }
118 }
119
120
121
122 static int photonParticipate (RAY *ray)
123 /* Trace photon through participating medium. Returns 1 if passed through,
124 or 0 if absorbed and $*%&ed. Analogon to rayparticipate(). */
125 {
126 int i;
127 RREAL cosTheta, cosPhi, du, dv;
128 const float cext = colorAvg(ray -> cext),
129 albedo = colorAvg(ray -> albedo);
130 FVECT u, v;
131 COLOR cvext;
132
133 /* Mean free distance until interaction with medium */
134 ray -> rmax = -log(pmapRandom(mediumState)) / cext;
135
136 while (!localhit(ray, &thescene)) {
137 setcolor(cvext, exp(-ray -> rmax * ray -> cext [0]),
138 exp(-ray -> rmax * ray -> cext [1]),
139 exp(-ray -> rmax * ray -> cext [2]));
140
141 /* Modify ray color and normalise */
142 multcolor(ray -> rcol, cvext);
143 colorNorm(ray -> rcol);
144 VCOPY(ray -> rorg, ray -> rop);
145
146 if (albedo > FTINY)
147 /* Add to volume photon map */
148 if (ray -> rlvl > 0) addPhoton(volumePmap, ray);
149
150 /* Absorbed? */
151 if (pmapRandom(rouletteState) > albedo) return 0;
152
153 /* Colour bleeding without attenuation (?) */
154 multcolor(ray -> rcol, ray -> albedo);
155 scalecolor(ray -> rcol, 1 / albedo);
156
157 /* Scatter photon */
158 cosTheta = ray -> gecc <= FTINY ? 2 * pmapRandom(scatterState) - 1
159 : 1 / (2 * ray -> gecc) *
160 (1 + ray -> gecc * ray -> gecc -
161 (1 - ray -> gecc * ray -> gecc) /
162 (1 - ray -> gecc + 2 * ray -> gecc *
163 pmapRandom(scatterState)));
164
165 cosPhi = cos(2 * PI * pmapRandom(scatterState));
166 du = dv = sqrt(1 - cosTheta * cosTheta); /* sin(theta) */
167 du *= cosPhi;
168 dv *= sqrt(1 - cosPhi * cosPhi); /* sin(phi) */
169
170 /* Get axes u & v perpendicular to photon direction */
171 i = 0;
172 do {
173 v [0] = v [1] = v [2] = 0;
174 v [i++] = 1;
175 fcross(u, v, ray -> rdir);
176 } while (normalize(u) < FTINY);
177 fcross(v, ray -> rdir, u);
178
179 for (i = 0; i < 3; i++)
180 ray -> rdir [i] = du * u [i] + dv * v [i] +
181 cosTheta * ray -> rdir [i];
182 ray -> rlvl++;
183 ray -> rmax = -log(pmapRandom(mediumState)) / cext;
184 }
185
186 setcolor(cvext, exp(-ray -> rot * ray -> cext [0]),
187 exp(-ray -> rot * ray -> cext [1]),
188 exp(-ray -> rot * ray -> cext [2]));
189
190 /* Modify ray color and normalise */
191 multcolor(ray -> rcol, cvext);
192 colorNorm(ray -> rcol);
193
194 /* Passed through medium */
195 return 1;
196 }
197
198
199
200 void tracePhoton (RAY *ray)
201 /* Follow photon as it bounces around... */
202 {
203 long mod;
204 OBJREC* mat;
205
206 if (ray -> rlvl > photonMaxBounce) {
207 error(WARNING, "runaway photon!");
208 return;
209 }
210
211 if (colorAvg(ray -> cext) > FTINY && !photonParticipate(ray))
212 return;
213
214 if (localhit(ray, &thescene)) {
215 mod = ray -> ro -> omod;
216
217 if ((ray -> clipset && inset(ray -> clipset, mod)) || mod == OVOID) {
218 /* Transfer ray if modifier is VOID or clipped within antimatta */
219 RAY tray;
220 photonRay(ray, &tray, PMAP_XFER, NULL);
221 tracePhoton(&tray);
222 }
223 else {
224 /* Scatter for modifier material */
225 mat = objptr(mod);
226 photonScatter [mat -> otype] (mat, ray);
227 }
228 }
229 }
230
231
232
233 static void preComputeGlobal (PhotonMap *pmap)
234 /* Precompute irradiance from global photons for final gathering using
235 the first finalGather * pmap -> heapSize photons in the heap. Returns
236 new heap with precomputed photons. */
237 {
238 unsigned long i, nuHeapSize;
239 unsigned j;
240 Photon *nuHeap, *p;
241 COLOR irrad;
242 RAY ray;
243 float nuMinPos [3], nuMaxPos [3];
244
245 repComplete = nuHeapSize = finalGather * pmap -> heapSize;
246
247 if (photonRepTime) {
248 sprintf(errmsg,
249 "Precomputing irradiance for %ld global photons...\n",
250 nuHeapSize);
251 eputs(errmsg);
252 fflush(stderr);
253 }
254
255 p = nuHeap = (Photon*)malloc(nuHeapSize * sizeof(Photon));
256 if (!nuHeap)
257 error(USER, "can't allocate photon heap");
258
259 for (j = 0; j <= 2; j++) {
260 nuMinPos [j] = FHUGE;
261 nuMaxPos [j] = -FHUGE;
262 }
263
264 /* Record start time, baby */
265 repStartTime = time(NULL);
266 signal(SIGCONT, pmapPreCompReport);
267 repProgress = 0;
268 memcpy(nuHeap, pmap -> heap, nuHeapSize * sizeof(Photon));
269
270 for (i = 0, p = nuHeap; i < nuHeapSize; i++, p++) {
271 ray.ro = NULL;
272 VCOPY(ray.rop, p -> pos);
273
274 /* Update min and max positions & set ray normal */
275 for (j = 0; j < 3; j++) {
276 if (p -> pos [j] < nuMinPos [j]) nuMinPos [j] = p -> pos [j];
277 if (p -> pos [j] > nuMaxPos [j]) nuMaxPos [j] = p -> pos [j];
278 ray.ron [j] = p -> norm [j] / 127.0;
279 }
280
281 photonDensity(pmap, &ray, irrad);
282 setPhotonFlux(p, irrad);
283 repProgress++;
284
285 if (photonRepTime > 0 && time(NULL) >= repLastTime + photonRepTime)
286 pmapPreCompReport();
287 #ifndef BSD
288 else signal(SIGCONT, pmapPreCompReport);
289 #endif
290 }
291
292 signal(SIGCONT, SIG_DFL);
293
294 /* Replace & rebuild heap */
295 free(pmap -> heap);
296 pmap -> heap = nuHeap;
297 pmap -> heapSize = pmap -> heapEnd = nuHeapSize;
298 VCOPY(pmap -> minPos, nuMinPos);
299 VCOPY(pmap -> maxPos, nuMaxPos);
300
301 if (photonRepTime) {
302 eputs("Rebuilding global photon heap...\n");
303 fflush(stderr);
304 }
305
306 balancePhotons(pmap, NULL);
307 }
308
309
310
311 void distribPhotons (PhotonMap **pmaps)
312 {
313 EmissionMap emap;
314 char errmsg2 [128];
315 unsigned t, srcIdx, passCnt = 0, prePassCnt = 0;
316 double totalFlux = 0;
317 PhotonMap *pm;
318
319 for (t = 0; t < NUM_PMAP_TYPES && !photonMaps [t]; t++);
320 if (t >= NUM_PMAP_TYPES)
321 error(USER, "no photon maps defined");
322
323 if (!nsources)
324 error(USER, "no light sources");
325
326 /* ===================================================================
327 * INITIALISATION - Set up emission and scattering funcs
328 * =================================================================== */
329 emap.samples = NULL;
330 emap.maxPartitions = MAXSPART;
331 emap.partitions = (unsigned char*)malloc(emap.maxPartitions >> 1);
332 if (!emap.partitions)
333 error(INTERNAL, "can't allocate source partitions");
334
335 /* Initialise all defined photon maps */
336 for (t = 0; t < NUM_PMAP_TYPES; t++)
337 initPhotonMap(photonMaps [t], t);
338
339 initPhotonEmissionFuncs();
340 initPhotonScatterFuncs();
341
342 /* Get photon ports if specified */
343 if (ambincl == 1)
344 getPhotonPorts();
345
346 /* Get photon sensor modifiers */
347 getPhotonSensors(photonSensorList);
348
349 /* Seed RNGs for photon distribution */
350 pmapSeed(randSeed, partState);
351 pmapSeed(randSeed, emitState);
352 pmapSeed(randSeed, cntState);
353 pmapSeed(randSeed, mediumState);
354 pmapSeed(randSeed, scatterState);
355 pmapSeed(randSeed, rouletteState);
356
357 if (photonRepTime)
358 eputs("\n");
359
360 /* ===================================================================
361 * FLUX INTEGRATION - Get total photon flux from light sources
362 * =================================================================== */
363 for (srcIdx = 0; srcIdx < nsources; srcIdx++) {
364 unsigned portCnt = 0;
365 emap.src = source + srcIdx;
366
367 do {
368 emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt
369 : NULL;
370 photonPartition [emap.src -> so -> otype] (&emap);
371
372 if (photonRepTime) {
373 sprintf(errmsg, "Integrating flux from source %s ",
374 source [srcIdx].so -> oname);
375
376 if (emap.port) {
377 sprintf(errmsg2, "via port %s ",
378 photonPorts [portCnt].so -> oname);
379 strcat(errmsg, errmsg2);
380 }
381
382 sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions);
383 strcat(errmsg, errmsg2);
384 eputs(errmsg);
385 fflush(stderr);
386 }
387
388 for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
389 emap.partitionCnt++) {
390 initPhotonEmission(&emap, pdfSamples);
391 totalFlux += colorAvg(emap.partFlux);
392 }
393
394 portCnt++;
395 } while (portCnt < numPhotonPorts);
396 }
397
398 if (totalFlux < FTINY)
399 error(USER, "zero flux from light sources");
400
401 /* Record start time and enable progress report signal handler */
402 repStartTime = time(NULL);
403 signal(SIGCONT, pmapDistribReport);
404 repProgress = prePassCnt = 0;
405
406 if (photonRepTime)
407 eputs("\n");
408
409 /* ===================================================================
410 * 2-PASS PHOTON DISTRIBUTION
411 * Pass 1 (pre): emit fraction of target photon count
412 * Pass 2 (main): based on outcome of pass 1, estimate remaining number
413 * of photons to emit to approximate target count
414 * =================================================================== */
415 do {
416 double numEmit;
417
418 if (!passCnt) {
419 /* INIT PASS 1 */
420 /* Skip if no photons contributed after sufficient iterations; make
421 * it clear to user which photon maps are missing so (s)he can
422 * check the scene geometry and materials */
423 if (++prePassCnt > maxPreDistrib) {
424 sprintf(errmsg, "too many prepasses");
425
426 for (t = 0; t < NUM_PMAP_TYPES; t++)
427 if (photonMaps [t] && !photonMaps [t] -> heapEnd) {
428 sprintf(errmsg2, ", no %s photons stored", pmapName [t]);
429 strcat(errmsg, errmsg2);
430 }
431
432 error(USER, errmsg);
433 break;
434 }
435
436 /* Num to emit is fraction of minimum target count */
437 numEmit = FHUGE;
438
439 for (t = 0; t < NUM_PMAP_TYPES; t++)
440 if (photonMaps [t])
441 numEmit = min(photonMaps [t] -> distribTarget, numEmit);
442
443 numEmit *= preDistrib;
444 }
445
446 else {
447 /* INIT PASS 2 */
448 /* Based on the outcome of the predistribution we can now estimate
449 * how many more photons we have to emit for each photon map to
450 * meet its respective target count. This value is clamped to 0 in
451 * case the target has already been exceeded in the pass 1. Note
452 * repProgress is the number of photons emitted thus far, while
453 * heapEnd is the number of photons stored in each photon map. */
454 double maxDistribRatio = 0;
455
456 /* Set the distribution ratio for each map; this indicates how many
457 * photons of each respective type are stored per emitted photon,
458 * and is used as probability for storing a photon by addPhoton().
459 * Since this biases the photon density, addPhoton() promotes the
460 * flux of stored photons to compensate. */
461 for (t = 0; t < NUM_PMAP_TYPES; t++)
462 if ((pm = photonMaps [t])) {
463 pm -> distribRatio = (double)pm -> distribTarget /
464 pm -> heapEnd - 1;
465
466 /* Check if photon map "overflowed", i.e. exceeded its target
467 * count in the prepass; correcting the photon flux via the
468 * distribution ratio is no longer possible, as no more
469 * photons of this type will be stored, so notify the user
470 * rather than deliver incorrect results.
471 * In future we should handle this more intelligently by
472 * using the photonFlux in each photon map to individually
473 * correct the flux after distribution. */
474 if (pm -> distribRatio <= FTINY) {
475 sprintf(errmsg,
476 "%s photon map overflow in prepass, reduce -apD",
477 pmapName [t]);
478 error(INTERNAL, errmsg);
479 }
480
481 maxDistribRatio = max(pm -> distribRatio, maxDistribRatio);
482 }
483
484 /* Normalise distribution ratios and calculate number of photons to
485 * emit in main pass */
486 for (t = 0; t < NUM_PMAP_TYPES; t++)
487 if ((pm = photonMaps [t]))
488 pm -> distribRatio /= maxDistribRatio;
489
490 if ((numEmit = repProgress * maxDistribRatio) < FTINY)
491 /* No photons left to distribute in main pass */
492 break;
493 }
494
495 /* Set completion count for progress report */
496 repComplete = numEmit + repProgress;
497
498 /* PHOTON DISTRIBUTION LOOP */
499 for (srcIdx = 0; srcIdx < nsources; srcIdx++) {
500 unsigned portCnt = 0;
501 emap.src = source + srcIdx;
502
503 do {
504 emap.port = emap.src -> sflags & SDISTANT ? photonPorts + portCnt
505 : NULL;
506 photonPartition [emap.src -> so -> otype] (&emap);
507
508 if (photonRepTime) {
509 if (!passCnt)
510 sprintf(errmsg, "PREPASS %d on source %s ",
511 prePassCnt, source [srcIdx].so -> oname);
512 else
513 sprintf(errmsg, "MAIN PASS on source %s ",
514 source [srcIdx].so -> oname);
515
516 if (emap.port) {
517 sprintf(errmsg2, "via port %s ",
518 photonPorts [portCnt].so -> oname);
519 strcat(errmsg, errmsg2);
520 }
521
522 sprintf(errmsg2, "(%lu partitions)...\n", emap.numPartitions);
523 strcat(errmsg, errmsg2);
524 eputs(errmsg);
525 fflush(stderr);
526 }
527
528 for (emap.partitionCnt = 0; emap.partitionCnt < emap.numPartitions;
529 emap.partitionCnt++) {
530 double partNumEmit;
531 unsigned long partEmitCnt;
532
533 /* Get photon origin within current source partishunn and
534 * build emission map */
535 photonOrigin [emap.src -> so -> otype] (&emap);
536 initPhotonEmission(&emap, pdfSamples);
537
538 /* Number of photons to emit from ziss partishunn --
539 * proportional to flux; photon ray weight and scalar flux
540 * are uniform (the latter only varying in RGB). */
541 partNumEmit = numEmit * colorAvg(emap.partFlux) / totalFlux;
542 partEmitCnt = (unsigned long)partNumEmit;
543
544 /* Probabilistically account for fractional photons */
545 if (pmapRandom(cntState) < partNumEmit - partEmitCnt)
546 partEmitCnt++;
547
548 /* Integer counter avoids FP rounding errors */
549 while (partEmitCnt--) {
550 RAY photonRay;
551
552 /* Emit photon based on PDF and trace through scene until
553 * absorbed/leaked */
554 emitPhoton(&emap, &photonRay);
555 tracePhoton(&photonRay);
556
557 /* Record progress */
558 repProgress++;
559
560 if (photonRepTime > 0 &&
561 time(NULL) >= repLastTime + photonRepTime)
562 pmapDistribReport();
563 #ifndef BSD
564 else signal(SIGCONT, pmapDistribReport);
565 #endif
566 }
567 }
568
569 portCnt++;
570 } while (portCnt < numPhotonPorts);
571 }
572
573 for (t = 0; t < NUM_PMAP_TYPES; t++)
574 if (photonMaps [t] && !photonMaps [t] -> heapEnd) {
575 /* Double preDistrib in case a photon map is empty and redo
576 * pass 1 --> possibility of infinite loop for pathological
577 * scenes (e.g. absorbing materials) */
578 preDistrib *= 2;
579 break;
580 }
581
582 if (t >= NUM_PMAP_TYPES) {
583 /* No empty photon maps found; now do pass 2 */
584 passCnt++;
585 if (photonRepTime)
586 eputs("\n");
587 }
588 } while (passCnt < 2);
589
590 /* ===================================================================
591 * POST-DISTRIBUTION - Set photon flux and build kd-tree, etc.
592 * =================================================================== */
593 signal(SIGCONT, SIG_DFL);
594 free(emap.samples);
595
596 /* Set photon flux (repProgress is total num emitted) */
597 totalFlux /= repProgress;
598
599 for (t = 0; t < NUM_PMAP_TYPES; t++)
600 if (photonMaps [t]) {
601 if (photonRepTime) {
602 sprintf(errmsg, "\nBuilding %s photon map...\n", pmapName [t]);
603 eputs(errmsg);
604 fflush(stderr);
605 }
606
607 balancePhotons(photonMaps [t], &totalFlux);
608 }
609
610 /* Precompute photon irradiance if necessary */
611 if (preCompPmap)
612 preComputeGlobal(preCompPmap);
613 }
614
615
616
617 void photonDensity (PhotonMap *pmap, RAY *ray, COLOR irrad)
618 /* Photon density estimate. Returns irradiance at ray -> rop. */
619 {
620 unsigned i;
621 PhotonSQNode *sq;
622 float r;
623 COLOR flux;
624
625 setcolor(irrad, 0, 0, 0);
626
627 if (!pmap -> maxGather)
628 return;
629
630 /* Ignore sources */
631 if (ray -> ro)
632 if (islight(objptr(ray -> ro -> omod) -> otype))
633 return;
634
635 pmap -> squeueEnd = 0;
636 findPhotons(pmap, ray);
637
638 /* Need at least 2 photons */
639 if (pmap -> squeueEnd < 2) {
640 #ifdef PMAP_NONEFOUND
641 sprintf(errmsg, "no photons found on %s at (%.3f, %.3f, %.3f)",
642 ray -> ro ? ray -> ro -> oname : "<null>",
643 ray -> rop [0], ray -> rop [1], ray -> rop [2]);
644 error(WARNING, errmsg);
645 #endif
646
647 return;
648 }
649
650 if (pmap -> minGather == pmap -> maxGather) {
651 /* No bias compensation. Just do a plain vanilla estimate */
652 sq = pmap -> squeue + 1;
653
654 /* Average radius between furthest two photons to improve accuracy */
655 r = max(sq -> dist, (sq + 1) -> dist);
656 r = 0.25 * (pmap -> maxDist + r + 2 * sqrt(pmap -> maxDist * r));
657
658 /* Skip the extra photon */
659 for (i = 1 ; i < pmap -> squeueEnd; i++, sq++) {
660 getPhotonFlux(sq -> photon, flux);
661 #ifdef PMAP_EPANECHNIKOV
662 /* Apply Epanechnikov kernel to photon flux (dists are squared) */
663 scalecolor(flux, 2 * (1 - sq -> dist / r));
664 #endif
665 addcolor(irrad, flux);
666 }
667
668 /* Divide by search area PI * r^2, 1 / PI required as ambient
669 normalisation factor */
670 scalecolor(irrad, 1 / (PI * PI * r));
671
672 return;
673 }
674 else
675 /* Apply bias compensation to density estimate */
676 biasComp(pmap, irrad);
677 }
678
679
680
681 void photonPreCompDensity (PhotonMap *pmap, RAY *r, COLOR irrad)
682 /* Returns precomputed photon density estimate at ray -> rop. */
683 {
684 Photon *p;
685
686 setcolor(irrad, 0, 0, 0);
687
688 /* Ignore sources */
689 if (r -> ro && islight(objptr(r -> ro -> omod) -> otype))
690 return;
691
692 if ((p = find1Photon(preCompPmap, r)))
693 getPhotonFlux(p, irrad);
694 }
695
696
697
698 void volumePhotonDensity (PhotonMap *pmap, RAY *ray, COLOR irrad)
699 /* Photon volume density estimate. Returns irradiance at ray -> rop. */
700 {
701 unsigned i;
702 PhotonSQNode *sq;
703 float gecc2, r, ph;
704 COLOR flux;
705
706 setcolor(irrad, 0, 0, 0);
707
708 if (!pmap -> maxGather)
709 return;
710
711 pmap -> squeueEnd = 0;
712 findPhotons(pmap, ray);
713
714 /* Need at least 2 photons */
715 if (pmap -> squeueEnd < 2)
716 return;
717
718 if (pmap -> minGather == pmap -> maxGather) {
719 /* No bias compensation. Just do a plain vanilla estimate */
720 gecc2 = ray -> gecc * ray -> gecc;
721 sq = pmap -> squeue + 1;
722
723 /* Average radius between furthest two photons to improve accuracy */
724 r = max(sq -> dist, (sq + 1) -> dist);
725 r = 0.25 * (pmap -> maxDist + r + 2 * sqrt(pmap -> maxDist * r));
726
727 /* Skip the extra photon */
728 for (i = 1 ; i < pmap -> squeueEnd; i++, sq++) {
729 /* Compute phase function for inscattering from photon */
730 if (gecc2 <= FTINY)
731 ph = 1;
732 else {
733 ph = DOT(ray -> rdir, sq -> photon -> norm) / 127;
734 ph = 1 + gecc2 - 2 * ray -> gecc * ph;
735 ph = (1 - gecc2) / (ph * sqrt(ph));
736 }
737
738 getPhotonFlux(sq -> photon, flux);
739 scalecolor(flux, ph);
740 addcolor(irrad, flux);
741 }
742
743 /* Divide by search volume 4 / 3 * PI * r^3 and phase function
744 normalization factor 1 / (4 * PI) */
745 scalecolor(irrad, 3 / (16 * PI * PI * r * sqrt(r)));
746
747 return;
748 }
749
750 else
751 /* Apply bias compensation to density estimate */
752 volumeBiasComp(pmap, ray, irrad);
753 }