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 
29 
30 #include <VirtualRobot/MathTools.h>
31 
32 #include <pcl/point_types.h>
33 #include <pcl/common/colors.h>
34 
35 
36 using namespace armarx;
37 
38 
40 {
41  usingTopic("SegmentedPointCloud");
42 
43  usingProxy(getProperty<std::string>("WorkingMemoryName").getValue());
44 
45  primitiveColorFrame = viz::Color::blue(150, 200);
46 }
47 
48 
50 {
51  workingMemory = getProxy<memoryx::WorkingMemoryInterfacePrx>(getProperty<std::string>("WorkingMemoryName").getValue());
52  environmentalPrimitiveSegment = workingMemory->getEnvironmentalPrimitiveSegment();
53 }
54 
55 
57 {
58 
59 }
60 
61 
63 {
64 
65 }
66 
67 
68 
70 {
71  std::unique_lock<std::mutex> lock(mutex, std::try_to_lock);
72 
73  if (!lock.owns_lock())
74  {
75  ARMARX_INFO << deactivateSpam(3) << "unable to process new geometric primitives. already processing previous primitive set.";
76  return;
77  }
78 
79 
80 ; layer.clear();
81 
82  std::vector<memoryx::EnvironmentalPrimitiveBasePtr> primitives = environmentalPrimitiveSegment->getEnvironmentalPrimitives();
83  for (memoryx::EnvironmentalPrimitiveBasePtr& primitive : primitives)
84  {
85  viz::Color color = getPrimitiveColor(primitive);
86  visualizePrimitive(primitive, color);
87  }
88 
89  arviz.commit({layer});
90 }
91 
92 
93 viz::Color PrimitiveVisualization::getPrimitiveColor(const memoryx::EnvironmentalPrimitiveBasePtr& primitive)
94 {
95  if (primitive->getLabel())
96  {
97  viz::Color color;
98  pcl::RGB c = pcl::GlasbeyLUT::at(primitive->getLabel() % pcl::GlasbeyLUT::size());
99  color.r = c.r;
100  color.g = c.g;
101  color.b = c.b;
102  color.a = 200; //primitive->getProbability()
103  return color;
104  }
105  else
106  {
107  return viz::Color::blue(128, 128);
108  }
109 
110 }
111 
112 
113 
114 void PrimitiveVisualization::visualizePrimitive(const memoryx::EnvironmentalPrimitiveBasePtr& primitive, 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 PrimitiveVisualization::visualizePlane(const memoryx::PlanePrimitiveBasePtr& plane, viz::Color color)
136 {
137  memoryx::PlanePrimitivePtr p = memoryx::PlanePrimitivePtr::dynamicCast(plane);
138 
139 
140  Eigen::Matrix4f pose = FramedPosePtr::dynamicCast(p->getPose())->toEigen();
141  Eigen::Vector3f dimensions = Vector3Ptr::dynamicCast(p->getOBBDimensions())->toEigen();
142 
143 
144  if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Obb)
145  {
146  // Old behavior: Visualize planes as boxes, while the depth of the box indicates the deviation of the
147  // point cloud from the plane model
148 
149  viz::Box box(p->getId());
150  box.size(dimensions);
151  box.color(color);
152  box.pose(pose);
153 
154  layer.add(box);
155 
156  }
157  else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Rectangle)
158  {
159  viz::Polygon rectangle(p->getId());
160 
161  rectangle.color(color);
162  rectangle.lineColor(primitiveColorFrame);
163 
164 
165  rectangle.addPoint(Eigen::Vector3f((pose * Eigen::Vector4f(dimensions.x() / 2, dimensions.y() / 2, 0, 1)).head(3)));
166  rectangle.addPoint(Eigen::Vector3f((pose * Eigen::Vector4f(dimensions.x() / 2, -dimensions.y() / 2, 0, 1)).head(3)));
167  rectangle.addPoint(Eigen::Vector3f((pose * Eigen::Vector4f(-dimensions.x() / 2, -dimensions.y() / 2, 0, 1)).head(3)));
168  rectangle.addPoint(Eigen::Vector3f((pose * Eigen::Vector4f(-dimensions.x() / 2, dimensions.y() / 2, 0, 1)).head(3)));
169 
170 
171  layer.add(rectangle);
172 
173  }
174  else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Hull)
175  {
176 
177  viz::Polygon polygon(p->getId());
178  polygon.color(color);
179  polygon.lineColor(primitiveColorFrame);
180 
181  for (const Vector3BasePtr& v : p->getGraspPoints())
182  {
183  polygon.addPoint(Vector3Ptr::dynamicCast(v)->toEigen());
184  }
185 
186  layer.add(polygon);
187  }
188  else if (getProperty<PLANE_VISUALIZATION_METHOD>("PlaneVisualizationMethod").getValue() == Inlier)
189  {
190  viz::PointCloud cloud(p->getId());
191 
192  for (const armarx::Vector3BasePtr& v : p->getInliers())
193  {
194  // ..todo:: add color
195  cloud.addPoint(v->x, v->y, v->z);
196  }
197 
198  layer.add(cloud);
199  }
200 
201 }
202 
203 void PrimitiveVisualization::visualizeSphere(const memoryx::SpherePrimitiveBasePtr& sphere, viz::Color color)
204 {
205  memoryx::SpherePrimitivePtr p = memoryx::SpherePrimitivePtr::dynamicCast(sphere);
206 
207  Eigen::Vector3f center = Vector3Ptr::dynamicCast(p->getSphereCenter())->toEigen();
208  float radius = p->getSphereRadius();
209 
210  viz::Sphere s(p->getId());
211  s.position(center);
212  s.radius(radius);
213  s.color(color);
214 
215  layer.add(s);
216 }
217 
218 void PrimitiveVisualization::visualizeCylinder(const memoryx::CylinderPrimitiveBasePtr& cylinder, viz::Color color)
219 {
220  memoryx::CylinderPrimitivePtr p = memoryx::CylinderPrimitivePtr::dynamicCast(cylinder);
221  Eigen::Vector3f center = Vector3Ptr::dynamicCast(p->getCylinderPoint())->toEigen();
222  Eigen::Vector3f direction = Vector3Ptr::dynamicCast(p->getCylinderAxisDirection())->toEigen();
223 
224  float radius = p->getCylinderRadius();
225  float length = p->getLength();
226 
227  viz::Cylinder c(p->getId());
228  c.position(center);
229  c.height(length);
230  c.direction(direction);
231  c.radius(radius);
232  layer.add(c);
233 }
234 
235 
236 
238 {
241 }
242 
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
EnvironmentalPrimitive.h
armarx::viz::toEigen
Eigen::Matrix4f toEigen(data::GlobalPose const &pose)
Definition: Interaction.h:46
armarx::viz::Layer::clear
void clear()
Definition: Layer.h:23
PrimitiveVisualization.h
armarx::Inlier
@ Inlier
Definition: PrimitiveVisualization.h:45
armarx::PrimitiveVisualizationPropertyDefinitions
Definition: PrimitiveVisualization.h:53
armarx::viz::Color::blue
static Color blue(int b=255, int a=255)
Definition: Color.h:89
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::PrimitiveVisualization::onConnectComponent
void onConnectComponent() override
Definition: PrimitiveVisualization.cpp:49
armarx::viz::Sphere
Definition: Elements.h:134
armarx::Rectangle
@ Rectangle
Definition: PrimitiveVisualization.h:45
IceInternal::Handle
Definition: forward_declarations.h:8
armarx::PrimitiveVisualization::workingMemory
memoryx::WorkingMemoryInterfacePrx workingMemory
Definition: PrimitiveVisualization.h:131
armarx::PrimitiveVisualization::onInitComponent
void onInitComponent() override
Definition: PrimitiveVisualization.cpp:39
armarx::viz::Cylinder
Definition: Elements.h:74
armarx::viz::Color
Definition: Color.h:13
armarx::viz::PointCloud
Definition: PointCloud.h:21
MemoryXCoreObjectFactories.h
armarx::viz::Box
Definition: Elements.h:51
armarx::viz::Polygon
Definition: Elements.h:258
armarx::Obb
@ Obb
Definition: PrimitiveVisualization.h:45
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:248
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::PrimitiveVisualization::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: PrimitiveVisualization.cpp:237
armarx::PrimitiveVisualization::reportNewPointCloudSegmentation
void reportNewPointCloudSegmentation(const Ice::Current &c=Ice::emptyCurrent) override
Definition: PrimitiveVisualization.cpp:69
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:43
IceUtil::Handle< class PropertyDefinitionContainer >
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:92
armarx::Hull
@ Hull
Definition: PrimitiveVisualization.h:45
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
armarx::PrimitiveVisualization::environmentalPrimitiveSegment
memoryx::EnvironmentalPrimitiveSegmentBasePrx environmentalPrimitiveSegment
Definition: PrimitiveVisualization.h:132
armarx::PrimitiveVisualization::onExitComponent
void onExitComponent() override
Definition: PrimitiveVisualization.cpp:62
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:151
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:28