Skip to content

Commit

Permalink
costmap_2d: resolve clang issue with std::pair non-const copy
Browse files Browse the repository at this point in the history
Signed-off-by: Rhys Mainwaring <rhys.mainwaring@me.com>
  • Loading branch information
srmainwaring committed Sep 22, 2023
1 parent 53e8e23 commit 2ed8fd9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,9 @@ class GroupsRemover
const IsBg & is_background) const
{
// Creates an image labels in which each obstacles group is labeled with a unique code
auto components = connectedComponents<connectivity>(image, buffer, label_trees, is_background);
const Label groups_count = components.second;
const Image<Label> & labels = components.first;
Label groups_count;
auto labels = connectedComponents<connectivity>(image, buffer, label_trees,
is_background, groups_count);

// Calculates the size of each group.
// Group size is equal to the number of pixels with the same label
Expand Down Expand Up @@ -1029,24 +1029,27 @@ class GroupsRemover
} // namespace imgproc_impl

template<ConnectivityType connectivity, class Label, class IsBg>
std::pair<Image<Label>, Label> connectedComponents(
Image<Label> connectedComponents(
const Image<uint8_t> & image, MemoryBuffer & buffer,
imgproc_impl::EquivalenceLabelTrees<Label> & label_trees, const IsBg & is_background)
imgproc_impl::EquivalenceLabelTrees<Label> & label_trees,
const IsBg & is_background,
Label & total_labels)
{
using namespace imgproc_impl;
const size_t pixels = image.rows() * image.columns();

if (pixels == 0) {
return {Image<Label>{}, 0};
total_labels = 0;
return Image<Label>{};
}

Label * image_buffer = buffer.get<Label>(pixels);
Image<Label> labels(image.rows(), image.columns(), image_buffer, image.columns());
label_trees.reset(image.rows(), image.columns(), connectivity);
const Label total_labels = connectedComponentsImpl<connectivity>(
total_labels = connectedComponentsImpl<connectivity>(
image, labels, label_trees,
is_background);
return std::make_pair(labels, total_labels);
return labels;
}

} // namespace nav2_costmap_2d
Expand Down
75 changes: 44 additions & 31 deletions nav2_costmap_2d/test/unit/image_processing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,71 +161,77 @@ Image<uint8_t> ConnectedComponentsTester::makeChessboardLikeImage(

TEST_F(ConnectedComponentsTester, way4EmptyTest) {
Image<uint8_t> empty;
const auto result = connectedComponents<ConnectivityType::Way4>(
uint8_t total_labels;
connectedComponents<ConnectivityType::Way4>(
empty, buffer_, label_trees_,
isBackground);
ASSERT_EQ(result.second, uint8_t(0));
isBackground, total_labels);
ASSERT_EQ(total_labels, uint8_t(0));
}

TEST_F(ConnectedComponentsTester, way4SinglePixelTest) {
Image<uint8_t> input = makeImage(1, 1, image_buffer_bytes_);
uint8_t total_labels;
{
input.row(0)[0] = BACKGROUND_CODE;

const auto result = connectedComponents<ConnectivityType::Way4>(
input, buffer_, label_trees_,
isBackground);
isBackground, total_labels);

ASSERT_EQ(result.first.row(0)[0], 0);
ASSERT_EQ(result.second, 1);
ASSERT_EQ(result.row(0)[0], 0);
ASSERT_EQ(total_labels, 1);
}
{
input.row(0)[0] = FOREGROUND_CODE;

const auto result = connectedComponents<ConnectivityType::Way4>(
input, buffer_, label_trees_,
isBackground);
isBackground, total_labels);

ASSERT_EQ(result.first.row(0)[0], 1);
ASSERT_EQ(result.second, 2);
ASSERT_EQ(result.row(0)[0], 1);
ASSERT_EQ(total_labels, 2);
}
}

TEST_F(ConnectedComponentsTester, way4ImageSmallTest) {
{
Image<uint8_t> input = makeImage(1, 2, image_buffer_bytes_);
uint8_t total_labels;
input.row(0)[0] = BACKGROUND_CODE;
input.row(0)[1] = FOREGROUND_CODE;

const auto result = connectedComponents<ConnectivityType::Way4>(
input, buffer_, label_trees_,
isBackground);
isBackground, total_labels);

ASSERT_EQ(result.second, uint8_t(2));
ASSERT_EQ(result.first.row(0)[0], 0);
ASSERT_EQ(result.first.row(0)[1], 1);
ASSERT_EQ(total_labels, uint8_t(2));
ASSERT_EQ(result.row(0)[0], 0);
ASSERT_EQ(result.row(0)[1], 1);
}
{
Image<uint8_t> input = makeImage(2, 1, image_buffer_bytes_);
uint8_t total_labels;
input.row(0)[0] = BACKGROUND_CODE;
input.row(1)[0] = FOREGROUND_CODE;

const auto result = connectedComponents<ConnectivityType::Way4>(
input, buffer_, label_trees_,
isBackground);
isBackground, total_labels);

