test_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 <gtest/gtest.h>
41 
42 TEST(GridTest, Grid2RowMajor)
43 {
44  const auto xmin = 0.0;
45  const auto xmax = 2.0;
46  const auto ymin = 0.0;
47  const auto ymax = 3.0;
48  const auto resolution = 1.0;
49  const auto xsize = ergodic_exploration::axis_length(xmin, xmax, resolution);
50  const auto ysize = ergodic_exploration::axis_length(ymin, ymax, resolution);
51  const ergodic_exploration::GridData grid_data(xsize * ysize, 0);
52 
53  const ergodic_exploration::GridMap grid_map(xmin, xmax, ymin, ymax, resolution,
54  grid_data);
55 
56  // Convert grid index to row major order
57  const auto i = 2;
58  const auto j = 1;
59 
60  const auto idx = grid_map.grid2RowMajor(i, j);
61  ASSERT_EQ(idx, 5);
62 }
63 
64 TEST(GridTest, RowMajor2Grid)
65 {
66  const auto xmin = 0.0;
67  const auto xmax = 2.0;
68  const auto ymin = 0.0;
69  const auto ymax = 3.0;
70  const auto resolution = 1.0;
71  const auto xsize = ergodic_exploration::axis_length(xmin, xmax, resolution);
72  const auto ysize = ergodic_exploration::axis_length(ymin, ymax, resolution);
73  const ergodic_exploration::GridData grid_data(xsize * ysize, 0);
74 
75  const ergodic_exploration::GridMap grid_map(xmin, xmax, ymin, ymax, resolution,
76  grid_data);
77 
78  // Convert row major order index to row and column ndex
79  const auto idx = 5;
80 
81  const auto indices = grid_map.rowMajor2Grid(idx);
82  ASSERT_EQ(indices.at(0), 2);
83  ASSERT_EQ(indices.at(1), 1);
84 }
85 
86 TEST(GridTest, GridBoundsRowMajor)
87 {
88  const auto xmin = 0.0;
89  const auto xmax = 2.0;
90  const auto ymin = 0.0;
91  const auto ymax = 3.0;
92  const auto resolution = 1.0;
93  const auto xsize = ergodic_exploration::axis_length(xmin, xmax, resolution);
94  const auto ysize = ergodic_exploration::axis_length(ymin, ymax, resolution);
95  const ergodic_exploration::GridData grid_data(xsize * ysize, 0);
96 
97  const ergodic_exploration::GridMap grid_map(xmin, xmax, ymin, ymax, resolution,
98  grid_data);
99 
100  const auto idx1 = 5;
101  ASSERT_TRUE(grid_map.gridBounds(idx1));
102 
103  const auto idx2 = 6;
104  ASSERT_FALSE(grid_map.gridBounds(idx2));
105 }
106 
107 TEST(GridTest, GridBoundsCoordinates)
108 {
109  const auto xmin = 0.0;
110  const auto xmax = 2.0;
111  const auto ymin = 0.0;
112  const auto ymax = 3.0;
113  const auto resolution = 1.0;
114  const auto xsize = ergodic_exploration::axis_length(xmin, xmax, resolution);
115  const auto ysize = ergodic_exploration::axis_length(ymin, ymax, resolution);
116  const ergodic_exploration::GridData grid_data(xsize * ysize, 0);
117 
118  const ergodic_exploration::GridMap grid_map(xmin, xmax, ymin, ymax, resolution,
119  grid_data);
120 
121  const auto i1 = 2;
122  const auto j1 = 0;
123  ASSERT_TRUE(grid_map.gridBounds(i1, j1));
124 
125  const auto i2 = 0;
126  const auto j2 = 2;
127  ASSERT_FALSE(grid_map.gridBounds(i2, j2));
128 }
129 
130 TEST(GridTest, Grid2World)
131 {
132  const auto xmin = -0.5;
133  const auto xmax = 0.5;
134  const auto ymin = 0.0;
135  const auto ymax = 1.5;
136  const auto resolution = 0.5;
137  const auto xsize = ergodic_exploration::axis_length(xmin, xmax, resolution);
138  const auto ysize = ergodic_exploration::axis_length(ymin, ymax, resolution);
139  const ergodic_exploration::GridData grid_data(xsize * ysize, 0);
140 
141  const ergodic_exploration::GridMap grid_map(xmin, xmax, ymin, ymax, resolution,
142  grid_data);
143 
144  const auto i1 = 0;
145  const auto j1 = 1;
146 
147  const auto coords1 = grid_map.grid2World(i1, j1);
148  ASSERT_DOUBLE_EQ(coords1.at(0), 0.25);
149  ASSERT_DOUBLE_EQ(coords1.at(1), 0.25);
150 
151  const auto idx2 = 4;
152  const auto coords2 = grid_map.grid2World(idx2);
153  ASSERT_DOUBLE_EQ(coords2.at(0), -0.25);
154  ASSERT_DOUBLE_EQ(coords2.at(1), 1.25);
155 }
156 
157 TEST(GridTest, World2Grid)
158 {
159  const auto xmin = -0.5;
160  const auto xmax = 0.5;
161  const auto ymin = 0.0;
162  const auto ymax = 1.5;
163  const auto resolution = 0.5;
164  const auto xsize = ergodic_exploration::axis_length(xmin, xmax, resolution);
165  const auto ysize = ergodic_exploration::axis_length(ymin, ymax, resolution);
166  const ergodic_exploration::GridData grid_data(xsize * ysize, 0);
167 
168  const ergodic_exploration::GridMap grid_map(xmin, xmax, ymin, ymax, resolution,
169  grid_data);
170 
171  const auto x1 = -0.25;
172  const auto y1 = 1.25;
173 
174  const auto indices1 = grid_map.world2Grid(x1, y1);
175  ASSERT_EQ(indices1.at(0), 2);
176  ASSERT_EQ(indices1.at(1), 0);
177 
178  const auto x2 = 0.25;
179  const auto y2 = 0.75;
180  const auto idx = grid_map.world2RowMajor(x2, y2);
181  ASSERT_EQ(idx, 3);
182 }
183 
184 TEST(GridTest, GetCellValue)
185 {
186  const auto xmin = -0.5;
187  const auto xmax = 0.5;
188  const auto ymin = 0.0;
189  const auto ymax = 1.5;
190  const auto resolution = 0.5;
191  const auto xsize = ergodic_exploration::axis_length(xmin, xmax, resolution);
192  const auto ysize = ergodic_exploration::axis_length(ymin, ymax, resolution);
193  ergodic_exploration::GridData grid_data(xsize * ysize, 0);
194 
195  grid_data.at(3) = 100;
196  grid_data.at(4) = 90;
197 
198  const ergodic_exploration::GridMap grid_map(xmin, xmax, ymin, ymax, resolution,
199  grid_data);
200 
201  const auto x = -0.25;
202  const auto y = 1.25;
203 
204  const unsigned int i = 1;
205  const unsigned int j = 1;
206 
207  ASSERT_EQ(grid_map.getCell(4), 0.9);
208  ASSERT_EQ(grid_map.getCell(x, y), 0.9);
209  ASSERT_EQ(grid_map.getCell(i, j), 1.0);
210 }
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::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::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
TEST
TEST(GridTest, Grid2RowMajor)
Definition: test_grid.cpp:42
grid.hpp
2D grid represented in row major order
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::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