coordinates_test.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2019, Locus Robotics
  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
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of the copyright holder nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. // Tests ripped from https://github.com/locusrobotics/robot_navigation/blob/master/nav_grid/test/utest.cpp
  35. #include <gtest/gtest.h>
  36. #include <costmap_2d/costmap_2d.h>
  37. using namespace costmap_2d;
  38. TEST(CostmapCoordinates, easy_coordinates_test)
  39. {
  40. Costmap2D costmap(2, 3, 1.0, 0, 0);
  41. double wx, wy;
  42. costmap.mapToWorld(0u, 0u, wx, wy);
  43. EXPECT_DOUBLE_EQ(wx, 0.5);
  44. EXPECT_DOUBLE_EQ(wy, 0.5);
  45. costmap.mapToWorld(1u, 2u, wx, wy);
  46. EXPECT_DOUBLE_EQ(wx, 1.5);
  47. EXPECT_DOUBLE_EQ(wy, 2.5);
  48. unsigned int umx, umy;
  49. int mx, my;
  50. ASSERT_TRUE(costmap.worldToMap(wx, wy, umx, umy));
  51. EXPECT_EQ(umx, 1);
  52. EXPECT_EQ(umy, 2);
  53. costmap.worldToMapNoBounds(wx, wy, mx, my);
  54. EXPECT_EQ(mx, 1);
  55. EXPECT_EQ(my, 2);
  56. // Invalid Coordinate
  57. wx = 2.5;
  58. EXPECT_FALSE(costmap.worldToMap(wx, wy, umx, umy));
  59. costmap.worldToMapEnforceBounds(wx, wy, mx, my);
  60. EXPECT_EQ(mx, 1);
  61. EXPECT_EQ(my, 2);
  62. costmap.worldToMapNoBounds(wx, wy, mx, my);
  63. EXPECT_EQ(mx, 2);
  64. EXPECT_EQ(my, 2);
  65. // Border Cases
  66. EXPECT_TRUE(costmap.worldToMap(0.0, wy, umx, umy));
  67. EXPECT_EQ(umx, 0);
  68. EXPECT_TRUE(costmap.worldToMap(0.25, wy, umx, umy));
  69. EXPECT_EQ(umx, 0);
  70. EXPECT_TRUE(costmap.worldToMap(0.75, wy, umx, umy));
  71. EXPECT_EQ(umx, 0);
  72. EXPECT_TRUE(costmap.worldToMap(0.9999, wy, umx, umy));
  73. EXPECT_EQ(umx, 0);
  74. EXPECT_TRUE(costmap.worldToMap(1.0, wy, umx, umy));
  75. EXPECT_EQ(umx, 1);
  76. EXPECT_TRUE(costmap.worldToMap(1.25, wy, umx, umy));
  77. EXPECT_EQ(umx, 1);
  78. EXPECT_TRUE(costmap.worldToMap(1.75, wy, umx, umy));
  79. EXPECT_EQ(umx, 1);
  80. EXPECT_TRUE(costmap.worldToMap(1.9999, wy, umx, umy));
  81. EXPECT_EQ(umx, 1);
  82. EXPECT_FALSE(costmap.worldToMap(2.0, wy, umx, umy));
  83. costmap.worldToMapEnforceBounds(2.0, wy, mx, my);
  84. EXPECT_EQ(mx, 1);
  85. }
  86. TEST(CostmapCoordinates, hard_coordinates_test)
  87. {
  88. Costmap2D costmap(2, 3, 0.1, -0.2, 0.2);
  89. double wx, wy;
  90. costmap.mapToWorld(0, 0, wx, wy);
  91. EXPECT_DOUBLE_EQ(wx, -0.15);
  92. EXPECT_DOUBLE_EQ(wy, 0.25);
  93. costmap.mapToWorld(1, 2, wx, wy);
  94. EXPECT_DOUBLE_EQ(wx, -0.05);
  95. EXPECT_DOUBLE_EQ(wy, 0.45);
  96. unsigned int umx, umy;
  97. int mx, my;
  98. EXPECT_TRUE(costmap.worldToMap(wx, wy, umx, umy));
  99. EXPECT_EQ(umx, 1);
  100. EXPECT_EQ(umy, 2);
  101. costmap.worldToMapNoBounds(wx, wy, mx, my);
  102. EXPECT_EQ(mx, 1);
  103. EXPECT_EQ(my, 2);
  104. // Invalid Coordinate
  105. wx = 2.5;
  106. EXPECT_FALSE(costmap.worldToMap(wx, wy, umx, umy));
  107. costmap.worldToMapEnforceBounds(wx, wy, mx, my);
  108. EXPECT_EQ(mx, 1);
  109. EXPECT_EQ(my, 2);
  110. costmap.worldToMapNoBounds(wx, wy, mx, my);
  111. EXPECT_EQ(mx, 27);
  112. EXPECT_EQ(my, 2);
  113. }
  114. int main(int argc, char** argv)
  115. {
  116. testing::InitGoogleTest( &argc, argv );
  117. return RUN_ALL_TESTS();
  118. }