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
6namespace visionx
7{
8
12
13 Eigen::Vector2f
18
19 void
21 {
22 rectangleSize = value;
23 }
24
25 size_t
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
45 Eigen::Matrix4f
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
61 Eigen::Matrix4f
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
85 alignedTransform = transform;
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
139 Eigen::Matrix4f
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>
151
152 float
157
158
159} // namespace visionx
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr aligned
Transformed so center is at 0 and z = normal.
std::vector< size_t > inlierCounts
The inlier counts per step.
void setInputCloud(const pcl::PointCloud< PointT > &cloud, Eigen::Vector3f normal, Eigen::Vector3f center)
Set the input cloud and the rectangle's position and normal.
std::vector< size_t > getInlierCounts() const
Get the inlier counts per step.
float getBestAngle() const
Get the best local rotation angle.
pcl::PointIndicesPtr findInliersOfAngle(float angle) const
Get the inliers for a given local rotation angle.
float stepToAngle(Int step) const
Get the local rotation angle of a solution step.
Eigen::Matrix4f alignedTransform
Transformation applied to get aligned.
void setNumSteps(const size_t &value)
Set the number of solution steps, i.e. the resolution of the solution space.
Eigen::Matrix4f angleToPose(float angle)
Get the global pose corresponding to a local rotation angle.
pcl::PointCloud< pcl::PointXYZRGBA >::Ptr rotated
Buffer for point clouds rotated for inlier counting.
size_t numSteps
The number of steps (i.e. resolution of solution space).
size_t countInliersOfAngle(float angle) const
Count the inliers for a given local rotation angle.
Eigen::Matrix4f fit()
Count the number of inliers per rotation step, and return the global pose corresponding to the rotati...
void setRectangleSize(const Eigen::Vector2f &value)
Set the rectangle size.
ArmarX headers.
pcl::PointCloud< Point > PointCloud
double angle(const Point &a, const Point &b, const Point &c)
Definition point.hpp:109