ASSERT_EQ(result.second, uint8_t(2));
ASSERT_EQ(result.first.row(0)[0], 0);
ASSERT_EQ(result.first.row(1)[0], 1);
ASSERT_EQ(total_labels, uint8_t(2));
ASSERT_EQ(result.row(0)[0], 0);
ASSERT_EQ(result.row(1)[0], 1);
}
}

TEST_F(ConnectedComponentsTester, way4LabelsOverflowTest) {
// big chessboard image
Image<uint8_t> input = makeChessboardLikeImage(32, 17, image_buffer_bytes_);
uint8_t total_labels;

ASSERT_THROW(
(connectedComponents<ConnectivityType::Way4>(input, buffer_, label_trees_, isBackground)),
(connectedComponents<ConnectivityType::Way4>(input, buffer_, label_trees_,
isBackground, total_labels)),
LabelOverflow);
}

Expand Down Expand Up @@ -273,12 +279,13 @@ TEST_F(ConnectedComponentsTester, way4ImageStepsTest) {
".xx."
"xx.."
"....", image_buffer_bytes2_);
uint8_t total_labels;
const auto result = connectedComponents<ConnectivityType::Way4>(
input, buffer_, label_trees_,
isBackground);
isBackground, total_labels);

ASSERT_EQ(result.second, uint8_t(2));
ASSERT_TRUE(isEqualLabels(result.first, expected_labels));
ASSERT_EQ(total_labels, uint8_t(2));
ASSERT_TRUE(isEqualLabels(result, expected_labels));
}

/// @brief create mapping '.'->0, 'a'->1, 'b'->2, ... max_symbol->n
Expand Down Expand Up @@ -307,13 +314,14 @@ TEST_F(ConnectedComponentsTester, way8ImageStepsTest) {
"...bb."
".....b"
"....b.", image_buffer_bytes2_, makeLabelsMap('b'));
uint8_t total_labels;

const auto result = connectedComponents<ConnectivityType::Way8>(
input, buffer_, label_trees_,
isBackground);
isBackground, total_labels);

ASSERT_EQ(result.second, uint8_t(3));
ASSERT_TRUE(isEqualLabels(result.first, expected_labels));
ASSERT_EQ(total_labels, uint8_t(3));
ASSERT_TRUE(isEqualLabels(result, expected_labels));
}

TEST_F(ConnectedComponentsTester, way4ImageSieveTest) {
Expand All @@ -329,13 +337,14 @@ TEST_F(ConnectedComponentsTester, way4ImageSieveTest) {
"f.g.h"
".i.j."
"k.l.m", image_buffer_bytes2_, makeLabelsMap('m'));
uint8_t total_labels;

const auto result = connectedComponents<ConnectivityType::Way4>(
input, buffer_, label_trees_,
isBackground);
isBackground, total_labels);

ASSERT_EQ(result.second, uint8_t(14));
ASSERT_TRUE(isEqualLabels(result.first, expected_labels));
ASSERT_EQ(total_labels, uint8_t(14));
ASSERT_TRUE(isEqualLabels(result, expected_labels));
}

template<ConnectivityType connectivity>
Expand All @@ -353,10 +362,12 @@ bool ConnectedComponentsTester::fingerTest()
"..b.c"
"a.b.c"
"a.b.c", image_buffer_bytes2_, makeLabelsMap('c'));
uint8_t total_labels;

const auto result = connectedComponents<connectivity>(input, buffer_, label_trees_, isBackground);
const auto result = connectedComponents<connectivity>(input, buffer_,
label_trees_, isBackground, total_labels);

return result.second == 4 && isEqualLabels(result.first, expected_labels);
return total_labels == 4 && isEqualLabels(result, expected_labels);
}

TEST_F(ConnectedComponentsTester, way4ImageFingerTest) {
Expand Down Expand Up @@ -386,9 +397,11 @@ bool ConnectedComponentsTester::spiralTest()
".x.xx.x"
".x....x"
".xxxxxx", image_buffer_bytes2_);
uint8_t total_labels;

const auto result = connectedComponents<connectivity>(input, buffer_, label_trees_, isBackground);
return result.second == 2 && isEqualLabels(result.first, expected_labels);
const auto result = connectedComponents<connectivity>(input, buffer_,
label_trees_, isBackground, total_labels);
return total_labels == 2 && isEqualLabels(result, expected_labels);
}

TEST_F(ConnectedComponentsTester, way4ImageSpiralTest) {
Expand Down

0 comments on commit 2ed8fd9

Please sign in to comment.