Skip to content

Commit 5b1cb23

Browse files
feat(lanelet2_extension): deprecate lanelet2_extension utilities functions (final) (#103)
Signed-off-by: Sarun Mukdapitak <sarun.mukda@gmail.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 68219c4 commit 5b1cb23

5 files changed

Lines changed: 261 additions & 24 deletions

File tree

autoware_lanelet2_extension/include/autoware_lanelet2_extension/utility/utilities.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ namespace lanelet::utils
3737
[[deprecated("please use autoware::lanelet2_utils::combine_lanelets_shape instead")]]
3838
lanelet::ConstLanelet combineLaneletsShape(const lanelet::ConstLanelets & lanelets);
3939

40+
[[deprecated("please use autoware::lanelet2_utils::get_fine_centerline instead")]]
4041
lanelet::LineString3d generateFineCenterline(
4142
const lanelet::ConstLanelet & lanelet_obj, const double resolution = 5.0);
4243
[[deprecated("please use autoware::lanelet2_utils::get_centerline_with_offset instead")]]
@@ -109,6 +110,7 @@ lanelet::ArcCoordinates getArcCoordinates(
109110
* - when the `use_waypoints` in the autoware_map_loader is false,
110111
* - the centerline in the lanelet2::LaneletMapPtr is used.
111112
*/
113+
[[deprecated("please use autoware::lanelet2_utils::get_arc_coordinates_on_ego_centerline instead")]]
112114
lanelet::ArcCoordinates getArcCoordinatesOnEgoCenterline(
113115
const lanelet::ConstLanelets & lanelet_sequence, const geometry_msgs::msg::Pose & pose,
114116
const lanelet::LaneletMapConstPtr & lanelet_map_ptr);
@@ -118,11 +120,13 @@ lanelet::ArcCoordinates getArcCoordinatesOnEgoCenterline(
118120
getClosestSegment(
119121
const lanelet::BasicPoint2d & search_pt, const lanelet::ConstLineString3d & linestring);
120122

123+
[[deprecated("please use autoware::lanelet2_utils::get_polygon_from_arc_length instead")]]
121124
lanelet::CompoundPolygon3d getPolygonFromArcLength(
122125
const lanelet::ConstLanelets & lanelets, const double s1, const double s2);
123126
[[deprecated("please use autoware::lanelet2_utils::get_lanelet_angle instead")]] double
124127
getLaneletAngle(
125128
const lanelet::ConstLanelet & lanelet, const geometry_msgs::msg::Point & search_point);
129+
[[deprecated("please use autoware::lanelet2_utils::is_in_lanelet instead")]]
126130
bool isInLanelet(
127131
const geometry_msgs::msg::Pose & current_pose, const lanelet::ConstLanelet & lanelet,
128132
const double radius = 0.0);

autoware_lanelet2_extension/lib/deprecated.cpp

Lines changed: 121 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,17 @@
2020

2121
#include "deprecated.hpp"
2222

23-
// TODO(sarun-hub): lanelet2_extension will be removed when toLaneletPoint is deprecated.
24-
#include <autoware_lanelet2_extension/utility/message_conversion.hpp> // For toLaneletPoint
2523
#include <tf2/utils.hpp>
2624

2725
#include <tf2_geometry_msgs/tf2_geometry_msgs.hpp>
2826

2927
#include <lanelet2_core/geometry/Lanelet.h>
3028

29+
#include <algorithm>
3130
#include <iostream>
3231
#include <limits>
32+
#include <utility>
33+
#include <vector>
3334

3435
using lanelet::utils::to2D;
3536

@@ -63,6 +64,96 @@ void copyZ(const T1 & from, T2 & to)
6364
to[i_to].z() = from[i_from - 1].z() + ratio * (from[i_from].z() - from[i_from - 1].z());
6465
}
6566
}
67+
68+
std::vector<double> calculateSegmentDistances(const lanelet::ConstLineString3d & line_string)
69+
{
70+
std::vector<double> segment_distances;
71+
segment_distances.reserve(line_string.size() - 1);
72+
73+
for (size_t i = 1; i < line_string.size(); ++i) {
74+
const auto distance = lanelet::geometry::distance(line_string[i], line_string[i - 1]);
75+
segment_distances.push_back(distance);
76+
}
77+
78+
return segment_distances;
79+
}
80+
81+
std::vector<double> calculateAccumulatedLengths(const lanelet::ConstLineString3d & line_string)
82+
{
83+
const auto segment_distances = calculateSegmentDistances(line_string);
84+
85+
std::vector<double> accumulated_lengths{0};
86+
accumulated_lengths.reserve(segment_distances.size() + 1);
87+
std::partial_sum(
88+
std::begin(segment_distances), std::end(segment_distances),
89+
std::back_inserter(accumulated_lengths));
90+
91+
return accumulated_lengths;
92+
}
93+
94+
std::pair<size_t, size_t> findNearestIndexPair(
95+
const std::vector<double> & accumulated_lengths, const double target_length)
96+
{
97+
// List size
98+
const auto N = accumulated_lengths.size();
99+
100+
// Front
101+
if (target_length < accumulated_lengths.at(1)) {
102+
return std::make_pair(0, 1);
103+
}
104+
105+
// Back
106+
if (target_length > accumulated_lengths.at(N - 2)) {
107+
return std::make_pair(N - 2, N - 1);
108+
}
109+
110+
// Middle
111+
for (std::size_t i = 1; i < N; ++i) {
112+
if (
113+
accumulated_lengths.at(i - 1) <= target_length &&
114+
target_length <= accumulated_lengths.at(i)) {
115+
return std::make_pair(i - 1, i);
116+
}
117+
}
118+
119+
// Throw an exception because this never happens
120+
throw std::runtime_error("No nearest point found.");
121+
}
122+
123+
std::vector<lanelet::BasicPoint3d> resamplePoints(
124+
const lanelet::ConstLineString3d & line_string, const int num_segments)
125+
{
126+
// Calculate length
127+
const auto line_length = static_cast<double>(lanelet::geometry::length(line_string));
128+
129+
// Calculate accumulated lengths
130+
const auto accumulated_lengths = calculateAccumulatedLengths(line_string);
131+
if (accumulated_lengths.size() < 2) return {};
132+
133+
// Create each segment
134+
std::vector<lanelet::BasicPoint3d> resampled_points;
135+
for (auto i = 0; i <= num_segments; ++i) {
136+
// Find two nearest points
137+
const auto target_length = (static_cast<double>(i) / num_segments) * line_length;
138+
const auto index_pair = findNearestIndexPair(accumulated_lengths, target_length);
139+
140+
// Apply linear interpolation
141+
const lanelet::BasicPoint3d back_point = line_string[index_pair.first];
142+
const lanelet::BasicPoint3d front_point = line_string[index_pair.second];
143+
const auto direction_vector = (front_point - back_point);
144+
145+
const auto back_length = accumulated_lengths.at(index_pair.first);
146+
const auto front_length = accumulated_lengths.at(index_pair.second);
147+
const auto segment_length = front_length - back_length;
148+
const auto target_point =
149+
back_point + (direction_vector * (target_length - back_length) / segment_length);
150+
151+
// Add to list
152+
resampled_points.emplace_back(target_point);
153+
}
154+
155+
return resampled_points;
156+
}
66157
} // namespace detail
67158

