SimulatorToArviz.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ArmarXSimulation::ArmarXObjects::SimulatorToArviz
17  * @author Peter Reiner ( ufekv at student dot kit dot edu )
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "SimulatorToArviz.h"
24 
25 #include <VirtualRobot/RuntimeEnvironment.h>
26 
27 namespace armarx
28 {
29 
30 
33  {
36 
37  defs->optional(properties.oldPriorKnowledge.use, "p.oldPriorKnowledge.use");
38  defs->optional(properties.oldPriorKnowledge.name, "p.oldPriorKnowledge.name");
39 
40  return defs;
41  }
42 
44  {
45  }
46 
47  std::string
49  {
50  return "SimulatorToArviz";
51  }
52 
53  void
55  {
56  usingTopic("SimulatorListenerInterface");
57  usingTopic("SimulatorVisuUpdates");
58 
59  if (properties.oldPriorKnowledge.use)
60  {
61  usingProxy(properties.oldPriorKnowledge.name);
62  }
63  }
64 
65  void
67  {
68  if (properties.oldPriorKnowledge.use)
69  {
70  getProxy(priorKnowledge, properties.oldPriorKnowledge.name);
71  objectClassSegment.initFromProxy(priorKnowledge, {});
72  }
73  }
74 
75  void
77  {
78  }
79 
80  void
82  {
83  }
84 
85  void
86  SimulatorToArviz::reportSceneUpdated(const SceneVisuData& scene, const ::Ice::Current&)
87  {
88  armarx::viz::Layer floorLayer = arviz.layer("FloorLayer");
89  armarx::viz::Layer robotLayer = arviz.layer("RobotLayer");
90  armarx::viz::Layer objectLayer = arviz.layer("ObjectLayer");
91  handleRobotLayer(scene.robots, robotLayer);
92  handleObjectLayer(scene.objects, objectLayer);
93 
94  if (scene.floor)
95  {
96  ARMARX_DEBUG << "has floor: " << scene.floor
97  << " with filename: " << scene.floorTextureFile;
98  std::string filename = "images/Floor20x20.xml";
99  VirtualRobot::RuntimeEnvironment::getDataFileAbsolute(filename);
100  floorLayer.add(armarx::viz::Object("Floor").file("", filename));
101  }
102 
103  arviz.commit({floorLayer, robotLayer, objectLayer});
104  }
105 
106  void
107  SimulatorToArviz::handleRobotLayer(const RobotVisuList& robotList,
108  armarx::viz::Layer& robotLayer)
109  {
110  for (const auto& e : robotList)
111  {
112  Eigen::Vector3f position(e.pose->position->x, e.pose->position->y, e.pose->position->z);
113  Eigen::Quaternionf orientation(e.pose->orientation->qw,
114  e.pose->orientation->qx,
115  e.pose->orientation->qy,
116  e.pose->orientation->qz);
117  ARMARX_DEBUG << "Drawing robot " << e.name << " using file: " << e.robotFile
118  << " and joint values " << e.jointValues;
119  armarx::viz::Robot robot = armarx::viz::Robot(e.name)
120  .file(e.project, e.robotFile)
121  .useFullModel()
122  .joints(e.jointValues)
123  .useOriginalColor()
124  .scale(e.scaling)
125  .pose(position, orientation);
126 
127  robotLayer.add(robot);
128  }
129  }
130 
131  void
132  SimulatorToArviz::handleObjectLayer(const ObjectVisuList& objectList,
133  armarx::viz::Layer& objectLayer)
134  {
135  for (const ObjectVisuData& data : objectList)
136  {
137  if (data.objectPrimitiveData)
138  {
139  handleObjectPrimitiveData(data, objectLayer);
140  }
141  else if (not data.filename.empty())
142  {
143  handleObjectFromFile(data, objectLayer);
144  }
145  else if (properties.oldPriorKnowledge.use)
146  {
147  handleObjectFromObjectClass(data, objectLayer);
148  }
149  }
150  }
151 
152  void
153  SimulatorToArviz::handleObjectPrimitiveData(const ObjectVisuData& _object,
154  armarx::viz::Layer& objectLayer)
155  {
156  const PoseBasePtr& objectPose = _object.objectPoses.at(_object.name);
157  Eigen::Vector3f position(
158  objectPose->position->x, objectPose->position->y, objectPose->position->z);
159  Eigen::Quaternionf orientation(objectPose->orientation->qw,
160  objectPose->orientation->qx,
161  objectPose->orientation->qy,
162  objectPose->orientation->qz);
163 
164  switch (_object.objectPrimitiveData->type)
165  {
166  case ObjectType::Box:
167  {
168  BoxVisuPrimitivePtr boxObject =
169  BoxVisuPrimitivePtr::dynamicCast(_object.objectPrimitiveData);
170  //armarx::viz::Box box = armarx::viz::Box(_object.name)
171  objectLayer.add(
172  armarx::viz::Box(_object.name)
173  .size(
174  Eigen::Vector3f{boxObject->width, boxObject->height, boxObject->depth})
175  .color(boxObject->color.r, boxObject->color.g, boxObject->color.b)
176  .position(position)
177  .orientation(orientation));
178  }
179  break;
180  case ObjectType::Sphere:
181  {
182  SphereVisuPrimitivePtr sphereObject =
183  SphereVisuPrimitivePtr::dynamicCast(_object.objectPrimitiveData);
184  //armarx::viz::Sphere sphere = armarx::viz::Sphere(_object.name)
185  objectLayer.add(
186  armarx::viz::Sphere(_object.name)
187  .radius(sphereObject->radius)
188  .color(sphereObject->color.r, sphereObject->color.g, sphereObject->color.b)
189  .position(position)
190  .orientation(orientation));
191  }
192  break;
193  case ObjectType::Cylinder:
194  {
195  CylinderVisuPrimitivePtr cylinderObject =
196  CylinderVisuPrimitivePtr::dynamicCast(_object.objectPrimitiveData);
197  //armarx::viz::Cylinder cylinder = armarx::viz::Cylinder(_object.name)
198  objectLayer.add(armarx::viz::Cylinder(_object.name)
199  .radius(cylinderObject->radius)
200  .height(cylinderObject->length)
201  .color(cylinderObject->color.r,
202  cylinderObject->color.g,
203  cylinderObject->color.b)
204  .position(position)
205  .orientation(orientation));
206  }
207  break;
208  }
209  }
210 
211  void
212  SimulatorToArviz::handleObjectFromFile(const ObjectVisuData& _object,
213  armarx::viz::Layer& objectLayer)
214  {
215  const PoseBasePtr& objectPose = _object.objectPoses.at(_object.name);
216 
217  Eigen::Vector3f position(
218  objectPose->position->x, objectPose->position->y, objectPose->position->z);
219  Eigen::Quaternionf orientation(objectPose->orientation->qw,
220  objectPose->orientation->qx,
221  objectPose->orientation->qy,
222  objectPose->orientation->qz);
223 
224  objectLayer.add(armarx::viz::Object(_object.name)
225  .file(_object.project, _object.filename)
226  .position(position)
227  .orientation(orientation));
228  }
229 
230  void
231  SimulatorToArviz::handleObjectFromObjectClass(const ObjectVisuData& _object,
232  armarx::viz::Layer& objectLayer)
233  {
234 
235  const PoseBasePtr& objectPose = _object.objectPoses.at(_object.name);
236 
237  Eigen::Vector3f position(
238  objectPose->position->x, objectPose->position->y, objectPose->position->z);
239  Eigen::Quaternionf orientation(objectPose->orientation->qw,
240  objectPose->orientation->qx,
241  objectPose->orientation->qy,
242  objectPose->orientation->qz);
243  std::string filename = getClassFilename(_object.objectClassName);
244  std::string projectname = "";
245  objectLayer.add(armarx::viz::Object(_object.name)
246  .file(projectname, filename)
247  .position(position)
248  .orientation(orientation));
249  }
250 
251  std::string
252  SimulatorToArviz::getClassFilename(const std::string& className)
253  {
254  auto it = classFilenameCache.find(className);
255  if (it != classFilenameCache.end())
256  {
257  return it->second;
258  }
259  else
260  {
261  std::optional<memoryx::ObjectClassWrapper> objectClass =
262  objectClassSegment.getClass(className);
263  if (!objectClass)
264  {
265  return "";
266  }
268  objectClass->classInMemory
270  std::string filename = wrapper->getManipulationObjectFileName();
271 
272  classFilenameCache[className] = filename;
273 
274  return filename;
275  }
276  }
277 } // namespace armarx
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:89
armarx::viz::Cylinder::radius
Cylinder & radius(float r)
Definition: Elements.h:76
armarx::viz::Object::file
Object & file(std::string const &project, std::string const &filename)
Definition: Elements.h:340
memoryx::ObjectClassSegmentWrapper::getClass
std::optional< ObjectClassWrapper > getClass(std::string const &className) const
Definition: ObjectClassSegmentWrapper.cpp:68
armarx::viz::Cylinder::height
Cylinder & height(float h)
Definition: Elements.h:84
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:31
armarx::viz::Sphere
Definition: Elements.h:133
armarx::viz::ElementOps::position
DerivedT & position(float x, float y, float z)
Definition: ElementOps.h:136
IceInternal::Handle< SimoxObjectWrapper >
armarx::viz::Object
Definition: Elements.h:332
armarx::SimulatorToArviz::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: SimulatorToArviz.cpp:32
armarx::SimulatorToArviz::onDisconnectComponent
void onDisconnectComponent() override
Definition: SimulatorToArviz.cpp:76
armarx::armem::human::Robot
@ Robot
Definition: util.h:17
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
memoryx::ObjectClassSegmentWrapper::initFromProxy
void initFromProxy(memoryx::PriorKnowledgeInterfacePrx const &priorKnowledge, const std::vector< std::string > &datasets)
Definition: ObjectClassSegmentWrapper.cpp:16
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::viz::Cylinder
Definition: Elements.h:71
armarx::viz::Sphere::radius
Sphere & radius(float r)
Definition: Elements.h:138
armarx::viz::ElementOps::orientation
DerivedT & orientation(Eigen::Quaternionf const &ori)
Definition: ElementOps.h:152
armarx::viz::Box
Definition: Elements.h:47
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::viz::Robot
Definition: Robot.h:10
Eigen::Quaternionf
Quaternion< float, 0 > Quaternionf
Definition: EigenForwardDeclarations.h:61
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:254
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:42
armarx::SimulatorToArviz::reportSceneUpdated
void reportSceneUpdated(const SceneVisuData &scene, const ::Ice::Current &=::Ice::emptyCurrent) override
Definition: SimulatorToArviz.cpp:86
armarx::Quaternion< float, 0 >
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::viz::Box::size
Box & size(Eigen::Vector3f const &s)
Definition: Elements.h:52
armarx::viz::ElementOps::color
DerivedT & color(Color color)
Definition: ElementOps.h:218
armarx::SimulatorToArviz::onConnectComponent
void onConnectComponent() override
Definition: SimulatorToArviz.cpp:66
SimulatorToArviz.h
armarx::SimulatorToArviz::onExitComponent
void onExitComponent() override
Definition: SimulatorToArviz.cpp:81
armarx::SimulatorToArviz::getDefaultName
std::string getDefaultName() const override
Definition: SimulatorToArviz.cpp:48
armarx::SimulatorToArviz::onInitComponent
void onInitComponent() override
Definition: SimulatorToArviz.cpp:54
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:80
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:407
armarx::viz::Layer
Definition: Layer.h:12
armarx::SimulatorToArviz::SimulatorToArviz
SimulatorToArviz()
Definition: SimulatorToArviz.cpp:43
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27