Open3D (C++ API)  0.15.1
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
36namespace open3d {
37namespace t {
38namespace geometry {
39
106class TriangleMesh : public Geometry, public DrawableGeometry {
107public:
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
126public:
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
154
158
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
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) {
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
371public:
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
383
385
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
449protected:
453};
454
455} // namespace geometry
456} // namespace t
457} // namespace open3d
#define AssertTensorDevice(tensor,...)
Definition: TensorCheck.h:62
#define AssertTensorShape(tensor,...)
Definition: TensorCheck.h:77
Definition: Device.h:39
Definition: Dtype.h:39
Definition: Tensor.h:51
int64_t GetLength() const
Definition: Tensor.h:1089
Tensor Min(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1198
Tensor Mean(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1177
Tensor Max(const SizeVector &dims, bool keepdim=false) const
Definition: Tensor.cpp:1205
Triangle mesh contains vertices and triangles represented by the indices to the vertices.
Definition: TriangleMesh.h:54
Mix-in class for geometry types that can be visualized.
Definition: DrawableGeometry.h:38
The base geometry class.
Definition: Geometry.h:38
Definition: TensorMap.h:49
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 Contains(const std::string &key) const
Definition: TensorMap.h:134
A triangle mesh contains vertices and triangles.
Definition: TriangleMesh.h:106
core::Tensor & GetVertexAttr(const std::string &key)
Definition: TriangleMesh.h:147
void SetTriangleIndices(const core::Tensor &value)
Set the vlaue of the "indices" attribute in triangle_attr_.
Definition: TriangleMesh.h:293
core::Tensor GetMinBound() const
Definition: TriangleMesh.h:382
void SetVertexPositions(const core::Tensor &value)
Definition: TriangleMesh.h:263
void SetVertexAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:256
core::Tensor & GetTriangleNormals()
Definition: TriangleMesh.h:180
core::Tensor & GetTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:170
void SetVertexColors(const core::Tensor &value)
Definition: TriangleMesh.h:270
TensorMap triangle_attr_
Definition: TriangleMesh.h:452
void SetTriangleColors(const core::Tensor &value)
Definition: TriangleMesh.h:307
virtual ~TriangleMesh() override
Definition: TriangleMesh.h:124
bool HasTriangleIndices() const
Definition: TriangleMesh.h:355
bool HasVertexColors() const
Definition: TriangleMesh.h:332
void SetTriangleAttr(const std::string &key, const core::Tensor &value)
Definition: TriangleMesh.h:287
core::Device device_
Definition: TriangleMesh.h:450
TriangleMesh & Clear() override
Clear all data in the trianglemesh.
Definition: TriangleMesh.h:373
void RemoveTriangleAttr(const std::string &key)
Definition: TriangleMesh.h:229
bool HasTriangleNormals() const
Definition: TriangleMesh.h:362
core::Device GetDevice() const
Definition: TriangleMesh.h:431
bool HasVertexNormals() const
Definition: TriangleMesh.h:339
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 RemoveVertexAttr(const std::string &key)
Definition: TriangleMesh.h:197
TriangleMesh To(const core::Device &device, bool copy=false) const
Definition: TriangleMesh.cpp:259
open3d::geometry::TriangleMesh ToLegacy() const
Convert to a legacy Open3D TriangleMesh.
Definition: TriangleMesh.cpp:218
const core::Tensor & GetTriangleNormals() const
Definition: TriangleMesh.h:241
const TensorMap & GetTriangleAttr() const
Getter for triangle_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:164
TriangleMesh Clone() const
Returns copy of the triangle mesh on the same device.
Definition: TriangleMesh.h:138
core::Tensor GetMaxBound() const
Definition: TriangleMesh.h:384
TriangleMesh & Rotate(const core::Tensor &R, const core::Tensor &center)
Rotates the VertexPositions, VertexNormals and TriangleNormals (if exists).
Definition: TriangleMesh.cpp:152
core::Tensor & GetVertexColors()
Definition: TriangleMesh.h:157
core::Tensor & GetTriangleIndices()
Definition: TriangleMesh.h:176
const core::Tensor & GetTriangleIndices() const
Definition: TriangleMesh.h:235
TriangleMesh(const core::Device &device=core::Device("CPU:0"))
Definition: TriangleMesh.cpp:44
const core::Tensor & GetVertexColors() const
Definition: TriangleMesh.h:207
TriangleMesh & Scale(double scale, const core::Tensor &center)
Scales the VertexPositions of the TriangleMesh.
Definition: TriangleMesh.cpp:141
void SetTriangleNormals(const core::Tensor &value)
Definition: TriangleMesh.h:300
core::Tensor & GetVertexPositions()
Definition: TriangleMesh.h:153
bool HasTriangleColors() const
Definition: TriangleMesh.h:369
const core::Tensor & GetTriangleColors() const
Definition: TriangleMesh.h:247
const core::Tensor & GetVertexPositions() const
Definition: TriangleMesh.h:201
bool HasTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:345
bool HasVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:316
const core::Tensor & GetVertexAttr(const std::string &key) const
Definition: TriangleMesh.h:189
TriangleMesh & Transform(const core::Tensor &transformation)
Transforms the VertexPositions, VertexNormals and TriangleNormals (if exist) of the TriangleMesh.
Definition: TriangleMesh.cpp:112
const TensorMap & GetVertexAttr() const
Getter for vertex_attr_ TensorMap. Used in Pybind.
Definition: TriangleMesh.h:141
void SetVertexNormals(const core::Tensor &value)
Definition: TriangleMesh.h:277
const core::Tensor & GetTriangleAttr(const std::string &key) const
Definition: TriangleMesh.h:221
core::Tensor GetCenter() const
Definition: TriangleMesh.h:386
TensorMap vertex_attr_
Definition: TriangleMesh.h:451
core::Tensor & GetTriangleColors()
Definition: TriangleMesh.h:184
std::string ToString() const
Text description.
Definition: TriangleMesh.cpp:66
core::Tensor & GetVertexNormals()
Definition: TriangleMesh.h:161
const core::Tensor & GetVertexNormals() const
Definition: TriangleMesh.h:213
bool HasVertexPositions() const
Definition: TriangleMesh.h:325
TriangleMesh & Translate(const core::Tensor &translation, bool relative=true)
Translates the VertexPositions of the TriangleMesh.
Definition: TriangleMesh.cpp:127
bool IsEmpty() const override
Returns !HasVertexPositions(), triangles are ignored.
Definition: TriangleMesh.h:380
const Dtype Int64
Definition: Dtype.cpp:66
const Dtype Float32
Definition: Dtype.cpp:61
constexpr nullopt_t nullopt
Definition: Optional.h:171
Definition: PinholeCameraIntrinsic.cpp:35