68159
lanelet::ConstLineString3d getClosestSegment(
@@ -240,15 +331,6 @@ bool getClosestLanelet(
240331
return found;
241332
}
242333

243-
bool isInLanelet(
244-
const geometry_msgs::msg::Pose & current_pose, const lanelet::ConstLanelet & lanelet,
245-
const double radius)
246-
{
247-
constexpr double eps = 1.0e-9;
248-
const lanelet::BasicPoint2d p(current_pose.position.x, current_pose.position.y);
249-
return boost::geometry::distance(p, lanelet.polygon2d().basicPolygon()) < radius + eps;
250-
}
251-
252334
double getLateralDistanceToCenterline(
253335
const lanelet::ConstLanelet & lanelet, const geometry_msgs::msg::Pose & pose)
254336
{
@@ -432,4 +514,32 @@ lanelet::ConstPoint3d toLaneletPoint(const geometry_msgs::msg::Point & src)
432514
toLaneletPoint(src, &dst);
433515
return dst;
434516
}
517+
518+
lanelet::LineString3d generateFineCenterline(
519+
const lanelet::ConstLanelet & lanelet_obj, const double resolution)
520+
{
521+
// Get length of longer border
522+
const double left_length =
523+
static_cast<double>(lanelet::geometry::length(lanelet_obj.leftBound()));
524+
const double right_length =
525+
static_cast<double>(lanelet::geometry::length(lanelet_obj.rightBound()));
526+
const double longer_distance = (left_length > right_length) ? left_length : right_length;
527+
const int num_segments = std::max(static_cast<int>(ceil(longer_distance / resolution)), 1);
528+
529+
// Resample points
530+
const auto left_points = detail::resamplePoints(lanelet_obj.leftBound(), num_segments);
531+
const auto right_points = detail::resamplePoints(lanelet_obj.rightBound(), num_segments);
532+
533+
// Create centerline
534+
lanelet::LineString3d centerline(lanelet::utils::getId());
535+
for (int i = 0; i < num_segments + 1; i++) {
536+
// Add ID for the average point of left and right
537+
const auto center_basic_point = (right_points.at(i) + left_points.at(i)) / 2;
538+
const lanelet::Point3d center_point(
539+
lanelet::utils::getId(), center_basic_point.x(), center_basic_point.y(),
540+
center_basic_point.z());
541+
centerline.push_back(center_point);
542+
}
543+
return centerline;
544+
}
435545
} // namespace deprecated

