supereight
map.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2016-2019 Emanuele Vespa
3  * SPDX-FileCopyrightText: 2019-2023 Smart Robotics Lab, Imperial College London, Technical University of Munich
4  * SPDX-FileCopyrightText: 2019-2023 Nils Funk
5  * SPDX-FileCopyrightText: 2019-2023 Sotiris Papatheodorou
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef SE_MAP_HPP
10 #define SE_MAP_HPP
11 
12 #include <Eigen/StdVector>
13 #include <optional>
14 
15 #include "se/common/math_util.hpp"
16 #include "se/common/str_utils.hpp"
19 #include "se/map/data.hpp"
20 #include "se/map/io/mesh_io.hpp"
21 #include "se/map/io/octree_io.hpp"
23 #include "se/map/raycaster.hpp"
25 
26 
27 
28 namespace se {
29 
30 struct MapConfig {
33  Eigen::Vector3f dim;
34 
37  float res;
38 
41  Eigen::Matrix4f T_MW;
42 
46  MapConfig();
47 
51  MapConfig(const std::string& yaml_file);
52 
53  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
54 };
55 
56 std::ostream& operator<<(std::ostream& os, const MapConfig& c);
57 
58 
59 
60 // Forward Declaration
61 template<typename DataT = se::Data<Field::TSDF, Colour::Off, Semantics::Off>,
62  Res ResT = Res::Single,
63  int BlockSize = 8>
64 class Map;
65 
66 
67 
68 template<Field FldT, Colour ColB, Semantics SemB, Res ResT, int BlockSize>
69 class Map<se::Data<FldT, ColB, SemB>, ResT, BlockSize> {
70  public:
74 
84  Map(const Eigen::Vector3f& dim,
85  const float res,
87 
94  Map(const MapConfig& map_config,
96 
103  inline bool contains(const Eigen::Vector3f& point_W) const;
104 
110  inline Eigen::Matrix4f getTMW() const
111  {
112  return T_MW_;
113  };
114 
120  inline Eigen::Matrix4f getTWM() const
121  {
122  return T_WM_;
123  };
124 
130  inline Eigen::Vector3f gettMW() const
131  {
132  return se::math::to_translation(T_MW_);
133  }
134 
140  inline Eigen::Vector3f gettWM() const
141  {
142  return se::math::to_translation(T_WM_);
143  }
144 
150  inline Eigen::Matrix3f getRMW() const
151  {
152  return se::math::to_rotation(T_MW_);
153  }
154 
160  inline Eigen::Matrix3f getRWM() const
161  {
162  return se::math::to_rotation(T_WM_);
163  }
164 
170  inline Eigen::Vector3f getDim() const
171  {
172  return dimension_;
173  }
174 
180  inline float getRes() const
181  {
182  return resolution_;
183  }
184 
190  inline DataConfigType getDataConfig() const
191  {
192  return data_config_;
193  }
194 
202  template<Safe SafeB = Safe::Off>
203  inline const DataType getData(const Eigen::Vector3f& point_W) const;
204 
214  template<Safe SafeB = Safe::Off, Res ResTDummy = ResT>
215  inline typename std::enable_if_t<ResTDummy == Res::Multi, DataType>
216  getMaxData(const Eigen::Vector3f& point_W, const int scale_desired) const
217  {
218  Eigen::Vector3i voxel_coord;
219 
220  if constexpr (SafeB == Safe::Off) // Evaluate at compile time
221  {
222  pointToVoxel<Safe::Off>(point_W, voxel_coord);
223  }
224  else {
225  if (!pointToVoxel<Safe::On>(point_W, voxel_coord)) {
226  return DataType();
227  }
228  }
229 
230  return se::visitor::getMaxData(*octree_ptr_, voxel_coord, scale_desired);
231  }
232 
233 
241  template<Safe SafeB = Safe::Off>
242  inline std::optional<se::field_t> getFieldInterp(const Eigen::Vector3f& point_W) const;
243 
253  template<Safe SafeB = Safe::Off, Res ResTDummy = ResT>
254  inline typename std::enable_if_t<ResTDummy == Res::Multi, std::optional<se::field_t>>
255  getFieldInterp(const Eigen::Vector3f& point_W, int& returned_scale) const;
256 
264  template<Safe SafeB = Safe::Off>
265  inline std::optional<se::field_vec_t> getFieldGrad(const Eigen::Vector3f& point_W) const;
266 
282  int saveFieldSlices(const std::string& filename_x,
283  const std::string& filename_y,
284  const std::string& filename_z,
285  const Eigen::Vector3f& point_W) const;
286 
303  template<se::Field FldTDummy = FldT>
304  typename std::enable_if_t<FldTDummy == se::Field::Occupancy, int>
305  saveMaxFieldSlices(const std::string& filename_x,
306  const std::string& filename_y,
307  const std::string& filename_z,
308  const Eigen::Vector3f& point_W,
309  const int scale) const;
310 
326  template<Res ResTDummy = ResT>
327  typename std::enable_if_t<ResTDummy == Res::Multi, int>
328  saveScaleSlices(const std::string& filename_x,
329  const std::string& filename_y,
330  const std::string& filename_z,
331  const Eigen::Vector3f& point_W) const;
332 
343  int saveStructure(const std::string& filename,
344  const Eigen::Matrix4f& T_WM = Eigen::Matrix4f::Identity()) const;
345 
355  int saveMesh(const std::string& filename,
356  const Eigen::Matrix4f& T_OW = Eigen::Matrix4f::Identity()) const;
357 
366  int saveMeshVoxel(const std::string& filename) const;
367 
376  inline void voxelToPoint(const Eigen::Vector3i& voxel_coord, Eigen::Vector3f& point_W) const;
377 
385  inline void voxelToPoint(const Eigen::Vector3i& voxel_coord,
386  const int voxel_size,
387  Eigen::Vector3f& point_W) const;
388 
397  inline void voxelToCornerPoints(const Eigen::Vector3i& voxel_coord,
398  Eigen::Matrix<float, 3, 8>& corner_points_W) const;
399 
407  inline void voxelToCornerPoints(const Eigen::Vector3i& voxel_coord,
408  const int voxel_size,
409  Eigen::Matrix<float, 3, 8>& corner_points_W) const;
410 
421  template<se::Safe SafeB = se::Safe::On>
422  inline typename std::enable_if_t<SafeB == se::Safe::On, bool>
423  pointToVoxel(const Eigen::Vector3f& point_W, Eigen::Vector3i& voxel_coord) const;
424 
433  template<se::Safe SafeB>
434  inline typename std::enable_if_t<SafeB == se::Safe::Off, bool>
435  pointToVoxel(const Eigen::Vector3f& point_W, Eigen::Vector3i& voxel_coord) const;
436 
445  template<se::Safe SafeB = se::Safe::On>
446  inline typename std::enable_if_t<SafeB == se::Safe::On, bool>
447  pointToVoxel(const Eigen::Vector3f& point_W, Eigen::Vector3f& voxel_coord_f) const;
448 
457  template<se::Safe SafeB>
458  inline typename std::enable_if_t<SafeB == se::Safe::Off, bool>
459  pointToVoxel(const Eigen::Vector3f& point_W, Eigen::Vector3f& voxel_coord_f) const;
460 
469  template<se::Safe SafeB = se::Safe::On>
470  inline typename std::enable_if_t<SafeB == se::Safe::On, bool> pointsToVoxels(
471  const std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f>>& points_W,
472  std::vector<Eigen::Vector3i, Eigen::aligned_allocator<Eigen::Vector3i>>& voxel_coords)
473  const;
474 
483  template<se::Safe SafeB>
484  inline typename std::enable_if_t<SafeB == se::Safe::Off, bool> pointsToVoxels(
485  const std::vector<Eigen::Vector3f, Eigen::aligned_allocator<Eigen::Vector3f>>& points_W,
486  std::vector<Eigen::Vector3i, Eigen::aligned_allocator<Eigen::Vector3i>>& voxel_coords)
487  const;
488 
494  inline std::shared_ptr<OctreeType> getOctree()
495  {
496  return octree_ptr_;
497  };
498 
504  inline std::shared_ptr<OctreeType> getOctree() const
505  {
506  return octree_ptr_;
507  };
508 
516  void setOctree(std::shared_ptr<OctreeType> octree_ptr)
517  {
518  // delete octree_ptr_; // TODO: Delete old octree when setting a new one.
519  octree_ptr_ = octree_ptr;
520  };
521 
522  static constexpr Field fld_ = FldT;
523  static constexpr Colour col_ = ColB;
524  static constexpr Semantics sem_ = SemB;
525 
526  static constexpr Res res_ = ResT;
527 
528  protected:
534  bool initialiseOctree();
535 
536  const Eigen::Vector3f dimension_;
537  const float resolution_;
538  const Eigen::Matrix4f T_MW_;
539  const Eigen::Matrix4f T_WM_;
540 
541  const Eigen::Vector3f lb_M_;
542  const Eigen::Vector3f ub_M_;
543 
544  std::shared_ptr<OctreeType> octree_ptr_ = nullptr;
545 
546  DataConfigType data_config_;
547 
548  Eigen::Matrix<float, 3, 8> corner_rel_steps_;
549 };
550 
552 template<se::Field FldT = se::Field::TSDF,
555  se::Res ResT = se::Res::Single,
556  int BlockSize = 8>
557 using MapD = Map<Data<FldT, ColB, SemB>, ResT, BlockSize>;
558 
559 
560 // Occupancy map setups
561 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
563 
564 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
566 
567 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
569 
570 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
572 
573 
574 // TSDF map setups
575 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
577 
578 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
580 
581 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
583 
584 template<se::Res ResT = se::Res::Single, int BlockSize = 8>
586 
587 } // namespace se
588 
589 #include "impl/map_impl.hpp"
590 
591 #endif // SE_MAP_HPP
DataConfigType data_config_
The configuration of the data.
Definition: map.hpp:546
static Eigen::Matrix3f to_rotation(const Eigen::Matrix4f &T)
Eigen::Matrix4f getTWM() const
Get the transformation from map to world frame.
Definition: map.hpp:120
const Eigen::Vector3f lb_M_
The lower map bound.
Definition: map.hpp:541
Eigen::Matrix4f T_MW
The transformation from the world frame W to the map frame M.
Definition: map.hpp:41
Res
Definition: setup_util.hpp:23
void setOctree(std::shared_ptr< OctreeType > octree_ptr)
Update the octree pointer to a new one.
Definition: map.hpp:516
std::shared_ptr< OctreeType > getOctree()
Get the shared pointer to the octree.
Definition: map.hpp:494
float res
The resolution of map voxels in metres.
Definition: map.hpp:37
std::ostream & operator<<(std::ostream &os, const FieldDataConfig< se::Field::Occupancy > &c)
Colour
Definition: setup_util.hpp:19
const Eigen::Vector3f dimension_
The dimensions of the map.
Definition: map.hpp:536
DataConfig< FldT, ColB, SemB > DataConfigType
Definition: map.hpp:72
MapConfig()
Initializes the config to a 10m x 10m x 3m map with a 10cm resolution and the origin at the centre of...
Eigen::Vector3f dim
The dimensions of the map in metres.
Definition: map.hpp:33
Eigen::Matrix< float, 3, 8 > corner_rel_steps_
The eight relative unit corner offsets.
Definition: map.hpp:548
std::enable_if_t< ResTDummy==Res::Multi, DataType > getMaxData(const Eigen::Vector3f &point_W, const int scale_desired) const
Get the stored max data at the provided coordinates in [meter] for a given scale. ...
Definition: map.hpp:216
Definition: data.hpp:95
OctreeT::DataType getData(const OctreeT &octree, const Eigen::Vector3i &voxel_coord)
Single/multi-res get data functions.
std::enable_if_t<(OctreeT::fld_==se::Field::TSDF &&OctreeT::res_==se::Res::Multi), std::optional< se::field_t > > getFieldInterp(const OctreeT &octree, const Eigen::Vector3f &voxel_coord_f, const int scale_desired, int &scale_returned)
Get the interplated field value for a given coordinate [float voxel coordinates] and desired scale...
Definition: visitor.hpp:273
Eigen::Vector3f gettMW() const
Get the translation from world to map frame.
Definition: map.hpp:130
Eigen::Matrix3f getRWM() const
Get the rotation from map to world frame.
Definition: map.hpp:160
Eigen::Matrix4f getTMW() const
Get the transformation from world to map frame.
Definition: map.hpp:110
const Eigen::Matrix4f T_MW_
The transformation from world to map frame.
Definition: map.hpp:538
Data< FldT, ColB, SemB > DataType
Definition: map.hpp:71
const float resolution_
The resolution of the map.
Definition: map.hpp:537
Eigen::Vector3f gettWM() const
Get the translation from map to world frame.
Definition: map.hpp:140
Eigen::Matrix3f getRMW() const
Get the rotation from world to map frame.
Definition: map.hpp:150
std::shared_ptr< OctreeType > getOctree() const
Get the const shared pointer to the octree.
Definition: map.hpp:504
Field
Definition: setup_util.hpp:18
Definition: map.hpp:30
const Eigen::Matrix4f T_WM_
The transformation from map to world frame.
Definition: map.hpp:539
static Eigen::Vector3f to_translation(const Eigen::Matrix4f &T)
The octree is the memory manager of the map.
Definition: octree.hpp:36
float getRes() const
Get the resolution of the map in [meter/voxel].
Definition: map.hpp:180
DataConfigType getDataConfig() const
Get the data configuration of the map.
Definition: map.hpp:190
std::enable_if_t< OctreeT::DataType::fld_==se::Field::Occupancy, typename OctreeT::DataType > getMaxData(const OctreeT &octree, const Eigen::Vector3i &voxel_coord, const int scale_desired)
Get the max occupancy data at a given scale.
Semantics
Definition: setup_util.hpp:20
Definition: data.hpp:294
se::Octree< DataType, ResT, BlockSize > OctreeType
Definition: map.hpp:73
Definition: map.hpp:64
const Eigen::Vector3f ub_M_
The upper map bound.
Definition: map.hpp:542
Eigen::Vector3f getDim() const
Get the dimensions of the map in [meter] (length x width x height)
Definition: map.hpp:170
std::enable_if_t< OctreeT::res_==se::Res::Single, std::optional< se::field_vec_t > > getFieldGrad(const OctreeT &octree, const Eigen::Vector3f &voxel_coord_f)
Single-res get gradient functions.
Helper wrapper to allocate and de-allocate octants in the octree.
Definition: colour_utils.hpp:17
static const std::vector< Eigen::Vector3f, Eigen::aligned_allocator< Eigen::Vector3f > > scale
The colours used for the various integration scales.
Definition: colour_utils.hpp:22