feat(costmap_2d): background reference graph, shared keys under root:

- ReferenceGraph / ReferenceGraphWorker: Voronoi ridges and one-way lanes are
  computed on a low-priority thread when the map changes (reference_graph_*
  keys, off with rolling_window), with unit tests.
- Shared costmap keys can live in a root: map or flat at the top of the YAML;
  costmap_common_root_test covers both layouts.
- Static and directional layers report map changes to the layered costmap.
This commit is contained in:
2026-11-10 16:37:55 +07:00
parent f5d190a3d0
commit 5034029c6e
25 changed files with 3445 additions and 21 deletions

View File

@@ -0,0 +1,98 @@
#include <robot_costmap_2d/costmap_2d_robot.h>
#include <cstdlib>
#include <stdexcept>
#include <string>
#include <gtest/gtest.h>
namespace
{
using robot_costmap_2d::Costmap2DROBOT;
TEST(CostmapCommonParameters, SelectAndCopySharedValues)
{
const char* layout = std::getenv("COSTMAP_TEST_LAYOUT");
ASSERT_NE(layout, nullptr);
const std::string expected_layout(layout);
ASSERT_TRUE(expected_layout == "rooted" || expected_layout == "flat");
ASSERT_NE(std::getenv("PNKX_NAV_CORE_CONFIG_DIR"), nullptr);
robot::NodeHandle tree_nh("~");
EXPECT_EQ(tree_nh.hasParam("root"), expected_layout == "rooted");
robot::NodeHandle common_nh = Costmap2DROBOT::commonParametersHandle(tree_nh);
double tolerance = 0.0;
ASSERT_TRUE(common_nh.getParam("transform_tolerance", tolerance));
EXPECT_DOUBLE_EQ(tolerance, 1.0);
std::string frame;
ASSERT_TRUE(common_nh.getParam("robot_base_frame", frame));
EXPECT_EQ(frame, "base_link");
Costmap2DROBOT::copyParentParameters("local_costmap", "obstacles", "VoxelLayer", common_nh, tree_nh);
Costmap2DROBOT::copyParentParameters("global_costmap", "obstacles", "VoxelLayer", common_nh, tree_nh);
Costmap2DROBOT::copyParentParameters("local_costmap", "forbidden", "StaticLayer", common_nh, tree_nh);
Costmap2DROBOT::copyParentParameters("global_costmap", "preferred", "PreferredLayer", common_nh, tree_nh);
std::string value;
ASSERT_TRUE(tree_nh.getParam("local_costmap/obstacles/observation_sources", value));
EXPECT_EQ(value, "f_scan b_scan");
ASSERT_TRUE(tree_nh.getParam("local_costmap/obstacles/f_scan/topic", value));
EXPECT_EQ(value, "/f_scan");
ASSERT_TRUE(tree_nh.getParam("local_costmap/forbidden/map_topic", value));
EXPECT_EQ(value, "forbidden");
ASSERT_TRUE(tree_nh.getParam("global_costmap/preferred/map_topic", value));
EXPECT_EQ(value, "preferred");
int voxels = 0;
double resolution = 0.0;
ASSERT_TRUE(tree_nh.getParam("global_costmap/obstacles/z_voxels", voxels));
EXPECT_EQ(voxels, 10);
ASSERT_TRUE(tree_nh.getParam("global_costmap/obstacles/z_resolution", resolution));
EXPECT_DOUBLE_EQ(resolution, 0.2);
ASSERT_TRUE(tree_nh.getParam("local_costmap/obstacles/z_voxels", voxels));
EXPECT_EQ(voxels, 8);
ASSERT_TRUE(tree_nh.getParam("local_costmap/obstacles/z_resolution", resolution));
EXPECT_DOUBLE_EQ(resolution, 0.15);
}
TEST(CostmapCommonParameters, PreservesSourceOverridesAndFillsMissingSourceKeys)
{
robot::NodeHandle tree_nh("~");
robot::NodeHandle common_nh = Costmap2DROBOT::commonParametersHandle(tree_nh);
Costmap2DROBOT::copyParentParameters("override_costmap", "obstacles", "VoxelLayer", common_nh, tree_nh);
std::string value;
ASSERT_TRUE(tree_nh.getParam("override_costmap/obstacles/observation_sources", value));
EXPECT_EQ(value, "b_scan");
ASSERT_TRUE(tree_nh.getParam("override_costmap/obstacles/b_scan/topic", value));
EXPECT_EQ(value, "/local_override");
ASSERT_TRUE(tree_nh.getParam("override_costmap/obstacles/b_scan/data_type", value));
EXPECT_EQ(value, "LaserScan");
EXPECT_FALSE(tree_nh.hasParam("override_costmap/obstacles/f_scan/topic"));
}
TEST(CostmapCommonParameters, RejectsInvalidSharedTypeOnlyWhenCopied)
{
robot::NodeHandle tree_nh("~");
robot::NodeHandle common_nh = Costmap2DROBOT::commonParametersHandle(tree_nh);
EXPECT_THROW(Costmap2DROBOT::copyParentParameters("invalid_costmap", "bad_obstacles", "VoxelLayer",
common_nh, tree_nh), std::invalid_argument);
EXPECT_NO_THROW(Costmap2DROBOT::copyParentParameters("protected_costmap", "bad_obstacles", "VoxelLayer",
common_nh, tree_nh));
int voxels = 0;
ASSERT_TRUE(tree_nh.getParam("protected_costmap/bad_obstacles/z_voxels", voxels));
EXPECT_EQ(voxels, 12);
}
TEST(CostmapCommonParameters, CreatesMissingCostmapNamespace)
{
robot::NodeHandle tree_nh("~");
robot::NodeHandle common_nh = Costmap2DROBOT::commonParametersHandle(tree_nh);
ASSERT_FALSE(tree_nh.hasParam("missing_costmap"));
Costmap2DROBOT::copyParentParameters("missing_costmap", "forbidden", "StaticLayer", common_nh, tree_nh);
std::string topic;
ASSERT_TRUE(tree_nh.getParam("missing_costmap/forbidden/map_topic", topic));
EXPECT_EQ(topic, "forbidden");
}
} // namespace

View File

@@ -0,0 +1,30 @@
robot_base_frame: base_link
transform_tolerance: 1.0
footprint_padding: 0.0
forbidden:
map_topic: forbidden
use_maximum: true
preferred:
map_topic: preferred
obstacles:
z_voxels: 8
z_resolution: 0.15
observation_sources: f_scan b_scan
f_scan: {topic: /f_scan, data_type: LaserScan, marking: true}
b_scan: {topic: /b_scan, data_type: LaserScan, clearing: true}
bad_obstacles:
z_voxels: invalid_integer
global_costmap:
obstacles: {enabled: false, z_voxels: 10, z_resolution: 0.2}
preferred: {}
local_costmap:
obstacles: {enabled: true}
forbidden: {}
override_costmap:
obstacles:
observation_sources: b_scan
b_scan: {topic: /local_override}
invalid_costmap:
bad_obstacles: {}
protected_costmap:
bad_obstacles: {z_voxels: 12}

View File

@@ -0,0 +1,35 @@
root:
robot_base_frame: base_link
transform_tolerance: 1.0
footprint_padding: 0.0
forbidden:
map_topic: forbidden
use_maximum: true
preferred:
map_topic: preferred
obstacles:
z_voxels: 8
z_resolution: 0.15
observation_sources: f_scan b_scan
f_scan: {topic: /f_scan, data_type: LaserScan, marking: true}
b_scan: {topic: /b_scan, data_type: LaserScan, clearing: true}
bad_obstacles:
z_voxels: invalid_integer
robot_base_frame: ignored_frame
transform_tolerance: 9.0
footprint_padding: 9.0
obstacles: {z_voxels: 99}
global_costmap:
obstacles: {enabled: false, z_voxels: 10, z_resolution: 0.2}
preferred: {}
local_costmap:
obstacles: {enabled: true}
forbidden: {}
override_costmap:
obstacles:
observation_sources: b_scan
b_scan: {topic: /local_override}
invalid_costmap:
bad_obstacles: {}
protected_costmap:
bad_obstacles: {z_voxels: 12}

View File

