grid.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  * BSD 3-Clause License
3  *
4  * Copyright (c) 2020 Northwestern University
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *********************************************************************/
38 #ifndef GRID_HPP
39 #define GRID_HPP
40 
41 #include <iosfwd>
42 #include <cmath>
43 #include <vector>
44 // #include <memory>
45 
46 #include <geometry_msgs/Pose.h>
47 #include <nav_msgs/OccupancyGrid.h>
48 
49 namespace ergodic_exploration
50 {
52 typedef std::vector<int8_t> GridData;
53 
61 inline unsigned int axis_length(double lower, double upper, double resolution)
62 {
63  return static_cast<unsigned int>(std::round((upper - lower) / resolution));
64 }
65 
73 inline double axis_upper(double lower, double resolution, unsigned int size)
74 {
75  return static_cast<double>(resolution * size) + lower;
76 }
77 
79 class GridMap
80 {
81 private:
82  friend class OccupancyMapper;
83 
84 public:
94  GridMap(double xmin, double xmax, double ymin, double ymax, double resolution,
95  const GridData& grid_data);
96 
101  GridMap(const nav_msgs::OccupancyGrid::ConstPtr& grid_msg);
102 
106  GridMap();
107 
112  void update(const nav_msgs::OccupancyGrid::ConstPtr& grid_msg);
113 
120  bool gridBounds(unsigned int i, unsigned int j) const;
121 
127  bool gridBounds(unsigned int idx) const;
128 
135  unsigned int grid2RowMajor(unsigned int i, unsigned int j) const;
136 
142  std::vector<unsigned int> rowMajor2Grid(unsigned int idx) const;
143 
150  std::vector<double> grid2World(unsigned int i, unsigned int j) const;
151 
157  std::vector<double> grid2World(unsigned int idx) const;
158 
165  std::vector<unsigned int> world2Grid(double x, double y) const;
166 
173  unsigned int world2RowMajor(double x, double y) const;
174 
181  double getCell(double x, double y) const;
182 
189  double getCell(unsigned int i, unsigned int j) const;
190 
196  double getCell(unsigned int idx) const;
197 
199  void print() const;
200 
202  const GridData& gridData() const
203  {
204  return grid_data_;
205  }
206 
208  double resolution() const
209  {
210  return resolution_;
211  }
212 
214  double xmin() const
215  {
216  return xmin_;
217  }
218 
220  double ymin() const
221  {
222  return ymin_;
223  }
224 
226  double xmax() const
227  {
228  return xmax_;
229  }
230 
232  double ymax() const
233  {
234  return ymax_;
235  }
236 
238  unsigned int xsize() const
239  {
240  return xsize_;
241  }
242 
244  unsigned int ysize() const
245  {
246  return ysize_;
247  }
248 
250  unsigned int size() const
251  {
252  return xsize_ * ysize_;
253  }
254 
255 private:
256  unsigned int xsize_, ysize_;
259 };
260 
261 } // namespace ergodic_exploration
262 #endif
ergodic_exploration::GridMap::xsize_
unsigned int xsize_
Definition: grid.hpp:256
ergodic_exploration::GridMap::gridBounds
bool gridBounds(unsigned int i, unsigned int j) const
Test if coordinates are within the grid.
Definition: grid.cpp:96
ergodic_exploration::GridMap::grid2RowMajor
unsigned int grid2RowMajor(unsigned int i, unsigned int j) const
Converts grid indices to row major order index.
Definition: grid.cpp:109
ergodic_exploration::GridMap::resolution
double resolution() const
Return grid resolution.
Definition: grid.hpp:208
ergodic_exploration::GridMap::gridData
const GridData & gridData() const
Return the grid data
Definition: grid.hpp:202
ergodic_exploration::GridMap::world2RowMajor
unsigned int world2RowMajor(double x, double y) const
Convert world coordinates to row major index.
Definition: grid.cpp:161
ergodic_exploration::GridMap::rowMajor2Grid
std::vector< unsigned int > rowMajor2Grid(unsigned int idx) const
Convert row major index to row and column indices.
Definition: grid.cpp:119
ergodic_exploration::GridMap::ymin_
double ymin_
Definition: grid.hpp:257
ergodic_exploration::GridMap::update
void update(const nav_msgs::OccupancyGrid::ConstPtr &grid_msg)
Update the grid.
Definition: grid.cpp:84
ergodic_exploration::GridMap::ymax
double ymax() const
Return grid ymax.
Definition: grid.hpp:232
ergodic_exploration::GridMap::ysize
unsigned int ysize() const
Return y-axis size.
Definition: grid.hpp:244
ergodic_exploration::GridMap::xmin_
double xmin_
Definition: grid.hpp:257
ergodic_exploration::GridMap::xmin
double xmin() const
Return grid xmin.
Definition: grid.hpp:214
ergodic_exploration::GridMap::ymax_
double ymax_
Definition: grid.hpp:257
ergodic_exploration::GridMap::grid_data_
GridData grid_data_
Definition: grid.hpp:258
ergodic_exploration::GridMap::world2Grid
std::vector< unsigned int > world2Grid(double x, double y) const
Convert world coordinates to row and column index in grid.
Definition: grid.cpp:143
ergodic_exploration
Definition: basis.hpp:43
ergodic_exploration::GridMap
Constructs an 2D grid.
Definition: grid.hpp:79
ergodic_exploration::GridMap::grid2World
std::vector< double > grid2World(unsigned int i, unsigned int j) const
Convert grid coordinates to world position.
Definition: grid.cpp:126
ergodic_exploration::GridMap::ymin
double ymin() const
Return grid ymin.
Definition: grid.hpp:220
ergodic_exploration::axis_upper
double axis_upper(double lower, double resolution, unsigned int size)
Upper axis limit.
Definition: grid.hpp:73
ergodic_exploration::GridMap::xsize
unsigned int xsize() const
Return grid x-axis size.
Definition: grid.hpp:238
ergodic_exploration::OccupancyMapper
Occupancy grid mapping.
Definition: mapping.hpp:52
ergodic_exploration::GridMap::xmax
double xmax() const
Return grid xmax.
Definition: grid.hpp:226
ergodic_exploration::GridMap::print
void print() const
Print grid properties.
Definition: grid.cpp:186
ergodic_exploration::GridMap::ysize_
unsigned int ysize_
Definition: grid.hpp:256
ergodic_exploration::GridMap::resolution_
double resolution_
Definition: grid.hpp:257
ergodic_exploration::GridMap::xmax_
double xmax_
Definition: grid.hpp:257
ergodic_exploration::GridData
std::vector< int8_t > GridData
Occupancy grid data.
Definition: grid.hpp:52
ergodic_exploration::axis_length
unsigned int axis_length(double lower, double upper, double resolution)
Length of a grid axis.
Definition: grid.hpp:61
ergodic_exploration::GridMap::size
unsigned int size() const
Return grid size.
Definition: grid.hpp:250
ergodic_exploration::GridMap::GridMap
GridMap()
Constructor a empty grid.
Definition: grid.cpp:79
ergodic_exploration::GridMap::getCell
double getCell(double x, double y) const
Get the value of the cell at a specified position.
Definition: grid.cpp:167


ergodic_exploration
Author(s): bostoncleek
autogenerated on Wed Mar 2 2022 00:17:13