gtsam_points
Loading...
Searching...
No Matches
point_cloud_cpu.hpp
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2021 Kenji Koide (k.koide@aist.go.jp)
3
4#pragma once
5
6#include <vector>
7#include <random>
8#include <Eigen/Core>
9
10#include <gtsam_points/types/point_cloud.hpp>
11
12struct CUstream_st;
13
14namespace gtsam_points {
15
19struct PointCloudCPU : public PointCloud {
20public:
21 using Ptr = std::shared_ptr<PointCloudCPU>;
22 using ConstPtr = std::shared_ptr<const PointCloudCPU>;
23
26
32 template <typename T, int D>
33 PointCloudCPU(const Eigen::Matrix<T, D, 1>* points, int num_points);
34
39 template <typename T, int D, typename Alloc>
40 PointCloudCPU(const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& points) : PointCloudCPU(points.data(), points.size()) {}
41
42 // Forbid shallow copy
43 PointCloudCPU(const PointCloudCPU& points) = delete;
44 PointCloudCPU& operator=(PointCloudCPU const&) = delete;
45
47 static PointCloudCPU::Ptr clone(const PointCloud& points);
48
49 template <typename T>
50 void add_times(const T* times, int num_points);
51 template <typename T>
52 void add_times(const std::vector<T>& times) {
53 add_times(times.data(), times.size());
54 }
55
56 template <typename T, int D>
57 void add_points(const Eigen::Matrix<T, D, 1>* points, int num_points);
58 template <typename T, int D, typename Alloc>
59 void add_points(const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& points) {
60 add_points(points.data(), points.size());
61 }
62
63 template <typename T, int D>
64 void add_normals(const Eigen::Matrix<T, D, 1>* normals, int num_points);
65 template <typename T, int D, typename Alloc>
66 void add_normals(const std::vector<Eigen::Matrix<T, D, 1>, Alloc>& normals) {
67 add_normals(normals.data(), normals.size());
68 }
69
70 template <typename T, int D>
71 void add_covs(const Eigen::Matrix<T, D, D>* covs, int num_points);
72 template <typename T, int D, typename Alloc>
73 void add_covs(const std::vector<Eigen::Matrix<T, D, D>, Alloc>& covs) {
74 add_covs(covs.data(), covs.size());
75 }
76
77 template <typename T>
78 void add_intensities(const T* intensities, int num_points);
79 template <typename T>
80 void add_intensities(const std::vector<T>& intensities) {
81 add_intensities(intensities.data(), intensities.size());
82 }
83
84 template <typename T>
85 void add_aux_attribute(const std::string& attrib_name, const T* values, int num_points) {
86 auto attributes = std::make_shared<std::vector<T>>(values, values + num_points);
87 aux_attributes_storage[attrib_name] = attributes;
88 aux_attributes[attrib_name] = std::make_pair(sizeof(T), attributes->data());
89 }
90 template <typename T, typename Alloc>
91 void add_aux_attribute(const std::string& attrib_name, const std::vector<T, Alloc>& values) {
92 add_aux_attribute(attrib_name, values.data(), values.size());
93 }
94
95 static PointCloudCPU::Ptr load(const std::string& path);
96
98 size_t memory_usage() const;
99
100public:
101 std::vector<double> times_storage;
102 std::vector<Eigen::Vector4d> points_storage;
103 std::vector<Eigen::Vector4d> normals_storage;
104 std::vector<Eigen::Matrix4d> covs_storage;
105 std::vector<double> intensities_storage;
106
107 std::unordered_map<std::string, std::shared_ptr<void>> aux_attributes_storage;
108};
109
116PointCloudCPU::Ptr sample(const PointCloud::ConstPtr& points, const std::vector<int>& indices);
117
125PointCloudCPU::Ptr random_sampling(const PointCloud::ConstPtr& points, const double sampling_rate, std::mt19937& mt);
126
136PointCloudCPU::Ptr voxelgrid_sampling(const PointCloud::ConstPtr& points, const double voxel_resolution, int num_threads = 1);
137
151PointCloudCPU::Ptr randomgrid_sampling(
152 const PointCloud::ConstPtr& points,
153 const double voxel_resolution,
154 const double sampling_rate,
155 std::mt19937& mt,
156 int num_threads = 1);
157
163template <typename Func>
164PointCloudCPU::Ptr filter(const PointCloud::ConstPtr& points, const Func& pred) {
165 std::vector<int> indices;
166 indices.reserve(points->size());
167 for (int i = 0; i < points->size(); i++) {
168 if (pred(points->points[i])) {
169 indices.push_back(i);
170 }
171 }
172 return sample(points, indices);
173}
174
180template <typename Func>
181PointCloudCPU::Ptr filter_by_index(const PointCloud::ConstPtr& points, const Func& pred) {
182 std::vector<int> indices;
183 indices.reserve(points->size());
184 for (int i = 0; i < points->size(); i++) {
185 if (pred(i)) {
186 indices.push_back(i);
187 }
188 }
189 return sample(points, indices);
190}
191
197template <typename Compare>
198PointCloudCPU::Ptr sort(const PointCloud::ConstPtr& points, const Compare& comp) {
199 std::vector<int> indices(points->size());
200 std::iota(indices.begin(), indices.end(), 0);
201 std::sort(indices.begin(), indices.end(), comp);
202 return gtsam_points::sample(points, indices);
203}
204
209PointCloudCPU::Ptr sort_by_time(const PointCloud::ConstPtr& points);
210
218template <typename Scalar, int Mode>
219PointCloudCPU::Ptr transform(const PointCloud::ConstPtr& points, const Eigen::Transform<Scalar, 3, Mode>& transformation);
220
226template <typename Scalar, int Mode>
227void transform_inplace(PointCloud& points, const Eigen::Transform<Scalar, 3, Mode>& transformation);
228
234template <typename Scalar, int Mode>
235void transform_inplace(PointCloud::Ptr points, const Eigen::Transform<Scalar, 3, Mode>& transformation) {
236 transform_inplace(*points, transformation);
237}
238
247std::vector<int>
248find_inlier_points(const PointCloud::ConstPtr& points, const std::vector<int>& neighbors, const int k, const double std_thresh = 1.0);
249
258PointCloudCPU::Ptr remove_outliers(const PointCloud::ConstPtr& points, const std::vector<int>& neighbors, const int k, const double std_thresh = 1.0);
259
267PointCloudCPU::Ptr remove_outliers(const PointCloud::ConstPtr& points, const int k = 10, const double std_thresh = 1.0, const int num_threads = 1);
268
275std::vector<double> distances(const PointCloud::ConstPtr& points, size_t max_scan_count = std::numeric_limits<size_t>::max());
276
283std::pair<double, double> minmax_distance(const PointCloud::ConstPtr& points, size_t max_scan_count = std::numeric_limits<size_t>::max());
284
291double median_distance(const PointCloud::ConstPtr& points, size_t max_scan_count = std::numeric_limits<size_t>::max());
292
301PointCloud::Ptr
302merge_frames(const std::vector<Eigen::Isometry3d>& poses, const std::vector<PointCloud::ConstPtr>& frames, double downsample_resolution);
303
305template <typename PointCloudPtr>
306std::enable_if_t<!std::is_same_v<PointCloudPtr, PointCloud::ConstPtr>, PointCloud::Ptr>
307merge_frames(const std::vector<Eigen::Isometry3d>& poses, const std::vector<PointCloudPtr>& frames, double downsample_resolution) {
308 std::vector<PointCloud::ConstPtr> frames_(frames.begin(), frames.end());
309 return merge_frames(poses, frames_, downsample_resolution);
310}
311
321PointCloud::Ptr merge_frames_gpu(
322 const std::vector<Eigen::Isometry3d>& poses,
323 const std::vector<PointCloud::ConstPtr>& frames,
324 double downsample_resolution,
325 CUstream_st* stream = 0);
326
327template <typename PointCloudPtr>
328std::enable_if_t<!std::is_same_v<PointCloudPtr, PointCloud::ConstPtr>, PointCloud::Ptr> merge_frames_gpu(
329 const std::vector<Eigen::Isometry3d>& poses,
330 const std::vector<PointCloudPtr>& frames,
331 double downsample_resolution,
332 CUstream_st* stream = 0) {
333 std::vector<PointCloud::ConstPtr> frames_(frames.begin(), frames.end());
334 return merge_frames_gpu(poses, frames_, downsample_resolution, stream);
335}
336
345PointCloud::Ptr
346merge_frames_auto(const std::vector<Eigen::Isometry3d>& poses, const std::vector<PointCloud::ConstPtr>& frames, double downsample_resolution);
347
348template <typename PointCloudPtr>
349std::enable_if_t<!std::is_same_v<PointCloudPtr, PointCloud::ConstPtr>, PointCloud::Ptr>
350merge_frames_auto(const std::vector<Eigen::Isometry3d>& poses, const std::vector<PointCloud::ConstPtr>& frames, double downsample_resolution) {
351 std::vector<PointCloud::ConstPtr> frames_(frames.begin(), frames.end());
352 return merge_frames_auto(poses, frames_, downsample_resolution);
353}
354
355} // namespace gtsam_points
Point cloud frame on CPU memory.
Definition point_cloud_cpu.hpp:19
size_t memory_usage() const
Memory usage in bytes.
static PointCloudCPU::Ptr clone(const PointCloud &points)
Deep copy.
PointCloudCPU(const std::vector< Eigen::Matrix< T, D, 1 >, Alloc > &points)
Constructor.
Definition point_cloud_cpu.hpp:40
PointCloudCPU(const Eigen::Matrix< T, D, 1 > *points, int num_points)
Constructor.
Standard point cloud class that holds only pointers to point attributes.
Definition point_cloud.hpp:19
Eigen::Vector4d * normals
Point normals (nx, ny, nz, 0)
Definition point_cloud.hpp:107
size_t num_points
Number of points.
Definition point_cloud.hpp:103
double * times
Per-point timestamp w.r.t. the first point (should be sorted)
Definition point_cloud.hpp:105
Eigen::Matrix4d * covs
Point covariances cov(3, 3) = 0.
Definition point_cloud.hpp:108
size_t size() const
Number of points.
Definition point_cloud.hpp:43
double * intensities
Point intensities.
Definition point_cloud.hpp:109
Eigen::Vector4d * points
Point coordinates (x, y, z, 1)
Definition point_cloud.hpp:106
std::unordered_map< std::string, std::pair< size_t, void * > > aux_attributes
Aux attributes <attribute_name, pair<element_size, data_ptr>>
Definition point_cloud.hpp:112