Open3D (C++ API)  0.12.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"
39 #include "open3d/utility/Console.h"
40 
41 namespace open3d {
42 namespace t {
43 namespace geometry {
44 
94 class PointCloud : public Geometry {
95 public:
97  PointCloud(const core::Device &device = core::Device("CPU:0"));
98 
109 
115  PointCloud(const std::unordered_map<std::string, core::Tensor>
116  &map_keys_to_tensors);
117 
118  virtual ~PointCloud() override {}
119 
121  const TensorMap &GetPointAttr() const { return point_attr_; }
122 
126  core::Tensor &GetPointAttr(const std::string &key) {
127  return point_attr_.at(key);
128  }
129 
131  core::Tensor &GetPoints() { return GetPointAttr("points"); }
132 
134  core::Tensor &GetPointColors() { return GetPointAttr("colors"); }
135 
137  core::Tensor &GetPointNormals() { return GetPointAttr("normals"); }
138 
142  const core::Tensor &GetPointAttr(const std::string &key) const {
143  return point_attr_.at(key);
144  }
145 
147  const core::Tensor &GetPoints() const { return GetPointAttr("points"); }
148 
150  const core::Tensor &GetPointColors() const {
151  return GetPointAttr("colors");
152  }
153 
155  const core::Tensor &GetPointNormals() const {
156  return GetPointAttr("normals");
157  }
158 
164  void SetPointAttr(const std::string &key, const core::Tensor &value) {
165  if (value.GetDevice() != device_) {
166  utility::LogError("Attribute device {} != Pointcloud's device {}.",
167  value.GetDevice().ToString(), device_.ToString());
168  }
169  point_attr_[key] = value;
170  }
171 
173  void SetPoints(const core::Tensor &value) {
175  SetPointAttr("points", value);
176  }
177 
179  void SetPointColors(const core::Tensor &value) {
181  SetPointAttr("colors", value);
182  }
183 
185  void SetPointNormals(const core::Tensor &value) {
187  SetPointAttr("normals", value);
188  }
189 
194  bool HasPointAttr(const std::string &key) const {
195  return point_attr_.Contains(key) && GetPointAttr(key).GetLength() > 0 &&
196  GetPointAttr(key).GetLength() == GetPoints().GetLength();
197  }
198 
201  bool HasPoints() const { return HasPointAttr("points"); }
202 
208  bool HasPointColors() const { return HasPointAttr("colors"); }
209 
215  bool HasPointNormals() const { return HasPointAttr("normals"); }
216 
217 public:
219  PointCloud &Clear() override {
220  point_attr_.clear();
221  return *this;
222  }
223 
225  bool IsEmpty() const override { return !HasPoints(); }
226 
228  core::Tensor GetMinBound() const;
229 
231  core::Tensor GetMaxBound() const;
232 
234  core::Tensor GetCenter() const;
235 
237  PointCloud Copy(const core::Device device) const;
238 
240  PointCloud Copy() const;
241 
253  PointCloud &Transform(const core::Tensor &transformation);
254 
260  PointCloud &Translate(const core::Tensor &translation,
261  bool relative = true);
262 
268  PointCloud &Scale(double scale, const core::Tensor &center);
269 
276  PointCloud &Rotate(const core::Tensor &R, const core::Tensor &center);
277 
279  core::Device GetDevice() const { return device_; }
280 
301  const Image &depth,
302  const core::Tensor &intrinsics,
303  const core::Tensor &extrinsics = core::Tensor::Eye(
304  4, core::Dtype::Float32, core::Device("CPU:0")),
305  double depth_scale = 1000.0,
306  double depth_max = 3.0,
307  int stride = 1);
308 
311  const open3d::geometry::PointCloud &pcd_legacy,
313  const core::Device &device = core::Device("CPU:0"));
314 
317 
318 protected:
321 };
322 
323 } // namespace geometry
324 } // namespace t
325 } // 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:86
PointCloud & Translate(const core::Tensor &translation, bool relative=true)
Translates the points of the PointCloud.
Definition: PointCloud.cpp:108
void SetPointAttr(const std::string &key, const core::Tensor &value)
Definition: PointCloud.h:164
PointCloud(const core::Device &device=core::Device("CPU:0"))
Construct an empty pointcloud.
Definition: PointCloud.cpp:45
bool IsEmpty() const override
Returns !HasPoints().
Definition: PointCloud.h:225
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:173
TensorMap point_attr_
Definition: PointCloud.h:320
const core::Tensor & GetPointNormals() const
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:155
bool HasPointColors() const
Definition: PointCloud.h:208
core::Device GetDevice() const
Returns the device attribute of this PointCloud.
Definition: PointCloud.h:279
Definition: Dtype.h:39
PointCloud & Scale(double scale, const core::Tensor &center)
Scales the points of the PointCloud.
Definition: PointCloud.cpp:121
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
const core::Tensor & GetPointAttr(const std::string &key) const
Definition: PointCloud.h:142
A point cloud consists of point coordinates, and optionally point colors and point normals...
Definition: PointCloud.h:54
core::Tensor & GetPoints()
Get the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:131
bool HasPointAttr(const std::string &key) const
Definition: PointCloud.h:194
Device GetDevice() const
Definition: Tensor.cpp:955
core::Tensor & GetPointColors()
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:134
void SetPointNormals(const core::Tensor &value)
Set the value of the "normals" 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:179
core::Device device_
Definition: PointCloud.h:319
The Image class stores image with customizable rols, cols, channels, dtype and device.
Definition: Image.h:45
const core::Tensor & GetPointColors() const
Get the value of the "colors" attribute. Convenience function.
Definition: PointCloud.h:150
bool Contains(const std::string &key) const
Definition: TensorMap.h:110
const core::Tensor & GetPoints() const
Get the value of the "points" attribute. Convenience function.
Definition: PointCloud.h:147
Definition: Device.h:39
A pointcloud contains a set of 3D points.
Definition: PointCloud.h:94
virtual ~PointCloud() override
Definition: PointCloud.h:118
size_t stride
Definition: TriangleMeshBuffers.cpp:183
open3d::geometry::PointCloud ToLegacyPointCloud() const
Convert to a legacy Open3D PointCloud.
Definition: PointCloud.cpp:203
core::Tensor GetMaxBound() const
Returns the max bound for point coordinates.
Definition: PointCloud.cpp:72
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:181
PointCloud & Clear() override
Clear all data in the pointcloud.
Definition: PointCloud.h:219
The base geometry class.
Definition: Geometry.h:38
bool HasPoints() const
Definition: PointCloud.h:201
int points
Definition: FilePCD.cpp:73
core::Tensor & GetPointNormals()
Get the value of the "normals" attribute. Convenience function.
Definition: PointCloud.h:137
core::Tensor GetMinBound() const
Returns the min bound for point coordinates.
Definition: PointCloud.cpp:70
Definition: PinholeCameraIntrinsic.cpp:35
bool HasPointNormals() const
Definition: PointCloud.h:215
Definition: Tensor.h:48
PointCloud & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotates the points and normals (if exists).
Definition: PointCloud.cpp:130
const TensorMap & GetPointAttr() const
Getter for point_attr_ TensorMap. Used in Pybind.
Definition: PointCloud.h:121
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")), double depth_scale=1000.0, double depth_max=3.0, int stride=1)
Factory function to create a pointcloud from a depth image and a camera model.
Definition: PointCloud.cpp:148
static Tensor Eye(int64_t n, Dtype dtype, const Device &device)
Create a identity matrix of size n x n.
Definition: Tensor.cpp:194
void AssertShapeCompatible(const DynamicSizeVector &expected_shape) const
Assert that Tensor&#39;s shape is compatible with a dynamic shape.
Definition: Tensor.cpp:1256
core::Tensor & GetPointAttr(const std::string &key)
Definition: PointCloud.h:126
core::Tensor GetCenter() const
Returns the center for point coordinates.
Definition: PointCloud.cpp:74
int64_t GetLength() const
Definition: Tensor.h:943
std::string ToString() const
Definition: Device.h:73
Definition: TensorMap.h:49
PointCloud Copy() const
Returns deep copy of the pointcloud on the same device.
Definition: PointCloud.cpp:84