EasyNav Plugins
Loading...
Searching...
No Matches
probabilistic_map.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <eigen3/Eigen/Geometry>
4#include <unordered_set>
5
6#include "bonxai/bonxai.hpp"
7
8namespace Bonxai
9{
10
11template<class Functor>
12void RayIterator(const CoordT & key_origin, const CoordT & key_end, const Functor & func);
13
14inline void ComputeRay(const CoordT & key_origin, const CoordT & key_end, std::vector<CoordT> & ray)
15{
16 ray.clear();
17 RayIterator(key_origin, key_end, [&ray](const CoordT & coord) {
18 ray.push_back(coord);
19 return true;
20 });
21}
22
30public:
31 using Vector3D = Eigen::Vector3d;
32
36 [[nodiscard]] static constexpr int32_t logods(float prob)
37 {
38 return int32_t(1e6 * std::log(prob / (1.0 - prob)));
39 }
40
42 [[nodiscard]] static constexpr float prob(int32_t logods_fixed)
43 {
44 float logods = float(logods_fixed) * 1e-6;
45 return 1.0 - 1.0 / (1.0 + std::exp(logods));
46 }
47
48 struct CellT
49 {
50 // variable used to check if a cell was already updated in this loop
51 int32_t update_id : 4;
52 // the probability of the cell to be occupied
53 int32_t probability_log : 28;
54
58 };
59
61 struct Options
62 {
63 int32_t prob_miss_log = logods(0.4f);
64 int32_t prob_hit_log = logods(0.7f);
65
66 int32_t clamp_min_log = logods(0.12f);
67 int32_t clamp_max_log = logods(0.97f);
68
70 };
71
72 static const int32_t UnknownProbability;
73
74 ProbabilisticMap(double resolution);
75
76 [[nodiscard]] VoxelGrid<CellT> & grid();
77
78 [[nodiscard]] const VoxelGrid<CellT> & grid() const;
79
80 [[nodiscard]] const Options & options() const;
81
82 void setOptions(const Options & options);
83
97 template<typename PointT, typename Allocator>
99 const std::vector<PointT, Allocator> & points, const PointT & origin, double max_range);
100
101 // This function is usually called by insertPointCloud
102 // We expose it here to add more control to the user.
103 // Once finished adding points, you must call updateFreeCells()
104 void addHitPoint(const Vector3D & point);
105
106 // This function is usually called by insertPointCloud
107 // We expose it here to add more control to the user.
108 // Once finished adding points, you must call updateFreeCells()
109 void addMissPoint(const Vector3D & point);
110
111 [[nodiscard]] bool isOccupied(const Bonxai::CoordT & coord) const;
112
113 [[nodiscard]] bool isUnknown(const Bonxai::CoordT & coord) const;
114
115 [[nodiscard]] bool isFree(const Bonxai::CoordT & coord) const;
116
117 [[nodiscard]] float queryProbability(const Eigen::Vector3f & p_world) const;
118 [[nodiscard]] float queryProbability(const Bonxai::CoordT & coord) const;
119
120 void getOccupiedVoxels(std::vector<Bonxai::CoordT> & coords);
121
122 void getFreeVoxels(std::vector<Bonxai::CoordT> & coords);
123
124 template<typename PointT>
125 void getOccupiedVoxels(std::vector<PointT> & points)
126 {
127 thread_local std::vector<Bonxai::CoordT> coords;
128 coords.clear();
129 getOccupiedVoxels(coords);
130 for (const auto & coord : coords) {
131 const auto p = _grid.coordToPos(coord);
132 points.emplace_back(p.x, p.y, p.z);
133 }
134 }
135
136private:
137 VoxelGrid<CellT> _grid;
138 Options _options;
139 uint8_t _update_count = 1;
140 double _resolution;
141
142 std::vector<CoordT> _miss_coords;
143 std::vector<CoordT> _hit_coords;
144
145 mutable Bonxai::VoxelGrid<CellT>::Accessor _accessor;
146
147 void updateFreeCells(const Vector3D & origin);
148};
149
150//--------------------------------------------------
151
152template<typename PointT, typename Alloc>
154 const std::vector<PointT, Alloc> & points, const PointT & origin, double max_range)
155{
156 const auto from = ConvertPoint<Vector3D>(origin);
157 const double max_range_sqr = max_range * max_range;
158 for (const auto & point : points) {
159 const auto to = ConvertPoint<Vector3D>(point);
160 Vector3D vect(to - from);
161 const double squared_norm = vect.squaredNorm();
162 // points that exceed the max_range will create a cleaning ray
163 if (squared_norm >= max_range_sqr) {
164 // The new point will have distance == max_range from origin
165 const Vector3D new_point = from + ((vect / std::sqrt(squared_norm)) * max_range);
166 addMissPoint(new_point);
167 } else {
168 addHitPoint(to);
169 }
170 }
171 updateFreeCells(from);
172}
173
174template<class Functor>
175inline void RayIterator(const CoordT & key_origin, const CoordT & key_end, const Functor & func)
176{
177 if (key_origin == key_end) {
178 return;
179 }
180 if (!func(key_origin)) {
181 return;
182 }
183
184 CoordT error = {0, 0, 0};
185 CoordT coord = key_origin;
186 CoordT delta = (key_end - coord);
187 const CoordT step = {delta.x < 0 ? -1 : 1, delta.y < 0 ? -1 : 1, delta.z < 0 ? -1 : 1};
188
189 delta = {
190 delta.x < 0 ? -delta.x : delta.x, delta.y < 0 ? -delta.y : delta.y,
191 delta.z < 0 ? -delta.z : delta.z};
192
193 const int max = std::max(std::max(delta.x, delta.y), delta.z);
194
195 // maximum change of any coordinate
196 for (int i = 0; i < max - 1; ++i) {
197 // update errors
198 error = error + delta;
199 // manual loop unrolling
200 if ((error.x << 1) >= max) {
201 coord.x += step.x;
202 error.x -= max;
203 }
204 if ((error.y << 1) >= max) {
205 coord.y += step.y;
206 error.y -= max;
207 }
208 if ((error.z << 1) >= max) {
209 coord.z += step.z;
210 error.z -= max;
211 }
212 if (!func(coord)) {
213 return;
214 }
215 }
216}
217
218} // namespace Bonxai
const Options & options() const
Definition probabilistic_map.cpp:25
void getOccupiedVoxels(std::vector< PointT > &points)
Definition probabilistic_map.hpp:125
static constexpr int32_t logods(float prob)
Compute the logds, but return the result as an integer, The real number is represented as a fixed pre...
Definition probabilistic_map.hpp:36
void getOccupiedVoxels(std::vector< Bonxai::CoordT > &coords)
static const int32_t UnknownProbability
Definition probabilistic_map.hpp:72
void addHitPoint(const Vector3D &point)
Definition probabilistic_map.cpp:35
void getFreeVoxels(std::vector< Bonxai::CoordT > &coords)
Definition probabilistic_map.cpp:150
Eigen::Vector3d Vector3D
Definition probabilistic_map.hpp:31
VoxelGrid< CellT > & grid()
Definition probabilistic_map.cpp:11
float queryProbability(const Eigen::Vector3f &p_world) const
Definition probabilistic_map.cpp:97
bool isUnknown(const Bonxai::CoordT &coord) const
Definition probabilistic_map.cpp:71
void setOptions(const Options &options)
Definition probabilistic_map.cpp:30
ProbabilisticMap(double resolution)
Definition probabilistic_map.cpp:16
static constexpr float prob(int32_t logods_fixed)
Expect the fixed comma value returned by logods().
Definition probabilistic_map.hpp:42
bool isOccupied(const Bonxai::CoordT &coord) const
Definition probabilistic_map.cpp:63
void insertPointCloud(const std::vector< PointT, Allocator > &points, const PointT &origin, double max_range)
insertPointCloud will update the probability map with a new set of detections.
void addMissPoint(const Vector3D &point)
Definition probabilistic_map.cpp:49
bool isFree(const Bonxai::CoordT &coord) const
Definition probabilistic_map.cpp:79
Class to be used to set and get values of a cell of the Grid.
Definition bonxai.hpp:276
Definition bonxai.hpp:124
Definition bonxai.hpp:28
void RayIterator(const CoordT &key_origin, const CoordT &key_end, const Functor &func)
Definition probabilistic_map.hpp:175
void ComputeRay(const CoordT &key_origin, const CoordT &key_end, std::vector< CoordT > &ray)
Definition probabilistic_map.hpp:14
PointOut ConvertPoint(const PointIn &v)
Definition grid_coord.hpp:143
Definition grid_coord.hpp:67
int32_t z
Definition grid_coord.hpp:70
int32_t y
Definition grid_coord.hpp:69
int32_t x
Definition grid_coord.hpp:68
CellT()
Definition probabilistic_map.hpp:55
int32_t probability_log
Definition probabilistic_map.hpp:53
int32_t update_id
Definition probabilistic_map.hpp:51
These default values are the same as OctoMap.
Definition probabilistic_map.hpp:62
int32_t prob_hit_log
Definition probabilistic_map.hpp:64
int32_t prob_miss_log
Definition probabilistic_map.hpp:63
int32_t clamp_max_log
Definition probabilistic_map.hpp:67
int32_t occupancy_threshold_log
Definition probabilistic_map.hpp:69
int32_t clamp_min_log
Definition probabilistic_map.hpp:66