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.8 by rschregle, Tue Oct 20 15:51:54 2015 UTC vs.
Revision 2.9 by rschregle, Tue May 17 17:39:47 2016 UTC

# Line 1 | Line 1
1   #ifndef lint
2   static const char RCSid[] = "$Id$";
3   #endif
4 +
5   /*
6 <   ==================================================================
6 >   ======================================================================
7     Photon map file I/O
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 24 | Line 26 | static const char RCSid[] = "$Id$";
26   void savePhotonMap (const PhotonMap *pmap, const char *fname,
27                      int argc, char **argv)
28   {
29 <   unsigned long i, j;
30 <   const Photon* p;
29 <   FILE* file;
29 >   unsigned long  i, j;
30 >   FILE           *file;
31  
32 <   if (!pmap || !pmap -> heap || !pmap -> heapSize ||
32 <       !validPmapType(pmap -> type)) {
32 >   if (!pmap || !pmap -> numPhotons || !validPmapType(pmap -> type)) {
33        error(INTERNAL, "attempt to save empty or invalid photon map");
34        return;
35     }
36        
37     if (photonRepTime) {
38 <      sprintf(errmsg, "Saving %s (%ld photons)...\n",
39 <              fname, pmap -> heapSize);
38 >      if (pmap -> numPrimary)
39 >         sprintf(errmsg, "Saving %s (%ld photons, %d primaries)...\n",
40 >                 fname, pmap -> numPhotons, pmap -> numPrimary);
41 >      else sprintf(errmsg, "Saving %s (%ld photons)...\n", fname,
42 >                   pmap -> numPhotons);
43 >                  
44        eputs(errmsg);
45        fflush(stderr);
46     }
# Line 53 | Line 57 | void savePhotonMap (const PhotonMap *pmap, const char
57     printargs(argc, argv, file);
58    
59     /* Include statistics in info text */
60 <   fprintf(file, "%ld photons @ (%.2e, %.2e, %.2e) avg watts\n"
61 <           "Extent [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n",
62 <           pmap -> heapSize, pmap -> photonFlux [0],
60 >   fprintf(file, "NumPhotons\t= %ld\n"
61 >           "AvgFlux\t\t= [%.2e, %.2e, %.2e]\n"
62 >           "Bbox\t\t= [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n"
63 >           "CoG\t\t= [%.3f, %.3f, %.3f]\n"
64 >           "MaxDist^2\t= %.3f\n",
65 >           pmap -> numPhotons, pmap -> photonFlux [0],
66             pmap -> photonFlux [1], pmap -> photonFlux [2],
67             pmap -> minPos [0], pmap -> minPos [1], pmap -> minPos [2],
68 <           pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2]);
69 <   if (pmap -> primary)
70 <      fprintf(file, "%d primary rays\n", pmap -> primaryEnd + 1);
68 >           pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2],
69 >           pmap -> CoG [0], pmap -> CoG [1], pmap -> CoG [2],
70 >           pmap -> CoGdist);
71 >          
72 >   if (pmap -> primaries)
73 >      fprintf(file, "%d primary rays\n", pmap -> numPrimary);
74    
75     /* Write format */
76     fputformat((char*)pmapFormat [pmap -> type], file);
77 <   fprintf(file, "VERSION=%d\n", PMAP_FILEVER);
77 >   fprintf(file, "VERSION=%s\n", PMAP_FILEVER);
78    
79     /* Empty line = end of header */
80     putc('\n', file);
81    
82     /* Write file format version */
83 <   putint(PMAP_FILEVER, sizeof(PMAP_FILEVER), file);
83 >   putstr(PMAP_FILEVER, file);
84    
85     /* Write number of photons */
86 <   putint(pmap -> heapSize, sizeof(pmap -> heapSize), file);
86 >   putint(pmap -> numPhotons, sizeof(pmap -> numPhotons), file);
87    
88     /* Write average photon flux */
89     for (j = 0; j < 3; j++)
# Line 91 | Line 101 | void savePhotonMap (const PhotonMap *pmap, const char
101        
102     /* Write avg distance to centre of gravity */
103     putflt(pmap -> CoGdist, file);
94        
95   for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) {
96      /* Write photon attributes */
97      for (j = 0; j < 3; j++)
98         putflt(p -> pos [j], file);
99        
100      /* Bytewise dump otherwise we have portability probs */
101      for (j = 0; j < 3; j++)
102         putint(p -> norm [j], 1, file);
103        
104      #ifdef PMAP_FLOAT_FLUX
105         for (j = 0; j < 3; j++)
106            putflt(p -> flux [j], file);
107      #else
108         for (j = 0; j < 4; j++)
109            putint(p -> flux [j], 1, file);
110      #endif
111
112      putint(p -> primary, sizeof(p -> primary), file);
113      putint(p -> flags, 1, file);
114            
115      if (ferror(file)) {
116         sprintf(errmsg, "error writing photon map file %s", fname);
117         error(SYSTEM, errmsg);
118      }
119   }
104    
105     /* Write out primary photon rays (or just zero count if none) */
106 <   if (pmap -> primary) {
107 <      /* primaryEnd points to last primary ray in array, so increment for
124 <       * number of entries */
125 <      putint(pmap -> primaryEnd + 1, sizeof(pmap -> primaryEnd), file);
106 >   if (pmap -> primaries) {
107 >      putint(pmap -> numPrimary, sizeof(pmap -> numPrimary), file);
108        
109 <      for (i = 0; i <= pmap -> primaryEnd; i++) {
110 <         PhotonPrimary *prim = pmap -> primary + i;      
129 <        
130 <         putint(prim -> srcIdx, sizeof(prim -> srcIdx), file);
109 >      for (i = 0; i < pmap -> numPrimary; i++) {
110 >         PhotonPrimary *prim = pmap -> primaries + i;      
111  
112 +         putint(prim -> srcIdx, sizeof(prim -> srcIdx), file);
113           putint(prim -> dir, sizeof(prim -> dir), file);
114 <            
114 > #ifdef PMAP_PRIMARYPOS        
115           for (j = 0; j < 3; j++)
116              putflt(prim -> pos [j], file);
117 <        
117 > #endif
118           if (ferror(file))
119              error(SYSTEM, "error writing primary photon rays");
120        }
121     }
122 <   else putint(0, sizeof(pmap -> primaryEnd), file);
122 >   else putint(0, sizeof(pmap -> numPrimary), file);
123 >  
124 >   /* Save photon storage */
125 > #ifdef PMAP_OOC
126 >   if (OOC_SavePhotons(pmap, file)) {
127 > #else
128 >   if (kdT_SavePhotons(pmap, file)) {
129 > #endif
130 >      sprintf(errmsg, "error writing photon map file %s", fname);
131 >      error(SYSTEM, errmsg);
132 >   }
133  
134     fclose(file);
135   }
# Line 147 | Line 138 | void savePhotonMap (const PhotonMap *pmap, const char
138  
139   PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname)
140   {
141 <   Photon* p;
142 <   PhotonMapType ptype = PMAP_TYPE_NONE;
143 <   char format [128];
144 <   unsigned long i, j;
154 <   FILE *file;
141 >   PhotonMapType  ptype = PMAP_TYPE_NONE;
142 >   char           format [128];
143 >   unsigned long  i, j;
144 >   FILE           *file;
145  
146     if (!pmap)
147        return PMAP_TYPE_NONE;
# Line 182 | Line 172 | PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch
172     initPhotonMap(pmap, ptype);
173    
174     /* Get file format version and check for compatibility */
175 <   if (getint(sizeof(PMAP_FILEVER), file) != PMAP_FILEVER)
175 >   if (strcmp(getstr(format, file), PMAP_FILEVER))
176        error(USER, "incompatible photon map file format");
177        
178     /* Get number of photons */
179 <   pmap -> heapSize = pmap -> heapEnd =
190 <      getint(sizeof(pmap -> heapSize), file);
191 <   pmap -> heap = (Photon *)malloc(pmap -> heapSize * sizeof(Photon));
192 <   if (!pmap -> heap)
193 <      error(INTERNAL, "can't allocate photon heap from file");
179 >   pmap -> numPhotons = getint(sizeof(pmap -> numPhotons), file);
180        
181     /* Get average photon flux */
182     for (j = 0; j < 3; j++)
# Line 208 | Line 194 | PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch
194        
195     /* Get avg distance to centre of gravity */
196     pmap -> CoGdist = getflt(file);
211      
212   /* Get photon attributes */  
213   for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) {
214      for (j = 0; j < 3; j++)
215         p -> pos [j] = getflt(file);
216        
217      /* Bytewise grab otherwise we have portability probs */
218      for (j = 0; j < 3; j++)
219         p -> norm [j] = getint(1, file);
220        
221      #ifdef PMAP_FLOAT_FLUX
222         for (j = 0; j < 3; j++)
223            p -> flux [j] = getflt(file);
224      #else      
225         for (j = 0; j < 4; j++)
226            p -> flux [j] = getint(1, file);
227      #endif
197  
229      p -> primary = getint(sizeof(p -> primary), file);
230      p -> flags = getint(1, file);
231      
232      if (feof(file)) {
233         sprintf(errmsg, "error reading photon map file %s", fname);
234         error(SYSTEM, errmsg);
235      }
236   }
237      
198     /* Get primary photon rays */
199 <   pmap -> primarySize = getint(sizeof(pmap -> primarySize), file);
199 >   pmap -> numPrimary = getint(sizeof(pmap -> numPrimary), file);
200    
201 <   if (pmap -> primarySize) {
202 <      pmap -> primaryEnd = pmap -> primarySize - 1;
203 <      pmap -> primary = (PhotonPrimary*)malloc(pmap -> primarySize *
244 <                                               sizeof(PhotonPrimary));
245 <      if (!pmap -> primary)
201 >   if (pmap -> numPrimary) {
202 >      pmap -> primaries = calloc(pmap -> numPrimary, sizeof(PhotonPrimary));      
203 >      if (!pmap -> primaries)
204           error(INTERNAL, "can't allocate primary photon rays");
205          
206 <      for (i = 0; i < pmap -> primarySize; i++) {
207 <         PhotonPrimary *prim = pmap -> primary + i;
206 >      for (i = 0; i < pmap -> numPrimary; i++) {
207 >         PhotonPrimary *prim = pmap -> primaries + i;
208          
209           prim -> srcIdx = getint(sizeof(prim -> srcIdx), file);
210          
211           prim -> dir = getint(sizeof(prim -> dir), file);
212 <            
212 > #ifdef PMAP_PRIMARYPOS            
213           for (j = 0; j < 3; j++)
214              prim -> pos [j] = getflt(file);
215 <            
215 > #endif            
216           if (feof(file))
217              error(SYSTEM, "error reading primary photon rays");
218        }
219 <   }
219 >   }      
220    
221 +   /* Load photon storage */
222 + #ifdef PMAP_OOC
223 +   if (OOC_LoadPhotons(pmap, file)) {
224 + #else
225 +   if (kdT_LoadPhotons(pmap, file)) {
226 + #endif
227 +      sprintf(errmsg, "error reading photon map file %s", fname);
228 +      error(SYSTEM, errmsg);
229 +   }
230 +
231     fclose(file);
264  
232     return ptype;
233   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines