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 |
< |
Lucerne University of Applied Sciences & Arts |
12 |
< |
================================================================== |
11 |
> |
(c) Lucerne University of Applied Sciences and Arts, |
12 |
> |
supported by the Swiss National Science Foundation (SNSF, #147053) |
13 |
> |
====================================================================== |
14 |
|
|
15 |
|
$Id$ |
16 |
|
*/ |
24 |
|
|
25 |
|
|
26 |
|
void savePhotonMap (const PhotonMap *pmap, const char *fname, |
27 |
< |
PhotonMapType type, int argc, char **argv) |
27 |
> |
int argc, char **argv) |
28 |
|
{ |
29 |
< |
unsigned long i, j; |
30 |
< |
const Photon* p; |
26 |
< |
FILE* file; |
29 |
> |
unsigned long i, j; |
30 |
> |
FILE *file; |
31 |
|
|
32 |
< |
if (!pmap || !pmap -> heap || !pmap -> heapSize || |
29 |
< |
!validPmapType(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 |
|
} |
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 [type], file); |
77 |
< |
fprintf(file, "VERSION=%d\n", PMAP_FILEVER); |
76 |
> |
fputformat((char*)pmapFormat [pmap -> type], file); |
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++) |
101 |
|
|
102 |
|
/* Write avg distance to centre of gravity */ |
103 |
|
putflt(pmap -> CoGdist, file); |
91 |
– |
|
92 |
– |
for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) { |
93 |
– |
/* Write photon attributes */ |
94 |
– |
for (j = 0; j < 3; j++) |
95 |
– |
putflt(p -> pos [j], file); |
96 |
– |
|
97 |
– |
/* Bytewise dump otherwise we have portability probs */ |
98 |
– |
for (j = 0; j < 3; j++) |
99 |
– |
putint(p -> norm [j], 1, file); |
100 |
– |
|
101 |
– |
#ifdef PMAP_FLOAT_FLUX |
102 |
– |
for (j = 0; j < 3; j++) |
103 |
– |
putflt(p -> flux [j], file); |
104 |
– |
#else |
105 |
– |
for (j = 0; j < 4; j++) |
106 |
– |
putint(p -> flux [j], 1, file); |
107 |
– |
#endif |
108 |
– |
|
109 |
– |
putint(p -> primary, sizeof(p -> primary), file); |
110 |
– |
putint(p -> flags, 1, file); |
111 |
– |
|
112 |
– |
if (ferror(file)) { |
113 |
– |
sprintf(errmsg, "error writing photon map file %s", fname); |
114 |
– |
error(SYSTEM, errmsg); |
115 |
– |
} |
116 |
– |
} |
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 |
121 |
< |
* number of entries */ |
122 |
< |
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; |
126 |
< |
|
127 |
< |
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 |
+ |
#ifdef PMAP_PRIMARYPOS |
115 |
|
for (j = 0; j < 3; j++) |
116 |
< |
putflt(prim -> dir [j], file); |
117 |
< |
|
132 |
< |
for (j = 0; j < 3; j++) |
133 |
< |
putflt(prim -> org [j], file); |
134 |
< |
|
116 |
> |
putflt(prim -> pos [j], file); |
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 |
|
} |
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; |
152 |
< |
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; |
161 |
|
|
162 |
|
/* Identify photon map type from format string */ |
163 |
|
for (ptype = 0; |
164 |
< |
strcmp(pmapFormat [ptype], format) && ptype < NUM_PMAP_TYPES; |
164 |
> |
ptype < NUM_PMAP_TYPES && strcmp(pmapFormat [ptype], format); |
165 |
|
ptype++); |
166 |
|
|
167 |
|
if (!validPmapType(ptype)) { |
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 = |
188 |
< |
getint(sizeof(pmap -> heapSize), file); |
189 |
< |
pmap -> heap = (Photon *)malloc(pmap -> heapSize * sizeof(Photon)); |
190 |
< |
if (!pmap -> heap) |
191 |
< |
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++) |
194 |
|
|
195 |
|
/* Get avg distance to centre of gravity */ |
196 |
|
pmap -> CoGdist = getflt(file); |
209 |
– |
|
210 |
– |
/* Get photon attributes */ |
211 |
– |
for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) { |
212 |
– |
for (j = 0; j < 3; j++) |
213 |
– |
p -> pos [j] = getflt(file); |
214 |
– |
|
215 |
– |
/* Bytewise grab otherwise we have portability probs */ |
216 |
– |
for (j = 0; j < 3; j++) |
217 |
– |
p -> norm [j] = getint(1, file); |
218 |
– |
|
219 |
– |
#ifdef PMAP_FLOAT_FLUX |
220 |
– |
for (j = 0; j < 3; j++) |
221 |
– |
p -> flux [j] = getflt(file); |
222 |
– |
#else |
223 |
– |
for (j = 0; j < 4; j++) |
224 |
– |
p -> flux [j] = getint(1, file); |
225 |
– |
#endif |
197 |
|
|
227 |
– |
p -> primary = getint(sizeof(p -> primary), file); |
228 |
– |
p -> flags = getint(1, file); |
229 |
– |
|
230 |
– |
if (feof(file)) { |
231 |
– |
sprintf(errmsg, "error reading photon map file %s", fname); |
232 |
– |
error(SYSTEM, errmsg); |
233 |
– |
} |
234 |
– |
} |
235 |
– |
|
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 * |
242 |
< |
sizeof(PhotonPrimary)); |
243 |
< |
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 |
+ |
#ifdef PMAP_PRIMARYPOS |
213 |
|
for (j = 0; j < 3; j++) |
214 |
< |
prim -> dir [j] = getflt(file); |
215 |
< |
|
254 |
< |
for (j = 0; j < 3; j++) |
255 |
< |
prim -> org [j] = getflt(file); |
256 |
< |
|
214 |
> |
prim -> pos [j] = getflt(file); |
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); |
263 |
– |
|
232 |
|
return ptype; |
233 |
|
} |