| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: pmapio.c,v 2.6 2015/08/18 18:45:55 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
==================================================================
|
| 6 |
Photon map file I/O
|
| 7 |
|
| 8 |
Roland Schregle (roland.schregle@{hslu.ch, gmail.com})
|
| 9 |
(c) Fraunhofer Institute for Solar Energy Systems,
|
| 10 |
(c) Lucerne University of Applied Sciences and Arts,
|
| 11 |
supported by the Swiss National Science Foundation (SNSF, #147053)
|
| 12 |
==================================================================
|
| 13 |
|
| 14 |
*/
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#include "pmapio.h"
|
| 19 |
#include "pmapdiag.h"
|
| 20 |
#include "resolu.h"
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
void savePhotonMap (const PhotonMap *pmap, const char *fname,
|
| 25 |
int argc, char **argv)
|
| 26 |
{
|
| 27 |
unsigned long i, j;
|
| 28 |
const Photon* p;
|
| 29 |
FILE* file;
|
| 30 |
|
| 31 |
if (!pmap || !pmap -> heap || !pmap -> heapSize ||
|
| 32 |
!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);
|
| 40 |
eputs(errmsg);
|
| 41 |
fflush(stderr);
|
| 42 |
}
|
| 43 |
|
| 44 |
if (!(file = fopen(fname, "wb"))) {
|
| 45 |
sprintf(errmsg, "can't open photon map file %s", fname);
|
| 46 |
error(SYSTEM, errmsg);
|
| 47 |
}
|
| 48 |
|
| 49 |
/* Write header */
|
| 50 |
newheader("RADIANCE", file);
|
| 51 |
|
| 52 |
/* Write command line */
|
| 53 |
printargs(argc, argv, file);
|
| 54 |
|
| 55 |
/* Include statistics in info text */
|
| 56 |
fprintf(file, "%ld photons @ (%.2e, %.2e, %.2e) avg watts\n"
|
| 57 |
"Extent [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n",
|
| 58 |
pmap -> heapSize, pmap -> photonFlux [0],
|
| 59 |
pmap -> photonFlux [1], pmap -> photonFlux [2],
|
| 60 |
pmap -> minPos [0], pmap -> minPos [1], pmap -> minPos [2],
|
| 61 |
pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2]);
|
| 62 |
if (pmap -> primary)
|
| 63 |
fprintf(file, "%d primary rays\n", pmap -> primaryEnd + 1);
|
| 64 |
|
| 65 |
/* Write format */
|
| 66 |
fputformat((char*)pmapFormat [pmap -> type], file);
|
| 67 |
fprintf(file, "VERSION=%d\n", PMAP_FILEVER);
|
| 68 |
|
| 69 |
/* Empty line = end of header */
|
| 70 |
putc('\n', file);
|
| 71 |
|
| 72 |
/* Write file format version */
|
| 73 |
putint(PMAP_FILEVER, sizeof(PMAP_FILEVER), file);
|
| 74 |
|
| 75 |
/* Write number of photons */
|
| 76 |
putint(pmap -> heapSize, sizeof(pmap -> heapSize), file);
|
| 77 |
|
| 78 |
/* Write average photon flux */
|
| 79 |
for (j = 0; j < 3; j++)
|
| 80 |
putflt(pmap -> photonFlux [j], file);
|
| 81 |
|
| 82 |
/* Write max and min photon positions */
|
| 83 |
for (j = 0; j < 3; j++) {
|
| 84 |
putflt(pmap -> minPos [j], file);
|
| 85 |
putflt(pmap -> maxPos [j], file);
|
| 86 |
}
|
| 87 |
|
| 88 |
/* Write centre of gravity */
|
| 89 |
for (j = 0; j < 3; j++)
|
| 90 |
putflt(pmap -> CoG [j], file);
|
| 91 |
|
| 92 |
/* Write avg distance to centre of gravity */
|
| 93 |
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 |
}
|
| 120 |
|
| 121 |
/* Write out primary photon rays (or just zero count if none) */
|
| 122 |
if (pmap -> primary) {
|
| 123 |
/* primaryEnd points to last primary ray in array, so increment for
|
| 124 |
* number of entries */
|
| 125 |
putint(pmap -> primaryEnd + 1, sizeof(pmap -> primaryEnd), file);
|
| 126 |
|
| 127 |
for (i = 0; i <= pmap -> primaryEnd; i++) {
|
| 128 |
PhotonPrimary *prim = pmap -> primary + i;
|
| 129 |
|
| 130 |
putint(prim -> srcIdx, sizeof(prim -> srcIdx), file);
|
| 131 |
|
| 132 |
putint(prim -> dir, sizeof(prim -> dir), file);
|
| 133 |
|
| 134 |
for (j = 0; j < 3; j++)
|
| 135 |
putflt(prim -> pos [j], file);
|
| 136 |
|
| 137 |
if (ferror(file))
|
| 138 |
error(SYSTEM, "error writing primary photon rays");
|
| 139 |
}
|
| 140 |
}
|
| 141 |
else putint(0, sizeof(pmap -> primaryEnd), file);
|
| 142 |
|
| 143 |
fclose(file);
|
| 144 |
}
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname)
|
| 149 |
{
|
| 150 |
Photon* p;
|
| 151 |
PhotonMapType ptype = PMAP_TYPE_NONE;
|
| 152 |
char format [128];
|
| 153 |
unsigned long i, j;
|
| 154 |
FILE *file;
|
| 155 |
|
| 156 |
if (!pmap)
|
| 157 |
return PMAP_TYPE_NONE;
|
| 158 |
|
| 159 |
if ((file = fopen(fname, "rb")) == NULL) {
|
| 160 |
sprintf(errmsg, "can't open photon map file %s", fname);
|
| 161 |
error(SYSTEM, errmsg);
|
| 162 |
}
|
| 163 |
|
| 164 |
/* Get format string */
|
| 165 |
strcpy(format, PMAP_FORMAT_GLOB);
|
| 166 |
if (checkheader(file, format, NULL) != 1) {
|
| 167 |
sprintf(errmsg, "photon map file %s has unknown format %s",
|
| 168 |
fname, format);
|
| 169 |
error(USER, errmsg);
|
| 170 |
}
|
| 171 |
|
| 172 |
/* Identify photon map type from format string */
|
| 173 |
for (ptype = 0;
|
| 174 |
strcmp(pmapFormat [ptype], format) && ptype < NUM_PMAP_TYPES;
|
| 175 |
ptype++);
|
| 176 |
|
| 177 |
if (!validPmapType(ptype)) {
|
| 178 |
sprintf(errmsg, "file %s contains an unknown photon map type", fname);
|
| 179 |
error(USER, errmsg);
|
| 180 |
}
|
| 181 |
|
| 182 |
initPhotonMap(pmap, ptype);
|
| 183 |
|
| 184 |
/* Get file format version and check for compatibility */
|
| 185 |
if (getint(sizeof(PMAP_FILEVER), file) != PMAP_FILEVER)
|
| 186 |
error(USER, "incompatible photon map file format");
|
| 187 |
|
| 188 |
/* Get number of photons */
|
| 189 |
pmap -> heapSize = pmap -> heapEnd =
|
| 190 |
getint(sizeof(pmap -> heapSize), file);
|
| 191 |
pmap -> heap = (Photon *)malloc(pmap -> heapSize * sizeof(Photon));
|
| 192 |
if (!pmap -> heap)
|
| 193 |
error(INTERNAL, "can't allocate photon heap from file");
|
| 194 |
|
| 195 |
/* Get average photon flux */
|
| 196 |
for (j = 0; j < 3; j++)
|
| 197 |
pmap -> photonFlux [j] = getflt(file);
|
| 198 |
|
| 199 |
/* Get max and min photon positions */
|
| 200 |
for (j = 0; j < 3; j++) {
|
| 201 |
pmap -> minPos [j] = getflt(file);
|
| 202 |
pmap -> maxPos [j] = getflt(file);
|
| 203 |
}
|
| 204 |
|
| 205 |
/* Get centre of gravity */
|
| 206 |
for (j = 0; j < 3; j++)
|
| 207 |
pmap -> CoG [j] = getflt(file);
|
| 208 |
|
| 209 |
/* Get avg distance to centre of gravity */
|
| 210 |
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
|
| 228 |
|
| 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 |
|
| 238 |
/* Get primary photon rays */
|
| 239 |
pmap -> primarySize = getint(sizeof(pmap -> primarySize), file);
|
| 240 |
|
| 241 |
if (pmap -> primarySize) {
|
| 242 |
pmap -> primaryEnd = pmap -> primarySize - 1;
|
| 243 |
pmap -> primary = (PhotonPrimary*)malloc(pmap -> primarySize *
|
| 244 |
sizeof(PhotonPrimary));
|
| 245 |
if (!pmap -> primary)
|
| 246 |
error(INTERNAL, "can't allocate primary photon rays");
|
| 247 |
|
| 248 |
for (i = 0; i < pmap -> primarySize; i++) {
|
| 249 |
PhotonPrimary *prim = pmap -> primary + i;
|
| 250 |
|
| 251 |
prim -> srcIdx = getint(sizeof(prim -> srcIdx), file);
|
| 252 |
|
| 253 |
prim -> dir = getint(sizeof(prim -> dir), file);
|
| 254 |
|
| 255 |
for (j = 0; j < 3; j++)
|
| 256 |
prim -> pos [j] = getflt(file);
|
| 257 |
|
| 258 |
if (feof(file))
|
| 259 |
error(SYSTEM, "error reading primary photon rays");
|
| 260 |
}
|
| 261 |
}
|
| 262 |
|
| 263 |
fclose(file);
|
| 264 |
|
| 265 |
return ptype;
|
| 266 |
}
|