pcl_eigen.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @author Fabian Reister ( fabian dot reister at kit dot edu )
17  * @date 2021
18  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
19  * GNU General Public License
20  */
21 
22 #pragma once
23 
24 #include <algorithm>
25 
26 #include <Eigen/Core>
27 
28 #include <pcl/point_cloud.h>
29 #include <pcl/point_types.h>
30 
31 namespace armarx::conversions
32 {
33  // pcl2eigen
34 
35  inline Eigen::Vector2f pcl2eigen(const pcl::PointXY& pt)
36  {
37  return Eigen::Vector2f{pt.x, pt.y};
38  }
39  inline Eigen::Vector3f pcl2eigen(const pcl::PointXYZ& pt)
40  {
41  return Eigen::Vector3f{pt.x, pt.y, pt.z};
42  }
43 
44  template <typename PointT,
45  typename EigenVector = decltype(pcl2eigen(PointT()))>
46  auto pcl2eigen(const pcl::PointCloud<PointT>& pointCloud)
47  -> std::vector<EigenVector>
48  {
49  std::vector<EigenVector> v;
50  v.reserve(pointCloud.points.size());
51 
52  std::transform(pointCloud.points.begin(),
53  pointCloud.points.end(),
54  std::back_inserter(v),
55  static_cast<EigenVector(*)(const PointT&)>(pcl2eigen));
56 
57  return v;
58  }
59 
60  // eigen2pcl
61 
62  inline pcl::PointXY eigen2pcl(const Eigen::Vector2f& pt)
63  {
64  return pcl::PointXY{pt.x(), pt.y()};
65  }
66 
67  inline pcl::PointXYZ eigen2pcl(const Eigen::Vector3f& pt)
68  {
69  return pcl::PointXYZ{pt.x(), pt.y(), 0.F};
70  }
71 
72  template <typename EigenVector,
73  typename PointT = decltype(eigen2pcl(EigenVector()))>
74  auto eigen2pcl(const std::vector<EigenVector>& points)
75  -> pcl::PointCloud<PointT>
76  {
77  pcl::PointCloud<PointT> pointCloud;
78  pointCloud.points.reserve(points.size());
79 
80  std::transform(points.begin(),
81  points.end(),
82  std::back_inserter(pointCloud.points),
83  static_cast<PointT(*)(const EigenVector&)>(eigen2pcl));
84 
85  pointCloud.width = pointCloud.points.size();
86  pointCloud.height = 1;
87  pointCloud.is_dense = true;
88 
89  return pointCloud;
90  }
91 
92 } // namespace armarx::conversions
armarx::conversions
Definition: eigen.cpp:5
armarx::conversions::eigen2pcl
pcl::PointXY eigen2pcl(const Eigen::Vector2f &pt)
Definition: pcl_eigen.h:62
armarx::PointT
pcl::PointXYZRGBL PointT
Definition: Common.h:28
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::conversions::pcl2eigen
Eigen::Vector2f pcl2eigen(const pcl::PointXY &pt)
Definition: pcl_eigen.h:35
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