@@ -0,0 +1,17 @@
reference_graph_fixture 1
size 200 60
resolution 0.05
origin -2.5 1.25
params 0.335 0.7 1.0 0.5 0.3 0.3
cost 3
254 2000 60 8000 254 2000
direction 1
0 12000
skeleton 200
5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999
nodes 2
-2.475 2.725
7.475000000000001 2.725
edges 1
0 1 41 -2.475 2.725 -2.225 2.725 -1.975 2.725 -1.725 2.725 -1.4749999999999999 2.725 -1.2249999999999999 2.725 -0.9749999999999999 2.725 -0.7249999999999999 2.725 -0.4750000000000001 2.725 -0.2250000000000001 2.725 0.025000000000000355 2.725 0.27500000000000036 2.725 0.5250000000000004 2.725 0.7750000000000004 2.725 1.0250000000000004 2.725 1.2750000000000004 2.725 1.5250000000000004 2.725 1.7750000000000004 2.725 2.0250000000000004 2.725 2.2750000000000004 2.725 2.5250000000000004 2.725 2.7750000000000004 2.725 3.0250000000000004 2.725 3.2750000000000004 2.725 3.5250000000000004 2.725 3.7750000000000004 2.725 4.025 2.725 4.275 2.725 4.525 2.725 4.775 2.725 5.025 2.725 5.275 2.725 5.525 2.725 5.775 2.725 6.025 2.725 6.275 2.725 6.525 2.725 6.775 2.725 7.025 2.725 7.275 2.725 7.475000000000001 2.725
lanes 0

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,28 @@
reference_graph_fixture 1
size 200 60
resolution 0.05
origin -2.5 1.25
params 0.335 0.7 1.0 0.5 0.3 0.3
cost 53
254 2000 60 95 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 190 254 10 60 3095 254 2000
direction 81
0 2050 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 2050
skeleton 214
5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6075 6076 6124 6276 6277 6323 6477 6478 6522 6678 6679 6721 6879 6880 6919 6920 7080 7081 7118 7281 7282 7317 7482 7483 7516 7683 7684 7685 7714 7715 7885 7886 7887 7913 8087 8088 8089 8110 8111 8112 8289 8290 8291 8292 8293 8294 8305 8306 8307 8308 8309 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504
nodes 6
4.9750000000000005 2.725
0.025000000000000355 2.725
5.025 2.725
-0.02499999999999991 2.725
-2.475 2.725
7.475000000000001 2.725
edges 5
0 1 24 4.9750000000000005 2.725 4.7250000000000005 2.725 4.4750000000000005 2.725 4.2250000000000005 2.725 3.9750000000000005 2.725 3.7250000000000005 2.725 3.5250000000000004 2.975 3.2750000000000004 3.175 3.0250000000000004 3.275 2.7750000000000004 3.325 2.5250000000000004 3.375 2.2750000000000004 3.375 2.075 3.325 1.875 3.275 1.7250000000000005 3.175 1.5750000000000002 3.075 1.475 2.925 1.3250000000000002 2.825 1.1750000000000003 2.725 0.9250000000000003 2.725 0.6750000000000003 2.725 0.42500000000000027 2.725 0.17500000000000027 2.725 0.025000000000000355 2.725
0 2 2 4.9750000000000005 2.725 5.025 2.725
3 4 11 -0.02499999999999991 2.725 -0.2749999999999999 2.725 -0.5249999999999999 2.725 -0.7749999999999999 2.725 -1.025 2.725 -1.275 2.725 -1.525 2.725 -1.775 2.725 -2.025 2.725 -2.275 2.725 -2.475 2.725
3 1 2 -0.02499999999999991 2.725 0.025000000000000355 2.725
5 2 11 7.475000000000001 2.725 7.225000000000001 2.725 6.975 2.725 6.725 2.725 6.475 2.725 6.225 2.725 5.975 2.725 5.725 2.725 5.4750000000000005 2.725 5.2250000000000005 2.725 5.025 2.725
lanes 3
0 3.5388358909926865e-16 3.3500000000000014 4.950495049504951 3.3500000000000014 4.950495049504951 0.0
0 3.5388358909926865e-16 2.8500000000000014 1.9306930693069315 2.8500000000000014 1.930693069306931 0.5
0 3.5388358909926865e-16 2.3500000000000014 1.9306930693069315 2.3500000000000014 1.930693069306931 1.0

View File

@@ -0,0 +1,28 @@
reference_graph_fixture 1
size 200 60
resolution 0.05
origin -2.5 1.25
params 0.335 0.7 1.0 0.5 0.3 0.3
cost 3
254 2000 60 8000 254 2000
direction 81
0 2050 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 100 1 100 0 2050
skeleton 200
5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999
nodes 6
4.9750000000000005 2.725
0.025000000000000355 2.725
5.025 2.725
-0.02499999999999991 2.725
-2.475 2.725
7.475000000000001 2.725
edges 5
0 1 21 4.9750000000000005 2.725 4.7250000000000005 2.725 4.4750000000000005 2.725 4.2250000000000005 2.725 3.9750000000000005 2.725 3.7250000000000005 2.725 3.4750000000000005 2.725 3.2250000000000005 2.725 2.9750000000000005 2.725 2.7250000000000005 2.725 2.4750000000000005 2.725 2.2250000000000005 2.725 1.9750000000000005 2.725 1.7250000000000005 2.725 1.475 2.725 1.225 2.725 0.9750000000000001 2.725 0.7250000000000001 2.725 0.4750000000000001 2.725 0.2250000000000001 2.725 0.025000000000000355 2.725
0 2 2 4.9750000000000005 2.725 5.025 2.725
3 4 11 -0.02499999999999991 2.725 -0.2749999999999999 2.725 -0.5249999999999999 2.725 -0.7749999999999999 2.725 -1.025 2.725 -1.275 2.725 -1.525 2.725 -1.775 2.725 -2.025 2.725 -2.275 2.725 -2.475 2.725
3 1 2 -0.02499999999999991 2.725 0.025000000000000355 2.725
5 2 11 7.475000000000001 2.725 7.225000000000001 2.725 6.975 2.725 6.725 2.725 6.475 2.725 6.225 2.725 5.975 2.725 5.725 2.725 5.4750000000000005 2.725 5.2250000000000005 2.725 5.025 2.725
lanes 3
0 3.5388358909926865e-16 2.75 4.950495049504951 2.75 4.950495049504951 0.0
0 3.5388358909926865e-16 2.25 4.950495049504951 2.25 4.950495049504951 0.5
0 3.5388358909926865e-16 3.25 4.950495049504951 3.25 4.950495049504951 0.5

View File

@@ -0,0 +1,14 @@
reference_graph_fixture 1
size 120 120
resolution 0.05
origin -2.5 1.25
params 0.335 0.7 1.0 0.5 0.3 0.3
cost 241
254 1253 60 14 254 101 60 24 254 92 60 32 254 86 60 36 254 81 60 42 254 76 60 46 254 72 60 50 254 69 60 52 254 66 60 56 254 63 60 58 254 60 60 62 254 57 60 64 254 55 60 66 254 53 60 68 254 51 60 70 254 49 60 72 254 47 60 74 254 45 60 76 254 43 60 78 254 41 60 80 254 40 60 80 254 39 60 82 254 37 60 84 254 36 60 84 254 35 60 86 254 33 60 88 254 32 60 88 254 31 60 90 254 30 60 90 254 29 60 92 254 28 60 92 254 28 60 92 254 27 60 94 254 26 60 94 254 25 60 96 254 24 60 96 254 24 60 96 254 24 60 96 254 23 60 98 254 22 60 98 254 22 60 46 254 6 60 46 254 22 60 44 254 10 60 44 254 22 60 42 254 14 60 42 254 21 60 42 254 16 60 42 254 20 60 42 254 16 60 42 254 20 60 41 254 18 60 41 254 20 60 41 254 18 60 41 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 40 254 20 60 41 254 18 60 41 254 20 60 41 254 18 60 41 254 20 60 42 254 16 60 42 254 20 60 42 254 16 60 42 254 21 60 42 254 14 60 42 254 22 60 44 254 10 60 44 254 22 60 46 254 6 60 46 254 22 60 98 254 22 60 98 254 23 60 96 254 24 60 96 254 24 60 96 254 24 60 96 254 25 60 94 254 26 60 94 254 27 60 92 254 28 60 92 254 28 60 92 254 29 60 90 254 30 60 90 254 31 60 88 254 32 60 88 254 33 60 86 254 35 60 84 254 36 60 84 254 37 60 82 254 39 60 80 254 40 60 80 254 41 60 78 254 43 60 76 254 45 60 74 254 47 60 72 254 49 60 70 254 51 60 68 254 53 60 66 254 55 60 64 254 57 60 62 254 60 60 58 254 63 60 56 254 66 60 52 254 69 60 50 254 72 60 46 254 76 60 42 254 81 60 36 254 86 60 32 254 92 60 24 254 101 60 14 254 1253
direction 1
0 14400
skeleton 205
3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3651 3652 3653 3654 3664 3665 3666 3667 3668 3768 3769 3770 3788 3789 3790 3791 3886 3887 3911 3912 3913 4004 4005 4033 4034 4035 4123 4155 4156 4241 4242 4276 4277 4278 4360 4398 4399 4479 4519 4520 4598 4640 4641 4717 4761 4762 4836 4882 4883 4955 5003 5075 5123 5124 5194 5244 5245 5313 5365 5433 5485 5486 5552 5606 5672 5726 5727 5791 5847 5911 5967 6031 6087 6088 6150 6208 6270 6328 6390 6448 6510 6568 6569 6629 6689 6749 6809 6869 6929 6989 7049 7109 7169 7229 7289 7349 7409 7469 7529 7589 7649 7709 7710 7769 7830 7888 7950 8008 8070 8128 8190 8191 8248 8311 8367 8431 8487 8551 8552 8607 8672 8726 8792 8793 8846 8913 8965 9033 9034 9085 9154 9155 9204 9275 9323 9395 9396 9443 9516 9517 9562 9637 9638 9681 9758 9759 9800 9879 9880 9919 10000 10001 10002 10037 10038 10122 10123 10156 10243 10244 10245 10274 10275 10365 10366 10367 10392 10393 10487 10488 10489 10490 10509 10510 10511 10610 10611 10612 10613 10614 10625 10626 10627 10628 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744
nodes 0
edges 0
lanes 0

View File

