| 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 |
|
*/ |
| 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; |
| 27 |
< |
FILE* file; |
| 29 |
> |
unsigned long i, j; |
| 30 |
> |
FILE *file; |
| 31 |
|
|
| 32 |
< |
if (!pmap || !pmap -> heap || !pmap -> heapSize || |
| 30 |
< |
!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); |
| 92 |
– |
|
| 93 |
– |
for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) { |
| 94 |
– |
/* Write photon attributes */ |
| 95 |
– |
for (j = 0; j < 3; j++) |
| 96 |
– |
putflt(p -> pos [j], file); |
| 97 |
– |
|
| 98 |
– |
/* Bytewise dump otherwise we have portability probs */ |
| 99 |
– |
for (j = 0; j < 3; j++) |
| 100 |
– |
putint(p -> norm [j], 1, file); |
| 101 |
– |
|
| 102 |
– |
#ifdef PMAP_FLOAT_FLUX |
| 103 |
– |
for (j = 0; j < 3; j++) |
| 104 |
– |
putflt(p -> flux [j], file); |
| 105 |
– |
#else |
| 106 |
– |
for (j = 0; j < 4; j++) |
| 107 |
– |
putint(p -> flux [j], 1, file); |
| 108 |
– |
#endif |
| 109 |
– |
|
| 110 |
– |
putint(p -> primary, sizeof(p -> primary), file); |
| 111 |
– |
putint(p -> flags, 1, file); |
| 112 |
– |
|
| 113 |
– |
if (ferror(file)) { |
| 114 |
– |
sprintf(errmsg, "error writing photon map file %s", fname); |
| 115 |
– |
error(SYSTEM, errmsg); |
| 116 |
– |
} |
| 117 |
– |
} |
| 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 |
| 122 |
< |
* number of entries */ |
| 123 |
< |
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; |
| 127 |
< |
|
| 128 |
< |
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++) |
| 131 |
– |
putflt(prim -> dir [j], file); |
| 132 |
– |
|
| 133 |
– |
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 |
|
} |
| 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; |
| 153 |
< |
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 = |
| 189 |
< |
getint(sizeof(pmap -> heapSize), file); |
| 190 |
< |
pmap -> heap = (Photon *)malloc(pmap -> heapSize * sizeof(Photon)); |
| 191 |
< |
if (!pmap -> heap) |
| 192 |
< |
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); |
| 210 |
– |
|
| 211 |
– |
/* Get photon attributes */ |
| 212 |
– |
for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) { |
| 213 |
– |
for (j = 0; j < 3; j++) |
| 214 |
– |
p -> pos [j] = getflt(file); |
| 215 |
– |
|
| 216 |
– |
/* Bytewise grab otherwise we have portability probs */ |
| 217 |
– |
for (j = 0; j < 3; j++) |
| 218 |
– |
p -> norm [j] = getint(1, file); |
| 219 |
– |
|
| 220 |
– |
#ifdef PMAP_FLOAT_FLUX |
| 221 |
– |
for (j = 0; j < 3; j++) |
| 222 |
– |
p -> flux [j] = getflt(file); |
| 223 |
– |
#else |
| 224 |
– |
for (j = 0; j < 4; j++) |
| 225 |
– |
p -> flux [j] = getint(1, file); |
| 226 |
– |
#endif |
| 197 |
|
|
| 228 |
– |
p -> primary = getint(sizeof(p -> primary), file); |
| 229 |
– |
p -> flags = getint(1, file); |
| 230 |
– |
|
| 231 |
– |
if (feof(file)) { |
| 232 |
– |
sprintf(errmsg, "error reading photon map file %s", fname); |
| 233 |
– |
error(SYSTEM, errmsg); |
| 234 |
– |
} |
| 235 |
– |
} |
| 236 |
– |
|
| 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 * |
| 243 |
< |
sizeof(PhotonPrimary)); |
| 244 |
< |
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++) |
| 253 |
– |
prim -> dir [j] = getflt(file); |
| 254 |
– |
|
| 255 |
– |
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 |
|
} |