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

Comparing ray/src/rt/pmapio.c (file contents):
Revision 2.6 by greg, Tue Aug 18 18:45:55 2015 UTC vs.
Revision 2.13 by rschregle, Thu Feb 18 17:08:50 2021 UTC

# Line 1 | Line 1
1   #ifndef lint
2   static const char RCSid[] = "$Id$";
3   #endif
4 +
5   /*
6 <   ==================================================================
7 <   Photon map file I/O
6 >   =======================================================================
7 >   Photon map portable file I/O
8  
9     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
10     (c) Fraunhofer Institute for Solar Energy Systems,
11 +       supported by the German Research Foundation
12 +       (DFG LU-204/10-2, "Fassadenintegrierte Regelsysteme FARESYS")
13     (c) Lucerne University of Applied Sciences and Arts,
14 <   supported by the Swiss National Science Foundation (SNSF, #147053)
15 <   ==================================================================
14 >       supported by the Swiss National Science Foundation
15 >       (SNSF #147053, "Daylight Redirecting Components",
16 >        SNSF #179067, "Light Fields for Spatio-Temporal Glare Assessment")
17 >   =======================================================================
18    
19     $Id$    
20   */
# Line 25 | Line 30 | static const char RCSid[] = "$Id$";
30   void savePhotonMap (const PhotonMap *pmap, const char *fname,
31                      int argc, char **argv)
32   {
33 <   unsigned long i, j;
34 <   const Photon* p;
30 <   FILE* file;
33 >   unsigned long  i, j;
34 >   FILE           *file;
35  
36 <   if (!pmap || !pmap -> heap || !pmap -> heapSize ||
33 <       !validPmapType(pmap -> type)) {
36 >   if (!pmap || !pmap -> numPhotons || !validPmapType(pmap -> type)) {
37        error(INTERNAL, "attempt to save empty or invalid photon map");
38        return;
39     }
40        
41 <   if (photonRepTime) {
42 <      sprintf(errmsg, "Saving %s (%ld photons)...\n",
43 <              fname, pmap -> heapSize);
41 >   if (verbose) {
42 >      if (pmap -> numPrimary)
43 >         sprintf(errmsg, "Saving %s (%ld photons, %d primaries)\n",
44 >                 fname, pmap -> numPhotons, pmap -> numPrimary);
45 >      else sprintf(errmsg, "Saving %s (%ld photons)\n", fname,
46 >                   pmap -> numPhotons);
47 >                  
48        eputs(errmsg);
49        fflush(stderr);
50     }
# Line 54 | Line 61 | void savePhotonMap (const PhotonMap *pmap, const char
61     printargs(argc, argv, file);
62    
63     /* Include statistics in info text */
64 <   fprintf(file, "%ld photons @ (%.2e, %.2e, %.2e) avg watts\n"
65 <           "Extent [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n",
66 <           pmap -> heapSize, pmap -> photonFlux [0],
67 <           pmap -> photonFlux [1], pmap -> photonFlux [2],
68 <           pmap -> minPos [0], pmap -> minPos [1], pmap -> minPos [2],
69 <           pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2]);
70 <   if (pmap -> primary)
71 <      fprintf(file, "%d primary rays\n", pmap -> primaryEnd + 1);
64 >   fprintf(
65 >      file,
66 >      "NumPhotons\t= %ld\n"
67 >      "AvgFlux\t\t= [%.2e, %.2e, %.2e]\n"
68 >      "Bbox\t\t= [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n"
69 >      "CoG\t\t= [%.3f, %.3f, %.3f]\n"
70 >      "MaxDist^2\t= %.3f\n",
71 >      pmap -> numPhotons,
72 >      pmap -> photonFlux [0], pmap -> photonFlux [1], pmap -> photonFlux [2],
73 >      pmap -> minPos [0], pmap -> minPos [1], pmap -> minPos [2],
74 >      pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2],
75 >      pmap -> CoG [0], pmap -> CoG [1], pmap -> CoG [2],
76 >      pmap -> CoGdist
77 >   );
78 >          
79 >   if (pmap -> primaries)
80 >      fprintf(file, "%d primary rays\n", pmap -> numPrimary);
81    
82 <   /* Write format */
82 >   /* Write format, including human-readable file version */
83     fputformat((char*)pmapFormat [pmap -> type], file);
84 <   fprintf(file, "VERSION=%d\n", PMAP_FILEVER);
84 >   fprintf(file, "VERSION=%s\n", PMAP_FILEVER);
85    
86     /* Empty line = end of header */
87     putc('\n', file);
88    
89 <   /* Write file format version */
90 <   putint(PMAP_FILEVER, sizeof(PMAP_FILEVER), file);
89 >   /* Write machine-readable file format version */
90 >   putstr(PMAP_FILEVER, file);
91    
92 <   /* Write number of photons */
93 <   putint(pmap -> heapSize, sizeof(pmap -> heapSize), file);
92 >   /* Write number of photons as fixed size, which possibly results in
93 >    * padding of MSB with 0 on some platforms.  Unlike sizeof() however,
94 >    * this ensures portability since this value may span 32 or 64 bits
95 >    * depending on platform.  */
96 >   putint(pmap -> numPhotons, PMAP_LONGSIZE, file);
97    
98     /* Write average photon flux */
99     for (j = 0; j < 3; j++)
# Line 92 | Line 111 | void savePhotonMap (const PhotonMap *pmap, const char
111        
112     /* Write avg distance to centre of gravity */
113     putflt(pmap -> CoGdist, file);
95        
96   for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) {
97      /* Write photon attributes */
98      for (j = 0; j < 3; j++)
99         putflt(p -> pos [j], file);
100        
101      /* Bytewise dump otherwise we have portability probs */
102      for (j = 0; j < 3; j++)
103         putint(p -> norm [j], 1, file);
104        
105      #ifdef PMAP_FLOAT_FLUX
106         for (j = 0; j < 3; j++)
107            putflt(p -> flux [j], file);
108      #else
109         for (j = 0; j < 4; j++)
110            putint(p -> flux [j], 1, file);
111      #endif
112
113      putint(p -> primary, sizeof(p -> primary), file);
114      putint(p -> flags, 1, file);
115            
116      if (ferror(file)) {
117         sprintf(errmsg, "error writing photon map file %s", fname);
118         error(SYSTEM, errmsg);
119      }
120   }
114    
115     /* Write out primary photon rays (or just zero count if none) */
116 <   if (pmap -> primary) {
117 <      /* primaryEnd points to last primary ray in array, so increment for
125 <       * number of entries */
126 <      putint(pmap -> primaryEnd + 1, sizeof(pmap -> primaryEnd), file);
116 >   if (pmap -> primaries) {
117 >      putint(pmap -> numPrimary, sizeof(pmap -> numPrimary), file);
118        
119 <      for (i = 0; i <= pmap -> primaryEnd; i++) {
120 <         PhotonPrimary *prim = pmap -> primary + i;      
130 <        
131 <         putint(prim -> srcIdx, sizeof(prim -> srcIdx), file);
119 >      for (i = 0; i < pmap -> numPrimary; i++) {
120 >         PhotonPrimary *prim = pmap -> primaries + i;      
121  
122 +         putint(prim -> srcIdx, sizeof(prim -> srcIdx), file);
123 + #ifdef PMAP_PRIMARYDIR        
124           putint(prim -> dir, sizeof(prim -> dir), file);
125 <            
125 > #endif        
126 > #ifdef PMAP_PRIMARYPOS        
127           for (j = 0; j < 3; j++)
128              putflt(prim -> pos [j], file);
129 <        
129 > #endif
130           if (ferror(file))
131              error(SYSTEM, "error writing primary photon rays");
132        }
133     }
134 <   else putint(0, sizeof(pmap -> primaryEnd), file);
134 >   else putint(0, sizeof(pmap -> numPrimary), file);
135 >  
136 >   /* Save photon storage */
137 > #ifdef PMAP_OOC
138 >   if (OOC_SavePhotons(pmap, file)) {
139 > #else
140 >   if (kdT_SavePhotons(pmap, file)) {
141 > #endif
142 >      sprintf(errmsg, "error writing photon map file %s", fname);
143 >      error(SYSTEM, errmsg);
144 >   }
145  
146     fclose(file);
147   }
# Line 148 | Line 150 | void savePhotonMap (const PhotonMap *pmap, const char
150  
151   PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname)
152   {
153 <   Photon* p;
154 <   PhotonMapType ptype = PMAP_TYPE_NONE;
155 <   char format [128];
156 <   unsigned long i, j;
155 <   FILE *file;
153 >   PhotonMapType  ptype = PMAP_TYPE_NONE;
154 >   char           format [MAXFMTLEN];
155 >   unsigned long  i, j;
156 >   FILE           *file;
157  
158     if (!pmap)
159        return PMAP_TYPE_NONE;
# Line 172 | Line 173 | PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch
173    
174     /* Identify photon map type from format string */
175     for (ptype = 0;
176 <        strcmp(pmapFormat [ptype], format) && ptype < NUM_PMAP_TYPES;
176 >        ptype < NUM_PMAP_TYPES && strcmp(pmapFormat [ptype], format);
177          ptype++);
178        
179     if (!validPmapType(ptype)) {
# Line 183 | Line 184 | PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch
184     initPhotonMap(pmap, ptype);
185    
186     /* Get file format version and check for compatibility */
187 <   if (getint(sizeof(PMAP_FILEVER), file) != PMAP_FILEVER)
187 >   if (strcmp(getstr(format, file), PMAP_FILEVER))
188        error(USER, "incompatible photon map file format");
189        
190 <   /* Get number of photons */
191 <   pmap -> heapSize = pmap -> heapEnd =
192 <      getint(sizeof(pmap -> heapSize), file);
193 <   pmap -> heap = (Photon *)malloc(pmap -> heapSize * sizeof(Photon));
194 <   if (!pmap -> heap)
194 <      error(INTERNAL, "can't allocate photon heap from file");
190 >   /* Get number of photons as fixed size, which possibly results in
191 >    * padding of MSB with 0 on some platforms.  Unlike sizeof() however,
192 >    * this ensures portability since this value may span 32 or 64 bits
193 >    * depending on platform.  */
194 >   pmap -> numPhotons = getint(PMAP_LONGSIZE, file);
195        
196     /* Get average photon flux */
197     for (j = 0; j < 3; j++)
# Line 209 | Line 209 | PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch
209        
210     /* Get avg distance to centre of gravity */
211     pmap -> CoGdist = getflt(file);
212      
213   /* Get photon attributes */  
214   for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) {
215      for (j = 0; j < 3; j++)
216         p -> pos [j] = getflt(file);
217        
218      /* Bytewise grab otherwise we have portability probs */
219      for (j = 0; j < 3; j++)
220         p -> norm [j] = getint(1, file);
221        
222      #ifdef PMAP_FLOAT_FLUX
223         for (j = 0; j < 3; j++)
224            p -> flux [j] = getflt(file);
225      #else      
226         for (j = 0; j < 4; j++)
227            p -> flux [j] = getint(1, file);
228      #endif
212  
230      p -> primary = getint(sizeof(p -> primary), file);
231      p -> flags = getint(1, file);
232      
233      if (feof(file)) {
234         sprintf(errmsg, "error reading photon map file %s", fname);
235         error(SYSTEM, errmsg);
236      }
237   }
238      
213     /* Get primary photon rays */
214 <   pmap -> primarySize = getint(sizeof(pmap -> primarySize), file);
214 >   pmap -> numPrimary = getint(sizeof(pmap -> numPrimary), file);
215    
216 <   if (pmap -> primarySize) {
217 <      pmap -> primaryEnd = pmap -> primarySize - 1;
218 <      pmap -> primary = (PhotonPrimary*)malloc(pmap -> primarySize *
245 <                                               sizeof(PhotonPrimary));
246 <      if (!pmap -> primary)
216 >   if (pmap -> numPrimary) {
217 >      pmap -> primaries = calloc(pmap -> numPrimary, sizeof(PhotonPrimary));      
218 >      if (!pmap -> primaries)
219           error(INTERNAL, "can't allocate primary photon rays");
220          
221 <      for (i = 0; i < pmap -> primarySize; i++) {
222 <         PhotonPrimary *prim = pmap -> primary + i;
221 >      for (i = 0; i < pmap -> numPrimary; i++) {
222 >         PhotonPrimary *prim = pmap -> primaries + i;
223          
224           prim -> srcIdx = getint(sizeof(prim -> srcIdx), file);
225 <        
225 > #ifdef PMAP_PRIMARYDIR        
226           prim -> dir = getint(sizeof(prim -> dir), file);
227 <            
227 > #endif        
228 > #ifdef PMAP_PRIMARYPOS            
229           for (j = 0; j < 3; j++)
230              prim -> pos [j] = getflt(file);
231 <            
231 > #endif            
232           if (feof(file))
233              error(SYSTEM, "error reading primary photon rays");
234        }
235 <   }
235 >   }      
236    
237 +   /* Load photon storage */
238 + #ifdef PMAP_OOC
239 +   if (OOC_LoadPhotons(pmap, file)) {
240 + #else
241 +   if (kdT_LoadPhotons(pmap, file)) {
242 + #endif
243 +      sprintf(errmsg, "error reading photon map file %s", fname);
244 +      error(SYSTEM, errmsg);
245 +   }
246 +
247     fclose(file);
265  
248     return ptype;
249   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines