From 4b9bed3b21fa21bd77853f2c8a1a36fbc81a402d Mon Sep 17 00:00:00 2001 From: Alexander Bock Date: Fri, 28 Oct 2016 11:52:44 +0200 Subject: [PATCH] Make Translation, Rotation, and Scaling available as Properties in SceneGraphNode --- include/openspace/scene/rotation.h | 8 +- include/openspace/scene/scenegraphnode.h | 15 +-- modules/base/rotation/spicerotation.cpp | 3 + modules/base/rotation/staticrotation.cpp | 1 + modules/base/translation/spicetranslation.cpp | 3 + src/scene/rotation.cpp | 4 +- src/scene/scale.cpp | 1 + src/scene/scenegraphnode.cpp | 126 +++++++----------- src/scene/scenegraphnode_doc.inl | 6 +- src/scene/translation.cpp | 3 +- 10 files changed, 75 insertions(+), 95 deletions(-) diff --git a/include/openspace/scene/rotation.h b/include/openspace/scene/rotation.h index de12533517..0bcb1b0df2 100644 --- a/include/openspace/scene/rotation.h +++ b/include/openspace/scene/rotation.h @@ -25,14 +25,16 @@ #ifndef __ROTATION_H__ #define __ROTATION_H__ -#include -#include +#include #include +#include + +#include namespace openspace { -class Rotation { +class Rotation : public properties::PropertyOwner { public: static Rotation* createFromDictionary(const ghoul::Dictionary& dictionary); diff --git a/include/openspace/scene/scenegraphnode.h b/include/openspace/scene/scenegraphnode.h index 365e8662be..f9276e872f 100644 --- a/include/openspace/scene/scenegraphnode.h +++ b/include/openspace/scene/scenegraphnode.h @@ -101,13 +101,6 @@ public: const Renderable* renderable() const; Renderable* renderable(); - // @TODO Remove once the scalegraph is in effect ---abock - - void setEphemeris(Translation* eph) { - delete _translation; - _translation = eph; - } - static documentation::Documentation Documentation(); private: @@ -129,9 +122,11 @@ private: PowerScaledScalar _boundingSphere; // Transformation defined by ephemeris, rotation and scale - Translation* _translation; - Rotation* _rotation; - Scale* _scale; + struct { + std::unique_ptr translation; + std::unique_ptr rotation; + std::unique_ptr scale; + } _transform; // Cached transform data glm::dvec3 _worldPositionCached; diff --git a/modules/base/rotation/spicerotation.cpp b/modules/base/rotation/spicerotation.cpp index a1adcdc2fa..d671f76d6c 100644 --- a/modules/base/rotation/spicerotation.cpp +++ b/modules/base/rotation/spicerotation.cpp @@ -110,6 +110,9 @@ SpiceRotation::SpiceRotation(const ghoul::Dictionary& dictionary) SpiceManager::ref().loadKernel(kernel); } } + + addProperty(_sourceFrame); + addProperty(_destinationFrame); } void SpiceRotation::update(const UpdateData& data) { diff --git a/modules/base/rotation/staticrotation.cpp b/modules/base/rotation/staticrotation.cpp index f44e768c6e..85e528060b 100644 --- a/modules/base/rotation/staticrotation.cpp +++ b/modules/base/rotation/staticrotation.cpp @@ -83,6 +83,7 @@ StaticRotation::StaticRotation(const ghoul::Dictionary& dictionary) _rotationMatrix = dictionary.value(KeyRotation); } + addProperty(_rotationMatrix); _rotationMatrix.onChange([this]() { _matrix = _rotationMatrix; }); } diff --git a/modules/base/translation/spicetranslation.cpp b/modules/base/translation/spicetranslation.cpp index dbb6060933..2fd3942c2a 100644 --- a/modules/base/translation/spicetranslation.cpp +++ b/modules/base/translation/spicetranslation.cpp @@ -130,6 +130,9 @@ SpiceTranslation::SpiceTranslation(const ghoul::Dictionary& dictionary) } } } + + addProperty(_target); + addProperty(_origin); } glm::dvec3 SpiceTranslation::position() const { diff --git a/src/scene/rotation.cpp b/src/scene/rotation.cpp index e098274235..ac814c8ec2 100644 --- a/src/scene/rotation.cpp +++ b/src/scene/rotation.cpp @@ -69,7 +69,9 @@ Rotation* Rotation::createFromDictionary(const ghoul::Dictionary& dictionary) { return result; } -Rotation::Rotation() {} +Rotation::Rotation() { + setName("Rotation"); +} Rotation::Rotation(const ghoul::Dictionary& dictionary) {} diff --git a/src/scene/scale.cpp b/src/scene/scale.cpp index c58c83ead6..ef43e0b4f6 100644 --- a/src/scene/scale.cpp +++ b/src/scene/scale.cpp @@ -64,6 +64,7 @@ Scale* Scale::createFromDictionary(const ghoul::Dictionary& dictionary) { auto factory = FactoryManager::ref().factory(); Scale* result = factory->create(scaleType, dictionary); + result->setName("Scale"); if (result == nullptr) { LERROR("Failed creating Scale object of type '" << scaleType << "'"); return nullptr; diff --git a/src/scene/scenegraphnode.cpp b/src/scene/scenegraphnode.cpp index a87e7a9b52..aa4e252727 100644 --- a/src/scene/scenegraphnode.cpp +++ b/src/scene/scenegraphnode.cpp @@ -22,7 +22,6 @@ * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * ****************************************************************************************/ -// open space includes #include #include @@ -31,7 +30,6 @@ #include #include -// ghoul includes #include #include #include @@ -77,13 +75,7 @@ SceneGraphNode* SceneGraphNode::createFromDictionary(const ghoul::Dictionary& di SceneGraphNode* result = new SceneGraphNode; - if (!dictionary.hasValue(KeyName)) { - LERROR("SceneGraphNode did not contain a '" << KeyName << "' key"); - delete result; - return nullptr; - } - std::string name; - dictionary.getValue(KeyName, name); + std::string name = dictionary.value(KeyName); result->setName(name); if (dictionary.hasValue(KeyRenderable)) { @@ -106,60 +98,48 @@ SceneGraphNode* SceneGraphNode::createFromDictionary(const ghoul::Dictionary& di if (dictionary.hasKey(keyTransformTranslation)) { ghoul::Dictionary translationDictionary; dictionary.getValue(keyTransformTranslation, translationDictionary); - result->_translation = - (Translation::createFromDictionary(translationDictionary)); - if (result->_translation == nullptr) { + result->_transform.translation = + std::unique_ptr(Translation::createFromDictionary(translationDictionary)); + if (result->_transform.translation == nullptr) { LERROR("Failed to create ephemeris for SceneGraphNode '" << result->name() << "'"); delete result; return nullptr; } + result->addPropertySubOwner(result->_transform.translation.get()); LDEBUG("Successfully created ephemeris for '" << result->name() << "'"); } if (dictionary.hasKey(keyTransformRotation)) { ghoul::Dictionary rotationDictionary; dictionary.getValue(keyTransformRotation, rotationDictionary); - result->_rotation = - (Rotation::createFromDictionary(rotationDictionary)); - if (result->_rotation == nullptr) { + result->_transform.rotation = + std::unique_ptr(Rotation::createFromDictionary(rotationDictionary)); + if (result->_transform.rotation == nullptr) { LERROR("Failed to create rotation for SceneGraphNode '" << result->name() << "'"); delete result; return nullptr; } + result->addPropertySubOwner(result->_transform.rotation.get()); LDEBUG("Successfully created rotation for '" << result->name() << "'"); } if (dictionary.hasKey(keyTransformScale)) { ghoul::Dictionary scaleDictionary; dictionary.getValue(keyTransformScale, scaleDictionary); - result->_scale = - (Scale::createFromDictionary(scaleDictionary)); - if (result->_scale == nullptr) { + result->_transform.scale = + std::unique_ptr(Scale::createFromDictionary(scaleDictionary)); + if (result->_transform.scale == nullptr) { LERROR("Failed to create scale for SceneGraphNode '" << result->name() << "'"); delete result; return nullptr; } + result->addPropertySubOwner(result->_transform.scale.get()); LDEBUG("Successfully created scale for '" << result->name() << "'"); } - //std::string parentName; - //if (!dictionary.getValue(KeyParentName, parentName)) { - // LWARNING("Could not find '" << KeyParentName << "' key, using 'Root'."); - // parentName = "Root"; - //} - - //SceneGraphNode* parentNode = sceneGraphNode(parentName); - //if (parentNode == nullptr) { - // LFATAL("Could not find parent named '" - // << parentName << "' for '" << result->name() << "'." - // << " Check module definition order. Skipping module."); - //} - - //parentNode->addNode(result); - LDEBUG("Successfully created SceneGraphNode '" << result->name() << "'"); return result; @@ -167,9 +147,11 @@ SceneGraphNode* SceneGraphNode::createFromDictionary(const ghoul::Dictionary& di SceneGraphNode::SceneGraphNode() : _parent(nullptr) - , _translation(new StaticTranslation()) - , _rotation(new StaticRotation()) - , _scale(new StaticScale()) + , _transform { + std::make_unique(), + std::make_unique(), + std::make_unique() + } , _performanceRecord({0, 0, 0}) , _renderable(nullptr) , _renderableVisible(false) @@ -182,15 +164,20 @@ SceneGraphNode::~SceneGraphNode() { } bool SceneGraphNode::initialize() { - if (_renderable) + if (_renderable) { _renderable->initialize(); + } - if (_translation) - _translation->initialize(); - if (_rotation) - _rotation->initialize(); - if (_scale) - _scale->initialize(); + if (_transform.translation) { + _transform.translation->initialize(); + } + + if (_transform.rotation) { + _transform.rotation->initialize(); + } + if (_transform.scale) { + _transform.scale->initialize(); + } return true; } @@ -203,26 +190,6 @@ bool SceneGraphNode::deinitialize() { delete _renderable; _renderable = nullptr; } - if (_translation) { - delete _translation; - _translation = nullptr; - } - if (_rotation) { - delete _rotation; - _rotation = nullptr; - } - if (_scale) { - delete _scale; - _scale = nullptr; - } - - //delete _ephemeris; - //_ephemeris = nullptr; - - // for (SceneGraphNode* child : _children) { - // child->deinitialize(); - // delete child; - //} _children.clear(); // reset variables @@ -235,49 +202,52 @@ bool SceneGraphNode::deinitialize() { } void SceneGraphNode::update(const UpdateData& data) { - if (_translation) { + if (_transform.translation) { if (data.doPerformanceMeasurement) { glFinish(); auto start = std::chrono::high_resolution_clock::now(); - _translation->update(data); + _transform.translation->update(data); glFinish(); auto end = std::chrono::high_resolution_clock::now(); _performanceRecord.updateTimeEphemeris = (end - start).count(); } - else - _translation->update(data); + else { + _transform.translation->update(data); + } } - if (_rotation) { + if (_transform.rotation) { if (data.doPerformanceMeasurement) { glFinish(); auto start = std::chrono::high_resolution_clock::now(); - _rotation->update(data); + _transform.rotation->update(data); glFinish(); auto end = std::chrono::high_resolution_clock::now(); _performanceRecord.updateTimeEphemeris = (end - start).count(); } - else - _rotation->update(data); + else { + _transform.rotation->update(data); + } } - if (_scale) { + if (_transform.scale) { if (data.doPerformanceMeasurement) { glFinish(); auto start = std::chrono::high_resolution_clock::now(); - _scale->update(data); + _transform.scale->update(data); glFinish(); auto end = std::chrono::high_resolution_clock::now(); _performanceRecord.updateTimeEphemeris = (end - start).count(); } - else - _scale->update(data); + else { + _transform.scale->update(data); + } } UpdateData newUpdateData = data; @@ -431,17 +401,17 @@ void SceneGraphNode::addChild(SceneGraphNode* child) { glm::dvec3 SceneGraphNode::position() const { - return _translation->position(); + return _transform.translation->position(); } const glm::dmat3& SceneGraphNode::rotationMatrix() const { - return _rotation->matrix(); + return _transform.rotation->matrix(); } double SceneGraphNode::scale() const { - return _scale->scaleValue(); + return _transform.scale->scaleValue(); } glm::dvec3 SceneGraphNode::worldPosition() const diff --git a/src/scene/scenegraphnode_doc.inl b/src/scene/scenegraphnode_doc.inl index 27b6ba7293..ba183dd42f 100644 --- a/src/scene/scenegraphnode_doc.inl +++ b/src/scene/scenegraphnode_doc.inl @@ -39,7 +39,8 @@ Documentation SceneGraphNode::Documentation() { "The name of this scenegraph node. This name must be unique among all scene " "graph nodes that are loaded in a specific scene. If a duplicate is detected " "the loading of the node will fail, as will all childing that depend on the " - "node." + "node.", + Optional::No }, { "Parent", @@ -49,7 +50,8 @@ Documentation SceneGraphNode::Documentation() { "This names the parent of the currently specified scenegraph node. The " "parent must not have been defined earlier, but must exist at loading time, " "or the scenegraph node creation will fail. A special parent 'Root' is " - "available that denotes the root of the scenegraph." + "available that denotes the root of the scenegraph.", + Optional::No }, { "Renderable", diff --git a/src/scene/translation.cpp b/src/scene/translation.cpp index b355f50163..ad0cbcafef 100644 --- a/src/scene/translation.cpp +++ b/src/scene/translation.cpp @@ -39,7 +39,7 @@ namespace openspace { Documentation Translation::Documentation() { using namespace openspace::documentation; - return{ + return { "Transformation Translation", "core_transform_translation", { @@ -68,6 +68,7 @@ Translation* Translation::createFromDictionary(const ghoul::Dictionary& dictiona ghoul::TemplateFactory* factory = FactoryManager::ref().factory(); Translation* result = factory->create(translationType, dictionary); + result->setName("Translation"); if (result == nullptr) { LERROR("Failed creating Translation object of type '" << translationType << "'"); return nullptr;