SceneElement.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_FRAMEWORK_SCENEELEMENT_H
17 #define SURGSIM_FRAMEWORK_SCENEELEMENT_H
18 
19 #include <algorithm>
20 #include <boost/any.hpp>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <vector>
26 
28 
29 
30 namespace YAML
31 {
32 class Node;
33 }
34 
35 namespace SurgSim
36 {
37 namespace Framework
38 {
39 
40 class Component;
41 class PoseComponent;
42 class Scene;
43 class Runtime;
44 
51 class SceneElement : public std::enable_shared_from_this<SceneElement>
52 {
53 public:
54 
57  explicit SceneElement(const std::string& name);
58 
60  virtual ~SceneElement();
61 
63  virtual std::string getClassName() const;
64 
68  bool addComponent(std::shared_ptr<Component> component);
69 
73  bool removeComponent(std::shared_ptr<Component> component);
74 
78  bool removeComponent(const std::string& name);
79 
81  void removeComponents();
82 
86  std::shared_ptr<Component> getComponent(const std::string& name) const;
87 
90  std::vector<std::shared_ptr<Component>> getComponents() const;
91 
94  template <class T>
95  std::vector<std::shared_ptr<T>> getComponents() const;
96 
103  template <class T>
104  T getValue(const std::string& component, const std::string& property) const;
105 
111  boost::any getValue(const std::string& component, const std::string& property) const;
112 
120  template <class T>
121  bool getValue(const std::string& component, const std::string& property, T* value) const;
122 
128  void setValue(const std::string& component, const std::string& property, const boost::any& value);
129 
132  void addToGroup(const std::string& group);
133 
136  void removeFromGroup(const std::string& group);
137 
140  void setGroups(const std::vector<std::string>& groups);
141 
143  std::vector<std::string> getGroups() const;
144 
148  bool inGroup(const std::string& name);
149 
152  bool initialize();
153 
156  std::string getName() const;
157 
160  void setPose(const SurgSim::Math::RigidTransform3d& pose);
161 
165 
169  std::shared_ptr<PoseComponent> getPoseComponent();
170 
173  void setScene(std::weak_ptr<Scene> scene);
174 
177  std::shared_ptr<Scene> getScene();
178 
181  void setRuntime(std::weak_ptr<Runtime> runtime);
184  std::shared_ptr<Runtime> getRuntime();
185 
188  bool isInitialized() const;
189 
196  void setActive(bool val);
197 
199  bool isActive() const;
200 
203  std::shared_ptr<SceneElement> getSharedPtr();
204 
209  virtual YAML::Node encode(bool standalone) const;
210 
215  virtual bool decode(const YAML::Node& node);
216 
217 private:
221  bool initializeComponent(std::shared_ptr<SurgSim::Framework::Component> component);
222 
224  std::string m_name;
225 
226  std::shared_ptr<SurgSim::Framework::PoseComponent> m_pose;
227 
229  std::unordered_map<std::string, std::shared_ptr<Component>> m_components;
230 
232  std::weak_ptr<Scene> m_scene;
233 
235  std::weak_ptr<Runtime> m_runtime;
236 
238  std::unordered_set<std::string> m_groups;
239 
242  virtual bool doInitialize() = 0;
243 
246  void setName(const std::string& name);
247 
250 
253 };
254 
255 }; // namespace Framework
256 }; // namespace SurgSim
257 
259 
260 #endif // SURGSIM_FRAMEWORK_SCENEELEMENT_H
SurgSim::Framework::SceneElement::getScene
std::shared_ptr< Scene > getScene()
Gets the Scene.
Definition: SceneElement.cpp:233
SurgSim::Framework::SceneElement::getGroups
std::vector< std::string > getGroups() const
Definition: SceneElement.cpp:407
SurgSim::Framework::SceneElement::getName
std::string getName() const
Return the name of this SceneElement.
Definition: SceneElement.cpp:175
SurgSim::Framework::SceneElement::addToGroup
void addToGroup(const std::string &group)
Add this scene element to the given group.
Definition: SceneElement.cpp:377
SurgSim::Framework::SceneElement::~SceneElement
virtual ~SceneElement()
Destructor.
Definition: SceneElement.cpp:44
SurgSim::Framework::SceneElement::getComponent
std::shared_ptr< Component > getComponent(const std::string &name) const
Gets the component identified by name.
Definition: SceneElement.cpp:136
SurgSim::Framework::SceneElement::getRuntime
std::shared_ptr< Runtime > getRuntime()
Gets the runtime.
Definition: SceneElement.cpp:243
SurgSim::Framework::SceneElement::m_runtime
std::weak_ptr< Runtime > m_runtime
A (weak) back pointer to the Runtime containing this SceneElement.
Definition: SceneElement.h:235
SurgSim::Math::RigidTransform3d
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
SurgSim::Framework::SceneElement::initializeComponent
bool initializeComponent(std::shared_ptr< SurgSim::Framework::Component > component)
Initialize the given component.
Definition: SceneElement.cpp:168
SurgSim::Framework::SceneElement::setName
void setName(const std::string &name)
Sets the name of this SceneElement.
Definition: SceneElement.cpp:267
SurgSim::Framework::SceneElement::getPose
const SurgSim::Math::RigidTransform3d & getPose() const
Get the pose of this SceneElement.
Definition: SceneElement.cpp:185
SurgSim::Framework::SceneElement::m_isInitialized
bool m_isInitialized
Indicates if this SceneElement has been initialized or not.
Definition: SceneElement.h:249
YAML
Definition: DataStructuresConvert.h:28
SurgSim::Framework::SceneElement::getComponents
std::vector< std::shared_ptr< Component > > getComponents() const
Gets all the components of this SceneElement.
Definition: SceneElement.cpp:195
SurgSim::Framework::SceneElement::getClassName
virtual std::string getClassName() const
Definition: SceneElement.cpp:361
SurgSim
Definition: CompoundShapeToGraphics.cpp:29
SurgSim::Framework::SceneElement::m_components
std::unordered_map< std::string, std::shared_ptr< Component > > m_components
A collection of Components.
Definition: SceneElement.h:229
SurgSim::Framework::SceneElement::getSharedPtr
std::shared_ptr< SceneElement > getSharedPtr()
Gets a shared pointer to this SceneElement.
Definition: SceneElement.cpp:253
SurgSim::Framework::SceneElement::m_groups
std::unordered_set< std::string > m_groups
Local groups for serialization local handling.
Definition: SceneElement.h:238
SurgSim::Framework::SceneElement::m_name
std::string m_name
Name of this SceneElement.
Definition: SceneElement.h:224
SurgSim::Framework::SceneElement::addComponent
bool addComponent(std::shared_ptr< Component > component)
Adds a component, calls initialize() on the component, if SceneElement::isInitialized() is true.
Definition: SceneElement.cpp:49
SurgSim::Framework::SceneElement::setActive
void setActive(bool val)
Set this SceneElement's status (active/inactive)
Definition: SceneElement.cpp:367
SurgSim::Framework::SceneElement::removeFromGroup
void removeFromGroup(const std::string &group)
Remove this scene element from the given group.
Definition: SceneElement.cpp:386
RigidTransform.h
SurgSim::Framework::SceneElement::m_isActive
bool m_isActive
Indicates if this SceneElement is active or not.
Definition: SceneElement.h:252
SurgSim::Framework::SceneElement::setGroups
void setGroups(const std::vector< std::string > &groups)
Set the groups of this scene element.
Definition: SceneElement.cpp:395
SurgSim::Framework::SceneElement::setScene
void setScene(std::weak_ptr< Scene > scene)
Sets the Scene.
Definition: SceneElement.cpp:224
SurgSim::Framework::SceneElement::isActive
bool isActive() const
Definition: SceneElement.cpp:372
SurgSim::Framework::SceneElement::getValue
T getValue(const std::string &component, const std::string &property) const
Retrieves the property value from a component.
Definition: SceneElement-inl.h:47
SurgSim::Framework::SceneElement::doInitialize
virtual bool doInitialize()=0
Method to initialize this SceneElement.
SurgSim::Framework::SceneElement::removeComponent
bool removeComponent(std::shared_ptr< Component > component)
Removes a given component.
Definition: SceneElement.cpp:94
SurgSim::Framework::SceneElement::encode
virtual YAML::Node encode(bool standalone) const
Convert to a YAML::Node.
Definition: SceneElement.cpp:272
SurgSim::Framework::SceneElement
SceneElement is the basic part of a scene, it is a container of components.
Definition: SceneElement.h:51
SceneElement-inl.h
SurgSim::Framework::SceneElement::removeComponents
void removeComponents()
Removes all components.
Definition: SceneElement.cpp:119
SurgSim::Framework::SceneElement::setValue
void setValue(const std::string &component, const std::string &property, const boost::any &value)
Sets the property value of a component.
Definition: SceneElement.cpp:215
SurgSim::Framework::SceneElement::decode
virtual bool decode(const YAML::Node &node)
Pull data from a YAML::Node.
Definition: SceneElement.cpp:301
SurgSim::Framework::SceneElement::setRuntime
void setRuntime(std::weak_ptr< Runtime > runtime)
Sets the Runtime.
Definition: SceneElement.cpp:238
SurgSim::Framework::SceneElement::SceneElement
SceneElement(const std::string &name)
Constructor.
Definition: SceneElement.cpp:34
SurgSim::Framework::SceneElement::isInitialized
bool isInitialized() const
Return if this SceneElement is initialized.
Definition: SceneElement.cpp:248
SurgSim::Framework::SceneElement::m_pose
std::shared_ptr< SurgSim::Framework::PoseComponent > m_pose
Definition: SceneElement.h:226
SurgSim::Framework::SceneElement::inGroup
bool inGroup(const std::string &name)
Test whether this SceneElement is in a specific group.
Definition: SceneElement.cpp:412
SurgSim::Framework::SceneElement::getPoseComponent
std::shared_ptr< PoseComponent > getPoseComponent()
Get the PoseComponent that controls the pose all Representations in this SceneElement.
Definition: SceneElement.cpp:190
SurgSim::Framework::SceneElement::m_scene
std::weak_ptr< Scene > m_scene
A (weak) back pointer to the Scene containing this SceneElement.
Definition: SceneElement.h:232
SurgSim::Framework::SceneElement::initialize
bool initialize()
Executes the initialize operation.
Definition: SceneElement.cpp:148
SurgSim::Framework::SceneElement::setPose
void setPose(const SurgSim::Math::RigidTransform3d &pose)
Set the pose of this SceneElement.
Definition: SceneElement.cpp:180