Skip to content

Commit fd1c114

Browse files
committed
RJD-1509 Add using only part of spline for getCollisionPointIn2D
1 parent beb51db commit fd1c114

File tree

6 files changed

+41
-28
lines changed

6 files changed

+41
-28
lines changed

common/math/geometry/include/geometry/spline/catmull_rom_spline.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,15 @@ class CatmullRomSpline : public CatmullRomSplineInterface
5050
auto getSquaredDistanceVector(const geometry_msgs::msg::Point & point, const double s) const
5151
-> geometry_msgs::msg::Vector3;
5252
auto getCollisionPointsIn2D(
53-
const std::vector<geometry_msgs::msg::Point> & polygon,
54-
const bool search_backward = false) const -> std::set<double>;
53+
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward = false,
54+
std::optional<std::pair<double, double>> s_part = std::nullopt) const -> std::set<double>;
5555
auto getCollisionPointIn2D(
5656
const geometry_msgs::msg::Point & point0, const geometry_msgs::msg::Point & point1,
5757
const bool search_backward = false) const -> std::optional<double>;
5858
auto getCollisionPointIn2D(
59-
const std::vector<geometry_msgs::msg::Point> & polygon,
60-
const bool search_backward = false) const -> std::optional<double> override;
59+
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward = false,
60+
std::optional<std::pair<double, double>> s_part = std::nullopt) const
61+
-> std::optional<double> override;
6162
auto getPolygon(const double width, const size_t num_points = 30, const double z_offset = 0)
6263
-> std::vector<geometry_msgs::msg::Point>;
6364
const std::vector<geometry_msgs::msg::Point> control_points;

common/math/geometry/include/geometry/spline/catmull_rom_spline_interface.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class CatmullRomSplineInterface
3232
virtual ~CatmullRomSplineInterface() = default;
3333
virtual double getLength() const = 0;
3434
virtual std::optional<double> getCollisionPointIn2D(
35-
const std::vector<geometry_msgs::msg::Point> & polygon,
36-
const bool search_backward = false) const = 0;
35+
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward = false,
36+
std::optional<std::pair<double, double>> s_part = std::nullopt) const = 0;
3737
virtual double getSquaredDistanceIn2D(
3838
const geometry_msgs::msg::Point & point, const double s) const = 0;
3939
};

common/math/geometry/include/geometry/spline/catmull_rom_subspline.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class CatmullRomSubspline : public CatmullRomSplineInterface
4343
double getLength() const override;
4444

4545
std::optional<double> getCollisionPointIn2D(
46-
const std::vector<geometry_msgs::msg::Point> & polygon,
47-
const bool search_backward = false) const override;
46+
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward = false,
47+
std::optional<std::pair<double, double>> s_part = std::nullopt) const override;
4848

4949
auto getSquaredDistanceIn2D(const geometry_msgs::msg::Point & point, const double s) const
5050
-> double override;

common/math/geometry/src/spline/catmull_rom_spline.cpp

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ auto CatmullRomSpline::getSInSplineCurve(const size_t curve_index, const double
279279
}
280280

