Skip to content

Commit ffbb99b

Browse files
committed
Fix merge segments
Signed-off-by: Alberto Tudela <[email protected]>
1 parent 58b0236 commit ffbb99b

File tree

3 files changed

+37
-30
lines changed

3 files changed

+37
-30
lines changed

src/segmentation/jump_distance.cpp

+17-15
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,23 @@ void JumpDistanceSegmentation::perform_segmentation(
7676
segments.push_back(current_segment);
7777

7878
// Check if last and first segments belongs to the same segment
79-
slg::Segment2D first_segment = segments.front();
80-
slg::Segment2D last_segment = segments.back();
81-
// Check if the point belong to the same segment or we have to discard it.
82-
// Remember that the points are radially sorted.
83-
if (!is_jump_between(last_segment, first_segment)) {
84-
last_segment.merge(first_segment);
85-
// Insert the last segment as the first one
86-
segments[0] = last_segment;
87-
// And remove the last one
88-
segments.pop_back();
89-
} else {
90-
// Fix the prior segment of the first segment
91-
segments.front().set_prior_segment(last_segment.last_point());
92-
// Fix the next segment of the last segment
93-
segments.back().set_next_segment(first_segment.first_point());
79+
if (segments.size() > 1) {
80+
slg::Segment2D first_segment = segments.front();
81+
slg::Segment2D last_segment = segments.back();
82+
// Check if the point belong to the same segment or we have to discard it.
83+
// Remember that the points are radially sorted.
84+
if (!is_jump_between(last_segment, first_segment)) {
85+
last_segment.merge(first_segment);
86+
// Insert the last segment as the first one
87+
segments[0] = last_segment;
88+
// And remove the last one
89+
segments.pop_back();
90+
} else {
91+
// Fix the prior segment of the first segment
92+
segments.front().set_prior_segment(last_segment.last_point());
93+
// Fix the next segment of the last segment
94+
segments.back().set_next_segment(first_segment.first_point());
95+
}
9496
}
9597
}
9698

src/segmentation/jump_distance_merge.cpp

+17-15
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,23 @@ void JumpDistanceSegmentationMerge::perform_segmentation(
105105
}
106106

107107
// Check if last and first segments belongs to the same segment
108-
slg::Segment2D first_segment = segments.front();
109-
slg::Segment2D last_segment = segments.back();
110-
// Check if the point belong to the same segment or we have to discard it.
111-
// Remember that the points are radially sorted.
112-
if (!is_jump_between(last_segment, first_segment)) {
113-
last_segment.merge(first_segment);
114-
// Insert the last segment as the first one
115-
segments[0] = last_segment;
116-
// And remove the last one
117-
segments.pop_back();
118-
} else {
119-
// Fix the prior segment of the first segment
120-
segments.front().set_prior_segment(last_segment.last_point());
121-
// Fix the next segment of the last segment
122-
segments.back().set_next_segment(first_segment.first_point());
108+
if (segments.size() > 1) {
109+
slg::Segment2D first_segment = segments.front();
110+
slg::Segment2D last_segment = segments.back();
111+
// Check if the point belong to the same segment or we have to discard it.
112+
// Remember that the points are radially sorted.
113+
if (!is_jump_between(last_segment, first_segment)) {
114+
last_segment.merge(first_segment);
115+
// Insert the last segment as the first one
116+
segments[0] = last_segment;
117+
// And remove the last one
118+
segments.pop_back();
119+
} else {
120+
// Fix the prior segment of the first segment
121+
segments.front().set_prior_segment(last_segment.last_point());
122+
// Fix the next segment of the last segment
123+
segments.back().set_next_segment(first_segment.first_point());
124+
}
123125
}
124126
}
125127

test/test_integration.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ TEST(LaserSegmentationTest, integration) {
6464
sub_node->configure();
6565
sub_node->activate();
6666
// Create a subscriber for the segments
67+
bool msg_received = false;
6768
auto seg_sub = sub_node->create_subscription<slg_msgs::msg::SegmentArray>(
6869
"segments", 1,
6970
[&](const slg_msgs::msg::SegmentArray msg) {
71+
msg_received = true;
7072
EXPECT_EQ(msg.segments.size(), 1);
7173
RCLCPP_INFO(sub_node->get_logger(), "Segment received: %ld", msg.segments.size());
7274
});
@@ -82,6 +84,7 @@ TEST(LaserSegmentationTest, integration) {
8284
// and the segment should have a publisher
8385
EXPECT_EQ(scan_pub->get_subscription_count(), 1);
8486
EXPECT_EQ(seg_sub->get_publisher_count(), 1);
87+
EXPECT_TRUE(msg_received);
8588

8689
// Deactivate the nodes
8790
seg_node->deactivate();

0 commit comments

Comments
 (0)