@@ -0,0 +1,21 @@
reference_graph_fixture 1
size 200 120
resolution 0.05
origin -2.5 1.25
params 0.335 0.7 1.0 0.5 0.3 0.3
cost 143
254 2000 60 8000 254 80 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 160 60 40 254 80
direction 1
0 24000
skeleton 290
5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6085 6086 6087 6088 6089 6090 6109 6110 6111 6112 6113 6290 6291 6292 6293 6306 6307 6308 6493 6494 6495 6504 6505 6695 6696 6697 6698 6701 6702 6703 6898 6899 6900 7099 7299 7499 7699 7899 8099 8299 8499 8699 8899 9099 9299 9499 9699 9899 10099 10299 10499 10699 10899 11099 11299 11499 11699 11899 12099 12299 12499 12699 12899 13099 13299 13499 13699 13899 14099 14299 14499 14699 14899 15099 15299 15499 15699 15899 16099 16299 16499 16699 16899 17099 17299 17499 17699 17899 18099 18299 18499 18699 18899 19099 19299 19499 19699 19899 20099 20299 20499 20699 20899 21099 21299 21499 21699 21899 22099 22299 22499 22699 22899 23099 23299 23499 23699 23899
nodes 4
2.4750000000000005 2.975
-2.475 2.725
7.475000000000001 2.725
2.4750000000000005 7.2250000000000005
edges 3
0 1 22 2.4750000000000005 2.975 2.2750000000000004 2.925 2.125 2.825 1.9249999999999998 2.7750000000000004 1.7250000000000005 2.725 1.475 2.725 1.225 2.725 0.9750000000000001 2.725 0.7250000000000001 2.725 0.4750000000000001 2.725 0.2250000000000001 2.725 -0.02499999999999991 2.725 -0.2749999999999999 2.725 -0.5249999999999999 2.725 -0.7749999999999999 2.725 -1.025 2.725 -1.275 2.725 -1.525 2.725 -1.775 2.725 -2.025 2.725 -2.275 2.725 -2.475 2.725
0 2 21 2.4750000000000005 2.975 2.7250000000000005 2.875 2.9750000000000005 2.7750000000000004 3.2250000000000005 2.725 3.4750000000000005 2.725 3.7250000000000005 2.725 3.9750000000000005 2.725 4.2250000000000005 2.725 4.4750000000000005 2.725 4.7250000000000005 2.725 4.9750000000000005 2.725 5.2250000000000005 2.725 5.4750000000000005 2.725 5.725 2.725 5.975 2.725 6.225 2.725 6.475 2.725 6.725 2.725 6.975 2.725 7.225000000000001 2.725 7.475000000000001 2.725
0 3 18 2.4750000000000005 2.975 2.4750000000000005 3.225 2.4750000000000005 3.475 2.4750000000000005 3.725 2.4750000000000005 3.975 2.4750000000000005 4.225 2.4750000000000005 4.475 2.4750000000000005 4.725 2.4750000000000005 4.975 2.4750000000000005 5.225 2.4750000000000005 5.4750000000000005 2.4750000000000005 5.7250000000000005 2.4750000000000005 5.9750000000000005 2.4750000000000005 6.2250000000000005 2.4750000000000005 6.4750000000000005 2.4750000000000005 6.7250000000000005 2.4750000000000005 6.9750000000000005 2.4750000000000005 7.2250000000000005
lanes 0

View File

@@ -0,0 +1,37 @@
reference_graph_fixture 1
size 200 60
resolution 0.05
origin -2.5 1.25
params 0.335 0.7 1.0 0.5 0.3 0.3
cost 3
254 2000 60 8000 254 2000
direction 121
0 2040 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 80 1 60 2 60 0 2040
skeleton 200
5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999
nodes 8
2.5250000000000004 2.725
2.4750000000000005 2.725
5.4750000000000005 2.725
-0.5249999999999999 2.725
-2.475 2.725
-0.4750000000000001 2.725
7.475000000000001 2.725
5.525 2.725
edges 7
0 1 2 2.5250000000000004 2.725 2.4750000000000005 2.725
0 2 13 2.5250000000000004 2.725 2.7750000000000004 2.725 3.0250000000000004 2.725 3.2750000000000004 2.725 3.5250000000000004 2.725 3.7750000000000004 2.725 4.025 2.725 4.275 2.725 4.525 2.725 4.775 2.725 5.025 2.725 5.275 2.725 5.4750000000000005 2.725
3 4 9 -0.5249999999999999 2.725 -0.7749999999999999 2.725 -1.025 2.725 -1.275 2.725 -1.525 2.725 -1.775 2.725 -2.025 2.725 -2.275 2.725 -2.475 2.725
3 5 2 -0.5249999999999999 2.725 -0.4750000000000001 2.725
6 7 9 7.475000000000001 2.725 7.225000000000001 2.725 6.975 2.725 6.725 2.725 6.475 2.725 6.225 2.725 5.975 2.725 5.725 2.725 5.525 2.725
7 2 2 5.525 2.725 5.4750000000000005 2.725
1 5 13 2.4750000000000005 2.725 2.2250000000000005 2.725 1.9750000000000005 2.725 1.7250000000000005 2.725 1.475 2.725 1.225 2.725 0.9750000000000001 2.725 0.7250000000000001 2.725 0.4750000000000001 2.725 0.2250000000000001 2.725 -0.02499999999999991 2.725 -0.2749999999999999 2.725 -0.4750000000000001 2.725
lanes 8
0 -0.5000000000000001 2.75 2.4508196721311477 2.75 2.9508196721311477 0.0
0 -0.5000000000000001 2.25 2.4508196721311477 2.25 2.9508196721311477 0.5
0 -0.5000000000000001 3.25 2.4508196721311477 3.25 2.9508196721311477 0.5
1 3.454225352112677 2.2042253521126765 4.545774647887324 3.2957746478873235 1.5436838180833075 0.0
1 4.159891418762288 2.20278463757574 5.251440714536936 3.294333933350387 1.5436838180833075 0.5
1 2.7485592854630645 2.205666066649612 3.8401085812377125 3.29721536242426 1.543683818083308 0.5
1 4.8655574854119 2.201343923038804 5.499360302313308 2.835146739940213 0.8963325395322439 1.0
1 2.500639697686692 2.664853260059787 3.134442514588101 3.2986560769611954 0.8963325395322439 1.0

View File

@@ -0,0 +1,20 @@
reference_graph_fixture 1
size 200 60
resolution 0.05
origin -2.5 1.25
params 0.335 0.7 1.0 0.5 0.3 0.3
cost 83
254 2000 60 90 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 180 255 20 60 90 254 2000
direction 1
0 12000
skeleton 180
5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999
nodes 4
3.0250000000000004 2.725
7.475000000000001 2.725
1.9750000000000005 2.725
-2.475 2.725
edges 2
0 1 19 3.0250000000000004 2.725 3.2750000000000004 2.725 3.5250000000000004 2.725 3.7750000000000004 2.725 4.025 2.725 4.275 2.725 4.525 2.725 4.775 2.725 5.025 2.725 5.275 2.725 5.525 2.725 5.775 2.725 6.025 2.725 6.275 2.725 6.525 2.725 6.775 2.725 7.025 2.725 7.275 2.725 7.475000000000001 2.725
2 3 19 1.9750000000000005 2.725 1.7250000000000005 2.725 1.475 2.725 1.225 2.725 0.9750000000000001 2.725 0.7250000000000001 2.725 0.4750000000000001 2.725 0.2250000000000001 2.725 -0.02499999999999991 2.725 -0.2749999999999999 2.725 -0.5249999999999999 2.725 -0.7749999999999999 2.725 -1.025 2.725 -1.275 2.725 -1.525 2.725 -1.775 2.725 -2.025 2.725 -2.275 2.725 -2.475 2.725
lanes 0

View File

