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
27namespace 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
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
79
80 void
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;
120 .file(e.project, e.robotFile)
121 .useFullModel()
122 .joints(e.jointValues)
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
269 ->getWrapper<memoryx::EntityWrappers::SimoxObjectWrapper>();
270 std::string filename = wrapper->getManipulationObjectFileName();
271
272 classFilenameCache[className] = filename;
273
274 return filename;
275 }
276 }
277} // namespace armarx
uint8_t data[1]
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
void onDisconnectComponent() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void reportSceneUpdated(const SceneVisuData &scene, const ::Ice::Current &=::Ice::emptyCurrent) override
void onConnectComponent() override
std::string getDefaultName() const override
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition ElementOps.h:176
DerivedT & scale(Eigen::Vector3f scale)
Definition ElementOps.h:254
Robot & useFullModel()
Definition Robot.h:42
Robot & useOriginalColor()
Definition Robot.h:58
Robot & joints(std::map< std::string, float > const &values)
Definition Robot.h:74
Robot & file(std::string const &project, std::string const &filename)
Definition Robot.h:16
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
Quaternion< float, 0 > Quaternionf
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< SimoxObjectWrapper > SimoxObjectWrapperPtr
void add(ElementT const &element)
Definition Layer.h:31