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
27#include <SimoxUtility/algorithm/string/string_tools.h>
28#include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
29#include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
30
31#include <Inventor/nodes/SoCube.h>
32#include <Inventor/nodes/SoMaterial.h>
33#include <Inventor/nodes/SoUnits.h>
35
36using namespace VirtualRobot;
37
38namespace memoryx
39{
40
41 static const std::string DEBUG_LAYER_NAME{"debug"};
42
46
50
51 void
53 {
54 // update custom visualizations
56 auto l2 = getScopedVisuLock();
57
58 for (auto i = accumulatedCustomUpdateData.entities.begin();
59 i != accumulatedCustomUpdateData.entities.end();
60 i++)
61 {
62 drawEntity(i->second);
63 }
64
65 accumulatedCustomUpdateData.entities.clear();
66 }
67
68 void
69 EntityDrawerComponent::removeCustomVisu(const std::string& layerName, const std::string& name)
70 {
71 removeObjectVisu(layerName, name);
72 }
73
74 void
76 {
77 auto l = getScopedVisuLock();
78
79 auto& layer = requestLayer(d.layerName);
80
81 if (!d.active)
82 {
83 ARMARX_INFO << "Removing entity " << d.name;
85 return;
86 }
87
88 // load or get object
89 VirtualRobot::SceneObjectPtr obj = requestEntity(d);
90
91 if (!obj)
92 {
93 ARMARX_ERROR << deactivateSpam() << "Could not determine object " << d.name
94 << " at layer " << d.layerName;
95 return;
96 }
97
98 if (d.updatePose)
99 {
100 obj->setGlobalPose(d.globalPose);
101 }
102
103 if (d.updateColor)
104 {
105 SoSeparator* sep = layer.addedCustomVisualizations[d.name];
106
107 if (!sep || sep->getNumChildren() < 2)
108 {
109 ARMARX_ERROR << "Internal entity layer error1";
110 return;
111 }
112
113 SoMaterial* m = dynamic_cast<SoMaterial*>(sep->getChild(0));
114
115 if (!m)
116 {
117 ARMARX_ERROR << "Internal entity layer error2";
118 return;
119 }
120
121 if (d.color.isNone())
122 {
123 m->setOverride(false);
124 }
125 else
126 {
127 if (d.color.r < 0 || d.color.g < 0 || d.color.b < 0)
128 {
129 // transparency mode
130 if (d.color.transparency == 1)
131 {
132 m->diffuseColor.setIgnored(FALSE);
133 m->setOverride(FALSE);
134 }
135 else
136 {
137 m->transparency = d.color.transparency;
138 m->diffuseColor.setIgnored(TRUE);
139 m->setOverride(TRUE);
140 }
141 }
142 else
143 {
144 // color & transparency mode
145 m->diffuseColor = SbColor(d.color.r, d.color.g, d.color.b);
146 m->ambientColor = SbColor(0, 0, 0);
147 m->transparency = std::max(0.0f, d.color.transparency);
148 m->setOverride(true);
149 }
150 }
151 }
152 }
153
154 void
155 EntityDrawerComponent::removeEntity(const std::string& layerName, const std::string& name)
156 {
157 auto l = getScopedVisuLock();
158
159 // process active entities
160 std::string entryName = "__" + layerName + "__" + name + "__";
161
162 if (activeObjects.find(entryName) != activeObjects.end())
163 {
164 activeObjects.erase(entryName);
165 }
166
167 // process visualizations
168 if (!hasLayer(layerName))
169 {
170 return;
171 }
172
173 auto& layer = layers.at(layerName);
174
175 if (layer.addedCustomVisualizations.find(name) == layer.addedCustomVisualizations.end())
176 {
177 return;
178 }
179
180 layer.mainNode->removeChild(layer.addedCustomVisualizations[name]);
181 layer.addedCustomVisualizations.erase(name);
182 }
183
184 void
186 {
187 DebugDrawerComponent::onInitComponent();
188
189 std::string commonStorageName = getProperty<std::string>("CommonStorageName").getValue();
190 usingProxy(commonStorageName);
191
192 std::string priorKnowledgeName = getProperty<std::string>("PriorKnowledgeName").getValue();
193 usingProxy(priorKnowledgeName);
194 }
195
196 void
198 {
199 DebugDrawerComponent::onConnectComponent();
200
201 std::string commonStorageName = getProperty<std::string>("CommonStorageName").getValue();
202 CommonStorageInterfacePrx commonStoragePrx =
203 getProxy<CommonStorageInterfacePrx>(commonStorageName);
204
205 if (commonStoragePrx)
206 {
207 fileManager.reset(new GridFileManager(commonStoragePrx));
208 }
209 else
210 {
211 ARMARX_ERROR << "Could not get CommonStorage Proxy";
212 }
213
214 std::string priorKnowledgeName = getProperty<std::string>("PriorKnowledgeName").getValue();
215 ARMARX_INFO << "Connecting to prior knowldege with name:" << priorKnowledgeName;
217 objectClassSegment = priorKnowledgePrx->getObjectClassesSegment();
218 }
219
220 void
222 {
223 DebugDrawerComponent::onDisconnectComponent();
224 }
225
226 void
228 {
229 DebugDrawerComponent::onExitComponent();
230 }
231
232 void
233 EntityDrawerComponent::removeObjectVisu(const std::string& layerName,
234 const std::string& objectName,
235 const Ice::Current& c)
236 {
238 std::string entryName = "__" + layerName + "__" + objectName + "__";
239 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
240 d.layerName = layerName;
241 d.name = objectName;
242 d.active = false;
243 }
244
245 void
246 EntityDrawerComponent::updateObjectColor(const std::string& layerName,
247 const std::string& objectName,
248 const armarx::DrawColor& color,
249 const Ice::Current& c)
250 {
252 std::string entryName = "__" + layerName + "__" + objectName + "__";
253 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
254
255 // update data
256 d.update = true;
257 d.updateColor = true;
258 d.layerName = layerName;
259 d.name = objectName;
260
261 if (color.a == 0 && color.b == 0 && color.r == 0 && color.g == 0)
262 {
263 d.color = VirtualRobot::VisualizationFactory::Color::None();
264 }
265 else
266 {
267 d.color =
268 VirtualRobot::VisualizationFactory::Color(color.r, color.g, color.b, 1 - color.a);
269 }
270
271 d.active = true;
272 }
273
274 void
276 const std::string& objectName,
277 float alpha,
278 const Ice::Current& c)
279 {
281 std::string entryName = "__" + layerName + "__" + objectName + "__";
282 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
283
284 if (alpha < 0)
285 {
286 alpha = 0;
287 }
288 if (alpha > 1.0f)
289 {
290 alpha = 1.0;
291 }
292
293 // update data
294 d.update = true;
295 d.updateColor = true;
296 d.layerName = layerName;
297 d.name = objectName;
298 d.color = VirtualRobot::VisualizationFactory::Color(-1, -1, -1, 1 - alpha);
299 d.active = true;
300 }
301
302 void
303 EntityDrawerComponent::updateObjectPose(const std::string& layerName,
304 const std::string& objectName,
305 const armarx::PoseBasePtr& globalPose,
306 const Ice::Current& c)
307 {
309 std::string entryName = "__" + layerName + "__" + objectName + "__";
310 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
311
312 // update data
313 d.update = true;
314 d.updatePose = true;
315 d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
316
317 d.layerName = layerName;
318 d.name = objectName;
319 d.active = true;
320 }
321
322 void
323 EntityDrawerComponent::setObjectVisu(const std::string& layerName,
324 const std::string& objectName,
325 const ObjectClassBasePtr& objectClassBase,
326 const armarx::PoseBasePtr& globalPose,
327 const Ice::Current& c)
328 {
330 std::string entryName = "__" + layerName + "__" + objectName + "__";
331 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
332
333 d.objectClass = ObjectClassPtr::dynamicCast(objectClassBase);
334 d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
335 d.layerName = layerName;
336 d.name = objectName;
337 d.active = true;
338 d.update = true;
339 d.updatePose = true;
340 }
341
342 void
343 EntityDrawerComponent::setObjectVisuByName(const std::string& layerName,
344 const std::string& customObjectName,
345 const std::string& objectClassName,
346 const armarx::PoseBasePtr& globalPose,
347 const Ice::Current& c)
348 {
349 auto objectClassBase = objectClassSegment->getObjectClassByName(objectClassName);
350 if (!objectClassBase)
351 {
352 ARMARX_WARNING << "An object with class name '" << objectClassName
353 << "' does not exist!";
354 return;
355 }
357 std::string entryName = "__" + layerName + "__" + customObjectName + "__";
358 EntityData& d = accumulatedCustomUpdateData.entities[entryName];
359
360 d.objectClass = ObjectClassPtr::dynamicCast(objectClassBase);
361 d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
362 d.layerName = layerName;
363 d.name = customObjectName;
364 d.active = true;
365 d.update = true;
366 d.updatePose = true;
367 }
368
369 VirtualRobot::SceneObjectPtr
371 {
372 auto l = getScopedVisuLock();
373
374 auto& layer = requestLayer(d.layerName);
375 std::string entryName = "__" + d.layerName + "__" + d.name + "__";
376
377 if (activeObjects.find(entryName) != activeObjects.end())
378 {
379 return activeObjects[entryName];
380 }
381
382 VirtualRobot::SceneObjectPtr result;
383
384 try
385 {
386
387 memoryx::ObjectClassPtr objectClass = d.objectClass;
388
389 if (!objectClass)
390 {
391 ARMARX_ERROR << "Invalid object class; could not create object with instance name "
392 << d.name;
393 return result;
394 }
395 if (objectCache.count(objectClass->getId()) == 0)
396 {
398 objectClass->addWrapper(new EntityWrappers::SimoxObjectWrapper(fileManager));
399
400 VirtualRobot::ManipulationObjectPtr mo = sw->getManipulationObject();
401
402 if (!mo)
403 {
404 ARMARX_ERROR << "Could not retrieve manipulation object of object class "
405 << objectClass->getName();
406 return result;
407 }
408
409 //ARMARX_INFO << "Filename mo:" << mo->getVisualization()->getFilename();
410 //ARMARX_INFO << "Filename sw:" << sw->getManipulationObjectFileName();
411
412 mo->setName(d.name);
413 result = mo;
414 objectCache[objectClass->getId()] = mo;
415 ARMARX_LOG << "Loaded manipulation object " << mo->getName() << " of class "
416 << objectClass->getName();
417 }
418 else
419 {
420 result = objectCache[objectClass->getId()]->clone(d.name);
421 ARMARX_LOG << "Using manipulation object " << result->getName() << " of class "
422 << objectClass->getName() << " from cache";
423 }
424 }
425 catch (...)
426 {
427 ARMARX_WARNING << "Loading object " << d.objectClass->getName() << "failed";
428 return result;
429 }
430
431
432 ARMARX_INFO << "Loaded object " << d.name;
433
434 SoSeparator* sep = new SoSeparator;
435 layer.mainNode->addChild(sep);
436
437 SoMaterial* m = new SoMaterial;
438 m->setOverride(false);
439 sep->addChild(m);
440
441 auto objVisu = result->getVisualization<CoinVisualization>(VirtualRobot::SceneObject::Full);
442 SoNode* sepo = objVisu->getCoinVisualization();
443 sep->addChild(sepo);
444
445 activeObjects[entryName] = result;
446 layer.addedCustomVisualizations[d.name] = sep;
447
448 return result;
449 }
450
451 void
453 {
454 auto lockData = getScopedAccumulatedDataLock();
455 std::string entryName = "__" + layerName + "__";
456 {
457 auto i1 = accumulatedCustomUpdateData.entities.begin();
458
459 while (i1 != accumulatedCustomUpdateData.entities.end())
460 {
461 if (simox::alg::starts_with(i1->first, entryName))
462 {
463 i1 = accumulatedCustomUpdateData.entities.erase(i1);
464 }
465 else
466 {
467 i1++;
468 }
469 }
470 }
471 }
472
473} // namespace memoryx
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)
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