Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions grid_map_core/src/GridMapMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ bool getIndexFromPosition(
internal::getVectorToOrigin(offset, mapLength);
Vector indexVector = ((position - offset - mapPosition).array() / resolution).matrix();
index = internal::getIndexFromIndexVector(indexVector, bufferSize, bufferStartIndex);
if (!checkIfPositionWithinMap(position, mapLength, mapPosition)) {return false;}
return true;
return checkIfPositionWithinMap(position, mapLength, mapPosition) && checkIfIndexInRange(index,
bufferSize);
}

bool checkIfPositionWithinMap(
Expand Down
111 changes: 111 additions & 0 deletions grid_map_core/test/GridMapMathTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,121 @@ TEST(checkIfPositionWithinMap, EdgeCases)
grid_map::Length mapLength(2.0, 3.0);
grid_map::Position mapPosition(0.0, 0.0);

/*
*
* A (is inside) B (is not inside)
* +-----------------------+
* | |
* | |
* | X |
* | ^ |
* | | |
* | | |
* | <-----+ |
* | Y |
* | |
* | |
* | |
* +-----------------------+
* C (is not inside) D (is not inside)
*
* Resulting coordinates are:
* A: (1.0, 1.5)
* B: (1.0, -1.5)
* C: (-1.0, 1.5)
* D: (-1.0, -1.5)
*
*/

// Noise around A.
EXPECT_TRUE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0, 1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0 + DBL_EPSILON, 1.5), mapLength,
mapPosition));
EXPECT_TRUE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0 - DBL_EPSILON, 1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0, 1.5 + DBL_EPSILON), mapLength,
mapPosition));
EXPECT_TRUE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0, 1.5 - DBL_EPSILON), mapLength,
mapPosition));

// Noise around B.
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0, -1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0 + DBL_EPSILON, -1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0 - DBL_EPSILON, -1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0, -1.5 + DBL_EPSILON), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(1.0, -1.5 - DBL_EPSILON), mapLength,
mapPosition));

// Noise around C.
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0, 1.5), mapLength,
mapPosition));
EXPECT_TRUE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0 + DBL_EPSILON, 1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0 - DBL_EPSILON, 1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0, 1.5 + DBL_EPSILON), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0, 1.5 - DBL_EPSILON), mapLength,
mapPosition));

// Noise around D.
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0, -1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0 + DBL_EPSILON, -1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0 - DBL_EPSILON, -1.5), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0, -1.5 + DBL_EPSILON), mapLength,
mapPosition));
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0, -1.5 - DBL_EPSILON), mapLength,
mapPosition));

// Extra tests.
EXPECT_FALSE(
grid_map::checkIfPositionWithinMap(
grid_map::Position(-1.0, 1.5), mapLength,
Expand Down
Loading