Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
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-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 "open3d/core/Tensor.h"
35 
36 namespace open3d {
37 namespace t {
38 namespace geometry {
39 
106 class TriangleMesh : public Geometry, public DrawableGeometry {
107 public:
111  TriangleMesh(const core::Device &device = core::Device("CPU:0"));
112 
121  TriangleMesh(const core::Tensor &vertex_positions,
122  const core::Tensor &triangle_indices);
123 
124  virtual ~TriangleMesh() override {}
125 
126 public:
128  std::string ToString() const;
129 
135  TriangleMesh To(const core::Device &device, bool copy = false) const;
136 
138  TriangleMesh Clone() const { return To(GetDevice(), /*copy=*/true); }
139 
141  const TensorMap &GetVertexAttr() const { return vertex_attr_; }
142 
147  core::Tensor &GetVertexAttr(const std::string &key) {
148  return vertex_attr_.at(key);
149  }
150 
153  core::Tensor &GetVertexPositions() { return GetVertexAttr("positions"); }
154 
157  core::Tensor &GetVertexColors() { return GetVertexAttr("colors"); }
158 
161  core::Tensor &GetVertexNormals() { return GetVertexAttr("normals"); }
162 
164  const TensorMap &GetTriangleAttr() const { return triangle_attr_; }
165 
170  core::Tensor &GetTriangleAttr(const std::string &key) {
171  return triangle_attr_.at(key);
172  }
173 
177 
181 
185 
189  const core::Tensor &GetVertexAttr(const std::string &key) const {
190  return vertex_attr_.at(key);
191  }
192 
197  void RemoveVertexAttr(const std::string &key) { vertex_attr_.Erase(key); }
198 
202  return GetVertexAttr("positions");
203  }
204 
207  const core::Tensor &GetVertexColors() const {
208  return GetVertexAttr("colors");
209  }
210 
214  return GetVertexAttr("normals");
215  }
216 
221  const core::Tensor &GetTriangleAttr(const std::string &key) const {
222  return triangle_attr_.at(key);
223  }
224 
229  void RemoveTriangleAttr(const std::string &key) {
230  triangle_attr_.Erase(key);
231  }
232 
236  return GetTriangleAttr("indices");
237  }
238 
242  return GetTriangleAttr("normals");
243  }
244 
248  return GetTriangleAttr("colors");
249  }
250 
256  void SetVertexAttr(const std::string &key, const core::Tensor &value) {
258  vertex_attr_[key] = value;
259  }
260 
263  void SetVertexPositions(const core::Tensor &value) {
265  SetVertexAttr("positions", value);
266  }
267 
270  void SetVertexColors(const core::Tensor &value) {
272  SetVertexAttr("colors", value);
273  }
274 
277  void SetVertexNormals(const core::Tensor &value) {
279  SetVertexAttr("normals", value);
280  }
281 
287  void SetTriangleAttr(const std::string &key, const core::Tensor &value) {
289  triangle_attr_[key] = value;
290  }
291 
293  void SetTriangleIndices(const core::Tensor &value) {
295  SetTriangleAttr("indices", value);
296  }
297 
300  void SetTriangleNormals(const core::Tensor &value) {
302  SetTriangleAttr("normals", value);
303  }
304 
307  void SetTriangleColors(const core::Tensor &value) {
309  SetTriangleAttr("colors", value);
310  }
311 
316  bool HasVertexAttr(const std::string &key) const {
317  return vertex_attr_.Contains(key) &&
318  GetVertexAttr(key).GetLength() > 0 &&
319  GetVertexAttr(key).GetLength() ==
321  }
322 
325  bool HasVertexPositions() const { return HasVertexAttr("positions"); }
326 
332  bool HasVertexColors() const { return HasVertexAttr("colors"); }
333 
339  bool HasVertexNormals() const { return HasVertexAttr("normals"); }
340 
345  bool HasTriangleAttr(const std::string &key) const {
346  return triangle_attr_.Contains(key) &&
347  GetTriangleAttr(key).GetLength() > 0 &&
348  GetTriangleAttr(key).GetLength() ==
350  }
351 
355  bool HasTriangleIndices() const { return HasTriangleAttr("indices"); }
356 
362  bool HasTriangleNormals() const { return HasTriangleAttr("normals"); }
363 
369  bool HasTriangleColors() const { return HasTriangleAttr("colors"); }
370 
371 public:
373  TriangleMesh &Clear() override {
374  vertex_attr_.clear();
375  triangle_attr_.clear();
376  return *this;
377  }
378 
380  bool IsEmpty() const override { return !HasVertexPositions(); }
381 
382  core::Tensor GetMinBound() const { return GetVertexPositions().Min({0}); }
383 
384  core::Tensor GetMaxBound() const { return GetVertexPositions().Max({0}); }
385 
386  core::Tensor GetCenter() const { return GetVertexPositions().Mean({0}); }
387 
407  TriangleMesh &Transform(const core::Tensor &transformation);
408 
413  TriangleMesh &Translate(const core::Tensor &translation,
414  bool relative = true);
415 
421  TriangleMesh &Scale(double scale, const core::Tensor &center);
422 
429  TriangleMesh &Rotate(const core::Tensor &R, const core::Tensor &center);
430 
431  core::Device GetDevice() const { return device_; }
432 
441  const open3d::geometry::TriangleMesh &mesh_legacy,
442  core::Dtype float_dtype = core::Float32,
443  core::Dtype int_dtype = core::Int64,
444  const core::Device &device = core::Device("CPU:0"));
445 
448 
449 protected:
453 };
454 
455 } // namespace geometry
456 } // namespace t
457 } // namespace open3d
const TensorMap & GetVertexAttr() const
Getter for vertex_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:141
void SetTriangleIndices(const core::Tensor &value)
Set the vlaue of the "indices" attribute in triangle_attr_.
Definition: TriangleMesh.h:293
bool HasVertexColors() const
Definition: TriangleMesh.h:332
constexpr nullopt_t nullopt
Definition: Optional.h:171
TriangleMesh & Clear() override
Clear all data in the trianglemesh.
Definition: TriangleMesh.h:373
core::Device GetDevice() const
Definition: TriangleMesh.h:431
const Dtype Int64
Definition: Dtype.cpp:66
TriangleMesh & Scale(double scale, const core::Tensor &center)
Scales the VertexPositions of the TriangleMesh.
Definition: TriangleMesh.cpp:141
TensorMap vertex_attr_
Definition: TriangleMesh.h:451
virtual ~TriangleMesh() override
Definition: TriangleMesh.h:124
void SetVertexNormals(const core::Tensor &value)
Definition: TriangleMesh.h:277
core::Tensor & GetVertexPositions()
Definition: TriangleMesh.h:153
Definition: Dtype.h:39
void SetVertexAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:256
A triangle mesh contains vertices and triangles.
Definition: TriangleMesh.h:106
const core::Tensor & GetTriangleNormals() const
Definition: TriangleMesh.h:241
bool HasTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:345
void SetTriangleAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:287
std::string ToString() const
Text description.
Definition: TriangleMesh.cpp:66
TriangleMesh Clone() const
Returns copy of the triangle mesh on the same device.
Definition: TriangleMesh.h:138
open3d::geometry::TriangleMesh ToLegacy() const
Convert to a legacy Open3D TriangleMesh.
Definition: TriangleMesh.cpp:218
void SetTriangleNormals(const core::Tensor &value)
Definition: TriangleMesh.h:300
const core::Tensor & GetVertexPositions() const
Definition: TriangleMesh.h:201
const Dtype Float32
Definition: Dtype.cpp:61
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1198
core::Tensor & GetVertexColors()
Definition: TriangleMesh.h:157
const core::Tensor & GetTriangleIndices() const
Definition: TriangleMesh.h:235
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1205
#define AssertTensorShape(tensor,...)
Definition: TensorCheck.h:77
const core::Tensor & GetVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:189
void RemoveTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:229
core::Tensor & GetVertexAttr(const std::string &key)
Definition: TriangleMesh.h:147
void SetVertexColors(const core::Tensor &value)
Definition: TriangleMesh.h:270
core::Tensor & GetTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:170
TriangleMesh & Transform(const core::Tensor &transformation)
Transforms the VertexPositions, VertexNormals and TriangleNormals (if exist) of the TriangleMesh...
Definition: TriangleMesh.cpp:112
bool Contains(const std::string &key) const
Definition: TensorMap.h:134
const TensorMap & GetTriangleAttr() const
Getter for triangle_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:164
bool IsEmpty() const override
Returns !HasVertexPositions(), triangles are ignored.
Definition: TriangleMesh.h:380
Definition: Device.h:39
void SetVertexPositions(const core::Tensor &value)
Definition: TriangleMesh.h:263
const core::Tensor & GetVertexColors() const
Definition: TriangleMesh.h:207
TriangleMesh To(const core::Device &device, bool copy=false) const
Definition: TriangleMesh.cpp:259
void RemoveVertexAttr(const std::string &key)
Definition: TriangleMesh.h:197
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1177
core::Tensor GetMinBound() const
Definition: TriangleMesh.h:382
#define AssertTensorDevice(tensor,...)
Definition: TensorCheck.h:62
The base geometry class.
Definition: Geometry.h:38
Definition: PinholeCameraIntrinsic.cpp:35
TriangleMesh & Translate(const core::Tensor &translation, bool relative=true)
Translates the VertexPositions of the TriangleMesh.
Definition: TriangleMesh.cpp:127
core::Device device_
Definition: TriangleMesh.h:450
Mix-in class for geometry types that can be visualized.
Definition: DrawableGeometry.h:38
TriangleMesh(const core::Device &device=core::Device("CPU:0"))
Definition: TriangleMesh.cpp:44
core::Tensor & GetTriangleColors()
Definition: TriangleMesh.h:184
core::Tensor & GetVertexNormals()
Definition: TriangleMesh.h:161
const core::Tensor & GetTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:221
core::Tensor & GetTriangleNormals()
Definition: TriangleMesh.h:180
bool HasVertexNormals() const
Definition: TriangleMesh.h:339
Triangle mesh contains vertices and triangles represented by the indices to the vertices.
Definition: TriangleMesh.h:54
bool HasVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:316
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:369
bool HasTriangleNormals() const
Definition: TriangleMesh.h:362
TriangleMesh & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotates the VertexPositions, VertexNormals and TriangleNormals (if exists).
Definition: TriangleMesh.cpp:152
int64_t GetLength() const
Definition: Tensor.h:1055
TensorMap triangle_attr_
Definition: TriangleMesh.h:452
static geometry::TriangleMesh FromLegacy(const open3d::geometry::TriangleMesh &mesh_legacy, core::Dtype float_dtype=core::Float32, core::Dtype int_dtype=core::Int64, const core::Device &device=core::Device("CPU:0"))
Definition: TriangleMesh.cpp:167
void SetTriangleColors(const core::Tensor &value)
Definition: TriangleMesh.h:307
core::Tensor GetCenter() const
Definition: TriangleMesh.h:386
bool HasVertexPositions() const
Definition: TriangleMesh.h:325
Definition: TensorMap.h:49
const core::Tensor & GetTriangleColors() const
Definition: TriangleMesh.h:247
bool HasTriangleIndices() const
Definition: TriangleMesh.h:355
const core::Tensor & GetVertexNormals() const
Definition: TriangleMesh.h:213
core::Tensor & GetTriangleIndices()
Definition: TriangleMesh.h:176
core::Tensor GetMaxBound() const
Definition: TriangleMesh.h:384