ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvMeshBase.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "ecvMeshBase.h"
9 
10 // LOCAL
11 #include <Logging.h>
12 
13 #include "ecvBBox.h"
14 #include "ecvHObjectCaster.h"
15 #include "ecvMesh.h"
16 #include "ecvOrientedBBox.h"
17 #include "ecvPointCloud.h"
18 #include "ecvQhull.h"
19 
20 // SYSTEM
21 #include <Eigen/Dense>
22 #include <array>
23 #include <numeric>
24 #include <queue>
25 #include <random>
26 #include <tuple>
27 #include <unordered_map>
28 
29 namespace cloudViewer {
30 namespace geometry {
31 
32 ccBBox ecvMeshBase::getOwnBB(bool withGLFeatures) {
34 }
35 
37  bbMin = GetMinBound();
38  bbMax = GetMaxBound();
39 }
40 
42  vertices_.clear();
43  vertex_normals_.clear();
44  vertex_colors_.clear();
45  return *this;
46 }
47 
48 bool ecvMeshBase::IsEmpty() const { return !HasVertices(); }
49 
50 Eigen::Vector3d ecvMeshBase::GetMinBound() const {
51  return ComputeMinBound(vertices_);
52 }
53 
54 Eigen::Vector3d ecvMeshBase::GetMaxBound() const {
55  return ComputeMaxBound(vertices_);
56 }
57 
58 Eigen::Vector3d ecvMeshBase::GetCenter() const {
59  return ComputeCenter(vertices_);
60 }
61 
64 }
65 
68 }
69 
70 ecvMeshBase &ecvMeshBase::Transform(const Eigen::Matrix4d &transformation) {
71  TransformPoints(transformation, vertices_);
72  TransformNormals(transformation, vertex_normals_);
73  return *this;
74 }
75 
76 ecvMeshBase &ecvMeshBase::Translate(const Eigen::Vector3d &translation,
77  bool relative) {
78  TranslatePoints(translation, vertices_, relative);
79  return *this;
80 }
81 
82 ecvMeshBase &ecvMeshBase::Scale(const double s, const Eigen::Vector3d &center) {
83  ScalePoints(s, vertices_, center);
84  return *this;
85 }
86 
87 ecvMeshBase &ecvMeshBase::Rotate(const Eigen::Matrix3d &R,
88  const Eigen::Vector3d &center) {
89  RotatePoints(R, vertices_, center);
91  return *this;
92 }
93 
95  if (mesh.IsEmpty()) return (*this);
96  size_t old_vert_num = vertices_.size();
97  size_t add_vert_num = mesh.vertices_.size();
98  size_t new_vert_num = old_vert_num + add_vert_num;
99  if ((!HasVertices() || HasVertexNormals()) && mesh.HasVertexNormals()) {
100  vertex_normals_.resize(new_vert_num);
101  for (size_t i = 0; i < add_vert_num; i++)
102  vertex_normals_[old_vert_num + i] = mesh.vertex_normals_[i];
103  } else {
104  vertex_normals_.clear();
105  }
106  if ((!HasVertices() || HasVertexColors()) && mesh.HasVertexColors()) {
107  vertex_colors_.resize(new_vert_num);
108  for (size_t i = 0; i < add_vert_num; i++)
109  vertex_colors_[old_vert_num + i] = mesh.vertex_colors_[i];
110  } else {
111  vertex_colors_.clear();
112  }
113  vertices_.resize(new_vert_num);
114  for (size_t i = 0; i < add_vert_num; i++)
115  vertices_[old_vert_num + i] = mesh.vertices_[i];
116  return (*this);
117 }
118 
120  return (ecvMeshBase(*this) += mesh);
121 }
122 
123 std::tuple<std::shared_ptr<ccMesh>, std::vector<size_t>>
126 }
127 
128 } // namespace geometry
129 } // namespace cloudViewer
Bounding box structure.
Definition: ecvBBox.h:25
static ccBBox CreateFromPoints(const std::vector< CCVector3 > &points)
Definition: ecvBBox.cpp:82
static Eigen::Vector3d ComputeMinBound(const std::vector< Eigen::Vector3d > &points)
Compute min bound of a list points.
Definition: ecvHObject.cpp:272
static Eigen::Vector3d ComputeCenter(const std::vector< Eigen::Vector3d > &points)
Computer center of a list of points.
Definition: ecvHObject.cpp:296
static void RotateNormals(const Eigen::Matrix3d &R, std::vector< Eigen::Vector3d > &normals)
Rotate all normals with the rotation matrix R.
Definition: ecvHObject.cpp:361
static void TransformNormals(const Eigen::Matrix4d &transformation, std::vector< Eigen::Vector3d > &normals)
Transforms the normals with the transformation matrix.
Definition: ecvHObject.cpp:317
static void TranslatePoints(const Eigen::Vector3d &translation, std::vector< Eigen::Vector3d > &points, bool relative)
Apply translation to the geometry coordinates.
Definition: ecvHObject.cpp:333
static void RotatePoints(const Eigen::Matrix3d &R, std::vector< Eigen::Vector3d > &points, const Eigen::Vector3d &center)
Rotate all points with the rotation matrix R.
Definition: ecvHObject.cpp:353
static void TransformPoints(const Eigen::Matrix4d &transformation, std::vector< Eigen::Vector3d > &points)
Transforms all points with the transformation matrix.
Definition: ecvHObject.cpp:307
static void ScalePoints(const double scale, std::vector< Eigen::Vector3d > &points, const Eigen::Vector3d &center)
Scale the coordinates of all points by the scaling factor scale.
Definition: ecvHObject.cpp:345
static Eigen::Vector3d ComputeMaxBound(const std::vector< Eigen::Vector3d > &points)
Compute max bound of a list points.
Definition: ecvHObject.cpp:284
static std::tuple< std::shared_ptr< ccMesh >, std::vector< size_t > > ComputeConvexHull(const std::vector< Eigen::Vector3d > &points)
Definition: ecvQhull.cpp:34
virtual ecvMeshBase & clear()
Definition: ecvMeshBase.cpp:41
virtual Eigen::Vector3d GetMaxBound() const override
Returns max bounds for geometry coordinates.
Definition: ecvMeshBase.cpp:54
virtual ecvMeshBase & Transform(const Eigen::Matrix4d &transformation) override
Apply transformation (4x4 matrix) to the geometry coordinates.
Definition: ecvMeshBase.cpp:70
ecvMeshBase(const char *name="ecvMeshBase")
Default Constructor.
Definition: ecvMeshBase.h:44
virtual Eigen::Vector3d GetCenter() const override
Returns the center of the geometry coordinates.
Definition: ecvMeshBase.cpp:58
virtual ccBBox getOwnBB(bool withGLFeatures=false) override
Returns the entity's own bounding-box.
Definition: ecvMeshBase.cpp:32
virtual void getBoundingBox(CCVector3 &bbMin, CCVector3 &bbMax) override
Returns the mesh bounding-box.
Definition: ecvMeshBase.cpp:36
bool HasVertices() const
Returns True if the mesh contains vertices.
Definition: ecvMeshBase.h:87
bool HasVertexColors() const
Returns True if the mesh contains vertex colors.
Definition: ecvMeshBase.h:96
virtual ecvMeshBase & Translate(const Eigen::Vector3d &translation, bool relative=true) override
Apply translation to the geometry coordinates.
Definition: ecvMeshBase.cpp:76
ecvMeshBase & operator+=(const ecvMeshBase &mesh)
Definition: ecvMeshBase.cpp:94
bool HasVertexNormals() const
Returns True if the mesh contains vertex normals.
Definition: ecvMeshBase.h:90
ecvMeshBase operator+(const ecvMeshBase &mesh) const
virtual bool IsEmpty() const override
Definition: ecvMeshBase.cpp:48
std::tuple< std::shared_ptr< ccMesh >, std::vector< size_t > > ComputeConvexHull() const
Function that computes the convex hull of the triangle mesh using qhull.
virtual ecvOrientedBBox GetOrientedBoundingBox() const override
Definition: ecvMeshBase.cpp:66
virtual ecvMeshBase & Scale(const double s, const Eigen::Vector3d &center) override
Apply scaling to the geometry coordinates. Given a scaling factor , and center , a given point is tr...
Definition: ecvMeshBase.cpp:82
virtual ecvMeshBase & 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: ecvMeshBase.cpp:87
std::vector< Eigen::Vector3d > vertex_normals_
Vertex normals.
Definition: ecvMeshBase.h:134
std::vector< Eigen::Vector3d > vertices_
Vertex coordinates.
Definition: ecvMeshBase.h:132
std::vector< Eigen::Vector3d > vertex_colors_
RGB colors of vertices.
Definition: ecvMeshBase.h:136
virtual Eigen::Vector3d GetMinBound() const override
Returns min bounds for geometry coordinates.
Definition: ecvMeshBase.cpp:50
virtual ccBBox GetAxisAlignedBoundingBox() const override
Returns an axis-aligned bounding box of the geometry.
Definition: ecvMeshBase.cpp:62
static ecvOrientedBBox CreateFromPoints(const std::vector< Eigen::Vector3d > &points)
Generic file read and write utility for python interface.