ArmarXPhysicsWorldVisualization.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2013-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXSimulation::ArmarXPhysicsWorldVisualization
19  * @author Nikolaus Vahrenkamp ( vahrenkamp at kit dot edu )
20  * @date 2014
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #pragma once
26 
27 #include <VirtualRobot/SceneObject.h>
28 #include <ArmarXSimulation/interface/simulator/SimulatorInterface.h>
29 
31 #include <RobotAPI/interface/core/RobotState.h>
34 
35 #include <Inventor/nodes/SoSeparator.h>
36 
37 #include <Eigen/Core>
38 #include <Eigen/Geometry>
39 
40 #include <memory>
41 #include <mutex>
42 
43 namespace armarx
44 {
45  /*!
46  * \class ArmarXPhysicsWorldVisualizationPropertyDefinitions
47  * \brief
48  */
51  {
52  public:
55  {
56  defineOptionalProperty<std::string>("ReportVisuTopicName", "SimulatorVisuUpdates", "The topic on which the visualization updates are published.");
57  }
58  };
59 
60 
61  /*!
62  * \brief The ArmarXPhysicsWorldVisualization class organizes the Coin3D visualization of the physics scene. It connects to the SimulatorVisuUpdate topic
63  * in order to retrieve the visualization stream of the current simulated environment.
64  * All scene changes (new objects / removed objects) are handled by checking the current setup with the retrieved one.
65  */
67  virtual public SimulatorListenerInterface,
68  virtual public Component
69  {
70  public:
71 
73 
75 
76  void releaseResources();
77 
78  // inherited from Component
79  std::string getDefaultName() const override
80  {
81  return "ArmarXPhysicsWorldVisualization";
82  }
83  void onInitComponent() override;
84  void onConnectComponent() override;
85  void onDisconnectComponent() override;
86  void onExitComponent() override;
87 
88  /*! Inherited from SimulatorListenerInterface.
89  * This method is called when the simulator publishes a scene update. For now the complete scene is described in the SyeneVisuData format.
90  *
91  */
92  void reportSceneUpdated(const ::armarx::SceneVisuData& actualData, const ::Ice::Current& = Ice::emptyCurrent) override;
93 
94  /*!
95  * \brief getVisu returns the visualization that is synchronized with ArmarXPhysicsWorld
96  * \return
97  */
98  SoSeparator* getVisualization();
99 
100  /*!
101  * \brief Returns the scene without floor (allows for better viewAll handling)
102  * \return
103  */
104  SoSeparator* getViewableScene();
105 
106 
107  /**
108  * @see PropertyUser::createPropertyDefinitions()
109  */
111  {
114  }
115 
116 
117  /*!
118  * \brief buildVisu Creates visualization with increased ref counter.
119  * \param o
120  * \param visuType
121  * \return
122  */
123  SoNode* buildVisu(VirtualRobot::SceneObjectPtr o, VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full);
124  /*!
125  * \brief buildVisu Creates visualization with increased ref counter.
126  * \param ro
127  * \param visuType
128  * \return
129  */
130  SoNode* buildVisu(VirtualRobot::RobotPtr ro, VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full);
131 
132  void addVisualization(VirtualRobot::SceneObjectPtr so, VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full);
133  void addVisualization(VirtualRobot::RobotPtr ro, VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full);
134 
135  void removeObjectVisualization(const std::string& name);
137  void removeRobotVisualization(const std::string& name);
139 
140  void enableVisuType(bool fullModel);
141  void addFloorVisualization(Eigen::Vector3f floorPos, Eigen::Vector3f floorUp, float floorExtendMM, std::string floorTexture);
142 
143  void showBaseCoord(bool enable, float scale = 5.0f);
144 
145  // If set, this mutex is used to generate locks (for visu updates)
146  void setMutex(std::shared_ptr<std::recursive_mutex> const& m);
147 
148  using RecursiveLockPtr = std::shared_ptr<std::unique_lock<std::recursive_mutex>>;
151 
152  /*!
153  * \brief Show/hide contacts
154  * \param enable
155  */
156  //void enableContactVisu(bool enable);
157 
158  /*!
159  * \brief Update the contact visualization
160  * Draws an arrow at each contact
161  * Does nothing if enableContacts is false
162  */
163  //void updateContactVisualization(std::vector<SimDynamics::DynamicsEngine::DynamicsContactInfo> &c);
164 
165  /*!
166  * \brief synchronizeVisualizationData Updates world poses of all objects of the visualization according to the current state of the physics engine.
167  * \return True on success.
168  */
170 
171  float getSyncVisuTime();
172 
173 
174  VirtualRobot::RobotPtr getRobot(const std::string& name);
175  VirtualRobot::SceneObjectPtr getObject(const std::string& name);
176 
177  std::vector<VirtualRobot::RobotPtr> getRobots() const;
178  std::vector<VirtualRobot::SceneObjectPtr> getObjects() const;
179 
180  /**
181  * @brief getSyncTimestamp
182  * @return time in microseconds
183  */
184  Ice::Long getSyncTimestamp() const;
185  protected:
186 
187  bool loadObject(const ObjectVisuData& obj);
188  bool loadRobot(const std::string& robotName, const std::string& robotFile, const std::string& project, float scaling, bool colModel);
189  bool loadObject(const std::string& objectFile, const std::string& project, const std::string& instanceName);
190 
191  bool synchronizeSceneObjectPoses(NamePoseMap& objMap, NameValueMap& jvMap, VirtualRobot::SceneObjectPtr currentObjVisu);
192 
193  SoSeparator* coinVisu;
194  SoSeparator* dynamicsVisu;
195  SoSeparator* baseCoordVisu;
196  //SoSeparator* contactsVisu;
197  SoSeparator* sceneViewableSep;
198  SoSeparator* floorVisu;
199 
200  std::map<VirtualRobot::SceneObjectPtr, SoNode*> addedVisualizations;
201  std::map<VirtualRobot::RobotPtr, SoNode*> addedRobotVisualizations;
202 
203  float synchronize2TimeMS; // synchronize data from engine to visu and report object
204 
205  //bool contactVisuEnabled;
206 
207  //! mutex to protect access to visualization models
208  std::shared_ptr<std::recursive_mutex> mutex;
209 
210  //! mutex to process SceneData
211  std::shared_ptr<std::recursive_mutex> mutexData;
212 
213  SceneVisuData currentSceneData;
214  SceneVisuData lastSceneData;
215 
216  VirtualRobot::SceneObject::VisualizationType modelType;
217 
219  bool synchronizeRobots();
220  bool synchronizeFloor();
221  bool synchronizeObjects();
222  bool createPrimitiveObject(ObjectVisuPrimitivePtr objectPrimitiveData, const std::string& name);
223  };
224 
226 }
armarx::ArmarXPhysicsWorldVisualization::synchronizeRobots
bool synchronizeRobots()
Definition: ArmarXPhysicsWorldVisualization.cpp:581
armarx::ArmarXPhysicsWorldVisualization::getSyncTimestamp
Ice::Long getSyncTimestamp() const
getSyncTimestamp
Definition: ArmarXPhysicsWorldVisualization.cpp:1058
armarx::ArmarXPhysicsWorldVisualization::lastSceneData
SceneVisuData lastSceneData
Definition: ArmarXPhysicsWorldVisualization.h:214
armarx::ArmarXPhysicsWorldVisualization::addedRobotVisualizations
std::map< VirtualRobot::RobotPtr, SoNode * > addedRobotVisualizations
Definition: ArmarXPhysicsWorldVisualization.h:201
armarx::ArmarXPhysicsWorldVisualization::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: ArmarXPhysicsWorldVisualization.h:79
armarx::ArmarXPhysicsWorldVisualization::~ArmarXPhysicsWorldVisualization
~ArmarXPhysicsWorldVisualization() override
Definition: ArmarXPhysicsWorldVisualization.cpp:73
armarx::ArmarXPhysicsWorldVisualizationPropertyDefinitions
Definition: ArmarXPhysicsWorldVisualization.h:49
armarx::ArmarXPhysicsWorldVisualization::getSyncVisuTime
float getSyncVisuTime()
Definition: ArmarXPhysicsWorldVisualization.cpp:838
armarx::ArmarXPhysicsWorldVisualization::synchronizeObjects
bool synchronizeObjects()
Definition: ArmarXPhysicsWorldVisualization.cpp:683
armarx::ArmarXPhysicsWorldVisualization::getViewableScene
SoSeparator * getViewableScene()
Returns the scene without floor (allows for better viewAll handling)
Definition: ArmarXPhysicsWorldVisualization.cpp:143
armarx::ArmarXPhysicsWorldVisualization::showBaseCoord
void showBaseCoord(bool enable, float scale=5.0f)
Definition: ArmarXPhysicsWorldVisualization.cpp:260
armarx::ArmarXPhysicsWorldVisualization::addVisualization
void addVisualization(VirtualRobot::SceneObjectPtr so, VirtualRobot::SceneObject::VisualizationType visuType=VirtualRobot::SceneObject::Full)
Pose.h
armarx::ArmarXPhysicsWorldVisualization::sceneViewableSep
SoSeparator * sceneViewableSep
Definition: ArmarXPhysicsWorldVisualization.h:197
armarx::ArmarXPhysicsWorldVisualization::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ArmarXPhysicsWorldVisualization.cpp:118
armarx::ArmarXPhysicsWorldVisualization::dynamicsVisu
SoSeparator * dynamicsVisu
Definition: ArmarXPhysicsWorldVisualization.h:194
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:333
armarx::ArmarXPhysicsWorldVisualization::enableVisuType
void enableVisuType(bool fullModel)
Definition: ArmarXPhysicsWorldVisualization.cpp:391
armarx::ArmarXPhysicsWorldVisualization
The ArmarXPhysicsWorldVisualization class organizes the Coin3D visualization of the physics scene....
Definition: ArmarXPhysicsWorldVisualization.h:66
armarx::ArmarXPhysicsWorldVisualization::removeFloorVisualization
void removeFloorVisualization()
Definition: ArmarXPhysicsWorldVisualization.cpp:252
so
linux gnu libIceDB so
Definition: CMakeLists.txt:7
armarx::ArmarXPhysicsWorldVisualization::getScopedDataLock
RecursiveLockPtr getScopedDataLock()
Definition: ArmarXPhysicsWorldVisualization.cpp:482
armarx::ArmarXPhysicsWorldVisualization::modelType
VirtualRobot::SceneObject::VisualizationType modelType
Definition: ArmarXPhysicsWorldVisualization.h:216
armarx::ArmarXPhysicsWorldVisualization::floorVisu
SoSeparator * floorVisu
Definition: ArmarXPhysicsWorldVisualization.h:198
armarx::ArmarXPhysicsWorldVisualization::releaseResources
void releaseResources()
Definition: ArmarXPhysicsWorldVisualization.cpp:90
project
std::string project
Definition: VisualizationRobot.cpp:82
armarx::ArmarXPhysicsWorldVisualization::currentSceneData
SceneVisuData currentSceneData
Definition: ArmarXPhysicsWorldVisualization.h:213
armarx::ArmarXPhysicsWorldVisualization::loadRobot
bool loadRobot(const std::string &robotName, const std::string &robotFile, const std::string &project, float scaling, bool colModel)
Definition: ArmarXPhysicsWorldVisualization.cpp:865
IceInternal::Handle< ArmarXPhysicsWorldVisualization >
armarx::ArmarXPhysicsWorldVisualization::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ArmarXPhysicsWorldVisualization.cpp:113
armarx::ArmarXPhysicsWorldVisualization::reportSceneUpdated
void reportSceneUpdated(const ::armarx::SceneVisuData &actualData, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: ArmarXPhysicsWorldVisualization.cpp:128
armarx::ArmarXPhysicsWorldVisualization::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ArmarXPhysicsWorldVisualization.cpp:123
armarx::ArmarXPhysicsWorldVisualization::getRobots
std::vector< VirtualRobot::RobotPtr > getRobots() const
Definition: ArmarXPhysicsWorldVisualization.cpp:1032
armarx::ArmarXPhysicsWorldVisualization::getVisualization
SoSeparator * getVisualization()
getVisu returns the visualization that is synchronized with ArmarXPhysicsWorld
Definition: ArmarXPhysicsWorldVisualization.cpp:137
armarx::ArmarXPhysicsWorldVisualization::removeObjectVisualization
void removeObjectVisualization(const std::string &name)
Definition: ArmarXPhysicsWorldVisualization.cpp:325
armarx::ArmarXPhysicsWorldVisualization::getScopedLock
RecursiveLockPtr getScopedLock()
Definition: ArmarXPhysicsWorldVisualization.cpp:475
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::ArmarXPhysicsWorldVisualization::getObject
VirtualRobot::SceneObjectPtr getObject(const std::string &name)
Definition: ArmarXPhysicsWorldVisualization.cpp:1018
armarx::ArmarXPhysicsWorldVisualization::synchronize2TimeMS
float synchronize2TimeMS
Definition: ArmarXPhysicsWorldVisualization.h:203
armarx::ArmarXPhysicsWorldVisualization::synchronizeFloor
bool synchronizeFloor()
Definition: ArmarXPhysicsWorldVisualization.cpp:557
armarx::ArmarXPhysicsWorldVisualization::mutex
std::shared_ptr< std::recursive_mutex > mutex
mutex to protect access to visualization models
Definition: ArmarXPhysicsWorldVisualization.h:208
armarx::ArmarXPhysicsWorldVisualization::RecursiveLockPtr
std::shared_ptr< std::unique_lock< std::recursive_mutex > > RecursiveLockPtr
Definition: ArmarXPhysicsWorldVisualization.h:148
Component.h
armarx::ArmarXPhysicsWorldVisualization::getObjects
std::vector< VirtualRobot::SceneObjectPtr > getObjects() const
Definition: ArmarXPhysicsWorldVisualization.cpp:1045
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::ArmarXPhysicsWorldVisualization::coinVisu
SoSeparator * coinVisu
Definition: ArmarXPhysicsWorldVisualization.h:193
armarx::control::njoint_controller::platform::platform_follower_controller::NameValueMap
std::map< std::string, float > NameValueMap
Definition: PlatformFollowerController.h:91
armarx::ArmarXPhysicsWorldVisualization::removeVisualization
void removeVisualization(VirtualRobot::RobotPtr r)
Definition: ArmarXPhysicsWorldVisualization.cpp:371
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::ArmarXPhysicsWorldVisualizationPropertyDefinitions::ArmarXPhysicsWorldVisualizationPropertyDefinitions
ArmarXPhysicsWorldVisualizationPropertyDefinitions(std::string prefix)
Definition: ArmarXPhysicsWorldVisualization.h:53
armarx::ArmarXPhysicsWorldVisualization::addedVisualizations
std::map< VirtualRobot::SceneObjectPtr, SoNode * > addedVisualizations
Definition: ArmarXPhysicsWorldVisualization.h:200
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
armarx::ArmarXPhysicsWorldVisualization::synchronizeSceneObjectPoses
bool synchronizeSceneObjectPoses(NamePoseMap &objMap, NameValueMap &jvMap, VirtualRobot::SceneObjectPtr currentObjVisu)
Definition: ArmarXPhysicsWorldVisualization.cpp:495
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::ArmarXPhysicsWorldVisualization::mutexData
std::shared_ptr< std::recursive_mutex > mutexData
mutex to process SceneData
Definition: ArmarXPhysicsWorldVisualization.h:211
armarx::ArmarXPhysicsWorldVisualization::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ArmarXPhysicsWorldVisualization.cpp:104
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
armarx::ArmarXPhysicsWorldVisualization::createPropertyDefinitions
PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ArmarXPhysicsWorldVisualization.h:110
armarx::ArmarXPhysicsWorldVisualization::addFloorVisualization
void addFloorVisualization(Eigen::Vector3f floorPos, Eigen::Vector3f floorUp, float floorExtendMM, std::string floorTexture)
Definition: ArmarXPhysicsWorldVisualization.cpp:233
armarx::ArmarXPhysicsWorldVisualization::removeRobotVisualization
void removeRobotVisualization(const std::string &name)
Definition: ArmarXPhysicsWorldVisualization.cpp:349
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::ArmarXPhysicsWorldVisualization::getRobot
VirtualRobot::RobotPtr getRobot(const std::string &name)
Definition: ArmarXPhysicsWorldVisualization.cpp:1163
armarx::ArmarXPhysicsWorldVisualization::loadObject
bool loadObject(const ObjectVisuData &obj)
Definition: ArmarXPhysicsWorldVisualization.cpp:844
armarx::ArmarXPhysicsWorldVisualization::baseCoordVisu
SoSeparator * baseCoordVisu
Definition: ArmarXPhysicsWorldVisualization.h:195
armarx::ArmarXPhysicsWorldVisualization::createPrimitiveObject
bool createPrimitiveObject(ObjectVisuPrimitivePtr objectPrimitiveData, const std::string &name)
Definition: ArmarXPhysicsWorldVisualization.cpp:1065
armarx::ArmarXPhysicsWorldVisualization::synchronizeVisualizationData
bool synchronizeVisualizationData()
Show/hide contacts.
Definition: ArmarXPhysicsWorldVisualization.cpp:772
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ArmarXPhysicsWorldVisualization::ArmarXPhysicsWorldVisualization
ArmarXPhysicsWorldVisualization()
Definition: ArmarXPhysicsWorldVisualization.cpp:46
Application.h
armarx::ArmarXPhysicsWorldVisualization::buildVisu
SoNode * buildVisu(VirtualRobot::SceneObjectPtr o, VirtualRobot::SceneObject::VisualizationType visuType=VirtualRobot::SceneObject::Full)
buildVisu Creates visualization with increased ref counter.
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::ArmarXPhysicsWorldVisualization::setMutex
void setMutex(std::shared_ptr< std::recursive_mutex > const &m)
Definition: ArmarXPhysicsWorldVisualization.cpp:469