MRSL Motion Primitive Library  1.2
A motion primitive library for generating trajectory for mobile robots
map_util.h
Go to the documentation of this file.
1 
5 #ifndef MPL_MAP_UTIL_H
6 #define MPL_MAP_UTIL_H
7 
8 #include <mpl_basis/data_type.h>
9 
10 #include <iostream>
11 
12 namespace MPL {
14 using Tmap = std::vector<signed char>;
19 template <int Dim>
20 class MapUtil {
21  public:
23  MapUtil() {}
25  Tmap getMap() { return map_; }
27  decimal_t getRes() { return res_; }
29  Veci<Dim> getDim() { return dim_; }
33  template <int U = Dim>
34  typename std::enable_if<U == 2, int>::type getIndex(const Veci<Dim> &pn) {
35  return pn(0) + dim_(0) * pn(1);
36  }
38  template <int U = Dim>
39  typename std::enable_if<U == 3, int>::type getIndex(const Veci<Dim> &pn) {
40  return pn(0) + dim_(0) * pn(1) + dim_(0) * dim_(1) * pn(2);
41  }
42 
44  bool isFree(int idx) { return map_[idx] < val_occ && map_[idx] >= val_free; }
46  bool isUnknown(int idx) { return map_[idx] == val_unknown; }
48  bool isOccupied(int idx) { return map_[idx] == val_occ; }
49 
51  bool isOutside(const Veci<Dim> &pn) {
52  for (int i = 0; i < Dim; i++)
53  if (pn(i) < 0 || pn(i) >= dim_(i)) return true;
54  return false;
55  }
57  bool isFree(const Veci<Dim> &pn) {
58  if (isOutside(pn))
59  return false;
60  else
61  return isFree(getIndex(pn));
62  }
64  bool isOccupied(const Veci<Dim> &pn) {
65  if (isOutside(pn))
66  return false;
67  else
68  return isOccupied(getIndex(pn));
69  }
71  bool isUnknown(const Veci<Dim> &pn) {
72  if (isOutside(pn)) return false;
73  return isUnknown(getIndex(pn));
74  }
75 
84  void setMap(const Vecf<Dim> &ori, const Veci<Dim> &dim, const Tmap &map,
85  decimal_t res) {
86  map_ = map;
87  dim_ = dim;
88  origin_d_ = ori;
89  res_ = res;
90  }
91 
93  void info() {
94  Vecf<Dim> range = dim_.template cast<decimal_t>() * res_;
95  std::cout << "MapUtil Info ========================== " << std::endl;
96  std::cout << " res: [" << res_ << "]" << std::endl;
97  std::cout << " origin: [" << origin_d_.transpose() << "]" << std::endl;
98  std::cout << " range: [" << range.transpose() << "]" << std::endl;
99  std::cout << " dim: [" << dim_.transpose() << "]" << std::endl;
100  };
101 
104  Veci<Dim> pn;
105  for (int i = 0; i < Dim; i++)
106  pn(i) = std::round((pt(i) - origin_d_(i)) / res_ - 0.5);
107  return pn;
108  }
111  // return pn.template cast<decimal_t>() * res_ + origin_d_;
112  return (pn.template cast<decimal_t>() + Vecf<Dim>::Constant(0.5)) * res_ +
113  origin_d_;
114  }
115 
117  vec_Veci<Dim> rayTrace(const Vecf<Dim> &pt1, const Vecf<Dim> &pt2) {
118  Vecf<Dim> diff = pt2 - pt1;
119  decimal_t k = 0.8;
120  int max_diff = (diff / res_).template lpNorm<Eigen::Infinity>() / k;
121  decimal_t s = 1.0 / max_diff;
122  Vecf<Dim> step = diff * s;
123 
124  vec_Veci<Dim> pns;
125  Veci<Dim> prev_pn = Veci<Dim>::Constant(-1);
126  for (int n = 1; n < max_diff; n++) {
127  Vecf<Dim> pt = pt1 + step * n;
128  Veci<Dim> new_pn = floatToInt(pt);
129  if (isOutside(new_pn)) break;
130  if (new_pn != prev_pn) pns.push_back(new_pn);
131  prev_pn = new_pn;
132  }
133  return pns;
134  }
135 
137  template <int U = Dim>
138  typename std::enable_if<U == 3, vec_Vec3f>::type getCloud() {
139  vec_Vecf<Dim> cloud;
140  Veci<Dim> n;
141  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
142  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
143  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
144  if (isOccupied(getIndex(n))) cloud.push_back(intToFloat(n));
145  }
146  }
147  }
148  return cloud;
149  }
151  template <int U = Dim>
152  typename std::enable_if<U == 2, vec_Vec2f>::type getCloud() {
153  vec_Vecf<Dim> cloud;
154  Veci<Dim> n;
155 
156  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
157  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
158  if (isOccupied(getIndex(n))) cloud.push_back(intToFloat(n));
159  }
160  }
161  return cloud;
162  }
163 
165  template <int U = Dim>
166  typename std::enable_if<U == 3, vec_Vec3f>::type getFreeCloud() {
167  vec_Vecf<Dim> cloud;
168  Veci<Dim> n;
169  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
170  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
171  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
172  if (isFree(getIndex(n))) cloud.push_back(intToFloat(n));
173  }
174  }
175  }
176  return cloud;
177  }
178 
180  template <int U = Dim>
181  typename std::enable_if<U == 2, vec_Vec2f>::type getFreeCloud() {
182  vec_Vecf<Dim> cloud;
183  Veci<Dim> n;
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))) cloud.push_back(intToFloat(n));
187  }
188  }
189  return cloud;
190  }
191 
193  template <int U = Dim>
194  typename std::enable_if<U == 3, vec_Vec3f>::type getUnknownCloud() {
195  vec_Vecf<Dim> cloud;
196  Veci<Dim> n;
197  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
198  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
199  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
200  if (isUnknown(getIndex(n))) cloud.push_back(intToFloat(n));
201  }
202  }
203  }
204  return cloud;
205  }
206 
208  template <int U = Dim>
209  typename std::enable_if<U == 2, vec_Vec2f>::type getUnknownCloud() {
210  vec_Vecf<Dim> cloud;
211  Veci<Dim> n;
212  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
213  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
214  if (isUnknown(getIndex(n))) cloud.push_back(intToFloat(n));
215  }
216  }
217  return cloud;
218  }
219 
221  template <int U = Dim>
222  typename std::enable_if<U == 3>::type dilate(
223  const vec_Veci<Dim> &dilate_neighbor) {
224  Tmap map = map_;
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)) map[getIndex(n + it)] = val_occ;
232  }
233  }
234  }
235  }
236  }
237  map_ = map;
238  }
239 
240  template <int U = Dim>
241  typename std::enable_if<U == 2>::type dilate(
242  const vec_Veci<Dim> &dilate_neighbor) {
243  Tmap map = map_;
245  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
246  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
247  if (isOccupied(getIndex(n))) {
248  for (const auto &it : dilate_neighbor) {
249  if (!isOutside(n + it)) map[getIndex(n + it)] = val_occ;
250  }
251  }
252  }
253  }
254 
255  map_ = map;
256  }
257 
259  void freeUnknown() {
260  Veci<Dim> n;
261  if (Dim == 3) {
262  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
263  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
264  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
265  if (isUnknown(getIndex(n))) map_[getIndex(n)] = val_free;
266  }
267  }
268  }
269  } else if (Dim == 2) {
270  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
271  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
272  if (isUnknown(getIndex(n))) map_[getIndex(n)] = val_free;
273  }
274  }
275  }
276  }
277 
279  void freeAll() {
280  Veci<Dim> n;
281  if (Dim == 3) {
282  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
283  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
284  for (n(2) = 0; n(2) < dim_(2); n(2)++) {
285  map_[getIndex(n)] = val_free;
286  }
287  }
288  }
289  } else if (Dim == 2) {
290  for (n(0) = 0; n(0) < dim_(0); n(0)++) {
291  for (n(1) = 0; n(1) < dim_(1); n(1)++) {
292  map_[getIndex(n)] = val_free;
293  }
294  }
295  }
296  }
297 
298  protected:
307 
309  int8_t val_occ = 100;
311  int8_t val_free = 0;
313  int8_t val_unknown = -1;
314 };
315 
316 typedef MapUtil<2> OccMapUtil;
317 
318 typedef MapUtil<3> VoxelMapUtil;
319 
320 } // namespace MPL
321 
322 #endif
Veci< Dim > getDim()
Get dimensions.
Definition: map_util.h:29
std::enable_if< U==3, vec_Vec3f >::type getCloud()
Get occupied voxels for 3D.
Definition: map_util.h:138
void freeAll()
Free all voxels.
Definition: map_util.h:279
Definition: map_util.h:12
Veci< Dim > floatToInt(const Vecf< Dim > &pt)
Float position to discrete cell coordinate.
Definition: map_util.h:103
bool isFree(int idx)
Check if the cell is free by index.
Definition: map_util.h:44
decimal_t res_
Map resolution.
Definition: map_util.h:300
void freeUnknown()
Free unknown voxels.
Definition: map_util.h:259
std::enable_if< U==3, int >::type getIndex(const Veci< Dim > &pn)
Get index of a cell for 3D.
Definition: map_util.h:39
decimal_t getRes()
Get resolution.
Definition: map_util.h:27
std::vector< signed char > Tmap
The type of map data Tmap is defined as a 1D array.
Definition: map_util.h:14
vec_Veci< Dim > rayTrace(const Vecf< Dim > &pt1, const Vecf< Dim > &pt2)
Raytrace from float point pt1 to pt2.
Definition: map_util.h:117
bool isUnknown(const Veci< Dim > &pn)
Check if the given cell is unknown by coordinate.
Definition: map_util.h:71
bool isOccupied(int idx)
Check if the cell is occupied by index.
Definition: map_util.h:48
Vecf< Dim > intToFloat(const Veci< Dim > &pn)
Discrete cell coordinate to float position.
Definition: map_util.h:110
bool isOutside(const Veci< Dim > &pn)
Check if the cell is outside by coordinate.
Definition: map_util.h:51
void info()
Print basic information about the util.
Definition: map_util.h:93
vec_E< Vecf< N > > vec_Vecf
Vector of Eigen 1D float vector.
Definition: data_type.h:70
Veci< Dim > dim_
Dimension, int type.
Definition: map_util.h:304
bool isOccupied(const Veci< Dim > &pn)
Check if the given cell is occupied by coordinate.
Definition: map_util.h:64
Eigen::Matrix< int, N, 1 > Veci
Eigen 1D int vector of size N.
Definition: data_type.h:59
double decimal_t
Rename the float type used in lib.
Definition: data_type.h:49
MapUtil()
Simple constructor.
Definition: map_util.h:23
std::enable_if< U==3 >::type dilate(const vec_Veci< Dim > &dilate_neighbor)
Dilate occupied cells.
Definition: map_util.h:222
Vecf< Dim > getOrigin()
Get origin.
Definition: map_util.h:31
Vecf< Dim > origin_d_
Origin, float type.
Definition: map_util.h:302
bool isFree(const Veci< Dim > &pn)
Check if the given cell is free by coordinate.
Definition: map_util.h:57
vec_E< Veci< N > > vec_Veci
Vector of Eigen 1D int vector.
Definition: data_type.h:73
Defines all data types used in this lib.
Eigen::Matrix< decimal_t, N, 1 > Vecf
Eigen 1D float vector of size N.
Definition: data_type.h:56
void setMap(const Vecf< Dim > &ori, const Veci< Dim > &dim, const Tmap &map, decimal_t res)
Set map.
Definition: map_util.h:84
int8_t val_free
Assume free cell has value 0.
Definition: map_util.h:311
std::enable_if< U==2, vec_Vec2f >::type getUnknownCloud()
Get unknown voxels for 2D.
Definition: map_util.h:209
int8_t val_occ
Assume occupied cell has value 100.
Definition: map_util.h:309
std::enable_if< U==2, vec_Vec2f >::type getFreeCloud()
Get free voxels for 2D.
Definition: map_util.h:181
std::enable_if< U==2, int >::type getIndex(const Veci< Dim > &pn)
Get index of a cell for 2D.
Definition: map_util.h:34
int8_t val_unknown
Assume unknown cell has value -1.
Definition: map_util.h:313
std::enable_if< U==3, vec_Vec3f >::type getFreeCloud()
Get free voxels for 3D.
Definition: map_util.h:166
Definition: map_util.h:20
std::enable_if< U==2, vec_Vec2f >::type getCloud()
Get occupied voxels for 2D.
Definition: map_util.h:152
Tmap map_
Map entity.
Definition: map_util.h:306
std::enable_if< U==3, vec_Vec3f >::type getUnknownCloud()
Get unknown voxels for 3D.
Definition: map_util.h:194
bool isUnknown(int idx)
Check if the cell is unknown by index.
Definition: map_util.h:46
Tmap getMap()
Get map data.
Definition: map_util.h:25