testSignedDistance2d.cpp
Go to the documentation of this file.
1 /*
2  * testSignedDistance2d.cpp
3  *
4  * Created on: Jul 10, 2020
5  * Author: Ruben Grandia
6  * Institute: ETH Zurich
7  */
8 
9 #include <gtest/gtest.h>
10 
13 
14 #include "naiveSignedDistance.hpp"
15 
16 using namespace grid_map;
17 using namespace signed_distance_field;
18 
19 TEST(testSignedDistance2d, signedDistance2d_noObstacles) {
20  const int n = 3;
21  const int m = 4;
22  const float resolution = 0.1;
23  const Matrix map = Matrix::Ones(n, m);
24 
25  const auto occupancy = occupancyAtHeight(map, 2.0);
26  const auto signedDistance = signedDistanceFromOccupancy(occupancy, resolution);
27 
28  ASSERT_TRUE((signedDistance.array() == INF).all());
29 }
30 
31 TEST(testSignedDistance2d, signedDistance2d_allObstacles) {
32  const int n = 3;
33  const int m = 4;
34  const float resolution = 0.1;
35  const Matrix map = Matrix::Ones(n, m);
36 
37  const auto occupancy = occupancyAtHeight(map, 0.0);
38  const auto signedDistance = signedDistanceFromOccupancy(occupancy, resolution);
39 
40  ASSERT_TRUE((signedDistance.array() == -INF).all());
41 }
42 
43 TEST(testSignedDistance2d, signedDistance2d_mixed) {
44  const int n = 2;
45  const int m = 3;
46  const float resolution = 1.0;
47  Matrix map(n, m);
48  map << 0.0, 1.0, 1.0, 0.0, 0.0, 1.0;
49  const auto occupancy = occupancyAtHeight(map, 0.5);
50 
51  const auto naiveSignedDistance = naiveSignedDistanceFromOccupancy(occupancy, resolution);
52  const auto signedDistance = signedDistanceFromOccupancy(occupancy, resolution);
53  ASSERT_TRUE(isEqualSdf(signedDistance, naiveSignedDistance, 1e-4));
54 }
55 
56 TEST(testSignedDistance2d, signedDistance2d_oneObstacle) {
57  const int n = 20;
58  const int m = 30;
59  const float resolution = 0.1;
60  Matrix map = Matrix::Zero(n, m);
61  map(n / 2, m / 2) = 1.0;
62  const auto occupancy = occupancyAtHeight(map, 0.5);
63 
64  const auto naiveSignedDistance = naiveSignedDistanceFromOccupancy(occupancy, resolution);
65  const auto signedDistance = signedDistanceFromOccupancy(occupancy, resolution);
66  ASSERT_TRUE(isEqualSdf(signedDistance, naiveSignedDistance, 1e-4));
67 }
68 
69 TEST(testSignedDistance2d, signedDistance2d_oneFreeSpace) {
70  const int n = 20;
71  const int m = 30;
72  const float resolution = 0.1;
73  Matrix map = Matrix::Ones(n, m);
74  map(n / 2, m / 2) = 0.0;
75 
76  const auto occupancy = occupancyAtHeight(map, 0.5);
77 
78  const auto naiveSignedDistance = naiveSignedDistanceFromOccupancy(occupancy, resolution);
79  const auto signedDistance = signedDistanceFromOccupancy(occupancy, resolution);
80  ASSERT_TRUE(isEqualSdf(signedDistance, naiveSignedDistance, 1e-4));
81 }
82 
83 TEST(testSignedDistance2d, signedDistance2d_debugcase) {
84  const int n = 3;
85  const int m = 3;
86  const float resolution = 1.0;
87  Matrix map(n, m);
88  map << 1.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0;
89 
90  const auto occupancy = occupancyAtHeight(map, 0.5);
91 
92  const auto naiveSignedDistance = naiveSignedDistanceFromOccupancy(occupancy, resolution);
93  const auto signedDistance = signedDistanceFromOccupancy(occupancy, resolution);
94  ASSERT_TRUE(isEqualSdf(signedDistance, naiveSignedDistance, 1e-4));
95 }
96 
97 TEST(testSignedDistance2d, signedDistance2d_random) {
98  const int n = 20;
99  const int m = 30;
100  const float resolution = 1.0;
101  Matrix map = Matrix::Random(n, m); // random [-1.0, 1.0]
102 
103  // Check at different heights, resulting in different levels of sparsity.
104  float heightStep = 0.1;
105  for (float height = -1.0 - heightStep; height < 1.0 + heightStep; height += heightStep) {
106  const auto occupancy = occupancyAtHeight(map, height);
107 
108  const auto naiveSignedDistance = naiveSignedDistanceFromOccupancy(occupancy, resolution);
109  const auto signedDistance = signedDistanceFromOccupancy(occupancy, resolution);
110  ASSERT_TRUE(isEqualSdf(signedDistance, naiveSignedDistance, 1e-4)) << "height: " << height;
111  }
112 }
Matrix naiveSignedDistanceFromOccupancy(const Eigen::Matrix< bool, -1, -1 > &occupancyGrid, float resolution)
Eigen::Matrix< bool, -1, -1 > occupancyAtHeight(const Matrix &elevationMap, float height)
Matrix signedDistanceFromOccupancy(const Eigen::Matrix< bool, -1, -1 > &occupancyGrid, float resolution)
Eigen::MatrixXf Matrix
constexpr float INF
Distance value that is considered infinite.
Definition: Utils.hpp:18
TEST(GridMap, CopyConstructor)
bool isEqualSdf(const Matrix &sdf0, const Matrix &sdf1, float tol)


grid_map_sdf
Author(s): Takahiro Miki , Péter Fankhauser
autogenerated on Wed Jul 5 2023 02:23:42