FitKnownRectangleRotationMaxPoints.cpp
Go to the documentation of this file.
2 
3 #include <SimoxUtility/math/pose/invert.h>
4 #include <SimoxUtility/math/pose/pose.h>
5 
6 
7 namespace visionx
8 {
9 
11  {
12  }
13 
14 
16  {
17  return rectangleSize;
18  }
20  {
22  }
23 
24 
26  {
27  return numSteps;
28  }
30  {
31  numSteps = value;
32  }
33 
34 
36  const pcl::PointCloud<PointT>& cloud, Eigen::Vector3f normal, Eigen::Vector3f center)
37  {
38  initAlignedPointCloud(cloud, normal, center);
39  }
40 
41 
43  {
44  inlierCounts = std::vector<size_t>(numSteps, 0);
45  for (size_t i = 0; i < numSteps; ++i)
46  {
48  }
49 
50  auto it = std::max_element(inlierCounts.begin(), inlierCounts.end());
51 
52  this->bestStep = size_t(std::distance(inlierCounts.begin(), it));
54  return angleToPose(bestAngle);
55  }
56 
57 
59  const pcl::PointCloud<FitKnownRectangleRotationMaxPoints::PointT>& cloud,
60  Eigen::Vector3f normal, Eigen::Vector3f center)
61  {
62  setInputCloud(cloud, normal, center);
63  return fit();
64  }
65 
66 
67  void FitKnownRectangleRotationMaxPoints::initAlignedPointCloud(
68  const pcl::PointCloud<FitKnownRectangleRotationMaxPoints::PointT>& cloud,
69  Eigen::Vector3f normal, Eigen::Vector3f center)
70  {
71  using namespace Eigen;
72 
73  // Move to center.
74  Eigen::Matrix4f transform = simox::math::pose(-center);
75  // Rotate so normal = z.
76  transform = simox::math::pose(Quaternionf::FromTwoVectors(normal, Vector3f::UnitZ()))
77  * transform;
78 
80  pcl::transformPointCloud(cloud, *aligned, transform);
81  }
82 
83  auto FitKnownRectangleRotationMaxPoints::getAlignedPointCloud() const -> pcl::PointCloud<PointT>::ConstPtr
84  {
85  return aligned;
86  }
87 
88 
89  void FitKnownRectangleRotationMaxPoints::setRotated(float angle) const
90  {
91  const Eigen::Matrix3f rot = Eigen::AngleAxisf(- angle, Eigen::Vector3f::UnitZ()).toRotationMatrix();
92  pcl::transformPointCloud(*aligned, *rotated, simox::math::pose(rot));
93  }
94 
95 
96  bool FitKnownRectangleRotationMaxPoints::isInlier(const PointT& p) const
97  {
98  return std::abs(p.x) <= 0.5f * rectangleSize.x()
99  && std::abs(p.y) <= 0.5f * rectangleSize.y();
100  }
101 
102 
104  {
105  setRotated(angle);
106  // Count inliers.
107  long count = std::count_if(rotated->begin(), rotated->end(),
108  [this](const PointT & p)
109  {
110  return isInlier(p);
111  });
112  return size_t(count);
113  }
114 
116  {
117  setRotated(angle);
118 
119  // Find inliers.
120  pcl::PointIndicesPtr indices(new pcl::PointIndices);
121  for (size_t i = 0; i < rotated->size(); ++i)
122  {
123  if (isInlier(rotated->points[i]))
124  {
125  indices->indices.push_back(int(i));
126  }
127  }
128 
129  return indices;
130  }
131 
132 
134  {
135  Eigen::AngleAxisf localRotation(angle, Eigen::Vector3f::UnitZ());
136  return simox::math::inverted_pose(alignedTransform) * simox::math::pose(localRotation);
137  }
138 
139 
141  {
142  return inlierCounts;
143  }
145  {
146  return bestAngle;
147  }
148 
149 
150 }
visionx::FitKnownRectangleRotationMaxPoints::setRectangleSize
void setRectangleSize(const Eigen::Vector2f &value)
Set the rectangle size.
Definition: FitKnownRectangleRotationMaxPoints.cpp:19
FitKnownRectangleRotationMaxPoints.h
pcl
Definition: pcl_point_operators.cpp:4
visionx::FitKnownRectangleRotationMaxPoints::alignedTransform
Eigen::Matrix4f alignedTransform
Transformation applied to get aligned.
Definition: FitKnownRectangleRotationMaxPoints.h:140
Eigen
Definition: Elements.h:36
visionx::FitKnownRectangleRotationMaxPoints::getRectangleSize
Eigen::Vector2f getRectangleSize() const
Definition: FitKnownRectangleRotationMaxPoints.cpp:15
visionx::FitKnownRectangleRotationMaxPoints::aligned
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr aligned
Transformed so center is at 0 and z = normal.
Definition: FitKnownRectangleRotationMaxPoints.h:138
visionx::FitKnownRectangleRotationMaxPoints::fit
Eigen::Matrix4f fit()
Count the number of inliers per rotation step, and return the global pose corresponding to the rotati...
Definition: FitKnownRectangleRotationMaxPoints.cpp:42
visionx::FitKnownRectangleRotationMaxPoints::numSteps
size_t numSteps
The number of steps (i.e. resolution of solution space).
Definition: FitKnownRectangleRotationMaxPoints.h:131
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::FitKnownRectangleRotationMaxPoints::rectangleSize
Eigen::Vector2f rectangleSize
The rectangle size.
Definition: FitKnownRectangleRotationMaxPoints.h:128
visionx::FitKnownRectangleRotationMaxPoints::angleToPose
Eigen::Matrix4f angleToPose(float angle)
Get the global pose corresponding to a local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:133
visionx::FitKnownRectangleRotationMaxPoints::getInlierCounts
std::vector< size_t > getInlierCounts() const
Get the inlier counts per step.
Definition: FitKnownRectangleRotationMaxPoints.cpp:140
visionx::FitKnownRectangleRotationMaxPoints::bestStep
size_t bestStep
Definition: FitKnownRectangleRotationMaxPoints.h:148
visionx::FitKnownRectangleRotationMaxPoints::countInliersOfAngle
size_t countInliersOfAngle(float angle) const
Count the inliers for a given local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:103
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
visionx::FitKnownRectangleRotationMaxPoints::inlierCounts
std::vector< size_t > inlierCounts
The inlier counts per step.
Definition: FitKnownRectangleRotationMaxPoints.h:152
pcl::graph::indices
pcl::PointIndices::Ptr indices(const PCG &g)
Retrieve the indices of the points of the point cloud stored in a point cloud graph that actually bel...
Definition: point_cloud_graph.h:737
visionx::FitKnownRectangleRotationMaxPoints::getNumSteps
size_t getNumSteps() const
Definition: FitKnownRectangleRotationMaxPoints.cpp:25
visionx::FitKnownRectangleRotationMaxPoints::setInputCloud
void setInputCloud(const pcl::PointCloud< PointT > &cloud, Eigen::Vector3f normal, Eigen::Vector3f center)
Set the input cloud and the rectangle's position and normal.
Definition: FitKnownRectangleRotationMaxPoints.cpp:35
visionx::FitKnownRectangleRotationMaxPoints::setNumSteps
void setNumSteps(const size_t &value)
Set the number of solution steps, i.e. the resolution of the solution space.
Definition: FitKnownRectangleRotationMaxPoints.cpp:29
PointCloud
Definition: PointCloud.h:69
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
visionx::FitKnownRectangleRotationMaxPoints::stepToAngle
float stepToAngle(Int step) const
Get the local rotation angle of a solution step.
Definition: FitKnownRectangleRotationMaxPoints.h:103
visionx::FitKnownRectangleRotationMaxPoints::FitKnownRectangleRotationMaxPoints
FitKnownRectangleRotationMaxPoints()
Definition: FitKnownRectangleRotationMaxPoints.cpp:10
visionx::FitKnownRectangleRotationMaxPoints::bestAngle
float bestAngle
The best angle.
Definition: FitKnownRectangleRotationMaxPoints.h:150
visionx::FitKnownRectangleRotationMaxPoints::PointT
pcl::PointXYZRGBA PointT
Definition: FitKnownRectangleRotationMaxPoints.h:26
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:100
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
visionx::FitKnownRectangleRotationMaxPoints::getBestAngle
float getBestAngle() const
Get the best local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:144
visionx::FitKnownRectangleRotationMaxPoints::rotated
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr rotated
Buffer for point clouds rotated for inlier counting.
Definition: FitKnownRectangleRotationMaxPoints.h:143
visionx::FitKnownRectangleRotationMaxPoints::findInliersOfAngle
pcl::PointIndicesPtr findInliersOfAngle(float angle) const
Get the inliers for a given local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:115