iMSTK
Interactive Medical Simulation Toolkit
imstkEntity.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 "imstkEventObject.h"
10 #include "imstkLogger.h"
11 
12 #include <atomic>
13 
14 namespace imstk
15 {
16 using EntityID = std::uint32_t;
17 
18 class Component;
19 
26 class Entity : public EventObject, public std::enable_shared_from_this<Entity>
27 {
28 public:
29  Entity(const std::string& name = "Entity");
30  ~Entity() override = default;
31 
32  // *INDENT-OFF*
33  SIGNAL(Entity, modified);
34  // *INDENT-ON*
35 
40  EntityID getID() const { return m_ID; }
41 
42  virtual const std::string getTypeName() const { return "Entity"; }
43 
47  const std::string& getName() const { return m_name; }
48  void setName(const std::string& name) { m_name = name; }
50 
54  template<typename T>
55  std::shared_ptr<T> addComponent()
56  {
57  auto component = std::make_shared<T>();
58  addComponent(component);
59  return component;
60  }
61 
62  template<typename T>
63  std::shared_ptr<T> addComponent(std::string name)
64  {
65  auto component = std::make_shared<T>(name);
66  addComponent(component);
67  return component;
68  }
69 
73  void addComponent(std::shared_ptr<Component> component);
74 
78  std::shared_ptr<Component> getComponent(const unsigned int index) const;
79 
83  template<typename T>
84  std::shared_ptr<T> getComponent() const
85  {
86  for (const auto& component : m_components)
87  {
88  if (auto compT = std::dynamic_pointer_cast<T>(component))
89  {
90  return compT;
91  }
92  }
93  return nullptr;
94  }
95 
99  template<typename T>
100  std::shared_ptr<T> getComponentN(const int index) const
101  {
102  int count = 0;
103  for (auto comp : m_components)
104  {
105  if (auto castComp = std::dynamic_pointer_cast<T>(comp))
106  {
107  if (count == index)
108  {
109  return castComp;
110  }
111  count++;
112  }
113  }
114  return nullptr;
115  }
116 
120  template<class T>
121  bool containsComponent() const { return getComponent<T>() != nullptr; }
125  bool containsComponent(std::shared_ptr<Component> component) const;
126 
130  template<typename T>
131  std::vector<std::shared_ptr<T>> getComponents() const
132  {
133  std::vector<std::shared_ptr<T>> components;
134  for (const auto& component : m_components)
135  {
136  if (auto compT = std::dynamic_pointer_cast<T>(component))
137  {
138  components.push_back(compT);
139  }
140  }
141  return components;
142  }
143 
144  const std::vector<std::shared_ptr<Component>>& getComponents() { return m_components; }
145 
149  void removeComponent(std::shared_ptr<Component> component);
150 
151 protected:
152  std::vector<std::shared_ptr<Component>> m_components;
153 
154  static std::atomic<EntityID> m_count;
155 
156  EntityID m_ID;
157  std::string m_name;
158 };
159 } // namespace imstk
std::shared_ptr< T > getComponentN(const int index) const
Get&#39;s the Nth component of type T.
Definition: imstkEntity.h:100
void removeComponent(std::shared_ptr< Component > component)
Remove component if it exists.
Definition: imstkEntity.cpp:51
Compound Geometry.
std::vector< std::shared_ptr< T > > getComponents() const
Get all components of type T.
Definition: imstkEntity.h:131
const std::string & getName() const
Get/Set the name of the entity.
Definition: imstkEntity.h:47
std::shared_ptr< T > getComponent() const
Get the first component of type T.
Definition: imstkEntity.h:84
static std::atomic< EntityID > m_count
current count of entities
Definition: imstkEntity.h:154
Top-level class for entities. Entities contain a collection of components which define how to be used...
Definition: imstkEntity.h:26
EventObject is the base class for all objects in iMSTK that can receive and emit events. It supports direct and queued observer functions. Direct observers receive events immediately on the same thread This can either be posted on an object or be a function pointer Queued observers receive events within their queue which they can process whenever they like. These can be connected with the connect/queuedConnect/disconnect functions Lambda recievers cannot be disconnected unless all receivers to a signal are removed.
EntityID getID() const
Get ID (ALWAYS query the ID in your code, DO NOT hardcode it)
Definition: imstkEntity.h:40
EntityID m_ID
unique ID of entity
Definition: imstkEntity.h:156
std::shared_ptr< T > addComponent()
Create and return a component on this entity.
Definition: imstkEntity.h:55
bool containsComponent() const
Check if contains component of type T.
Definition: imstkEntity.h:121
std::string m_name
Not unique name.
Definition: imstkEntity.h:157