Skip to content

Commit d39f2e8

Browse files
committed
add lateral metrics example (adjust example file)
Signed-off-by: Sarun Mukdapitak <sarun.mukda@gmail.com>
1 parent a9f6cb6 commit d39f2e8

2 files changed

Lines changed: 34 additions & 13 deletions

File tree

common/autoware_trajectory/README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,20 @@ common/autoware_trajectory/examples/example_self_intersecting.cpp:352:357
297297

298298
#### <span style="font-size: 1.2em;">`<autoware/trajectory/utils/lateral_metrics.hpp>`</span>
299299

300-
| Function | Description | Detail |
301-
| -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
302-
| <ul><li>`compute_lateral_distance`</li></ul> | A utility function that computes the normal distance between a given `target point` and the tangent line of the pose at specified arc-length `s` on the trajectory.<br> See below cell figure | |
303-
| <ul><li>`is_left_side`</li></ul> | A utility function that determines whether a given `target point` lies on the **left side** of the trajectory's tangent direction at the specified arc-length `s`. | ![lateral_metrics](./images/utils/lateral_distance_computation.drawio.svg)[View in Drawio]({{ drawio("/common/autoware_trajectory/images/utils/lateral_distance_computation.drawio.svg") }}) |
300+
| Function | Description | Detail |
301+
| -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
302+
| <ul><li>`compute_lateral_distance`</li></ul> | A utility function that computes the normal distance between a given `target point` and the tangent line of the pose at specified arc-length `s` on the trajectory. The result will always be positive. <br> See below cell figure | |
303+
| <ul><li>`is_left_side`</li></ul> | A utility function that determines whether a given `target point` lies on the **left side** of the trajectory's tangent direction at the specified arc-length `s`. | ![lateral_metrics](./images/utils/lateral_distance_computation.drawio.svg)[View in Drawio]({{ drawio("/common/autoware_trajectory/images/utils/lateral_distance_computation.drawio.svg") }}) |
304+
305+
##### <span style="font-size: 1.2em;">Example Usage of `lateral_metrics`</span>
306+
307+
Calculate lateral distance from Parabolic Trajectory to given point and check the side of the point from the trajectory.
308+
309+
```cpp title="./examples/example_lateral_metrics.cpp:144:155"
310+
--8<--
311+
common/autoware_trajectory/examples/example_lateral_metrics.cpp:144:155
312+
--8<--
313+
```
304314

305315
#### <span style="font-size: 1.2em;">`<autoware/trajectory/utils/footprint.hpp>`</span>
306316

common/autoware_trajectory/examples/example_lateral_metrics.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,24 @@ static void draw_line(
135135
}
136136
}
137137

138+
template <class TrajectoryPointType>
138139
static void draw_square_angle_line(
139-
autoware::pyplot::Axes & ax, const geometry_msgs::msg::Pose & from_pose,
140-
const geometry_msgs::msg::Point & to_point, const std::string & color, const std::string & label)
140+
autoware::pyplot::Axes & ax, const Trajectory<TrajectoryPointType> & traj, const double target_s,
141+
const geometry_msgs::msg::Pose & from_pose, const geometry_msgs::msg::Point & to_point,
142+
const std::string & color, const std::string & label)
141143
{
142-
const auto lateral_offset = autoware_utils_geometry::calc_lateral_deviation(from_pose, to_point);
143-
const auto vertical_point =
144-
autoware_utils_geometry::calc_offset_pose(from_pose, 0, lateral_offset, 0, 0);
144+
// Always give positive value
145+
double lateral_offset =
146+
autoware::experimental::trajectory::compute_lateral_distance(traj, to_point, target_s);
147+
auto on_left = autoware::experimental::trajectory::is_left_side(traj, to_point, target_s);
148+
149+
geometry_msgs::msg::Pose vertical_point;
150+
if (!on_left) {
151+
// positive y is translated to left, so need to convert to negative value for right side.
152+
vertical_point = autoware_utils_geometry::calc_offset_pose(from_pose, 0, -lateral_offset, 0, 0);
153+
} else { // left
154+
vertical_point = autoware_utils_geometry::calc_offset_pose(from_pose, 0, lateral_offset, 0, 0);
155+
}
145156

146157
std::vector<double> x_{from_pose.position.x, vertical_point.position.x, to_point.x};
147158
std::vector<double> y_{from_pose.position.y, vertical_point.position.y, to_point.y};
@@ -195,7 +206,7 @@ int main1()
195206

196207
draw_line(ax1, target_point, vertical_pose.position, "orange", "lateral distance");
197208
draw_line(ax1, target_point, target_pose.position, "red", "distance2d");
198-
draw_square_angle_line(ax1, target_pose, target_point, "black", line_label);
209+
draw_square_angle_line(ax1, traj, target_s, target_pose, target_point, "black", line_label);
199210
draw_tangent_line(ax1, target_pose);
200211

201212
plot_trajectory(ax2, traj, "Parabolic to the right");
@@ -205,7 +216,7 @@ int main1()
205216

206217
draw_line(ax2, target_point, vertical_pose.position, "orange", "lateral distance", true);
207218
draw_line(ax2, target_point, target_pose.position, "red", "distance2d", true);
208-
draw_square_angle_line(ax2, target_pose, target_point, "black", line_label);
219+
draw_square_angle_line(ax2, traj, target_s, target_pose, target_point, "black", line_label);
209220
draw_tangent_line(ax2, target_pose);
210221

211222
ax2.set_title(Args("To the right (Point on the Right)"), Kwargs("fontsize"_a = 16));
@@ -260,7 +271,7 @@ int main2()
260271
plot_point(ax1, vertical_pose.position, "blue", "Perpendicular Point");
261272
draw_line(ax1, target_point, vertical_pose.position, "orange", "lateral distance");
262273
draw_line(ax1, target_point, target_pose.position, "red", "distance2d");
263-
draw_square_angle_line(ax1, target_pose, target_point, "black", line_label);
274+
draw_square_angle_line(ax1, traj, target_s, target_pose, target_point, "black", line_label);
264275
draw_tangent_line(ax1, target_pose);
265276

266277
plot_trajectory(ax2, traj, "Parabolic to the right");
@@ -270,7 +281,7 @@ int main2()
270281

271282
draw_line(ax2, target_point, vertical_pose.position, "orange", "lateral distance", true);
272283
draw_line(ax2, target_point, target_pose.position, "red", "distance2d", true);
273-
draw_square_angle_line(ax2, target_pose, target_point, "black", line_label);
284+
draw_square_angle_line(ax2, traj, target_s, target_pose, target_point, "black", line_label);
274285
draw_tangent_line(ax2, target_pose);
275286

276287
ax2.set_title(Args("To the left (Point on the Left)"), Kwargs("fontsize"_a = 16));

0 commit comments

Comments
 (0)