iMSTK
Interactive Medical Simulation Toolkit
imstkComponent.h
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 #pragma once
8 
9 #include "imstkLogger.h"
10 #include "imstkMacros.h"
11 #include "imstkTaskGraph.h"
12 
13 namespace imstk
14 {
15 class Entity;
16 
27 class Component
28 {
29 friend class Entity;
30 
31 protected:
32  Component(const std::string& name = "Component") : m_name(name) { }
33 
34 public:
35  virtual ~Component() = default;
36 
37  const std::string& getName() const { return m_name; }
38  void setName(const std::string& name) { m_name = name; }
39 
43  std::weak_ptr<Entity> getEntity() const { return m_entity; }
44 
49  void initialize();
50 
51 protected:
56  virtual void init() { }
57 
58  std::string m_name;
60  std::weak_ptr<Entity> m_entity;
61 };
62 
88 template<typename UpdateInfo>
89 class Behaviour : public Component
90 {
91 protected:
92  Behaviour(const std::string& name = "Behaviour") : Component(name) { }
93  Behaviour(const bool useTaskGraph, const std::string& name = "Behaviour") : Component(name)
94  {
95  if (useTaskGraph)
96  {
97  m_taskGraph = std::make_shared<TaskGraph>();
98 
99  // Set default names
100  m_taskGraph->getSource()->m_name = "Behavior_Source";
101  m_taskGraph->getSink()->m_name = "Behavior_Sink";
102  }
103  }
104 
105 public:
106  ~Behaviour() override = default;
107 
108  virtual void update(const UpdateInfo& imstkNotUsed(updateData)) { }
109  virtual void visualUpdate(const UpdateInfo& imstkNotUsed(updateData)) { }
110 
115  {
116  CHECK(m_taskGraph != nullptr) << "Tried to setup task graph edges but no TaskGraph exists";
117  m_taskGraph->clearEdges();
118  initGraphEdges(m_taskGraph->getSource(), m_taskGraph->getSink());
119  }
120 
121  std::shared_ptr<TaskGraph> getTaskGraph() const { return m_taskGraph; }
122 
123 protected:
126  // /// \param source, first node of the graph (does no function)
129  virtual void initGraphEdges(std::shared_ptr<TaskNode> imstkNotUsed(source),
130  std::shared_ptr<TaskNode> imstkNotUsed(sink)) { }
131 
132  std::shared_ptr<TaskGraph> m_taskGraph = nullptr;
133 };
134 
144 
149 {
150 public:
151  LambdaBehaviour(const std::string& name = "LambdaBehaviour") : Behaviour(name) { }
152  ~LambdaBehaviour() override = default;
153 
154  void update(const double& dt) override;
155  void visualUpdate(const double& dt) override;
156 
157  void setUpdate(std::function<void(const double& dt)> updateFunc) { m_updateFunc = updateFunc; }
158  void setVisualUpdate(std::function<void(const double& dt)> updateFunc) { m_visualUpdateFunc = updateFunc; }
159 
160 protected:
161  std::function<void(const double& dt)> m_updateFunc;
162  std::function<void(const double& dt)> m_visualUpdateFunc;
163 };
164 } // namespace imstk
std::weak_ptr< Entity > m_entity
Parent entity this component exists on.
void initialize()
Initialize the component, called at a later time after all component construction is complete...
Compound Geometry.
A Behaviour represents a single component system A template is used here for UpdateInfo to keep the C...
std::weak_ptr< Entity > getEntity() const
Get parent entity.
virtual void initGraphEdges(std::shared_ptr< TaskNode > imstkNotUsed(source), std::shared_ptr< TaskNode > imstkNotUsed(sink))
Setup the edges/connections of the TaskGraph.
virtual void init()
Initialize the component, called at a later time after all component construction is complete...
void initTaskGraphEdges()
Setup the edges/connections of the TaskGraph.
Top-level class for entities. Entities contain a collection of components which define how to be used...
Definition: imstkEntity.h:26
Represents a part of an entity, involved in a system. The component system is doubly linked meaning t...
A SceneBehaviour that can update via a lambda function.