PrimitiveVisualization.cpp
Go to the documentation of this file.
1/*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package VisionX::ArmarXObjects::PrimitiveVisualization
17 * @author Markus Grotz ( markus dot grotz at kit dot edu )
18 * @date 2016
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22
24
26
27#include <pcl/common/colors.h>
28#include <pcl/point_types.h>
29
30#include <VirtualRobot/MathTools.h>
31
35
36
37using namespace armarx;
38
39void
41{
42 usingTopic("SegmentedPointCloud");
43
44 usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
45
46 primitiveColorFrame = viz::Color::blue(150, 200);
47}
48
49void
56
57void
61
62void
66
67void
69{
70 std::unique_lock<std::mutex> lock(mutex, std::try_to_lock);
71
72 if (!lock.owns_lock())
73 {
75 << "unable to process new geometric primitives. already processing previous "
76 "primitive set.";
77 return;
78 }
79
80
81 ;
82 layer.clear();
83
84 std::vector<memoryx::EnvironmentalPrimitiveBasePtr> primitives =
85 environmentalPrimitiveSegment->getEnvironmentalPrimitives();
86 for (memoryx::EnvironmentalPrimitiveBasePtr& primitive : primitives)
87 {
88 viz::Color color = getPrimitiveColor(primitive);
89 visualizePrimitive(primitive, color);
90 }
91
92 arviz.commit({layer});
93}
94
96PrimitiveVisualization::getPrimitiveColor(const memoryx::EnvironmentalPrimitiveBasePtr& primitive)
97{
98 if (primitive->getLabel())
99 {
100 viz::Color color;
101 pcl::RGB c = pcl::GlasbeyLUT::at(primitive->getLabel() % pcl::GlasbeyLUT::size());
102 color.r = c.r;
103 color.g = c.g;
104 color.b = c.b;
105 color.a = 200; //primitive->getProbability()
106 return color;
107 }
108 else
109 {
110 return viz::Color::blue(128, 128);
111 }
112}
113
114void
115PrimitiveVisualization::visualizePrimitive(const memoryx::EnvironmentalPrimitiveBasePtr& primitive,
116 viz::Color color)
117{
118 if (primitive->ice_isA(memoryx::PlanePrimitiveBase::ice_staticId()))
119 {
120 visualizePlane(memoryx::PlanePrimitiveBasePtr::dynamicCast(primitive), color);
121 }
122 else if (primitive->ice_isA(memoryx::SpherePrimitiveBase::ice_staticId()))
123 {
124 visualizeSphere(memoryx::SpherePrimitiveBasePtr::dynamicCast(primitive), color);
125 }
126 else if (primitive->ice_isA(memoryx::CylinderPrimitiveBase::ice_staticId()))
127 {
128 visualizeCylinder(memoryx::CylinderPrimitiveBasePtr::dynamicCast(primitive), color);
129 }
130 else
131 {
132 ARMARX_WARNING << "Cannot visualize unknown primitive type: " << primitive->getName();
133 return;
134 }
135}
136
137void
138PrimitiveVisualization::visualizePlane(const memoryx::PlanePrimitiveBasePtr& plane,
139 viz::Color color)
140{
141 memoryx::PlanePrimitivePtr p = memoryx::PlanePrimitivePtr::dynamicCast(plane);
142
143
144 Eigen::Matrix4f pose = FramedPosePtr::dynamicCast(p->getPose())->toEigen();
145 Eigen::Vector3f dimensions = Vector3Ptr::dynamicCast(p->getOBBDimensions())->toEigen();
146
147
148 if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Obb)
149 {
150 // Old behavior: Visualize planes as boxes, while the depth of the box indicates the deviation of the
151 // point cloud from the plane model
152
153 viz::Box box(p->getId());
154 box.size(dimensions);
155 box.color(color);
156 box.pose(pose);
157
158 layer.add(box);
159 }
160 else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() ==
161 Rectangle)
162 {
163 viz::Polygon rectangle(p->getId());
164
165 rectangle.color(color);
166 rectangle.lineColor(primitiveColorFrame);
167
168
169 rectangle.addPoint(Eigen::Vector3f(
170 (pose * Eigen::Vector4f(dimensions.x() / 2, dimensions.y() / 2, 0, 1)).head(3)));
171 rectangle.addPoint(Eigen::Vector3f(
172 (pose * Eigen::Vector4f(dimensions.x() / 2, -dimensions.y() / 2, 0, 1)).head(3)));
173 rectangle.addPoint(Eigen::Vector3f(
174 (pose * Eigen::Vector4f(-dimensions.x() / 2, -dimensions.y() / 2, 0, 1)).head(3)));
175 rectangle.addPoint(Eigen::Vector3f(
176 (pose * Eigen::Vector4f(-dimensions.x() / 2, dimensions.y() / 2, 0, 1)).head(3)));
177
178
179 layer.add(rectangle);
180 }
181 else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Hull)
182 {
183
184 viz::Polygon polygon(p->getId());
185 polygon.color(color);
186 polygon.lineColor(primitiveColorFrame);
187
188 for (const Vector3BasePtr& v : p->getGraspPoints())
189 {
190 polygon.addPoint(Vector3Ptr::dynamicCast(v)->toEigen());
191 }
192
193 layer.add(polygon);
194 }
195 else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() ==
196 Inlier)
197 {
198 viz::PointCloud cloud(p->getId());
199
200 for (const armarx::Vector3BasePtr& v : p->getInliers())
201 {
202 // ..todo:: add color
203 cloud.addPoint(v->x, v->y, v->z);
204 }
205
206 layer.add(cloud);
207 }
208}
209
210void
211PrimitiveVisualization::visualizeSphere(const memoryx::SpherePrimitiveBasePtr& sphere,
212 viz::Color color)
213{
214 memoryx::SpherePrimitivePtr p = memoryx::SpherePrimitivePtr::dynamicCast(sphere);
215
216 Eigen::Vector3f center = Vector3Ptr::dynamicCast(p->getSphereCenter())->toEigen();
217 float radius = p->getSphereRadius();
218
219 viz::Sphere s(p->getId());
220 s.position(center);
221 s.radius(radius);
222 s.color(color);
223
224 layer.add(s);
225}
226
227void
228PrimitiveVisualization::visualizeCylinder(const memoryx::CylinderPrimitiveBasePtr& cylinder,
229 viz::Color color)
230{
231 memoryx::CylinderPrimitivePtr p = memoryx::CylinderPrimitivePtr::dynamicCast(cylinder);
232 Eigen::Vector3f center = Vector3Ptr::dynamicCast(p->getCylinderPoint())->toEigen();
233 Eigen::Vector3f direction = Vector3Ptr::dynamicCast(p->getCylinderAxisDirection())->toEigen();
234
235 float radius = p->getCylinderRadius();
236 float length = p->getLength();
237
238 viz::Cylinder c(p->getId());
239 c.position(center);
240 c.height(length);
241 c.direction(direction);
242 c.radius(radius);
243 layer.add(c);
244}
245
252
253std::string
255{
256 return "PrimitiveVisualization";
257}
258
259std::string
264
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
constexpr T c
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
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
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Brief description of class PrimitiveVisualization.
void reportNewPointCloudSegmentation(const Ice::Current &c=Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
memoryx::WorkingMemoryInterfacePrx workingMemory
memoryx::EnvironmentalPrimitiveSegmentBasePrx environmentalPrimitiveSegment
std::string getDefaultName() const override
Retrieve default name of component.
Property< PropertyType > getProperty(const std::string &name)
Property creation and retrieval.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition utils.h:80
double s(double t, double s0, double v0, double a0, double j)
Definition CtrlUtil.h:33
double v(double t, double v0, double a0, double j)
Definition CtrlUtil.h:39
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)
IceInternal::Handle< SpherePrimitive > SpherePrimitivePtr
IceInternal::Handle< PlanePrimitive > PlanePrimitivePtr
IceInternal::Handle< CylinderPrimitive > CylinderPrimitivePtr