Skip to content

Commit aacbaa9

Browse files
authored
Merge pull request tier4#1609 from tier4/feature/delay_curventure_calculation
Feature/delay curventure calculation
2 parents 434b40b + c8084ef commit aacbaa9

File tree

12 files changed

+183
-62
lines changed

12 files changed

+183
-62
lines changed

common/math/geometry/include/geometry/solver/polynomial_solver.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ class PolynomialSolver
101101
*/
102102
constexpr static double tolerance = 1e-7;
103103

104+
/**
105+
* @brief check the value0 and value1 is equal or not with considering tolerance.
106+
* @param value0 the value you want to compare
107+
* @param value1 the value you want to compared
108+
* @return true value0 and value1 are equal
109+
* @return false value0 and value1 are not equal
110+
*/
111+
auto isApproximatelyEqualTo(const double value0, const double value1) const -> bool;
112+
104113
private:
105114
/**
106115
* @brief solve cubic equation x^3 + a*x^2 + b*x + c = 0
@@ -119,14 +128,6 @@ class PolynomialSolver
119128
auto filterByRange(
120129
const std::vector<double> & values, const double min_value, const double max_value) const
121130
-> std::vector<double>;
122-
/**
123-
* @brief check the value0 and value1 is equal or not with considering tolerance.
124-
* @param value0 the value you want to compare
125-
* @param value1 the value you want to compared
126-
* @return true value0 and value1 are equal
127-
* @return false value0 and value1 are not equal
128-
*/
129-
auto isApproximatelyEqualTo(const double value0, const double value1) const -> bool;
130131
};
131132
} // namespace geometry
132133
} // namespace math

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@
1919
#include <geometry/spline/catmull_rom_spline_interface.hpp>
2020
#include <geometry/spline/hermite_curve.hpp>
2121
#include <geometry_msgs/msg/point.hpp>
22+
#include <memory>
2223
#include <optional>
2324
#include <string>
25+
#include <traffic_simulator_msgs/msg/polyline_trajectory.hpp>
2426
#include <utility>
2527
#include <vector>
2628

@@ -33,6 +35,9 @@ class CatmullRomSpline : public CatmullRomSplineInterface
3335
public:
3436
CatmullRomSpline() = default;
3537
explicit CatmullRomSpline(const std::vector<geometry_msgs::msg::Point> & control_points);
38+
explicit CatmullRomSpline(
39+
const geometry_msgs::msg::Point & start_point,
40+
const std::shared_ptr<traffic_simulator_msgs::msg::PolylineTrajectory> & trajectory);
3641
auto getLength() const -> double override { return total_length_; }
3742
auto getMaximum2DCurvature() const -> double;
3843
auto getPoint(const double s) const -> geometry_msgs::msg::Point;
@@ -43,6 +48,8 @@ class CatmullRomSpline : public CatmullRomSplineInterface
4348
auto getTrajectory(
4449
const double start_s, const double end_s, const double resolution,
4550
const double offset = 0.0) const -> std::vector<geometry_msgs::msg::Point>;
51+
auto getTrajectoryPoses(const double start_s, const double end_s, const double resolution) const
52+
-> std::vector<geometry_msgs::msg::Pose>;
4653
auto getSValue(const geometry_msgs::msg::Pose & pose, double threshold_distance = 3.0) const
4754
-> std::optional<double>;
4855
auto getSquaredDistanceIn2D(const geometry_msgs::msg::Point & point, const double s) const
@@ -78,7 +85,8 @@ class CatmullRomSpline : public CatmullRomSplineInterface
7885
std::vector<LineSegment> line_segments_;
7986
std::vector<HermiteCurve> curves_;
8087
std::vector<double> length_list_;
81-
std::vector<double> maximum_2d_curvatures_;
88+
/// @note Since curvature calculation requires a large computational cost, it is inefficient to calculate it in the member initializer list and must be calculated when the getMaximum2DCurvature function is first called.
89+
mutable std::vector<double> maximum_2d_curvatures_;
8290
double total_length_;
8391
};
8492
} // namespace geometry

