ObjectInstancesToArViz.cpp
Go to the documentation of this file.
2 
3 #include <VirtualRobot/RuntimeEnvironment.h>
4 
7 
9 
10 namespace memoryx
11 {
12 
14  {
15  }
16 
17  void
19  {
20  this->arviz = arviz;
21  }
22 
23  void
25  const PriorKnowledgeInterfacePrx& priorKnowledge,
26  const WorkingMemoryInterfacePrx& workingMemory,
27  const armarx::RobotStateComponentInterfacePrx& robotStateComponent,
28  const std::vector<std::string>& datasets)
29  {
31  ARMARX_CHECK_NOT_NULL(priorKnowledge);
32  ARMARX_CHECK_NOT_NULL(workingMemory);
33  ARMARX_CHECK_NOT_NULL(robotStateComponent);
34 
35  objectClassSegment.initFromProxy(priorKnowledge, datasets);
36  attachments.initFromProxies(workingMemory, robotStateComponent);
37 
38  this->robotStateComponent = robotStateComponent;
40  robotStateComponent, "", {}, VirtualRobot::RobotIO::RobotDescription::eStructure);
41  this->objectInstanceSegment = workingMemory->getObjectInstancesSegment();
42  }
43 
44  void
45  ObjectInstancesToArViz::updateFloorPolygon(const Eigen::Vector2f& extents, float height)
46  {
48  arviz.commitLayerContaining(layerNameFloor, makeFloorPolygon(extents, height));
49  }
50 
51  void
53  {
56  makeFloorObject().position(Eigen::Vector3f(0, 0, height)));
57  }
58 
59  void
61  {
63  /* We are not using the `memoryx::ObjectInstanceWrapper`, since it internally clones a
64  * `VirtualRobot::ManipulationObjectPtr`, which is a very costly operation
65  * (e.g. 300 ms for 10 objects). */
66  std::vector<ObjectInstancePtr> objectInstances = attachments.queryObjects();
67 
68  updateObjects(objectInstances);
69  }
70 
71  void
72  ObjectInstancesToArViz::updateObjects(const std::vector<ObjectInstancePtr>& objectInstances)
73  {
76  for (const auto& object : objectInstances)
77  {
78  if (auto vizObject = makeObject(object))
79  {
80  layer.add(*vizObject);
81  }
82  }
83  arviz.commit({layer});
84  }
85 
87  ObjectInstancesToArViz::makeFloorPolygon(const Eigen::Vector2f& extents, float height)
88  {
90  return armarx::viz::Polygon("Floor")
91  .plane(Eigen::Vector3f(0, 0, height), Eigen::Quaternionf::Identity(), extents)
92  .color(simox::Color::gray(200));
93  }
94 
96  ObjectInstancesToArViz::makeFloorObject(const std::string& name)
97  {
99  return armarx::viz::Object(name).file("", getFloorObjectFile());
100  }
101 
102  std::string
104  {
105  ARMARX_TRACE;
106  std::string filename = this->floorObjectFilename;
107  VirtualRobot::RuntimeEnvironment::getDataFileAbsolute(filename);
108  return filename;
109  }
110 
111  std::optional<armarx::viz::Object>
113  {
114  ARMARX_TRACE;
115  std::string objectName = object->getName();
116  const std::string className = object->getMostProbableClass();
117  const std::string vizName = objectName + "_" + object->getId();
118 
119  if (objectClassBlackWhitelist.isExcluded(className))
120  {
121  return std::nullopt;
122  }
123 
124  // Get filename.
125  std::string filename = getClassFilename(className);
126  if (filename.empty())
127  {
128  ARMARX_WARNING << deactivateSpam() << "Could not find object class '" << className
129  << "'. "
130  << "Skipping object '" << object->getName() << "'.";
131  return std::nullopt;
132  }
133 
134  // Get global pose.
135  Eigen::Matrix4f globalPose = attachments.getObjectPoseInFrame(object, armarx::GlobalFrame);
136 
137  return armarx::viz::Object(vizName).pose(globalPose).file("", filename);
138  }
139 
140  std::string
141  ObjectInstancesToArViz::getClassFilename(const std::string& className)
142  {
143  ARMARX_TRACE;
144  if (auto it = classFilenameCache.find(className); it != classFilenameCache.end())
145  {
146  // From cache.
147  return it->second;
148  }
149  else
150  {
151  // Get filename via class object.
152  std::optional<ObjectClassWrapper> objectClass = objectClassSegment.getClass(className);
153  if (!objectClass)
154  {
155  return "";
156  }
158  objectClass->classInMemory->getWrapper<EntityWrappers::SimoxObjectWrapper>();
159 
160  std::string filename = wrapper->getManipulationObjectFileName();
161 
162  // Update cache.
163  classFilenameCache[className] = filename;
164 
165  return filename;
166  }
167  }
168 
169  void
171  const armarx::BlackWhitelistUpdate& update)
172  {
173  ARMARX_TRACE;
174  armarx::updateBlackWhitelist(objectClassBlackWhitelist, update);
175  ARMARX_VERBOSE << "Updated object class black-whitelist: \n" << objectClassBlackWhitelist;
176  }
177 
178  void
179  ObjectInstancesToArViz::attachObjectToRobotNode(const AttachObjectToRobotNodeInput& attachment)
180  {
181  ARMARX_TRACE;
182  attachments.attachObjectToRobotNode(attachment);
183  }
184 
185  void
187  const DetachObjectFromRobotNodeInput& detachment)
188  {
189  ARMARX_TRACE;
190  attachments.detachObjectFromRobotNode(detachment);
191  }
192 
193 } // namespace memoryx
memoryx::ObjectInstancesToArViz::setArViz
void setArViz(armarx::viz::Client arviz)
Set the ArViz client.
Definition: ObjectInstancesToArViz.cpp:18
memoryx::ObjectInstancesToArViz::makeFloorPolygon
armarx::viz::Polygon makeFloorPolygon(const Eigen::Vector2f &extents={10000, 10000}, float height=0)
Make a polygon representing the floor.
Definition: ObjectInstancesToArViz.cpp:87
RemoteRobot.h
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:89
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
memoryx::ObjectInstancesToArViz::updateFloorPolygon
void updateFloorPolygon(const Eigen::Vector2f &extents={10000, 10000}, float height=0)
Draw a the floor as a polygon.
Definition: ObjectInstancesToArViz.cpp:45
memoryx::ObjectInstancesToArViz::attachObjectToRobotNode
void attachObjectToRobotNode(const memoryx::AttachObjectToRobotNodeInput &attachment)
Definition: ObjectInstancesToArViz.cpp:179
armarx::updateBlackWhitelist
void updateBlackWhitelist(StringBlackWhitelist &bw, const armarx::BlackWhitelistUpdate &update)
Definition: BlackWhitelistUpdate.h:13
memoryx::ObjectInstancesToArViz::floorObjectFilename
std::string floorObjectFilename
Definition: ObjectInstancesToArViz.h:77
memoryx::ObjectInstanceToRobotNodeAttachments::attachObjectToRobotNode
void attachObjectToRobotNode(const memoryx::AttachObjectToRobotNodeInput &attachment)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:33
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::viz::Object::file
Object & file(std::string const &project, std::string const &filename)
Definition: Elements.h:340
memoryx::ObjectInstancesToArViz::updateFloorObject
void updateFloorObject(float height=0)
Draw a the floor as a simox object.
Definition: ObjectInstancesToArViz.cpp:52
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::ObjectClassSegmentWrapper::getClass
std::optional< ObjectClassWrapper > getClass(std::string const &className) const
Definition: ObjectClassSegmentWrapper.cpp:68
memoryx::ObjectInstancesToArViz::ObjectInstancesToArViz
ObjectInstancesToArViz()
Definition: ObjectInstancesToArViz.cpp:13
memoryx::ObjectInstancesToArViz::getFloorObjectFile
std::string getFloorObjectFile()
Definition: ObjectInstancesToArViz.cpp:103
memoryx::ObjectInstancesToArViz::initFromProxies
void initFromProxies(const PriorKnowledgeInterfacePrx &priorKnowledge, const WorkingMemoryInterfacePrx &workingMemory, const armarx::RobotStateComponentInterfacePrx &robotStateComponent, const std::vector< std::string > &datasets)
Set the proxies.
Definition: ObjectInstancesToArViz.cpp:24
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:31
memoryx::ObjectInstanceToRobotNodeAttachments::detachObjectFromRobotNode
void detachObjectFromRobotNode(const memoryx::DetachObjectFromRobotNodeInput &detachment)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:64
IceInternal::Handle< ObjectInstance >
armarx::BlackWhitelist::isExcluded
bool isExcluded(const Key &element) const
An element is excluded if (1) it is in the blacklist, or (2) it is not in the non-empty whitelist.
Definition: BlackWhitelist.h:41
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::viz::Object
Definition: Elements.h:332
armarx::wykobi::Polygon
::wykobi::polygon< float, 2 > Polygon
Definition: wykobi_types.h:30
memoryx::ObjectInstancesToArViz::makeFloorObject
armarx::viz::Object makeFloorObject(const std::string &name="Floor")
Definition: ObjectInstancesToArViz.cpp:96
memoryx::ObjectClassSegmentWrapper::initFromProxy
void initFromProxy(memoryx::PriorKnowledgeInterfacePrx const &priorKnowledge, const std::vector< std::string > &datasets)
Definition: ObjectClassSegmentWrapper.cpp:16
memoryx::ObjectInstancesToArViz::detachObjectFromRobotNode
void detachObjectFromRobotNode(const memoryx::DetachObjectFromRobotNodeInput &detachment)
Definition: ObjectInstancesToArViz.cpp:186
memoryx::ObjectInstanceToRobotNodeAttachments::initFromProxies
void initFromProxies(const WorkingMemoryInterfacePrx &workingMemory, const armarx::RobotStateComponentInterfacePrx &robotStateComponent)
Set the proxies.
Definition: ObjectInstanceToRobotNodeAttachments.cpp:19
filename
std::string filename
Definition: VisualizationRobot.cpp:86
armarx::viz::Polygon
Definition: Elements.h:260
memoryx::EntityWrappers::SimoxObjectWrapperPtr
IceInternal::Handle< SimoxObjectWrapper > SimoxObjectWrapperPtr
Definition: SimoxObjectWrapper.h:300
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
BlackWhitelistUpdate.h
SimoxObjectWrapper.h
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:176
armarx::RemoteRobot::createLocalClone
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
Definition: RemoteRobot.cpp:381
ObjectInstancesToArViz.h
IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface >
memoryx::ObjectInstanceToRobotNodeAttachments::getObjectPoseInFrame
Eigen::Matrix4f getObjectPoseInFrame(ObjectInstancePtr object, const std::string &frame)
Definition: ObjectInstanceToRobotNodeAttachments.cpp:106
memoryx::ObjectInstancesToArViz::layerNameFloor
std::string layerNameFloor
Definition: ObjectInstancesToArViz.h:74
memoryx::ObjectInstancesToArViz::updateObjectClassBlackWhitelist
void updateObjectClassBlackWhitelist(const armarx::BlackWhitelistUpdate &update)
Definition: ObjectInstancesToArViz.cpp:170
memoryx::ObjectInstancesToArViz::layerNameObjects
std::string layerNameObjects
Definition: ObjectInstancesToArViz.h:75
memoryx::ObjectInstanceToRobotNodeAttachments::queryObjects
std::vector< ObjectInstancePtr > queryObjects()
Get all entities from objectInstanceSegment and cast them to ObjectInstance.
Definition: ObjectInstanceToRobotNodeAttachments.cpp:88
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:80
armarx::viz::Client
Definition: Client.h:117
armarx::viz::Layer
Definition: Layer.h:12
memoryx::ObjectInstancesToArViz::updateObjects
void updateObjects()
Query object instances and update their visualization.
Definition: ObjectInstancesToArViz.cpp:60
armarx::viz::Client::commitLayerContaining
void commitLayerContaining(std::string const &name)
Definition: Client.h:165
memoryx::ObjectInstancesToArViz::makeObject
std::optional< armarx::viz::Object > makeObject(const ObjectInstancePtr &object)
Make a armarx::viz::Object from a memoryx::ObjectInstance.
Definition: ObjectInstancesToArViz.cpp:112