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 |
|
*/ |
21 |
|
|
22 |
|
|
30 |
|
void savePhotonMap (const PhotonMap *pmap, const char *fname, |
31 |
|
int argc, char **argv) |
32 |
|
{ |
33 |
< |
unsigned long i, j; |
34 |
< |
const Photon* p; |
29 |
< |
FILE* file; |
33 |
> |
unsigned long i, j; |
34 |
> |
FILE *file; |
35 |
|
|
36 |
< |
if (!pmap || !pmap -> heap || !pmap -> heapSize || |
32 |
< |
!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 |
|
} |
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 */ |
83 |
< |
fputformat((char*)pmapFormat [pmap -> type], file); |
84 |
< |
fprintf(file, "VERSION=%d\n", PMAP_FILEVER); |
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 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++) |
111 |
|
|
112 |
|
/* Write avg distance to centre of gravity */ |
113 |
|
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 |
– |
} |
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 |
124 |
< |
* number of entries */ |
125 |
< |
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; |
129 |
< |
|
130 |
< |
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 |
|
} |
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; |
154 |
< |
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; |
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) |
193 |
< |
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++) |
209 |
|
|
210 |
|
/* Get avg distance to centre of gravity */ |
211 |
|
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 |
212 |
|
|
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 |
– |
|
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 * |
244 |
< |
sizeof(PhotonPrimary)); |
245 |
< |
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); |
264 |
– |
|
248 |
|
return ptype; |
249 |
|
} |