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
25#include <pcl/common/colors.h>
26#include <pcl/point_types.h>
27
28#include <VirtualRobot/MathTools.h>
29
33
34
35using namespace armarx;
36
37void
39{
40 usingTopic("SegmentedPointCloud");
41
42 usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
43
44 primitiveColorFrame = viz::Color::blue(150, 200);
45}
46
47void
54
55void
59
60void
64
65void
67{
68 std::unique_lock<std::mutex> lock(mutex, std::try_to_lock);
69
70 if (!lock.owns_lock())
71 {
73 << "unable to process new geometric primitives. already processing previous "
74 "primitive set.";
75 return;
76 }
77
78
79 ;
80 layer.clear();
81
82 std::vector<memoryx::EnvironmentalPrimitiveBasePtr> primitives =
83 environmentalPrimitiveSegment->getEnvironmentalPrimitives();
84 for (memoryx::EnvironmentalPrimitiveBasePtr& primitive : primitives)
85 {
86 viz::Color color = getPrimitiveColor(primitive);
87 visualizePrimitive(primitive, color);
88 }
89
90 arviz.commit({layer});
91}
92
94PrimitiveVisualization::getPrimitiveColor(const memoryx::EnvironmentalPrimitiveBasePtr& primitive)
95{
96 if (primitive->getLabel())
97 {
98 viz::Color color;
99 pcl::RGB c = pcl::GlasbeyLUT::at(primitive->getLabel() % pcl::GlasbeyLUT::size());
100 color.r = c.r;
101 color.g = c.g;
102 color.b = c.b;
103 color.a = 200; //primitive->getProbability()
104 return color;
105 }
106 else
107 {
108 return viz::Color::blue(128, 128);
109 }
110}
111
112void
113PrimitiveVisualization::visualizePrimitive(const memoryx::EnvironmentalPrimitiveBasePtr& primitive,
114 viz::Color color)
115{
116 if (primitive->ice_isA(memoryx::PlanePrimitiveBase::ice_staticId()))
117 {
118 visualizePlane(memoryx::PlanePrimitiveBasePtr::dynamicCast(primitive), color);
119 }
120 else if (primitive->ice_isA(memoryx::SpherePrimitiveBase::ice_staticId()))
121 {
122 visualizeSphere(memoryx::SpherePrimitiveBasePtr::dynamicCast(primitive), color);
123 }
124 else if (primitive->ice_isA(memoryx::CylinderPrimitiveBase::ice_staticId()))
125 {
126 visualizeCylinder(memoryx::CylinderPrimitiveBasePtr::dynamicCast(primitive), color);
127 }
128 else
129 {
130 ARMARX_WARNING << "Cannot visualize unknown primitive type: " << primitive->getName();
131 return;
132 }
133}
134
135void
136PrimitiveVisualization::visualizePlane(const memoryx::PlanePrimitiveBasePtr& plane,
137 viz::Color color)
138{
139 memoryx::PlanePrimitivePtr p = memoryx::PlanePrimitivePtr::dynamicCast(plane);
140
141
142 Eigen::Matrix4f pose = FramedPosePtr::dynamicCast(p->getPose())->toEigen();
143 Eigen::Vector3f dimensions = Vector3Ptr::dynamicCast(p->getOBBDimensions())->toEigen();
144
145
146 if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Obb)
147 {
148 // Old behavior: Visualize planes as boxes, while the depth of the box indicates the deviation of the
149 // point cloud from the plane model
150
151 viz::Box box(p->getId());
152 box.size(dimensions);
153 box.color(color);
154 box.pose(pose);
155
156 layer.add(box);
157 }
158 else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() ==
159 Rectangle)
160 {
161 viz::Polygon rectangle(p->getId());
162
163 rectangle.color(color);
164 rectangle.lineColor(primitiveColorFrame);
165
166
167 rectangle.addPoint(Eigen::Vector3f(
168 (pose * Eigen::Vector4f(dimensions.x() / 2, dimensions.y() / 2, 0, 1)).head(3)));
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
176
177 layer.add(rectangle);
178 }
179 else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Hull)
180 {
181
182 viz::Polygon polygon(p->getId());
183 polygon.color(color);
184 polygon.lineColor(primitiveColorFrame);
185
186 for (const Vector3BasePtr& v : p->getGraspPoints())
187 {
188 polygon.addPoint(Vector3Ptr::dynamicCast(v)->toEigen());
189 }
190
191 layer.add(polygon);
192 }
193 else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() ==
194 Inlier)
195 {
196 viz::PointCloud cloud(p->getId());
197
198 for (const armarx::Vector3BasePtr& v : p->getInliers())
199 {
200 // ..todo:: add color
201 cloud.addPoint(v->x, v->y, v->z);
202 }
203
204 layer.add(cloud);
205 }
206}
207
208void
209PrimitiveVisualization::visualizeSphere(const memoryx::SpherePrimitiveBasePtr& sphere,
210 viz::Color color)
211{
212 memoryx::SpherePrimitivePtr p = memoryx::SpherePrimitivePtr::dynamicCast(sphere);
213
214 Eigen::Vector3f center = Vector3Ptr::dynamicCast(p->getSphereCenter())->toEigen();
215 float radius = p->getSphereRadius();
216
217 viz::Sphere s(p->getId());
218 s.position(center);
219 s.radius(radius);
220 s.color(color);
221
222 layer.add(s);
223}
224
225void
226PrimitiveVisualization::visualizeCylinder(const memoryx::CylinderPrimitiveBasePtr& cylinder,
227 viz::Color color)
228{
229 memoryx::CylinderPrimitivePtr p = memoryx::CylinderPrimitivePtr::dynamicCast(cylinder);
230 Eigen::Vector3f center = Vector3Ptr::dynamicCast(p->getCylinderPoint())->toEigen();
231 Eigen::Vector3f direction = Vector3Ptr::dynamicCast(p->getCylinderAxisDirection())->toEigen();
232
233 float radius = p->getCylinderRadius();
234 float length = p->getLength();
235
236 viz::Cylinder c(p->getId());
237 c.position(center);
238 c.height(length);
239 c.direction(direction);
240 c.radius(radius);
241 layer.add(c);
242}
243
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)
void reportNewPointCloudSegmentation(const Ice::Current &c=Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
memoryx::WorkingMemoryInterfacePrx workingMemory
memoryx::EnvironmentalPrimitiveSegmentBasePrx environmentalPrimitiveSegment
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