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 
25 #include "EntityDrawerComponent.h"
26 
27 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
28 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
29 #include <Inventor/nodes/SoUnits.h>
30 #include <Inventor/nodes/SoCube.h>
31 #include <Inventor/nodes/SoMaterial.h>
33 
34 #include <SimoxUtility/algorithm/string/string_tools.h>
35 
36 using namespace VirtualRobot;
37 
38 namespace memoryx
39 {
40 
41  static const std::string DEBUG_LAYER_NAME
42  {
43  "debug"
44  };
45 
46  EntityDrawerComponent::EntityDrawerComponent():
47  DebugDrawerComponent()
48  {
49 
50  }
51 
53  {
54 
55  }
56 
58  {
59  // update custom visualizations
60  auto l1 = getScopedAccumulatedDataLock();
61  auto l2 = getScopedVisuLock();
62 
63  for (auto i = accumulatedCustomUpdateData.entities.begin(); i != accumulatedCustomUpdateData.entities.end(); i++)
64  {
65  drawEntity(i->second);
66  }
67 
69 
70  }
71 
72  void EntityDrawerComponent::removeCustomVisu(const std::string& layerName, const std::string& name)
73  {
74  removeObjectVisu(layerName, name);
75  }
76 
77 
79  {
80  auto l = getScopedVisuLock();
81 
82  auto& layer = requestLayer(d.layerName);
83 
84  if (!d.active)
85  {
86  ARMARX_INFO << "Removing entity " << d.name;
87  removeEntity(d.layerName, d.name);
88  return;
89  }
90 
91  // load or get object
93 
94  if (!obj)
95  {
96  ARMARX_ERROR << deactivateSpam() << "Could not determine object " << d.name << " 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 EntityDrawerComponent::removeEntity(const std::string& layerName, const std::string& name)
157  {
158  auto l = getScopedVisuLock();
159 
160  // process active entities
161  std::string entryName = "__" + layerName + "__" + name + "__";
162 
163  if (activeObjects.find(entryName) != activeObjects.end())
164  {
165  activeObjects.erase(entryName);
166  }
167 
168  // process visualizations
169  if (!hasLayer(layerName))
170  {
171  return;
172  }
173 
174  auto& layer = layers.at(layerName);
175 
176  if (layer.addedCustomVisualizations.find(name) == layer.addedCustomVisualizations.end())
177  {
178  return;
179  }
180 
181  layer.mainNode->removeChild(layer.addedCustomVisualizations[name]);
182  layer.addedCustomVisualizations.erase(name);
183  }
184 
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 
197  {
198  DebugDrawerComponent::onConnectComponent();
199 
200  std::string commonStorageName = getProperty<std::string>("CommonStorageName").getValue();
201  CommonStorageInterfacePrx commonStoragePrx = getProxy<CommonStorageInterfacePrx>(commonStorageName);
202 
203  if (commonStoragePrx)
204  {
205  fileManager.reset(new GridFileManager(commonStoragePrx));
206  }
207  else
208  {
209  ARMARX_ERROR << "Could not get CommonStorage Proxy";
210  }
211 
212  std::string priorKnowledgeName = getProperty<std::string>("PriorKnowledgeName").getValue();
213  ARMARX_INFO << "Connecting to prior knowldege with name:" << priorKnowledgeName;
214  priorKnowledgePrx = getProxy<PriorKnowledgeInterfacePrx>(priorKnowledgeName);
215  objectClassSegment = priorKnowledgePrx->getObjectClassesSegment();
216  }
217 
219  {
220  DebugDrawerComponent::onDisconnectComponent();
221  }
222 
224  {
225  DebugDrawerComponent::onExitComponent();
226  }
227 
228  void EntityDrawerComponent::removeObjectVisu(const std::string& layerName, const std::string& objectName, const Ice::Current& c)
229  {
230  auto l = getScopedAccumulatedDataLock();
231  std::string entryName = "__" + layerName + "__" + objectName + "__";
233  d.layerName = layerName;
234  d.name = objectName;
235  d.active = false;
236  }
237 
238  void EntityDrawerComponent::updateObjectColor(const std::string& layerName, const std::string& objectName, const armarx::DrawColor& color, const Ice::Current& c)
239  {
240  auto l = getScopedAccumulatedDataLock();
241  std::string entryName = "__" + layerName + "__" + objectName + "__";
243 
244  // update data
245  d.update = true;
246  d.updateColor = true;
247  d.layerName = layerName;
248  d.name = objectName;
249 
250  if (color.a == 0 && color.b == 0 && color.r == 0 && color.g == 0)
251  {
252  d.color = VirtualRobot::VisualizationFactory::Color::None();
253  }
254  else
255  {
256  d.color = VirtualRobot::VisualizationFactory::Color(color.r, color.g, color.b, 1 - color.a);
257  }
258 
259  d.active = true;
260  }
261 
262  void EntityDrawerComponent::updateObjectTransparency(const std::string& layerName, const std::string& objectName, float alpha, const Ice::Current& c)
263  {
264  auto l = getScopedAccumulatedDataLock();
265  std::string entryName = "__" + layerName + "__" + objectName + "__";
267 
268  if (alpha < 0)
269  {
270  alpha = 0;
271  }
272  if (alpha > 1.0f)
273  {
274  alpha = 1.0;
275  }
276 
277  // update data
278  d.update = true;
279  d.updateColor = true;
280  d.layerName = layerName;
281  d.name = objectName;
282  d.color = VirtualRobot::VisualizationFactory::Color(-1, -1, -1, 1 - alpha);
283  d.active = true;
284  }
285 
286  void EntityDrawerComponent::updateObjectPose(const std::string& layerName, const std::string& objectName, const armarx::PoseBasePtr& globalPose, const Ice::Current& c)
287  {
288  auto l = getScopedAccumulatedDataLock();
289  std::string entryName = "__" + layerName + "__" + objectName + "__";
291 
292  // update data
293  d.update = true;
294  d.updatePose = true;
295  d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
296 
297  d.layerName = layerName;
298  d.name = objectName;
299  d.active = true;
300  }
301 
302  void EntityDrawerComponent::setObjectVisu(const std::string& layerName, const std::string& objectName, const ObjectClassBasePtr& objectClassBase, const armarx::PoseBasePtr& globalPose, const Ice::Current& c)
303  {
304  auto l = getScopedAccumulatedDataLock();
305  std::string entryName = "__" + layerName + "__" + objectName + "__";
307 
308  d.objectClass = ObjectClassPtr::dynamicCast(objectClassBase);
309  d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
310  d.layerName = layerName;
311  d.name = objectName;
312  d.active = true;
313  d.update = true;
314  d.updatePose = true;
315  }
316 
317  void EntityDrawerComponent::setObjectVisuByName(const std::string& layerName, const std::string& customObjectName, const std::string& objectClassName, const armarx::PoseBasePtr& globalPose, const Ice::Current& c)
318  {
319  auto objectClassBase = objectClassSegment->getObjectClassByName(objectClassName);
320  if (!objectClassBase)
321  {
322  ARMARX_WARNING << "An object with class name '" << objectClassName << "' does not exist!";
323  return;
324  }
325  auto l = getScopedAccumulatedDataLock();
326  std::string entryName = "__" + layerName + "__" + customObjectName + "__";
328 
329  d.objectClass = ObjectClassPtr::dynamicCast(objectClassBase);
330  d.globalPose = armarx::PosePtr::dynamicCast(globalPose)->toEigen();
331  d.layerName = layerName;
332  d.name = customObjectName;
333  d.active = true;
334  d.update = true;
335  d.updatePose = true;
336  }
337 
339  {
340  auto l = getScopedVisuLock();
341 
342  auto& layer = requestLayer(d.layerName);
343  std::string entryName = "__" + d.layerName + "__" + d.name + "__";
344 
345  if (activeObjects.find(entryName) != activeObjects.end())
346  {
347  return activeObjects[entryName];
348  }
349 
351 
352  try
353  {
354 
355  memoryx::ObjectClassPtr objectClass = d.objectClass;
356 
357  if (!objectClass)
358  {
359  ARMARX_ERROR << "Invalid object class; could not create object with instance name " << d.name;
360  return result;
361  }
362  if (objectCache.count(objectClass->getId()) == 0)
363  {
365 
366  VirtualRobot::ManipulationObjectPtr mo = sw->getManipulationObject();
367 
368  if (!mo)
369  {
370  ARMARX_ERROR << "Could not retrieve manipulation object of object class " << objectClass->getName();
371  return result;
372  }
373 
374  //ARMARX_INFO << "Filename mo:" << mo->getVisualization()->getFilename();
375  //ARMARX_INFO << "Filename sw:" << sw->getManipulationObjectFileName();
376 
377  mo->setName(d.name);
378  result = mo;
379  objectCache[objectClass->getId()] = mo;
380  ARMARX_LOG << "Loaded manipulation object " << mo->getName() << " of class " << objectClass->getName();
381  }
382  else
383  {
384  result = objectCache[objectClass->getId()]->clone(d.name);
385  ARMARX_LOG << "Using manipulation object " << result->getName() << " of class " << objectClass->getName() << " from cache";
386  }
387 
388  }
389  catch (...)
390  {
391  ARMARX_WARNING << "Loading object " << d.objectClass->getName() << "failed";
392  return result;
393  }
394 
395 
396 
397  ARMARX_INFO << "Loaded object " << d.name;
398 
399  SoSeparator* sep = new SoSeparator;
400  layer.mainNode->addChild(sep);
401 
402  SoMaterial* m = new SoMaterial;
403  m->setOverride(false);
404  sep->addChild(m);
405 
406  auto objVisu = result->getVisualization<CoinVisualization>(VirtualRobot::SceneObject::Full);
407  SoNode* sepo = objVisu->getCoinVisualization();
408  sep->addChild(sepo);
409 
410  activeObjects[entryName] = result;
411  layer.addedCustomVisualizations[d.name] = sep;
412 
413  return result;
414  }
415 
416 
417  void EntityDrawerComponent::onRemoveAccumulatedData(const std::string& layerName)
418  {
419  auto lockData = getScopedAccumulatedDataLock();
420  std::string entryName = "__" + layerName + "__";
421  {
422  auto i1 = accumulatedCustomUpdateData.entities.begin();
423 
424  while (i1 != accumulatedCustomUpdateData.entities.end())
425  {
426  if (simox::alg::starts_with(i1->first, entryName))
427  {
428  i1 = accumulatedCustomUpdateData.entities.erase(i1);
429  }
430  else
431  {
432  i1++;
433  }
434  }
435  }
436  }
437 
438 }
439 
memoryx::EntityDrawerComponent::setObjectVisu
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
Definition: EntityDrawerComponent.cpp:302
memoryx::EntityDrawerComponent::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: EntityDrawerComponent.cpp:196
memoryx::EntityDrawerComponent::accumulatedCustomUpdateData
EntityUpdateData accumulatedCustomUpdateData
Definition: EntityDrawerComponent.h:147
memoryx::EntityDrawerComponent::setObjectVisuByName
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
Definition: EntityDrawerComponent.cpp:317
VirtualRobot
Definition: FramedPose.h:43
armarx::DebugDrawerComponent::getScopedVisuLock
RecursiveMutexLockPtr getScopedVisuLock()
getScopedLock If using the coin visualization it must be ensured that all rendering calls are protect...
Definition: DebugDrawerComponent.cpp:2365
memoryx::EntityDrawerComponent::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: EntityDrawerComponent.cpp:185
memoryx::EntityDrawerComponent::updateObjectTransparency
void updateObjectTransparency(const std::string &layerName, const std::string &objectName, float alpha, const Ice::Current &c=Ice::emptyCurrent) override
Definition: EntityDrawerComponent.cpp:262
memoryx::EntityDrawerComponent::~EntityDrawerComponent
~EntityDrawerComponent() override
Definition: EntityDrawerComponent.cpp:52
memoryx::EntityDrawerComponent::objectClassSegment
memoryx::PersistentObjectClassSegmentBasePrx objectClassSegment
Definition: EntityDrawerComponent.h:162
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::DebugDrawerComponent::hasLayer
bool hasLayer(const std::string &layerName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DebugDrawerComponent.cpp:2533
memoryx::EntityDrawerComponent::removeEntity
void removeEntity(const std::string &layerName, const std::string &name)
Definition: EntityDrawerComponent.cpp:156
memoryx::EntityDrawerComponent::onUpdateVisualization
void onUpdateVisualization() override
onUpdateVisualization derived methods can overwrite this method to update custom visualizations.
Definition: EntityDrawerComponent.cpp:57
memoryx::EntityDrawerComponent::EntityUpdateData::entities
std::map< std::string, EntityData > entities
Definition: EntityDrawerComponent.h:144
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:43
memoryx::EntityDrawerComponent::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: EntityDrawerComponent.cpp:218
memoryx::EntityDrawerComponent::activeObjects
std::map< std::string, VirtualRobot::SceneObjectPtr > activeObjects
Definition: EntityDrawerComponent.h:158
memoryx::EntityDrawerComponent::updateObjectPose
void updateObjectPose(const std::string &layerName, const std::string &objectName, const armarx::PoseBasePtr &globalPose, const Ice::Current &c=Ice::emptyCurrent) override
Definition: EntityDrawerComponent.cpp:286
IceInternal::Handle< ObjectClass >
memoryx::EntityDrawerComponent::fileManager
GridFileManagerPtr fileManager
Definition: EntityDrawerComponent.h:164
Color
uint32_t Color
RGBA color.
Definition: color.h:8
memoryx::EntityDrawerComponent::EntityData::globalPose
Eigen::Matrix4f globalPose
Definition: EntityDrawerComponent.h:139
memoryx::EntityDrawerComponent::priorKnowledgePrx
memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx
Definition: EntityDrawerComponent.h:161
armarx::DebugDrawerComponent::getScopedAccumulatedDataLock
RecursiveMutexLockPtr getScopedAccumulatedDataLock()
Definition: DebugDrawerComponent.cpp:2382
max
T max(T t1, T t2)
Definition: gdiam.h:48
memoryx::EntityDrawerComponent::updateObjectColor
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
Definition: EntityDrawerComponent.cpp:238
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:189
memoryx::EntityDrawerComponent::requestEntity
VirtualRobot::SceneObjectPtr requestEntity(const EntityData &d)
Definition: EntityDrawerComponent.cpp:338
memoryx::EntityDrawerComponent::EntityData::updatePose
bool updatePose
Definition: EntityDrawerComponent.h:138
armarx::DebugDrawerComponent::requestLayer
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...
Definition: DebugDrawerComponent.cpp:2393
memoryx::EntityDrawerComponent::drawEntity
void drawEntity(const EntityData &d)
Definition: EntityDrawerComponent.cpp:78
memoryx::EntityDrawerComponent::objectCache
std::map< std::string, VirtualRobot::ManipulationObjectPtr > objectCache
Definition: EntityDrawerComponent.h:159
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
memoryx::EntityDrawerComponent::EntityData::color
VirtualRobot::VisualizationFactory::Color color
Definition: EntityDrawerComponent.h:136
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:163
memoryx::EntityDrawerComponent::EntityData
Definition: EntityDrawerComponent.h:126
EntityDrawerComponent.h
SimoxObjectWrapper.h
memoryx::EntityDrawerComponent::removeObjectVisu
void removeObjectVisu(const std::string &layerName, const std::string &objectName, const Ice::Current &c=Ice::emptyCurrent) override
Definition: EntityDrawerComponent.cpp:228
memoryx::EntityDrawerComponent::onRemoveAccumulatedData
void onRemoveAccumulatedData(const std::string &layerName) override
Definition: EntityDrawerComponent.cpp:417
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
memoryx::EntityDrawerComponent::EntityData::updateColor
bool updateColor
Definition: EntityDrawerComponent.h:135
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:42
armarx::Logging::deactivateSpam
SpamFilterDataPtr deactivateSpam(float deactivationDurationSec=10.0f, const std::string &identifier="", bool deactivate=true) const
disables the logging for the current line for the given amount of seconds.
Definition: Logging.cpp:92
memoryx::EntityDrawerComponent::removeCustomVisu
void removeCustomVisu(const std::string &layerName, const std::string &name) override
Definition: EntityDrawerComponent.cpp:72
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
memoryx::EntityDrawerComponent::EntityData::objectClass
memoryx::ObjectClassPtr objectClass
Definition: EntityDrawerComponent.h:133
memoryx::EntityDrawerComponent::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: EntityDrawerComponent.cpp:223
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:151
armarx::DebugDrawerComponent::layers
std::map< const std::string, Layer > layers
All existing layers.
Definition: DebugDrawerComponent.h:596