mesh_map.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020, Sebastian Pütz
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following
13  * disclaimer in the documentation and/or other materials provided
14  * with the distribution.
15  *
16  * 3. Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * authors:
34  * Sebastian Pütz <spuetz@uni-osnabrueck.de>
35  *
36  */
37 
38 #ifndef MESH_MAP__MESH_MAP_H
39 #define MESH_MAP__MESH_MAP_H
40 
41 #include <atomic>
42 #include <dynamic_reconfigure/server.h>
43 #include <geometry_msgs/Point.h>
45 #include <lvr2/io/HDF5IO.hpp>
46 #include <mesh_map/MeshMapConfig.h>
48 #include <mesh_msgs/MeshVertexCosts.h>
49 #include <mesh_msgs/MeshVertexColors.h>
50 #include <mutex>
51 #include <pluginlib/class_loader.h>
52 #include <std_msgs/ColorRGBA.h>
53 #include <tf2_ros/buffer.h>
54 #include <tuple>
55 #include "nanoflann.hpp"
56 #include "nanoflann_mesh_adaptor.h"
57 
58 namespace mesh_map
59 {
60 class MeshMap
61 {
62 public:
64 
66 
71  bool readMap();
72 
77  bool loadLayerPlugins();
78 
83  bool initLayerPlugins();
84 
91 
99  bool inTriangle(const Vector& pos, const lvr2::FaceHandle& face, const float& dist);
100 
107  lvr2::OptionalFaceHandle getContainingFace(Vector& position, const float& max_dist);
108 
115  boost::optional<std::tuple<lvr2::FaceHandle, std::array<mesh_map::Vector, 3>,
116  std::array<float, 3>>> searchContainingFace(Vector& position, const float& max_dist);
117 
121  void reconfigureCallback(mesh_map::MeshMapConfig& config, uint32_t level);
122 
126  void combineVertexCosts();
127 
133  void findContours(std::vector<std::vector<lvr2::VertexHandle>>& contours, int min_contour_size);
134 
140  void publishVertexCosts(const lvr2::VertexMap<float>& costs, const std::string& name);
141 
145  void publishVertexColors();
146 
150  void publishCostLayers();
151 
161  bool projectedBarycentricCoords(const Vector& p, const lvr2::FaceHandle& triangle,
162  std::array<float, 3>& barycentric_coords, float& dist);
163 
173  bool barycentricCoords(const Vector& p, const lvr2::FaceHandle& triangle, float& u, float& v, float& w);
174 
179  void layerChanged(const std::string& layer_name);
180 
187  void findLethalByContours(const int& min_contour_size, std::set<lvr2::VertexHandle>& lethals);
188 
192  const std::string getGlobalFrameID();
193 
197  inline bool isLethal(const lvr2::VertexHandle& vH);
198 
202  inline const geometry_msgs::Point toPoint(const Vector& vec);
203 
212  const std::array<lvr2::VertexHandle, 3>& vertices,
213  const std::array<float, 3>& barycentric_coords);
214 
222  float costAtPosition(const lvr2::DenseVertexMap<float>& costs, const std::array<lvr2::VertexHandle, 3>& vertices,
223  const std::array<float, 3>& barycentric_coords);
224 
231  float costAtPosition(const std::array<lvr2::VertexHandle, 3>& vertices,
232  const std::array<float, 3>& barycentric_coords);
233 
247  bool rayTriangleIntersect(const Vector& orig, const Vector& dir, const Vector& v0, const Vector& v1, const Vector& v2,
248  float& t, float& u, float& v, Vector& p);
249 
255  bool resetLayers();
256 
261  {
262  return vector_map;
263  };
264 
269  {
270  return *mesh_ptr;
271  }
272 
277  {
278  return vertex_costs;
279  }
280 
284  const std::string& mapFrame()
285  {
286  return global_frame;
287  }
288 
293  {
294  return face_normals;
295  }
296 
301  {
302  return vertex_normals;
303  }
304 
309  {
310  return edge_weights;
311  }
312 
317  {
318  return edge_distances;
319  }
320 
330  boost::optional<std::tuple<lvr2::FaceHandle, std::array<Vector, 3>, std::array<float, 3>>>
331  searchNeighbourFaces(const Vector& pos, const lvr2::FaceHandle& face,
332  const float& max_radius, const float& max_dist);
333 
343  bool meshAhead(Vector& vec, lvr2::FaceHandle& face, const float& step_width);
344 
349 
356  void publishDebugPoint(const Vector pos, const std_msgs::ColorRGBA& color, const std::string& name);
357 
364  void publishDebugFace(const lvr2::FaceHandle& face_handle, const std_msgs::ColorRGBA& color, const std::string& name);
365 
372  void publishVectorField(const std::string& name, const lvr2::DenseVertexMap<lvr2::BaseVector<float>>& vector_map,
373  const bool publish_face_vectors = false);
374 
383  void publishVectorField(const std::string& name, const lvr2::DenseVertexMap<lvr2::BaseVector<float>>& vector_map,
385  const std::function<float(float)>& cost_function = {},
386  const bool publish_face_vectors = false);
387 
392 
396  mesh_map::AbstractLayer::Ptr layer(const std::string& layer_name);
397 
398  std::shared_ptr<lvr2::AttributeMeshIOBase> mesh_io_ptr;
399  std::shared_ptr<lvr2::HalfEdgeMesh<Vector>> mesh_ptr;
400 
402 
403 private:
406 
408  std::map<std::string, mesh_map::AbstractLayer::Ptr> layer_names;
409 
411  std::vector<std::pair<std::string, mesh_map::AbstractLayer::Ptr>> layers;
412 
414  std::map<std::string, std::set<lvr2::VertexHandle>> lethal_indices;
415 
417  std::set<lvr2::VertexHandle> lethals;
418 
420  std::string global_frame;
421 
423  std::string srv_url;
424 
426  std::string srv_username;
427 
429  std::string srv_password;
430 
431  std::string mesh_layer;
432 
437  float bb_min_x;
438  float bb_min_y;
439  float bb_min_z;
440  float bb_max_x;
441  float bb_max_y;
442  float bb_max_z;
443 
444  std::string mesh_file;
445  std::string mesh_part;
446 
449 
452 
455 
458 
461 
464 
467 
470 
473 
476 
479 
482 
484  dynamic_reconfigure::Server<mesh_map::MeshMapConfig>::CallbackType config_callback;
485 
488 
491 
493  MeshMapConfig config;
494 
497 
500 
502  std::string uuid_str;
503 
505  std::mutex layer_mtx;
506 
511 
513  std::unique_ptr<NanoFlannMeshAdaptor> adaptor_ptr;
514 
516  std::unique_ptr<KDTree> kd_tree_ptr;
517 };
518 
519 } /* namespace mesh_map */
520 
521 #endif // MESH_NAVIGATION__MESH_MAP_H
boost::optional< std::tuple< lvr2::FaceHandle, std::array< Vector, 3 >, std::array< float, 3 > > > searchNeighbourFaces(const Vector &pos, const lvr2::FaceHandle &face, const float &max_radius, const float &max_dist)
Definition: mesh_map.cpp:919
bool meshAhead(Vector &vec, lvr2::FaceHandle &face, const float &step_width)
Definition: mesh_map.cpp:989
const lvr2::DenseVertexMap< Normal > & vertexNormals()
Returns the mesh&#39;s vertex normals.
Definition: mesh_map.h:300
ros::Publisher mesh_geometry_pub
publisher for the mesh geometry
Definition: mesh_map.h:472
bool loadLayerPlugins()
Loads all configures layer plugins.
Definition: mesh_map.cpp:267
void publishDebugPoint(const Vector pos, const std_msgs::ColorRGBA &color, const std::string &name)
Publishes a position as marker. Used for debug purposes.
Definition: mesh_map.cpp:649
pluginlib::ClassLoader< mesh_map::AbstractLayer > layer_loader
plugin class loader for for the layer plugins
Definition: mesh_map.h:405
bool readMap()
Reads in the mesh geometry, normals and cost values and publishes all as mesh_msgs.
Definition: mesh_map.cpp:105
const lvr2::HalfEdgeMesh< Vector > & mesh()
Returns the stored mesh.
Definition: mesh_map.h:268
ros::NodeHandle private_nh
private node handle within the mesh map namespace
Definition: mesh_map.h:496
boost::optional< std::tuple< lvr2::FaceHandle, std::array< mesh_map::Vector, 3 >, std::array< float, 3 > > > searchContainingFace(Vector &position, const float &max_dist)
Searches for a triangle which contains the given position with respect to the maximum distance...
Definition: mesh_map.cpp:1038
mesh_map::AbstractLayer::Ptr layer(const std::string &layer_name)
returns a shared pointer to the specified layer
Definition: mesh_map.cpp:1109
std::set< lvr2::VertexHandle > lethals
all impassable vertices
Definition: mesh_map.h:417
std::string mesh_part
Definition: mesh_map.h:445
std::string srv_password
login password to connect to the server
Definition: mesh_map.h:429
bool rayTriangleIntersect(const Vector &orig, const Vector &dir, const Vector &v0, const Vector &v1, const Vector &v2, float &t, float &u, float &v, Vector &p)
Definition: mesh_map.cpp:1120
bool inTriangle(const Vector &pos, const lvr2::FaceHandle &face, const float &dist)
return true if the given position lies inside the triangle with respect to the given maximum distance...
Definition: mesh_map.cpp:911
lvr2::DenseEdgeMap< float > edge_weights
edge weights
Definition: mesh_map.h:457
std::string srv_username
login username to connect to the server
Definition: mesh_map.h:426
bool initLayerPlugins()
Initialized all loaded layer plugins.
Definition: mesh_map.cpp:384
void combineVertexCosts()
A method which combines all layer costs with the respective weightings.
Definition: mesh_map.cpp:417
std::string mesh_layer
Definition: mesh_map.h:431
lvr2::OptionalVertexHandle getNearestVertexHandle(const mesh_map::Vector &pos)
Return and optional vertex handle of to the closest vertex to the given position. ...
Definition: mesh_map.cpp:1082
std::unique_ptr< NanoFlannMeshAdaptor > adaptor_ptr
k-d tree nano flann mesh adaptor to access mesh data
Definition: mesh_map.h:513
void publishCostLayers()
Publishes all layer costs as mesh_msgs/VertexCosts.
Definition: mesh_map.cpp:1185
const geometry_msgs::Point toPoint(const Vector &vec)
Converts a vector to a ROS geometry_msgs/Point message.
Definition: mesh_map.cpp:1091
bool projectedBarycentricCoords(const Vector &p, const lvr2::FaceHandle &triangle, std::array< float, 3 > &barycentric_coords, float &dist)
Computes the projected barycentric coordinates, it implements Heidrich&#39;s method See https://www...
Definition: mesh_map.cpp:1102
std::string srv_url
server url
Definition: mesh_map.h:423
bool first_config
first reconfigure call
Definition: mesh_map.h:487
bool isLethal(const lvr2::VertexHandle &vH)
Checks if the given vertex handle corresponds to a lethal / not passable vertex.
std::shared_ptr< lvr2::HalfEdgeMesh< Vector > > mesh_ptr
Definition: mesh_map.h:399
bool resetLayers()
Resets all layers.
Definition: mesh_map.cpp:1180
void publishVectorField(const std::string &name, const lvr2::DenseVertexMap< lvr2::BaseVector< float >> &vector_map, const bool publish_face_vectors=false)
Publishes a vector field as visualisation_msgs/Marker.
Definition: mesh_map.cpp:703
tf2_ros::Buffer & tf_buffer
transformation buffer
Definition: mesh_map.h:499
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< float, NanoFlannMeshAdaptor >, NanoFlannMeshAdaptor, 3 > KDTree
k-d tree type for 3D with a custom mesh adaptor
Definition: mesh_map.h:510
lvr2::DenseVertexMap< float > vertex_costs
combined layer costs
Definition: mesh_map.h:448
const lvr2::DenseFaceMap< Normal > & faceNormals()
Returns the mesh&#39;s triangle normals.
Definition: mesh_map.h:292
std::vector< std::pair< std::string, mesh_map::AbstractLayer::Ptr > > layers
vector of name and layer instances
Definition: mesh_map.h:411
lvr2::DenseVertexMap< mesh_map::Vector > vector_map
stored vector map to share between planner and controller
Definition: mesh_map.h:451
ros::Publisher vertex_costs_pub
publisher for vertex costs
Definition: mesh_map.h:466
void publishVertexColors()
Definition: mesh_map.cpp:1202
lvr2::OptionalFaceHandle getContainingFace(Vector &position, const float &max_dist)
Searches for a triangle which contains the given position with respect to the maximum distance...
Definition: mesh_map.cpp:1029
std::shared_ptr< lvr2::AttributeMeshIOBase > mesh_io_ptr
Definition: mesh_map.h:398
float min_height_diff
Definition: mesh_map.h:435
const std::string & mapFrame()
Returns the map frame / coordinate system id.
Definition: mesh_map.h:284
values
void publishCombinedVectorField()
Publishes the vector field as visualisation_msgs/Marker.
Definition: mesh_map.cpp:710
ros::Publisher vertex_colors_pub
publisher for vertex colors
Definition: mesh_map.h:469
float max_roughness
Definition: mesh_map.h:434
void setVectorMap(lvr2::DenseVertexMap< mesh_map::Vector > &vector_map)
Stores the given vector map.
Definition: mesh_map.cpp:595
void reconfigureCallback(mesh_map::MeshMapConfig &config, uint32_t level)
reconfigure callback function which is called if a dynamic reconfiguration were triggered.
Definition: mesh_map.cpp:1229
float max_height_diff
Definition: mesh_map.h:436
ros::Publisher marker_pub
publisher for the debug markers
Definition: mesh_map.h:475
std::map< std::string, std::set< lvr2::VertexHandle > > lethal_indices
each layer maps to a set of impassable indices
Definition: mesh_map.h:414
lvr2::DenseFaceMap< Normal > face_normals
triangle normals
Definition: mesh_map.h:460
void findContours(std::vector< std::vector< lvr2::VertexHandle >> &contours, int min_contour_size)
Computes contours.
Definition: mesh_map.cpp:515
std::string global_frame
global frame / coordinate system id
Definition: mesh_map.h:420
bool map_loaded
indicates whether the map has been loaded
Definition: mesh_map.h:490
float min_roughness
Definition: mesh_map.h:433
const lvr2::DenseEdgeMap< float > & edgeDistances()
Returns the mesh&#39;s vertex distances.
Definition: mesh_map.h:316
bool barycentricCoords(const Vector &p, const lvr2::FaceHandle &triangle, float &u, float &v, float &w)
Definition: mesh_map.cpp:1114
void layerChanged(const std::string &layer_name)
Callback function which is called from inside a layer plugin if cost values change.
Definition: mesh_map.cpp:335
MeshMapConfig config
current mesh map configuration
Definition: mesh_map.h:493
lvr2::DenseVertexMap< Normal > vertex_normals
vertex normals
Definition: mesh_map.h:463
lvr2::DenseEdgeMap< float > edge_distances
vertex distance for each edge
Definition: mesh_map.h:454
lvr2::DenseVertexMap< bool > invalid
Definition: mesh_map.h:401
const lvr2::DenseVertexMap< float > & vertexCosts()
Returns the stored combined costs.
Definition: mesh_map.h:276
MeshMap(tf2_ros::Buffer &tf)
Definition: mesh_map.cpp:65
void publishDebugFace(const lvr2::FaceHandle &face_handle, const std_msgs::ColorRGBA &color, const std::string &name)
Publishes a triangle as marker. Used for debug purposes.
Definition: mesh_map.cpp:673
ros::Publisher vector_field_pub
publisher for the stored vector field
Definition: mesh_map.h:478
std::mutex layer_mtx
layer mutex to handle simultaneous layer changes
Definition: mesh_map.h:505
boost::optional< Vector > directionAtPosition(const lvr2::VertexMap< lvr2::BaseVector< float >> &vector_map, const std::array< lvr2::VertexHandle, 3 > &vertices, const std::array< float, 3 > &barycentric_coords)
Definition: mesh_map.cpp:600
std_msgs::ColorRGBA color(const float &r, const float &g, const float &b, const float &a=1.0)
Function to build std_msgs color instances.
Definition: util.cpp:149
const std::string getGlobalFrameID()
Returns the global frame / coordinate system id string.
Definition: mesh_map.cpp:1250
float costAtPosition(const lvr2::DenseVertexMap< float > &costs, const std::array< lvr2::VertexHandle, 3 > &vertices, const std::array< float, 3 > &barycentric_coords)
Definition: mesh_map.cpp:633
void findLethalByContours(const int &min_contour_size, std::set< lvr2::VertexHandle > &lethals)
Compute all contours and returns the corresponding vertices to use these as lethal vertices...
Definition: mesh_map.cpp:503
std::unique_ptr< KDTree > kd_tree_ptr
k-d tree to query mesh vertices in logarithmic time
Definition: mesh_map.h:516
boost::shared_ptr< dynamic_reconfigure::Server< mesh_map::MeshMapConfig > > reconfigure_server_ptr
shared pointer to dynamic reconfigure server
Definition: mesh_map.h:481
std::string uuid_str
uuid for the load mesh map
Definition: mesh_map.h:502
const lvr2::DenseVertexMap< mesh_map::Vector > & getVectorMap()
Returns the stored vector map.
Definition: mesh_map.h:260
dynamic_reconfigure::Server< mesh_map::MeshMapConfig >::CallbackType config_callback
dynamic reconfigure callback function binding
Definition: mesh_map.h:484
void publishVertexCosts(const lvr2::VertexMap< float > &costs, const std::string &name)
Publishes the given vertex map as mesh_msgs/VertexCosts, e.g. to visualize these. ...
Definition: mesh_map.cpp:1196
std::string mesh_file
Definition: mesh_map.h:444
std::map< std::string, mesh_map::AbstractLayer::Ptr > layer_names
mapping from name to layer instance
Definition: mesh_map.h:408
boost::shared_ptr< MeshMap > Ptr
Definition: mesh_map.h:63
const lvr2::DenseEdgeMap< float > & edgeWeights()
Returns the mesh&#39;s edge weights.
Definition: mesh_map.h:308


mesh_map
Author(s): Sebastian Pütz
autogenerated on Tue May 24 2022 02:57:53