Volume conversion and rendering (#350)

Add volume rendering features
 - Improve task runner
 - Improve reading from CDF files
 - Basic time varying volume rendering
 - Fix scaling bug in RenderableToyVolume
This commit is contained in:
Emil Axelsson
2017-09-22 12:03:23 +02:00
committed by GitHub
parent 7aceb54bec
commit ea5382c028
59 changed files with 1553 additions and 176 deletions
@@ -0,0 +1,250 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/volume/rendering/basicvolumeraycaster.h>
#include <ghoul/glm.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <sstream>
#include <ghoul/opengl/programobject.h>
#include <ghoul/opengl/textureunit.h>
#include <openspace/util/powerscaledcoordinate.h>
#include <openspace/util/updatestructures.h>
#include <openspace/rendering/renderable.h>
namespace {
const char* GlslRaycastPath = "${MODULE_VOLUME}/shaders/raycast.glsl";
const char* GlslHelperPath = "${MODULE_VOLUME}/shaders/helper.glsl";
const char* GlslBoundsVsPath = "${MODULE_VOLUME}/shaders/boundsvs.glsl";
const char* GlslBoundsFsPath = "${MODULE_VOLUME}/shaders/boundsfs.glsl";
}
namespace openspace {
namespace volume {
BasicVolumeRaycaster::BasicVolumeRaycaster(
std::shared_ptr<ghoul::opengl::Texture> volumeTexture,
std::shared_ptr<TransferFunction> transferFunction,
std::shared_ptr<VolumeClipPlanes> clipPlanes)
: _volumeTexture(volumeTexture)
, _transferFunction(transferFunction)
, _clipPlanes(clipPlanes)
, _boundingBox(glm::vec3(1.0))
, _opacity(20.0)
, _rNormalization(0.0)
, _rUpperBound(1.0)
, _valueRemapping(0.0, 1.0)
{}
BasicVolumeRaycaster::~BasicVolumeRaycaster() {}
void BasicVolumeRaycaster::initialize() {
_boundingBox.initialize();
}
void BasicVolumeRaycaster::deinitialize() {}
void BasicVolumeRaycaster::renderEntryPoints(
const RenderData& data,
ghoul::opengl::ProgramObject& program)
{
program.setUniform("modelViewTransform", glm::mat4(modelViewTransform(data)));
program.setUniform("projectionTransform", data.camera.projectionMatrix());
// Cull back face
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
// Render bounding geometry
_boundingBox.render();
}
glm::dmat4 BasicVolumeRaycaster::modelViewTransform(const RenderData& data) {
glm::dmat4 modelTransform =
glm::translate(glm::dmat4(1.0), data.modelTransform.translation) *
glm::dmat4(data.modelTransform.rotation) *
glm::scale(glm::dmat4(1.0), glm::dvec3(data.modelTransform.scale)) *
glm::dmat4(_modelTransform);
return data.camera.combinedViewMatrix() * modelTransform;
}
void BasicVolumeRaycaster::renderExitPoints(
const RenderData& data,
ghoul::opengl::ProgramObject& program)
{
program.setUniform("modelViewTransform", glm::mat4(modelViewTransform(data)));
program.setUniform("projectionTransform", data.camera.projectionMatrix());
// Cull front face
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
// Render bounding geometry
_boundingBox.render();
// Restore defaults
glCullFace(GL_BACK);
}
void BasicVolumeRaycaster::preRaycast(
const RaycastData& data,
ghoul::opengl::ProgramObject& program)
{
if (!_volumeTexture || !_transferFunction) {
return;
}
std::string stepSizeUniformName = "maxStepSize" + std::to_string(data.id);
program.setUniform(stepSizeUniformName, _stepSize);
std::string id = std::to_string(data.id);
_tfUnit = std::make_unique<ghoul::opengl::TextureUnit>();
_tfUnit->activate();
_transferFunction->getTexture().bind();
program.setUniform("transferFunction_" + id, _tfUnit->unitNumber());
_textureUnit = std::make_unique<ghoul::opengl::TextureUnit>();
_textureUnit->activate();
_volumeTexture->bind();
program.setUniform("volumeTexture_" + id, _textureUnit->unitNumber());
program.setUniform("gridType_" + id, static_cast<int>(_gridType));
std::vector<glm::vec3> clipNormals = _clipPlanes->normals();
std::vector<glm::vec2> clipOffsets = _clipPlanes->offsets();
int nClips = static_cast<int>(clipNormals.size());
program.setUniform("nClips_" + id, nClips);
program.setUniform("clipNormals_" + id, clipNormals.data(), nClips);
program.setUniform("clipOffsets_" + id, clipOffsets.data(), nClips);
program.setUniform("opacity_" + id, _opacity);
program.setUniform("rNormalization_" + id, _rNormalization);
program.setUniform("rUpperBound_" + id, _rUpperBound);
program.setUniform("valueRemapping_" + id, _valueRemapping);
}
void BasicVolumeRaycaster::postRaycast(const RaycastData&, ghoul::opengl::ProgramObject&)
{
// For example: release texture units
_textureUnit = nullptr;
_tfUnit = nullptr;
}
bool BasicVolumeRaycaster::cameraIsInside(
const RenderData & data,
glm::vec3 & localPosition)
{
glm::vec4 modelPos =
glm::inverse(modelViewTransform(data)) * glm::vec4(0.0, 0.0, 0.0, 1.0);
localPosition = (glm::vec3(modelPos) + glm::vec3(0.5));
return (localPosition.x > 0 && localPosition.x < 1 &&
localPosition.y > 0 && localPosition.y < 1 &&
localPosition.z > 0 && localPosition.z < 1);
}
std::string BasicVolumeRaycaster::getBoundsVsPath() const {
return GlslBoundsVsPath;
}
std::string BasicVolumeRaycaster::getBoundsFsPath() const {
return GlslBoundsFsPath;
}
std::string BasicVolumeRaycaster::getRaycastPath() const {
return GlslRaycastPath;
}
std::string BasicVolumeRaycaster::getHelperPath() const {
return GlslHelperPath;
}
void BasicVolumeRaycaster::setTransferFunction(
std::shared_ptr<TransferFunction> transferFunction)
{
_transferFunction = transferFunction;
}
void BasicVolumeRaycaster::setVolumeTexture(
std::shared_ptr<ghoul::opengl::Texture> volumeTexture)
{
_volumeTexture = volumeTexture;
}
std::shared_ptr<ghoul::opengl::Texture> BasicVolumeRaycaster::volumeTexture() const {
return _volumeTexture;
}
void BasicVolumeRaycaster::setStepSize(float stepSize) {
_stepSize = stepSize;
}
void BasicVolumeRaycaster::setOpacity(float opacity) {
_opacity = opacity;
}
float BasicVolumeRaycaster::opacity() const {
return _opacity;
}
void BasicVolumeRaycaster::setRNormalization(float rNormalization) {
_rNormalization = rNormalization;
}
float BasicVolumeRaycaster::rNormalization() const {
return _rNormalization;
}
void BasicVolumeRaycaster::setRUpperBound(float rUpperBound) {
_rUpperBound = rUpperBound;
}
float BasicVolumeRaycaster::rUpperBound() const {
return _rUpperBound;
}
void BasicVolumeRaycaster::setValueRemapping(float mapZeroTo, float mapOneTo) {
_valueRemapping = glm::vec2(mapZeroTo, mapOneTo);
}
VolumeGridType BasicVolumeRaycaster::gridType() const {
return _gridType;
}
void BasicVolumeRaycaster::setGridType(VolumeGridType gridType) {
_gridType = gridType;
}
void BasicVolumeRaycaster::setModelTransform(const glm::mat4 & transform) {
_modelTransform = transform;
}
} // namespace volume
} // namespace openspace
@@ -0,0 +1,114 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_VOLUME___BASICVOLUMERAYCASTER___H__
#define __OPENSPACE_MODULE_VOLUME___BASICVOLUMERAYCASTER___H__
#include <string>
#include <vector>
#include <memory>
#include <ghoul/glm.h>
#include <ghoul/opengl/texture.h>
#include <openspace/rendering/volumeraycaster.h>
#include <openspace/util/boxgeometry.h>
#include <openspace/rendering/transferfunction.h>
#include <modules/volume/rendering/volumeclipplanes.h>
#include <modules/volume/volumegridtype.h>
namespace ghoul::opengl {
class Texture;
class ProgramObject;
class TextureUnit;
}
namespace openspace {
struct RenderData;
struct RaycastData;
namespace volume {
class BasicVolumeRaycaster : public VolumeRaycaster {
public:
BasicVolumeRaycaster(
std::shared_ptr<ghoul::opengl::Texture> texture,
std::shared_ptr<TransferFunction> transferFunction,
std::shared_ptr<VolumeClipPlanes> clipPlanes);
virtual ~BasicVolumeRaycaster();
void initialize();
void deinitialize();
void renderEntryPoints(const RenderData& data, ghoul::opengl::ProgramObject& program) override;
void renderExitPoints(const RenderData& data, ghoul::opengl::ProgramObject& program) override;
void preRaycast(const RaycastData& data, ghoul::opengl::ProgramObject& program) override;
void postRaycast(const RaycastData& data, ghoul::opengl::ProgramObject& program) override;
bool cameraIsInside(const RenderData& data, glm::vec3& localPosition) override;
std::string getBoundsVsPath() const override;
std::string getBoundsFsPath() const override;
std::string getRaycastPath() const override;
std::string getHelperPath() const override;
void setVolumeTexture(std::shared_ptr<ghoul::opengl::Texture> texture);
std::shared_ptr<ghoul::opengl::Texture> volumeTexture() const;
void setTransferFunction(std::shared_ptr<TransferFunction> transferFunction);
void setStepSize(float stepSize);
float opacity() const;
void setOpacity(float opacity);
float rNormalization() const;
void setRNormalization(float rNormalization);
float rUpperBound() const;
void setRUpperBound(float rNormalization);
void setValueRemapping(float mapZeroTo, float mapOneTo);
VolumeGridType gridType() const;
void setGridType(VolumeGridType gridType);
void setModelTransform(const glm::mat4& transform);
private:
glm::dmat4 modelViewTransform(const RenderData& data);
std::shared_ptr<VolumeClipPlanes> _clipPlanes;
std::shared_ptr<ghoul::opengl::Texture> _volumeTexture;
std::shared_ptr<TransferFunction> _transferFunction;
BoxGeometry _boundingBox;
VolumeGridType _gridType;
glm::mat4 _modelTransform;
float _opacity;
float _rNormalization;
float _rUpperBound;
glm::vec2 _valueRemapping;
std::unique_ptr<ghoul::opengl::TextureUnit> _tfUnit;
std::unique_ptr<ghoul::opengl::TextureUnit> _textureUnit;
float _stepSize;
};
} // namespace volume
} // namespace openspace
#endif // __OPENSPACE_MODULE_VOLUME___BASICVOLUMERAYCASTER___H__
@@ -0,0 +1,473 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#include <modules/volume/rendering/renderabletimevaryingvolume.h>
#include <modules/volume/rawvolumereader.h>
#include <modules/volume/rawvolume.h>
#include <openspace/rendering/renderable.h>
#include <openspace/engine/openspaceengine.h>
#include <openspace/rendering/renderengine.h>
#include <openspace/rendering/raycastermanager.h>
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
#include <openspace/util/timemanager.h>
#include <openspace/util/time.h>
#include <ghoul/glm.h>
#include <ghoul/opengl/ghoul_gl.h>
#include <ghoul/filesystem/filesystem.h>
#include <ghoul/filesystem/cachemanager.h>
#include <ghoul/logging/logmanager.h>
#include <glm/gtc/matrix_transform.hpp>
namespace {
const char* _loggerCat = "RenderableTimeVaryingVolume";
}
namespace {
const char* KeyDimensions = "Dimensions";
const char* KeyStepSize = "StepSize";
const char* KeyTransferFunction = "TransferFunction";
const char* KeySourceDirectory = "SourceDirectory";
const char* KeyLowerDomainBound = "LowerDomainBound";
const char* KeyUpperDomainBound = "UpperDomainBound";
const char* KeyLowerValueBound = "LowerValueBound";
const char* KeyUpperValueBound = "UpperValueBound";
const char* KeyClipPlanes = "ClipPlanes";
const char* KeySecondsBefore = "SecondsBefore";
const char* KeySecondsAfter = "SecondsAfter";
const char* KeyGridType = "GridType";
const char* KeyMinValue = "MinValue";
const char* KeyMaxValue = "MaxValue";
const char* KeyTime = "Time";
const float SecondsInOneDay = 60 * 60 * 24;
}
namespace openspace {
namespace volume {
RenderableTimeVaryingVolume::RenderableTimeVaryingVolume(const ghoul::Dictionary& dictionary)
: Renderable(dictionary)
, _clipPlanes(nullptr)
, _stepSize({ "stepSize", "Step Size", "" }, 0.02, 0.01, 1)
, _gridType({ "gridType", "Grid Type", "" }, properties::OptionProperty::DisplayType::Dropdown)
, _secondsBefore({ "secondsBefore", "Seconds before", "" }, 0.0, 0.01, SecondsInOneDay)
, _secondsAfter({ "secondsAfter", "Seconds after", "" }, 0.0, 0.01, SecondsInOneDay)
, _sourceDirectory({ "sourceDirectory", "Source Directory", "" })
, _transferFunctionPath({"transferFunctionPath", "Transfer Function Path", "" })
, _triggerTimeJump({"triggerTimeJump", "Jump", "" })
, _jumpToTimestep({"jumpToTimestep", "Jump to timestep", "" }, 0, 0, 256)
, _currentTimestep({"currentTimestep", "Current timestep", "" }, 0, 0, 256)
, _opacity({"opacity", "Opacity", "" }, 10.0f, 0.0f, 50.0f)
, _rNormalization({"rNormalization", "Radius normalization", "" }, 0.0f, 0.0f, 2.0f)
, _rUpperBound({"rUpperBound", "Radius upper bound", "" }, 1.0f, 0.0f, 2.0f)
, _lowerValueBound({"lowerValueBound", "Lower value bound", "" }, 0.0f, 0.0f, 1000000.0f)
, _upperValueBound({"upperValueBound", "Upper value bound", "" }, 0.0f, 0.0f, 1000000.0f)
, _raycaster(nullptr)
, _transferFunction(nullptr)
{
documentation::testSpecificationAndThrow(
Documentation(),
dictionary,
"RenderableTimeVaryingVolume"
);
_sourceDirectory = absPath(dictionary.value<std::string>(KeySourceDirectory));
_transferFunctionPath = absPath(dictionary.value<std::string>(KeyTransferFunction));
_lowerValueBound = dictionary.value<float>(KeyLowerValueBound);
_upperValueBound = dictionary.value<float>(KeyUpperValueBound);
_transferFunction = std::make_shared<TransferFunction>(_transferFunctionPath);
_gridType.addOption(static_cast<int>(volume::VolumeGridType::Cartesian), "Cartesian grid");
_gridType.addOption(static_cast<int>(volume::VolumeGridType::Spherical), "Spherical grid");
_gridType.setValue(static_cast<int>(volume::VolumeGridType::Cartesian));
if (dictionary.hasValue<float>(KeySecondsBefore)) {
_secondsBefore = dictionary.value<float>(KeySecondsBefore);
}
_secondsAfter = dictionary.value<float>(KeySecondsAfter);
ghoul::Dictionary clipPlanesDictionary;
dictionary.getValue(KeyClipPlanes, clipPlanesDictionary);
_clipPlanes = std::make_shared<volume::VolumeClipPlanes>(clipPlanesDictionary);
_clipPlanes->setName("clipPlanes");
if (dictionary.hasValue<std::string>(KeyGridType)) {
VolumeGridType gridType = volume::parseGridType(dictionary.value<std::string>(KeyGridType));
_gridType = (gridType == VolumeGridType::Spherical) ? 1 : 0;
}
}
RenderableTimeVaryingVolume::~RenderableTimeVaryingVolume() {}
void RenderableTimeVaryingVolume::initialize() {
using RawPath = ghoul::filesystem::Directory::RawPath;
ghoul::filesystem::Directory sequenceDir(_sourceDirectory, RawPath::Yes);
if (!FileSys.directoryExists(sequenceDir)) {
LERROR("Could not load sequence directory '" << sequenceDir.path() << "'");
return;
}
using Recursive = ghoul::filesystem::Directory::Recursive;
using Sort = ghoul::filesystem::Directory::Sort;
std::vector<std::string> sequencePaths = sequenceDir.read(Recursive::Yes, Sort::No);
for (auto path : sequencePaths) {
ghoul::filesystem::File currentFile(path);
std::string extension = currentFile.fileExtension();
if (extension == "dictionary") {
loadTimestepMetadata(path);
}
}
// TODO: defer loading of data to later. (separate thread or at least not when loading)
for (auto& p : _volumeTimesteps) {
Timestep& t = p.second;
std::string path = FileSys.pathByAppendingComponent(_sourceDirectory, t.baseName) + ".rawvolume";
RawVolumeReader<float> reader(path, t.dimensions);
t.rawVolume = reader.read();
float min = t.minValue;
float diff = t.maxValue - t.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.0f, 1.0f);
}
// TODO: handle normalization properly for different timesteps + transfer function
t.texture = std::make_shared<ghoul::opengl::Texture>(
t.dimensions,
ghoul::opengl::Texture::Format::Red,
GL_RED,
GL_FLOAT,
ghoul::opengl::Texture::FilterMode::Linear,
ghoul::opengl::Texture::WrappingMode::Clamp
);
t.texture->setPixelData(reinterpret_cast<void*>(data), ghoul::opengl::Texture::TakeOwnership::No);
t.texture->uploadTexture();
}
_clipPlanes->initialize();
_transferFunction->update();
_raycaster = std::make_unique<volume::BasicVolumeRaycaster>(nullptr, _transferFunction, _clipPlanes);
_raycaster->initialize();
OsEng.renderEngine().raycasterManager().attachRaycaster(*_raycaster.get());
auto onChange = [&](bool enabled) {
if (enabled) {
OsEng.renderEngine().raycasterManager().attachRaycaster(*_raycaster.get());
} else {
OsEng.renderEngine().raycasterManager().detachRaycaster(*_raycaster.get());
}
};
onEnabledChange(onChange);
_triggerTimeJump.onChange([this] () {
jumpToTimestep(_jumpToTimestep);
});
_jumpToTimestep.onChange([this] () {
jumpToTimestep(_jumpToTimestep);
});
const int lastTimestep = (_volumeTimesteps.size() > 0) ? (_volumeTimesteps.size() - 1) : 0;
_currentTimestep.setMaxValue(lastTimestep);
_jumpToTimestep.setMaxValue(lastTimestep);
addProperty(_stepSize);
addProperty(_transferFunctionPath);
addProperty(_sourceDirectory);
addPropertySubOwner(_clipPlanes.get());
addProperty(_triggerTimeJump);
addProperty(_jumpToTimestep);
addProperty(_currentTimestep);
addProperty(_opacity);
addProperty(_rNormalization);
addProperty(_rUpperBound);
addProperty(_lowerValueBound);
addProperty(_upperValueBound);
_raycaster->setGridType((_gridType.value() == 1) ? VolumeGridType::Spherical : VolumeGridType::Cartesian);
_gridType.onChange([this] {
_raycaster->setGridType((_gridType.value() == 1) ? VolumeGridType::Spherical : VolumeGridType::Cartesian);
});
}
void RenderableTimeVaryingVolume::loadTimestepMetadata(const std::string& path) {
ghoul::Dictionary dictionary = ghoul::lua::loadDictionaryFromFile(path);
try {
documentation::testSpecificationAndThrow(TimestepDocumentation(), dictionary, "TimeVaryingVolumeTimestep");
} catch (const documentation::SpecificationError& e) {
LERROR(e.message << e.component);
return;
}
Timestep t;
t.baseName = ghoul::filesystem::File(path).baseName();
t.dimensions = dictionary.value<glm::vec3>(KeyDimensions);
t.lowerDomainBound = dictionary.value<glm::vec3>(KeyLowerDomainBound);
t.upperDomainBound = dictionary.value<glm::vec3>(KeyUpperDomainBound);
t.minValue = dictionary.value<float>(KeyMinValue);
t.maxValue = dictionary.value<float>(KeyMaxValue);
std::string timeString = dictionary.value<std::string>(KeyTime);
t.time = Time::convertTime(timeString);
t.inRam = false;
t.onGpu = false;
_volumeTimesteps[t.time] = std::move(t);
}
RenderableTimeVaryingVolume::Timestep* RenderableTimeVaryingVolume::currentTimestep() {
if (_volumeTimesteps.size() == 0) {
return nullptr;
}
double currentTime = OsEng.timeManager().time().j2000Seconds();
// Get the first item with time > currentTime
auto currentTimestepIt = _volumeTimesteps.upper_bound(currentTime);
if (currentTimestepIt == _volumeTimesteps.end()) {
// No such timestep was found: show last timestep if it is within the time margin.
RenderableTimeVaryingVolume::Timestep* lastTimestep = &(_volumeTimesteps.rbegin()->second);
double threshold = lastTimestep->time + static_cast<double>(_secondsAfter);
return currentTime < threshold ? lastTimestep : nullptr;
}
if (currentTimestepIt == _volumeTimesteps.begin()) {
// No such timestep was found: show first timestep if it is within the time margin.
RenderableTimeVaryingVolume::Timestep* firstTimestep = &(_volumeTimesteps.begin()->second);
double threshold = firstTimestep->time - static_cast<double>(_secondsBefore);
return currentTime >= threshold ? firstTimestep : nullptr;
}
// Get the last item with time <= currentTime
currentTimestepIt--;
return &(currentTimestepIt->second);
}
int RenderableTimeVaryingVolume::timestepIndex(const RenderableTimeVaryingVolume::Timestep* t) const {
if (!t) {
return -1;
}
int index = 0;
for (auto& it : _volumeTimesteps) {
if (&(it.second) == t) {
return index;
}
++index;
}
return -1;
}
RenderableTimeVaryingVolume::Timestep* RenderableTimeVaryingVolume::timestepFromIndex(int target) {
if (target < 0) target = 0;
int index = 0;
for (auto& it : _volumeTimesteps) {
if (index == target) {
return &(it.second);
}
++index;
}
return nullptr;
}
void RenderableTimeVaryingVolume::jumpToTimestep(int target) {
Timestep* t = timestepFromIndex(target);
if (!t) {
return;
}
OsEng.timeManager().setTimeNextFrame(t->time);
}
void RenderableTimeVaryingVolume::update(const UpdateData& data) {
if (_raycaster) {
Timestep* t = currentTimestep();
_currentTimestep = timestepIndex(t);
if (t && t->texture) {
if (_raycaster->gridType() == volume::VolumeGridType::Cartesian) {
glm::dvec3 scale = t->upperDomainBound - t->lowerDomainBound;
glm::dvec3 translation = (t->lowerDomainBound + t->upperDomainBound) * 0.5f;
glm::dmat4 modelTransform = glm::translate(glm::dmat4(1.0), translation);
glm::dmat4 scaleMatrix = glm::scale(glm::dmat4(1.0), scale);
modelTransform = modelTransform * scaleMatrix;
_raycaster->setModelTransform(glm::mat4(modelTransform));
} else {
_raycaster->setModelTransform(
glm::scale(
glm::dmat4(1.0),
glm::dvec3(t->upperDomainBound[0])
)
);
}
_raycaster->setVolumeTexture(t->texture);
// Remap volume value to that TF value 0 is sampled for lowerValueBound, and 1 is sampled for upperLowerBound.
// This means that volume values = 0 need to be remapped to how localMin relates to the global range.
float zeroMap = (t->minValue - _lowerValueBound) / (_upperValueBound - _lowerValueBound);
// Volume values = 1 are mapped to how localMax relates to the global range.
float oneMap = (t->maxValue - _lowerValueBound) / (_upperValueBound - _lowerValueBound);
_raycaster->setValueRemapping(zeroMap, oneMap);
} else {
_raycaster->setVolumeTexture(nullptr);
}
_raycaster->setStepSize(_stepSize);
_raycaster->setOpacity(_opacity);
_raycaster->setRNormalization(_rNormalization);
_raycaster->setRUpperBound(_rUpperBound);
}
}
void RenderableTimeVaryingVolume::render(const RenderData& data, RendererTasks& tasks) {
if (_raycaster && _raycaster->volumeTexture()) {
tasks.raycasterTasks.push_back({ _raycaster.get(), data });
}
}
bool RenderableTimeVaryingVolume::isReady() const {
return true;
}
void RenderableTimeVaryingVolume::deinitialize() {
if (_raycaster) {
OsEng.renderEngine().raycasterManager().detachRaycaster(*_raycaster.get());
_raycaster = nullptr;
}
}
documentation::Documentation RenderableTimeVaryingVolume::Documentation() {
using namespace documentation;
return {
"RenderableTimevaryingVolume",
"volume_renderable_timevaryingvolume",
{
{
KeySourceDirectory,
new StringVerifier,
Optional::No,
"Specifies the path to load timesteps from"
},
{
KeyTransferFunction,
new StringVerifier,
Optional::No,
"Specifies the transfer function file path",
},
{
KeyLowerValueBound,
new DoubleVerifier,
Optional::No,
"Specifies the lower value bound."
"This number will be mapped to 0 before uploadin to the GPU.",
},
{
KeyUpperValueBound,
new DoubleVerifier,
Optional::No,
"Specifies the lower value bound."
"This number will be mapped to 0 before uploadin to the GPU."
},
{
KeyGridType,
new StringInListVerifier({"Cartesian", "Spherical"}),
Optional::Yes,
"Specifies the grid type"
},
{
KeySecondsBefore,
new DoubleVerifier,
Optional::Yes,
"Specifies the number of seconds to show the the first timestep before its actual time."
"The default value is 0.",
},
{
KeySecondsAfter,
new DoubleVerifier,
Optional::No,
"Specifies the number of seconds to show the the last timestep after its actual time",
},
}
};
}
documentation::Documentation RenderableTimeVaryingVolume::TimestepDocumentation() {
using namespace documentation;
return {
"TimevaryingVolumeTimestep",
"volume_timevaryingvolumetimestep",
{
{
KeyLowerDomainBound,
new Vector3Verifier<float>,
Optional::No,
"Specifies the lower domain bounds in the model coordinate system",
},
{
KeyUpperDomainBound,
new Vector3Verifier<float>,
Optional::No,
"Specifies the upper domain bounds in the model coordinate system",
},
{
KeyDimensions,
new Vector3Verifier<float>,
Optional::No,
"Specifies the number of grid cells in each dimension",
},
{
KeyTime,
new StringVerifier,
Optional::No,
"Specifies the time on the format YYYY-MM-DDTHH:MM:SS.000Z",
},
{
KeyMinValue,
new DoubleVerifier,
Optional::No,
"Specifies the minimum value stored in the volume"
},
{
KeyMaxValue,
new DoubleVerifier,
Optional::No,
"Specifies the maximum value stored in the volume"
}
}
};
}
} // namespace volume
} // namespace openspace
@@ -0,0 +1,109 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2017 *
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
* software and associated documentation files (the "Software"), to deal in the Software *
* without restriction, including without limitation the rights to use, copy, modify, *
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
* permit persons to whom the Software is furnished to do so, subject to the following *
* conditions: *
* *
* The above copyright notice and this permission notice shall be included in all copies *
* or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
****************************************************************************************/
#ifndef __OPENSPACE_MODULE_VOLUME___RENDERABLEKAMELEONVOLUME___H__
#define __OPENSPACE_MODULE_VOLUME___RENDERABLEKAMELEONVOLUME___H__
#include <openspace/rendering/renderable.h>
#include <modules/volume/rawvolume.h>
#include <modules/volume/rendering/basicvolumeraycaster.h>
#include <modules/volume/rendering/volumeclipplanes.h>
#include <openspace/properties/vectorproperty.h>
#include <openspace/properties/optionproperty.h>
#include <openspace/properties/stringproperty.h>
#include <openspace/rendering/transferfunction.h>
#include <openspace/util/boxgeometry.h>
namespace openspace {
struct RenderData;
namespace volume {
class RenderableTimeVaryingVolume : public Renderable {
public:
RenderableTimeVaryingVolume(const ghoul::Dictionary& dictionary);
~RenderableTimeVaryingVolume();
void initialize() override;
void deinitialize() override;
bool isReady() const override;
void render(const RenderData& data, RendererTasks& tasks) override;
void update(const UpdateData& data) override;
static documentation::Documentation Documentation();
static documentation::Documentation TimestepDocumentation();
private:
struct Timestep {
std::string baseName;
double time;
float minValue;
float maxValue;
glm::uvec3 dimensions;
glm::vec3 lowerDomainBound;
glm::vec3 upperDomainBound;
bool inRam;
bool onGpu;
std::unique_ptr<RawVolume<float>> rawVolume;
std::shared_ptr<ghoul::opengl::Texture> texture;
};
Timestep* currentTimestep();
int timestepIndex(const Timestep* t) const;
Timestep* timestepFromIndex(int index);
void jumpToTimestep(int i);
void loadTimestepMetadata(const std::string& path);
properties::OptionProperty _gridType;
std::shared_ptr<VolumeClipPlanes> _clipPlanes;
properties::FloatProperty _stepSize;
properties::FloatProperty _opacity;
properties::FloatProperty _rNormalization;
properties::FloatProperty _rUpperBound;
properties::FloatProperty _secondsBefore;
properties::FloatProperty _secondsAfter;
properties::StringProperty _sourceDirectory;
properties::StringProperty _transferFunctionPath;
properties::FloatProperty _lowerValueBound;
properties::FloatProperty _upperValueBound;
properties::TriggerProperty _triggerTimeJump;
properties::IntProperty _jumpToTimestep;
properties::IntProperty _currentTimestep;
std::map<double, Timestep> _volumeTimesteps;
std::unique_ptr<BasicVolumeRaycaster> _raycaster;
std::shared_ptr<TransferFunction> _transferFunction;
};
} // namespace volume
} // namespace openspace
#endif // __OPENSPACE_MODULE_KAMELEONVOLUME___RENDERABLEKAMELEONVOLUME___H__
+2 -1
View File
@@ -1,4 +1,3 @@
/*****************************************************************************************
* *
* OpenSpace *
@@ -28,6 +27,7 @@
namespace openspace {
namespace volume {
VolumeClipPlane::VolumeClipPlane(const ghoul::Dictionary& dictionary)
: properties::PropertyOwner({ "" }) // @TODO Missing name
@@ -66,4 +66,5 @@ glm::vec2 VolumeClipPlane::offsets() {
return _offsets;
}
} // namespace volume
} // namespace openspace
+3 -1
View File
@@ -31,6 +31,7 @@
namespace ghoul { class Dictionary; }
namespace openspace {
namespace volume {
class VolumeClipPlane : public properties::PropertyOwner {
public:
@@ -45,6 +46,7 @@ private:
};
} // namespace openspace
} // namespace volume
} // namespace openspace
#endif // __OPENSPACE_MODULE_VOLUME___VOLUMECLIPPLANE___H__
@@ -28,6 +28,7 @@
namespace openspace {
namespace volume {
VolumeClipPlanes::VolumeClipPlanes(const ghoul::Dictionary& dictionary)
: properties::PropertyOwner({ "" }) // @TODO Missing name
@@ -70,4 +71,5 @@ std::vector<glm::vec2> VolumeClipPlanes::offsets() {
return offsets;
}
} // namespace volume
} // namespace openspace
+3 -1
View File
@@ -36,6 +36,7 @@
namespace ghoul { class Dictionary; }
namespace openspace {
namespace volume {
class VolumeClipPlanes : public properties::PropertyOwner {
public:
@@ -51,6 +52,7 @@ private:
std::vector<std::shared_ptr<VolumeClipPlane>> _clipPlanes;
};
} // namespace openspace
} // namepsace volume
} // namespace openspace
#endif // __OPENSPACE_MODULE_VOLUME___VOLUMECLIPPLANES___H__