ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapio.c
Revision: 2.13
Committed: Thu Feb 18 17:08:50 2021 UTC (3 years, 2 months ago) by rschregle
Content type: text/plain
Branch: MAIN
Changes since 2.12: +36 -23 lines
Log Message:
fix(mkpmap,pmapdump): Made pmap files (hopefully) more portable by
forcing numPhotons to fixed size irrespective of platform (possibly with 0-padding)

File Contents

# User Rev Content
1 greg 2.6 #ifndef lint
2 rschregle 2.13 static const char RCSid[] = "$Id: pmapio.c,v 2.12 2018/08/02 18:33:49 greg Exp $";
3 greg 2.6 #endif
4 rschregle 2.9
5 greg 2.1 /*
6 rschregle 2.13 =======================================================================
7     Photon map portable file I/O
8 greg 2.1
9     Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
10     (c) Fraunhofer Institute for Solar Energy Systems,
11 rschregle 2.13 supported by the German Research Foundation
12     (DFG LU-204/10-2, "Fassadenintegrierte Regelsysteme FARESYS")
13 rschregle 2.2 (c) Lucerne University of Applied Sciences and Arts,
14 rschregle 2.13 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 greg 2.1
19 rschregle 2.13 $Id: pmapio.c,v 2.12 2018/08/02 18:33:49 greg Exp $
20 greg 2.1 */
21    
22    
23    
24     #include "pmapio.h"
25     #include "pmapdiag.h"
26     #include "resolu.h"
27    
28    
29    
30     void savePhotonMap (const PhotonMap *pmap, const char *fname,
31 greg 2.5 int argc, char **argv)
32 greg 2.1 {
33 rschregle 2.9 unsigned long i, j;
34     FILE *file;
35 greg 2.1
36 rschregle 2.9 if (!pmap || !pmap -> numPhotons || !validPmapType(pmap -> type)) {
37 greg 2.1 error(INTERNAL, "attempt to save empty or invalid photon map");
38     return;
39     }
40    
41 rschregle 2.10 if (verbose) {
42 rschregle 2.9 if (pmap -> numPrimary)
43 rschregle 2.10 sprintf(errmsg, "Saving %s (%ld photons, %d primaries)\n",
44 rschregle 2.9 fname, pmap -> numPhotons, pmap -> numPrimary);
45 rschregle 2.10 else sprintf(errmsg, "Saving %s (%ld photons)\n", fname,
46 rschregle 2.9 pmap -> numPhotons);
47    
48 greg 2.1 eputs(errmsg);
49     fflush(stderr);
50     }
51    
52     if (!(file = fopen(fname, "wb"))) {
53     sprintf(errmsg, "can't open photon map file %s", fname);
54     error(SYSTEM, errmsg);
55     }
56    
57     /* Write header */
58     newheader("RADIANCE", file);
59    
60     /* Write command line */
61     printargs(argc, argv, file);
62    
63     /* Include statistics in info text */
64 rschregle 2.13 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 rschregle 2.9
79     if (pmap -> primaries)
80     fprintf(file, "%d primary rays\n", pmap -> numPrimary);
81 greg 2.1
82 rschregle 2.13 /* Write format, including human-readable file version */
83 greg 2.5 fputformat((char*)pmapFormat [pmap -> type], file);
84 rschregle 2.9 fprintf(file, "VERSION=%s\n", PMAP_FILEVER);
85 greg 2.1
86     /* Empty line = end of header */
87     putc('\n', file);
88    
89 rschregle 2.13 /* Write machine-readable file format version */
90 rschregle 2.9 putstr(PMAP_FILEVER, file);
91 greg 2.1
92 rschregle 2.13 /* 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 greg 2.1
98     /* Write average photon flux */
99     for (j = 0; j < 3; j++)
100     putflt(pmap -> photonFlux [j], file);
101    
102     /* Write max and min photon positions */
103     for (j = 0; j < 3; j++) {
104     putflt(pmap -> minPos [j], file);
105     putflt(pmap -> maxPos [j], file);
106     }
107    
108     /* Write centre of gravity */
109     for (j = 0; j < 3; j++)
110     putflt(pmap -> CoG [j], file);
111    
112     /* Write avg distance to centre of gravity */
113     putflt(pmap -> CoGdist, file);
114    
115     /* Write out primary photon rays (or just zero count if none) */
116 rschregle 2.9 if (pmap -> primaries) {
117     putint(pmap -> numPrimary, sizeof(pmap -> numPrimary), file);
118 greg 2.1
119 rschregle 2.9 for (i = 0; i < pmap -> numPrimary; i++) {
120     PhotonPrimary *prim = pmap -> primaries + i;
121    
122 greg 2.1 putint(prim -> srcIdx, sizeof(prim -> srcIdx), file);
123 rschregle 2.11 #ifdef PMAP_PRIMARYDIR
124 greg 2.4 putint(prim -> dir, sizeof(prim -> dir), file);
125 rschregle 2.11 #endif
126 rschregle 2.9 #ifdef PMAP_PRIMARYPOS
127 greg 2.1 for (j = 0; j < 3; j++)
128 greg 2.3 putflt(prim -> pos [j], file);
129 rschregle 2.9 #endif
130 greg 2.1 if (ferror(file))
131     error(SYSTEM, "error writing primary photon rays");
132     }
133     }
134 rschregle 2.9 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 greg 2.1
146     fclose(file);
147     }
148    
149    
150    
151     PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname)
152     {
153 rschregle 2.9 PhotonMapType ptype = PMAP_TYPE_NONE;
154 greg 2.12 char format [MAXFMTLEN];
155 rschregle 2.9 unsigned long i, j;
156     FILE *file;
157 greg 2.1
158     if (!pmap)
159     return PMAP_TYPE_NONE;
160    
161     if ((file = fopen(fname, "rb")) == NULL) {
162     sprintf(errmsg, "can't open photon map file %s", fname);
163     error(SYSTEM, errmsg);
164     }
165    
166     /* Get format string */
167     strcpy(format, PMAP_FORMAT_GLOB);
168     if (checkheader(file, format, NULL) != 1) {
169     sprintf(errmsg, "photon map file %s has unknown format %s",
170     fname, format);
171     error(USER, errmsg);
172     }
173    
174     /* Identify photon map type from format string */
175     for (ptype = 0;
176 rschregle 2.8 ptype < NUM_PMAP_TYPES && strcmp(pmapFormat [ptype], format);
177 greg 2.1 ptype++);
178    
179     if (!validPmapType(ptype)) {
180     sprintf(errmsg, "file %s contains an unknown photon map type", fname);
181     error(USER, errmsg);
182     }
183    
184     initPhotonMap(pmap, ptype);
185    
186     /* Get file format version and check for compatibility */
187 rschregle 2.9 if (strcmp(getstr(format, file), PMAP_FILEVER))
188 greg 2.1 error(USER, "incompatible photon map file format");
189    
190 rschregle 2.13 /* 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 greg 2.1
196     /* Get average photon flux */
197     for (j = 0; j < 3; j++)
198     pmap -> photonFlux [j] = getflt(file);
199    
200     /* Get max and min photon positions */
201     for (j = 0; j < 3; j++) {
202     pmap -> minPos [j] = getflt(file);
203     pmap -> maxPos [j] = getflt(file);
204     }
205    
206     /* Get centre of gravity */
207     for (j = 0; j < 3; j++)
208     pmap -> CoG [j] = getflt(file);
209    
210     /* Get avg distance to centre of gravity */
211     pmap -> CoGdist = getflt(file);
212    
213     /* Get primary photon rays */
214 rschregle 2.9 pmap -> numPrimary = getint(sizeof(pmap -> numPrimary), file);
215 greg 2.1
216 rschregle 2.9 if (pmap -> numPrimary) {
217     pmap -> primaries = calloc(pmap -> numPrimary, sizeof(PhotonPrimary));
218     if (!pmap -> primaries)
219 greg 2.1 error(INTERNAL, "can't allocate primary photon rays");
220    
221 rschregle 2.9 for (i = 0; i < pmap -> numPrimary; i++) {
222     PhotonPrimary *prim = pmap -> primaries + i;
223 greg 2.1
224     prim -> srcIdx = getint(sizeof(prim -> srcIdx), file);
225 rschregle 2.11 #ifdef PMAP_PRIMARYDIR
226 greg 2.4 prim -> dir = getint(sizeof(prim -> dir), file);
227 rschregle 2.11 #endif
228 rschregle 2.9 #ifdef PMAP_PRIMARYPOS
229 greg 2.1 for (j = 0; j < 3; j++)
230 greg 2.3 prim -> pos [j] = getflt(file);
231 rschregle 2.9 #endif
232 greg 2.1 if (feof(file))
233     error(SYSTEM, "error reading primary photon rays");
234     }
235 rschregle 2.9 }
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 greg 2.1 }
246 rschregle 2.9
247 greg 2.1 fclose(file);
248     return ptype;
249     }