MRSL JPS3D Library  1.1
An implementaion of Jump Point Search on 3D voxel map
map_util.h
Go to the documentation of this file.
1 
5 #ifndef JPS_MAP_UTIL_H
6 #define JPS_MAP_UTIL_H
7 
8 #include <iostream>
9 #include <jps_basis/data_type.h>
10 
11 namespace JPS {
13  using Tmap = std::vector<signed char>;
18  template <int Dim> class MapUtil {
19  public:
21  MapUtil() {}
23  Tmap getMap() { return map_; }
25  decimal_t getRes() { return res_; }
27  Veci<Dim> getDim() { return dim_; }
31  int getIndex(const Veci<Dim>& pn) {
32  return Dim == 2 ? pn(0) + dim_(0) * pn(1) :
33  pn(0) + dim_(0) * pn(1) + dim_(0) * dim_(1) * pn(2);
34  }
35 
37  bool isOutsideXYZ(const Veci<Dim> &n, int i) { return n(i) < 0 || n(i) >= dim_(i); }
39  bool isFree(int idx) { return map_[idx] == val_free; }
41  bool isUnknown(int idx) { return map_[idx] == val_unknown; }
43  bool isOccupied(int idx) { return map_[idx] > val_free; }
44 
46  bool isOutside(const Veci<Dim> &pn) {
47  for(int i = 0; i < Dim; i++)
48  if (pn(i) < 0 || pn(i) >= dim_(i))
49  return true;
50  return false;
51  }
53  bool isFree(const Veci<Dim> &pn) {
54  if (isOutside(pn))
55  return false;
56  else
57  return isFree(getIndex(pn));
58  }
60  bool isOccupied(const Veci<Dim> &pn) {
61  if (isOutside(pn))
62  return false;
63  else
64  return isOccupied(getIndex(pn));
65  }
67  bool isUnknown(const Veci<Dim> &pn) {
68  if (isOutside(pn))
69  return false;
70  return map_[getIndex(pn)] == val_unknown;
71  }
72 
81  void setMap(const Vecf<Dim>& ori, const Veci<Dim>& dim, const Tmap &map, decimal_t res) {
82  map_ = map;
83  dim_ = dim;
84  origin_d_ = ori;
85  res_ = res;
86  }
87 
89  void info() {
90  Vecf<Dim> range = dim_.template cast<decimal_t>() * res_;
91  std::cout << "MapUtil Info ========================== " << std::endl;
92  std::cout << " res: [" << res_ << "]" << std::endl;
93  std::cout << " origin: [" << origin_d_.transpose() << "]" << std::endl;
94  std::cout << " range: [" << range.transpose() << "]" << std::endl;
95  std::cout << " dim: [" << dim_.transpose() << "]" << std::endl;
96  };
97 
100  Veci<Dim> pn;
101  for(int i = 0; i < Dim; i++)
102  pn(i) = std::round((pt(i) - origin_d_(i)) / res_ - 0.5);
103  return pn;
104  }
107  //return pn.template cast<decimal_t>() * res_ + origin_d_;
108  return (pn.template cast<decimal_t>() + Vecf<Dim>::Constant(0.5)) * res_ + origin_d_;
109  }
110 
112  vec_Veci<Dim> rayTrace(const Vecf<Dim> &pt1, const Vecf<Dim> &pt2) {
113  Vecf<Dim> diff = pt2 - pt1;
114  decimal_t k = 0.8;
115  int max_diff = (diff / res_).template lpNorm<Eigen::Infinity>() / k;
116  decimal_t s = 1.0 / max_diff;
117  Vecf<Dim> step = diff * s;
118 
119  vec_Veci<Dim> pns;
120  Veci<Dim> prev_pn = Veci<Dim>::Constant(-1);
121  for (int n = 1; n < max_diff; n++) {
122  Vecf<Dim> pt = pt1 + step * n;
123  Veci<Dim> new_pn = floatToInt(pt);
124  if (isOutside(new_pn))
125  break;
126  if (new_pn != prev_pn)
127  pns.push_back(new_pn);
128  prev_pn = new_pn;
129  }
130  return pns;
131  }
132 
134  bool isBlocked(const Vecf<Dim>& p1, const Vecf<Dim>& p2, int8_t val = 100) {
135  vec_Veci<Dim> pns = rayTrace(p1, p2);
136  for (const auto &pn : pns) {
137  if (map_[getIndex(pn)] >= val)
138  return true;
139  }
140  return false;
141  }
142 
145  vec_Vecf<Dim> cloud;
146  Veci<Dim> n;
147  if(Dim == 3) {
148  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
149  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
150  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
151  if (isOccupied(getIndex(n)))
152  cloud.push_back(intToFloat(n));
153  }
154  }
155  }
156  }
157  else if (Dim == 2) {
158  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
159  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
160  if (isOccupied(getIndex(n)))
161  cloud.push_back(intToFloat(n));
162  }
163  }
164  }
165 
166  return cloud;
167  }
168 
171  vec_Vecf<Dim> cloud;
172  Veci<Dim> n;
173  if(Dim == 3) {
174  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
175  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
176  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
177  if (isFree(getIndex(n)))
178  cloud.push_back(intToFloat(n));
179  }
180  }
181  }
182  }
183  else if (Dim == 2) {
184  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
185  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
186  if (isFree(getIndex(n)))
187  cloud.push_back(intToFloat(n));
188  }
189  }
190  }
191 
192  return cloud;
193  }
194 
197  vec_Vecf<Dim> cloud;
198  Veci<Dim> n;
199  if(Dim == 3) {
200  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
201  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
202  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
203  if (isUnknown(getIndex(n)))
204  cloud.push_back(intToFloat(n));
205  }
206  }
207  }
208  }
209  else if (Dim == 2) {
210  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
211  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
212  if (isUnknown(getIndex(n)))
213  cloud.push_back(intToFloat(n));
214  }
215  }
216  }
217 
218  return cloud;
219  }
220 
222  void dilate(const vec_Veci<Dim>& dilate_neighbor) {
223  Tmap map = map_;
225  if(Dim == 3) {
226  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
227  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
228  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
229  if (isOccupied(getIndex(n))) {
230  for (const auto &it : dilate_neighbor) {
231  if (!isOutside(n + it))
232  map[getIndex(n + it)] = val_occ;
233  }
234  }
235  }
236  }
237  }
238  }
239  else if(Dim == 2) {
240  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
241  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
242  if (isOccupied(getIndex(n))) {
243  for (const auto &it : dilate_neighbor) {
244  if (!isOutside(n + it))
245  map[getIndex(n + it)] = val_occ;
246  }
247  }
248  }
249  }
250  }
251 
252  map_ = map;
253  }
254 
256  void freeUnknown() {
257  Veci<Dim> n;
258  if(Dim == 3) {
259  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
260  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
261  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
262  if (isUnknown(getIndex(n)))
263  map_[getIndex(n)] = val_free;
264  }
265  }
266  }
267  }
268  else if (Dim == 2) {
269  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
270  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
271  if (isUnknown(getIndex(n)))
272  map_[getIndex(n)] = val_free;
273  }
274  }
275  }
276  }
277 
280  protected:
288  int8_t val_occ = 100;
290  int8_t val_free = 0;
292  int8_t val_unknown = -1;
293  };
294 
295  typedef MapUtil<2> OccMapUtil;
296 
297  typedef MapUtil<3> VoxelMapUtil;
298 
299 }
300 
301 #endif
vec_Vecf< Dim > getFreeCloud()
Get free voxels.
Definition: map_util.h:170
void dilate(const vec_Veci< Dim > &dilate_neighbor)
Dilate occupied cells.
Definition: map_util.h:222
Definition: map_util.h:11
MapUtil()
Simple constructor.
Definition: map_util.h:21
bool isOccupied(const Veci< Dim > &pn)
Check if the given cell is occupied by coordinate.
Definition: map_util.h:60
int8_t val_unknown
Assume unknown cell has value -1.
Definition: map_util.h:292
Vecf< Dim > origin_d_
Origin, float type.
Definition: map_util.h:284
Definition: map_util.h:18
bool isOutside(const Veci< Dim > &pn)
Check if the cell is outside by coordinate.
Definition: map_util.h:46
std::vector< signed char > Tmap
The type of map data Tmap is defined as a 1D array.
Definition: map_util.h:13
void info()
Print basic information about the util.
Definition: map_util.h:89
decimal_t getRes()
Get resolution.
Definition: map_util.h:25
Veci< Dim > floatToInt(const Vecf< Dim > &pt)
Float position to discrete cell coordinate.
Definition: map_util.h:99
vec_E< Vecf< N > > vec_Vecf
Vector of Eigen 1D float vector.
Definition: data_type.h:69
Vecf< Dim > intToFloat(const Veci< Dim > &pn)
Discrete cell coordinate to float position.
Definition: map_util.h:106
int8_t val_free
Assume free cell has value 0.
Definition: map_util.h:290
Vecf< Dim > getOrigin()
Get origin.
Definition: map_util.h:29
Eigen::Matrix< int, N, 1 > Veci
Eigen 1D int vector of size N.
Definition: data_type.h:58
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:48
int getIndex(const Veci< Dim > &pn)
Get index of a cell.
Definition: map_util.h:31
bool isUnknown(int idx)
Check if the cell is unknown by index.
Definition: map_util.h:41
bool isFree(const Veci< Dim > &pn)
Check if the given cell is free by coordinate.
Definition: map_util.h:53
vec_Vecf< Dim > getCloud()
Get occupied voxels.
Definition: map_util.h:144
vec_E< Veci< N > > vec_Veci
Vector of Eigen 1D int vector.
Definition: data_type.h:72
bool isOccupied(int idx)
Check if the cell is occupied by index.
Definition: map_util.h:43
decimal_t res_
Resolution.
Definition: map_util.h:282
Defines all data types used in this lib.
Tmap map_
Map entity.
Definition: map_util.h:279
Tmap getMap()
Get map data.
Definition: map_util.h:23
void freeUnknown()
Free unknown voxels.
Definition: map_util.h:256
bool isBlocked(const Vecf< Dim > &p1, const Vecf< Dim > &p2, int8_t val=100)
Check if the ray from p1 to p2 is occluded.
Definition: map_util.h:134
Eigen::Matrix< decimal_t, N, 1 > Vecf
Eigen 1D float vector of size N.
Definition: data_type.h:55
Veci< Dim > dim_
Dimension, int type.
Definition: map_util.h:286
vec_Veci< Dim > rayTrace(const Vecf< Dim > &pt1, const Vecf< Dim > &pt2)
Raytrace from float point pt1 to pt2.
Definition: map_util.h:112
int8_t val_occ
Assume occupied cell has value 100.
Definition: map_util.h:288
bool isOutsideXYZ(const Veci< Dim > &n, int i)
Check if the given cell is outside of the map in i-the dimension.
Definition: map_util.h:37
vec_Vecf< Dim > getUnknownCloud()
Get unknown voxels.
Definition: map_util.h:196
bool isFree(int idx)
Check if the cell is free by index.
Definition: map_util.h:39
Veci< Dim > getDim()
Get dimensions.
Definition: map_util.h:27
void setMap(const Vecf< Dim > &ori, const Veci< Dim > &dim, const Tmap &map, decimal_t res)
Set map.
Definition: map_util.h:81
bool isUnknown(const Veci< Dim > &pn)
Check if the given cell is unknown by coordinate.
Definition: map_util.h:67