ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/pmapio.c
Revision: 2.14
Committed: Thu Mar 3 03:55:13 2022 UTC (2 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.13: +3 -3 lines
Log Message:
feat: Added float and double alignment to i/o routines for future mmap() use

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: pmapio.c,v 2.13 2021/02/18 17:08:50 rschregle Exp $";
3 #endif
4
5 /*
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
15 (SNSF #147053, "Daylight Redirecting Components",
16 SNSF #179067, "Light Fields for Spatio-Temporal Glare Assessment")
17 =======================================================================
18
19 $Id: pmapio.c,v 2.13 2021/02/18 17:08:50 rschregle Exp $
20 */
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 int argc, char **argv)
32 {
33 unsigned long i, j;
34 FILE *file;
35
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 (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 }
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 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, including human-readable file version */
83 fputformat(pmapFormat [pmap -> type], file);
84 fprintf(file, "VERSION=%s\n", PMAP_FILEVER);
85
86 /* Empty line = end of header */
87 putc('\n', file);
88
89 /* Write machine-readable file format version */
90 putstr(PMAP_FILEVER, file);
91
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++)
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 if (pmap -> primaries) {
117 putint(pmap -> numPrimary, sizeof(pmap -> numPrimary), file);
118
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 #endif
126 #ifdef PMAP_PRIMARYPOS
127 for (j = 0; j < 3; j++)
128 putflt(prim -> pos [j], file);
129 #endif
130 if (ferror(file))
131 error(SYSTEM, "error writing primary photon rays");
132 }
133 }
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 }
148
149
150
151 PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname)
152 {
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;
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 ptype < NUM_PMAP_TYPES && strcmp(pmapFormat [ptype], format);
177 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 if (strcmp(getstr(format, file), PMAP_FILEVER))
188 error(USER, "incompatible photon map file format");
189
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++)
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 pmap -> numPrimary = getint(sizeof(pmap -> numPrimary), file);
215
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 -> numPrimary; i++) {
222 PhotonPrimary *prim = pmap -> primaries + i;
223
224 prim -> srcIdx = getint(sizeof(prim -> srcIdx), file);
225 #ifdef PMAP_PRIMARYDIR
226 prim -> dir = getint(sizeof(prim -> dir), file);
227 #endif
228 #ifdef PMAP_PRIMARYPOS
229 for (j = 0; j < 3; j++)
230 prim -> pos [j] = getflt(file);
231 #endif
232 if (feof(file))
233 error(SYSTEM, "error reading primary photon rays");
234 }
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);
248 return ptype;
249 }