iMSTK
Interactive Medical Simulation Toolkit
imstkSceneObject.cpp
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #include "imstkSceneObject.h"
8 #include "imstkGeometry.h"
9 #include "imstkLogger.h"
10 #include "imstkTaskGraph.h"
11 #include "imstkVisualModel.h"
12 
13 namespace imstk
14 {
15 SceneObject::SceneObject(const std::string& name) : Entity(name),
16  m_taskGraph(std::make_shared<TaskGraph>(
17  "SceneObject_" + m_name + "_Source",
18  "SceneObject_" + m_name + "_Sink"))
19 {
20  m_updateNode = m_taskGraph->addFunction("SceneObject_" + m_name + "_Update", [this]() { update(); });
21  m_updateGeometryNode = m_taskGraph->addFunction("SceneObject_" + m_name + "_UpdateGeometry", [this]() { updateGeometries(); });
22 }
23 
24 std::shared_ptr<Geometry>
26 {
27  auto visualModel = getComponent<VisualModel>();
28  if (visualModel != nullptr)
29  {
30  return visualModel->getGeometry();
31  }
32  return nullptr;
33 }
34 
35 void
36 SceneObject::setVisualGeometry(std::shared_ptr<Geometry> geometry)
37 {
38  auto iter = std::find_if(m_components.begin(), m_components.end(),
39  [](std::shared_ptr<Component> comp)
40  {
41  return std::dynamic_pointer_cast<VisualModel>(comp) != nullptr;
42  });
43  if (iter != m_components.end())
44  {
45  std::dynamic_pointer_cast<VisualModel>(*iter)->setGeometry(geometry);
46  }
47  else
48  {
49  auto visualModel = addComponent<VisualModel>();
50  visualModel->setName(m_name + "_VisualModel");
51  visualModel->setGeometry(geometry);
52  }
53 }
54 
55 std::shared_ptr<VisualModel>
56 SceneObject::getVisualModel(const int index) const
57 {
58  return getComponentN<VisualModel>(index);
59 }
60 
61 void
62 SceneObject::addVisualModel(std::shared_ptr<VisualModel> visualModel)
63 {
64  addComponent(visualModel);
65 }
66 
67 void
68 SceneObject::removeVisualModel(std::shared_ptr<VisualModel> visualModel)
69 {
70  removeComponent(visualModel);
71 }
72 
73 void
75 {
76  m_taskGraph->clearEdges();
77  initGraphEdges(m_taskGraph->getSource(), m_taskGraph->getSink());
78 }
79 
80 void
81 SceneObject::initGraphEdges(std::shared_ptr<TaskNode> source, std::shared_ptr<TaskNode> sink)
82 {
83  m_taskGraph->addEdge(source, m_updateNode);
84  m_taskGraph->addEdge(m_updateNode, m_updateGeometryNode);
85  m_taskGraph->addEdge(m_updateGeometryNode, sink);
86 }
87 
88 void
90 {
91  // Assume geometry may be changed upon reset
92  for (auto comp : m_components)
93  {
94  if (auto visualModel = std::dynamic_pointer_cast<VisualModel>(comp))
95  {
96  if (visualModel->getGeometry() != nullptr)
97  {
98  visualModel->getGeometry()->postModified();
99  }
100  }
101  }
102 }
103 } // namespace imstk
std::shared_ptr< Geometry > getVisualGeometry() const
Sets the visual geometry, adds (sets the first) VisualModel.
void removeComponent(std::shared_ptr< Component > component)
Remove component if it exists.
Definition: imstkEntity.cpp:51
Compound Geometry.
void update() override
Update the SceneObject, called during scene update.
std::shared_ptr< VisualModel > getVisualModel(const int index) const
Get/add visual model.
void updateGeometries() final
Update the physics geometry and the apply the maps (if defined)
std::shared_ptr< T > addComponent()
Create and return a component on this entity.
Definition: imstkEntity.h:55
std::shared_ptr< TaskGraph > m_taskGraph
Computational Graph.
std::string m_name
Not unique name.
Definition: imstkEntity.h:157
void initGraphEdges()
Initializes the edges of the SceneObject&#39;s computational graph.
virtual void postModifiedAll()
Posts modified for all geometries.
Contains geometric, material, and render information.