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