Open3D (C++ API)  0.17.0+b7f9f3a
PointCloud.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <string>
11 #include <unordered_map>
12 #include <unordered_set>
13 
14 #include "open3d/core/Tensor.h"
25 #include "open3d/utility/Logging.h"
26 
27 namespace open3d {
28 namespace t {
29 namespace geometry {
30 
31 class LineSet;
32 
80 class PointCloud : public Geometry, public DrawableGeometry {
81 public:
85  PointCloud(const core::Device &device = core::Device("CPU:0"));
86 
94 
100  PointCloud(const std::unordered_map<std::string, core::Tensor>
101  &map_keys_to_tensors);
102 
103  virtual ~PointCloud() override {}
104 
106  std::string ToString() const;
107 
109  const TensorMap &GetPointAttr() const { return point_attr_; }
110 
113 
117  core::Tensor &GetPointAttr(const std::string &key) {
118  return point_attr_.at(key);
119  }
120 
122  core::Tensor &GetPointPositions() { return GetPointAttr("positions"); }
123 
125  core::Tensor &GetPointColors() { return GetPointAttr("colors"); }
126 
128  core::Tensor &GetPointNormals() { return GetPointAttr("normals"); }
129 
133  const core::Tensor &GetPointAttr(const std::string &key) const {
134  return point_attr_.at(key);
135  }
136 
139  return GetPointAttr("positions");
140  }
141 
143  const core::Tensor &GetPointColors() const {
144  return GetPointAttr("colors");
145  }
146 
148  const core::Tensor &GetPointNormals() const {
149  return GetPointAttr("normals");
150  }
151 
157  void SetPointAttr(const std::string &key, const core::Tensor &value) {
158  if (value.GetDevice() != device_) {
159  utility::LogError("Attribute device {} != Pointcloud's device {}.",
160  value.GetDevice().ToString(), device_.ToString());
161  }
162  point_attr_[key] = value;
163  }
164 
166  void SetPointPositions(const core::Tensor &value) {
168  SetPointAttr("positions", value);
169  }
170 
172  void SetPointColors(const core::Tensor &value) {
174  SetPointAttr("colors", value);
175  }
176 
178  void SetPointNormals(const core::Tensor &value) {
180  SetPointAttr("normals", value);
181  }
182 
187  bool HasPointAttr(const std::string &key) const {
188  return point_attr_.Contains(key) && GetPointAttr(key).GetLength() > 0 &&
189  GetPointAttr(key).GetLength() == GetPointPositions().GetLength();
190  }
191 
196  void RemovePointAttr(const std::string &key) { point_attr_.Erase(key); }
197 
200  bool HasPointPositions() const { return HasPointAttr("positions"); }
201 
207  bool HasPointColors() const { return HasPointAttr("colors"); }
208 
214  bool HasPointNormals() const { return HasPointAttr("normals"); }
215 
216 public:
222  PointCloud To(const core::Device &device, bool copy = false) const;
223 
225  PointCloud Clone() const;
226 
228  PointCloud &Clear() override {
229  point_attr_.clear();
230  return *this;
231  }
232 
234  bool IsEmpty() const override { return !HasPointPositions(); }
235 
237  core::Tensor GetMinBound() const;
238 
240  core::Tensor GetMaxBound() const;
241 
243  core::Tensor GetCenter() const;
244 
250  PointCloud Append(const PointCloud &other) const;
251 
254  PointCloud operator+(const PointCloud &other) const {
255  return Append(other);
256  }
257 
277  PointCloud &Transform(const core::Tensor &transformation);
278 
284  PointCloud &Translate(const core::Tensor &translation,
285  bool relative = true);
286 
292  PointCloud &Scale(double scale, const core::Tensor &center);
293 
300  PointCloud &Rotate(const core::Tensor &R, const core::Tensor &center);
301 
307 
314  PointCloud SelectByMask(const core::Tensor &boolean_mask,
315  bool invert = false) const;
316 
325  PointCloud SelectByIndex(const core::Tensor &indices,
326  bool invert = false,
327  bool remove_duplicates = false) const;
328 
333  PointCloud VoxelDownSample(double voxel_size,
334  const std::string &reduction = "mean") const;
335 
341  PointCloud UniformDownSample(size_t every_k_points) const;
342 
348  PointCloud RandomDownSample(double sampling_ratio) const;
349 
357  PointCloud FarthestPointDownSample(size_t num_samples) const;
358 
366  std::tuple<PointCloud, core::Tensor> RemoveRadiusOutliers(
367  size_t nb_points, double search_radius) const;
368 
376  std::tuple<PointCloud, core::Tensor> RemoveStatisticalOutliers(
377  size_t nb_neighbors, double std_ratio) const;
378 
383  std::tuple<PointCloud, core::Tensor> RemoveDuplicatedPoints() const;
384 
392  std::tuple<PointCloud, core::Tensor> RemoveNonFinitePoints(
393  bool remove_nan = true, bool remove_infinite = true) const;
394 
396  core::Device GetDevice() const override { return device_; }
397 
413  std::tuple<TriangleMesh, core::Tensor> HiddenPointRemoval(
414  const core::Tensor &camera_location, double radius) const;
415 
428  core::Tensor ClusterDBSCAN(double eps,
429  size_t min_points,
430  bool print_progress = false) const;
431 
444  std::tuple<core::Tensor, core::Tensor> SegmentPlane(
445  const double distance_threshold = 0.01,
446  const int ransac_n = 3,
447  const int num_iterations = 100,
448  const double probability = 0.99999999) const;
449 
464  TriangleMesh ComputeConvexHull(bool joggle_inputs = false) const;
465 
476  std::tuple<PointCloud, core::Tensor> ComputeBoundaryPoints(
477  double radius,
478  int max_nn = 30,
479  double angle_threshold = 90.0) const;
480 
481 public:
484 
496  void EstimateNormals(
497  const utility::optional<int> max_nn = 30,
499 
505  const core::Tensor &orientation_reference =
506  core::Tensor::Init<float>({0, 0, 1},
507  core::Device("CPU:0")));
508 
514  const core::Tensor &camera_location = core::Tensor::Zeros(
515  {3}, core::Float32, core::Device("CPU:0")));
516 
524 
539  const utility::optional<int> max_nn = 30,
541 
542 public:
568  const Image &depth,
569  const core::Tensor &intrinsics,
570  const core::Tensor &extrinsics =
572  float depth_scale = 1000.0f,
573  float depth_max = 3.0f,
574  int stride = 1,
575  bool with_normals = false);
576 
603  const RGBDImage &rgbd_image,
604  const core::Tensor &intrinsics,
605  const core::Tensor &extrinsics =
607  float depth_scale = 1000.0f,
608  float depth_max = 3.0f,
609  int stride = 1,
610  bool with_normals = false);
611 
613  static PointCloud FromLegacy(
614  const open3d::geometry::PointCloud &pcd_legacy,
615  core::Dtype dtype = core::Float32,
616  const core::Device &device = core::Device("CPU:0"));
617 
620 
623  int width,
624  int height,
625  const core::Tensor &intrinsics,
626  const core::Tensor &extrinsics =
628  float depth_scale = 1000.0f,
629  float depth_max = 3.0f);
630 
633  int width,
634  int height,
635  const core::Tensor &intrinsics,
636  const core::Tensor &extrinsics =
638  float depth_scale = 1000.0f,
639  float depth_max = 3.0f);
640 
643 
646 
653  bool invert = false) const;
654 
660  PointCloud Crop(const OrientedBoundingBox &obb, bool invert = false) const;
661 
670  LineSet ExtrudeRotation(double angle,
671  const core::Tensor &axis,
672  int resolution = 16,
673  double translation = 0.0,
674  bool capping = true) const;
675 
681  LineSet ExtrudeLinear(const core::Tensor &vector,
682  double scale = 1.0,
683  bool capping = true) const;
684 
690  int PCAPartition(int max_points);
691 
692 protected:
695 };
696 
697 } // namespace geometry
698 } // namespace t
699 } // namespace open3d
math::float4 color
Definition: LineSetBuffers.cpp:45
#define LogError(...)
Definition: Logging.h:48
#define AssertTensorShape(tensor,...)
Definition: TensorCheck.h:58
size_t stride
Definition: TriangleMeshBuffers.cpp:165
bool copy
Definition: VtkUtils.cpp:73
Definition: Device.h:18
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
Definition: Device.cpp:88
Definition: Dtype.h:20
Definition: Tensor.h:32
int64_t GetLength() const
Definition: Tensor.h:1124
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:374
Device GetDevice() const override
Definition: Tensor.cpp:1403
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:386
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:159
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:34
LineSet define a sets of lines in 3D. A typical application is to display the point cloud corresponde...
Definition: LineSet.h:29
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:25
A point cloud consists of point coordinates, and optionally point colors and point normals.
Definition: PointCloud.h:36
RGBDImage is for a pair of registered color and depth images,.
Definition: RGBDImage.h:27
Mix-in class for geometry types that can be visualized.
Definition: DrawableGeometry.h:19
The base geometry class.
Definition: Geometry.h:21
A point cloud contains a list of 3D points.
Definition: PointCloud.h:80
std::tuple< PointCloud, core::Tensor > RemoveStatisticalOutliers(size_t nb_neighbors, double std_ratio) const
Remove points that are further away from their nb_neighbor neighbors in average. This function is not...
Definition: PointCloud.cpp:422
core::Tensor & GetPointAttr(const std::string &key)
Definition: PointCloud.h:117
bool HasPointAttr(const std::string &key) const
Definition: PointCloud.h:187
PointCloud & PaintUniformColor(const core::Tensor &color)
Assigns uniform color to the point cloud.
Definition: PointCloud.cpp:526
PointCloud UniformDownSample(size_t every_k_points) const
Downsamples a point cloud by selecting every kth index point and its attributes.
Definition: PointCloud.cpp:343
PointCloud & Clear() override
Clear all data in the point cloud.
Definition: PointCloud.h:228
PointCloud & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotates the PointPositions and PointNormals (if exists).
Definition: PointCloud.cpp:204
std::tuple< core::Tensor, core::Tensor > SegmentPlane(const double distance_threshold=0.01, const int ransac_n=3, const int num_iterations=100, const double probability=0.99999999) const
Segment PointCloud plane using the RANSAC algorithm. This is a wrapper for a CPU implementation and a...
Definition: PointCloud.cpp:1140
PointCloud Crop(const AxisAlignedBoundingBox &aabb, bool invert=false) const
Function to crop pointcloud into output pointcloud.
Definition: PointCloud.cpp:1257
TriangleMesh ComputeConvexHull(bool joggle_inputs=false) const
Definition: PointCloud.cpp:1162
std::tuple< PointCloud, core::Tensor > RemoveRadiusOutliers(size_t nb_points, double search_radius) const
Remove points that have less than nb_points neighbors in a sphere of a given radius.
Definition: PointCloud.cpp:394
std::tuple< PointCloud, core::Tensor > RemoveDuplicatedPoints() const
Remove duplicated points and there associated attributes.
Definition: PointCloud.cpp:484
const core::Tensor & GetPointPositions() const
Get the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:138
geometry::RGBDImage ProjectToRGBDImage(int width, int height, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Float32, core::Device("CPU:0")), float depth_scale=1000.0f, float depth_max=3.0f)
Project a point cloud to an RGBD image.
Definition: PointCloud.cpp:994
bool HasPointPositions() const
Definition: PointCloud.h:200
bool HasPointColors() const
Definition: PointCloud.h:207
PointCloud & Scale(double scale, const core::Tensor &center)
Scales the PointPositions of the PointCloud.
Definition: PointCloud.cpp:194
const core::Tensor & GetPointAttr(const std::string &key) const
Definition: PointCloud.h:133
core::Tensor & GetPointNormals()
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:128
void EstimateNormals(const utility::optional< int > max_nn=30, const utility::optional< double > radius=utility::nullopt)
Function to estimate point normals. If the point cloud normals exist, the estimated normals are orien...
Definition: PointCloud.cpp:587
core::Device device_
Definition: PointCloud.h:693
PointCloud & NormalizeNormals()
Normalize point normals to length 1.
Definition: PointCloud.cpp:506
PointCloud To(const core::Device &device, bool copy=false) const
Definition: PointCloud.cpp:110
std::tuple< PointCloud, core::Tensor > ComputeBoundaryPoints(double radius, int max_nn=30, double angle_threshold=90.0) const
Compute the boundary points of a point cloud. The implementation is inspired by the PCL implementatio...
Definition: PointCloud.cpp:542
core::Device GetDevice() const override
Returns the device attribute of this PointCloud.
Definition: PointCloud.h:396
geometry::Image ProjectToDepthImage(int width, int height, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Float32, core::Device("CPU:0")), float depth_scale=1000.0f, float depth_max=3.0f)
Project a point cloud to a depth image.
Definition: PointCloud.cpp:977
OrientedBoundingBox GetOrientedBoundingBox() const
Create an oriented bounding box from attribute "positions".
Definition: PointCloud.cpp:1236
PointCloud VoxelDownSample(double voxel_size, const std::string &reduction="mean") const
Downsamples a point cloud with a specified voxel size.
Definition: PointCloud.cpp:278
void SetPointPositions(const core::Tensor &value)
Set the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:166
void SetPointColors(const core::Tensor &value)
Set the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:172
core::Tensor ClusterDBSCAN(double eps, size_t min_points, bool print_progress=false) const
Cluster PointCloud using the DBSCAN algorithm Ester et al., "A Density-Based Algorithm for Discoverin...
Definition: PointCloud.cpp:1128
TensorMap point_attr_
Definition: PointCloud.h:694
const core::Tensor & GetPointColors() const
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:143
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const
Create an axis-aligned bounding box from attribute "positions".
Definition: PointCloud.cpp:1232
bool IsEmpty() const override
Returns !HasPointPositions().
Definition: PointCloud.h:234
open3d::geometry::PointCloud ToLegacy() const
Convert to a legacy Open3D PointCloud.
Definition: PointCloud.cpp:1049
core::Tensor & GetPointColors()
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:125
PointCloud Append(const PointCloud &other) const
Definition: PointCloud.cpp:123
PointCloud FarthestPointDownSample(size_t num_samples) const
Downsample a pointcloud into output pointcloud with a set of points has farthest distance.
Definition: PointCloud.cpp:386
const core::Tensor & GetPointNormals() const
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:148
void OrientNormalsToAlignWithDirection(const core::Tensor &orientation_reference=core::Tensor::Init< float >({0, 0, 1}, core::Device("CPU:0")))
Function to orient the normals of a point cloud.
Definition: PointCloud.cpp:681
core::Tensor GetMinBound() const
Returns the min bound for point coordinates.
Definition: PointCloud.cpp:98
int PCAPartition(int max_points)
Definition: PointCloud.cpp:1284
PointCloud RandomDownSample(double sampling_ratio) const
Downsample a pointcloud by selecting random index point and its attributes.
Definition: PointCloud.cpp:361
static PointCloud CreateFromRGBDImage(const RGBDImage &rgbd_image, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Float32, core::Device("CPU:0")), float depth_scale=1000.0f, float depth_max=3.0f, int stride=1, bool with_normals=false)
Factory function to create a point cloud from an RGB-D image and a camera model.
Definition: PointCloud.cpp:950
static PointCloud CreateFromDepthImage(const Image &depth, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Float32, core::Device("CPU:0")), float depth_scale=1000.0f, float depth_max=3.0f, int stride=1, bool with_normals=false)
Factory function to create a point cloud from a depth image and a camera model.
Definition: PointCloud.cpp:920
virtual ~PointCloud() override
Definition: PointCloud.h:103
void EstimateColorGradients(const utility::optional< int > max_nn=30, const utility::optional< double > radius=utility::nullopt)
Function to compute point color gradients. If radius is provided, then HybridSearch is used,...
Definition: PointCloud.cpp:745
PointCloud Clone() const
Returns copy of the point cloud on the same device.
Definition: PointCloud.cpp:121
void RemovePointAttr(const std::string &key)
Definition: PointCloud.h:196
std::tuple< PointCloud, core::Tensor > RemoveNonFinitePoints(bool remove_nan=true, bool remove_infinite=true) const
Remove all points from the point cloud that have a nan entry, or infinite value. It also removes the ...
Definition: PointCloud.cpp:460
core::Tensor & GetPointPositions()
Get the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:122
PointCloud & Transform(const core::Tensor &transformation)
Transforms the PointPositions and PointNormals (if exist) of the PointCloud.
Definition: PointCloud.cpp:169
std::string ToString() const
Text description.
Definition: PointCloud.cpp:72
PointCloud(const core::Device &device=core::Device("CPU:0"))
Definition: PointCloud.cpp:47
core::Tensor GetCenter() const
Returns the center for point coordinates.
Definition: PointCloud.cpp:106
static PointCloud FromLegacy(const open3d::geometry::PointCloud &pcd_legacy, core::Dtype dtype=core::Float32, const core::Device &device=core::Device("CPU:0"))
Create a PointCloud from a legacy Open3D PointCloud.
Definition: PointCloud.cpp:1026
LineSet ExtrudeLinear(const core::Tensor &vector, double scale=1.0, bool capping=true) const
Definition: PointCloud.cpp:1250
PointCloud operator+(const PointCloud &other) const
Definition: PointCloud.h:254
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
Definition: PointCloud.h:109
std::tuple< TriangleMesh, core::Tensor > HiddenPointRemoval(const core::Tensor &camera_location, double radius) const
This is an implementation of the Hidden Point Removal operator described in Katz et....
Definition: PointCloud.cpp:1102
void OrientNormalsTowardsCameraLocation(const core::Tensor &camera_location=core::Tensor::Zeros({3}, core::Float32, core::Device("CPU:0")))
Function to orient the normals of a point cloud.
Definition: PointCloud.cpp:708
TensorMap & GetPointAttr()
Getter for point_attr_ TensorMap.
Definition: PointCloud.h:112
core::Tensor GetMaxBound() const
Returns the max bound for point coordinates.
Definition: PointCloud.cpp:102
LineSet ExtrudeRotation(double angle, const core::Tensor &axis, int resolution=16, double translation=0.0, bool capping=true) const
Definition: PointCloud.cpp:1240
void OrientNormalsConsistentTangentPlane(size_t k)
Function to consistently orient estimated normals based on consistent tangent planes as described in ...
Definition: PointCloud.cpp:734
void SetPointAttr(const std::string &key, const core::Tensor &value)
Definition: PointCloud.h:157
PointCloud SelectByIndex(const core::Tensor &indices, bool invert=false, bool remove_duplicates=false) const
Select points from input pointcloud, based on indices list into output point cloud.
Definition: PointCloud.cpp:243
PointCloud SelectByMask(const core::Tensor &boolean_mask, bool invert=false) const
Select points from input pointcloud, based on boolean mask indices into output point cloud.
Definition: PointCloud.cpp:217
void SetPointNormals(const core::Tensor &value)
Set the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:178
bool HasPointNormals() const
Definition: PointCloud.h:214
PointCloud & Translate(const core::Tensor &translation, bool relative=true)
Translates the PointPositions of the PointCloud.
Definition: PointCloud.cpp:180
Definition: TensorMap.h:31
std::size_t Erase(const std::string key)
Erase elements for the TensorMap by key value, if the key exists. If the key does not exists,...
Definition: TensorMap.h:92
bool Contains(const std::string &key) const
Definition: TensorMap.h:187
A triangle mesh contains vertices and triangles.
Definition: TriangleMesh.h:92
Definition: Optional.h:259
int width
Definition: FilePCD.cpp:52
int height
Definition: FilePCD.cpp:53
int points
Definition: FilePCD.cpp:54
const Dtype Float32
Definition: Dtype.cpp:42
constexpr nullopt_t nullopt
Definition: Optional.h:152
Definition: PinholeCameraIntrinsic.cpp:16