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 namespace visionx
7 {
8 
10  {
11  }
12 
13  Eigen::Vector2f
15  {
16  return rectangleSize;
17  }
18 
19  void
21  {
23  }
24 
25  size_t
27  {
28  return numSteps;
29  }
30 
31  void
33  {
34  numSteps = value;
35  }
36 
37  void
38  FitKnownRectangleRotationMaxPoints::setInputCloud(const pcl::PointCloud<PointT>& cloud,
39  Eigen::Vector3f normal,
40  Eigen::Vector3f center)
41  {
42  initAlignedPointCloud(cloud, normal, center);
43  }
44 
47  {
48  inlierCounts = std::vector<size_t>(numSteps, 0);
49  for (size_t i = 0; i < numSteps; ++i)
50  {
52  }
53 
54  auto it = std::max_element(inlierCounts.begin(), inlierCounts.end());
55 
56  this->bestStep = size_t(std::distance(inlierCounts.begin(), it));
58  return angleToPose(bestAngle);
59  }
60 
63  const pcl::PointCloud<FitKnownRectangleRotationMaxPoints::PointT>& cloud,
64  Eigen::Vector3f normal,
65  Eigen::Vector3f center)
66  {
67  setInputCloud(cloud, normal, center);
68  return fit();
69  }
70 
71  void
72  FitKnownRectangleRotationMaxPoints::initAlignedPointCloud(
73  const pcl::PointCloud<FitKnownRectangleRotationMaxPoints::PointT>& cloud,
74  Eigen::Vector3f normal,
75  Eigen::Vector3f center)
76  {
77  using namespace Eigen;
78 
79  // Move to center.
80  Eigen::Matrix4f transform = simox::math::pose(-center);
81  // Rotate so normal = z.
82  transform =
83  simox::math::pose(Quaternionf::FromTwoVectors(normal, Vector3f::UnitZ())) * transform;
84 
86  pcl::transformPointCloud(cloud, *aligned, transform);
87  }
88 
89  auto
90  FitKnownRectangleRotationMaxPoints::getAlignedPointCloud() const
91  -> pcl::PointCloud<PointT>::ConstPtr
92  {
93  return aligned;
94  }
95 
96  void
97  FitKnownRectangleRotationMaxPoints::setRotated(float angle) const
98  {
99  const Eigen::Matrix3f rot =
100  Eigen::AngleAxisf(-angle, Eigen::Vector3f::UnitZ()).toRotationMatrix();
101  pcl::transformPointCloud(*aligned, *rotated, simox::math::pose(rot));
102  }
103 
104  bool
105  FitKnownRectangleRotationMaxPoints::isInlier(const PointT& p) const
106  {
107  return std::abs(p.x) <= 0.5f * rectangleSize.x() &&
108  std::abs(p.y) <= 0.5f * rectangleSize.y();
109  }
110 
111  size_t
113  {
114  setRotated(angle);
115  // Count inliers.
116  long count = std::count_if(
117  rotated->begin(), rotated->end(), [this](const PointT& p) { return isInlier(p); });
118  return size_t(count);
119  }
120 
121  pcl::PointIndicesPtr
123  {
124  setRotated(angle);
125 
126  // Find inliers.
127  pcl::PointIndicesPtr indices(new pcl::PointIndices);
128  for (size_t i = 0; i < rotated->size(); ++i)
129  {
130  if (isInlier(rotated->points[i]))
131  {
132  indices->indices.push_back(int(i));
133  }
134  }
135 
136  return indices;
137  }
138 
141  {
142  Eigen::AngleAxisf localRotation(angle, Eigen::Vector3f::UnitZ());
143  return simox::math::inverted_pose(alignedTransform) * simox::math::pose(localRotation);
144  }
145 
146  std::vector<size_t>
148  {
149  return inlierCounts;
150  }
151 
152  float
154  {
155  return bestAngle;
156  }
157 
158 
159 } // namespace visionx
visionx::FitKnownRectangleRotationMaxPoints::setRectangleSize
void setRectangleSize(const Eigen::Vector2f &value)
Set the rectangle size.
Definition: FitKnownRectangleRotationMaxPoints.cpp:20
FitKnownRectangleRotationMaxPoints.h
pcl
Definition: pcl_point_operators.cpp:3
visionx::FitKnownRectangleRotationMaxPoints::alignedTransform
Eigen::Matrix4f alignedTransform
Transformation applied to get aligned.
Definition: FitKnownRectangleRotationMaxPoints.h:142
Eigen
Definition: Elements.h:32
visionx::FitKnownRectangleRotationMaxPoints::getRectangleSize
Eigen::Vector2f getRectangleSize() const
Definition: FitKnownRectangleRotationMaxPoints.cpp:14
visionx::FitKnownRectangleRotationMaxPoints::aligned
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr aligned
Transformed so center is at 0 and z = normal.
Definition: FitKnownRectangleRotationMaxPoints.h:140
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:46
visionx::FitKnownRectangleRotationMaxPoints::numSteps
size_t numSteps
The number of steps (i.e. resolution of solution space).
Definition: FitKnownRectangleRotationMaxPoints.h:133
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::FitKnownRectangleRotationMaxPoints::rectangleSize
Eigen::Vector2f rectangleSize
The rectangle size.
Definition: FitKnownRectangleRotationMaxPoints.h:130
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
visionx::FitKnownRectangleRotationMaxPoints::angleToPose
Eigen::Matrix4f angleToPose(float angle)
Get the global pose corresponding to a local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:140
visionx::FitKnownRectangleRotationMaxPoints::getInlierCounts
std::vector< size_t > getInlierCounts() const
Get the inlier counts per step.
Definition: FitKnownRectangleRotationMaxPoints.cpp:147
visionx::FitKnownRectangleRotationMaxPoints::bestStep
size_t bestStep
Definition: FitKnownRectangleRotationMaxPoints.h:151
visionx::FitKnownRectangleRotationMaxPoints::countInliersOfAngle
size_t countInliersOfAngle(float angle) const
Count the inliers for a given local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:112
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:281
visionx::FitKnownRectangleRotationMaxPoints::inlierCounts
std::vector< size_t > inlierCounts
The inlier counts per step.
Definition: FitKnownRectangleRotationMaxPoints.h:155
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:717
visionx::FitKnownRectangleRotationMaxPoints::getNumSteps
size_t getNumSteps() const
Definition: FitKnownRectangleRotationMaxPoints.cpp:26
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:38
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:351
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:32
PointCloud
Definition: PointCloud.h:85
visionx::FitKnownRectangleRotationMaxPoints::stepToAngle
float stepToAngle(Int step) const
Get the local rotation angle of a solution step.
Definition: FitKnownRectangleRotationMaxPoints.h:104
visionx::FitKnownRectangleRotationMaxPoints::FitKnownRectangleRotationMaxPoints
FitKnownRectangleRotationMaxPoints()
Definition: FitKnownRectangleRotationMaxPoints.cpp:9
visionx::FitKnownRectangleRotationMaxPoints::bestAngle
float bestAngle
The best angle.
Definition: FitKnownRectangleRotationMaxPoints.h:153
visionx::FitKnownRectangleRotationMaxPoints::PointT
pcl::PointXYZRGBA PointT
Definition: FitKnownRectangleRotationMaxPoints.h:24
armarx::view_selection::skills::direction::state::center
state::Type center(state::Type previous)
Definition: LookDirection.cpp:233
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:109
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:95
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
visionx::FitKnownRectangleRotationMaxPoints::getBestAngle
float getBestAngle() const
Get the best local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:153
visionx::FitKnownRectangleRotationMaxPoints::rotated
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr rotated
Buffer for point clouds rotated for inlier counting.
Definition: FitKnownRectangleRotationMaxPoints.h:145
visionx::FitKnownRectangleRotationMaxPoints::findInliersOfAngle
pcl::PointIndicesPtr findInliersOfAngle(float angle) const
Get the inliers for a given local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:122