EntityDrawerComponent.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2012-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 MemoryX::ArmarXObjects::EntityDrawerComponent
19 * @author Nikolaus Vahrenkamp ( vahrenkamp at kit dot edu )
20 * @date 2015
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24
26
28
29#include <SimoxUtility/algorithm/string/string_tools.h>
30#include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
31#include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
32
33#include <Inventor/nodes/SoCube.h>
34#include <Inventor/nodes/SoMaterial.h>
35#include <Inventor/nodes/SoUnits.h>
37
38using namespace VirtualRobot;
39
40namespace memoryx
41{
42
43 static const std::string DEBUG_LAYER_NAME{"debug"};
44
48
52
53 void
55 {
56 // update custom visualizations
58 auto l2 = getScopedVisuLock();
59
60 for (auto i = accumulatedCustomUpdateData.entities.begin();
61 i != accumulatedCustomUpdateData.entities.end();
62 i++)
63 {
64 drawEntity(i->second);
65 }
66
67 accumulatedCustomUpdateData.entities.clear();
68 }
69
70 void
71 EntityDrawerComponent::removeCustomVisu(const std::string& layerName, const std::string& name)
72 {
73 removeObjectVisu(layerName, name);
74 }
75
76 void
78 {
79 auto l = getScopedVisuLock();
80
81 auto& layer = requestLayer(d.layerName);
82
83 if (!d.active)
84 {
85 ARMARX_INFO << "Removing entity " << d.name;
87 return;
88 }
89
90 // load or get object
91 VirtualRobot::SceneObjectPtr obj = requestEntity(d);
92
93 if (!obj)
94 {
95 ARMARX_ERROR << deactivateSpam() << "Could not determine object " << d.name
96 << " at layer " << d.layerName;
97 return;
98 }
99
100 if (d.updatePose)
101 {
102 obj->setGlobalPose(d.globalPose);
103 }
104
105 if (d.updateColor)
106 {
107 SoSeparator* sep = layer.addedCustomVisualizations[d.name];
108
109 if (!sep || sep->getNumChildren() < 2)
110 {
111 ARMARX_ERROR << "Internal entity layer error1";
112 return;
113 }
114
115 SoMaterial* m = dynamic_cast<SoMaterial*>(sep->getChild(0));
116
117 if (!m)
118 {
119 ARMARX_ERROR << "Internal entity layer error2";
120 return;
121 }
122
123 if (d.color.isNone())
124 {
125 m->setOverride(false);
126 }
127 else
128 {
129 if (d.color.r < 0 || d.color.g < 0 || d.color.b < 0)
130 {
131 // transparency mode
132 if (d.color.transparency == 1)
133 {
134 m->diffuseColor.setIgnored(FALSE);
135 m->setOverride(FALSE);
136 }
137 else
138 {
139 m->transparency = d.color.transparency;
140 m->diffuseColor.setIgnored(TRUE);
141 m->setOverride(TRUE);
142 }
143 }
144 else
145 {
146 // color & transparency mode
147 m->diffuseColor = SbColor(d.color.r, d.color.g, d.color.b);
148 m->ambientColor = SbColor(0, 0, 0);
149 m->transparency = std::max(0.0f, d.color.transparency);
150 m->setOverride(true);
151 }
152 }
153 }
154 }
155
156 void
157 EntityDrawerComponent::removeEntity(const std::string& layerName, const std::string& name)
158 {
159 auto l = getScopedVisuLock();
160
161 // process active entities
162 std::string entryName = "__" + layerName + "__" + name + "__";
163
164 if (activeObjects.find(entryName) != activeObjects.end())
165 {
166 activeObjects.erase(entryName);
167 }
168
169 // process visualizations
170 if (!hasLayer(layerName))
171 {
172 return;
173 }
174
175 auto& layer = layers.at(layerName);
176
177 if (layer.addedCustomVisualizations.find(name) == layer.addedCustomVisualizations.end())
178 {
179 return;
180 }
181
182 layer.mainNode->removeChild(layer.addedCustomVisualizations[name]);
183 layer.addedCustomVisualizations.erase(name);
184 }
185
186 void
188 {
189 DebugDrawerComponent::onInitComponent();
190
191 std::string commonStorageName = getProperty<std::string>("CommonStorageName").getValue();
192 usingProxy(commonStorageName);
193
194 std::string priorKnowledgeName = getProperty<std::string>("PriorKnowledgeName").getValue();
195 usingProxy(priorKnowledgeName);
196 }
197
198 void
200 {
201 DebugDrawerComponent::onConnectComponent();
202
203 std::string commonStorageName = getProperty<std::string>("CommonStorageName").getValue();
204 CommonStorageInterfacePrx commonStoragePrx =
205 getProxy<CommonStorageInterfacePrx>(commonStorageName);
206
207 if (commonStoragePrx)
208 {
209 fileManager.reset(new GridFileManager(commonStoragePrx));
210 }
211 else
212 {
213 ARMARX_ERROR << "Could not get CommonStorage Proxy";
214 }
215
216 std::string priorKnowledgeName = getProperty<std::string>("PriorKnowledgeName").getValue();
217 ARMARX_INFO << "Connecting to prior knowldege with name:" << priorKnowledgeName;
219 objectClassSegment = priorKnowledgePrx->getObjectClassesSegment();
220 }
221
222 void
224 {
225 DebugDrawerComponent::onDisconnectComponent();
226 }
227
228 void
230 {
231 DebugDrawerComponent::onExitComponent();
232 }
233
234 void
235 EntityDrawerComponent::removeObjectVisu(const std::string& layerName,
236 const std::string& objectName,
237 const Ice::Current& c)
238 {
240 std::string entryName = "__" + layerName + "__" + objectName + "__";
241 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
242 d.layerName = layerName;
243 d.name = objectName;
244 d.active = false;
245 }
246
247 void
248 EntityDrawerComponent::updateObjectColor(const std::string& layerName,
249 const std::string& objectName,
250 const armarx::DrawColor& color,
251 const Ice::Current& c)
252 {
254 std::string entryName = "__" + layerName + "__" + objectName + "__";
255 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
256
257 // update data
258 d.update = true;
259 d.updateColor = true;
260 d.layerName = layerName;
261 d.name = objectName;
262
263 if (color.a == 0 && color.b == 0 && color.r == 0 && color.g == 0)
264 {
265 d.color = VirtualRobot::VisualizationFactory::Color::None();
266 }
267 else
268 {
269 d.color =
270 VirtualRobot::VisualizationFactory::Color(color.r, color.g, color.b, 1 - color.a);
271 }
272
273 d.active = true;
274 }
275
276 void
278 const std::string& objectName,
279 float alpha,
280 const Ice::Current& c)
281 {
283 std::string entryName = "__" + layerName + "__" + objectName + "__";
284 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
285
286 if (alpha < 0)
287 {
288 alpha = 0;
289 }
290 if (alpha > 1.0f)
291 {
292 alpha = 1.0;
293 }
294
295 // update data
296 d.update = true;
297 d.updateColor = true;
298 d.layerName = layerName;
299 d.name = objectName;
300 d.color = VirtualRobot::VisualizationFactory::Color(-1, -1, -1, 1 - alpha);
301 d.active = true;
302 }
303
304 void
305 EntityDrawerComponent::updateObjectPose(const std::string& layerName,
306 const std::string& objectName,
307 const armarx::PoseBasePtr& globalPose,
308 const Ice::Current& c)
309 {
311 std::string entryName = "__" + layerName + "__" + objectName + "__";
312 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
313
314 // update data
315 d.update = true;
316 d.updatePose = true;
317 d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
318
319 d.layerName = layerName;
320 d.name = objectName;
321 d.active = true;
322 }
323
324 void
325 EntityDrawerComponent::setObjectVisu(const std::string& layerName,
326 const std::string& objectName,
327 const ObjectClassBasePtr& objectClassBase,
328 const armarx::PoseBasePtr& globalPose,
329 const Ice::Current& c)
330 {
332 std::string entryName = "__" + layerName + "__" + objectName + "__";
333 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
334
335 d.objectClass = ObjectClassPtr::dynamicCast(objectClassBase);
336 d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
337 d.layerName = layerName;
338 d.name = objectName;
339 d.active = true;
340 d.update = true;
341 d.updatePose = true;
342 }
343
344 void
345 EntityDrawerComponent::setObjectVisuByName(const std::string& layerName,
346 const std::string& customObjectName,
347 const std::string& objectClassName,
348 const armarx::PoseBasePtr& globalPose,
349 const Ice::Current& c)
350 {
351 auto objectClassBase = objectClassSegment->getObjectClassByName(objectClassName);
352 if (!objectClassBase)
353 {
354 ARMARX_WARNING << "An object with class name '" << objectClassName
355 << "' does not exist!";
356 return;
357 }
359 std::string entryName = "__" + layerName + "__" + customObjectName + "__";
360 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
361
362 d.objectClass = ObjectClassPtr::dynamicCast(objectClassBase);
363 d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
364 d.layerName = layerName;
365 d.name = customObjectName;
366 d.active = true;
367 d.update = true;
368 d.updatePose = true;
369 }
370
371 VirtualRobot::SceneObjectPtr
373 {
374 auto l = getScopedVisuLock();
375
376 auto& layer = requestLayer(d.layerName);
377 std::string entryName = "__" + d.layerName + "__" + d.name + "__";
378
379 if (activeObjects.find(entryName) != activeObjects.end())
380 {
381 return activeObjects[entryName];
382 }
383
384 VirtualRobot::SceneObjectPtr result;
385
386 try
387 {
388
389 memoryx::ObjectClassPtr objectClass = d.objectClass;
390
391 if (!objectClass)
392 {
393 ARMARX_ERROR << "Invalid object class; could not create object with instance name "
394 << d.name;
395 return result;
396 }
397 if (objectCache.count(objectClass->getId()) == 0)
398 {
400 objectClass->addWrapper(new EntityWrappers::SimoxObjectWrapper(fileManager));
401
402 VirtualRobot::ManipulationObjectPtr mo = sw->getManipulationObject();
403
404 if (!mo)
405 {
406 ARMARX_ERROR << "Could not retrieve manipulation object of object class "
407 << objectClass->getName();
408 return result;
409 }
410
411 //ARMARX_INFO << "Filename mo:" << mo->getVisualization()->getFilename();
412 //ARMARX_INFO << "Filename sw:" << sw->getManipulationObjectFileName();
413
414 mo->setName(d.name);
415 result = mo;
416 objectCache[objectClass->getId()] = mo;
417 ARMARX_LOG << "Loaded manipulation object " << mo->getName() << " of class "
418 << objectClass->getName();
419 }
420 else
421 {
422 result = objectCache[objectClass->getId()]->clone(d.name);
423 ARMARX_LOG << "Using manipulation object " << result->getName() << " of class "
424 << objectClass->getName() << " from cache";
425 }
426 }
427 catch (...)
428 {
429 ARMARX_WARNING << "Loading object " << d.objectClass->getName() << "failed";
430 return result;
431 }
432
433
434 ARMARX_INFO << "Loaded object " << d.name;
435
436 SoSeparator* sep = new SoSeparator;
437 layer.mainNode->addChild(sep);
438
439 SoMaterial* m = new SoMaterial;
440 m->setOverride(false);
441 sep->addChild(m);
442
443 auto objVisu = result->getVisualization<CoinVisualization>(VirtualRobot::SceneObject::Full);
444 SoNode* sepo = objVisu->getCoinVisualization();
445 sep->addChild(sepo);
446
447 activeObjects[entryName] = result;
448 layer.addedCustomVisualizations[d.name] = sep;
449
450 return result;
451 }
452
453 void
455 {
456 auto lockData = getScopedAccumulatedDataLock();
457 std::string entryName = "__" + layerName + "__";
458 {
459 auto i1 = accumulatedCustomUpdateData.entities.begin();
460
461 while (i1 != accumulatedCustomUpdateData.entities.end())
462 {
463 if (simox::alg::starts_with(i1->first, entryName))
464 {
465 i1 = accumulatedCustomUpdateData.entities.erase(i1);
466 }
467 else
468 {
469 i1++;
470 }
471 }
472 }
473 }
474
475 std::string
477 {
478 return "EntityDrawer";
479 }
480
481} // namespace memoryx
482
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
constexpr T c
Property< PropertyType > getProperty(const std::string &name)
DebugDrawerComponent()
DebugDrawerComponent Constructs a debug drawer.
RecursiveMutexLockPtr getScopedVisuLock()
getScopedLock If using the coin visualization it must be ensured that all rendering calls are protect...
Layer & requestLayer(const std::string &layerName)
If a layer with layerName exists it is returned. Otherwise a new layer with given name is created and...
bool hasLayer(const std::string &layerName, const ::Ice::Current &=Ice::emptyCurrent) override
RecursiveMutexLockPtr getScopedAccumulatedDataLock()
std::map< const std::string, Layer > layers
All existing layers.
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
The EntityDrawerComponent class implements a component that listens to layered / debug drawer command...
std::map< std::string, VirtualRobot::SceneObjectPtr > activeObjects
memoryx::PersistentObjectClassSegmentBasePrx objectClassSegment
void onInitComponent() override
Pure virtual hook for the subclass.
void updateObjectColor(const std::string &layerName, const std::string &objectName, const armarx::DrawColor &color, const Ice::Current &c=Ice::emptyCurrent) override
updateObjectColor Colorizes the object visualization
void updateObjectPose(const std::string &layerName, const std::string &objectName, const armarx::PoseBasePtr &globalPose, const Ice::Current &c=Ice::emptyCurrent) override
VirtualRobot::SceneObjectPtr requestEntity(const EntityData &d)
void setObjectVisuByName(const std::string &layerName, const std::string &customObjectName, const std::string &objectClassName, const armarx::PoseBasePtr &globalPose, const Ice::Current &c=Ice::emptyCurrent) override
void setObjectVisu(const std::string &layerName, const std::string &objectName, const memoryx::ObjectClassBasePtr &objectClassBase, const armarx::PoseBasePtr &globalPose, const Ice::Current &c=Ice::emptyCurrent) override
void onDisconnectComponent() override
Hook for subclass.
void removeObjectVisu(const std::string &layerName, const std::string &objectName, const Ice::Current &c=Ice::emptyCurrent) override
memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx
void removeEntity(const std::string &layerName, const std::string &name)
void removeCustomVisu(const std::string &layerName, const std::string &name) override
std::map< std::string, VirtualRobot::ManipulationObjectPtr > objectCache
void onConnectComponent() override
Pure virtual hook for the subclass.
EntityDrawerComponent()
EntityDrawerComponent Constructs a debug drawer.
void onRemoveAccumulatedData(const std::string &layerName) override
void onExitComponent() override
Hook for subclass.
void drawEntity(const EntityData &d)
void updateObjectTransparency(const std::string &layerName, const std::string &objectName, float alpha, const Ice::Current &c=Ice::emptyCurrent) override
void onUpdateVisualization() override
onUpdateVisualization derived methods can overwrite this method to update custom visualizations.
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_LOG
Definition Logging.h:165
IceInternal::Handle< SimoxObjectWrapper > SimoxObjectWrapperPtr
VirtualRobot headers.
IceInternal::Handle< ObjectClass > ObjectClassPtr
Definition ObjectClass.h:35
bool update
Whether an existing visu should be updated.
VirtualRobot::VisualizationFactory::Color color