281281
auto CatmullRomSpline::getCollisionPointsIn2D(
282-
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward) const
283-
-> std::set<double>
282+
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward,
283+
std::optional<std::pair<double, double>> s_part) const -> std::set<double>
284284
{
285285
if (polygon.size() <= 1) {
286286
THROW_SIMULATION_ERROR(
@@ -291,15 +291,19 @@ auto CatmullRomSpline::getCollisionPointsIn2D(
291291
}
292292
/// @note If the spline has three or more control points.
293293
const auto get_collision_point_2d_with_curve =
294-
[this](const auto & local_polygon, const auto local_search_backward) {
294+
[this, s_part](const auto & local_polygon, const auto local_search_backward) {
295295
std::set<double> s_value_candidates;
296+
double s_all = 0;
296297
for (size_t i = 0; i < curves_.size(); ++i) {
297-
/// @note The local_polygon is assumed to be closed
298-
const auto s =
299-
curves_[i].getCollisionPointsIn2D(local_polygon, local_search_backward, true, true);
300-
std::for_each(s.begin(), s.end(), [&s_value_candidates, i, this](const auto & s) {
301-
s_value_candidates.insert(getSInSplineCurve(i, s));
302-
});
298+
if (s_part == std::nullopt || (s_all >= s_part->first && s_all <= s_part->second)) {
299+
/// @note The local_polygon is assumed to be closed
300+
const auto s =
301+
curves_[i].getCollisionPointsIn2D(local_polygon, local_search_backward, true, true);
302+
std::for_each(s.begin(), s.end(), [&s_value_candidates, i, this](const auto & s) {
303+
s_value_candidates.insert(getSInSplineCurve(i, s));
304+
});
305+
}
306+
s_all += curves_[i].getLength();
303307
}
304308
return s_value_candidates;
305309
};
@@ -358,10 +362,10 @@ auto CatmullRomSpline::getCollisionPointsIn2D(
358362
* @return std::optional<double> Denormalized s values along the frenet coordinate of the spline curve.
359363
*/
360364
auto CatmullRomSpline::getCollisionPointIn2D(
361-
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward) const
362-
-> std::optional<double>
365+
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward,
366+
std::optional<std::pair<double, double>> s_part) const -> std::optional<double>
363367
{
364-
std::set<double> s_value_candidates = getCollisionPointsIn2D(polygon, search_backward);
368+
std::set<double> s_value_candidates = getCollisionPointsIn2D(polygon, search_backward, s_part);
365369
if (s_value_candidates.empty()) {
366370
return std::nullopt;
367371
}

common/math/geometry/src/spline/catmull_rom_subspline.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ namespace geometry
2424
double CatmullRomSubspline::getLength() const { return end_s_ - start_s_; }
2525

2626
std::optional<double> CatmullRomSubspline::getCollisionPointIn2D(
27-
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward) const
27+
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward,
28+
std::optional<std::pair<double, double>> s_part) const
2829
{
2930
/// @note Make sure end is greater than start, otherwise the spline is invalid
3031
if (end_s_ < start_s_) {
@@ -37,7 +38,7 @@ std::optional<double> CatmullRomSubspline::getCollisionPointIn2D(
3738
"contact the developer of traffic_simulator.");
3839
}
3940

40-
std::set<double> s_value_candidates = spline_->getCollisionPointsIn2D(polygon);
41+
std::set<double> s_value_candidates = spline_->getCollisionPointsIn2D(polygon, false, s_part);
4142

4243
if (s_value_candidates.empty()) {
4344
return std::nullopt;

simulation/behavior_tree_plugin/src/action_node.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,14 @@ auto ActionNode::getDistanceToTargetEntity(
331331
target_lanelet_pose) {
332332
const auto & from_lanelet_pose = canonicalized_entity_status->getCanonicalizedLaneletPose();
333333
const auto & from_bounding_box = canonicalized_entity_status->getBoundingBox();
334-
if (const auto bounding_box_distances =
335-
traffic_simulator::distance::laneLongitudinalDistances(
336-
*from_lanelet_pose, from_bounding_box, *target_lanelet_pose, target_bounding_box,
337-
include_adjacent_lanelet, include_opposite_direction, routing_configuration,
338-
hdmap_utils);
334+
const auto bounding_box_map_points = math::geometry::transformPoints(
335+
static_cast<geometry_msgs::msg::Pose>(*target_lanelet_pose),
336+
math::geometry::getPointsFromBbox(target_bounding_box));
337+
const auto bounding_box_diagonal_length =
338+
math::geometry::getDistance(bounding_box_map_points[0], bounding_box_map_points[2]);
339+
if (const auto bounding_box_distances = traffic_simulator::distance::laneLongitudinalDistances(
340+
*from_lanelet_pose, from_bounding_box, *target_lanelet_pose, target_bounding_box,
341+
include_adjacent_lanelet, include_opposite_direction, routing_configuration, hdmap_utils);
339342
!bounding_box_distances || bounding_box_distances.value().first < 0.0) {
340343
return std::nullopt;
341344
} else {
@@ -352,7 +355,11 @@ auto ActionNode::getDistanceToTargetEntity(
352355
/// @note if the distance of the target entity to the spline cannot be calculated because a collision occurs
353356
else if (const auto target_polygon = math::geometry::transformPoints(
354357
status.getMapPose(), math::geometry::getPointsFromBbox(target_bounding_box));
355-
spline.getCollisionPointIn2D(target_polygon, search_backward)) {
358+
spline.getCollisionPointIn2D(
359+
target_polygon, search_backward,
360+
std::make_pair(
361+
bounding_box_distances.value().first,
362+
bounding_box_distances.value().first + bounding_box_diagonal_length))) {
356363
return target_bounding_box_distance;
357364
}
358365
}

0 commit comments

Comments
 (0)