Open3D (C++ API)
MeshBase.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 <Eigen/Core>
30 #include <memory>
31 #include <tuple>
32 #include <unordered_map>
33 #include <unordered_set>
34 #include <vector>
35 
37 #include "Open3D/Utility/Helper.h"
38 
39 namespace open3d {
40 namespace geometry {
41 
42 class PointCloud;
43 class TriangleMesh;
44 
51 class MeshBase : public Geometry3D {
52 public:
62 
70  enum class FilterScope { All, Color, Normal, Vertex };
71 
76  enum class DeformAsRigidAsPossibleEnergy { Spokes, Smoothed };
77 
80  ~MeshBase() override {}
81 
82 public:
83  virtual MeshBase &Clear() override;
84  virtual bool IsEmpty() const override;
85  virtual Eigen::Vector3d GetMinBound() const override;
86  virtual Eigen::Vector3d GetMaxBound() const override;
87  virtual Eigen::Vector3d GetCenter() const override;
88  virtual AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override;
89  virtual OrientedBoundingBox GetOrientedBoundingBox() const override;
90  virtual MeshBase &Transform(const Eigen::Matrix4d &transformation) override;
91  virtual MeshBase &Translate(const Eigen::Vector3d &translation,
92  bool relative = true) override;
93  virtual MeshBase &Scale(const double scale,
94  const Eigen::Vector3d &center) override;
95  virtual MeshBase &Rotate(const Eigen::Matrix3d &R,
96  const Eigen::Vector3d &center) override;
97 
98  MeshBase &operator+=(const MeshBase &mesh);
99  MeshBase operator+(const MeshBase &mesh) const;
100 
102  bool HasVertices() const { return vertices_.size() > 0; }
103 
105  bool HasVertexNormals() const {
106  return vertices_.size() > 0 &&
107  vertex_normals_.size() == vertices_.size();
108  }
109 
111  bool HasVertexColors() const {
112  return vertices_.size() > 0 &&
113  vertex_colors_.size() == vertices_.size();
114  }
115 
118  for (size_t i = 0; i < vertex_normals_.size(); i++) {
119  vertex_normals_[i].normalize();
120  if (std::isnan(vertex_normals_[i](0))) {
121  vertex_normals_[i] = Eigen::Vector3d(0.0, 0.0, 1.0);
122  }
123  }
124  return *this;
125  }
126 
130  MeshBase &PaintUniformColor(const Eigen::Vector3d &color) {
132  return *this;
133  }
134 
136  std::tuple<std::shared_ptr<TriangleMesh>, std::vector<size_t>>
137  ComputeConvexHull() const;
138 
139 protected:
140  // Forward child class type to avoid indirect nonvirtual base
143  const std::vector<Eigen::Vector3d> &vertices)
144  : Geometry3D(type), vertices_(vertices) {}
145 
146 public:
148  std::vector<Eigen::Vector3d> vertices_;
150  std::vector<Eigen::Vector3d> vertex_normals_;
152  std::vector<Eigen::Vector3d> vertex_colors_;
153 };
154 
155 } // namespace geometry
156 } // namespace open3d
MeshBase & operator+=(const MeshBase &mesh)
Definition: MeshBase.cpp:97
virtual AxisAlignedBoundingBox GetAxisAlignedBoundingBox() const override
Returns an axis-aligned bounding box of the geometry.
Definition: MeshBase.cpp:65
virtual Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
Definition: MeshBase.cpp:63
The base geometry class.
Definition: Geometry.h:37
virtual MeshBase & Rotate(const Eigen::Matrix3d &R, const Eigen::Vector3d &center) override
Apply rotation to the geometry coordinates and normals. Given a rotation matrix , and center ...
Definition: MeshBase.cpp:90
A bounding box that is aligned along the coordinate axes.
Definition: BoundingVolume.h:164
MeshBase()
Default Constructor.
Definition: MeshBase.h:79
MeshBase & PaintUniformColor(const Eigen::Vector3d &color)
Assigns each vertex in the TriangleMesh the same color.
Definition: MeshBase.h:130
MeshBash Class.
Definition: MeshBase.h:51
MeshBase & NormalizeNormals()
Normalize vertex normals to length 1.
Definition: MeshBase.h:117
A bounding box oriented along an arbitrary frame of reference.
Definition: BoundingVolume.h:44
Definition: TriangleMeshSimplification.cpp:42
bool HasVertexNormals() const
Returns True if the mesh contains vertex normals.
Definition: MeshBase.h:105
bool HasVertices() const
Returns True if the mesh contains vertices.
Definition: MeshBase.h:102
math::float4 color
Definition: LineSetBuffers.cpp:46
std::vector< Eigen::Vector3d > vertex_normals_
Vertex normals.
Definition: MeshBase.h:150
virtual Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
Definition: MeshBase.cpp:59
The base geometry class for 3D geometries.
Definition: Geometry3D.h:46
FilterScope
Indicates the scope of filter operations.
Definition: MeshBase.h:70
virtual MeshBase & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
Definition: MeshBase.cpp:73
virtual bool IsEmpty() const override
Returns true iff the geometry is empty.
Definition: MeshBase.cpp:53
SimplificationContraction
Indicates the method that is used for mesh simplification if multiple vertices are combined to a sing...
Definition: MeshBase.h:61
DeformAsRigidAsPossibleEnergy
Definition: MeshBase.h:76
char type
Definition: FilePCD.cpp:58
virtual MeshBase & Scale(const double scale, const Eigen::Vector3d &center) override
Apply scaling to the geometry coordinates. Given a scaling factor , and center , a given point is tr...
Definition: MeshBase.cpp:85
std::vector< Eigen::Vector3d > vertex_colors_
RGB colors of vertices.
Definition: MeshBase.h:152
MeshBase(Geometry::GeometryType type)
Definition: MeshBase.h:141
std::tuple< std::shared_ptr< TriangleMesh >, std::vector< size_t > > ComputeConvexHull() const
Function that computes the convex hull of the triangle mesh using qhull.
Definition: MeshBase.cpp:127
Definition: Open3DViewer.h:29
virtual OrientedBoundingBox GetOrientedBoundingBox() const override
Returns an oriented bounding box of the geometry.
Definition: MeshBase.cpp:69
GeometryType
Specifies possible geometry types.
Definition: Geometry.h:42
MeshBase operator+(const MeshBase &mesh) const
Definition: MeshBase.cpp:122
MeshBase(Geometry::GeometryType type, const std::vector< Eigen::Vector3d > &vertices)
Definition: MeshBase.h:142
void ResizeAndPaintUniformColor(std::vector< Eigen::Vector3d > &colors, const size_t size, const Eigen::Vector3d &color) const
Resizes the colors vector and paints a uniform color.
Definition: Geometry3D.cpp:71
~MeshBase() override
Definition: MeshBase.h:80
virtual Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
Definition: MeshBase.cpp:55
std::vector< Eigen::Vector3d > vertices_
Vertex coordinates.
Definition: MeshBase.h:148
virtual MeshBase & Clear() override
Clear all elements in the geometry.
Definition: MeshBase.cpp:46
virtual MeshBase & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
Definition: MeshBase.cpp:79
bool HasVertexColors() const
Returns True if the mesh contains vertex colors.
Definition: MeshBase.h:111