|
| 1 | +// Copyright (c) 2023 Samsung R&D Institute Russia |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. Reserved. |
| 14 | + |
| 15 | +#include <gtest/gtest.h> |
| 16 | + |
| 17 | +#include <string> |
| 18 | +#include <memory> |
| 19 | + |
| 20 | +#include "rclcpp/rclcpp.hpp" |
| 21 | +#include "nav2_util/occ_grid_values.hpp" |
| 22 | +#include "nav_msgs/msg/occupancy_grid.hpp" |
| 23 | +#include "geometry_msgs/msg/pose2_d.hpp" |
| 24 | +#include "nav2_costmap_2d/costmap_2d.hpp" |
| 25 | +#include "nav2_costmap_2d/costmap_filters/costmap_filter.hpp" |
| 26 | + |
| 27 | +class CostmapFilterWrapper : public nav2_costmap_2d::CostmapFilter |
| 28 | +{ |
| 29 | +public: |
| 30 | + CostmapFilterWrapper() {} |
| 31 | + |
| 32 | + bool worldToMask( |
| 33 | + nav_msgs::msg::OccupancyGrid::ConstSharedPtr filter_mask, |
| 34 | + double wx, double wy, unsigned int & mx, unsigned int & my) const |
| 35 | + { |
| 36 | + return nav2_costmap_2d::CostmapFilter::worldToMask(filter_mask, wx, wy, mx, my); |
| 37 | + } |
| 38 | + |
| 39 | + // API coverage |
| 40 | + void initializeFilter(const std::string &) {} |
| 41 | + void process( |
| 42 | + nav2_costmap_2d::Costmap2D &, int, int, int, int, const geometry_msgs::msg::Pose2D &) |
| 43 | + {} |
| 44 | + void resetFilter() {} |
| 45 | +}; |
| 46 | + |
| 47 | +TEST(CostmapFilter, testWorldToMask) |
| 48 | +{ |
| 49 | + // Create occupancy grid for test as follows: |
| 50 | + // |
| 51 | + // ^ |
| 52 | + // | (6,6) |
| 53 | + // | *-----* |
| 54 | + // | |/////| <- mask |
| 55 | + // | |/////| |
| 56 | + // | *-----* |
| 57 | + // | (3,3) |
| 58 | + // *----------------> |
| 59 | + // (0,0) |
| 60 | + |
| 61 | + const unsigned int width = 3; |
| 62 | + const unsigned int height = 3; |
| 63 | + |
| 64 | + auto mask = std::make_shared<nav_msgs::msg::OccupancyGrid>(); |
| 65 | + mask->header.frame_id = "map"; |
| 66 | + mask->info.resolution = 1.0; |
| 67 | + mask->info.width = width; |
| 68 | + mask->info.height = height; |
| 69 | + mask->info.origin.position.x = 3.0; |
| 70 | + mask->info.origin.position.y = 3.0; |
| 71 | + |
| 72 | + mask->data.resize(width * height, nav2_util::OCC_GRID_OCCUPIED); |
| 73 | + |
| 74 | + CostmapFilterWrapper cf; |
| 75 | + unsigned int mx, my; |
| 76 | + // Point inside mask |
| 77 | + ASSERT_TRUE(cf.worldToMask(mask, 4.0, 5.0, mx, my)); |
| 78 | + ASSERT_EQ(mx, 1); |
| 79 | + ASSERT_EQ(my, 2); |
| 80 | + // Corner cases |
| 81 | + ASSERT_TRUE(cf.worldToMask(mask, 3.0, 3.0, mx, my)); |
| 82 | + ASSERT_EQ(mx, 0); |
| 83 | + ASSERT_EQ(my, 0); |
| 84 | + ASSERT_TRUE(cf.worldToMask(mask, 5.9, 5.9, mx, my)); |
| 85 | + ASSERT_EQ(mx, 2); |
| 86 | + ASSERT_EQ(my, 2); |
| 87 | + // Point outside mask |
| 88 | + ASSERT_FALSE(cf.worldToMask(mask, 2.9, 2.9, mx, my)); |
| 89 | + ASSERT_FALSE(cf.worldToMask(mask, 6.0, 6.0, mx, my)); |
| 90 | +} |
| 91 | + |
| 92 | +int main(int argc, char ** argv) |
| 93 | +{ |
| 94 | + // Initialize the system |
| 95 | + testing::InitGoogleTest(&argc, argv); |
| 96 | + rclcpp::init(argc, argv); |
| 97 | + |
| 98 | + // Actual testing |
| 99 | + bool test_result = RUN_ALL_TESTS(); |
| 100 | + |
| 101 | + // Shutdown |
| 102 | + rclcpp::shutdown(); |
| 103 | + |
| 104 | + return test_result; |
| 105 | +} |
0 commit comments