Open3D (C++ API)  0.16.1
PointCloud.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <string>
30 #include <unordered_map>
31 #include <unordered_set>
32 
33 #include "open3d/core/Tensor.h"
44 #include "open3d/utility/Logging.h"
45 
46 namespace open3d {
47 namespace t {
48 namespace geometry {
49 
50 class LineSet;
51 
99 class PointCloud : public Geometry, public DrawableGeometry {
100 public:
104  PointCloud(const core::Device &device = core::Device("CPU:0"));
105 
113 
119  PointCloud(const std::unordered_map<std::string, core::Tensor>
120  &map_keys_to_tensors);
121 
122  virtual ~PointCloud() override {}
123 
125  std::string ToString() const;
126 
128  const TensorMap &GetPointAttr() const { return point_attr_; }
129 
132 
136  core::Tensor &GetPointAttr(const std::string &key) {
137  return point_attr_.at(key);
138  }
139 
141  core::Tensor &GetPointPositions() { return GetPointAttr("positions"); }
142 
144  core::Tensor &GetPointColors() { return GetPointAttr("colors"); }
145 
147  core::Tensor &GetPointNormals() { return GetPointAttr("normals"); }
148 
152  const core::Tensor &GetPointAttr(const std::string &key) const {
153  return point_attr_.at(key);
154  }
155 
158  return GetPointAttr("positions");
159  }
160 
162  const core::Tensor &GetPointColors() const {
163  return GetPointAttr("colors");
164  }
165 
167  const core::Tensor &GetPointNormals() const {
168  return GetPointAttr("normals");
169  }
170 
176  void SetPointAttr(const std::string &key, const core::Tensor &value) {
177  if (value.GetDevice() != device_) {
178  utility::LogError("Attribute device {} != Pointcloud's device {}.",
179  value.GetDevice().ToString(), device_.ToString());
180  }
181  point_attr_[key] = value;
182  }
183 
185  void SetPointPositions(const core::Tensor &value) {
187  SetPointAttr("positions", value);
188  }
189 
191  void SetPointColors(const core::Tensor &value) {
193  SetPointAttr("colors", value);
194  }
195 
197  void SetPointNormals(const core::Tensor &value) {
199  SetPointAttr("normals", value);
200  }
201 
206  bool HasPointAttr(const std::string &key) const {
207  return point_attr_.Contains(key) && GetPointAttr(key).GetLength() > 0 &&
208  GetPointAttr(key).GetLength() == GetPointPositions().GetLength();
209  }
210 
215  void RemovePointAttr(const std::string &key) { point_attr_.Erase(key); }
216 
219  bool HasPointPositions() const { return HasPointAttr("positions"); }
220 
226  bool HasPointColors() const { return HasPointAttr("colors"); }
227 
233  bool HasPointNormals() const { return HasPointAttr("normals"); }
234 
235 public:
241  PointCloud To(const core::Device &device, bool copy = false) const;
242 
244  PointCloud Clone() const;
245 
247  PointCloud &Clear() override {
248  point_attr_.clear();
249  return *this;
250  }
251 
253  bool IsEmpty() const override { return !HasPointPositions(); }
254 
256  core::Tensor GetMinBound() const;
257 
259  core::Tensor GetMaxBound() const;
260 
262  core::Tensor GetCenter() const;
263 
269  PointCloud Append(const PointCloud &other) const;
270 
273  PointCloud operator+(const PointCloud &other) const {
274  return Append(other);
275  }
276 
296  PointCloud &Transform(const core::Tensor &transformation);
297 
303  PointCloud &Translate(const core::Tensor &translation,
304  bool relative = true);
305 
311  PointCloud &Scale(double scale, const core::Tensor &center);
312 
319  PointCloud &Rotate(const core::Tensor &R, const core::Tensor &center);
320 
326 
333  PointCloud SelectByMask(const core::Tensor &boolean_mask,
334  bool invert = false) const;
335 
344  PointCloud SelectByIndex(const core::Tensor &indices,
345  bool invert = false,
346  bool remove_duplicates = false) const;
347 
351  PointCloud VoxelDownSample(double voxel_size,
352  const core::HashBackendType &backend =
354 
360  PointCloud UniformDownSample(size_t every_k_points) const;
361 
367  PointCloud RandomDownSample(double sampling_ratio) const;
368 
376  PointCloud FarthestPointDownSample(size_t num_samples) const;
377 
385  std::tuple<PointCloud, core::Tensor> RemoveRadiusOutliers(
386  size_t nb_points, double search_radius) const;
387 
395  std::tuple<PointCloud, core::Tensor> RemoveStatisticalOutliers(
396  size_t nb_neighbors, double std_ratio) const;
397 
402  std::tuple<PointCloud, core::Tensor> RemoveDuplicatedPoints() const;
403 
411  std::tuple<PointCloud, core::Tensor> RemoveNonFinitePoints(
412  bool remove_nan = true, bool remove_infinite = true) const;
413 
415  core::Device GetDevice() const override { return device_; }
416 
432  std::tuple<TriangleMesh, core::Tensor> HiddenPointRemoval(
433  const core::Tensor &camera_location, double radius) const;
434 
447  core::Tensor ClusterDBSCAN(double eps,
448  size_t min_points,
449  bool print_progress = false) const;
450 
463  std::tuple<core::Tensor, core::Tensor> SegmentPlane(
464  const double distance_threshold = 0.01,
465  const int ransac_n = 3,
466  const int num_iterations = 100,
467  const double probability = 0.99999999) const;
468 
483  TriangleMesh ComputeConvexHull(bool joggle_inputs = false) const;
484 
495  std::tuple<PointCloud, core::Tensor> ComputeBoundaryPoints(
496  double radius,
497  int max_nn = 30,
498  double angle_threshold = 90.0) const;
499 
500 public:
503 
515  void EstimateNormals(
516  const utility::optional<int> max_nn = 30,
518 
524  const core::Tensor &orientation_reference =
525  core::Tensor::Init<float>({0, 0, 1},
526  core::Device("CPU:0")));
527 
533  const core::Tensor &camera_location = core::Tensor::Zeros(
534  {3}, core::Float32, core::Device("CPU:0")));
535 
543 
558  const utility::optional<int> max_nn = 30,
560 
561 public:
587  const Image &depth,
588  const core::Tensor &intrinsics,
589  const core::Tensor &extrinsics =
591  float depth_scale = 1000.0f,
592  float depth_max = 3.0f,
593  int stride = 1,
594  bool with_normals = false);
595 
622  const RGBDImage &rgbd_image,
623  const core::Tensor &intrinsics,
624  const core::Tensor &extrinsics =
626  float depth_scale = 1000.0f,
627  float depth_max = 3.0f,
628  int stride = 1,
629  bool with_normals = false);
630 
632  static PointCloud FromLegacy(
633  const open3d::geometry::PointCloud &pcd_legacy,
634  core::Dtype dtype = core::Float32,
635  const core::Device &device = core::Device("CPU:0"));
636 
639 
642  int width,
643  int height,
644  const core::Tensor &intrinsics,
645  const core::Tensor &extrinsics =
647  float depth_scale = 1000.0f,
648  float depth_max = 3.0f);
649 
652  int width,
653  int height,
654  const core::Tensor &intrinsics,
655  const core::Tensor &extrinsics =
657  float depth_scale = 1000.0f,
658  float depth_max = 3.0f);
659 
662 
669  bool invert = false) const;
670 
679  LineSet ExtrudeRotation(double angle,
680  const core::Tensor &axis,
681  int resolution = 16,
682  double translation = 0.0,
683  bool capping = true) const;
684 
690  LineSet ExtrudeLinear(const core::Tensor &vector,
691  double scale = 1.0,
692  bool capping = true) const;
693 
694 protected:
697 };
698 
699 } // namespace geometry
700 } // namespace t
701 } // namespace open3d
math::float4 color
Definition: LineSetBuffers.cpp:64
#define LogError(...)
Definition: Logging.h:67
#define AssertTensorShape(tensor,...)
Definition: TensorCheck.h:77
size_t stride
Definition: TriangleMeshBuffers.cpp:184
bool copy
Definition: VtkUtils.cpp:89
Definition: Device.h:37
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
Definition: Device.cpp:107
Definition: Dtype.h:39
Definition: Tensor.h:51
int64_t GetLength() const
Definition: Tensor.h:1130
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:392
Device GetDevice() const override
Definition: Tensor.cpp:1384
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:404
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:155
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:53
LineSet define a sets of lines in 3D. A typical application is to display the point cloud corresponde...
Definition: LineSet.h:48
A point cloud consists of point coordinates, and optionally point colors and point normals.
Definition: PointCloud.h:55
RGBDImage is for a pair of registered color and depth images,.
Definition: RGBDImage.h:46
Mix-in class for geometry types that can be visualized.
Definition: DrawableGeometry.h:38
The base geometry class.
Definition: Geometry.h:42
A point cloud contains a list of 3D points.
Definition: PointCloud.h:99
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:404
core::Tensor & GetPointAttr(const std::string &key)
Definition: PointCloud.h:136
bool HasPointAttr(const std::string &key) const
Definition: PointCloud.h:206
PointCloud & PaintUniformColor(const core::Tensor &color)
Assigns uniform color to the point cloud.
Definition: PointCloud.cpp:508
PointCloud UniformDownSample(size_t every_k_points) const
Downsamples a point cloud by selecting every kth index point and its attributes.
Definition: PointCloud.cpp:325
PointCloud & Clear() override
Clear all data in the point cloud.
Definition: PointCloud.h:247
PointCloud & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotates the PointPositions and PointNormals (if exists).
Definition: PointCloud.cpp:222
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:1122
PointCloud Crop(const AxisAlignedBoundingBox &aabb, bool invert=false) const
Function to crop pointcloud into output pointcloud.
Definition: PointCloud.cpp:1214
TriangleMesh ComputeConvexHull(bool joggle_inputs=false) const
Definition: PointCloud.cpp:1144
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:376
std::tuple< PointCloud, core::Tensor > RemoveDuplicatedPoints() const
Remove duplicated points and there associated attributes.
Definition: PointCloud.cpp:466
const core::Tensor & GetPointPositions() const
Get the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:157
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:976
bool HasPointPositions() const
Definition: PointCloud.h:219
bool HasPointColors() const
Definition: PointCloud.h:226
PointCloud & Scale(double scale, const core::Tensor &center)
Scales the PointPositions of the PointCloud.
Definition: PointCloud.cpp:212
const core::Tensor & GetPointAttr(const std::string &key) const
Definition: PointCloud.h:152
core::Tensor & GetPointNormals()
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:147
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:569
core::Device device_
Definition: PointCloud.h:695
PointCloud & NormalizeNormals()
Normalize point normals to length 1.
Definition: PointCloud.cpp:488
PointCloud To(const core::Device &device, bool copy=false) const
Definition: PointCloud.cpp:128
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:524
core::Device GetDevice() const override
Returns the device attribute of this PointCloud.
Definition: PointCloud.h:415
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:959
void SetPointPositions(const core::Tensor &value)
Set the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:185
void SetPointColors(const core::Tensor &value)
Set the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:191
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:1110
TensorMap point_attr_
Definition: PointCloud.h:696
const core::Tensor & GetPointColors() const
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:162
AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const
Create an axis-aligned bounding box from attribute "positions".
Definition: PointCloud.cpp:1228
bool IsEmpty() const override
Returns !HasPointPositions().
Definition: PointCloud.h:253
PointCloud VoxelDownSample(double voxel_size, const core::HashBackendType &backend=core::HashBackendType::Default) const
Downsamples a point cloud with a specified voxel size.
Definition: PointCloud.cpp:296
open3d::geometry::PointCloud ToLegacy() const
Convert to a legacy Open3D PointCloud.
Definition: PointCloud.cpp:1031
core::Tensor & GetPointColors()
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:144
PointCloud Append(const PointCloud &other) const
Definition: PointCloud.cpp:141
PointCloud FarthestPointDownSample(size_t num_samples) const
Downsample a pointcloud into output pointcloud with a set of points has farthest distance.
Definition: PointCloud.cpp:368
const core::Tensor & GetPointNormals() const
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:167
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:663
core::Tensor GetMinBound() const
Returns the min bound for point coordinates.
Definition: PointCloud.cpp:116
PointCloud RandomDownSample(double sampling_ratio) const
Downsample a pointcloud by selecting random index point and its attributes.
Definition: PointCloud.cpp:343
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:932
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:902
virtual ~PointCloud() override
Definition: PointCloud.h:122
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:727
PointCloud Clone() const
Returns copy of the point cloud on the same device.
Definition: PointCloud.cpp:139
void RemovePointAttr(const std::string &key)
Definition: PointCloud.h:215
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:442
core::Tensor & GetPointPositions()
Get the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:141
PointCloud & Transform(const core::Tensor &transformation)
Transforms the PointPositions and PointNormals (if exist) of the PointCloud.
Definition: PointCloud.cpp:187
std::string ToString() const
Text description.
Definition: PointCloud.cpp:90
PointCloud(const core::Device &device=core::Device("CPU:0"))
Definition: PointCloud.cpp:65
core::Tensor GetCenter() const
Returns the center for point coordinates.
Definition: PointCloud.cpp:124
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:1008
LineSet ExtrudeLinear(const core::Tensor &vector, double scale=1.0, bool capping=true) const
Definition: PointCloud.cpp:1242
PointCloud operator+(const PointCloud &other) const
Definition: PointCloud.h:273
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
Definition: PointCloud.h:128
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:1084
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:690
TensorMap & GetPointAttr()
Getter for point_attr_ TensorMap.
Definition: PointCloud.h:131
core::Tensor GetMaxBound() const
Returns the max bound for point coordinates.
Definition: PointCloud.cpp:120
LineSet ExtrudeRotation(double angle, const core::Tensor &axis, int resolution=16, double translation=0.0, bool capping=true) const
Definition: PointCloud.cpp:1232
void OrientNormalsConsistentTangentPlane(size_t k)
Function to consistently orient estimated normals based on consistent tangent planes as described in ...
Definition: PointCloud.cpp:716
void SetPointAttr(const std::string &key, const core::Tensor &value)
Definition: PointCloud.h:176
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:261
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:235
void SetPointNormals(const core::Tensor &value)
Set the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:197
bool HasPointNormals() const
Definition: PointCloud.h:233
PointCloud & Translate(const core::Tensor &translation, bool relative=true)
Translates the PointPositions of the PointCloud.
Definition: PointCloud.cpp:198
Definition: TensorMap.h:50
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:111
bool Contains(const std::string &key) const
Definition: TensorMap.h:206
A triangle mesh contains vertices and triangles.
Definition: TriangleMesh.h:111
Definition: Optional.h:278
int width
Definition: FilePCD.cpp:71
int height
Definition: FilePCD.cpp:72
int points
Definition: FilePCD.cpp:73
HashBackendType
Definition: HashMap.h:39
const Dtype Float32
Definition: Dtype.cpp:61
constexpr nullopt_t nullopt
Definition: Optional.h:171
Definition: PinholeCameraIntrinsic.cpp:35