From ea06670c51e33a64d07bf267550ca91a8343033f Mon Sep 17 00:00:00 2001 From: Roland Schregle Date: Thu, 18 Feb 2021 17:08:50 +0000 Subject: [PATCH] fix(mkpmap,pmapdump): Made pmap files (hopefully) more portable by forcing numPhotons to fixed size irrespective of platform (possibly with 0-padding) --- src/rt/pmapdump.c | 23 ++++++++++-------- src/rt/pmapio.c | 59 +++++++++++++++++++++++++++++------------------ src/rt/pmapio.h | 14 +++++++---- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/src/rt/pmapdump.c b/src/rt/pmapdump.c index 7dc7565c6..438459b95 100644 --- a/src/rt/pmapdump.c +++ b/src/rt/pmapdump.c @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: pmapdump.c,v 2.17 2020/08/07 01:21:13 rschregle Exp $"; +static const char RCSid[] = "$Id: pmapdump.c,v 2.18 2021/02/18 17:08:50 rschregle Exp $"; #endif /* @@ -9,15 +9,17 @@ static const char RCSid[] = "$Id: pmapdump.c,v 2.17 2020/08/07 01:21:13 rschregl Roland Schregle (roland.schregle@{hslu.ch, gmail.com}) (c) Fraunhofer Institute for Solar Energy Systems, - supported by the German Research Foundation (DFG) - under the FARESYS project. + 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") (c) Tokyo University of Science, - supported by the JSPS KAKENHI Grant Number JP19KK0115. + supported by the JSPS Grants-in-Aid for Scientific Research + (KAKENHI JP19KK0115, "Three-Dimensional Light Flow") ====================================================================== - $Id: pmapdump.c,v 2.17 2020/08/07 01:21:13 rschregle Exp $ + $Id: pmapdump.c,v 2.18 2021/02/18 17:08:50 rschregle Exp $ */ @@ -219,9 +221,12 @@ int main (int argc, char** argv) fputc('\n', stdout); } } - - /* Get number of photons */ - pm.numPhotons = getint(sizeof(pm.numPhotons), pmapFile); + + /* 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. */ + pm.numPhotons = getint(PMAP_LONGSIZE, pmapFile); /* Skip avg photon flux */ for (j = 0; j < 3; j++) diff --git a/src/rt/pmapio.c b/src/rt/pmapio.c index 395de8c13..7daebed0d 100644 --- a/src/rt/pmapio.c +++ b/src/rt/pmapio.c @@ -1,18 +1,22 @@ #ifndef lint -static const char RCSid[] = "$Id: pmapio.c,v 2.12 2018/08/02 18:33:49 greg Exp $"; +static const char RCSid[] = "$Id: pmapio.c,v 2.13 2021/02/18 17:08:50 rschregle 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.12 2018/08/02 18:33:49 greg Exp $ + $Id: pmapio.c,v 2.13 2021/02/18 17:08:50 rschregle Exp $ */ @@ -57,33 +61,39 @@ void savePhotonMap (const PhotonMap *pmap, const char *fname, 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 */ + /* Write format, including human-readable file version */ fputformat((char*)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++) @@ -177,8 +187,11 @@ PhotonMapType loadPhotonMap (PhotonMap *pmap, const char *fname) 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++) diff --git a/src/rt/pmapio.h b/src/rt/pmapio.h index 177a14726..4ba78a367 100644 --- a/src/rt/pmapio.h +++ b/src/rt/pmapio.h @@ -1,16 +1,19 @@ -/* RCSid $Id: pmapio.h,v 2.7 2018/12/07 20:07:41 rschregle Exp $ */ +/* RCSid $Id: pmapio.h,v 2.8 2021/02/18 17:08:50 rschregle Exp $ */ /* ====================================================================== - 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") ====================================================================== - $Id: pmapio.h,v 2.7 2018/12/07 20:07:41 rschregle Exp $ + $Id: pmapio.h,v 2.8 2021/02/18 17:08:50 rschregle Exp $ */ @@ -51,6 +54,9 @@ PMAP_FILEVER_FLX PMAP_FILEVER_PRIPOS \ PMAP_FILEVER_PRIDIR) + /* Maximum portable size of a long int in photon map file */ + #define PMAP_LONGSIZE 8 + void savePhotonMap (const PhotonMap *pmap, const char *fname, int argc, char **argv);