--- ray/src/rt/pmapio.c 2015/10/20 15:51:54 2.8 +++ ray/src/rt/pmapio.c 2016/05/17 17:39:47 2.9 @@ -1,16 +1,18 @@ #ifndef lint -static const char RCSid[] = "$Id: pmapio.c,v 2.8 2015/10/20 15:51:54 rschregle Exp $"; +static const char RCSid[] = "$Id: pmapio.c,v 2.9 2016/05/17 17:39:47 rschregle Exp $"; #endif + /* - ================================================================== + ====================================================================== Photon map file I/O Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) (c) Fraunhofer Institute for Solar Energy Systems, (c) Lucerne University of Applied Sciences and Arts, - supported by the Swiss National Science Foundation (SNSF, #147053) - ================================================================== + supported by the Swiss National Science Foundation (SNSF, #147053) + ====================================================================== + $Id: pmapio.c,v 2.9 2016/05/17 17:39:47 rschregle Exp $ */ @@ -24,19 +26,21 @@ static const char RCSid[] = "$Id: pmapio.c,v 2.8 2015/ void savePhotonMap (const PhotonMap *pmap, const char *fname, int argc, char **argv) { - unsigned long i, j; - const Photon* p; - FILE* file; + unsigned long i, j; + FILE *file; - if (!pmap || !pmap -> heap || !pmap -> heapSize || - !validPmapType(pmap -> type)) { + if (!pmap || !pmap -> numPhotons || !validPmapType(pmap -> type)) { error(INTERNAL, "attempt to save empty or invalid photon map"); return; } if (photonRepTime) { - sprintf(errmsg, "Saving %s (%ld photons)...\n", - fname, pmap -> heapSize); + if (pmap -> numPrimary) + sprintf(errmsg, "Saving %s (%ld photons, %d primaries)...\n", + fname, pmap -> numPhotons, pmap -> numPrimary); + else sprintf(errmsg, "Saving %s (%ld photons)...\n", fname, + pmap -> numPhotons); + eputs(errmsg); fflush(stderr); } @@ -53,27 +57,33 @@ void savePhotonMap (const PhotonMap *pmap, const char printargs(argc, argv, file); /* Include statistics in info text */ - fprintf(file, "%ld photons @ (%.2e, %.2e, %.2e) avg watts\n" - "Extent [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n", - pmap -> heapSize, pmap -> photonFlux [0], + fprintf(file, "NumPhotons\t= %ld\n" + "AvgFlux\t\t= [%.2e, %.2e, %.2e]\n" + "Bbox\t\t= [%.3f, %.3f, %.3f] [%.3f, %.3f, %.3f]\n" + "CoG\t\t= [%.3f, %.3f, %.3f]\n" + "MaxDist^2\t= %.3f\n", + pmap -> numPhotons, pmap -> photonFlux [0], pmap -> photonFlux [1], pmap -> photonFlux [2], pmap -> minPos [0], pmap -> minPos [1], pmap -> minPos [2], - pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2]); - if (pmap -> primary) - fprintf(file, "%d primary rays\n", pmap -> primaryEnd + 1); + pmap -> maxPos [0], pmap -> maxPos [1], pmap -> maxPos [2], + pmap -> CoG [0], pmap -> CoG [1], pmap -> CoG [2], + pmap -> CoGdist); + + if (pmap -> primaries) + fprintf(file, "%d primary rays\n", pmap -> numPrimary); /* Write format */ fputformat((char*)pmapFormat [pmap -> type], file); - fprintf(file, "VERSION=%d\n", PMAP_FILEVER); + fprintf(file, "VERSION=%s\n", PMAP_FILEVER); /* Empty line = end of header */ putc('\n', file); /* Write file format version */ - putint(PMAP_FILEVER, sizeof(PMAP_FILEVER), file); + putstr(PMAP_FILEVER, file); /* Write number of photons */ - putint(pmap -> heapSize, sizeof(pmap -> heapSize), file); + putint(pmap -> numPhotons, sizeof(pmap -> numPhotons), file); /* Write average photon flux */ for (j = 0; j < 3; j++) @@ -91,54 +101,35 @@ void savePhotonMap (const PhotonMap *pmap, const char /* Write avg distance to centre of gravity */ putflt(pmap -> CoGdist, file); - - for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) { - /* Write photon attributes */ - for (j = 0; j < 3; j++) - putflt(p -> pos [j], file); - - /* Bytewise dump otherwise we have portability probs */ - for (j = 0; j < 3; j++) - putint(p -> norm [j], 1, file); - - #ifdef PMAP_FLOAT_FLUX - for (j = 0; j < 3; j++) - putflt(p -> flux [j], file); - #else - for (j = 0; j < 4; j++) - putint(p -> flux [j], 1, file); - #endif - - putint(p -> primary, sizeof(p -> primary), file); - putint(p -> flags, 1, file); - - if (ferror(file)) { - sprintf(errmsg, "error writing photon map file %s", fname); - error(SYSTEM, errmsg); - } - } /* Write out primary photon rays (or just zero count if none) */ - if (pmap -> primary) { - /* primaryEnd points to last primary ray in array, so increment for - * number of entries */ - putint(pmap -> primaryEnd + 1, sizeof(pmap -> primaryEnd), file); + if (pmap -> primaries) { + putint(pmap -> numPrimary, sizeof(pmap -> numPrimary), file); - for (i = 0; i <= pmap -> primaryEnd; i++) { - PhotonPrimary *prim = pmap -> primary + i; - - putint(prim -> srcIdx, sizeof(prim -> srcIdx), file); + for (i = 0; i < pmap -> numPrimary; i++) { + PhotonPrimary *prim = pmap -> primaries + i; + putint(prim -> srcIdx, sizeof(prim -> srcIdx), file); putint(prim -> dir, sizeof(prim -> dir), file); - +#ifdef PMAP_PRIMARYPOS for (j = 0; j < 3; j++) putflt(prim -> pos [j], file); - +#endif if (ferror(file)) error(SYSTEM, "error writing primary photon rays"); } } - else putint(0, sizeof(pmap -> primaryEnd), file); + else putint(0, sizeof(pmap -> numPrimary), file); + + /* Save photon storage */ +#ifdef PMAP_OOC + if (OOC_SavePhotons(pmap, file)) { +#else + if (kdT_SavePhotons(pmap, file)) { +#endif + sprintf(errmsg, "error writing photon map file %s", fname); + error(SYSTEM, errmsg); + } fclose(file); } @@ -147,11 +138,10 @@ void savePhotonMap (const PhotonMap *pmap, const char PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname) { - Photon* p; - PhotonMapType ptype = PMAP_TYPE_NONE; - char format [128]; - unsigned long i, j; - FILE *file; + PhotonMapType ptype = PMAP_TYPE_NONE; + char format [128]; + unsigned long i, j; + FILE *file; if (!pmap) return PMAP_TYPE_NONE; @@ -182,15 +172,11 @@ PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch initPhotonMap(pmap, ptype); /* Get file format version and check for compatibility */ - if (getint(sizeof(PMAP_FILEVER), file) != PMAP_FILEVER) + if (strcmp(getstr(format, file), PMAP_FILEVER)) error(USER, "incompatible photon map file format"); /* Get number of photons */ - pmap -> heapSize = pmap -> heapEnd = - getint(sizeof(pmap -> heapSize), file); - pmap -> heap = (Photon *)malloc(pmap -> heapSize * sizeof(Photon)); - if (!pmap -> heap) - error(INTERNAL, "can't allocate photon heap from file"); + pmap -> numPhotons = getint(sizeof(pmap -> numPhotons), file); /* Get average photon flux */ for (j = 0; j < 3; j++) @@ -208,59 +194,40 @@ PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch /* Get avg distance to centre of gravity */ pmap -> CoGdist = getflt(file); - - /* Get photon attributes */ - for (i = 0, p = pmap -> heap; i < pmap -> heapSize; i++, p++) { - for (j = 0; j < 3; j++) - p -> pos [j] = getflt(file); - - /* Bytewise grab otherwise we have portability probs */ - for (j = 0; j < 3; j++) - p -> norm [j] = getint(1, file); - - #ifdef PMAP_FLOAT_FLUX - for (j = 0; j < 3; j++) - p -> flux [j] = getflt(file); - #else - for (j = 0; j < 4; j++) - p -> flux [j] = getint(1, file); - #endif - p -> primary = getint(sizeof(p -> primary), file); - p -> flags = getint(1, file); - - if (feof(file)) { - sprintf(errmsg, "error reading photon map file %s", fname); - error(SYSTEM, errmsg); - } - } - /* Get primary photon rays */ - pmap -> primarySize = getint(sizeof(pmap -> primarySize), file); + pmap -> numPrimary = getint(sizeof(pmap -> numPrimary), file); - if (pmap -> primarySize) { - pmap -> primaryEnd = pmap -> primarySize - 1; - pmap -> primary = (PhotonPrimary*)malloc(pmap -> primarySize * - sizeof(PhotonPrimary)); - if (!pmap -> primary) + if (pmap -> numPrimary) { + pmap -> primaries = calloc(pmap -> numPrimary, sizeof(PhotonPrimary)); + if (!pmap -> primaries) error(INTERNAL, "can't allocate primary photon rays"); - for (i = 0; i < pmap -> primarySize; i++) { - PhotonPrimary *prim = pmap -> primary + i; + for (i = 0; i < pmap -> numPrimary; i++) { + PhotonPrimary *prim = pmap -> primaries + i; prim -> srcIdx = getint(sizeof(prim -> srcIdx), file); prim -> dir = getint(sizeof(prim -> dir), file); - +#ifdef PMAP_PRIMARYPOS for (j = 0; j < 3; j++) prim -> pos [j] = getflt(file); - +#endif if (feof(file)) error(SYSTEM, "error reading primary photon rays"); } - } + } + /* Load photon storage */ +#ifdef PMAP_OOC + if (OOC_LoadPhotons(pmap, file)) { +#else + if (kdT_LoadPhotons(pmap, file)) { +#endif + sprintf(errmsg, "error reading photon map file %s", fname); + error(SYSTEM, errmsg); + } + fclose(file); - return ptype; }