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

Comparing ray/src/rt/pmapdump.c (file contents):
Revision 2.6 by rschregle, Thu Feb 4 19:39:10 2016 UTC vs.
Revision 2.9 by rschregle, Sat Dec 9 18:40:43 2017 UTC

# Line 1 | Line 1
1   #ifndef lint
2   static const char RCSid[] = "$Id$";
3   #endif
4 +
5   /*
6 <   ==================================================================
6 >   ======================================================================
7     Dump photon maps as RADIANCE scene description to stdout
8  
9     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
10     (c) Fraunhofer Institute for Solar Energy Systems,
11     (c) Lucerne University of Applied Sciences and Arts,
12 <   supported by the Swiss National Science Foundation (SNSF, #147053)
13 <   ==================================================================
12 >       supported by the Swiss National Science Foundation (SNSF, #147053)
13 >   ======================================================================
14    
15 +   $Id$
16   */
17  
18  
# Line 23 | Line 25 | static const char RCSid[] = "$Id$";
25   #include "random.h"
26   #include "math.h"
27  
28 + #define PMAPDUMP_REC "$Revision$"  
29  
30 +
31   /* Defaults */
32   /*    Sphere radius as fraction of avg. intersphere dist */
33   /*    Relative scale for sphere radius (fudge factor) */
# Line 39 | Line 43 | typedef struct {
43   } RadianceDef;
44  
45    
42 static char header [] = "$Revision$";
46  
44
47   /* Colour code is as follows:    global         = blue
48                                   precomp global = cyan
49                                   caustic        = red
# Line 73 | Line 75 | const RadianceDef radDefs [] = {
75  
76   int main (int argc, char** argv)
77   {
78 <   char format [128];
79 <   RREAL rad, radScale = RADSCALE, vol, dumpRatio;
80 <   FVECT minPos, maxPos;
81 <   unsigned arg, j, ptype;
82 <   long numPhotons, numSpheres = NSPHERES;
83 <   FILE *pmapFile;
84 <   Photon p;
78 >   char           format [128];
79 >   RREAL          rad, radScale = RADSCALE, extent, dumpRatio;
80 >   unsigned       arg, j, ptype, dim;
81 >   long           numSpheres = NSPHERES;
82 >   FILE           *pmapFile;
83 >   PhotonMap      pm;
84 >   PhotonPrimary  pri;
85 >   Photon         p;
86 > #ifdef PMAP_OOC
87 >   char           leafFname [1024];
88 > #endif
89    
90     if (argc < 2) {
91        puts("Dump photon maps as RADIANCE scene description\n");
# Line 137 | Line 143 | int main (int argc, char** argv)
143        }
144  
145        /* Get file format version and check for compatibility */
146 <      if (getint(sizeof(PMAP_FILEVER), pmapFile) != PMAP_FILEVER)
146 >      if (strcmp(getstr(format, pmapFile), PMAP_FILEVER))      
147           error(USER, "incompatible photon map file format");
148          
149        /* Dump command line as comment */
# Line 149 | Line 155 | int main (int argc, char** argv)
155        fputs(radDefs [ptype].mat, stdout);
156        fputc('\n', stdout);
157        
158 <      /* Get number of photons (is this sizeof() hack portable?) */
159 <      numPhotons = getint(sizeof(((PhotonMap*)NULL) -> heapSize), pmapFile);
158 >      /* Get number of photons */
159 >      pm.numPhotons = getint(sizeof(pm.numPhotons), pmapFile);
160        
161        /* Skip avg photon flux */
162        for (j = 0; j < 3; j++)
# Line 158 | Line 164 | int main (int argc, char** argv)
164        
165        /* Get distribution extent (min & max photon positions) */
166        for (j = 0; j < 3; j++) {
167 <         minPos [j] = getflt(pmapFile);
168 <         maxPos [j] = getflt(pmapFile);
167 >         pm.minPos [j] = getflt(pmapFile);
168 >         pm.maxPos [j] = getflt(pmapFile);
169        }
170        
171        /* Skip centre of gravity, and avg photon dist to it */
172        for (j = 0; j < 4; j++)
173           getflt(pmapFile);
174        
175 <      /* Sphere radius based on avg intersphere dist
176 <         (= sphere distrib density ^-1/3) */
177 <      vol = (maxPos [0] - minPos [0]) * (maxPos [1] - minPos [1]) *
178 <            (maxPos [2] - minPos [2]);
179 <      rad = radScale * RADCOEFF * pow(vol / numSpheres, 1./3.);
175 >      /* Sphere radius based on avg intersphere dist depending on
176 >         whether the distribution occupies a 1D line (!), a 2D plane,
177 >         or 3D volume (= sphere distrib density ^-1/d, where d is the
178 >         dimensionality of the distribution) */
179 >      for (j = 0, extent = 1.0, dim = 0; j < 3; j++) {
180 >         rad = pm.maxPos [j] - pm.minPos [j];
181 >        
182 >         if (rad > FTINY) {
183 >            dim++;
184 >            extent *= rad;
185 >         }
186 >      }
187 >
188 >      rad = radScale * RADCOEFF * pow(extent / numSpheres, 1./dim);
189        
190        /* Photon dump probability to satisfy target sphere count */
191 <      dumpRatio = numSpheres < numPhotons ? (float)numSpheres / numPhotons
192 <                                          : 1;
191 >      dumpRatio = numSpheres < pm.numPhotons
192 >                  ? (float)numSpheres / pm.numPhotons : 1;
193        
194 <      while (numPhotons-- > 0) {
194 >      /* Skip primary rays */
195 >      pm.numPrimary = getint(sizeof(pm.numPrimary), pmapFile);
196 >      while (pm.numPrimary-- > 0) {
197 >         /* Skip source index & incident dir */
198 >         getint(sizeof(pri.srcIdx) + sizeof(pri.dir), pmapFile);
199 > #ifdef PMAP_PRIMARYPOS        
200 >         /* Skip primary hitpoint */
201 >         for (j = 0; j < 3; j++)
202 >            getflt(pmapFile);
203 > #endif
204 >      }
205 >
206 > #ifdef PMAP_OOC
207 >      /* Open leaf file with filename derived from pmap, replace pmapFile
208 >       * (which is currently the node file) */
209 >      strncpy(leafFname, argv [arg], 1024);
210 >      strncat(leafFname, PMAP_OOC_LEAFSUFFIX, 1024);
211 >      fclose(pmapFile);
212 >      if (!(pmapFile = fopen(leafFname, "rb"))) {
213 >         sprintf(errmsg, "cannot open leaf file %s", leafFname);
214 >         error(SYSTEM, errmsg);
215 >      }
216 > #endif
217 >            
218 >      /* Load photons */      
219 >      while (pm.numPhotons-- > 0) {
220 > #ifdef PMAP_OOC
221 >         /* Get entire photon record
222 >            !!! OOC PMAP FILES CURRENTLY DON'T USE PORTABLE I/O !!! */
223 >         if (!fread(&p, sizeof(p), 1, pmapFile)) {
224 >            sprintf(errmsg, "error reading OOC leaf file %s", leafFname);
225 >            error(SYSTEM, errmsg);
226 >         }
227 > #else        
228           /* Get photon position */            
229           for (j = 0; j < 3; j++)
230              p.pos [j] = getflt(pmapFile);
231 <
231 > #endif
232           /* Dump photon probabilistically acc. to target sphere count */
233           if (frandom() <= dumpRatio) {
234              printf(radDefs [ptype].obj, p.pos [0], p.pos [1], p.pos [2], rad);
235              fputc('\n', stdout);
236           }
237          
238 + #ifndef PMAP_OOC
239           /* Skip photon normal and flux */
240           for (j = 0; j < 3; j++)
241              getint(sizeof(p.norm [j]), pmapFile);
242              
243 <         #ifdef PMAP_FLOAT_FLUX
244 <            for (j = 0; j < 3; j++)
245 <               getflt(pmapFile);
246 <         #else      
247 <            for (j = 0; j < 4; j++)
248 <               getint(1, pmapFile);
249 <         #endif
243 > #ifdef PMAP_FLOAT_FLUX
244 >         for (j = 0; j < 3; j++)
245 >            getflt(pmapFile);
246 > #else      
247 >         for (j = 0; j < 4; j++)
248 >            getint(1, pmapFile);
249 > #endif
250  
251           /* Skip primary ray index */
252           getint(sizeof(p.primary), pmapFile);
253  
254           /* Skip flags */
255           getint(sizeof(p.flags), pmapFile);
256 + #endif
257          
258 <         if (feof(pmapFile)) {
258 >         if (ferror(pmapFile) || feof(pmapFile)) {
259              sprintf(errmsg, "error reading %s", argv [arg]);
260              error(USER, errmsg);
261           }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines