Open3D (C++ API)  0.11.0
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 <Eigen/Core>
30 #include <string>
31 #include <unordered_map>
32 #include <unordered_set>
33 
34 #include "open3d/core/Tensor.h"
35 #include "open3d/core/TensorList.h"
39 
40 namespace open3d {
41 namespace t {
42 namespace geometry {
43 
93 class PointCloud : public Geometry {
94 public:
97  const core::Device &device = core::Device("CPU:0"));
98 
109 
115  PointCloud(const std::unordered_map<std::string, core::TensorList>
116  &map_keys_to_tensorlists);
117 
118  virtual ~PointCloud() override {}
119 
121  const TensorListMap &GetPointAttrPybind() const { return point_attr_; }
122 
124  void SetPointAttrPybind(const TensorListMap &point_attr) {
125  point_attr_ = point_attr;
126  };
127 
131  core::TensorList &GetPointAttr(const std::string &key) {
132  return point_attr_.at(key);
133  }
134 
136  core::TensorList &GetPoints() { return GetPointAttr("points"); }
137 
140 
143 
147  const core::TensorList &GetPointAttr(const std::string &key) const {
148  return point_attr_.at(key);
149  }
150 
152  const core::TensorList &GetPoints() const { return GetPointAttr("points"); }
153 
156  return GetPointAttr("colors");
157  }
158 
161  return GetPointAttr("normals");
162  }
163 
169  void SetPointAttr(const std::string &key, const core::TensorList &value) {
170  value.AssertDevice(device_);
171  point_attr_[key] = value;
172  }
173 
175  void SetPoints(const core::TensorList &value) {
176  value.AssertElementShape({3});
177  SetPointAttr("points", value);
178  }
179 
181  void SetPointColors(const core::TensorList &value) {
182  value.AssertElementShape({3});
183  SetPointAttr("colors", value);
184  }
185 
187  void SetPointNormals(const core::TensorList &value) {
188  value.AssertElementShape({3});
189  SetPointAttr("normals", value);
190  }
191 
196  bool HasPointAttr(const std::string &key) const {
197  return point_attr_.Contains(key) && GetPointAttr(key).GetSize() > 0 &&
198  GetPointAttr(key).GetSize() == GetPoints().GetSize();
199  }
200 
203  bool HasPoints() const { return HasPointAttr("points"); }
204 
210  bool HasPointColors() const { return HasPointAttr("colors"); }
211 
217  bool HasPointNormals() const { return HasPointAttr("normals"); }
218 
226  const std::unordered_map<std::string, core::Tensor>
227  &map_keys_to_tensors) {
228  point_attr_.SynchronizedPushBack(map_keys_to_tensors);
229  }
230 
231 public:
233  PointCloud &Clear() override {
234  point_attr_.clear();
235  return *this;
236  }
237 
239  bool IsEmpty() const override { return !HasPoints(); }
240 
242  core::Tensor GetMinBound() const;
243 
245  core::Tensor GetMaxBound() const;
246 
248  core::Tensor GetCenter() const;
249 
251  PointCloud &Transform(const core::Tensor &transformation);
252 
254  PointCloud &Translate(const core::Tensor &translation,
255  bool relative = true);
256 
258  PointCloud &Scale(double scale, const core::Tensor &center);
259 
261  PointCloud &Rotate(const core::Tensor &R, const core::Tensor &center);
262 
263  core::Device GetDevice() const { return device_; }
264 
267  const open3d::geometry::PointCloud &pcd_legacy,
269  const core::Device &device = core::Device("CPU:0"));
270 
273 
274 protected:
277 };
278 
279 } // namespace geometry
280 } // namespace t
281 } // namespace open3d
void SynchronizedPushBack(const std::unordered_map< std::string, core::Tensor > &map_keys_to_tensors)
Definition: TensorListMap.cpp:89
PointCloud & Transform(const core::Tensor &transformation)
Transforms the points and normals (if exist).
Definition: PointCloud.cpp:75
PointCloud & Translate(const core::Tensor &translation, bool relative=true)
Translates points.
Definition: PointCloud.cpp:80
bool IsEmpty() const override
Returns !HasPoints().
Definition: PointCloud.h:239
const core::TensorList & GetPointNormals() const
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:160
core::TensorList & GetPointAttr(const std::string &key)
Definition: PointCloud.h:131
static geometry::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:104
PointCloud(core::Dtype dtype=core::Dtype::Float32, const core::Device &device=core::Device("CPU:0"))
Construct an empty pointcloud.
Definition: PointCloud.cpp:42
int64_t GetSize() const
Definition: TensorList.h:269
core::TensorList & GetPointColors()
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:139
bool HasPointColors() const
Definition: PointCloud.h:210
core::Device GetDevice() const
Definition: PointCloud.h:263
Definition: Dtype.h:39
PointCloud & Scale(double scale, const core::Tensor &center)
Scale points.
Definition: PointCloud.cpp:91
void SetPoints(const core::TensorList &value)
Set the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:175
A point cloud consists of point coordinates, and optionally point colors and point normals...
Definition: PointCloud.h:54
void SetPointColors(const core::TensorList &value)
Set the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:181
Definition: TensorListMap.h:44
Definition: TensorList.h:58
bool HasPointAttr(const std::string &key) const
Definition: PointCloud.h:196
const TensorListMap & GetPointAttrPybind() const
Getter for point_attr_ TensorListMap. Used in Pybind.
Definition: PointCloud.h:121
core::TensorList & GetPoints()
Get the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:136
core::Device device_
Definition: PointCloud.h:275
void SynchronizedPushBack(const std::unordered_map< std::string, core::Tensor > &map_keys_to_tensors)
Definition: PointCloud.h:225
void SetPointNormals(const core::TensorList &value)
Set the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:187
Definition: Device.h:39
A pointcloud contains a set of 3D points.
Definition: PointCloud.h:93
virtual ~PointCloud() override
Definition: PointCloud.h:118
const core::TensorList & GetPoints() const
Get the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:152
core::TensorList & GetPointNormals()
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:142
void SetPointAttrPybind(const TensorListMap &point_attr)
Setter for point_attr_ TensorListMap. Used in Pybind.
Definition: PointCloud.h:124
void AssertDevice(const Device &expected_device) const
Definition: TensorList.h:257
open3d::geometry::PointCloud ToLegacyPointCloud() const
Convert to a legacy Open3D PointCloud.
Definition: PointCloud.cpp:130
core::Tensor GetMaxBound() const
Returns the max bound for point coordinates.
Definition: PointCloud.cpp:67
static const Dtype Float32
Definition: Dtype.h:42
PointCloud & Clear() override
Clear all data in the pointcloud.
Definition: PointCloud.h:233
void AssertElementShape(const SizeVector &expected_element_shape) const
Definition: TensorList.h:248
const core::TensorList & GetPointAttr(const std::string &key) const
Definition: PointCloud.h:147
void SetPointAttr(const std::string &key, const core::TensorList &value)
Definition: PointCloud.h:169
The base geometry class.
Definition: Geometry.h:36
bool HasPoints() const
Definition: PointCloud.h:203
TensorListMap point_attr_
Definition: PointCloud.h:276
int points
Definition: FilePCD.cpp:73
core::Tensor GetMinBound() const
Returns the min bound for point coordinates.
Definition: PointCloud.cpp:63
Definition: PinholeCameraIntrinsic.cpp:35
bool HasPointNormals() const
Definition: PointCloud.h:217
Definition: Tensor.h:48
PointCloud & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotate points and normals (if exist).
Definition: PointCloud.cpp:98
bool Contains(const std::string &key) const
Definition: TensorListMap.h:97
core::Tensor GetCenter() const
Returns the center for point coordinates.
Definition: PointCloud.cpp:71
const core::TensorList & GetPointColors() const
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:155