Skip to content

Commit df70002

Browse files
committed
Review changes
1 parent fd1c114 commit df70002

File tree

8 files changed

+53
-51
lines changed

8 files changed

+53
-51
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ class CatmullRomSpline : public CatmullRomSplineInterface
5151
-> geometry_msgs::msg::Vector3;
5252
auto getCollisionPointsIn2D(
5353
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>;
54+
const std::optional<std::pair<double, double>> & s_range = std::nullopt) const
55+
-> std::set<double>;
5556
auto getCollisionPointIn2D(
5657
const geometry_msgs::msg::Point & point0, const geometry_msgs::msg::Point & point1,
5758
const bool search_backward = false) const -> std::optional<double>;
5859
auto getCollisionPointIn2D(
5960
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+
const std::optional<std::pair<double, double>> & s_range = std::nullopt) const
6162
-> std::optional<double> override;
6263
auto getPolygon(const double width, const size_t num_points = 30, const double z_offset = 0)
6364
-> std::vector<geometry_msgs::msg::Point>;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class CatmullRomSplineInterface
3333
virtual double getLength() const = 0;
3434
virtual std::optional<double> getCollisionPointIn2D(
3535
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;
36+
const std::optional<std::pair<double, double>> & s_range = 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class CatmullRomSubspline : public CatmullRomSplineInterface
4444

4545
std::optional<double> getCollisionPointIn2D(
4646
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;
47+
const std::optional<std::pair<double, double>> & s_range = 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: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ auto CatmullRomSpline::getSInSplineCurve(const size_t curve_index, const double
280280

281281
auto CatmullRomSpline::getCollisionPointsIn2D(
282282
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>
283+
const std::optional<std::pair<double, double>> & s_range) const -> std::set<double>
284284
{
285285
if (polygon.size() <= 1) {
286286
THROW_SIMULATION_ERROR(
@@ -290,23 +290,27 @@ auto CatmullRomSpline::getCollisionPointsIn2D(
290290
"developer of traffic_simulator.");
291291
}
292292
/// @note If the spline has three or more control points.
293-
const auto get_collision_point_2d_with_curve =
294-
[this, s_part](const auto & local_polygon, const auto local_search_backward) {
295-
std::set<double> s_value_candidates;
296-
double s_all = 0;
297-
for (size_t i = 0; i < curves_.size(); ++i) {
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));
293+
const auto get_collision_point_2d_with_curve = [this, &s_range](
294+
const auto & local_polygon,
295+
const auto local_search_backward) {
296+
std::set<double> s_value_candidates;
297+
auto current_curve_start_s = 0.0;
298+
for (size_t i = 0; i < curves_.size(); ++i) {
299+
if (
300+
s_range == std::nullopt ||
301+
(current_curve_start_s >= s_range->first && current_curve_start_s <= s_range->second)) {
302+
/// @note The local_polygon is assumed to be closed
303+
const auto s =
304+
curves_[i].getCollisionPointsIn2D(local_polygon, local_search_backward, true, true);
305+
std::for_each(
306+
s.begin(), s.end(), [&s_value_candidates, &current_curve_start_s, this](const auto & s) {
307+
s_value_candidates.insert(current_curve_start_s + s);
304308
});
305-
}
306-
s_all += curves_[i].getLength();
307309
}
308-
return s_value_candidates;
309-
};
310+
current_curve_start_s += curves_[i].getLength();
311+
}
312+
return s_value_candidates;
313+
};
310314
/// @note If the spline has two control points. (Same as single line segment.)
311315
const auto get_collision_point_2d_with_line = [this](const auto & local_polygon) {
312316
std::set<double> s_value_candidates;
@@ -363,9 +367,9 @@ auto CatmullRomSpline::getCollisionPointsIn2D(
363367
*/
364368
auto CatmullRomSpline::getCollisionPointIn2D(
365369
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>
370+
const std::optional<std::pair<double, double>> & s_range) const -> std::optional<double>
367371
{
368-
std::set<double> s_value_candidates = getCollisionPointsIn2D(polygon, search_backward, s_part);
372+
std::set<double> s_value_candidates = getCollisionPointsIn2D(polygon, search_backward, s_range);
369373
if (s_value_candidates.empty()) {
370374
return std::nullopt;
371375
}

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

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

2626
std::optional<double> CatmullRomSubspline::getCollisionPointIn2D(
2727
const std::vector<geometry_msgs::msg::Point> & polygon, const bool search_backward,
28-
std::optional<std::pair<double, double>> s_part) const
28+
const std::optional<std::pair<double, double>> & s_range) const
2929
{
3030
/// @note Make sure end is greater than start, otherwise the spline is invalid
3131
if (end_s_ < start_s_) {
@@ -38,7 +38,8 @@ std::optional<double> CatmullRomSubspline::getCollisionPointIn2D(
3838
"contact the developer of traffic_simulator.");
3939
}
4040

41-
std::set<double> s_value_candidates = spline_->getCollisionPointsIn2D(polygon, false, s_part);
41+
std::set<double> s_value_candidates =
42+
spline_->getCollisionPointsIn2D(polygon, search_backward, s_range);
4243

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

simulation/behavior_tree_plugin/src/action_node.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,19 +336,26 @@ auto ActionNode::getDistanceToTargetEntity(
336336
math::geometry::getPointsFromBbox(target_bounding_box));
337337
const auto bounding_box_diagonal_length =
338338
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);
342-
!bounding_box_distances || bounding_box_distances.value().first < 0.0) {
339+
if (const auto position_distance = traffic_simulator::distance::longitudinalDistance(
340+
*from_lanelet_pose, *target_lanelet_pose, include_adjacent_lanelet,
341+
include_opposite_direction, routing_configuration, hdmap_utils);
342+
!position_distance) {
343+
return std::nullopt;
344+
} else if (const auto bounding_box_distance =
345+
traffic_simulator::distance::boundingBoxLaneLongitudinalDistance(
346+
position_distance, from_bounding_box, target_bounding_box);
347+
!bounding_box_distance || bounding_box_distance.value() < 0.0) {
343348
return std::nullopt;
344349
} else {
350+
// TODO rotation of NPC is not taken into account, same as in boundingBoxLaneLongitudinalDistance
351+
// this should be considered to be changed in separate task in the future
345352
const auto target_bounding_box_distance =
346-
bounding_box_distances.value().first + from_bounding_box.dimensions.x / 2.0;
353+
bounding_box_distance.value() + from_bounding_box.dimensions.x / 2.0;
347354

348355
/// @note if the distance of the target entity to the spline is smaller than the width of the reference entity
349356
if (const auto target_to_spline_distance = traffic_simulator::distance::distanceToSpline(
350357
static_cast<geometry_msgs::msg::Pose>(*target_lanelet_pose), target_bounding_box,
351-
spline, bounding_box_distances.value().second);
358+
spline, position_distance.value());
352359
target_to_spline_distance <= from_bounding_box.dimensions.y / 2.0) {
353360
return target_bounding_box_distance;
354361
}
@@ -358,8 +365,8 @@ auto ActionNode::getDistanceToTargetEntity(
358365
spline.getCollisionPointIn2D(
359366
target_polygon, search_backward,
360367
std::make_pair(
361-
bounding_box_distances.value().first,
362-
bounding_box_distances.value().first + bounding_box_diagonal_length))) {
368+
bounding_box_distance.value(),
369+
bounding_box_distance.value() + bounding_box_diagonal_length))) {
363370
return target_bounding_box_distance;
364371
}
365372
}

simulation/traffic_simulator/include/traffic_simulator/utils/distance.hpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,10 @@ auto boundingBoxLaneLongitudinalDistance(
7171
const traffic_simulator::RoutingConfiguration & routing_configuration,
7272
const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils_ptr) -> std::optional<double>;
7373

74-
auto laneLongitudinalDistances(
75-
const CanonicalizedLaneletPose & from,
74+
auto boundingBoxLaneLongitudinalDistance(
75+
const std::optional<double> & longitudinal_distance,
7676
const traffic_simulator_msgs::msg::BoundingBox & from_bounding_box,
77-
const CanonicalizedLaneletPose & to,
78-
const traffic_simulator_msgs::msg::BoundingBox & to_bounding_box, bool include_adjacent_lanelet,
79-
bool include_opposite_direction,
80-
const traffic_simulator::RoutingConfiguration & routing_configuration,
81-
const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils_ptr) -> std::optional<std::pair<double,double>>;
77+
const traffic_simulator_msgs::msg::BoundingBox & to_bounding_box) -> std::optional<double>;
8278

8379
// Bounds
8480
auto distanceToLaneBound(

simulation/traffic_simulator/src/utils/distance.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,12 @@ auto boundingBoxLaneLongitudinalDistance(
197197
return std::nullopt;
198198
}
199199

200-
auto laneLongitudinalDistances(
201-
const CanonicalizedLaneletPose & from,
200+
auto boundingBoxLaneLongitudinalDistance(
201+
const std::optional<double> & longitudinal_distance,
202202
const traffic_simulator_msgs::msg::BoundingBox & from_bounding_box,
203-
const CanonicalizedLaneletPose & to,
204-
const traffic_simulator_msgs::msg::BoundingBox & to_bounding_box, bool include_adjacent_lanelet,
205-
bool include_opposite_direction,
206-
const traffic_simulator::RoutingConfiguration & routing_configuration,
207-
const std::shared_ptr<hdmap_utils::HdMapUtils> & hdmap_utils_ptr) -> std::optional<std::pair<double,double>>
203+
const traffic_simulator_msgs::msg::BoundingBox & to_bounding_box) -> std::optional<double>
208204
{
209-
if (const auto longitudinal_distance = longitudinalDistance(
210-
from, to, include_adjacent_lanelet, include_opposite_direction, routing_configuration,
211-
hdmap_utils_ptr);
212-
longitudinal_distance) {
205+
if (longitudinal_distance) {
213206
const auto from_bounding_box_distances =
214207
math::geometry::getDistancesFromCenterToEdge(from_bounding_box);
215208
const auto to_bounding_box_distances =
@@ -222,7 +215,7 @@ auto laneLongitudinalDistances(
222215
bounding_box_distance =
223216
+std::abs(from_bounding_box_distances.rear) + std::abs(to_bounding_box_distances.front);
224217
}
225-
return std::make_pair(longitudinal_distance.value() + bounding_box_distance, longitudinal_distance.value());
218+
return longitudinal_distance.value() + bounding_box_distance;
226219
}
227220
return std::nullopt;
228221
}

0 commit comments

Comments
 (0)