Open3D (C++ API)  0.12.0
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:
131  const TensorMap &GetVertexAttr() const { return vertex_attr_; }
132 
137  core::Tensor &GetVertexAttr(const std::string &key) {
138  return vertex_attr_.at(key);
139  }
140 
143  core::Tensor &GetVertices() { return GetVertexAttr("vertices"); }
144 
147  core::Tensor &GetVertexColors() { return GetVertexAttr("colors"); }
148 
151  core::Tensor &GetVertexNormals() { return GetVertexAttr("normals"); }
152 
154  const TensorMap &GetTriangleAttr() const { return triangle_attr_; }
155 
160  core::Tensor &GetTriangleAttr(const std::string &key) {
161  return triangle_attr_.at(key);
162  }
163 
166  core::Tensor &GetTriangles() { return GetTriangleAttr("triangles"); }
167 
171 
175 
179  const core::Tensor &GetVertexAttr(const std::string &key) const {
180  return vertex_attr_.at(key);
181  }
182 
185  const core::Tensor &GetVertices() const {
186  return GetVertexAttr("vertices");
187  }
188 
191  const core::Tensor &GetVertexColors() const {
192  return GetVertexAttr("colors");
193  }
194 
198  return GetVertexAttr("normals");
199  }
200 
205  const core::Tensor &GetTriangleAttr(const std::string &key) const {
206  return triangle_attr_.at(key);
207  }
208 
211  const core::Tensor &GetTriangles() const {
212  return GetTriangleAttr("triangles");
213  }
214 
218  return GetTriangleAttr("normals");
219  }
220 
224  return GetTriangleAttr("colors");
225  }
226 
232  void SetVertexAttr(const std::string &key, const core::Tensor &value) {
233  value.AssertDevice(device_);
234  vertex_attr_[key] = value;
235  }
236 
239  void SetVertices(const core::Tensor &value) {
241  SetVertexAttr("vertices", value);
242  }
243 
246  void SetVertexColors(const core::Tensor &value) {
248  SetVertexAttr("colors", value);
249  }
250 
253  void SetVertexNormals(const core::Tensor &value) {
255  SetVertexAttr("normals", value);
256  }
257 
263  void SetTriangleAttr(const std::string &key, const core::Tensor &value) {
264  value.AssertDevice(device_);
265  triangle_attr_[key] = value;
266  }
267 
269  void SetTriangles(const core::Tensor &value) {
271  SetTriangleAttr("triangles", value);
272  }
273 
276  void SetTriangleNormals(const core::Tensor &value) {
278  SetTriangleAttr("normals", value);
279  }
280 
283  void SetTriangleColors(const core::Tensor &value) {
285  SetTriangleAttr("colors", value);
286  }
287 
292  bool HasVertexAttr(const std::string &key) const {
293  return vertex_attr_.Contains(key) &&
294  GetVertexAttr(key).GetLength() > 0 &&
295  GetVertexAttr(key).GetLength() == GetVertices().GetLength();
296  }
297 
300  bool HasVertices() const { return HasVertexAttr("vertices"); }
301 
307  bool HasVertexColors() const { return HasVertexAttr("colors"); }
308 
314  bool HasVertexNormals() const { return HasVertexAttr("normals"); }
315 
320  bool HasTriangleAttr(const std::string &key) const {
321  return triangle_attr_.Contains(key) &&
322  GetTriangleAttr(key).GetLength() > 0 &&
323  GetTriangleAttr(key).GetLength() == GetTriangles().GetLength();
324  }
325 
329  bool HasTriangles() const { return HasTriangleAttr("triangles"); }
330 
336  bool HasTriangleNormals() const { return HasTriangleAttr("normals"); }
337 
343  bool HasTriangleColors() const { return HasTriangleAttr("colors"); }
344 
345 public:
347  TriangleMesh &Clear() override {
348  vertex_attr_.clear();
349  triangle_attr_.clear();
350  return *this;
351  }
352 
354  bool IsEmpty() const override { return !HasVertices(); }
355 
356  core::Tensor GetMinBound() const { utility::LogError("Unimplemented"); }
357 
358  core::Tensor GetMaxBound() const { utility::LogError("Unimplemented"); }
359 
360  core::Tensor GetCenter() const { utility::LogError("Unimplemented"); }
361 
362  TriangleMesh &Transform(const core::Tensor &transformation) {
363  utility::LogError("Unimplemented");
364  }
365 
366  TriangleMesh &Translate(const core::Tensor &translation,
367  bool relative = true) {
368  utility::LogError("Unimplemented");
369  }
370 
371  TriangleMesh &Scale(double scale, const core::Tensor &center) {
372  utility::LogError("Unimplemented");
373  }
374 
375  TriangleMesh &Rotate(const core::Tensor &R, const core::Tensor &center) {
376  utility::LogError("Unimplemented");
377  }
378 
379  core::Device GetDevice() const { return device_; }
380 
389  const open3d::geometry::TriangleMesh &mesh_legacy,
390  core::Dtype float_dtype = core::Dtype::Float32,
391  core::Dtype int_dtype = core::Dtype::Int64,
392  const core::Device &device = core::Device("CPU:0"));
393 
396 
397 protected:
401 };
402 
403 } // namespace geometry
404 } // namespace t
405 } // namespace open3d
const TensorMap & GetVertexAttr() const
Getter for vertex_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:131
bool HasVertexColors() const
Definition: TriangleMesh.h:307
constexpr nullopt_t nullopt
Definition: Optional.h:146
TriangleMesh & Clear() override
Clear all data in the trianglemesh.
Definition: TriangleMesh.h:347
core::Device GetDevice() const
Definition: TriangleMesh.h:379
TensorMap vertex_attr_
Definition: TriangleMesh.h:399
TriangleMesh & Transform(const core::Tensor &transformation)
Definition: TriangleMesh.h:362
virtual ~TriangleMesh() override
Definition: TriangleMesh.h:127
void SetVertexNormals(const core::Tensor &value)
Definition: TriangleMesh.h:253
Definition: Dtype.h:39
void SetVertexAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:232
A TriangleMesh contains vertices and triangles.
Definition: TriangleMesh.h:106
const core::Tensor & GetTriangleNormals() const
Definition: TriangleMesh.h:217
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
bool HasTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:320
void SetTriangleAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:263
TriangleMesh & Translate(const core::Tensor &translation, bool relative=true)
Definition: TriangleMesh.h:366
void SetTriangleNormals(const core::Tensor &value)
Definition: TriangleMesh.h:276
bool HasVertices() const
Definition: TriangleMesh.h:300
core::Tensor & GetVertexColors()
Definition: TriangleMesh.h:147
open3d::geometry::TriangleMesh ToLegacyTriangleMesh() const
Convert to a legacy Open3D TriangleMesh.
Definition: TriangleMesh.cpp:106
const core::Tensor & GetVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:179
core::Tensor & GetVertexAttr(const std::string &key)
Definition: TriangleMesh.h:137
void SetVertices(const core::Tensor &value)
Definition: TriangleMesh.h:239
void SetVertexColors(const core::Tensor &value)
Definition: TriangleMesh.h:246
const core::Tensor & GetVertices() const
Definition: TriangleMesh.h:185
core::Tensor & GetTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:160
TriangleMesh & Rotate(const core::Tensor &R, const core::Tensor &center)
Definition: TriangleMesh.h:375
core::Tensor & GetTriangles()
Definition: TriangleMesh.h:166
void AssertDevice(const Device &expected_device) const
Assert that the Tensor has the specified device.
Definition: Tensor.cpp:1261
bool Contains(const std::string &key) const
Definition: TensorMap.h:110
bool HasTriangles() const
Definition: TriangleMesh.h:329
const TensorMap & GetTriangleAttr() const
Getter for triangle_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:154
bool IsEmpty() const override
Returns !HasVertices(), triangles are ignored.
Definition: TriangleMesh.h:354
Definition: Device.h:39
const core::Tensor & GetVertexColors() const
Definition: TriangleMesh.h:191
core::Tensor & GetVertices()
Definition: TriangleMesh.h:143
core::Tensor GetMinBound() const
Definition: TriangleMesh.h:356
static const Dtype Float32
Definition: Dtype.h:42
The base geometry class.
Definition: Geometry.h:38
static const Dtype Int64
Definition: Dtype.h:45
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:48
core::Device device_
Definition: TriangleMesh.h:398
TriangleMesh(const core::Device &device=core::Device("CPU:0"))
Construct an empty trianglemesh.
Definition: TriangleMesh.cpp:41
core::Tensor & GetTriangleColors()
Definition: TriangleMesh.h:174
core::Tensor & GetVertexNormals()
Definition: TriangleMesh.h:151
const core::Tensor & GetTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:205
core::Tensor & GetTriangleNormals()
Definition: TriangleMesh.h:170
bool HasVertexNormals() const
Definition: TriangleMesh.h:314
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:211
bool HasVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:292
void AssertShapeCompatible(const DynamicSizeVector &expected_shape) const
Assert that Tensor's shape is compatible with a dynamic shape.
Definition: Tensor.cpp:1256
bool HasTriangleColors() const
Definition: TriangleMesh.h:343
bool HasTriangleNormals() const
Definition: TriangleMesh.h:336
int64_t GetLength() const
Definition: Tensor.h:943
TensorMap triangle_attr_
Definition: TriangleMesh.h:400
void SetTriangles(const core::Tensor &value)
Set the vlaue of the "triangles" attribute in triangle_attr_.
Definition: TriangleMesh.h:269
void SetTriangleColors(const core::Tensor &value)
Definition: TriangleMesh.h:283
TriangleMesh & Scale(double scale, const core::Tensor &center)
Definition: TriangleMesh.h:371
core::Tensor GetCenter() const
Definition: TriangleMesh.h:360
Definition: TensorMap.h:49
const core::Tensor & GetTriangleColors() const
Definition: TriangleMesh.h:223
const core::Tensor & GetVertexNormals() const
Definition: TriangleMesh.h:197
core::Tensor GetMaxBound() const
Definition: TriangleMesh.h:358