--- ray/src/rt/pmapio.c 2016/05/17 17:39:47 2.9 +++ ray/src/rt/pmapio.c 2022/03/03 03:55:13 2.14 @@ -1,18 +1,22 @@ #ifndef lint -static const char RCSid[] = "$Id: pmapio.c,v 2.9 2016/05/17 17:39:47 rschregle Exp $"; +static const char RCSid[] = "$Id: pmapio.c,v 2.14 2022/03/03 03:55:13 greg Exp $"; #endif /* - ====================================================================== - Photon map file I/O + ======================================================================= + Photon map portable file I/O Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) (c) Fraunhofer Institute for Solar Energy Systems, + supported by the German Research Foundation + (DFG LU-204/10-2, "Fassadenintegrierte Regelsysteme FARESYS") (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, "Daylight Redirecting Components", + SNSF #179067, "Light Fields for Spatio-Temporal Glare Assessment") + ======================================================================= - $Id: pmapio.c,v 2.9 2016/05/17 17:39:47 rschregle Exp $ + $Id: pmapio.c,v 2.14 2022/03/03 03:55:13 greg Exp $ */ @@ -34,11 +38,11 @@ void savePhotonMap (const PhotonMap *pmap, const char return; } - if (photonRepTime) { + if (verbose) { if (pmap -> numPrimary) - sprintf(errmsg, "Saving %s (%ld photons, %d primaries)...\n", + sprintf(errmsg, "Saving %s (%ld photons, %d primaries)\n", fname, pmap -> numPhotons, pmap -> numPrimary); - else sprintf(errmsg, "Saving %s (%ld photons)...\n", fname, + else sprintf(errmsg, "Saving %s (%ld photons)\n", fname, pmap -> numPhotons); eputs(errmsg); @@ -57,33 +61,39 @@ void savePhotonMap (const PhotonMap *pmap, const char printargs(argc, argv, file); /* Include statistics in info text */ - 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], - pmap -> CoG [0], pmap -> CoG [1], pmap -> CoG [2], - pmap -> CoGdist); + 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], + 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); + /* Write format, including human-readable file version */ + fputformat(pmapFormat [pmap -> type], file); fprintf(file, "VERSION=%s\n", PMAP_FILEVER); /* Empty line = end of header */ putc('\n', file); - /* Write file format version */ + /* Write machine-readable file format version */ putstr(PMAP_FILEVER, file); - /* Write number of photons */ - putint(pmap -> numPhotons, sizeof(pmap -> numPhotons), file); + /* Write number of photons as fixed size, which possibly results in + * padding of MSB with 0 on some platforms. Unlike sizeof() however, + * this ensures portability since this value may span 32 or 64 bits + * depending on platform. */ + putint(pmap -> numPhotons, PMAP_LONGSIZE, file); /* Write average photon flux */ for (j = 0; j < 3; j++) @@ -110,7 +120,9 @@ void savePhotonMap (const PhotonMap *pmap, const char PhotonPrimary *prim = pmap -> primaries + i; putint(prim -> srcIdx, sizeof(prim -> srcIdx), file); +#ifdef PMAP_PRIMARYDIR putint(prim -> dir, sizeof(prim -> dir), file); +#endif #ifdef PMAP_PRIMARYPOS for (j = 0; j < 3; j++) putflt(prim -> pos [j], file); @@ -139,7 +151,7 @@ void savePhotonMap (const PhotonMap *pmap, const char PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname) { PhotonMapType ptype = PMAP_TYPE_NONE; - char format [128]; + char format [MAXFMTLEN]; unsigned long i, j; FILE *file; @@ -175,8 +187,11 @@ PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch if (strcmp(getstr(format, file), PMAP_FILEVER)) error(USER, "incompatible photon map file format"); - /* Get number of photons */ - pmap -> numPhotons = getint(sizeof(pmap -> numPhotons), file); + /* Get number of photons as fixed size, which possibly results in + * padding of MSB with 0 on some platforms. Unlike sizeof() however, + * this ensures portability since this value may span 32 or 64 bits + * depending on platform. */ + pmap -> numPhotons = getint(PMAP_LONGSIZE, file); /* Get average photon flux */ for (j = 0; j < 3; j++) @@ -207,8 +222,9 @@ PhotonMapType loadPhotonMap (PhotonMap *pmap, const ch PhotonPrimary *prim = pmap -> primaries + i; prim -> srcIdx = getint(sizeof(prim -> srcIdx), file); - +#ifdef PMAP_PRIMARYDIR prim -> dir = getint(sizeof(prim -> dir), file); +#endif #ifdef PMAP_PRIMARYPOS for (j = 0; j < 3; j++) prim -> pos [j] = getflt(file);