Open3D (C++ API)  0.13.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TriangleMesh.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 "open3d/core/Tensor.h"
33 
34 namespace open3d {
35 namespace t {
36 namespace geometry {
37 
106 class TriangleMesh : public Geometry {
107 public:
109  TriangleMesh(const core::Device &device = core::Device("CPU:0"));
110 
125  TriangleMesh(const core::Tensor &vertices, const core::Tensor &triangles);
126 
127  virtual ~TriangleMesh() override {}
128 
129 public:
135  TriangleMesh To(const core::Device &device, bool copy = false) const;
136 
138  TriangleMesh Clone() const { return To(GetDevice()); }
139 
143  TriangleMesh CPU() const { return To(core::Device("CPU:0")); };
144 
149  TriangleMesh CUDA(int device_id = 0) const {
150  return To(core::Device(core::Device::DeviceType::CUDA, device_id));
151  };
152 
154  const TensorMap &GetVertexAttr() const { return vertex_attr_; }
155 
160  core::Tensor &GetVertexAttr(const std::string &key) {
161  return vertex_attr_.at(key);
162  }
163 
166  core::Tensor &GetVertices() { return GetVertexAttr("vertices"); }
167 
170  core::Tensor &GetVertexColors() { return GetVertexAttr("colors"); }
171 
174  core::Tensor &GetVertexNormals() { return GetVertexAttr("normals"); }
175 
177  const TensorMap &GetTriangleAttr() const { return triangle_attr_; }
178 
183  core::Tensor &GetTriangleAttr(const std::string &key) {
184  return triangle_attr_.at(key);
185  }
186 
189  core::Tensor &GetTriangles() { return GetTriangleAttr("triangles"); }
190 
194 
198 
202  const core::Tensor &GetVertexAttr(const std::string &key) const {
203  return vertex_attr_.at(key);
204  }
205 
210  void RemoveVertexAttr(const std::string &key) { vertex_attr_.Erase(key); }
211 
214  const core::Tensor &GetVertices() const {
215  return GetVertexAttr("vertices");
216  }
217 
220  const core::Tensor &GetVertexColors() const {
221  return GetVertexAttr("colors");
222  }
223 
227  return GetVertexAttr("normals");
228  }
229 
234  const core::Tensor &GetTriangleAttr(const std::string &key) const {
235  return triangle_attr_.at(key);
236  }
237 
242  void RemoveTriangleAttr(const std::string &key) {
243  triangle_attr_.Erase(key);
244  }
245 
248  const core::Tensor &GetTriangles() const {
249  return GetTriangleAttr("triangles");
250  }
251 
255  return GetTriangleAttr("normals");
256  }
257 
261  return GetTriangleAttr("colors");
262  }
263 
269  void SetVertexAttr(const std::string &key, const core::Tensor &value) {
270  value.AssertDevice(device_);
271  vertex_attr_[key] = value;
272  }
273 
276  void SetVertices(const core::Tensor &value) {
278  SetVertexAttr("vertices", value);
279  }
280 
283  void SetVertexColors(const core::Tensor &value) {
285  SetVertexAttr("colors", value);
286  }
287 
290  void SetVertexNormals(const core::Tensor &value) {
292  SetVertexAttr("normals", value);
293  }
294 
300  void SetTriangleAttr(const std::string &key, const core::Tensor &value) {
301  value.AssertDevice(device_);
302  triangle_attr_[key] = value;
303  }
304 
306  void SetTriangles(const core::Tensor &value) {
308  SetTriangleAttr("triangles", value);
309  }
310 
313  void SetTriangleNormals(const core::Tensor &value) {
315  SetTriangleAttr("normals", value);
316  }
317 
320  void SetTriangleColors(const core::Tensor &value) {
322  SetTriangleAttr("colors", value);
323  }
324 
329  bool HasVertexAttr(const std::string &key) const {
330  return vertex_attr_.Contains(key) &&
331  GetVertexAttr(key).GetLength() > 0 &&
332  GetVertexAttr(key).GetLength() == GetVertices().GetLength();
333  }
334 
337  bool HasVertices() const { return HasVertexAttr("vertices"); }
338 
344  bool HasVertexColors() const { return HasVertexAttr("colors"); }
345 
351  bool HasVertexNormals() const { return HasVertexAttr("normals"); }
352 
357  bool HasTriangleAttr(const std::string &key) const {
358  return triangle_attr_.Contains(key) &&
359  GetTriangleAttr(key).GetLength() > 0 &&
360  GetTriangleAttr(key).GetLength() == GetTriangles().GetLength();
361  }
362 
366  bool HasTriangles() const { return HasTriangleAttr("triangles"); }
367 
373  bool HasTriangleNormals() const { return HasTriangleAttr("normals"); }
374 
380  bool HasTriangleColors() const { return HasTriangleAttr("colors"); }
381 
382 public:
384  TriangleMesh &Clear() override {
385  vertex_attr_.clear();
386  triangle_attr_.clear();
387  return *this;
388  }
389 
391  bool IsEmpty() const override { return !HasVertices(); }
392 
393  core::Tensor GetMinBound() const { utility::LogError("Unimplemented"); }
394 
395  core::Tensor GetMaxBound() const { utility::LogError("Unimplemented"); }
396 
397  core::Tensor GetCenter() const { utility::LogError("Unimplemented"); }
398 
399  TriangleMesh &Transform(const core::Tensor &transformation) {
400  utility::LogError("Unimplemented");
401  }
402 
403  TriangleMesh &Translate(const core::Tensor &translation,
404  bool relative = true) {
405  utility::LogError("Unimplemented");
406  }
407 
408  TriangleMesh &Scale(double scale, const core::Tensor &center) {
409  utility::LogError("Unimplemented");
410  }
411 
412  TriangleMesh &Rotate(const core::Tensor &R, const core::Tensor &center) {
413  utility::LogError("Unimplemented");
414  }
415 
416  core::Device GetDevice() const { return device_; }
417 
426  const open3d::geometry::TriangleMesh &mesh_legacy,
427  core::Dtype float_dtype = core::Dtype::Float32,
428  core::Dtype int_dtype = core::Dtype::Int64,
429  const core::Device &device = core::Device("CPU:0"));
430 
433 
434 protected:
438 };
439 
440 } // namespace geometry
441 } // namespace t
442 } // namespace open3d
TriangleMesh CUDA(int device_id=0) const
Definition: TriangleMesh.h:149
const TensorMap & GetVertexAttr() const
Getter for vertex_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:154
bool HasVertexColors() const
Definition: TriangleMesh.h:344
void AssertShapeCompatible(const DynamicSizeVector &expected_shape, const std::string &error_msg="") const
Assert that Tensor's shape is compatible with a dynamic shape.
Definition: Tensor.cpp:1513
constexpr nullopt_t nullopt
Definition: Optional.h:146
TriangleMesh & Clear() override
Clear all data in the trianglemesh.
Definition: TriangleMesh.h:384
core::Device GetDevice() const
Definition: TriangleMesh.h:416
TensorMap vertex_attr_
Definition: TriangleMesh.h:436
TriangleMesh & Transform(const core::Tensor &transformation)
Definition: TriangleMesh.h:399
virtual ~TriangleMesh() override
Definition: TriangleMesh.h:127
void SetVertexNormals(const core::Tensor &value)
Definition: TriangleMesh.h:290
Definition: Dtype.h:39
void SetVertexAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:269
A TriangleMesh contains vertices and triangles.
Definition: TriangleMesh.h:106
const core::Tensor & GetTriangleNormals() const
Definition: TriangleMesh.h:254
bool HasTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:357
void SetTriangleAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:300
TriangleMesh & Translate(const core::Tensor &translation, bool relative=true)
Definition: TriangleMesh.h:403
TriangleMesh Clone() const
Returns copy of the triangle mesh on the same device.
Definition: TriangleMesh.h:138
void SetTriangleNormals(const core::Tensor &value)
Definition: TriangleMesh.h:313
bool HasVertices() const
Definition: TriangleMesh.h:337
core::Tensor & GetVertexColors()
Definition: TriangleMesh.h:170
open3d::geometry::TriangleMesh ToLegacyTriangleMesh() const
Convert to a legacy Open3D TriangleMesh.
Definition: TriangleMesh.cpp:106
#define LogError(...)
Definition: Console.h:79
const core::Tensor & GetVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:202
void RemoveTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:242
core::Tensor & GetVertexAttr(const std::string &key)
Definition: TriangleMesh.h:160
void SetVertices(const core::Tensor &value)
Definition: TriangleMesh.h:276
void SetVertexColors(const core::Tensor &value)
Definition: TriangleMesh.h:283
const core::Tensor & GetVertices() const
Definition: TriangleMesh.h:214
core::Tensor & GetTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:183
TriangleMesh & Rotate(const core::Tensor &R, const core::Tensor &center)
Definition: TriangleMesh.h:412
core::Tensor & GetTriangles()
Definition: TriangleMesh.h:189
bool Contains(const std::string &key) const
Definition: TensorMap.h:125
bool HasTriangles() const
Definition: TriangleMesh.h:366
const TensorMap & GetTriangleAttr() const
Getter for triangle_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:177
bool IsEmpty() const override
Returns !HasVertices(), triangles are ignored.
Definition: TriangleMesh.h:391
Definition: Device.h:39
const core::Tensor & GetVertexColors() const
Definition: TriangleMesh.h:220
TriangleMesh To(const core::Device &device, bool copy=false) const
Definition: TriangleMesh.cpp:137
void RemoveVertexAttr(const std::string &key)
Definition: TriangleMesh.h:210
core::Tensor & GetVertices()
Definition: TriangleMesh.h:166
core::Tensor GetMinBound() const
Definition: TriangleMesh.h:393
TriangleMesh CPU() const
Definition: TriangleMesh.h:143
static const Dtype Float32
Definition: Dtype.h:42
The base geometry class.
Definition: Geometry.h:38
static const Dtype Int64
Definition: Dtype.h:47
static geometry::TriangleMesh FromLegacyTriangleMesh(const open3d::geometry::TriangleMesh &mesh_legacy, core::Dtype float_dtype=core::Dtype::Float32, core::Dtype int_dtype=core::Dtype::Int64, const core::Device &device=core::Device("CPU:0"))
Definition: TriangleMesh.cpp:63
Definition: PinholeCameraIntrinsic.cpp:35
Definition: Tensor.h:50
core::Device device_
Definition: TriangleMesh.h:435
TriangleMesh(const core::Device &device=core::Device("CPU:0"))
Construct an empty trianglemesh.
Definition: TriangleMesh.cpp:41
core::Tensor & GetTriangleColors()
Definition: TriangleMesh.h:197
core::Tensor & GetVertexNormals()
Definition: TriangleMesh.h:174
const core::Tensor & GetTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:234
core::Tensor & GetTriangleNormals()
Definition: TriangleMesh.h:193
bool HasVertexNormals() const
Definition: TriangleMesh.h:351
void AssertDevice(const Device &expected_device, const std::string &error_msg="") const
Assert that the Tensor has the specified device.
Definition: Tensor.cpp:1518
Triangle mesh contains vertices and triangles represented by the indices to the vertices.
Definition: TriangleMesh.h:54
const core::Tensor & GetTriangles() const
Definition: TriangleMesh.h:248
bool HasVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:329
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 HasTriangleColors() const
Definition: TriangleMesh.h:380
bool HasTriangleNormals() const
Definition: TriangleMesh.h:373
int64_t GetLength() const
Definition: Tensor.h:986
TensorMap triangle_attr_
Definition: TriangleMesh.h:437
void SetTriangles(const core::Tensor &value)
Set the vlaue of the "triangles" attribute in triangle_attr_.
Definition: TriangleMesh.h:306
void SetTriangleColors(const core::Tensor &value)
Definition: TriangleMesh.h:320
TriangleMesh & Scale(double scale, const core::Tensor &center)
Definition: TriangleMesh.h:408
core::Tensor GetCenter() const
Definition: TriangleMesh.h:397
Definition: TensorMap.h:49
const core::Tensor & GetTriangleColors() const
Definition: TriangleMesh.h:260
const core::Tensor & GetVertexNormals() const
Definition: TriangleMesh.h:226
core::Tensor GetMaxBound() const
Definition: TriangleMesh.h:395