testPixelBorderDistance.cpp
Go to the documentation of this file.
1 /*
2  * testPixelBorderDistance.cpp
3  *
4  * Created on: Aug 7, 2020
5  * Author: Ruben Grandia
6  * Institute: ETH Zurich
7  */
8 
9 #include <gtest/gtest.h>
10 
12 #include "grid_map_sdf/Utils.hpp"
13 
14 using namespace grid_map;
15 using namespace signed_distance_field;
16 
17 TEST(testPixelBorderDistance, distanceFunction) {
18  // Basic properties of the distance function
19  ASSERT_TRUE(pixelBorderDistance(0, 0) == 0.0F);
20  ASSERT_FLOAT_EQ(pixelBorderDistance(0, 1), 0.5);
21  ASSERT_FLOAT_EQ(pixelBorderDistance(0, 2), 1.5);
22  ASSERT_TRUE(pixelBorderDistance(0, 1) == pixelBorderDistance(1, 0));
23  ASSERT_TRUE(pixelBorderDistance(-10, 42) == pixelBorderDistance(42, -10));
24 }
25 
26 TEST(testPixelBorderDistance, equidistantPoint) {
27  int pixelRange = 10;
28  float offsetRange = 20.0;
29  float offsetStep = 0.25;
30  float tol = 1e-4;
31 
32  for (int p = -pixelRange; p < pixelRange; ++p) {
33  for (float fp = -offsetRange; fp < offsetRange; fp += offsetStep) {
34  for (int q = -pixelRange; q < pixelRange; ++q) {
35  for (float fq = -offsetRange; fq < offsetRange; fq += offsetStep) {
36  // Fix that offset is the same if pixels are the same
37  if (p == q) {
38  fp = fq;
39  }
40  // Check symmetry of the equidistant point computation
41  float s0 = equidistancePoint(q, fq, p, fp);
42  float s1 = equidistancePoint(p, fp, q, fq);
43  ASSERT_LT(std::abs(s0 - s1), tol);
44 
45  // Check that the distance from s0 to p and q is indeed equal
46  float dp = squarePixelBorderDistance(s0, p, fp);
47  float dq = squarePixelBorderDistance(s0, q, fq);
48  ASSERT_LT(std::abs(dp - dq), tol) << "p: " << p << ", q: " << q << ", fp: " << fp << ", fq: " << fq;
49  }
50  }
51  }
52  }
53 }
54 
55 TEST(testPixelBorderDistance, equidistantPointInfCases) {
56  const float pixelTestDistance{1e6}; // Pick a very high pixel index
57  // With one of the cells at +INF, the intersection will always be +- INF on the side of that cell
58  // Here the intersection is at the left = -INF
59  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, INF, pixelTestDistance, std::numeric_limits<float>::max()), -INF);
60  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, INF, pixelTestDistance, 0.0F), -INF);
61  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, INF, pixelTestDistance, std::numeric_limits<float>::lowest()), -INF);
62  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, INF, pixelTestDistance, -INF), -INF);
63  // Here the intersection is at the right = +INF
64  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, std::numeric_limits<float>::max(), pixelTestDistance, INF), INF);
65  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, 0.0F, pixelTestDistance, INF), INF);
66  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, std::numeric_limits<float>::lowest(), pixelTestDistance, INF), INF);
67  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, -INF, pixelTestDistance, INF), INF);
68  // Except when both are infinite, then the intersection is in the middle
69  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, INF, pixelTestDistance, INF), 0.0F);
70  EXPECT_FLOAT_EQ(
71  equidistancePoint(-pixelTestDistance, std::numeric_limits<float>::max(), pixelTestDistance, std::numeric_limits<float>::max()), 0.0F);
72  EXPECT_FLOAT_EQ(
73  equidistancePoint(-pixelTestDistance, std::numeric_limits<float>::lowest(), pixelTestDistance, std::numeric_limits<float>::lowest()),
74  0.0F);
75  EXPECT_FLOAT_EQ(equidistancePoint(-pixelTestDistance, -INF, pixelTestDistance, -INF), 0.0F);
76 }
float squarePixelBorderDistance(float i, float j, float f)
float pixelBorderDistance(float i, float j)
float equidistancePoint(float q, float fq, float p, float fp)
constexpr float INF
Distance value that is considered infinite.
Definition: Utils.hpp:18
TEST(GridMap, CopyConstructor)


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