Adding nullptr check, error throwing to log, cleanup

This commit is contained in:
Lovisa Hassler
2020-01-10 00:25:37 +01:00
parent b99bcf9490
commit 311be5ac6a
4 changed files with 23 additions and 15 deletions

View File

@@ -38,4 +38,4 @@ create_new_module(
"VisLab"
vislab_module
${HEADER_FILES} ${SOURCE_FILES}
)
)

View File

@@ -24,6 +24,7 @@
#include <modules/vislab/rendering/renderabledistancelabel.h>
#include <ghoul/logging/logmanager.h>
#include <modules/base/rendering/renderablenodeline.h>
#include <openspace/documentation/documentation.h>
#include <openspace/documentation/verifier.h>
@@ -63,7 +64,7 @@ documentation::Documentation RenderableDistanceLabel::Documentation() {
RenderableDistanceLabel::RenderableDistanceLabel(const ghoul::Dictionary& dictionary)
: RenderableLabels(dictionary)
, _nodeLine(NodeLineInfo)
, _nodelineId(NodeLineInfo)
{
documentation::testSpecificationAndThrow(
Documentation(),
@@ -72,21 +73,28 @@ RenderableDistanceLabel::RenderableDistanceLabel(const ghoul::Dictionary& dictio
);
if (dictionary.hasKey(NodeLineInfo.identifier)) {
_nodeLine = dictionary.value<std::string>(NodeLineInfo.identifier);
addProperty(_nodeLine);
_nodelineId = dictionary.value<std::string>(NodeLineInfo.identifier);
addProperty(_nodelineId);
}
}
void RenderableDistanceLabel::update(const UpdateData& data) {
if (global::renderEngine.scene()->sceneGraphNode(_nodeLine)) {
SceneGraphNode* nodelineNode = global::renderEngine.scene()->sceneGraphNode(_nodelineId);
if (nodelineNode && !_errorThrown) {
// Calculate distance
SceneGraphNode* nodelineNode = global::renderEngine.scene()->sceneGraphNode(_nodeLine);
RenderableNodeLine* nodeline = dynamic_cast<RenderableNodeLine*>(nodelineNode->renderable());
if (!nodeline) {
LERROR("Expected renderable to be of type 'RenderableNodeLine'");
_errorThrown = true;
return;
}
double myDistance = nodeline->getDistance();
// format string
// Format string
float scale = getUnit(Kilometer);
std::string distanceText = std::to_string(std::round(myDistance / scale));
int pos = distanceText.find(".");
@@ -96,11 +104,15 @@ void RenderableDistanceLabel::update(const UpdateData& data) {
setLabelText(finalText);
// Update placement of label with transformation matrix
glm::dvec3 start = global::renderEngine.scene()->sceneGraphNode(nodeline->_start)->worldPosition();
glm::dvec3 end = global::renderEngine.scene()->sceneGraphNode(nodeline->_end)->worldPosition();
glm::dvec3 start = global::renderEngine.scene()->sceneGraphNode(nodeline->getStart())->worldPosition();
glm::dvec3 end = global::renderEngine.scene()->sceneGraphNode(nodeline->getEnd())->worldPosition();
glm::dvec3 goalPos = start + (end - start) / 2.0;
_transformationMatrix = glm::translate(glm::dmat4(1.0), goalPos);
}
else if (!_errorThrown) {
LERROR(fmt::format("There is no scenegraph node with id {}", _nodelineId));
_errorThrown = true;
}
}
} // namespace openspace

View File

@@ -39,13 +39,10 @@ public:
void update(const UpdateData& data) override;
static documentation::Documentation Documentation();
properties::StringProperty _nodeLine;
private:
properties::StringProperty _nodelineId;
bool _errorThrown = false;
};
} // namespace openspace
#endif // __OPENSPACE_MODULE_VISLAB___RENDERABLEDISTANCELABEL___H__

View File

@@ -25,7 +25,6 @@
#include <modules/vislab/vislabmodule.h>
namespace openspace {
constexpr const char* _loggerCat = "VisLab Module";
VisLabModule::VisLabModule() : OpenSpaceModule(Name) {
}