grid.cpp
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  *********************************************************************/
39 #include <iostream>
40 #include <stdexcept>
41 
43 
44 namespace ergodic_exploration
45 {
46 GridMap::GridMap(double xmin, double xmax, double ymin, double ymax, double resolution,
47  const GridData& grid_data)
48  : xsize_(axis_length(xmin, xmax, resolution))
49  , ysize_(axis_length(ymin, ymax, resolution))
50  , resolution_(resolution)
51  , xmin_(xmin)
52  , ymin_(ymin)
53  , xmax_(xmax)
54  , ymax_(ymax)
55  , grid_data_(grid_data)
56 {
57  if (xsize_ * ysize_ != grid_data_.size())
58  {
59  throw std::invalid_argument("Grid data size does not match the grid size");
60  }
61 }
62 
63 GridMap::GridMap(const nav_msgs::OccupancyGrid::ConstPtr& grid_msg)
64  : xsize_(grid_msg->info.width)
65  , ysize_(grid_msg->info.height)
66  , resolution_(grid_msg->info.resolution)
67  , xmin_(grid_msg->info.origin.position.x)
68  , ymin_(grid_msg->info.origin.position.y)
69  , xmax_(axis_upper(xmin_, resolution_, xsize_))
70  , ymax_(axis_upper(ymin_, resolution_, ysize_))
71  , grid_data_(grid_msg->data)
72 {
73  if (xsize_ * ysize_ != grid_data_.size())
74  {
75  throw std::invalid_argument("Grid data size does not match the grid size");
76  }
77 }
78 
80  : xsize_(0), ysize_(0), resolution_(0), xmin_(0.0), ymin_(0.0), xmax_(0.0), ymax_(0.0)
81 {
82 }
83 
84 void GridMap::update(const nav_msgs::OccupancyGrid::ConstPtr& grid_msg)
85 {
86  xsize_ = grid_msg->info.width;
87  ysize_ = grid_msg->info.height;
88  resolution_ = grid_msg->info.resolution;
89  xmin_ = grid_msg->info.origin.position.x;
90  ymin_ = grid_msg->info.origin.position.y;
93  grid_data_ = grid_msg->data;
94 }
95 
96 bool GridMap::gridBounds(unsigned int i, unsigned int j) const
97 {
98  // unsigned int is always greater than 0
99  return ((i <= ysize_ - 1) and (j <= xsize_ - 1)) ? true : false;
100 }
101 
102 bool GridMap::gridBounds(unsigned int idx) const
103 {
104  return (idx <= (xsize_ * ysize_ - 1)) ? true : false;
105  // std::vector<unsigned int> indices = rowMajor2Grid(idx);
106  // return gridBounds(indices.at(0), indices.at(1));
107 }
108 
109 unsigned int GridMap::grid2RowMajor(unsigned int i, unsigned int j) const
110 {
111  if (!gridBounds(i, j))
112  {
113  std::cout << "WARNING (grid2RowMajor) i and j NOT within bounds" << std::endl;
114  }
115  // ith_row * Ncols + jth_col
116  return i * xsize_ + j;
117 }
118 
119 std::vector<unsigned int> GridMap::rowMajor2Grid(unsigned int idx) const
120 {
121  const auto i = static_cast<unsigned int>(idx / xsize_);
122  const auto j = idx - i * xsize_;
123  return { i, j };
124 }
125 
126 std::vector<double> GridMap::grid2World(unsigned int i, unsigned int j) const
127 {
128  const auto x = static_cast<double>(j * resolution_) + resolution_ / 2.0 + xmin_;
129  const auto y = static_cast<double>(i * resolution_) + resolution_ / 2.0 + ymin_;
130  return { x, y };
131 }
132 
133 std::vector<double> GridMap::grid2World(unsigned int idx) const
134 {
135  const auto indices = rowMajor2Grid(idx);
136  const auto x =
137  static_cast<double>(indices.at(1) * resolution_) + resolution_ / 2.0 + xmin_;
138  const auto y =
139  static_cast<double>(indices.at(0) * resolution_) + resolution_ / 2.0 + ymin_;
140  return { x, y };
141 }
142 
143 std::vector<unsigned int> GridMap::world2Grid(double x, double y) const
144 {
145  unsigned int j = static_cast<unsigned int>(std::floor((x - xmin_) / resolution_));
146  unsigned int i = static_cast<unsigned int>(std::floor((y - ymin_) / resolution_));
147 
148  if (j == xsize_)
149  {
150  j--;
151  }
152 
153  if (i == ysize_)
154  {
155  i--;
156  }
157 
158  return { i, j };
159 }
160 
161 unsigned int GridMap::world2RowMajor(double x, double y) const
162 {
163  const auto indices = world2Grid(x, y);
164  return grid2RowMajor(indices.at(0), indices.at(1));
165 }
166 
167 double GridMap::getCell(double x, double y) const
168 {
169  return getCell(world2RowMajor(x, y));
170 }
171 
172 double GridMap::getCell(unsigned int i, unsigned int j) const
173 {
174  return getCell(grid2RowMajor(i, j));
175 }
176 
177 double GridMap::getCell(unsigned int idx) const
178 {
179  if (!gridBounds(idx))
180  {
181  throw std::invalid_argument("Grid index out of range");
182  }
183  return static_cast<double>(grid_data_.at(idx)) / 100.0;
184 }
185 
186 void GridMap::print() const
187 {
188  std::cout << "Grid \n"
189  << "x-axis: [" << xmin_ << ", " << xmax_ << "] \n"
190  << "y-axis: [" << ymin_ << ", " << ymax_ << "] \n"
191  << "xsize: " << xsize_ << "\n"
192  << "ysize: " << ysize_ << "\n"
193  << "size: " << grid_data_.size() << std::endl;
194 }
195 
196 } // namespace ergodic_exploration
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::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::xmin_
double xmin_
Definition: grid.hpp:257
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::grid2World
std::vector< double > grid2World(unsigned int i, unsigned int j) const
Convert grid coordinates to world position.
Definition: grid.cpp:126
ergodic_exploration::axis_upper
double axis_upper(double lower, double resolution, unsigned int size)
Upper axis limit.
Definition: grid.hpp:73
grid.hpp
2D grid represented in row major order
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::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