Open3D (C++ API)  0.15.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"
43
44namespace open3d {
45namespace t {
46namespace geometry {
47
95class PointCloud : public Geometry, public DrawableGeometry {
96public:
100 PointCloud(const core::Device &device = core::Device("CPU:0"));
101
109
115 PointCloud(const std::unordered_map<std::string, core::Tensor>
116 &map_keys_to_tensors);
117
118 virtual ~PointCloud() override {}
119
121 std::string ToString() const;
122
124 const TensorMap &GetPointAttr() const { return point_attr_; }
125
129 core::Tensor &GetPointAttr(const std::string &key) {
130 return point_attr_.at(key);
131 }
132
134 core::Tensor &GetPointPositions() { return GetPointAttr("positions"); }
135
137 core::Tensor &GetPointColors() { return GetPointAttr("colors"); }
138
140 core::Tensor &GetPointNormals() { return GetPointAttr("normals"); }
141
145 const core::Tensor &GetPointAttr(const std::string &key) const {
146 return point_attr_.at(key);
147 }
148
151 return GetPointAttr("positions");
152 }
153
156 return GetPointAttr("colors");
157 }
158
161 return GetPointAttr("normals");
162 }
163
169 void SetPointAttr(const std::string &key, const core::Tensor &value) {
170 if (value.GetDevice() != device_) {
171 utility::LogError("Attribute device {} != Pointcloud's device {}.",
172 value.GetDevice().ToString(), device_.ToString());
173 }
174 point_attr_[key] = value;
175 }
176
178 void SetPointPositions(const core::Tensor &value) {
180 SetPointAttr("positions", value);
181 }
182
184 void SetPointColors(const core::Tensor &value) {
186 SetPointAttr("colors", value);
187 }
188
190 void SetPointNormals(const core::Tensor &value) {
192 SetPointAttr("normals", value);
193 }
194
199 bool HasPointAttr(const std::string &key) const {
200 return point_attr_.Contains(key) && GetPointAttr(key).GetLength() > 0 &&
201 GetPointAttr(key).GetLength() == GetPointPositions().GetLength();
202 }
203
208 void RemovePointAttr(const std::string &key) { point_attr_.Erase(key); }
209
212 bool HasPointPositions() const { return HasPointAttr("positions"); }
213
219 bool HasPointColors() const { return HasPointAttr("colors"); }
220
226 bool HasPointNormals() const { return HasPointAttr("normals"); }
227
228public:
234 PointCloud To(const core::Device &device, bool copy = false) const;
235
237 PointCloud Clone() const;
238
240 PointCloud &Clear() override {
241 point_attr_.clear();
242 return *this;
243 }
244
246 bool IsEmpty() const override { return !HasPointPositions(); }
247
250
253
255 core::Tensor GetCenter() const;
256
262 PointCloud Append(const PointCloud &other) const;
263
266 PointCloud operator+(const PointCloud &other) const {
267 return Append(other);
268 }
269
289 PointCloud &Transform(const core::Tensor &transformation);
290
296 PointCloud &Translate(const core::Tensor &translation,
297 bool relative = true);
298
304 PointCloud &Scale(double scale, const core::Tensor &center);
305
312 PointCloud &Rotate(const core::Tensor &R, const core::Tensor &center);
313
316 PointCloud VoxelDownSample(double voxel_size,
317 const core::HashBackendType &backend =
319
321 core::Device GetDevice() const { return device_; }
322
323public:
331 void EstimateNormals(
332 const int max_nn = 30,
334
343 const int max_nn = 30,
345
346public:
372 const Image &depth,
373 const core::Tensor &intrinsics,
374 const core::Tensor &extrinsics =
376 float depth_scale = 1000.0f,
377 float depth_max = 3.0f,
378 int stride = 1,
379 bool with_normals = false);
380
407 const RGBDImage &rgbd_image,
408 const core::Tensor &intrinsics,
409 const core::Tensor &extrinsics =
411 float depth_scale = 1000.0f,
412 float depth_max = 3.0f,
413 int stride = 1,
414 bool with_normals = false);
415
417 static PointCloud FromLegacy(
418 const open3d::geometry::PointCloud &pcd_legacy,
420 const core::Device &device = core::Device("CPU:0"));
421
424
427 int width,
428 int height,
429 const core::Tensor &intrinsics,
430 const core::Tensor &extrinsics =
432 float depth_scale = 1000.0f,
433 float depth_max = 3.0f);
434
437 int width,
438 int height,
439 const core::Tensor &intrinsics,
440 const core::Tensor &extrinsics =
442 float depth_scale = 1000.0f,
443 float depth_max = 3.0f);
444
445protected:
448};
449
450} // namespace geometry
451} // namespace t
452} // namespace open3d
#define LogError(...)
Definition: Logging.h:67
#define AssertTensorShape(tensor,...)
Definition: TensorCheck.h:77
size_t stride
Definition: TriangleMeshBuffers.cpp:184
Definition: Device.h:39
std::string ToString() const
Definition: Device.h:75
Definition: Dtype.h:39
Definition: Tensor.h:51
int64_t GetLength() const
Definition: Tensor.h:1089
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create an identity matrix of size n x n.
Definition: Tensor.cpp:392
Device GetDevice() const
Definition: Tensor.cpp:1365
A point cloud consists of point coordinates, and optionally point colors and point normals.
Definition: PointCloud.h:55
Mix-in class for geometry types that can be visualized.
Definition: DrawableGeometry.h:38
The base geometry class.
Definition: Geometry.h:38
The Image class stores image with customizable rows, cols, channels, dtype and device.
Definition: Image.h:48
A point cloud contains a list of 3D points.
Definition: PointCloud.h:95
bool HasPointAttr(const std::string &key) const
Definition: PointCloud.h:199
PointCloud & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotates the PointPositions and PointNormals (if exists).
Definition: PointCloud.cpp:202
PointCloud & Clear() override
Clear all data in the point cloud.
Definition: PointCloud.h:240
const core::Tensor & GetPointNormals() const
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:160
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:552
core::Tensor & GetPointNormals()
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:140
bool HasPointPositions() const
Definition: PointCloud.h:212
bool HasPointColors() const
Definition: PointCloud.h:219
PointCloud & Scale(double scale, const core::Tensor &center)
Scales the PointPositions of the PointCloud.
Definition: PointCloud.cpp:192
core::Device device_
Definition: PointCloud.h:446
core::Device GetDevice() const
Returns the device attribute of this PointCloud.
Definition: PointCloud.h:321
PointCloud To(const core::Device &device, bool copy=false) const
Definition: PointCloud.cpp:108
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:535
core::Tensor & GetPointColors()
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:137
void SetPointPositions(const core::Tensor &value)
Set the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:178
void SetPointColors(const core::Tensor &value)
Set the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:184
TensorMap point_attr_
Definition: PointCloud.h:447
void EstimateColorGradients(const 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:320
bool IsEmpty() const override
Returns !HasPointPositions().
Definition: PointCloud.h:246
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:215
const core::Tensor & GetPointPositions() const
Get the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:150
open3d::geometry::PointCloud ToLegacy() const
Convert to a legacy Open3D PointCloud.
Definition: PointCloud.cpp:607
const core::Tensor & GetPointAttr(const std::string &key) const
Definition: PointCloud.h:145
PointCloud Append(const PointCloud &other) const
Definition: PointCloud.cpp:121
core::Tensor & GetPointAttr(const std::string &key)
Definition: PointCloud.h:129
core::Tensor GetMinBound() const
Returns the min bound for point coordinates.
Definition: PointCloud.cpp:96
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:508
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:478
void EstimateNormals(const 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:244
virtual ~PointCloud() override
Definition: PointCloud.h:118
PointCloud Clone() const
Returns copy of the point cloud on the same device.
Definition: PointCloud.cpp:119
void RemovePointAttr(const std::string &key)
Definition: PointCloud.h:208
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
Definition: PointCloud.h:124
PointCloud & Transform(const core::Tensor &transformation)
Transforms the PointPositions and PointNormals (if exist) of the PointCloud.
Definition: PointCloud.cpp:167
std::string ToString() const
Text description.
Definition: PointCloud.cpp:75
PointCloud(const core::Device &device=core::Device("CPU:0"))
Definition: PointCloud.cpp:50
core::Tensor GetCenter() const
Returns the center for point coordinates.
Definition: PointCloud.cpp:104
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:584
PointCloud operator+(const PointCloud &other) const
Definition: PointCloud.h:266
const core::Tensor & GetPointColors() const
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:155
core::Tensor GetMaxBound() const
Returns the max bound for point coordinates.
Definition: PointCloud.cpp:100
core::Tensor & GetPointPositions()
Get the value of the "positions" attribute. Convenience function.
Definition: PointCloud.h:134
void SetPointAttr(const std::string &key, const core::Tensor &value)
Definition: PointCloud.h:169
void SetPointNormals(const core::Tensor &value)
Set the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:190
bool HasPointNormals() const
Definition: PointCloud.h:226
PointCloud & Translate(const core::Tensor &translation, bool relative=true)
Translates the PointPositions of the PointCloud.
Definition: PointCloud.cpp:178
RGBDImage A pair of color and depth images.
Definition: RGBDImage.h:40
Definition: TensorMap.h:49
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
bool Contains(const std::string &key) const
Definition: TensorMap.h:134
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:38
const Dtype Float32
Definition: Dtype.cpp:61
constexpr nullopt_t nullopt
Definition: Optional.h:171
Definition: PinholeCameraIntrinsic.cpp:35