small_gicp
point_cloud.hpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright 2024 Kenji Koide
2 // SPDX-License-Identifier: MIT
3 #pragma once
4 
5 #include <memory>
6 #include <vector>
7 #include <Eigen/Core>
9 
10 namespace small_gicp {
11 
15 struct PointCloud {
16 public:
17  using Ptr = std::shared_ptr<PointCloud>;
18  using ConstPtr = std::shared_ptr<const PointCloud>;
19 
22 
25  template <typename T, int D, typename Allocator>
26  explicit PointCloud(const std::vector<Eigen::Matrix<T, D, 1>, Allocator>& points) {
27  this->resize(points.size());
28  for (size_t i = 0; i < points.size(); i++) {
29  this->point(i) << points[i].template cast<double>().template head<3>(), 1.0;
30  }
31  }
32 
35 
37  size_t size() const { return points.size(); }
38 
40  bool empty() const { return points.empty(); }
41 
44  void resize(size_t n) {
45  points.resize(n);
46  normals.resize(n);
47  covs.resize(n);
48  }
49 
51  Eigen::Vector4d& point(size_t i) { return points[i]; }
52 
54  Eigen::Vector4d& normal(size_t i) { return normals[i]; }
55 
57  Eigen::Matrix4d& cov(size_t i) { return covs[i]; }
58 
60  const Eigen::Vector4d& point(size_t i) const { return points[i]; }
61 
63  const Eigen::Vector4d& normal(size_t i) const { return normals[i]; }
64 
66  const Eigen::Matrix4d& cov(size_t i) const { return covs[i]; }
67 
68 public:
69  std::vector<Eigen::Vector4d> points;
70  std::vector<Eigen::Vector4d> normals;
71  std::vector<Eigen::Matrix4d> covs;
72 };
73 
74 namespace traits {
75 
76 template <>
77 struct Traits<PointCloud> {
78  using Points = PointCloud;
79 
80  static size_t size(const Points& points) { return points.size(); }
81 
82  static bool has_points(const Points& points) { return !points.points.empty(); }
83  static bool has_normals(const Points& points) { return !points.normals.empty(); }
84  static bool has_covs(const Points& points) { return !points.covs.empty(); }
85 
86  static const Eigen::Vector4d& point(const Points& points, size_t i) { return points.point(i); }
87  static const Eigen::Vector4d& normal(const Points& points, size_t i) { return points.normal(i); }
88  static const Eigen::Matrix4d& cov(const Points& points, size_t i) { return points.cov(i); }
89 
90  static void resize(Points& points, size_t n) { points.resize(n); }
91  static void set_point(Points& points, size_t i, const Eigen::Vector4d& pt) { points.point(i) = pt; }
92  static void set_normal(Points& points, size_t i, const Eigen::Vector4d& n) { points.normal(i) = n; }
93  static void set_cov(Points& points, size_t i, const Eigen::Matrix4d& cov) { points.cov(i) = cov; }
94 };
95 
96 } // namespace traits
97 
98 } // namespace small_gicp
auto cov(const T &points, size_t i)
Get i-th covariance. Only the top-left 3x3 matrix is filled, and the bottom row and the right col mus...
Definition: traits.hpp:52
Definition: flat_container.hpp:12
Point cloud.
Definition: point_cloud.hpp:15
bool empty() const
Check if the point cloud is empty.
Definition: point_cloud.hpp:40
~PointCloud()
Destructor.
Definition: point_cloud.hpp:34
PointCloud()
Constructor.
Definition: point_cloud.hpp:21
const Eigen::Matrix4d & cov(size_t i) const
Get i-th covariance (const).
Definition: point_cloud.hpp:66
std::vector< Eigen::Matrix4d > covs
Point covariances (3x3 matrix) + zero padding.
Definition: point_cloud.hpp:71
std::vector< Eigen::Vector4d > points
Point coordinates (x, y, z, 1)
Definition: point_cloud.hpp:69
std::shared_ptr< const PointCloud > ConstPtr
Definition: point_cloud.hpp:18
const Eigen::Vector4d & normal(size_t i) const
Get i-th normal (const).
Definition: point_cloud.hpp:63
Eigen::Matrix4d & cov(size_t i)
Get i-th covariance.
Definition: point_cloud.hpp:57
Eigen::Vector4d & point(size_t i)
Get i-th point.
Definition: point_cloud.hpp:51
const Eigen::Vector4d & point(size_t i) const
Get i-th point (const).
Definition: point_cloud.hpp:60
std::vector< Eigen::Vector4d > normals
Point normals (nx, ny, nz, 0)
Definition: point_cloud.hpp:70
size_t size() const
Number of points.
Definition: point_cloud.hpp:37
void resize(size_t n)
Resize point/normal/cov buffers.
Definition: point_cloud.hpp:44
PointCloud(const std::vector< Eigen::Matrix< T, D, 1 >, Allocator > &points)
Constructor.
Definition: point_cloud.hpp:26
std::shared_ptr< PointCloud > Ptr
Definition: point_cloud.hpp:17
Eigen::Vector4d & normal(size_t i)
Get i-th normal.
Definition: point_cloud.hpp:54
static bool has_points(const Points &points)
Definition: point_cloud.hpp:82
static const Eigen::Vector4d & normal(const Points &points, size_t i)
Definition: point_cloud.hpp:87
static void resize(Points &points, size_t n)
Definition: point_cloud.hpp:90
static void set_normal(Points &points, size_t i, const Eigen::Vector4d &n)
Definition: point_cloud.hpp:92
static bool has_normals(const Points &points)
Definition: point_cloud.hpp:83
static size_t size(const Points &points)
Definition: point_cloud.hpp:80
static void set_point(Points &points, size_t i, const Eigen::Vector4d &pt)
Definition: point_cloud.hpp:91
static const Eigen::Vector4d & point(const Points &points, size_t i)
Definition: point_cloud.hpp:86
static bool has_covs(const Points &points)
Definition: point_cloud.hpp:84
static void set_cov(Points &points, size_t i, const Eigen::Matrix4d &cov)
Definition: point_cloud.hpp:93
static const Eigen::Matrix4d & cov(const Points &points, size_t i)
Definition: point_cloud.hpp:88
Definition: traits.hpp:13