common/math/geometry/include/geometry/vector3/operator.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <geometry_msgs/msg/point.hpp>
2121
#include <geometry_msgs/msg/quaternion.hpp>
2222
#include <geometry_msgs/msg/vector3.hpp>
23+
#include <limits>
2324

2425
namespace math
2526
{
@@ -131,6 +132,16 @@ auto operator+=(T & a, const U & b) -> decltype(auto)
131132
a.z += b.z;
132133
return a;
133134
}
135+
136+
template <
137+
typename T, typename U,
138+
std::enable_if_t<std::conjunction_v<IsLikeVector3<T>, IsLikeVector3<U>>, std::nullptr_t> =
139+
nullptr>
140+
auto operator==(const T & a, const U & b) -> bool
141+
{
142+
constexpr decltype(a.x) e = std::numeric_limits<decltype(a.x)>::epsilon();
143+
return (std::abs(a.x - b.x) < e) && (std::abs(a.y - b.y) < e) && (std::abs(a.z - b.z) < e);
144+
}
134145
} // namespace geometry
135146
} // namespace math
136147

common/math/geometry/src/polygon/line_segment.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,15 @@ auto getLineSegments(
314314
} else {
315315
std::vector<LineSegment> seg;
316316
for (size_t i = 0; i < points.size() - 1; i++) {
317-
seg.emplace_back(points[i], points[i + 1]);
317+
if (points[i] != points[i + 1]) {
318+
seg.emplace_back(points[i], points[i + 1]);
319+
}
318320
}
319321
/// @note If true, the end point(points[points.size() - 1]) and start point(points[0]) was connected.
320322
if (close_start_end) {
321-
seg.emplace_back(points[points.size() - 1], points[0]);
323+
if (points[points.size() - 1] != points[0]) {
324+
seg.emplace_back(points[points.size() - 1], points[0]);
325+
}
322326
}
323327
return seg;
324328
}

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

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ auto CatmullRomSpline::getTrajectory(
101101
const double start_s, const double end_s, const double resolution, const double offset) const
102102
-> std::vector<geometry_msgs::msg::Point>
103103
{
104+
if (std::fabs(resolution) <= std::numeric_limits<double>::epsilon()) {
105+
THROW_SIMULATION_ERROR(
106+
"Resolution should not be zero.",
107+
"This message is not originally intended to be displayed, if you see it, please contact ",
108+
"the developer of traffic_simulator.");
109+
}
104110
if (start_s > end_s) {
105111
std::vector<geometry_msgs::msg::Point> ret;
106112
double s = start_s;
@@ -122,8 +128,70 @@ auto CatmullRomSpline::getTrajectory(
122128
}
123129
}
124130

125-
CatmullRomSpline::CatmullRomSpline(const std::vector<geometry_msgs::msg::Point> & control_points)
126-
: control_points(control_points), line_segments_(getLineSegments(control_points)), total_length_(0)
131+
auto CatmullRomSpline::getTrajectoryPoses(
132+
const double start_s, const double end_s, const double resolution) const
133+
-> std::vector<geometry_msgs::msg::Pose>
134+
{
135+
if (std::fabs(resolution) <= std::numeric_limits<double>::epsilon()) {
136+
THROW_SIMULATION_ERROR(
137+
"Resolution should not be zero.",
138+
"This message is not originally intended to be displayed, if you see it, please contact ",
139+
"the developer of traffic_simulator.");
140+
}
141+
if (start_s > end_s) {
142+
std::vector<geometry_msgs::msg::Pose> ret;
143+
double s = start_s;
144+
while (s > end_s) {
145+
ret.emplace_back(getPose(s));
146+
s = s - std::fabs(resolution);
147+
}
148+
ret.emplace_back(getPose(end_s));
149+
return ret;
150+
} else {
151+
std::vector<geometry_msgs::msg::Pose> ret;
152+
double s = start_s;
153+
while (s < end_s) {
154+
ret.emplace_back(getPose(s));
155+
s = s + std::fabs(resolution);
156+
}
157+
ret.emplace_back(getPose(end_s));
158+
return ret;
159+
}
160+
}
161+
162+
CatmullRomSpline::CatmullRomSpline(
163+
const geometry_msgs::msg::Point & start_point,
164+
const std::shared_ptr<traffic_simulator_msgs::msg::PolylineTrajectory> & trajectory)
165+
: CatmullRomSpline([start_point, trajectory] {
166+
if (!trajectory) {
167+
THROW_SIMULATION_ERROR(
168+
"Trajectory is empty.",
169+
"This message is not originally intended to be displayed, if you see it, please contact "
170+
"the developer of traffic_simulator.");
171+
}
172+
std::vector<geometry_msgs::msg::Point> control_points;
173+
control_points.reserve(trajectory->shape.vertices.size() + 2UL);
174+
control_points.emplace_back(start_point);
175+
for (const auto & vertex : trajectory->shape.vertices) {
176+
control_points.emplace_back(vertex.position.position);
177+
}
178+
if (trajectory->closed) {
179+
control_points.emplace_back(control_points[0]);
180+
}
181+
return control_points;
182+
}())
183+
{
184+
}
185+
186+
CatmullRomSpline::CatmullRomSpline(
187+
const std::vector<geometry_msgs::msg::Point> & raw_control_points)
188+
: control_points([raw_control_points]() {
189+
std::vector<geometry_msgs::msg::Point> ret = raw_control_points;
190+
ret.erase(std::unique(ret.begin(), ret.end()), ret.end());
191+
return ret;
192+
}()),
193+
line_segments_(getLineSegments(control_points)),
194+
total_length_(0)
127195
{
128196
switch (control_points.size()) {
129197
case 0:
@@ -232,7 +300,6 @@ CatmullRomSpline::CatmullRomSpline(const std::vector<geometry_msgs::msg::Point>
232300
}
233301
for (const auto & curve : curves_) {
234302
length_list_.emplace_back(curve.getLength());
235-
maximum_2d_curvatures_.emplace_back(curve.getMaximum2DCurvature());
236303
}
237304
total_length_ = 0;
238305
for (const auto & length : length_list_) {
@@ -585,8 +652,17 @@ auto CatmullRomSpline::getPoint(const double s, const double offset) const
585652

586653
auto CatmullRomSpline::getMaximum2DCurvature() const -> double
587654
{
655+
if (curves_.empty()) {
656+
THROW_SEMANTIC_ERROR(
657+
"Curves are empty. We cannot determine the maximum 2D curvature of the spline.",
658+
"This message is not originally intended to be displayed, if you see it, please contact "
659+
"the developer of traffic_simulator.");
660+
}
661+
/// @note Maximum 2D curvature is empty means that it is not calculated yet.
588662
if (maximum_2d_curvatures_.empty()) {
589-
THROW_SIMULATION_ERROR("maximum 2D curvature vector size is 0."); // LCOV_EXCL_LINE
663+
for (const auto & curve : curves_) {
664+
maximum_2d_curvatures_.emplace_back(curve.getMaximum2DCurvature());
665+
}
590666
}
591667
const auto [min, max] =
592668
std::minmax_element(maximum_2d_curvatures_.begin(), maximum_2d_curvatures_.end());

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,21 +135,16 @@ std::set<double> HermiteCurve::getCollisionPointsIn2D(
135135
double d = dy_ * ex - dx_ * ey - ex * fy + ey * fx;
136136

137137
const auto get_solutions = [search_backward, a, b, c, d, this]() -> std::vector<double> {
138-
try {
138+
if (
139+
solver_.isApproximatelyEqualTo(a, 0.0) && solver_.isApproximatelyEqualTo(b, 0.0) &&
140+
solver_.isApproximatelyEqualTo(c, 0.0) && solver_.isApproximatelyEqualTo(d, 0.0)) {
139141
/**
140-
* @note Obtain a solution to the cubic equation ax^3 + bx^2 + cx + d = 0 that falls within the range [0, 1].
142+
* @note If all coefficients are zero, the cubic equation is satisfied for all x values.
143+
* In this case, return 0 or 1 depending on the search direction.
141144
*/
142-
return solver_.solveCubicEquation(a, b, c, d, 0, 1);
143-
}
144-
/**
145-
* @note PolynomialSolver::solveCubicEquation throws common::SimulationError when any x value can satisfy the equation,
146-
* so the beginning and end point of this curve can collide with the line segment.
147-
* If search_backward = true, the line segment collisions at the end of the curve. So return 1.
148-
* If search_backward = false, the line segment collisions at the start of the curve. So return 0.
149-
*/
150-
catch (const common::SimulationError &) {
151145
return {search_backward ? 1.0 : 0.0};
152146
}
147+
return solver_.solveCubicEquation(a, b, c, d, 0, 1);
153148
};
154149

155150
/**

common/math/geometry/test/src/polygon/test_line_segment.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -752,13 +752,7 @@ TEST(LineSegment, getLineSegmentsVectorIdentical)
752752
const std::vector<geometry_msgs::msg::Point> points{point, point, point, point};
753753
const std::vector<math::geometry::LineSegment> lines =
754754
math::geometry::getLineSegments(points, false);
755-
EXPECT_EQ(lines.size(), size_t(3));
756-
EXPECT_POINT_EQ(lines[0].start_point, point);
757-
EXPECT_POINT_EQ(lines[0].end_point, point);
758-
EXPECT_POINT_EQ(lines[1].start_point, point);
759-
EXPECT_POINT_EQ(lines[1].end_point, point);
760-
EXPECT_POINT_EQ(lines[2].start_point, point);
761-
EXPECT_POINT_EQ(lines[2].end_point, point);
755+
EXPECT_EQ(lines.size(), size_t(0));
762756
}
763757

764758
TEST(LineSegment, getLineSegmentsVectorIdentical_closeStartEnd)
@@ -767,15 +761,7 @@ TEST(LineSegment, getLineSegmentsVectorIdentical_closeStartEnd)
767761
const std::vector<geometry_msgs::msg::Point> points{point, point, point, point};
768762
const std::vector<math::geometry::LineSegment> lines =
769763
math::geometry::getLineSegments(points, true);
770-
EXPECT_EQ(lines.size(), size_t(4));
771-
EXPECT_POINT_EQ(lines[0].start_point, point);
772-
EXPECT_POINT_EQ(lines[0].end_point, point);
773-
EXPECT_POINT_EQ(lines[1].start_point, point);
774-
EXPECT_POINT_EQ(lines[1].end_point, point);
775-
EXPECT_POINT_EQ(lines[2].start_point, point);
776-
EXPECT_POINT_EQ(lines[2].end_point, point);
777-
EXPECT_POINT_EQ(lines[3].start_point, point);
778-
EXPECT_POINT_EQ(lines[3].end_point, point);
764+
EXPECT_EQ(lines.size(), size_t(0));
779765
}
780766

781767
int main(int argc, char ** argv)

mock/cpp_mock_scenarios/launch/mock_test.launch.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ def launch_setup(context, *args, **kwargs):
107107
autoware_launch_package = LaunchConfiguration("autoware_launch_package", default=default_autoware_launch_package_of(architecture_type.perform(context)))
108108
consider_acceleration_by_road_slope = LaunchConfiguration("consider_acceleration_by_road_slope", default=False)
109109
consider_pose_by_road_slope = LaunchConfiguration("consider_pose_by_road_slope", default=True)
110+
enable_perf = LaunchConfiguration("enable_perf", default=False)
110111
global_frame_rate = LaunchConfiguration("global_frame_rate", default=20.0)
111112
global_real_time_factor = LaunchConfiguration("global_real_time_factor", default=1.0)
112113
global_timeout = LaunchConfiguration("global_timeout", default=180)
@@ -135,6 +136,7 @@ def launch_setup(context, *args, **kwargs):
135136
print(f"autoware_launch_package := {autoware_launch_package.perform(context)}")
136137
print(f"consider_acceleration_by_road_slope := {consider_acceleration_by_road_slope.perform(context)}")
137138
print(f"consider_pose_by_road_slope := {consider_pose_by_road_slope.perform(context)}")
139+
print(f"enable_perf := {enable_perf.perform(context)}")
138140
print(f"global_frame_rate := {global_frame_rate.perform(context)}")
139141
print(f"global_real_time_factor := {global_real_time_factor.perform(context)}")
140142
print(f"global_timeout := {global_timeout.perform(context)}")
@@ -182,6 +184,13 @@ def make_parameters():
182184
parameters += [parameter_file_path.perform(context)]
183185
return parameters
184186

187+
def make_launch_prefix():
188+
if enable_perf.perform(context) == "True":
189+
return "perf record -F 10000"
190+
else:
191+
return ""
192+
193+
185194
def make_vehicle_parameters():
186195
parameters = []
187196

@@ -219,6 +228,7 @@ def description():
219228
DeclareLaunchArgument("autoware_launch_package", default_value=autoware_launch_package ),
220229
DeclareLaunchArgument("consider_acceleration_by_road_slope", default_value=consider_acceleration_by_road_slope),
221230
DeclareLaunchArgument("consider_pose_by_road_slope", default_value=consider_pose_by_road_slope ),
231+
DeclareLaunchArgument("enable_perf", default_value=enable_perf ),
222232
DeclareLaunchArgument("global_frame_rate", default_value=global_frame_rate ),
223233
DeclareLaunchArgument("global_real_time_factor", default_value=global_real_time_factor ),
224234
DeclareLaunchArgument("global_timeout", default_value=global_timeout ),
@@ -253,6 +263,7 @@ def description():
253263
name="visualizer",
254264
output="screen",
255265
remappings=[("/simulation/entity/status", "/entity/status")],
266+
prefix=make_launch_prefix(),
256267
),
257268
Node(
258269
package="rviz2",

simulation/do_nothing_plugin/src/plugin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <geometry/vector3/operator.hpp>
2121
#include <geometry_msgs/msg/accel.hpp>
2222
#include <geometry_msgs/msg/twist.hpp>
23+
#include <traffic_simulator/lanelet_wrapper/lanelet_map.hpp>
2324

2425
namespace entity_behavior
2526
{
@@ -180,7 +181,7 @@ void DoNothingBehavior::update(double current_time, double step_time)
180181
canonicalized_entity_status_->setTime(current_time);
181182
if (getRequest() == traffic_simulator::behavior::Request::FOLLOW_POLYLINE_TRAJECTORY) {
182183
canonicalized_entity_status_->set(
183-
interpolate_entity_status_on_polyline_trajectory(),
184+
interpolate_entity_status_on_polyline_trajectory(), getRouteLanelets(),
184185
getDefaultMatchingDistanceForLaneletPoseCalculation());
185186
if (
186187
getCurrentTime() + getStepTime() >=
@@ -190,7 +191,7 @@ void DoNothingBehavior::update(double current_time, double step_time)
190191
} else {
191192
canonicalized_entity_status_->set(
192193
static_cast<traffic_simulator::EntityStatus>(*canonicalized_entity_status_),
193-
getDefaultMatchingDistanceForLaneletPoseCalculation());
194+
getRouteLanelets(), getDefaultMatchingDistanceForLaneletPoseCalculation());
194195
}
195196
}
196197
auto DoNothingBehavior::getCurrentAction() -> const std::string &

simulation/traffic_simulator/include/traffic_simulator/entity/entity_manager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class EntityManager
6565
lanelet_marker_pub_ptr_(rclcpp::create_publisher<MarkerArray>(
6666
node, "lanelet/marker", LaneletMarkerQoS(),
6767
rclcpp::PublisherOptionsWithAllocator<AllocatorT>())),
68+
entities_(128),
6869
hdmap_utils_ptr_(std::make_shared<hdmap_utils::HdMapUtils>(
6970
configuration_.lanelet2_map_path(), getOrigin(*node))),
7071
markers_raw_(hdmap_utils_ptr_->generateMarker())

0 commit comments

Comments
 (0)