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 <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 
36 using namespace VirtualRobot;
37 
38 namespace memoryx
39 {
40 
41  static const std::string DEBUG_LAYER_NAME{"debug"};
42 
43  EntityDrawerComponent::EntityDrawerComponent() : DebugDrawerComponent()
44  {
45  }
46 
48  {
49  }
50 
51  void
53  {
54  // update custom visualizations
55  auto l1 = getScopedAccumulatedDataLock();
56  auto l2 = getScopedVisuLock();
57 
58  for (auto i = accumulatedCustomUpdateData.entities.begin();
60  i++)
61  {
62  drawEntity(i->second);
63  }
64 
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;
84  removeEntity(d.layerName, d.name);
85  return;
86  }
87 
88  // load or get object
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;
216  priorKnowledgePrx = getProxy<PriorKnowledgeInterfacePrx>(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  {
237  auto l = getScopedAccumulatedDataLock();
238  std::string entryName = "__" + layerName + "__" + objectName + "__";
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  {
251  auto l = getScopedAccumulatedDataLock();
252  std::string entryName = "__" + layerName + "__" + objectName + "__";
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  {
280  auto l = getScopedAccumulatedDataLock();
281  std::string entryName = "__" + layerName + "__" + objectName + "__";
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  {
308  auto l = getScopedAccumulatedDataLock();
309  std::string entryName = "__" + layerName + "__" + objectName + "__";
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  {
329  auto l = getScopedAccumulatedDataLock();
330  std::string entryName = "__" + layerName + "__" + objectName + "__";
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  }
356  auto l = getScopedAccumulatedDataLock();
357  std::string entryName = "__" + layerName + "__" + customObjectName + "__";
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 
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 
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
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:323
memoryx::EntityDrawerComponent::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: EntityDrawerComponent.cpp:197
memoryx::EntityDrawerComponent::accumulatedCustomUpdateData
EntityUpdateData accumulatedCustomUpdateData
Definition: EntityDrawerComponent.h:170
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:343
VirtualRobot
Definition: FramedPose.h:42
armarx::DebugDrawerComponent::getScopedVisuLock
RecursiveMutexLockPtr getScopedVisuLock()
getScopedLock If using the coin visualization it must be ensured that all rendering calls are protect...
Definition: DebugDrawerComponent.cpp:2715
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:275
memoryx::EntityDrawerComponent::~EntityDrawerComponent
~EntityDrawerComponent() override
Definition: EntityDrawerComponent.cpp:47
memoryx::EntityDrawerComponent::objectClassSegment
memoryx::PersistentObjectClassSegmentBasePrx objectClassSegment
Definition: EntityDrawerComponent.h:185
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::DebugDrawerComponent::hasLayer
bool hasLayer(const std::string &layerName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: DebugDrawerComponent.cpp:2888
memoryx::EntityDrawerComponent::removeEntity
void removeEntity(const std::string &layerName, const std::string &name)
Definition: EntityDrawerComponent.cpp:155
memoryx::EntityDrawerComponent::onUpdateVisualization
void onUpdateVisualization() override
onUpdateVisualization derived methods can overwrite this method to update custom visualizations.
Definition: EntityDrawerComponent.cpp:52
memoryx::EntityDrawerComponent::EntityUpdateData::entities
std::map< std::string, EntityData > entities
Definition: EntityDrawerComponent.h:167
armarx::starts_with
bool starts_with(const std::string &haystack, const std::string &needle)
Definition: StringHelpers.cpp:47
memoryx::EntityDrawerComponent::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: EntityDrawerComponent.cpp:221
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:303
IceInternal::Handle< ObjectClass >
memoryx::EntityDrawerComponent::fileManager
GridFileManagerPtr fileManager
Definition: EntityDrawerComponent.h:187
Color
uint32_t Color
RGBA color.
Definition: color.h:8
memoryx::EntityDrawerComponent::objectCache
std::map< std::string, VirtualRobot::ManipulationObjectPtr > objectCache
Definition: EntityDrawerComponent.h:182
memoryx::EntityDrawerComponent::EntityData::globalPose
Eigen::Matrix4f globalPose
Definition: EntityDrawerComponent.h:162
memoryx::EntityDrawerComponent::priorKnowledgePrx
memoryx::PriorKnowledgeInterfacePrx priorKnowledgePrx
Definition: EntityDrawerComponent.h:184
armarx::DebugDrawerComponent::getScopedAccumulatedDataLock
RecursiveMutexLockPtr getScopedAccumulatedDataLock()
Definition: DebugDrawerComponent.cpp:2733
max
T max(T t1, T t2)
Definition: gdiam.h:51
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:246
ARMARX_ERROR
#define ARMARX_ERROR
Definition: Logging.h:196
memoryx::EntityDrawerComponent::requestEntity
VirtualRobot::SceneObjectPtr requestEntity(const EntityData &d)
Definition: EntityDrawerComponent.cpp:370
memoryx::EntityDrawerComponent::EntityData::updatePose
bool updatePose
Definition: EntityDrawerComponent.h:161
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:2746
memoryx::EntityDrawerComponent::drawEntity
void drawEntity(const EntityData &d)
Definition: EntityDrawerComponent.cpp:75
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:159
ARMARX_LOG
#define ARMARX_LOG
Definition: Logging.h:165
memoryx::EntityDrawerComponent::activeObjects
std::map< std::string, VirtualRobot::SceneObjectPtr > activeObjects
Definition: EntityDrawerComponent.h:181
memoryx::EntityDrawerComponent::EntityData
Definition: EntityDrawerComponent.h:148
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:233
memoryx::EntityDrawerComponent::onRemoveAccumulatedData
void onRemoveAccumulatedData(const std::string &layerName) override
Definition: EntityDrawerComponent.cpp:452
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
memoryx::EntityDrawerComponent::EntityData::updateColor
bool updateColor
Definition: EntityDrawerComponent.h:158
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:41
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:99
memoryx::EntityDrawerComponent::removeCustomVisu
void removeCustomVisu(const std::string &layerName, const std::string &name) override
Definition: EntityDrawerComponent.cpp:69
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
memoryx::EntityDrawerComponent::EntityData::objectClass
memoryx::ObjectClassPtr objectClass
Definition: EntityDrawerComponent.h:156
memoryx::EntityDrawerComponent::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: EntityDrawerComponent.cpp:227
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:154
armarx::DebugDrawerComponent::layers
std::map< const std::string, Layer > layers
All existing layers.
Definition: DebugDrawerComponent.h:849