From 3a352ccfc504081ba25cc8152d5e43bbd4a1c8a5 Mon Sep 17 00:00:00 2001 From: Andreas Engberg Date: Tue, 25 Mar 2025 11:43:26 +0100 Subject: [PATCH] reader wip --- .../rendering/renderabletimevaryingvolume.cpp | 52 +++++++++++++----- .../rendering/renderabletimevaryingvolume.h | 1 + modules/volume/xmlreader.cpp | 55 +++++++------------ modules/volume/xmlreader.h | 4 +- 4 files changed, 62 insertions(+), 50 deletions(-) diff --git a/modules/volume/rendering/renderabletimevaryingvolume.cpp b/modules/volume/rendering/renderabletimevaryingvolume.cpp index 07bc97f7dd..1942757523 100644 --- a/modules/volume/rendering/renderabletimevaryingvolume.cpp +++ b/modules/volume/rendering/renderabletimevaryingvolume.cpp @@ -234,34 +234,60 @@ void RenderableTimeVaryingVolume::initializeGL() { } namespace fs = std::filesystem; + double timestep = 0.0; for (const fs::directory_entry& e : fs::recursive_directory_iterator(sequenceDir)) { if (e.is_regular_file() && e.path().extension() == ".dictionary") { loadTimestepMetadata(e.path()); } if (e.is_regular_file() && e.path().extension() == ".vti") { - readVTIFile(e.path()); + const auto [metadata, scalars] = readVTIFile(e.path(), timestep++); + + Timestep t; + t.metadata = metadata; + t.baseName = ""; + t.inRam = false; + t.onGpu = false; + t.rawData = scalars; + + _volumeTimesteps[t.metadata.time] = std::move(t); } } // TODO: defer loading of data to later (separate thread or at least not when loading) for (std::pair& p : _volumeTimesteps) { Timestep& t = p.second; - const std::string path = std::format( - "{}/{}.rawvolume", _sourceDirectory.value(), t.baseName - ); - RawVolumeReader reader(path, t.metadata.dimensions); - t.rawVolume = reader.read(_invertDataAtZ); + + // Read volume from file if it exists + if (t.baseName != "") { + const std::string path = std::format( + "{}/{}.rawvolume", _sourceDirectory.value(), t.baseName + ); + RawVolumeReader reader(path, t.metadata.dimensions); + t.rawVolume = reader.read(_invertDataAtZ); + } const float min = t.metadata.minValue; const float diff = t.metadata.maxValue - t.metadata.minValue; - float* data = t.rawVolume->data(); - for (size_t i = 0; i < t.rawVolume->nCells(); i++) { - data[i] = glm::clamp((data[i] - min) / diff, 0.f, 1.f); - } - t.histogram = std::make_shared(0.f, 1.f, 100); - for (size_t i = 0; i < t.rawVolume->nCells(); i++) { - t.histogram->add(data[i]); + float* data; + // We've read data from binary file + if (t.rawVolume) { + data = t.rawVolume->data(); + for (size_t i = 0; i < t.rawVolume->nCells(); i++) { + data[i] = glm::clamp((data[i] - min) / diff, 0.f, 1.f); + } + + t.histogram = std::make_shared(0.f, 1.f, 100); + for (size_t i = 0; i < t.rawVolume->nCells(); i++) { + t.histogram->add(data[i]); + } + } + // Data came from xml file + else { + data = t.rawData.data(); + for (size_t i = 0; i < t.rawData.size(); i++) { + data[i] = glm::clamp((data[i] - min) / diff, 0.f, 1.f); + } } // TODO: handle normalization properly for different timesteps + transfer function diff --git a/modules/volume/rendering/renderabletimevaryingvolume.h b/modules/volume/rendering/renderabletimevaryingvolume.h index 631de3a34d..9a9520d5c6 100644 --- a/modules/volume/rendering/renderabletimevaryingvolume.h +++ b/modules/volume/rendering/renderabletimevaryingvolume.h @@ -68,6 +68,7 @@ private: std::shared_ptr> rawVolume; std::shared_ptr texture; std::shared_ptr histogram; + std::vector rawData; }; Timestep* currentTimestep(); diff --git a/modules/volume/xmlreader.cpp b/modules/volume/xmlreader.cpp index 586d464ce2..e3604790c5 100644 --- a/modules/volume/xmlreader.cpp +++ b/modules/volume/xmlreader.cpp @@ -23,12 +23,12 @@ ****************************************************************************************/ #include -#include + #include #include +#include #include #include - #include @@ -49,7 +49,7 @@ std::string getAttribute(const std::string& line, const std::string& attribute) } -void readVTIFile(const std::filesystem::path& path) { +std::pair> readVTIFile(const std::filesystem::path& path, double timestep) { tinyxml2::XMLDocument doc; if (doc.LoadFile(path.string().c_str()) != tinyxml2::XML_SUCCESS) { @@ -86,49 +86,32 @@ void readVTIFile(const std::filesystem::path& path) { ss.clear(); float value; + float minValue = std::numeric_limits::max(); + float maxValue = std::numeric_limits::lowest(); + while (ss >> value) { scalars.push_back(value); + minValue = std::min(minValue, value); + maxValue = std::max(maxValue, value); } - std::cout << scalars.size(); + volume::RawVolumeMetadata metadata; - //std::ifstream file; - //file.open(path); - //ghoul_assert(file.good(), "File handle should be good"); + metadata.dimensions = extents; - //std::stringstream ss; - //std::string line; - //while (ghoul::getline(file, line)) { - // std::cout << line << std::endl; + metadata.hasDomainBounds = true; + metadata.lowerDomainBound = minExtent; + metadata.upperDomainBound = maxExtent; - // if (line.find(" dataValues; - //while (ss >> value) { - // dataValues.push_back(value); - //} - - //file.close(); + return std::make_pair(metadata, scalars); } } // namespace openspace diff --git a/modules/volume/xmlreader.h b/modules/volume/xmlreader.h index f78722c91c..303ef7d930 100644 --- a/modules/volume/xmlreader.h +++ b/modules/volume/xmlreader.h @@ -23,9 +23,11 @@ ****************************************************************************************/ #include +#include + namespace openspace { - void readVTIFile(const std::filesystem::path& path); +std::pair> readVTIFile(const std::filesystem::path& path, double timestep); } // namespace openspace