@@ -0,0 +1,569 @@
#include <robot_costmap_2d/reference_graph.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <limits>
#include <set>
#include <stdexcept>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
#include <robot_costmap_2d/cost_values.h>
using namespace robot_costmap_2d;
namespace
{
constexpr double kExactM = 1e-9;
/** Expected output written by the Python prototype, oneway_eval/r12_export_cpp_text.py. */
struct Fixture
{
DirectionGeometry geometry{};
ReferenceGraphParams params;
double half_x = 0.0, half_y = 0.0;
std::vector<uint8_t> cost, direction;
std::vector<int64_t> skeleton;
std::vector<ReferencePoint> nodes;
struct Edge
{
uint32_t from, to;
std::vector<ReferencePoint> points;
};
std::vector<Edge> edges;
std::vector<ReferenceLane> lanes;
};
std::vector<uint8_t> readRuns(std::istream& in, const std::string& name)
{
std::string key;
std::size_t runs = 0;
in >> key >> runs;
if (key != name)
throw std::runtime_error("expected " + name + ", got " + key);
std::vector<uint8_t> out;
for (std::size_t i = 0; i < runs; ++i)
{
int value = 0;
std::size_t count = 0;
in >> value >> count;
out.insert(out.end(), count, static_cast<uint8_t>(value));
}
return out;
}
Fixture loadFixture(const std::string& path)
{
std::ifstream in(path);
if (!in)
throw std::runtime_error("cannot open " + path);
Fixture f;
std::string key;
int version = 0;
in >> key >> version;
if (key != "reference_graph_fixture" || version != 1)
throw std::runtime_error("bad fixture header in " + path);
f.geometry.frame_id = "map";
in >> key >> f.geometry.width >> f.geometry.height;
in >> key >> f.geometry.resolution;
in >> key >> f.geometry.origin_x >> f.geometry.origin_y;
in >> key >> f.params.min_clearance_m >> f.params.ridge_min_separation_m >> f.params.spur_min_length_m >>
f.params.lane_offset_step_m >> f.half_x >> f.half_y;
f.cost = readRuns(in, "cost");
f.direction = readRuns(in, "direction");
std::size_t count = 0;
in >> key >> count;
f.skeleton.resize(count);
for (int64_t& cell : f.skeleton)
in >> cell;
in >> key >> count;
f.nodes.resize(count);
for (ReferencePoint& node : f.nodes)
in >> node.x >> node.y;
in >> key >> count;
f.edges.resize(count);
for (Fixture::Edge& edge : f.edges)
{
std::size_t points = 0;
in >> edge.from >> edge.to >> points;
edge.points.resize(points);
for (ReferencePoint& point : edge.points)
in >> point.x >> point.y;
}
in >> key >> count;
f.lanes.resize(count);
for (ReferenceLane& lane : f.lanes)
{
int direction = 0;
in >> direction >> lane.entry.x >> lane.entry.y >> lane.exit.x >> lane.exit.y >> lane.length_m >>
lane.offset_from_centre_m;
lane.direction_index = static_cast<uint8_t>(direction);
}
if (!in)
throw std::runtime_error("truncated fixture " + path);
return f;
}
std::vector<robot_geometry_msgs::Point> rectangle(double half_x, double half_y)
{
std::vector<robot_geometry_msgs::Point> out(4);
out[0].x = half_x, out[0].y = half_y;
out[1].x = -half_x, out[1].y = half_y;
out[2].x = -half_x, out[2].y = -half_y;
out[3].x = half_x, out[3].y = -half_y;
return out;
}
ReferenceGridView viewOf(const Fixture& f)
{
ReferenceGridView view;
view.cost = f.cost.data();
view.geometry = f.geometry;
const bool zones = std::any_of(f.direction.begin(), f.direction.end(), [](uint8_t code) { return code != 0; });
view.direction_codes = zones ? f.direction.data() : nullptr;
return view;
}
double pointToSegment(const ReferencePoint& p, const ReferencePoint& a, const ReferencePoint& b)
{
const double dx = b.x - a.x, dy = b.y - a.y;
const double squared = dx * dx + dy * dy;
const double t = squared <= 0.0 ? 0.0 : std::min(1.0, std::max(0.0, ((p.x - a.x) * dx + (p.y - a.y) * dy) / squared));
return std::hypot(p.x - a.x - t * dx, p.y - a.y - t * dy);
}
double pointToPolyline(const ReferencePoint& p, const std::vector<ReferencePoint>& line)
{
double best = std::numeric_limits<double>::infinity();
for (std::size_t i = 0; i + 1 < line.size(); ++i)
best = std::min(best, pointToSegment(p, line[i], line[i + 1]));
return best;
}
double polylineLength(const std::vector<ReferencePoint>& line)
{
double total = 0.0;
for (std::size_t i = 0; i + 1 < line.size(); ++i)
total += std::hypot(line[i + 1].x - line[i].x, line[i + 1].y - line[i].y);
return total;
}
std::vector<std::pair<double, double>> sortedPoints(const std::vector<ReferencePoint>& points)
{
std::vector<std::pair<double, double>> out;
out.reserve(points.size());
for (const ReferencePoint& point : points)
out.emplace_back(point.x, point.y);
std::sort(out.begin(), out.end());
return out;
}
/** Both sides read the same chain of cells, but a walk from the other end keeps different cells.
* So edges are matched by end nodes and compared as polylines, within one and a half cells. */
void expectEdgesMatch(const Fixture& f, const ReferenceGraph& graph)
{
using Ends = std::tuple<double, double, double, double>;
const auto ends = [](const ReferencePoint& a, const ReferencePoint& b) {
return a.x < b.x || (a.x == b.x && a.y < b.y) ? Ends(a.x, a.y, b.x, b.y) : Ends(b.x, b.y, a.x, a.y);
};
std::vector<std::pair<Ends, std::size_t>> expected, actual;
for (std::size_t i = 0; i < f.edges.size(); ++i)
expected.emplace_back(ends(f.nodes[f.edges[i].from], f.nodes[f.edges[i].to]), i);
for (std::size_t i = 0; i < graph.edges.size(); ++i)
actual.emplace_back(ends(graph.nodes[graph.edges[i].from], graph.nodes[graph.edges[i].to]), i);
std::sort(expected.begin(), expected.end());
std::sort(actual.begin(), actual.end());
ASSERT_EQ(expected.size(), actual.size());
const double tolerance = 1.5 * f.geometry.resolution;
for (std::size_t i = 0; i < expected.size(); ++i)
{
ASSERT_EQ(expected[i].first, actual[i].first) << "edge end nodes differ at sorted position " << i;
// Parallel edges share their end nodes: pick the closest one among them.
const Fixture::Edge& python = f.edges[expected[i].second];
double best = std::numeric_limits<double>::infinity();
for (std::size_t j = i; j < actual.size() && actual[j].first == expected[i].first; ++j)
{
double worst = 0.0;
for (const ReferencePoint& point : graph.edges[actual[j].second].points)
worst = std::max(worst, pointToPolyline(point, python.points));
best = std::min(best, worst);
}
EXPECT_LE(best, tolerance) << "edge " << i << " leaves the Python polyline";
const VoronoiEdge& edge = graph.edges[actual[i].second];
EXPECT_NEAR(edge.length_m, polylineLength(edge.points), kExactM);
}
}
void expectLanesMatch(const Fixture& f, const ReferenceGraph& graph)
{
const auto order = [](const ReferenceLane& a, const ReferenceLane& b) {
return std::make_tuple(a.direction_index, a.entry.x, a.entry.y) < std::make_tuple(b.direction_index, b.entry.x, b.entry.y);
};
std::vector<ReferenceLane> expected = f.lanes, actual = graph.lanes;
std::sort(expected.begin(), expected.end(), order);
std::sort(actual.begin(), actual.end(), order);
ASSERT_EQ(expected.size(), actual.size());
for (std::size_t i = 0; i < expected.size(); ++i)
{
EXPECT_EQ(expected[i].direction_index, actual[i].direction_index);
EXPECT_NEAR(expected[i].entry.x, actual[i].entry.x, kExactM);
EXPECT_NEAR(expected[i].entry.y, actual[i].entry.y, kExactM);
EXPECT_NEAR(expected[i].exit.x, actual[i].exit.x, kExactM);
EXPECT_NEAR(expected[i].exit.y, actual[i].exit.y, kExactM);
EXPECT_NEAR(expected[i].length_m, actual[i].length_m, kExactM);
EXPECT_NEAR(expected[i].offset_from_centre_m, actual[i].offset_from_centre_m, kExactM);
}
}
/** Run steps 1 and 2 on a fixture and compare every stage. Returns step times [ms] for the timing test. */
std::pair<double, double> expectParity(const Fixture& f)
{
const ReferenceGridView view = viewOf(f);
const auto t0 = std::chrono::steady_clock::now();
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, f.params);
const auto t1 = std::chrono::steady_clock::now();
const ReferenceGraph graph = buildReferenceGraph(view, skeleton, rectangle(f.half_x, f.half_y), f.params);
const auto t2 = std::chrono::steady_clock::now();
std::vector<int64_t> cells;
for (std::size_t i = 0; i < skeleton.mask.size(); ++i)
if (skeleton.mask[i] != 0)
cells.push_back(static_cast<int64_t>(i));
std::vector<int64_t> only_cpp, only_python;
std::set_difference(cells.begin(), cells.end(), f.skeleton.begin(), f.skeleton.end(), std::back_inserter(only_cpp));
std::set_difference(f.skeleton.begin(), f.skeleton.end(), cells.begin(), cells.end(), std::back_inserter(only_python));
EXPECT_TRUE(only_cpp.empty()) << only_cpp.size() << " skeleton cells only in C++";
EXPECT_TRUE(only_python.empty()) << only_python.size() << " skeleton cells only in Python";
const std::vector<std::pair<double, double>> expected_nodes = sortedPoints(f.nodes), actual_nodes = sortedPoints(graph.nodes);
EXPECT_EQ(expected_nodes.size(), actual_nodes.size());
for (std::size_t i = 0; i < std::min(expected_nodes.size(), actual_nodes.size()); ++i)
{
EXPECT_NEAR(expected_nodes[i].first, actual_nodes[i].first, kExactM);
EXPECT_NEAR(expected_nodes[i].second, actual_nodes[i].second, kExactM);
}
expectEdgesMatch(f, graph);
expectLanesMatch(f, graph);
EXPECT_EQ(graph.static_cost, f.cost);
EXPECT_EQ(graph.clearance_mm, skeleton.clearance_mm);
const auto ms = [](std::chrono::steady_clock::time_point a, std::chrono::steady_clock::time_point b) {
return std::chrono::duration<double, std::milli>(b - a).count();
};
return { ms(t0, t1), ms(t1, t2) };
}
std::string packagedFixture(const std::string& name)
{
return std::string(ROBOT_COSTMAP_2D_DIR) + "/test/data/reference_graph/" + name + ".fixture";
}
DirectionGeometry geometryOf(uint32_t width, uint32_t height)
{
return { width, height, 0.05, 0.0, 0.0, "map" };
}
/** Free corridor 2 m wide between two lethal walls, as in the corridor fixture. */
std::vector<uint8_t> corridorCost(uint32_t width, uint32_t height)
{
std::vector<uint8_t> cost(static_cast<std::size_t>(width) * height, FREE_SPACE);
for (uint32_t y = 0; y < height; ++y)
if (y < 10 || y >= height - 10)
std::fill_n(cost.begin() + static_cast<std::ptrdiff_t>(y * width), width, LETHAL_OBSTACLE);
return cost;
}
} // namespace
// Exact answers from the Python prototype, one scene per behaviour.
TEST(ReferenceGraphParity, Corridor)
{
const Fixture f = loadFixture(packagedFixture("corridor"));
expectParity(f);
EXPECT_EQ(f.nodes.size(), 2u);
EXPECT_EQ(f.edges.size(), 1u);
}
TEST(ReferenceGraphParity, TJunction)
{
const Fixture f = loadFixture(packagedFixture("t_junction"));
expectParity(f);
EXPECT_EQ(f.nodes.size(), 4u);
EXPECT_EQ(f.edges.size(), 3u);
}
TEST(ReferenceGraphParity, ClosedRingHasNoNode)
{
const Fixture f = loadFixture(packagedFixture("ring"));
expectParity(f);
EXPECT_FALSE(f.skeleton.empty());
EXPECT_TRUE(f.nodes.empty());
}
TEST(ReferenceGraphParity, UnknownCellsSplitTheRidge)
{
const Fixture f = loadFixture(packagedFixture("unknown_block"));
expectParity(f);
EXPECT_EQ(f.edges.size(), 2u);
for (int64_t cell : f.skeleton)
EXPECT_NE(f.cost[static_cast<std::size_t>(cell)], NO_INFORMATION);
}
TEST(ReferenceGraphParity, OneZoneSplitsEdgesAndAddsLanes)
{
const Fixture f = loadFixture(packagedFixture("one_zone"));
expectParity(f);
EXPECT_EQ(f.edges.size(), 5u);
ASSERT_EQ(f.lanes.size(), 3u);
for (const ReferenceLane& lane : f.lanes)
{
EXPECT_EQ(lane.direction_index, 0);
EXPECT_GT(lane.exit.x, lane.entry.x);
}
}
TEST(ReferenceGraphParity, TwoZones)
{
expectParity(loadFixture(packagedFixture("two_zones")));
}
TEST(ReferenceGraphParity, ZoneCutByObstacle)
{
expectParity(loadFixture(packagedFixture("cut_zone")));
}
TEST(ReferenceGraphParity, MapCropWithZones)
{
expectParity(loadFixture(packagedFixture("crop")));
}
// The whole-map fixtures stay outside the repo. Point REFERENCE_GRAPH_FIXTURE_DIR at them to run these.
TEST(ReferenceGraphParity, WholeMaps)
{
const char* directory = std::getenv("REFERENCE_GRAPH_FIXTURE_DIR");
if (directory == nullptr)
{
// The installed gtest predates GTEST_SKIP.
std::cout << "[ SKIPPED ] REFERENCE_GRAPH_FIXTURE_DIR is not set" << std::endl;
return;
}
for (const char* name : { "kit", "sehc" })
{
SCOPED_TRACE(name);
const std::pair<double, double> times = expectParity(loadFixture(std::string(directory) + "/" + name + ".fixture"));
std::cout << "[ TIMING ] " << name << " step 1 " << times.first << " ms, step 2 " << times.second << " ms" << std::endl;
#ifdef NDEBUG
// Pass condition of the plan on the dev machine. The workspace builds Debug by default, where it means nothing.
EXPECT_LT(times.first + times.second, 5000.0);
#endif
}
}
TEST(ReferenceGraphGrid, EmptyGridHasNoSkeleton)
{
const DirectionGeometry geometry = geometryOf(40, 30);
const std::vector<uint8_t> cost(1200, FREE_SPACE);
ReferenceGridView view;
view.cost = cost.data();
view.geometry = geometry;
const ReferenceGraphParams params;
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, params);
ASSERT_EQ(skeleton.mask.size(), 1200u);
EXPECT_EQ(std::count(skeleton.mask.begin(), skeleton.mask.end(), 1), 0);
EXPECT_EQ(skeleton.clearance_mm, std::vector<uint16_t>(1200, 65535));
const ReferenceGraph graph = buildReferenceGraph(view, skeleton, {}, params);
EXPECT_TRUE(graph.nodes.empty());
EXPECT_TRUE(graph.edges.empty());
EXPECT_TRUE(graph.lanes.empty());
}
TEST(ReferenceGraphGrid, AllLethalHasNoSkeleton)
{
const std::vector<uint8_t> cost(1200, LETHAL_OBSTACLE);
ReferenceGridView view;
view.cost = cost.data();
view.geometry = geometryOf(40, 30);
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, ReferenceGraphParams());
EXPECT_EQ(std::count(skeleton.mask.begin(), skeleton.mask.end(), 1), 0);
EXPECT_EQ(skeleton.clearance_mm, std::vector<uint16_t>(1200, 0));
}
TEST(ReferenceGraphGrid, ClearanceIsInMillimetres)
{
const std::vector<uint8_t> cost = corridorCost(100, 60);
ReferenceGridView view;
view.cost = cost.data();
view.geometry = geometryOf(100, 60);
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, ReferenceGraphParams());
// Row 10 touches the wall at row 9, one cell away. Row 29 is 20 cells from the wall at row 9.
EXPECT_EQ(skeleton.clearance_mm[10 * 100 + 50], 50);
EXPECT_EQ(skeleton.clearance_mm[29 * 100 + 50], 1000);
EXPECT_EQ(skeleton.clearance_mm[5 * 100 + 50], 0);
}
TEST(ReferenceGraphGrid, InflatedCellsNeverJoinTheSkeleton)
{
std::vector<uint8_t> cost = corridorCost(100, 60);
// A band of inflation across the middle of the corridor, as a master costmap crop would hold.
for (uint32_t y = 25; y < 35; ++y)
std::fill_n(cost.begin() + y * 100, 100, INSCRIBED_INFLATED_OBSTACLE);
ReferenceGridView view;
view.cost = cost.data();
view.geometry = geometryOf(100, 60);
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, ReferenceGraphParams());
for (std::size_t i = 0; i < cost.size(); ++i)
if (skeleton.mask[i] != 0)
EXPECT_LT(cost[i], INSCRIBED_INFLATED_OBSTACLE) << "cell " << i;
}
TEST(ReferenceGraphGrid, SuppliedClearanceReplacesTheComputedOne)
{
const std::vector<uint8_t> cost = corridorCost(100, 60);
ReferenceGridView view;
view.cost = cost.data();
view.geometry = geometryOf(100, 60);
// An obstacle just outside a window crop: clearance from the full map rules out every cell.
const std::vector<uint16_t> tight(6000, 100);
view.clearance_mm = tight.data();
VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, ReferenceGraphParams());
EXPECT_EQ(std::count(skeleton.mask.begin(), skeleton.mask.end(), 1), 0);
EXPECT_EQ(skeleton.clearance_mm, tight);
const std::vector<uint16_t> wide(6000, 2000);
view.clearance_mm = wide.data();
skeleton = computeVoronoiSkeleton(view, ReferenceGraphParams());
EXPECT_GT(std::count(skeleton.mask.begin(), skeleton.mask.end(), 1), 0);
EXPECT_EQ(skeleton.clearance_mm, wide);
}
TEST(ReferenceGraphGrid, RepeatedRunsGiveTheSameGraph)
{
const Fixture f = loadFixture(packagedFixture("two_zones"));
const ReferenceGridView view = viewOf(f);
const VoronoiSkeleton a = computeVoronoiSkeleton(view, f.params);
const VoronoiSkeleton b = computeVoronoiSkeleton(view, f.params);
EXPECT_EQ(a.mask, b.mask);
const ReferenceGraph first = buildReferenceGraph(view, a, rectangle(f.half_x, f.half_y), f.params);
const ReferenceGraph second = buildReferenceGraph(view, b, rectangle(f.half_x, f.half_y), f.params);
ASSERT_EQ(first.nodes.size(), second.nodes.size());
ASSERT_EQ(first.edges.size(), second.edges.size());
ASSERT_EQ(first.lanes.size(), second.lanes.size());
for (std::size_t i = 0; i < first.edges.size(); ++i)
EXPECT_EQ(first.edges[i].points.size(), second.edges[i].points.size());
}
TEST(ReferenceGraphGrid, BiggerFootprintDropsLanes)
{
const Fixture f = loadFixture(packagedFixture("one_zone"));
const ReferenceGridView view = viewOf(f);
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, f.params);
const ReferenceGraph fits = buildReferenceGraph(view, skeleton, rectangle(f.half_x, f.half_y), f.params);
// Half width 1.1 m does not fit the 2 m corridor.
const ReferenceGraph blocked = buildReferenceGraph(view, skeleton, rectangle(f.half_x, 1.1), f.params);
EXPECT_FALSE(fits.lanes.empty());
EXPECT_TRUE(blocked.lanes.empty());
EXPECT_EQ(fits.edges.size(), blocked.edges.size());
}
TEST(ReferenceGraphGrid, RegionCentreLineMatchesPython)
{
const DirectionGeometry geometry = geometryOf(100, 40);
std::vector<uint8_t> mask(4000, 0);
for (uint32_t y = 16; y < 24; ++y)
std::fill_n(mask.begin() + y * 100 + 10, 80, 1);
const std::vector<uint8_t> line = computeRegionCentreLine(mask, geometry, ReferenceGraphParams());
EXPECT_EQ(std::count(line.begin(), line.end(), 1), 81);
uint32_t x_min = 100, x_max = 0;
for (uint32_t y = 0; y < 40; ++y)
{
for (uint32_t x = 0; x < 100; ++x)
{
if (line[y * 100 + x] == 0)
continue;
EXPECT_NE(mask[y * 100 + x], 0) << "centre line left the region";
x_min = std::min(x_min, x);
x_max = std::max(x_max, x);
}
}
EXPECT_EQ(x_min, 10u);
EXPECT_EQ(x_max, 89u);
const std::vector<uint8_t> nothing(4000, 0);
const std::vector<uint8_t> empty_line = computeRegionCentreLine(nothing, geometry, ReferenceGraphParams());
EXPECT_EQ(std::count(empty_line.begin(), empty_line.end(), 1), 0);
}
TEST(ReferenceGraphInput, BadParametersAreRefused)
{
const std::vector<uint8_t> cost(100, FREE_SPACE);
ReferenceGridView view;
view.cost = cost.data();
view.geometry = geometryOf(10, 10);
ReferenceGraphParams params;
params.min_clearance_m = -0.1;
EXPECT_FALSE(validateReferenceGraphParams(params).empty());
EXPECT_THROW(computeVoronoiSkeleton(view, params), std::invalid_argument);
params = ReferenceGraphParams();
params.ridge_min_separation_m = 0.0;
EXPECT_THROW(computeVoronoiSkeleton(view, params), std::invalid_argument);
params = ReferenceGraphParams();
params.spur_min_length_m = std::numeric_limits<double>::quiet_NaN();
EXPECT_THROW(computeVoronoiSkeleton(view, params), std::invalid_argument);
params = ReferenceGraphParams();
params.lane_offset_step_m = -1.0;
EXPECT_THROW(computeVoronoiSkeleton(view, params), std::invalid_argument);
EXPECT_TRUE(validateReferenceGraphParams(ReferenceGraphParams()).empty());
}
TEST(ReferenceGraphInput, BadGridsAreRefused)
{
const std::vector<uint8_t> cost(100, FREE_SPACE);
ReferenceGridView view;
view.geometry = geometryOf(10, 10);
EXPECT_THROW(computeVoronoiSkeleton(view, ReferenceGraphParams()), std::invalid_argument);
view.cost = cost.data();
view.geometry.resolution = 0.0;
EXPECT_THROW(computeVoronoiSkeleton(view, ReferenceGraphParams()), std::invalid_argument);
view.geometry = geometryOf(0, 10);
EXPECT_THROW(computeVoronoiSkeleton(view, ReferenceGraphParams()), std::invalid_argument);
view.geometry = geometryOf(10, 10);
VoronoiSkeleton wrong_size;
EXPECT_THROW(buildReferenceGraph(view, wrong_size, {}, ReferenceGraphParams()), std::invalid_argument);
}
TEST(ReferenceGraphInput, ZonesNeedAFootprint)
{
const Fixture f = loadFixture(packagedFixture("one_zone"));
const ReferenceGridView view = viewOf(f);
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, f.params);
EXPECT_THROW(buildReferenceGraph(view, skeleton, {}, f.params), std::invalid_argument);
EXPECT_THROW(buildReferenceGraph(view, skeleton, rectangle(0.0, 0.0), f.params), std::invalid_argument);
}
TEST(ReferenceGraphCancel, CancelGivesAnEmptyResult)
{
const Fixture f = loadFixture(packagedFixture("one_zone"));
const ReferenceGridView view = viewOf(f);
const VoronoiSkeleton skeleton = computeVoronoiSkeleton(view, f.params);
std::atomic<bool> cancel(true);
EXPECT_TRUE(computeVoronoiSkeleton(view, f.params, &cancel).mask.empty());
const ReferenceGraph graph = buildReferenceGraph(view, skeleton, rectangle(f.half_x, f.half_y), f.params, &cancel);
EXPECT_TRUE(graph.static_cost.empty());
EXPECT_TRUE(graph.nodes.empty());
EXPECT_TRUE(graph.edges.empty());
EXPECT_TRUE(graph.lanes.empty());
}

