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 
23 #include "PrimitiveVisualization.h"
24 
25 #include <pcl/common/colors.h>
26 #include <pcl/point_types.h>
27 
28 #include <VirtualRobot/MathTools.h>
29 
33 
34 
35 using namespace armarx;
36 
37 void
39 {
40  usingTopic("SegmentedPointCloud");
41 
42  usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
43 
44  primitiveColorFrame = viz::Color::blue(150, 200);
45 }
46 
47 void
49 {
50  workingMemory = getProxy<memoryx::WorkingMemoryInterfacePrx>(
51  getProperty<std::string>("WorkingMemoryName").getValue());
52  environmentalPrimitiveSegment = workingMemory->getEnvironmentalPrimitiveSegment();
53 }
54 
55 void
57 {
58 }
59 
60 void
62 {
63 }
64 
65 void
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 
94 PrimitiveVisualization::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 
112 void
113 PrimitiveVisualization::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 
135 void
136 PrimitiveVisualization::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 
208 void
209 PrimitiveVisualization::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 
225 void
226 PrimitiveVisualization::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 
246 {
249 }
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:89
EnvironmentalPrimitive.h
armarx::viz::Layer::clear
void clear()
Definition: Layer.h:24
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
PrimitiveVisualization.h
armarx::Inlier
@ Inlier
Definition: PrimitiveVisualization.h:46
armarx::PrimitiveVisualizationPropertyDefinitions
Definition: PrimitiveVisualization.h:55
armarx::viz::Color::blue
static Color blue(int b=255, int a=255)
Definition: Color.h:100
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:31
armarx::PrimitiveVisualization::onConnectComponent
void onConnectComponent() override
Definition: PrimitiveVisualization.cpp:48
armarx::viz::Sphere
Definition: Elements.h:133
armarx::Rectangle
@ Rectangle
Definition: PrimitiveVisualization.h:48
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::PrimitiveVisualization::workingMemory
memoryx::WorkingMemoryInterfacePrx workingMemory
Definition: PrimitiveVisualization.h:134
armarx::toEigen
Eigen::Vector3f toEigen(const pcl::PointXYZ &pt)
Definition: PointToModelICP.h:67
armarx::PrimitiveVisualization::onInitComponent
void onInitComponent() override
Definition: PrimitiveVisualization.cpp:38
armarx::viz::Cylinder
Definition: Elements.h:71
armarx::viz::Color
Definition: Color.h:12
armarx::viz::PointCloud
Definition: PointCloud.h:19
MemoryXCoreObjectFactories.h
armarx::viz::Box
Definition: Elements.h:47
armarx::viz::Polygon
Definition: Elements.h:260
armarx::Obb
@ Obb
Definition: PrimitiveVisualization.h:47
armarx::control::common::getValue
T getValue(nlohmann::json &userConfig, nlohmann::json &defaultConfig, const std::string &entryName)
Definition: utils.h:71
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:254
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:79
armarx::PrimitiveVisualization::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: PrimitiveVisualization.cpp:245
armarx::PrimitiveVisualization::reportNewPointCloudSegmentation
void reportNewPointCloudSegmentation(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveVisualization.cpp:66
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:42
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::view_selection::skills::direction::state::center
state::Type center(state::Type previous)
Definition: LookDirection.cpp:233
MemoryXTypesObjectFactories.h
armarx::PrimitiveVisualization::onDisconnectComponent
void onDisconnectComponent() override
Definition: PrimitiveVisualization.cpp:56
armarx::Logging::deactivateSpam
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
armarx::Hull
@ Hull
Definition: PrimitiveVisualization.h:45
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
armarx::PrimitiveVisualization::environmentalPrimitiveSegment
memoryx::EnvironmentalPrimitiveSegmentBasePrx environmentalPrimitiveSegment
Definition: PrimitiveVisualization.h:135
armarx::PrimitiveVisualization::onExitComponent
void onExitComponent() override
Definition: PrimitiveVisualization.cpp:61
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:154
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27