SaDVIO
mesh.h
Go to the documentation of this file.
1 #ifndef MESH_H
2 #define MESH_H
3 
4 #include <thread>
5 #include <vector>
6 
8 #include "isaeslam/data/frame.h"
10 #include "utilities/geometry.h"
12 
13 namespace isae {
14 
16 typedef std::vector<std::shared_ptr<AFeature>> FeatPolygon;
17 
19 typedef std::vector<std::shared_ptr<ALandmark>> LmkPolygon;
20 
21 class Mesh3D;
22 struct Vertex;
23 struct Polygon;
24 
32 class Mesh3D {
33  public:
34  Mesh3D() = default;
35  Mesh3D(double ZNCC_tsh, double max_length_tsh) : _ZNCC_tsh(ZNCC_tsh), _max_length_tsh(max_length_tsh) {}
36  ~Mesh3D() = default;
37 
38  std::vector<std::shared_ptr<Polygon>> getPolygonVector() const {
39  std::lock_guard<std::mutex> lock(_mesh_mtx);
40  return _polygons;
41  }
42  std::vector<Eigen::Vector3d> getPointCloud() const {
43  std::lock_guard<std::mutex> lock(_pc_mtx);
44  return _point_cloud;
45  }
46  std::shared_ptr<Frame> getFrame() const {
47  std::lock_guard<std::mutex> lock(_mesh_mtx);
48  return _reference_frame;
49  }
50 
51  std::unordered_map<std::shared_ptr<ALandmark>, std::shared_ptr<Vertex>> getMap() { return _map_lmk_vertex; }
52 
58  void updateMesh(std::vector<FeatPolygon> feats_polygon, std::shared_ptr<Frame> frame);
59 
63  bool checkTriangle(std::vector<std::shared_ptr<Vertex>> vertices);
64 
68  bool checkPolygon(std::shared_ptr<Polygon> polygon);
69 
74  bool checkPolygonArea(std::shared_ptr<Polygon> polygon, double area2d);
75 
79  bool checkPolygonTri(std::shared_ptr<Polygon> polygon3d, FeatPolygon polygon2d);
80  void removePolygon(std::shared_ptr<Polygon> polygon) {
81  std::lock_guard<std::mutex> lock(_mesh_mtx);
82  for (int i = _polygons.size() - 1; i >= 0; i--) {
83  if (_polygons.at(i) == polygon) {
84  _polygons.erase(_polygons.begin() + i);
85  }
86  }
87  }
88 
92  void analysePolygon(std::shared_ptr<Polygon> polygon);
93 
97  void filterMesh();
98 
102  void projectMesh();
103 
107  void generatePointCloud();
108 
109  mutable std::mutex _mesh_mtx;
110  mutable std::mutex _pc_mtx;
111 
112  private:
113  std::unordered_map<std::shared_ptr<ALandmark>, std::shared_ptr<Vertex>>
114  _map_lmk_vertex;
115  std::vector<std::shared_ptr<Polygon>> _polygons;
116  std::vector<Eigen::Vector3d> _point_cloud;
117  std::unordered_map<std::shared_ptr<Polygon>, std::vector<Eigen::Vector2d>>
118  _map_poly_tri2d;
119 
120  // Storage of frame related objects
121  std::shared_ptr<Frame> _reference_frame;
122  std::shared_ptr<ImageSensor> _cam0, _cam1;
123  cv::Mat _img0, _img1;
124  Eigen::Affine3d _T_w_cam0;
125 
126  // Tuning parameters
127  double _ZNCC_tsh = 0.8;
128  double _max_length_tsh = 5;
129 };
130 
136 struct Vertex {
137  public:
138  Vertex() {}
139 
140  Vertex(std::shared_ptr<ALandmark> lmk) : _lmk(lmk) {}
141  ~Vertex() = default;
142 
143  Eigen::Vector3d getVertexPosition() const {
144  std::lock_guard<std::mutex> lock(_lmk_mtx);
145  return _lmk->getPose().translation();
146  }
147  Eigen::Vector3d getVertexNormal() const { return _vertex_normal; }
148  std::vector<std::shared_ptr<Polygon>> getPolygons() const { return _polygons; }
149  std::shared_ptr<ALandmark> getLmk() const { return _lmk; }
150 
151  void addPolygon(std::shared_ptr<Polygon> polygon) { _polygons.push_back(polygon); }
152  void removePolygon(std::shared_ptr<Polygon> polygon) {
153  for (int i = _polygons.size() - 1; i >= 0; i--) {
154  if (_polygons.at(i) == polygon) {
155  _polygons.erase(_polygons.begin() + i);
156  }
157  }
158  }
159 
160  private:
161  std::shared_ptr<ALandmark> _lmk;
162  Eigen::Vector3d _vertex_normal;
163  std::vector<std::shared_ptr<Polygon>> _polygons;
164 
165  mutable std::mutex _lmk_mtx;
166 };
167 
173 struct Polygon : std::enable_shared_from_this<Polygon> {
174  public:
175  Polygon() {}
176 
177  Polygon(std::vector<std::shared_ptr<Vertex>> vertices) : _vertices(vertices) { _outlier = false; }
178  ~Polygon() = default;
179 
180  void setNormal(Eigen::Vector3d normal) { _normal = normal; }
181  void setBarycenter(Eigen::Vector3d barycenter) { _barycenter = barycenter; }
182  void setCovariance(Eigen::Matrix2d covariance) { _covariance = covariance; }
183  void setScore(double score) { _traversability_score = score; }
184  void setOutlier() { _outlier = true; }
185 
186  Eigen::Vector3d getPolygonNormal() const { return _normal; }
187  Eigen::Vector3d getBarycenter() const { return _barycenter; }
188  Eigen::Matrix2d getCovariance() const { return _covariance; }
189  std::vector<std::shared_ptr<Vertex>> getVertices() const { return _vertices; }
190  double getScore() const { return _traversability_score; }
191  bool isOutlier() { return _outlier; }
192 
193  private:
194  Eigen::Vector3d _normal;
195  Eigen::Vector3d _barycenter;
196  Eigen::Matrix2d _covariance;
197  double _traversability_score;
198  bool _outlier;
199  std::vector<std::shared_ptr<Vertex>> _vertices;
200 };
201 
202 } // namespace isae
203 
204 #endif // MESH_H
isae::Mesh3D::analysePolygon
void analysePolygon(std::shared_ptr< Polygon > polygon)
Compute the barycenter and the normal of a polygon.
Definition: mesh.cpp:140
isae::Vertex::getPolygons
std::vector< std::shared_ptr< Polygon > > getPolygons() const
Definition: mesh.h:148
isae::Mesh3D::generatePointCloud
void generatePointCloud()
Generate a point cloud from the mesh with raycasting.
Definition: mesh.cpp:538
isae::Polygon::getBarycenter
Eigen::Vector3d getBarycenter() const
Definition: mesh.h:187
isae::Polygon::setScore
void setScore(double score)
Definition: mesh.h:183
isae::Polygon::getPolygonNormal
Eigen::Vector3d getPolygonNormal() const
Definition: mesh.h:186
isae::Vertex::Vertex
Vertex(std::shared_ptr< ALandmark > lmk)
Definition: mesh.h:140
geometry.h
isae::Vertex::getLmk
std::shared_ptr< ALandmark > getLmk() const
Definition: mesh.h:149
isae::Mesh3D::Mesh3D
Mesh3D()=default
isae::Vertex::~Vertex
~Vertex()=default
isae::Vertex
A vertex in the 3D mesh, representing a landmark and its associated polygons.
Definition: mesh.h:136
isae::Mesh3D::getFrame
std::shared_ptr< Frame > getFrame() const
Definition: mesh.h:46
isae::Mesh3D::updateMesh
void updateMesh(std::vector< FeatPolygon > feats_polygon, std::shared_ptr< Frame > frame)
Update the 3D mesh with a 2D Mesh of features for a given frame.
Definition: mesh.cpp:5
isae::Mesh3D::checkPolygonArea
bool checkPolygonArea(std::shared_ptr< Polygon > polygon, double area2d)
EXPERIMENTAL Photometric check of a polygon using ZNCC on a patch on the barycenter of the polygon of...
Definition: mesh.cpp:346
isae::Polygon::isOutlier
bool isOutlier()
Definition: mesh.h:191
isae::Mesh3D::getPolygonVector
std::vector< std::shared_ptr< Polygon > > getPolygonVector() const
Definition: mesh.h:38
isae::Mesh3D::checkPolygonTri
bool checkPolygonTri(std::shared_ptr< Polygon > polygon3d, FeatPolygon polygon2d)
EXPERIMENTAL Photometric check of a polygon using ZNCC on a patch with the full 2D polygon in it.
Definition: mesh.cpp:256
isae::Polygon::setNormal
void setNormal(Eigen::Vector3d normal)
Definition: mesh.h:180
isae::Vertex::getVertexNormal
Eigen::Vector3d getVertexNormal() const
Definition: mesh.h:147
AFeature2D.h
isae::Polygon::~Polygon
~Polygon()=default
isae::Vertex::addPolygon
void addPolygon(std::shared_ptr< Polygon > polygon)
Definition: mesh.h:151
isae::Vertex::Vertex
Vertex()
Definition: mesh.h:138
isae::Polygon::setBarycenter
void setBarycenter(Eigen::Vector3d barycenter)
Definition: mesh.h:181
isae::Mesh3D::getPointCloud
std::vector< Eigen::Vector3d > getPointCloud() const
Definition: mesh.h:42
isae::FeatPolygon
std::vector< std::shared_ptr< AFeature > > FeatPolygon
A vector of features for 2D Meshing.
Definition: mesh.h:16
imgProcessing.h
isae::Mesh3D::~Mesh3D
~Mesh3D()=default
isae::Mesh3D::checkPolygon
bool checkPolygon(std::shared_ptr< Polygon > polygon)
Photometric check of a polygon using ZNCC on a patch on the barycenter of the polygon.
Definition: mesh.cpp:431
isae::LmkPolygon
std::vector< std::shared_ptr< ALandmark > > LmkPolygon
A vector of landmarks for 3D Meshing.
Definition: mesh.h:19
ALandmark.h
isae::Polygon::Polygon
Polygon()
Definition: mesh.h:175
isae::Polygon::setOutlier
void setOutlier()
Definition: mesh.h:184
isae::Polygon::getCovariance
Eigen::Matrix2d getCovariance() const
Definition: mesh.h:188
isae::Mesh3D::filterMesh
void filterMesh()
Remove polygon outliers from the 3D mesh.
Definition: mesh.cpp:100
isae::Mesh3D::removePolygon
void removePolygon(std::shared_ptr< Polygon > polygon)
Definition: mesh.h:80
isae::Polygon
A polygon in the 3D mesh, representing a surface with its vertices, normal, barycenter,...
Definition: mesh.h:173
isae::Mesh3D
A class to build and update a 3D mesh from 2D features and landmarks.
Definition: mesh.h:32
isae::Polygon::getScore
double getScore() const
Definition: mesh.h:190
frame.h
isae
Definition: AFeature2D.h:8
isae::Polygon::Polygon
Polygon(std::vector< std::shared_ptr< Vertex >> vertices)
Definition: mesh.h:177
isae::Polygon::setCovariance
void setCovariance(Eigen::Matrix2d covariance)
Definition: mesh.h:182
isae::Mesh3D::Mesh3D
Mesh3D(double ZNCC_tsh, double max_length_tsh)
Definition: mesh.h:35
isae::Polygon::getVertices
std::vector< std::shared_ptr< Vertex > > getVertices() const
Definition: mesh.h:189
isae::Mesh3D::getMap
std::unordered_map< std::shared_ptr< ALandmark >, std::shared_ptr< Vertex > > getMap()
Definition: mesh.h:51
isae::Vertex::getVertexPosition
Eigen::Vector3d getVertexPosition() const
Definition: mesh.h:143
isae::Mesh3D::_mesh_mtx
std::mutex _mesh_mtx
Definition: mesh.h:109
isae::Vertex::removePolygon
void removePolygon(std::shared_ptr< Polygon > polygon)
Definition: mesh.h:152
isae::Mesh3D::projectMesh
void projectMesh()
Project the mesh on camera 0 of the current frame for raycasting.
Definition: mesh.cpp:223
isae::Mesh3D::_pc_mtx
std::mutex _pc_mtx
Definition: mesh.h:110
isae::Mesh3D::checkTriangle
bool checkTriangle(std::vector< std::shared_ptr< Vertex >> vertices)
Check if a triangle is valid. (i.e. no accute angles or too long edges)
Definition: mesh.cpp:188