Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 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"
40 #include "open3d/utility/Console.h"
41 
42 namespace open3d {
43 namespace t {
44 namespace geometry {
45 
95 class PointCloud : public Geometry {
96 public:
98  PointCloud(const core::Device &device = core::Device("CPU:0"));
99 
110 
116  PointCloud(const std::unordered_map<std::string, core::Tensor>
117  &map_keys_to_tensors);
118 
119  virtual ~PointCloud() override {}
120 
122  std::string ToString() const;
123 
125  const TensorMap &GetPointAttr() const { return point_attr_; }
126 
130  core::Tensor &GetPointAttr(const std::string &key) {
131  return point_attr_.at(key);
132  }
133 
135  core::Tensor &GetPoints() { return GetPointAttr("points"); }
136 
138  core::Tensor &GetPointColors() { return GetPointAttr("colors"); }
139 
141  core::Tensor &GetPointNormals() { return GetPointAttr("normals"); }
142 
146  const core::Tensor &GetPointAttr(const std::string &key) const {
147  return point_attr_.at(key);
148  }
149 
151  const core::Tensor &GetPoints() const { return GetPointAttr("points"); }
152 
154  const core::Tensor &GetPointColors() const {
155  return GetPointAttr("colors");
156  }
157 
159  const core::Tensor &GetPointNormals() const {
160  return GetPointAttr("normals");
161  }
162 
168  void SetPointAttr(const std::string &key, const core::Tensor &value) {
169  if (value.GetDevice() != device_) {
170  utility::LogError("Attribute device {} != Pointcloud's device {}.",
171  value.GetDevice().ToString(), device_.ToString());
172  }
173  point_attr_[key] = value;
174  }
175 
177  void SetPoints(const core::Tensor &value) {
179  SetPointAttr("points", value);
180  }
181 
183  void SetPointColors(const core::Tensor &value) {
185  SetPointAttr("colors", value);
186  }
187 
189  void SetPointNormals(const core::Tensor &value) {
191  SetPointAttr("normals", value);
192  }
193 
198  bool HasPointAttr(const std::string &key) const {
199  return point_attr_.Contains(key) && GetPointAttr(key).GetLength() > 0 &&
200  GetPointAttr(key).GetLength() == GetPoints().GetLength();
201  }
202 
207  void RemovePointAttr(const std::string &key) { point_attr_.Erase(key); }
208 
211  bool HasPoints() const { return HasPointAttr("points"); }
212 
218  bool HasPointColors() const { return HasPointAttr("colors"); }
219 
225  bool HasPointNormals() const { return HasPointAttr("normals"); }
226 
227 public:
233  PointCloud To(const core::Device &device, bool copy = false) const;
234 
236  PointCloud Clone() const;
237 
241  PointCloud CPU() const { return To(core::Device("CPU:0")); };
242 
247  PointCloud CUDA(int device_id = 0) const {
248  return To(core::Device(core::Device::DeviceType::CUDA, device_id));
249  };
250 
252  PointCloud &Clear() override {
253  point_attr_.clear();
254  return *this;
255  }
256 
258  bool IsEmpty() const override { return !HasPoints(); }
259 
261  core::Tensor GetMinBound() const;
262 
264  core::Tensor GetMaxBound() const;
265 
267  core::Tensor GetCenter() const;
268 
274  PointCloud Append(const PointCloud &other) const;
275 
278  PointCloud operator+(const PointCloud &other) const {
279  return Append(other);
280  }
281 
293  PointCloud &Transform(const core::Tensor &transformation);
294 
300  PointCloud &Translate(const core::Tensor &translation,
301  bool relative = true);
302 
308  PointCloud &Scale(double scale, const core::Tensor &center);
309 
316  PointCloud &Rotate(const core::Tensor &R, const core::Tensor &center);
317 
320  PointCloud VoxelDownSample(double voxel_size,
321  const core::HashmapBackend &backend =
323 
325  core::Device GetDevice() const { return device_; }
326 
352  const Image &depth,
353  const core::Tensor &intrinsics,
354  const core::Tensor &extrinsics = core::Tensor::Eye(
355  4, core::Dtype::Float32, core::Device("CPU:0")),
356  float depth_scale = 1000.0f,
357  float depth_max = 3.0f,
358  int stride = 1,
359  bool with_normals = false);
360 
387  const RGBDImage &rgbd_image,
388  const core::Tensor &intrinsics,
389  const core::Tensor &extrinsics = core::Tensor::Eye(
390  4, core::Dtype::Float32, core::Device("CPU:0")),
391  float depth_scale = 1000.0f,
392  float depth_max = 3.0f,
393  int stride = 1,
394  bool with_normals = false);
395 
398  const open3d::geometry::PointCloud &pcd_legacy,
400  const core::Device &device = core::Device("CPU:0"));
401 
404 
407  int width,
408  int height,
409  const core::Tensor &intrinsics,
410  const core::Tensor &extrinsics = core::Tensor::Eye(
411  4, core::Dtype::Float32, core::Device("CPU:0")),
412  float depth_scale = 1000.0f,
413  float depth_max = 3.0f);
414 
417  int width,
418  int height,
419  const core::Tensor &intrinsics,
420  const core::Tensor &extrinsics = core::Tensor::Eye(
421  4, core::Dtype::Float32, core::Device("CPU:0")),
422  float depth_scale = 1000.0f,
423  float depth_max = 3.0f);
424 
425 protected:
428 };
429 
430 } // namespace geometry
431 } // namespace t
432 } // namespace open3d
PointCloud & Transform(const core::Tensor &transformation)
Transforms the points and normals (if exist) of the PointCloud. Extracts R, t from Transformation T (...
Definition: PointCloud.cpp:156
PointCloud & Translate(const core::Tensor &translation, bool relative=true)
Translates the points of the PointCloud.
Definition: PointCloud.cpp:183
void SetPointAttr(const std::string &key, const core::Tensor &value)
Definition: PointCloud.h:168
PointCloud(const core::Device &device=core::Device("CPU:0"))
Construct an empty pointcloud.
Definition: PointCloud.cpp:46
void AssertShapeCompatible(const DynamicSizeVector &expected_shape, const std::string &error_msg="") const
Assert that Tensor&#39;s shape is compatible with a dynamic shape.
Definition: Tensor.cpp:1513
bool IsEmpty() const override
Returns !HasPoints().
Definition: PointCloud.h:258
constexpr nullopt_t nullopt
Definition: Optional.h:146
void SetPoints(const core::Tensor &value)
Set the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:177
TensorMap point_attr_
Definition: PointCloud.h:427
const core::Tensor & GetPointNormals() const
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:159
HashmapBackend
Definition: Hashmap.h:38
bool HasPointColors() const
Definition: PointCloud.h:218
core::Device GetDevice() const
Returns the device attribute of this PointCloud.
Definition: PointCloud.h:325
Definition: Dtype.h:39
PointCloud & Scale(double scale, const core::Tensor &center)
Scales the points of the PointCloud.
Definition: PointCloud.cpp:196
static PointCloud CreateFromRGBDImage(const RGBDImage &rgbd_image, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Dtype::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 pointcloud from an RGB-D image and a camera model.
Definition: PointCloud.cpp:365
const core::Tensor & GetPointAttr(const std::string &key) const
Definition: PointCloud.h:146
PointCloud CUDA(int device_id=0) const
Definition: PointCloud.h:247
A point cloud consists of point coordinates, and optionally point colors and point normals...
Definition: PointCloud.h:54
void RemovePointAttr(const std::string &key)
Definition: PointCloud.h:207
core::Tensor & GetPoints()
Get the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:135
bool HasPointAttr(const std::string &key) const
Definition: PointCloud.h:198
Device GetDevice() const
Definition: Tensor.cpp:1098
core::Tensor & GetPointColors()
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:138
#define LogError(...)
Definition: Console.h:79
void SetPointNormals(const core::Tensor &value)
Set the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:189
std::string ToString() const
Text description.
Definition: PointCloud.cpp:71
void SetPointColors(const core::Tensor &value)
Set the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:183
core::Device device_
Definition: PointCloud.h:426
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
geometry::RGBDImage ProjectToRGBDImage(int width, int height, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Dtype::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:410
const core::Tensor & GetPointColors() const
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:154
bool Contains(const std::string &key) const
Definition: TensorMap.h:125
const core::Tensor & GetPoints() const
Get the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:151
PointCloud VoxelDownSample(double voxel_size, const core::HashmapBackend &backend=core::HashmapBackend::Default) const
Downsamples a point cloud with a specified voxel size.
Definition: PointCloud.cpp:223
Definition: Device.h:39
PointCloud operator+(const PointCloud &other) const
Definition: PointCloud.h:278
A pointcloud contains a set of 3D points.
Definition: PointCloud.h:95
virtual ~PointCloud() override
Definition: PointCloud.h:119
static PointCloud CreateFromDepthImage(const Image &depth, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Dtype::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 pointcloud from a depth image and a camera model.
Definition: PointCloud.cpp:337
size_t stride
Definition: TriangleMeshBuffers.cpp:183
open3d::geometry::PointCloud ToLegacyPointCloud() const
Convert to a legacy Open3D PointCloud.
Definition: PointCloud.cpp:455
core::Tensor GetMaxBound() const
Returns the max bound for point coordinates.
Definition: PointCloud.cpp:92
static const Dtype Float32
Definition: Dtype.h:42
static PointCloud FromLegacyPointCloud(const open3d::geometry::PointCloud &pcd_legacy, core::Dtype dtype=core::Dtype::Float32, const core::Device &device=core::Device("CPU:0"))
Create a PointCloud from a legacy Open3D PointCloud.
Definition: PointCloud.cpp:433
PointCloud & Clear() override
Clear all data in the pointcloud.
Definition: PointCloud.h:252
The base geometry class.
Definition: Geometry.h:38
bool HasPoints() const
Definition: PointCloud.h:211
int points
Definition: FilePCD.cpp:73
PointCloud CPU() const
Definition: PointCloud.h:241
core::Tensor & GetPointNormals()
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:141
core::Tensor GetMinBound() const
Returns the min bound for point coordinates.
Definition: PointCloud.cpp:90
Definition: PinholeCameraIntrinsic.cpp:35
bool HasPointNormals() const
Definition: PointCloud.h:225
Definition: Tensor.h:50
PointCloud & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotates the points and normals (if exists).
Definition: PointCloud.cpp:205
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
Definition: PointCloud.h:125
RGBDImage A pair of color and depth images.
Definition: RGBDImage.h:40
int height
Definition: FilePCD.cpp:72
PointCloud Append(const PointCloud &other) const
Definition: PointCloud.cpp:109
geometry::Image ProjectToDepthImage(int width, int height, const core::Tensor &intrinsics, const core::Tensor &extrinsics=core::Tensor::Eye(4, core::Dtype::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:396
PointCloud Clone() const
Returns copy of the point cloud on the same device.
Definition: PointCloud.cpp:107
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:252
PointCloud To(const core::Device &device, bool copy=false) const
Definition: PointCloud.cpp:96
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:100
core::Tensor & GetPointAttr(const std::string &key)
Definition: PointCloud.h:130
core::Tensor GetCenter() const
Returns the center for point coordinates.
Definition: PointCloud.cpp:94
int64_t GetLength() const
Definition: Tensor.h:986
std::string ToString() const
Definition: Device.h:73
Definition: TensorMap.h:49
int width
Definition: FilePCD.cpp:71