autoware_lanelet2_extension/lib/deprecated.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,6 @@ bool getClosestLanelet(
7373
const lanelet::ConstLanelets & lanelets, const geometry_msgs::msg::Pose & search_pose,
7474
lanelet::ConstLanelet * closest_lanelet_ptr);
7575

76-
bool isInLanelet(
77-
const geometry_msgs::msg::Pose & current_pose, const lanelet::ConstLanelet & lanelet,
78-
const double radius);
79-
8076
double getLateralDistanceToCenterline(
8177
const lanelet::ConstLanelet & lanelet, const geometry_msgs::msg::Pose & pose);
8278

@@ -98,6 +94,9 @@ void toGeomMsgPt32(const Eigen::Vector3d & src, geometry_msgs::msg::Point32 * ds
9894
void toLaneletPoint(const geometry_msgs::msg::Point & src, lanelet::ConstPoint3d * dst);
9995

10096
lanelet::ConstPoint3d toLaneletPoint(const geometry_msgs::msg::Point & src);
97+
98+
lanelet::LineString3d generateFineCenterline(
99+
const lanelet::ConstLanelet & lanelet_obj, const double resolution = 5.0);
101100
} // namespace deprecated
102101
// NOLINTEND(readability-identifier-naming)
103102

autoware_lanelet2_extension/lib/utilities.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ void overwriteLaneletsCenterline(
516516
{
517517
for (auto & lanelet_obj : lanelet_map->laneletLayer) {
518518
if (force_overwrite || !lanelet_obj.hasCustomCenterline()) {
519-
const auto fine_center_line = generateFineCenterline(lanelet_obj, resolution);
519+
const auto fine_center_line = deprecated::generateFineCenterline(lanelet_obj, resolution);
520520
lanelet_obj.setCenterline(fine_center_line);
521521
}
522522
}
@@ -527,15 +527,15 @@ void overwriteLaneletsCenterlineWithWaypoints(
527527
{
528528
for (auto & lanelet_obj : lanelet_map->laneletLayer) {
529529
if (force_overwrite) {
530-
const auto fine_center_line = generateFineCenterline(lanelet_obj, resolution);
530+
const auto fine_center_line = deprecated::generateFineCenterline(lanelet_obj, resolution);
531531
lanelet_obj.setCenterline(fine_center_line);
532532
} else {
533533
if (lanelet_obj.hasCustomCenterline()) {
534534
const auto & centerline = lanelet_obj.centerline();
535535
lanelet_obj.setAttribute("waypoints", centerline.id());
536536
}
537537

538-
const auto fine_center_line = generateFineCenterline(lanelet_obj, resolution);
538+
const auto fine_center_line = deprecated::generateFineCenterline(lanelet_obj, resolution);
539539
lanelet_obj.setCenterline(fine_center_line);
540540
}
541541
}

0 commit comments

Comments
 (0)