tools.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <set>
4 
5 #include <Eigen/Geometry>
6 
7 #include <pcl/point_cloud.h>
8 #include <pcl/point_types.h>
9 #include <pcl/PointIndices.h>
10 
11 #include <pcl/point_types_conversion.h>
12 #include <pcl/common/io.h>
13 
14 
15 namespace Eigen
16 {
17  using Hyperplane3f = Hyperplane<float, 3>;
18 }
19 
20 
21 namespace visionx
22 {
23 
24  Eigen::Vector3f pointCloudMedian(const pcl::PointCloud<pcl::PointXYZRGBA>& cloud,
25  const pcl::PointIndices& indices = {});
26 
27 
28  Eigen::Hyperplane3f fitPlaneSVD(const pcl::PointCloud<pcl::PointXYZRGBA>& cloud,
29  const pcl::PointIndices& indices,
30  Eigen::Vector3f center = Eigen::Vector3f::Zero());
31  Eigen::Hyperplane3f fitPlaneSVD(const pcl::PointCloud<pcl::PointXYZRGBA>& cloud,
32  Eigen::Vector3f center = Eigen::Vector3f::Zero());
33 
34  /// Flip `plane` if its normal does not point towards `normal`.
35  Eigen::Hyperplane3f alignPlaneNormal(Eigen::Hyperplane3f plane, Eigen::Vector3f normal);
36 
37 
38  pcl::PointIndicesPtr getPlaneInliers(const pcl::PointCloud<pcl::PointXYZRGBA>& cloud,
39  Eigen::Hyperplane3f plane, float maxDistance);
40 
41 
42  float hsvDistance(const pcl::PointXYZHSV& lhs, const pcl::PointXYZHSV& rhs,
43  const Eigen::Vector3f& hsvWeights = Eigen::Vector3f::Ones());
44  float hsvDistance(const Eigen::Vector3f& lhs, const Eigen::Vector3f& rhs,
45  const Eigen::Vector3f& hsvWeights = Eigen::Vector3f::Ones());
46 
47 
48  Eigen::Vector3f hsvMean(const pcl::PointCloud<pcl::PointXYZHSV>& hsvCloud, const std::vector<int>& indices);
49 
50  template <class PointRGB>
51  Eigen::Vector3f hsvMean(const pcl::PointCloud<PointRGB>& rgbCloud, const std::vector<int>& indices)
52  {
53  pcl::PointCloud<pcl::PointXYZHSV> hsvCloud;
54  pcl::PointCloudXYZRGBAtoXYZHSV(rgbCloud, hsvCloud);
55  return hsvMean(hsvCloud, indices);
56  }
57 
58 
59  template <class PointT>
60  void copyPointCloudWithoutIndices(const pcl::PointCloud<PointT>& input, const std::vector<int>& indices,
61  pcl::PointCloud<PointT>& output)
62  {
63  const std::set<int> indexSet(indices.begin(), indices.end());
64  output.clear();
65  output.reserve(input.size() - indexSet.size());
66  for (size_t i = 0; i < input.size(); ++i)
67  {
68  if (indexSet.count(int(i)) == 0)
69  {
70  output.push_back(input[i]);
71  }
72  }
73  }
74 
75 
76  template <class PointT>
77  typename pcl::PointCloud<PointT>::Ptr getPointCloudWithoutIndices(
78  const pcl::PointCloud<PointT>& input, const std::vector<int>& indices)
79  {
80  typename pcl::PointCloud<PointT>::Ptr output(new pcl::PointCloud<PointT>);
82  return output;
83  }
84 
85 
86  template <class PointT>
87  typename pcl::PointCloud<PointT>::Ptr getPointCloudWithIndices(
88  const pcl::PointCloud<PointT>& input, const std::vector<int>& indices)
89  {
90  typename pcl::PointCloud<PointT>::Ptr output(new pcl::PointCloud<PointT>);
91  pcl::copyPointCloud(input, indices, *output);
92  return output;
93  }
94 
95  template <class PointT>
96  typename pcl::PointCloud<PointT>::Ptr getPointCloudWithIndices(
97  const pcl::PointCloud<PointT>& input, const pcl::PointIndices& indices)
98  {
99  typename pcl::PointCloud<PointT>::Ptr output(new pcl::PointCloud<PointT>);
100  pcl::copyPointCloud(input, indices.indices, *output);
101  return output;
102  }
103 
104 
105  inline bool comparePointClustersDescending(const pcl::PointIndices& lhs, const pcl::PointIndices& rhs)
106  {
107  return lhs.indices.size() > rhs.indices.size();
108  }
109 
110  inline void sortPointClustersDescending(std::vector<pcl::PointIndices>& clusterIndices)
111  {
112  std::sort(clusterIndices.begin(), clusterIndices.end(), comparePointClustersDescending);
113  }
114 
115  template <class PointT>
116  inline void filterNaNs(pcl::PointCloud<PointT>& pc)
117  {
118  const auto isNan = [](const PointT& p)->bool{
119  return std::isnan(p.z);
120  };
121 
122  pc.points.erase(std::remove_if(pc.points.begin(), pc.points.end(), isNan), pc.points.end());
123 
124  // Needed?
125  pc.width = pc.points.size();
126  pc.height = 1;
127  pc.is_dense = true;
128  }
129 
130 }
visionx::fitPlaneSVD
Eigen::Hyperplane3f fitPlaneSVD(const pcl::PointCloud< pcl::PointXYZRGBA > &cloud, const pcl::PointIndices &indices, Eigen::Vector3f center=Eigen::Vector3f::Zero())
Definition: tools.cpp:53
Eigen
Definition: Elements.h:36
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::hsvDistance
float hsvDistance(const pcl::PointXYZHSV &lhs, const pcl::PointXYZHSV &rhs, const Eigen::Vector3f &hsvWeights=Eigen::Vector3f::Ones())
Definition: tools.cpp:111
visionx::sortPointClustersDescending
void sortPointClustersDescending(std::vector< pcl::PointIndices > &clusterIndices)
Definition: tools.h:110
visionx::pointCloudMedian
Eigen::Vector3f pointCloudMedian(const pcl::PointCloud< pcl::PointXYZRGBA > &cloud, const pcl::PointIndices &indices={})
Definition: tools.cpp:7
visionx::hsvMean
Eigen::Vector3f hsvMean(const pcl::PointCloud< pcl::PointXYZHSV > &hsvCloud, const std::vector< int > &indices)
Definition: tools.cpp:129
visionx::copyPointCloudWithoutIndices
void copyPointCloudWithoutIndices(const pcl::PointCloud< PointT > &input, const std::vector< int > &indices, pcl::PointCloud< PointT > &output)
Definition: tools.h:60
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
Eigen::Hyperplane3f
Hyperplane< float, 3 > Hyperplane3f
Definition: Elements.h:38
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
visionx::getPlaneInliers
pcl::PointIndicesPtr getPlaneInliers(const pcl::PointCloud< pcl::PointXYZRGBA > &cloud, Eigen::Hyperplane3f plane, float maxDistance)
Definition: tools.cpp:96
visionx::getPointCloudWithoutIndices
pcl::PointCloud< PointT >::Ptr getPointCloudWithoutIndices(const pcl::PointCloud< PointT > &input, const std::vector< int > &indices)
Definition: tools.h:77
visionx::filterNaNs
void filterNaNs(pcl::PointCloud< PointT > &pc)
Definition: tools.h:116
visionx::alignPlaneNormal
Eigen::Hyperplane3f alignPlaneNormal(Eigen::Hyperplane3f plane, Eigen::Vector3f normal)
Flip plane if its normal does not point towards normal.
Definition: tools.cpp:148
visionx::comparePointClustersDescending
bool comparePointClustersDescending(const pcl::PointIndices &lhs, const pcl::PointIndices &rhs)
Definition: tools.h:105
pc
Introduction Thank you for taking interest in our work and downloading this software This library implements the algorithm described in the paper R R R Klein Efficient RANSAC for Point Cloud Shape in Computer Graphics Blackwell June If you use this software you should cite the aforementioned paper in any resulting publication Please send comments or bug reports to Ruwen Roland BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE Example usage This section shows how to use the library to detect the shapes in a point cloud PointCloud pc
Definition: ReadMe.txt:68
visionx::getPointCloudWithIndices
pcl::PointCloud< PointT >::Ptr getPointCloudWithIndices(const pcl::PointCloud< PointT > &input, const std::vector< int > &indices)
Definition: tools.h:87
visionx::PointT
pcl::PointXYZRGBA PointT
Definition: MaskRCNNPointCloudObjectLocalizer.h:79