ObjectInstancesToArViz.cpp
Go to the documentation of this file.
2
3#include <VirtualRobot/RuntimeEnvironment.h>
4
7
9
10namespace memoryx
11{
12
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 {
55 arviz.commitLayerContaining(layerNameFloor,
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 {
75 armarx::viz::Layer layer = arviz.layer(layerNameObjects);
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
97 {
100 }
101
102 std::string
104 {
106 std::string filename = this->floorObjectFilename;
107 VirtualRobot::RuntimeEnvironment::getDataFileAbsolute(filename);
108 return filename;
109 }
110
111 std::optional<armarx::viz::Object>
113 {
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 {
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 {
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 {
182 attachments.attachObjectToRobotNode(attachment);
183 }
184
185 void
187 const DetachObjectFromRobotNodeInput& detachment)
188 {
190 attachments.detachObjectFromRobotNode(detachment);
191 }
192
193} // namespace memoryx
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition Logging.cpp:75
VirtualRobot::RobotPtr createLocalClone()
Clones the structure of this remote robot to a local instance.
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition ElementOps.h:176
DerivedT & color(Color color)
Definition ElementOps.h:218
std::optional< ObjectClassWrapper > getClass(std::string const &className) const
std::optional< armarx::viz::Object > makeObject(const ObjectInstancePtr &object)
Make a armarx::viz::Object from a memoryx::ObjectInstance.
void initFromProxies(const PriorKnowledgeInterfacePrx &priorKnowledge, const WorkingMemoryInterfacePrx &workingMemory, const armarx::RobotStateComponentInterfacePrx &robotStateComponent, const std::vector< std::string > &datasets)
Set the proxies.
void updateObjectClassBlackWhitelist(const armarx::BlackWhitelistUpdate &update)
void attachObjectToRobotNode(const memoryx::AttachObjectToRobotNodeInput &attachment)
void detachObjectFromRobotNode(const memoryx::DetachObjectFromRobotNodeInput &detachment)
armarx::viz::Polygon makeFloorPolygon(const Eigen::Vector2f &extents={10000, 10000}, float height=0)
Make a polygon representing the floor.
void setArViz(armarx::viz::Client arviz)
Set the ArViz client.
armarx::viz::Object makeFloorObject(const std::string &name="Floor")
void updateFloorObject(float height=0)
Draw a the floor as a simox object.
void updateFloorPolygon(const Eigen::Vector2f &extents={10000, 10000}, float height=0)
Draw a the floor as a polygon.
void updateObjects()
Query object instances and update their visualization.
#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...
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
::IceInternal::ProxyHandle<::IceProxy::armarx::RobotStateComponentInterface > RobotStateComponentInterfacePrx
void updateBlackWhitelist(StringBlackWhitelist &bw, const armarx::BlackWhitelistUpdate &update)
IceInternal::Handle< SimoxObjectWrapper > SimoxObjectWrapperPtr
VirtualRobot headers.
IceInternal::Handle< ObjectInstance > ObjectInstancePtr
void add(ElementT const &element)
Definition Layer.h:31
Object & file(std::string const &project, std::string const &filename)
Definition Elements.h:340
Polygon & plane(Eigen::Hyperplane3f plane, Eigen::Vector3f at, Eigen::Vector2f size)
Add points representing a plane as rectangle.
Definition Elements.cpp:149
#define ARMARX_TRACE
Definition trace.h:77