From d9268593cec0366c9be487cee87eddea3f93f4c1 Mon Sep 17 00:00:00 2001 From: Oskar Carlbaum Date: Wed, 4 Oct 2017 14:56:47 +0200 Subject: [PATCH] Add functionality for saving states to the OSFLS format --- .../util/fieldlinesstate.cpp | 62 +++++++++++++++++++ .../fieldlinessequence/util/fieldlinesstate.h | 1 + 2 files changed, 63 insertions(+) diff --git a/modules/fieldlinessequence/util/fieldlinesstate.cpp b/modules/fieldlinessequence/util/fieldlinesstate.cpp index d5801bfb84..264513ce0c 100644 --- a/modules/fieldlinessequence/util/fieldlinesstate.cpp +++ b/modules/fieldlinessequence/util/fieldlinesstate.cpp @@ -193,6 +193,68 @@ bool FieldlinesState::loadStateFromJson(const std::string& PATH_TO_JSON_FILE, return true; } +/** + * @param ABS_FILEPATH must be the path to the file (incl. filename but excl. extension!) + * Directory must exist! File is created (or overwritten if already existing). + * File is structured like this: (for version 0) + * 0. int - version number of binary state file! (in case something needs to be altered in the future, then increase CURRENT_VERSION) + * 1. double - _triggerTime + * 2. int - _model + * 3. bool - _isMorphable + * 4. size_t - Number of lines in the state == _lineStart.size() == _lineCount.size() + * 5. size_t - Total number of vertex points == _vertexPositions.size() == _extraQuantities[i].size() + * 6. size_t - Number of extra quantites == _extraQuantities.size() == _extraQuantityNames.size() + * 7. site_t - Number of total bytes that ALL _extraQuantityNames consists of (Each such name is stored as a c_str which means it ends with the null char '\0' ) + * 7. std::vector - _lineStart + * 8. std::vector - _lineCount + * 9. std::vector - _vertexPositions + * 10. std::vector - _extraQuantities + * 11. array of c_str - Strings naming the extra quantities (elements of _extraQuantityNames). Each string ends with null char '\0' + */ +void FieldlinesState::saveStateToOsfls(const std::string& ABS_FILEPATH) { + // Create the file + const std::string EXT = ".osfls"; + std::ofstream ofs(ABS_FILEPATH + EXT, std::ofstream::binary | std::ofstream::trunc); + if (!ofs.is_open()) { + LERROR("Failed to save state to binary file: " << ABS_FILEPATH << EXT); + return; + } + + std::string allExtraQuantityNamesInOne = ""; + for (std::string str : _extraQuantityNames) { + allExtraQuantityNamesInOne += str + '\0'; // Add the null char '\0' for easier reading + } + + const size_t N_LINES = _lineStart.size(); + const size_t N_POINTS = _vertexPositions.size(); + const size_t N_EXTRAS = _extraQuantities.size(); + const size_t N_STRING_BYTES = allExtraQuantityNamesInOne.size(); + + //------------------------------ WRITE EVERYTHING TO FILE ------------------------------ + // WHICH VERSION OF BINARY FIELDLINES STATE FILE - IN CASE STRUCTURE CHANGES IN THE FUTURE + ofs.write( (char*)(&CURRENT_VERSION), sizeof( int ) ); + + //-------------------- WRITE META DATA FOR STATE -------------------------------- + ofs.write( reinterpret_cast(&_triggerTime), sizeof( _triggerTime ) ); + ofs.write( reinterpret_cast(&_model), sizeof( int ) ); + ofs.write( reinterpret_cast(&_isMorphable), sizeof( bool ) ); + + ofs.write( reinterpret_cast(&N_LINES), sizeof( size_t ) ); + ofs.write( reinterpret_cast(&N_POINTS), sizeof( size_t ) ); + ofs.write( reinterpret_cast(&N_EXTRAS), sizeof( size_t ) ); + ofs.write( reinterpret_cast(&N_STRING_BYTES), sizeof( size_t ) ); + + //---------------------- WRITE ALL ARRAYS OF DATA -------------------------------- + ofs.write( reinterpret_cast(_lineStart.data()), sizeof(GLint) * N_LINES); + ofs.write( reinterpret_cast(_lineCount.data()), sizeof(GLsizei) * N_LINES); + ofs.write( reinterpret_cast(_vertexPositions.data()), sizeof(glm::vec3) * N_POINTS); + // Write the data for each vector in _extraQuantities + for (std::vector& vec : _extraQuantities) { + ofs.write( reinterpret_cast(vec.data()), sizeof(float) * N_POINTS); + } + ofs.write( allExtraQuantityNamesInOne.c_str(), N_STRING_BYTES); +} + // TODO: This should probably be rewritten, but this is the way the files were structured by CCMC // Structure of File! NO TRAILING COMMAS ALLOWED! // Additional info can be stored within each line as the code only extracts the keys it needs (time, trace & data) diff --git a/modules/fieldlinessequence/util/fieldlinesstate.h b/modules/fieldlinessequence/util/fieldlinesstate.h index 2417907a71..a4f2a9b50e 100644 --- a/modules/fieldlinessequence/util/fieldlinesstate.h +++ b/modules/fieldlinessequence/util/fieldlinesstate.h @@ -43,6 +43,7 @@ public: FieldlinesState(const std::string& PATH_TO_OSFLS_FILE, bool& loadSucessful); bool loadStateFromOsfls(const std::string& PATH_TO_OSFLS_FILE); + void saveStateToOsfls(const std::string& PATH_TO_OSFLS_FILE); bool loadStateFromJson(const std::string& PATH_TO_JSON_FILE, const fls::Model MODEL, const float COORD_TO_METERS);