Open3D (C++ API)  0.11.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"
30 #include "open3d/core/TensorList.h"
34 
35 namespace open3d {
36 namespace t {
37 namespace geometry {
38 
107 class TriangleMesh : public Geometry {
108 public:
111  core::Dtype triangle_dtype = core::Dtype::Int64,
112  const core::Device &device = core::Device("CPU:0"));
113 
128  TriangleMesh(const core::TensorList &vertices,
129  const core::TensorList &triangles);
130 
131  virtual ~TriangleMesh() override {}
132 
133 public:
138  core::TensorList &GetVertexAttr(const std::string &key) {
139  return vertex_attr_.at(key);
140  }
141 
144  core::TensorList &GetVertices() { return GetVertexAttr("vertices"); }
145 
149 
153 
158  core::TensorList &GetTriangleAttr(const std::string &key) {
159  return triangle_attr_.at(key);
160  }
161 
164  core::TensorList &GetTriangles() { return GetTriangleAttr("triangles"); }
165 
169  return GetTriangleAttr("normals");
170  }
171 
175 
179  const core::TensorList &GetVertexAttr(const std::string &key) const {
180  return vertex_attr_.at(key);
181  }
182 
185  const core::TensorList &GetVertices() const {
186  return GetVertexAttr("vertices");
187  }
188 
192  return GetVertexAttr("colors");
193  }
194 
198  return GetVertexAttr("normals");
199  }
200 
205  const core::TensorList &GetTriangleAttr(const std::string &key) const {
206  return triangle_attr_.at(key);
207  }
208 
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::TensorList &value) {
233  value.AssertDevice(device_);
234  vertex_attr_[key] = value;
235  }
236 
239  void SetVertices(const core::TensorList &value) {
240  value.AssertElementShape({3});
241  SetVertexAttr("vertices", value);
242  }
243 
246  void SetVertexColors(const core::TensorList &value) {
247  value.AssertElementShape({3});
248  SetVertexAttr("colors", value);
249  }
250 
253  void SetVertexNormals(const core::TensorList &value) {
254  value.AssertElementShape({3});
255  SetVertexAttr("normals", value);
256  }
257 
263  void SetTriangleAttr(const std::string &key,
264  const core::TensorList &value) {
265  value.AssertDevice(device_);
266  triangle_attr_[key] = value;
267  }
268 
270  void SetTriangles(const core::TensorList &value) {
271  value.AssertElementShape({3});
272  SetTriangleAttr("triangles", value);
273  }
274 
278  value.AssertElementShape({3});
279  SetTriangleAttr("normals", value);
280  }
281 
284  void SetTriangleColors(const core::TensorList &value) {
285  value.AssertElementShape({3});
286  SetTriangleAttr("colors", value);
287  }
288 
293  bool HasVertexAttr(const std::string &key) const {
294  return vertex_attr_.Contains(key) && GetVertexAttr(key).GetSize() > 0 &&
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).GetSize() > 0 &&
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 
353  const std::unordered_map<std::string, core::Tensor>
354  &map_keys_to_tensors) {
355  vertex_attr_.SynchronizedPushBack(map_keys_to_tensors);
356  }
357 
366  const std::unordered_map<std::string, core::Tensor>
367  &map_keys_to_tensors) {
368  triangle_attr_.SynchronizedPushBack(map_keys_to_tensors);
369  }
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 !HasVertices(); }
381 
382  core::Tensor GetMinBound() const { utility::LogError("Unimplemented"); }
383 
384  core::Tensor GetMaxBound() const { utility::LogError("Unimplemented"); }
385 
386  core::Tensor GetCenter() const { utility::LogError("Unimplemented"); }
387 
388  TriangleMesh &Transform(const core::Tensor &transformation) {
389  utility::LogError("Unimplemented");
390  }
391 
392  TriangleMesh &Translate(const core::Tensor &translation,
393  bool relative = true) {
394  utility::LogError("Unimplemented");
395  }
396 
397  TriangleMesh &Scale(double scale, const core::Tensor &center) {
398  utility::LogError("Unimplemented");
399  }
400 
401  TriangleMesh &Rotate(const core::Tensor &R, const core::Tensor &center) {
402  utility::LogError("Unimplemented");
403  }
404 
405  core::Device GetDevice() const { return device_; }
406 
409  const geometry::TriangleMesh &mesh_legacy,
411  const core::Device &device = core::Device("CPU:0")) {
412  utility::LogError("Unimplemented");
413  }
414 
417  utility::LogError("Unimplemented");
418  }
419 
420 protected:
424 };
425 
426 } // namespace geometry
427 } // namespace t
428 } // namespace open3d
void SynchronizedPushBack(const std::unordered_map< std::string, core::Tensor > &map_keys_to_tensors)
Definition: TensorListMap.cpp:89
const core::TensorList & GetTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:205
core::TensorList & GetVertices()
Definition: TriangleMesh.h:144
bool HasVertexColors() const
Definition: TriangleMesh.h:307
TriangleMesh & Clear() override
Clear all data in the trianglemesh.
Definition: TriangleMesh.h:373
core::Device GetDevice() const
Definition: TriangleMesh.h:405
core::TensorList & GetTriangles()
Definition: TriangleMesh.h:164
int64_t GetSize() const
Definition: TensorList.h:269
geometry::TriangleMesh ToLegacyTriangleMesh() const
Convert to a legacy Open3D TriangleMesh.
Definition: TriangleMesh.h:416
TriangleMesh & Transform(const core::Tensor &transformation)
Definition: TriangleMesh.h:388
virtual ~TriangleMesh() override
Definition: TriangleMesh.h:131
Definition: Dtype.h:39
TensorListMap triangle_attr_
Definition: TriangleMesh.h:423
const core::TensorList & GetVertexNormals() const
Definition: TriangleMesh.h:197
TriangleMesh(core::Dtype vertex_dtype=core::Dtype::Float32, core::Dtype triangle_dtype=core::Dtype::Int64, const core::Device &device=core::Device("CPU:0"))
Construct an empty trianglemesh.
Definition: TriangleMesh.cpp:42
A TriangleMesh contains vertices and triangles.
Definition: TriangleMesh.h:107
void SetVertexNormals(const core::TensorList &value)
Definition: TriangleMesh.h:253
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
const core::TensorList & GetTriangleColors() const
Definition: TriangleMesh.h:223
bool HasTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:320
TriangleMesh & Translate(const core::Tensor &translation, bool relative=true)
Definition: TriangleMesh.h:392
void VertexSynchronizedPushBack(const std::unordered_map< std::string, core::Tensor > &map_keys_to_tensors)
Definition: TriangleMesh.h:352
Definition: TensorListMap.h:44
bool HasVertices() const
Definition: TriangleMesh.h:300
Definition: TensorList.h:58
void SetTriangleAttr(const std::string &key, const core::TensorList &value)
Definition: TriangleMesh.h:263
core::TensorList & GetTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:158
TriangleMesh & Rotate(const core::Tensor &R, const core::Tensor &center)
Definition: TriangleMesh.h:401
bool HasTriangles() const
Definition: TriangleMesh.h:329
void TriangleSynchronizedPushBack(const std::unordered_map< std::string, core::Tensor > &map_keys_to_tensors)
Definition: TriangleMesh.h:365
bool IsEmpty() const override
Returns !HasVertices(), triangles are ignored.
Definition: TriangleMesh.h:380
void SetVertices(const core::TensorList &value)
Definition: TriangleMesh.h:239
const core::TensorList & GetTriangleNormals() const
Definition: TriangleMesh.h:217
Definition: Device.h:39
const core::TensorList & GetVertexColors() const
Definition: TriangleMesh.h:191
core::Tensor GetMinBound() const
Definition: TriangleMesh.h:382
core::TensorList & GetVertexAttr(const std::string &key)
Definition: TriangleMesh.h:138
core::TensorList & GetVertexColors()
Definition: TriangleMesh.h:148
void AssertDevice(const Device &expected_device) const
Definition: TensorList.h:257
core::TensorList & GetTriangleColors()
Definition: TriangleMesh.h:174
static const Dtype Float32
Definition: Dtype.h:42
void AssertElementShape(const SizeVector &expected_element_shape) const
Definition: TensorList.h:248
The base geometry class.
Definition: Geometry.h:36
void SetVertexColors(const core::TensorList &value)
Definition: TriangleMesh.h:246
static const Dtype Int64
Definition: Dtype.h:45
const core::TensorList & GetVertices() const
Definition: TriangleMesh.h:185
Definition: PinholeCameraIntrinsic.cpp:35
void SetTriangleNormals(const core::TensorList &value)
Definition: TriangleMesh.h:277
Definition: Tensor.h:48
core::Device device_
Definition: TriangleMesh.h:421
void SetTriangleColors(const core::TensorList &value)
Definition: TriangleMesh.h:284
void SetVertexAttr(const std::string &key, const core::TensorList &value)
Definition: TriangleMesh.h:232
void SetTriangles(const core::TensorList &value)
Set the vlaue of the "triangles" attribute in triangle_attr_.
Definition: TriangleMesh.h:270
static geometry::TriangleMesh FromLegacyTrangleMesh(const geometry::TriangleMesh &mesh_legacy, core::Dtype dtype=core::Dtype::Float32, const core::Device &device=core::Device("CPU:0"))
Create a TriangleMesh from a legacy Open3D TriangleMesh.
Definition: TriangleMesh.h:408
bool HasVertexNormals() const
Definition: TriangleMesh.h:314
core::TensorList & GetVertexNormals()
Definition: TriangleMesh.h:152
TensorListMap vertex_attr_
Definition: TriangleMesh.h:422
core::TensorList & GetTriangleNormals()
Definition: TriangleMesh.h:168
bool Contains(const std::string &key) const
Definition: TensorListMap.h:97
bool HasVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:293
bool HasTriangleColors() const
Definition: TriangleMesh.h:343
bool HasTriangleNormals() const
Definition: TriangleMesh.h:336
TriangleMesh & Scale(double scale, const core::Tensor &center)
Definition: TriangleMesh.h:397
core::Tensor GetCenter() const
Definition: TriangleMesh.h:386
const core::TensorList & GetVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:179
const core::TensorList & GetTriangles() const
Definition: TriangleMesh.h:211
core::Tensor GetMaxBound() const
Definition: TriangleMesh.h:384