FitKnownRectangleRotationMaxPoints.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Eigen/Geometry>
4 
5 #include <pcl/common/transforms.h>
6 #include <pcl/point_cloud.h>
7 #include <pcl/point_types.h>
8 
9 namespace Eigen
10 {
11  using Hyperplane3f = Hyperplane<float, 3>;
12 }
13 
14 namespace visionx
15 {
16 
17  /**
18  * @brief Finds the rotation of a rectangle of known size at a given position
19  * that contains the most points from a point cloud.
20  */
22  {
23  public:
24  using PointT = pcl::PointXYZRGBA;
25 
26 
27  public:
29 
30 
31  // Input
32 
33  /// Set the rectangle size.
34  void setRectangleSize(const Eigen::Vector2f& value);
35  Eigen::Vector2f getRectangleSize() const;
36 
37  /// Set the number of solution steps, i.e. the resolution of the solution space.
38  void setNumSteps(const size_t& value);
39  size_t getNumSteps() const;
40 
41 
42  /**
43  * @brief Set the input cloud and the rectangle's position and normal.
44  * @param cloud The input cloud.
45  * @param normal The rectangle normal.
46  * @param center The rectangle center.
47  */
48  void setInputCloud(const pcl::PointCloud<PointT>& cloud,
49  Eigen::Vector3f normal,
50  Eigen::Vector3f center);
51 
52 
53  // Run
54 
55  /**
56  * @brief Count the number of inliers per rotation step, and return
57  * the global pose corresponding to the rotation angle with the highest
58  * number of inliers.
59  *
60  * The best rotation angle can be retrieved via `getAngle()`.
61  * The number of inliers per step retrieved via `getInlierCounts()`.
62  *
63  * @return The global pose of the fitted rectangle.
64  */
66  /// @see `setInputCloud()`, `fit()`
68  fit(const pcl::PointCloud<PointT>& cloud, Eigen::Vector3f normal, Eigen::Vector3f center);
69 
70 
71  /// Count the inliers for a given local rotation angle.
72  size_t countInliersOfAngle(float angle) const;
73  /// Get the inliers for a given local rotation angle.
74  pcl::PointIndicesPtr findInliersOfAngle(float angle) const;
75 
76 
77  // Results
78 
79  /// Get the best local rotation angle.
80  float getBestAngle() const;
81 
82  /// Get the best local rotation angle.
83  size_t
84  getNumInliers() const
85  {
86  return inlierCounts.at(bestStep);
87  }
88 
89  /// Get the inliers of the best local rotation angle.
90  pcl::PointIndicesPtr
91  getInliers() const
92  {
94  }
95 
96  /// Get the inlier counts per step.
97  std::vector<size_t> getInlierCounts() const;
98 
99  // Helpers
100 
101  /// Get the local rotation angle of a solution step.
102  template <typename Int>
103  float
104  stepToAngle(Int step) const
105  {
106  return (step / float(numSteps)) * maxAngle;
107  }
108 
109  /// Get the global pose corresponding to a local rotation angle.
111 
112 
113  private:
114  /// Initialize `aligned` and `alignedTransform`.
115  void initAlignedPointCloud(const pcl::PointCloud<PointT>& cloud,
116  Eigen::Vector3f normal,
117  Eigen::Vector3f center);
118  pcl::PointCloud<PointT>::ConstPtr getAlignedPointCloud() const;
119 
120  /// Set `rotated` by rotating `aligned` by the given angle.
121  void setRotated(float angle) const;
122  /// Checks whether a rotated point is an inlier.
123  bool isInlier(const PointT& p) const;
124 
125 
126  public:
127  // Settings.
128 
129  /// The rectangle size.
130  Eigen::Vector2f rectangleSize = Eigen::Vector2f::Ones();
131 
132  /// The number of steps (i.e. resolution of solution space).
133  size_t numSteps = 180;
134  const float maxAngle = float(M_PI); // Need to cover 180 degrees.
135 
136 
137  // Runtime.
138 
139  /// Transformed so center is at 0 and z = normal.
140  pcl::PointCloud<pcl::PointXYZRGBA>::Ptr aligned{new pcl::PointCloud<pcl::PointXYZRGBA>};
141  /// Transformation applied to get `aligned`.
143 
144  /// Buffer for point clouds rotated for inlier counting.
145  mutable pcl::PointCloud<pcl::PointXYZRGBA>::Ptr rotated{
146  new pcl::PointCloud<pcl::PointXYZRGBA>};
147 
148 
149  // Results
150 
151  size_t bestStep = 0;
152  /// The best angle.
153  float bestAngle = 0;
154  /// The inlier counts per step.
155  std::vector<size_t> inlierCounts;
156  };
157 
158 } // namespace visionx
visionx::FitKnownRectangleRotationMaxPoints::setRectangleSize
void setRectangleSize(const Eigen::Vector2f &value)
Set the rectangle size.
Definition: FitKnownRectangleRotationMaxPoints.cpp:20
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
visionx::FitKnownRectangleRotationMaxPoints
Finds the rotation of a rectangle of known size at a given position that contains the most points fro...
Definition: FitKnownRectangleRotationMaxPoints.h:21
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
visionx::FitKnownRectangleRotationMaxPoints::getInliers
pcl::PointIndicesPtr getInliers() const
Get the inliers of the best local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.h:91
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
visionx::FitKnownRectangleRotationMaxPoints::maxAngle
const float maxAngle
Definition: FitKnownRectangleRotationMaxPoints.h:134
visionx::FitKnownRectangleRotationMaxPoints::inlierCounts
std::vector< size_t > inlierCounts
The inlier counts per step.
Definition: FitKnownRectangleRotationMaxPoints.h:155
M_PI
#define M_PI
Definition: MathTools.h:17
Eigen::Hyperplane3f
Hyperplane< float, 3 > Hyperplane3f
Definition: Elements.h:34
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
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
visionx::FitKnownRectangleRotationMaxPoints::stepToAngle
float stepToAngle(Int step) const
Get the local rotation angle of a solution step.
Definition: FitKnownRectangleRotationMaxPoints.h:104
float
#define float
Definition: 16_Level.h:22
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
visionx::FitKnownRectangleRotationMaxPoints::getNumInliers
size_t getNumInliers() const
Get the best local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.h:84
armarx::view_selection::skills::direction::state::center
state::Type center(state::Type previous)
Definition: LookDirection.cpp:233
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
angle
double angle(const Point &a, const Point &b, const Point &c)
Definition: point.hpp:109
visionx::FitKnownRectangleRotationMaxPoints::getBestAngle
float getBestAngle() const
Get the best local rotation angle.
Definition: FitKnownRectangleRotationMaxPoints.cpp:153
visionx::PointT
pcl::PointXYZRGBA PointT
Definition: MaskRCNNPointCloudObjectLocalizer.h:77
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