View File

@@ -0,0 +1,670 @@
#include <robot_costmap_2d/reference_graph_worker.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdio>
#include <functional>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#include <thread>
#include <vector>
#include <boost/make_shared.hpp>
#include <boost/thread/locks.hpp>
#include <gtest/gtest.h>
#include <robot_costmap_2d/cost_values.h>
#include <robot_costmap_2d/costmap_layer.h>
#include <robot_costmap_2d/layered_costmap.h>
using namespace robot_costmap_2d;
namespace
{
using Clock = std::chrono::steady_clock;
constexpr double kResolution = 0.05;
bool waitUntil(const std::function<bool()>& condition, double seconds = 60.0)
{
const Clock::time_point end = Clock::now() + std::chrono::duration_cast<Clock::duration>(std::chrono::duration<double>(seconds));
while (Clock::now() < end)
{
if (condition())
return true;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
return condition();
}
DirectionGeometry geometryOf(uint32_t width, uint32_t height) { return { width, height, kResolution, 0.0, 0.0, "map" }; }
/** Free map with a lethal border, so its ridge runs along the middle. */
std::vector<uint8_t> corridor(uint32_t width, uint32_t height)
{
std::vector<uint8_t> cost(static_cast<std::size_t>(width) * height, FREE_SPACE);
for (uint32_t x = 0; x < width; ++x)
cost[x] = cost[static_cast<std::size_t>(height - 1) * width + x] = LETHAL_OBSTACLE;
for (uint32_t y = 0; y < height; ++y)
cost[static_cast<std::size_t>(y) * width] = cost[static_cast<std::size_t>(y) * width + width - 1] = LETHAL_OBSTACLE;
return cost;
}
std::vector<robot_geometry_msgs::Point> rectangle(double half_x, double half_y)
{
std::vector<robot_geometry_msgs::Point> footprint(4);
const double xs[4] = { half_x, -half_x, -half_x, half_x };
const double ys[4] = { half_y, half_y, -half_y, -half_y };
for (int i = 0; i < 4; ++i)
{
footprint[static_cast<std::size_t>(i)].x = xs[i];
footprint[static_cast<std::size_t>(i)].y = ys[i];
}
return footprint;
}
/** Zone with direction +X over a band in the middle of the map. */
DirectionFieldSnapshot bandZone(uint32_t width, uint32_t height, uint64_t generation)
{
robot_nav_msgs::OccupancyGrid grid;
grid.header.frame_id = "map";
grid.info.width = width;
grid.info.height = height;
grid.info.resolution = kResolution;
grid.data.assign(static_cast<std::size_t>(width) * height, 0);
for (uint32_t y = height / 3; y < 2 * height / 3; ++y)
for (uint32_t x = width / 4; x < 3 * width / 4; ++x)
grid.data[static_cast<std::size_t>(y) * width + x] = 1;
DirectionFieldSnapshot snapshot;
snapshot.state = DirectionFieldState::kReady;
snapshot.field = DirectionField::create(grid, geometryOf(width, height), 1.5);
snapshot.generation = generation;
return snapshot;
}
ReferenceGraphParams quietParams()
{
ReferenceGraphParams params;
params.debug_log = false;
return params;
}
/** A costmap stand-in the test controls from its own thread. */
class FakeCostmap
{
public:
FakeCostmap(uint32_t width, uint32_t height) : geometry_(geometryOf(width, height)), grid_(corridor(width, height)), footprint_(rectangle(0.3, 0.3)) {}
ReferenceGraphWorker::Source source()
{
ReferenceGraphWorker::Source out;
out.capture = [this](bool with_grid, ReferenceGraphInput* input) {
++captures;
if (on_capture)
on_capture();
std::lock_guard<std::mutex> lock(mutex_);
if (status_ != ReferenceCaptureStatus::kReady)
return status_;
input->geometry = geometry_;
input->footprint = footprint_;
if (with_grid)
{
++grid_captures;
input->static_layers.push_back(grid_);
}
return ReferenceCaptureStatus::kReady;
};
out.direction = [this]() {
std::lock_guard<std::mutex> lock(mutex_);
return direction_;
};
return out;
}
void setGrid(std::vector<uint8_t> grid)
{
std::lock_guard<std::mutex> lock(mutex_);
grid_ = std::move(grid);
}
void setStatus(ReferenceCaptureStatus status)
{
std::lock_guard<std::mutex> lock(mutex_);
status_ = status;
}
void setDirection(DirectionFieldSnapshot direction)
{
std::lock_guard<std::mutex> lock(mutex_);
direction_ = std::move(direction);
}
std::atomic<int> captures{ 0 };
std::atomic<int> grid_captures{ 0 };
std::function<void()> on_capture; ///< Set before the worker starts.
private:
std::mutex mutex_;
DirectionGeometry geometry_;
std::vector<uint8_t> grid_;
std::vector<robot_geometry_msgs::Point> footprint_;
ReferenceCaptureStatus status_ = ReferenceCaptureStatus::kReady;
DirectionFieldSnapshot direction_;
};
bool ready(const ReferenceGraphWorker& worker) { return worker.snapshot().state == ReferenceGraphState::kReady; }
/** Holds the worker inside a capture until the test lets it go. */
class Gate
{
public:
void hold()
{
std::unique_lock<std::mutex> lock(mutex_);
reached_ = true;
changed_.notify_all();
changed_.wait(lock, [this]() { return open_; });
}
void waitReached()
{
std::unique_lock<std::mutex> lock(mutex_);
changed_.wait(lock, [this]() { return reached_; });
}
void open()
{
std::lock_guard<std::mutex> lock(mutex_);
open_ = true;
changed_.notify_all();
}
private:
std::mutex mutex_;
std::condition_variable changed_;
bool reached_ = false;
bool open_ = false;
};
class FakeLayer : public CostmapLayer
{
public:
explicit FakeLayer(LayerType type, bool current = true) : type_(type)
{
enabled_ = true;
current_ = current;
}
LayerType getType() const override { return type_; }
void setCurrent(bool current) { current_ = current; }
void write(const std::vector<uint8_t>& cost) { std::copy(cost.begin(), cost.end(), getCharMap()); }
protected:
void handleImpl(const void*, const std::type_info&, const std::string&) override {}
private:
LayerType type_;
};
class FakeDirectionLayer : public Layer, public DirectionFieldProvider
{
public:
FakeDirectionLayer() { enabled_ = true; current_ = true; }
LayerType getType() const override { return LayerType::DIRECTIONAL_LAYER; }
DirectionFieldSnapshot directionSnapshot() const override
{
std::lock_guard<std::mutex> lock(mutex_);
return snapshot_;
}
void publish(DirectionFieldSnapshot snapshot)
{
std::lock_guard<std::mutex> lock(mutex_);
snapshot_ = std::move(snapshot);
layered_costmap_->notifyDirectionFieldChanged();
}
protected:
void handleImpl(const void*, const std::type_info&, const std::string&) override {}
private:
mutable std::mutex mutex_;
DirectionFieldSnapshot snapshot_;
};
/** A LayeredCostmap with one static layer that already holds a map. */
struct Costmap
{
Costmap(uint32_t width, uint32_t height) : layered("map", false, true), layer(boost::make_shared<FakeLayer>(LayerType::STATIC_LAYER))
{
layer->initialize(&layered, "static", nullptr);
layered.addPlugin(layer);
layered.resizeMap(width, height, kResolution, 0.0, 0.0);
layer->write(corridor(width, height));
layered.setFootprint(rectangle(0.3, 0.3));
}
bool ready() const { return layered.getReferenceGraphSnapshot().state == ReferenceGraphState::kReady; }
LayeredCostmap layered;
boost::shared_ptr<FakeLayer> layer;
};
} // namespace
TEST(MergeStaticLayers, LethalWinsAndUnknownNeedsEveryLayer)
{
const std::vector<uint8_t> merged = mergeStaticLayers({ { FREE_SPACE, LETHAL_OBSTACLE, NO_INFORMATION, NO_INFORMATION, INSCRIBED_INFLATED_OBSTACLE },
{ LETHAL_OBSTACLE, FREE_SPACE, FREE_SPACE, NO_INFORMATION, NO_INFORMATION } });
EXPECT_EQ(merged, (std::vector<uint8_t>{ LETHAL_OBSTACLE, LETHAL_OBSTACLE, FREE_SPACE, NO_INFORMATION, FREE_SPACE }));
EXPECT_TRUE(mergeStaticLayers({}).empty());
EXPECT_THROW(mergeStaticLayers({ { FREE_SPACE }, { FREE_SPACE, FREE_SPACE } }), std::invalid_argument);
}
TEST(ReferenceGraphWorker, BadInputIsRefused)
{
FakeCostmap costmap(40, 40);
ReferenceGraphParams bad = quietParams();
bad.spur_min_length_m = -1.0;
EXPECT_THROW(ReferenceGraphWorker(costmap.source(), bad), std::invalid_argument);
EXPECT_THROW(ReferenceGraphWorker(ReferenceGraphWorker::Source{}, quietParams()), std::invalid_argument);
}
TEST(ReferenceGraphWorker, GoesFromPendingToReadyAndSleeps)
{
FakeCostmap costmap(120, 60);
Gate gate;
costmap.on_capture = [&gate]() { gate.hold(); };
ReferenceGraphWorker worker(costmap.source(), quietParams());
gate.waitReached();
const ReferenceGraphSnapshot pending = worker.snapshot();
EXPECT_EQ(pending.state, ReferenceGraphState::kPending);
EXPECT_EQ(pending.graph, nullptr);
gate.open();
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
const ReferenceGraphSnapshot done = worker.snapshot();
ASSERT_NE(done.graph, nullptr);
EXPECT_NE(done.generation, pending.generation);
EXPECT_FALSE(done.graph->edges.empty());
EXPECT_EQ(done.graph->static_cost, mergeStaticLayers({ corridor(120, 60) }));
EXPECT_EQ(done.graph->clearance_mm.size(), 120U * 60U);
EXPECT_EQ(done.graph->geometry.frame_id, "map");
// A ready worker does no work at all, so it costs nothing on a 2-core robot.
const int captures = costmap.captures;
std::this_thread::sleep_for(std::chrono::milliseconds(450));
EXPECT_EQ(costmap.captures, captures);
EXPECT_EQ(worker.snapshot().generation, done.generation);
}
TEST(ReferenceGraphWorker, RetainedSnapshotSurvivesTheNextChange)
{
FakeCostmap costmap(120, 60);
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
const ReferenceGraphSnapshot held = worker.snapshot();
costmap.setGrid(corridor(120, 60));
worker.notifyStaticChanged();
EXPECT_EQ(worker.snapshot().graph == held.graph, false);
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
EXPECT_FALSE(held.graph->static_cost.empty());
}
TEST(ReferenceGraphWorker, WaitsForTheStaticLayer)
{
FakeCostmap costmap(120, 60);
costmap.setStatus(ReferenceCaptureStatus::kWait);
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
EXPECT_EQ(worker.stats().step1_runs, 0U);
costmap.setStatus(ReferenceCaptureStatus::kReady);
EXPECT_TRUE(waitUntil([&]() { return ready(worker); }));
}
TEST(ReferenceGraphWorker, NoStaticLayerStaysPending)
{
FakeCostmap costmap(120, 60);
costmap.setStatus(ReferenceCaptureStatus::kNoStaticLayer);
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
}
TEST(ReferenceGraphWorker, WaitsForTheDirectionGridThenRunsOnlyStep2)
{
FakeCostmap costmap(200, 120);
DirectionFieldSnapshot pending;
pending.state = DirectionFieldState::kPending;
costmap.setDirection(pending);
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return worker.stats().step1_runs == 1; }));
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
EXPECT_EQ(worker.stats().step2_runs, 0U);
costmap.setDirection(bandZone(200, 120, 7));
worker.notifyDirectionChanged();
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
const ReferenceGraphSnapshot done = worker.snapshot();
EXPECT_EQ(done.graph->direction_generation, 7U);
EXPECT_FALSE(done.graph->lanes.empty());
EXPECT_EQ(worker.stats().step1_runs, 1U);
EXPECT_EQ(costmap.grid_captures, 1);
}
TEST(ReferenceGraphWorker, GridOfTheOldMapIsNotUsed)
{
FakeCostmap costmap(200, 120);
costmap.setDirection(bandZone(100, 60, 3));
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return costmap.captures >= 2; }));
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
costmap.setDirection(bandZone(200, 120, 4));
worker.notifyDirectionChanged();
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
EXPECT_EQ(worker.snapshot().graph->direction_generation, 4U);
}
TEST(ReferenceGraphWorker, DirectionAndFootprintChangesRunOnlyStep2)
{
FakeCostmap costmap(120, 60);
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
ASSERT_EQ(worker.stats().step1_runs, 1U);
worker.notifyDirectionChanged();
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
EXPECT_EQ(worker.snapshot().graph, nullptr);
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
worker.notifyFootprintChanged();
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
const ReferenceGraphWorkerStats stats = worker.stats();
EXPECT_EQ(stats.step1_runs, 1U);
EXPECT_EQ(stats.step2_runs, 3U);
EXPECT_EQ(stats.published, 3U);
EXPECT_EQ(costmap.grid_captures, 1);
}
TEST(ReferenceGraphWorker, StaticChangeRunsBothSteps)
{
FakeCostmap costmap(120, 60);
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
worker.notifyStaticChanged();
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
EXPECT_EQ(worker.stats().step1_runs, 2U);
EXPECT_EQ(worker.stats().step2_runs, 2U);
}
TEST(ReferenceGraphWorker, ThreeMapsInARowPublishOnlyTheLast)
{
FakeCostmap costmap(120, 60);
Gate gate;
costmap.on_capture = [&gate]() { gate.hold(); };
ReferenceGraphWorker worker(costmap.source(), quietParams());
gate.waitReached();
std::vector<uint8_t> last = corridor(120, 60);
for (int i = 0; i < 3; ++i)
{
last[static_cast<std::size_t>(30 * 120 + 10 + i)] = LETHAL_OBSTACLE;
costmap.setGrid(last);
worker.notifyStaticChanged();
}
gate.open();
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
EXPECT_EQ(worker.snapshot().graph->static_cost, mergeStaticLayers({ last }));
EXPECT_EQ(worker.stats().published, 1U);
}
TEST(ReferenceGraphWorker, MapDuringAComputationRestartsIt)
{
const uint32_t size = 1500;
FakeCostmap costmap(size, size);
std::atomic<bool> first{ true };
ReferenceGraphWorker* worker_ptr = nullptr;
std::vector<uint8_t> changed = corridor(size, size);
changed[static_cast<std::size_t>(size / 2) * size + size / 2] = LETHAL_OBSTACLE;
std::mutex started_mutex;
std::condition_variable started;
bool started_flag = false;
costmap.on_capture = [&]() {
if (first.exchange(false))
{
std::lock_guard<std::mutex> lock(started_mutex);
started_flag = true;
started.notify_all();
}
};
ReferenceGraphWorker worker(costmap.source(), quietParams());
worker_ptr = &worker;
{
std::unique_lock<std::mutex> lock(started_mutex);
started.wait(lock, [&]() { return started_flag; });
}
costmap.setGrid(changed);
worker_ptr->notifyStaticChanged();
ASSERT_TRUE(waitUntil([&]() { return ready(worker); }));
EXPECT_EQ(worker.snapshot().graph->static_cost, mergeStaticLayers({ changed }));
EXPECT_EQ(worker.stats().published, 1U);
}
TEST(ReferenceGraphWorker, ExceptionStaysPendingUntilTheNextChange)
{
FakeCostmap costmap(120, 60);
std::atomic<bool> fail{ true };
costmap.on_capture = [&fail]() {
if (fail)
throw std::runtime_error("injected");
};
ReferenceGraphWorker worker(costmap.source(), quietParams());
ASSERT_TRUE(waitUntil([&]() { return worker.stats().errors == 1; }));
EXPECT_EQ(worker.snapshot().state, ReferenceGraphState::kPending);
const int captures = costmap.captures;
std::this_thread::sleep_for(std::chrono::milliseconds(450));
EXPECT_EQ(costmap.captures, captures);
fail = false;
worker.notifyStaticChanged();
EXPECT_TRUE(waitUntil([&]() { return ready(worker); }));
}
TEST(ReferenceGraphWorker, StopsQuicklyDuringAComputation)
{
const uint32_t size = 3000;
FakeCostmap costmap(size, size);
std::unique_ptr<ReferenceGraphWorker> worker(new ReferenceGraphWorker(costmap.source(), quietParams()));
ASSERT_TRUE(waitUntil([&]() { return costmap.grid_captures >= 1; }));
// Let it get well into step 1, where the map is largest.
std::this_thread::sleep_for(std::chrono::milliseconds(100));
const Clock::time_point began = Clock::now();
worker.reset();
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - began).count();
std::printf("stopped in %.1f ms\n", ms);
EXPECT_LT(ms, 100.0);
}
TEST(LayeredCostmapReferenceGraph, DisabledHasNoWork)
{
Costmap costmap(120, 60);
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kDisabled);
costmap.layered.notifyStaticMapChanged(LayerType::STATIC_LAYER);
costmap.layered.notifyDirectionFieldChanged();
costmap.layered.setFootprint(rectangle(0.4, 0.4));
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kDisabled);
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::STATIC_LAYER), 1U);
}
TEST(LayeredCostmapReferenceGraph, RollingCostmapIsRefused)
{
LayeredCostmap rolling("map", true, true);
EXPECT_FALSE(rolling.enableReferenceGraph(quietParams()));
EXPECT_EQ(rolling.getReferenceGraphSnapshot().state, ReferenceGraphState::kDisabled);
}
TEST(LayeredCostmapReferenceGraph, BadParametersAndASecondCallAreRefused)
{
Costmap costmap(120, 60);
ReferenceGraphParams bad = quietParams();
bad.min_clearance_m = -1.0;
EXPECT_FALSE(costmap.layered.enableReferenceGraph(bad));
EXPECT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
EXPECT_FALSE(costmap.layered.enableReferenceGraph(quietParams()));
}
TEST(LayeredCostmapReferenceGraph, MapThatArrivedBeforeEnablingCounts)
{
Costmap costmap(120, 60);
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
const auto graph = costmap.layered.getReferenceGraphSnapshot().graph;
EXPECT_EQ(graph->geometry.width, 120U);
EXPECT_EQ(graph->direction_generation, 0U);
EXPECT_TRUE(graph->lanes.empty());
}
TEST(LayeredCostmapReferenceGraph, WaitsUntilTheLayerWroteItsMap)
{
Costmap costmap(120, 60);
costmap.layer->setCurrent(false);
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
{
boost::unique_lock<Costmap2D::mutex_t> lock(*costmap.layered.getCostmap()->getMutex());
costmap.layer->setCurrent(true);
}
EXPECT_TRUE(waitUntil([&]() { return costmap.ready(); }));
}
TEST(LayeredCostmapReferenceGraph, NoStaticLayerStaysPending)
{
LayeredCostmap layered("map", false, true);
layered.resizeMap(120, 60, kResolution, 0.0, 0.0);
ASSERT_TRUE(layered.enableReferenceGraph(quietParams()));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
EXPECT_EQ(layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
}
TEST(LayeredCostmapReferenceGraph, OtherLayerTypesOnlyCountARevision)
{
Costmap costmap(120, 60);
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
const ReferenceGraphSnapshot before = costmap.layered.getReferenceGraphSnapshot();
costmap.layered.notifyStaticMapChanged(LayerType::PREFERRED_LAYER);
costmap.layered.notifyStaticMapChanged(LayerType::UNPREFERRED_LAYER);
costmap.layered.notifyStaticMapChanged(LayerType::CRITICAL_LAYER);
const ReferenceGraphSnapshot after = costmap.layered.getReferenceGraphSnapshot();
EXPECT_EQ(after.state, ReferenceGraphState::kReady);
EXPECT_EQ(after.generation, before.generation);
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::PREFERRED_LAYER), 1U);
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::STATIC_LAYER), 0U);
costmap.layered.notifyStaticMapChanged(LayerType::STATIC_LAYER);
EXPECT_EQ(costmap.layered.getStaticMapRevision(LayerType::STATIC_LAYER), 1U);
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
EXPECT_TRUE(waitUntil([&]() { return costmap.ready(); }));
}
TEST(LayeredCostmapReferenceGraph, MergesStaticLayersAndIgnoresSoftOnes)
{
Costmap costmap(120, 60);
auto walls = boost::make_shared<FakeLayer>(LayerType::STATIC_LAYER);
auto preferred = boost::make_shared<FakeLayer>(LayerType::PREFERRED_LAYER);
for (const auto& layer : { walls, preferred })
{
layer->initialize(&costmap.layered, "extra", nullptr);
costmap.layered.addPlugin(layer);
layer->matchSize();
}
std::vector<uint8_t> wall_cost(120 * 60, NO_INFORMATION);
wall_cost[30 * 120 + 60] = LETHAL_OBSTACLE;
walls->write(wall_cost);
std::vector<uint8_t> soft_cost(120 * 60, NO_INFORMATION);
soft_cost[20 * 120 + 40] = LETHAL_OBSTACLE;
preferred->write(soft_cost);
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
const auto graph = costmap.layered.getReferenceGraphSnapshot().graph;
EXPECT_EQ(graph->static_cost[30 * 120 + 60], LETHAL_OBSTACLE);
EXPECT_EQ(graph->static_cost[20 * 120 + 40], FREE_SPACE);
EXPECT_EQ(graph->static_cost[0], LETHAL_OBSTACLE);
}
TEST(LayeredCostmapReferenceGraph, OnlyARealFootprintChangeRecomputes)
{
Costmap costmap(120, 60);
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
const uint64_t generation = costmap.layered.getReferenceGraphSnapshot().generation;
costmap.layered.setFootprint(rectangle(0.3, 0.3));
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().generation, generation);
costmap.layered.setFootprint(rectangle(0.35, 0.3));
EXPECT_NE(costmap.layered.getReferenceGraphSnapshot().generation, generation);
EXPECT_TRUE(waitUntil([&]() { return costmap.ready(); }));
}
TEST(LayeredCostmapReferenceGraph, ReadsTheDirectionProviderAndFollowsIt)
{
Costmap costmap(200, 120);
auto direction = boost::make_shared<FakeDirectionLayer>();
direction->initialize(&costmap.layered, "direction", nullptr);
costmap.layered.addPlugin(direction);
DirectionFieldSnapshot pending;
pending.state = DirectionFieldState::kPending;
direction->publish(pending);
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
std::this_thread::sleep_for(std::chrono::milliseconds(300));
EXPECT_EQ(costmap.layered.getReferenceGraphSnapshot().state, ReferenceGraphState::kPending);
direction->publish(bandZone(200, 120, 11));
ASSERT_TRUE(waitUntil([&]() { return costmap.ready(); }));
const auto graph = costmap.layered.getReferenceGraphSnapshot().graph;
EXPECT_EQ(graph->direction_generation, 11U);
EXPECT_FALSE(graph->lanes.empty());
direction->publish(bandZone(200, 120, 12));
ASSERT_TRUE(waitUntil([&]() {
const auto now = costmap.layered.getReferenceGraphSnapshot();
return now.state == ReferenceGraphState::kReady && now.graph->direction_generation == 12U;
}));
}
TEST(LayeredCostmapReferenceGraph, MasterMutexStaysFreeWhileComputing)
{
// SEHC-sized map. The costmap update must not feel the graph, so the master mutex may be held only for the copy.
const uint32_t size = 3060;
Costmap costmap(size, size);
ASSERT_TRUE(costmap.layered.enableReferenceGraph(quietParams()));
double worst_ms = 0.0;
const Clock::time_point end = Clock::now() + std::chrono::seconds(120);
while (!costmap.ready() && Clock::now() < end)
{
const Clock::time_point began = Clock::now();
{
boost::unique_lock<Costmap2D::mutex_t> lock(*costmap.layered.getCostmap()->getMutex());
}
worst_ms = std::max(worst_ms, std::chrono::duration<double, std::milli>(Clock::now() - began).count());
std::this_thread::sleep_for(std::chrono::microseconds(200));
}
ASSERT_TRUE(costmap.ready());
std::printf("longest wait for the master mutex %.2f ms\n", worst_ms);
EXPECT_LT(worst_ms, 10.0);
}
TEST(LayeredCostmapReferenceGraph, DestroyedDuringAComputation)
{
const uint32_t size = 2000;
std::unique_ptr<Costmap> costmap(new Costmap(size, size));
ASSERT_TRUE(costmap->layered.enableReferenceGraph(quietParams()));
std::this_thread::sleep_for(std::chrono::milliseconds(50));
const Clock::time_point began = Clock::now();
costmap.reset();
const double ms = std::chrono::duration<double, std::milli>(Clock::now() - began).count();
EXPECT_LT(ms, 100.0);
}