Open3D (C++ API)
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 <vector>
30 #include <unordered_set>
31 #include <memory>
32 #include <Eigen/Core>
33 
35 
36 namespace open3d {
37 namespace geometry {
38 
39 class TriangleMesh : public Geometry3D {
40 public:
42  ~TriangleMesh() override{};
43 
44 public:
45  void Clear() override;
46  bool IsEmpty() const override;
47  Eigen::Vector3d GetMinBound() const override;
48  Eigen::Vector3d GetMaxBound() const override;
49  void Transform(const Eigen::Matrix4d &transformation) override;
50 
51 public:
52  TriangleMesh &operator+=(const TriangleMesh &mesh);
53  TriangleMesh operator+(const TriangleMesh &mesh) const;
54 
56  void ComputeTriangleNormals(bool normalized = true);
57 
59  void ComputeVertexNormals(bool normalized = true);
60 
62  void ComputeAdjacencyList();
63 
65  void Purge();
66 
67 protected:
68  // Forward child class type to avoid indirect nonvirtual base
70  virtual void RemoveDuplicatedVertices();
71  virtual void RemoveDuplicatedTriangles();
72  virtual void RemoveNonManifoldVertices();
73  virtual void RemoveNonManifoldTriangles();
74 
75 public:
76  bool HasVertices() const { return vertices_.size() > 0; }
77 
78  bool HasTriangles() const {
79  return vertices_.size() > 0 && triangles_.size() > 0;
80  }
81 
82  bool HasVertexNormals() const {
83  return vertices_.size() > 0 &&
84  vertex_normals_.size() == vertices_.size();
85  }
86 
87  bool HasVertexColors() const {
88  return vertices_.size() > 0 &&
89  vertex_colors_.size() == vertices_.size();
90  }
91 
92  bool HasTriangleNormals() const {
93  return HasTriangles() && triangles_.size() == triangle_normals_.size();
94  }
95 
96  bool HasAdjacencyList() const {
97  return vertices_.size() > 0 &&
98  adjacency_list_.size() == vertices_.size();
99  }
100 
102  for (size_t i = 0; i < vertex_normals_.size(); i++) {
103  vertex_normals_[i].normalize();
104  if (std::isnan(vertex_normals_[i](0))) {
105  vertex_normals_[i] = Eigen::Vector3d(0.0, 0.0, 1.0);
106  }
107  }
108  for (size_t i = 0; i < triangle_normals_.size(); i++) {
109  triangle_normals_[i].normalize();
110  if (std::isnan(triangle_normals_[i](0))) {
111  triangle_normals_[i] = Eigen::Vector3d(0.0, 0.0, 1.0);
112  }
113  }
114  }
115 
116  void PaintUniformColor(const Eigen::Vector3d &color) {
117  vertex_colors_.resize(vertices_.size());
118  for (size_t i = 0; i < vertices_.size(); i++) {
119  vertex_colors_[i] = color;
120  }
121  }
122 
123 public:
124  std::vector<Eigen::Vector3d> vertices_;
125  std::vector<Eigen::Vector3d> vertex_normals_;
126  std::vector<Eigen::Vector3d> vertex_colors_;
127  std::vector<Eigen::Vector3i> triangles_;
128  std::vector<Eigen::Vector3d> triangle_normals_;
129  std::vector<std::unordered_set<int>> adjacency_list_;
130 };
131 
135 std::shared_ptr<TriangleMesh> SelectDownSample(
136  const TriangleMesh &input, const std::vector<size_t> &indices);
137 
141 std::shared_ptr<TriangleMesh> CropTriangleMesh(
142  const TriangleMesh &input,
143  const Eigen::Vector3d &min_bound,
144  const Eigen::Vector3d &max_bound);
145 
150 std::shared_ptr<TriangleMesh> CreateMeshBox(double width = 1.0,
151  double height = 1.0,
152  double depth = 1.0);
153 
159 std::shared_ptr<TriangleMesh> CreateMeshSphere(double radius = 1.0,
160  int resolution = 20);
161 
166 std::shared_ptr<TriangleMesh> CreateMeshCylinder(double radius = 1.0,
167  double height = 2.0,
168  int resolution = 20,
169  int split = 4);
170 
175 std::shared_ptr<TriangleMesh> CreateMeshCone(double radius = 1.0,
176  double height = 2.0,
177  int resolution = 20,
178  int split = 1);
179 
190 std::shared_ptr<TriangleMesh> CreateMeshArrow(double cylinder_radius = 1.0,
191  double cone_radius = 1.5,
192  double cylinder_height = 5.0,
193  double cone_height = 4.0,
194  int resolution = 20,
195  int cylinder_split = 4,
196  int cone_split = 1);
197 
202 std::shared_ptr<TriangleMesh> CreateMeshCoordinateFrame(
203  double size = 1.0,
204  const Eigen::Vector3d &origin = Eigen::Vector3d(0.0, 0.0, 0.0));
205 
206 } // namespace geometry
207 } // namespace open3d
std::shared_ptr< TriangleMesh > CreateMeshSphere(double radius=1.0, int resolution=20)
Definition: TriangleMeshFactory.cpp:60
void ComputeVertexNormals(bool normalized=true)
Function to compute vertex normals, usually called before rendering.
Definition: TriangleMesh.cpp:180
std::vector< Eigen::Vector3d > vertex_colors_
Definition: TriangleMesh.h:126
std::vector< Eigen::Vector3i > triangles_
Definition: TriangleMesh.h:127
virtual void RemoveDuplicatedVertices()
Definition: TriangleMesh.cpp:216
Eigen::Vector3d GetMaxBound() const override
Definition: TriangleMesh.cpp:72
void PaintUniformColor(const Eigen::Vector3d &color)
Definition: TriangleMesh.h:116
std::shared_ptr< TriangleMesh > CropTriangleMesh(const TriangleMesh &input, const Eigen::Vector3d &min_bound, const Eigen::Vector3d &max_bound)
Definition: DownSample.cpp:488
Definition: Geometry.h:32
void ComputeTriangleNormals(bool normalized=true)
Function to compute triangle normals, usually called before rendering.
Definition: TriangleMesh.cpp:167
std::shared_ptr< TriangleMesh > CreateMeshArrow(double cylinder_radius=1.0, double cone_radius=1.5, double cylinder_height=5.0, double cone_height=4.0, int resolution=20, int cylinder_split=4, int cone_split=1)
Definition: TriangleMeshFactory.cpp:188
void Clear() override
Definition: TriangleMesh.cpp:39
virtual void RemoveDuplicatedTriangles()
Definition: TriangleMesh.cpp:258
std::vector< Eigen::Vector3d > vertices_
Definition: TriangleMesh.h:124
void Transform(const Eigen::Matrix4d &transformation) override
Definition: TriangleMesh.cpp:94
TriangleMesh()
Definition: TriangleMesh.h:41
bool HasVertices() const
Definition: TriangleMesh.h:76
bool IsEmpty() const override
Definition: TriangleMesh.cpp:48
std::shared_ptr< TriangleMesh > CreateMeshCone(double radius=1.0, double height=2.0, int resolution=20, int split=1)
Definition: TriangleMeshFactory.cpp:144
TriangleMesh & operator+=(const TriangleMesh &mesh)
Definition: TriangleMesh.cpp:117
int size
Definition: FilePCD.cpp:55
void NormalizeNormals()
Definition: TriangleMesh.h:101
std::shared_ptr< PointCloud > SelectDownSample(const PointCloud &input, const std::vector< size_t > &indices, bool invert)
Definition: DownSample.cpp:145
Definition: Geometry3D.h:35
std::shared_ptr< TriangleMesh > CreateMeshCylinder(double radius=1.0, double height=2.0, int resolution=20, int split=4)
Definition: TriangleMeshFactory.cpp:102
std::vector< Eigen::Vector3d > vertex_normals_
Definition: TriangleMesh.h:125
bool HasTriangles() const
Definition: TriangleMesh.h:78
TriangleMesh operator+(const TriangleMesh &mesh) const
Definition: TriangleMesh.cpp:163
std::shared_ptr< TriangleMesh > CreateMeshBox(double width=1.0, double height=1.0, double depth=1.0)
Definition: TriangleMeshFactory.cpp:32
void Purge()
Function to remove duplicated and non-manifold vertices/triangles.
Definition: TriangleMesh.cpp:209
char type
Definition: FilePCD.cpp:56
virtual void RemoveNonManifoldVertices()
Definition: TriangleMesh.cpp:303
virtual void RemoveNonManifoldTriangles()
Definition: TriangleMesh.cpp:346
bool HasAdjacencyList() const
Definition: TriangleMesh.h:96
std::shared_ptr< TriangleMesh > CreateMeshCoordinateFrame(double size=1.0, const Eigen::Vector3d &origin=Eigen::Vector3d(0.0, 0.0, 0.0))
Definition: TriangleMeshFactory.cpp:209
Definition: PinholeCameraIntrinsic.cpp:33
GeometryType
Definition: Geometry.h:34
~TriangleMesh() override
Definition: TriangleMesh.h:42
int height
Definition: FilePCD.cpp:68
std::vector< std::unordered_set< int > > adjacency_list_
Definition: TriangleMesh.h:129
std::vector< Eigen::Vector3d > triangle_normals_
Definition: TriangleMesh.h:128
bool HasVertexColors() const
Definition: TriangleMesh.h:87
bool HasVertexNormals() const
Definition: TriangleMesh.h:82
TriangleMesh(Geometry::GeometryType type)
Definition: TriangleMesh.h:69
Eigen::Vector3d GetMinBound() const override
Definition: TriangleMesh.cpp:50
bool HasTriangleNormals() const
Definition: TriangleMesh.h:92
Definition: TriangleMesh.h:39
void ComputeAdjacencyList()
Function to compute adjacency list, call before adjacency list is needed.
Definition: TriangleMesh.cpp:196
int width
Definition: FilePCD.cpp:67