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
28
29namespace armarx
30{
31
32
35 {
38
39 defs->optional(properties.oldPriorKnowledge.use, "p.oldPriorKnowledge.use");
40 defs->optional(properties.oldPriorKnowledge.name, "p.oldPriorKnowledge.name");
41
42 return defs;
43 }
44
48
49 std::string
51 {
52 return GetDefaultName();
53 }
54
55 std::string
57 {
58 return "SimulatorToArviz";
59 }
60
61 void
63 {
64 usingTopic("SimulatorListenerInterface");
65 usingTopic("SimulatorVisuUpdates");
66
67 if (properties.oldPriorKnowledge.use)
68 {
69 usingProxy(properties.oldPriorKnowledge.name);
70 }
71 }
72
73 void
75 {
76 if (properties.oldPriorKnowledge.use)
77 {
78 getProxy(priorKnowledge, properties.oldPriorKnowledge.name);
79 objectClassSegment.initFromProxy(priorKnowledge, {});
80 }
81 }
82
83 void
87
88 void
92
93 void
94 SimulatorToArviz::reportSceneUpdated(const SceneVisuData& scene, const ::Ice::Current&)
95 {
96 armarx::viz::Layer floorLayer = arviz.layer("FloorLayer");
97 armarx::viz::Layer robotLayer = arviz.layer("RobotLayer");
98 armarx::viz::Layer objectLayer = arviz.layer("ObjectLayer");
99 handleRobotLayer(scene.robots, robotLayer);
100 handleObjectLayer(scene.objects, objectLayer);
101
102 if (scene.floor)
103 {
104 ARMARX_DEBUG << "has floor: " << scene.floor
105 << " with filename: " << scene.floorTextureFile;
106 std::string filename = "images/Floor20x20.xml";
107 VirtualRobot::RuntimeEnvironment::getDataFileAbsolute(filename);
108 floorLayer.add(armarx::viz::Object("Floor").file("", filename));
109 }
110
111 arviz.commit({floorLayer, robotLayer, objectLayer});
112 }
113
114 void
115 SimulatorToArviz::handleRobotLayer(const RobotVisuList& robotList,
116 armarx::viz::Layer& robotLayer)
117 {
118 for (const auto& e : robotList)
119 {
120 Eigen::Vector3f position(e.pose->position->x, e.pose->position->y, e.pose->position->z);
121 Eigen::Quaternionf orientation(e.pose->orientation->qw,
122 e.pose->orientation->qx,
123 e.pose->orientation->qy,
124 e.pose->orientation->qz);
125 ARMARX_DEBUG << "Drawing robot " << e.name << " using file: " << e.robotFile
126 << " and joint values " << e.jointValues;
128 .file(e.project, e.robotFile)
129 .useFullModel()
130 .joints(e.jointValues)
132 .scale(e.scaling)
133 .pose(position, orientation);
134
135 robotLayer.add(robot);
136 }
137 }
138
139 void
140 SimulatorToArviz::handleObjectLayer(const ObjectVisuList& objectList,
141 armarx::viz::Layer& objectLayer)
142 {
143 for (const ObjectVisuData& data : objectList)
144 {
145 if (data.objectPrimitiveData)
146 {
147 handleObjectPrimitiveData(data, objectLayer);
148 }
149 else if (not data.filename.empty())
150 {
151 handleObjectFromFile(data, objectLayer);
152 }
153 else if (properties.oldPriorKnowledge.use)
154 {
155 handleObjectFromObjectClass(data, objectLayer);
156 }
157 }
158 }
159
160 void
161 SimulatorToArviz::handleObjectPrimitiveData(const ObjectVisuData& _object,
162 armarx::viz::Layer& objectLayer)
163 {
164 const PoseBasePtr& objectPose = _object.objectPoses.at(_object.name);
165 Eigen::Vector3f position(
166 objectPose->position->x, objectPose->position->y, objectPose->position->z);
167 Eigen::Quaternionf orientation(objectPose->orientation->qw,
168 objectPose->orientation->qx,
169 objectPose->orientation->qy,
170 objectPose->orientation->qz);
171
172 switch (_object.objectPrimitiveData->type)
173 {
174 case ObjectType::Box:
175 {
176 BoxVisuPrimitivePtr boxObject =
177 BoxVisuPrimitivePtr::dynamicCast(_object.objectPrimitiveData);
178 //armarx::viz::Box box = armarx::viz::Box(_object.name)
179 objectLayer.add(
180 armarx::viz::Box(_object.name)
181 .size(
182 Eigen::Vector3f{boxObject->width, boxObject->height, boxObject->depth})
183 .color(boxObject->color.r, boxObject->color.g, boxObject->color.b)
184 .position(position)
185 .orientation(orientation));
186 }
187 break;
188 case ObjectType::Sphere:
189 {
190 SphereVisuPrimitivePtr sphereObject =
191 SphereVisuPrimitivePtr::dynamicCast(_object.objectPrimitiveData);
192 //armarx::viz::Sphere sphere = armarx::viz::Sphere(_object.name)
193 objectLayer.add(
194 armarx::viz::Sphere(_object.name)
195 .radius(sphereObject->radius)
196 .color(sphereObject->color.r, sphereObject->color.g, sphereObject->color.b)
197 .position(position)
198 .orientation(orientation));
199 }
200 break;
201 case ObjectType::Cylinder:
202 {
203 CylinderVisuPrimitivePtr cylinderObject =
204 CylinderVisuPrimitivePtr::dynamicCast(_object.objectPrimitiveData);
205 //armarx::viz::Cylinder cylinder = armarx::viz::Cylinder(_object.name)
206 objectLayer.add(armarx::viz::Cylinder(_object.name)
207 .radius(cylinderObject->radius)
208 .height(cylinderObject->length)
209 .color(cylinderObject->color.r,
210 cylinderObject->color.g,
211 cylinderObject->color.b)
212 .position(position)
213 .orientation(orientation));
214 }
215 break;
216 }
217 }
218
219 void
220 SimulatorToArviz::handleObjectFromFile(const ObjectVisuData& _object,
221 armarx::viz::Layer& objectLayer)
222 {
223 const PoseBasePtr& objectPose = _object.objectPoses.at(_object.name);
224
225 Eigen::Vector3f position(
226 objectPose->position->x, objectPose->position->y, objectPose->position->z);
227 Eigen::Quaternionf orientation(objectPose->orientation->qw,
228 objectPose->orientation->qx,
229 objectPose->orientation->qy,
230 objectPose->orientation->qz);
231
232 objectLayer.add(armarx::viz::Object(_object.name)
233 .file(_object.project, _object.filename)
234 .position(position)
235 .orientation(orientation));
236 }
237
238 void
239 SimulatorToArviz::handleObjectFromObjectClass(const ObjectVisuData& _object,
240 armarx::viz::Layer& objectLayer)
241 {
242
243 const PoseBasePtr& objectPose = _object.objectPoses.at(_object.name);
244
245 Eigen::Vector3f position(
246 objectPose->position->x, objectPose->position->y, objectPose->position->z);
247 Eigen::Quaternionf orientation(objectPose->orientation->qw,
248 objectPose->orientation->qx,
249 objectPose->orientation->qy,
250 objectPose->orientation->qz);
251 std::string filename = getClassFilename(_object.objectClassName);
252 std::string projectname = "";
253 objectLayer.add(armarx::viz::Object(_object.name)
254 .file(projectname, filename)
255 .position(position)
256 .orientation(orientation));
257 }
258
259 std::string
260 SimulatorToArviz::getClassFilename(const std::string& className)
261 {
262 auto it = classFilenameCache.find(className);
263 if (it != classFilenameCache.end())
264 {
265 return it->second;
266 }
267 else
268 {
269 std::optional<memoryx::ObjectClassWrapper> objectClass =
270 objectClassSegment.getClass(className);
271 if (!objectClass)
272 {
273 return "";
274 }
276 objectClass->classInMemory
277 ->getWrapper<memoryx::EntityWrappers::SimoxObjectWrapper>();
278 std::string filename = wrapper->getManipulationObjectFileName();
279
280 classFilenameCache[className] = filename;
281
282 return filename;
283 }
284 }
286
287} // namespace armarx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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)
Brief description of class SimulatorToArviz.
void onDisconnectComponent() override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void reportSceneUpdated(const SceneVisuData &scene, const ::Ice::Current &=::Ice::emptyCurrent) override
void onConnectComponent() override
static std::string GetDefaultName()
std::string getDefaultName() const override
Retrieve default name of component.
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