Skip to content

Commit

Permalink
feat(motion_utils): add resample utils (autowarefoundation#1867)
Browse files Browse the repository at this point in the history
* feat(motion_utils): add resample utils

Signed-off-by: yutaka <purewater0901@gmail.com>

* use motion utils overlap threshold

Signed-off-by: yutaka <purewater0901@gmail.com>

* fix tests

Signed-off-by: yutaka <purewater0901@gmail.com>

Signed-off-by: yutaka <purewater0901@gmail.com>
  • Loading branch information
purewater0901 authored and boyali committed Oct 3, 2022
1 parent d8db49c commit 2b86874
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 157 deletions.
117 changes: 117 additions & 0 deletions common/motion_utils/include/motion_utils/resample/resample_utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2022 Tier IV, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef MOTION_UTILS__RESAMPLE__RESAMPLE_UTILS_HPP_
#define MOTION_UTILS__RESAMPLE__RESAMPLE_UTILS_HPP_

#include <motion_utils/constants.hpp>
#include <motion_utils/trajectory/trajectory.hpp>
#include <tier4_autoware_utils/geometry/geometry.hpp>
#include <tier4_autoware_utils/geometry/path_with_lane_id_geometry.hpp>

#include <vector>

namespace resample_utils
{
constexpr double CLOSE_S_THRESHOLD = 1e-6;

template <class T>
bool validate_size(const T & points)
{
if (points.size() < 2) {
return false;
}
return true;
}

template <class T>
bool validate_resampling_range(const T & points, const std::vector<double> & resampling_intervals)
{
const double points_length = motion_utils::calcArcLength(points);
if (points_length < resampling_intervals.back()) {
return false;
}

return true;
}

template <class T>
bool validate_points_duplication(const T & points)
{
for (size_t i = 0; i < points.size() - 1; ++i) {
const auto & curr_pt = tier4_autoware_utils::getPoint(points.at(i));
const auto & next_pt = tier4_autoware_utils::getPoint(points.at(i + 1));
const double ds = tier4_autoware_utils::calcDistance2d(curr_pt, next_pt);
if (ds < CLOSE_S_THRESHOLD) {
return false;
}
}

return true;
}

template <class T>
bool validate_arguments(const T & input_points, const std::vector<double> & resampling_intervals)
{
// Check size of the arguments
if (!validate_size(input_points)) {
std::cerr << "The number of input points is less than 2" << std::endl;
return false;
} else if (!validate_size(resampling_intervals)) {
std::cerr << "The number of resampling intervals is less than 2" << std::endl;
return false;
}

// Check resampling range
if (!validate_resampling_range(input_points, resampling_intervals)) {
std::cerr << "resampling interval is longer than input points" << std::endl;
return false;
}

// Check duplication
if (!validate_points_duplication(input_points)) {
std::cerr << "input points has some duplicated points" << std::endl;
return false;
}

return true;
}

template <class T>
bool validate_arguments(const T & input_points, const double resampling_interval)
{
// Check size of the arguments
if (!validate_size(input_points)) {
std::cerr << "The number of input points is less than 2" << std::endl;
return false;
}

// check resampling interval
if (resampling_interval < motion_utils::overlap_threshold) {
std::cerr << "resampling interval is less than " << motion_utils::overlap_threshold
<< std::endl;
return false;
}

// Check duplication
if (!validate_points_duplication(input_points)) {
std::cerr << "input points has some duplicated points" << std::endl;
return false;
}

return true;
}
} // namespace resample_utils

#endif // MOTION_UTILS__RESAMPLE__RESAMPLE_UTILS_HPP_
79 changes: 21 additions & 58 deletions common/motion_utils/src/resample/resample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,18 @@

#include "motion_utils/resample/resample.hpp"

#include "motion_utils/resample/resample_utils.hpp"
#include "tier4_autoware_utils/geometry/geometry.hpp"

constexpr double CLOSE_S_THRESHOLD = 1e-6;
namespace motion_utils
{
std::vector<geometry_msgs::msg::Pose> resamplePath(
const std::vector<geometry_msgs::msg::Pose> & points,
const std::vector<double> & resampled_arclength, const bool use_lerp_for_xy,
const bool use_lerp_for_z)
{
// Check vector size and if out_arclength have the end point of the path
const double input_path_len = motion_utils::calcArcLength(points);
if (
points.size() < 2 || resampled_arclength.size() < 2 ||
input_path_len < resampled_arclength.back()) {
std::cerr
<< "[motion_utils]: input points size, input points length or resampled arclength is wrong"
<< std::endl;
// validate arguments
if (!resample_utils::validate_arguments(points, resampled_arclength)) {
return points;
}

Expand All @@ -53,9 +47,6 @@ std::vector<geometry_msgs::msg::Pose> resamplePath(
const auto & prev_pt = points.at(i - 1);
const auto & curr_pt = points.at(i);
const double ds = tier4_autoware_utils::calcDistance2d(prev_pt.position, curr_pt.position);
if (ds < CLOSE_S_THRESHOLD) {
continue;
}
input_arclength.push_back(ds + input_arclength.back());
x.push_back(curr_pt.position.x);
y.push_back(curr_pt.position.y);
Expand Down Expand Up @@ -104,9 +95,8 @@ autoware_auto_planning_msgs::msg::PathWithLaneId resamplePath(
const std::vector<double> & resampled_arclength, const bool use_lerp_for_xy,
const bool use_lerp_for_z, const bool use_zero_order_hold_for_v)
{
// Check vector size and if out_arclength have the end point of the path
if (input_path.points.size() < 2 || resampled_arclength.size() < 2) {
std::cerr << "[motion_utils]: input path size or input path length is wrong" << std::endl;
// validate arguments
if (!resample_utils::validate_arguments(input_path.points, resampled_arclength)) {
return input_path;
}

Expand Down Expand Up @@ -150,9 +140,6 @@ autoware_auto_planning_msgs::msg::PathWithLaneId resamplePath(
const auto & curr_pt = input_path.points.at(i).point;
const double ds =
tier4_autoware_utils::calcDistance2d(prev_pt.pose.position, curr_pt.pose.position);
if (ds < CLOSE_S_THRESHOLD) {
continue;
}
input_arclength.push_back(ds + input_arclength.back());
input_pose.push_back(curr_pt.pose);
v_lon.push_back(curr_pt.longitudinal_velocity_mps);
Expand Down Expand Up @@ -213,11 +200,8 @@ autoware_auto_planning_msgs::msg::PathWithLaneId resamplePath(
const double resample_interval, const bool use_lerp_for_xy, const bool use_lerp_for_z,
const bool use_zero_order_hold_for_v, const bool resample_input_path_stop_point)
{
// Check vector size and if out_arclength have the end point of the trajectory
if (input_path.points.size() < 2 || resample_interval < 1e-3) {
std::cerr << "[motion_utils]: input trajectory size or resample "
"interval is invalid"
<< std::endl;
// validate arguments
if (!resample_utils::validate_arguments(input_path.points, resample_interval)) {
return input_path;
}

Expand All @@ -240,7 +224,7 @@ autoware_auto_planning_msgs::msg::PathWithLaneId resamplePath(
}

// Insert terminal point
if (input_path_len - resampling_arclength.back() < 1e-3) {
if (input_path_len - resampling_arclength.back() < motion_utils::overlap_threshold) {
resampling_arclength.back() = input_path_len;
} else {
resampling_arclength.push_back(input_path_len);
Expand All @@ -259,9 +243,9 @@ autoware_auto_planning_msgs::msg::PathWithLaneId resamplePath(
std::fabs(*distance_to_stop_point - resampling_arclength.at(i - 1));
const double dist_to_following_point =
std::fabs(resampling_arclength.at(i) - *distance_to_stop_point);
if (dist_to_prev_point < 1e-3) {
if (dist_to_prev_point < motion_utils::overlap_threshold) {
resampling_arclength.at(i - 1) = *distance_to_stop_point;
} else if (dist_to_following_point < 1e-3) {
} else if (dist_to_following_point < motion_utils::overlap_threshold) {
resampling_arclength.at(i) = *distance_to_stop_point;
} else {
resampling_arclength.insert(resampling_arclength.begin() + i, *distance_to_stop_point);
Expand All @@ -281,14 +265,8 @@ autoware_auto_planning_msgs::msg::Path resamplePath(
const std::vector<double> & resampled_arclength, const bool use_lerp_for_xy,
const bool use_lerp_for_z, const bool use_zero_order_hold_for_v)
{
// Check vector size and if out_arclength have the end point of the path
const double input_path_len = motion_utils::calcArcLength(input_path.points);
if (
input_path.points.size() < 2 || resampled_arclength.size() < 2 ||
input_path_len < resampled_arclength.back()) {
std::cerr
<< "[motion_utils]: input path size, input path length or resampled arclength is wrong"
<< std::endl;
// validate arguments
if (!resample_utils::validate_arguments(input_path.points, resampled_arclength)) {
return input_path;
}

Expand All @@ -314,9 +292,6 @@ autoware_auto_planning_msgs::msg::Path resamplePath(
const auto & curr_pt = input_path.points.at(i);
const double ds =
tier4_autoware_utils::calcDistance2d(prev_pt.pose.position, curr_pt.pose.position);
if (ds < CLOSE_S_THRESHOLD) {
continue;
}
input_arclength.push_back(ds + input_arclength.back());
input_pose.push_back(curr_pt.pose);
v_lon.push_back(curr_pt.longitudinal_velocity_mps);
Expand Down Expand Up @@ -365,14 +340,8 @@ autoware_auto_planning_msgs::msg::Trajectory resampleTrajectory(
const std::vector<double> & resampled_arclength, const bool use_lerp_for_xy,
const bool use_lerp_for_z, const bool use_zero_order_hold_for_twist)
{
// Check vector size and if out_arclength have the end point of the trajectory
const double input_trajectory_len = motion_utils::calcArcLength(input_trajectory.points);
if (
input_trajectory.points.size() < 2 || resampled_arclength.size() < 2 ||
input_trajectory_len < resampled_arclength.back()) {
std::cerr << "[motion_utils]: input trajectory size, input trajectory length or resampled "
"arclength is wrong"
<< std::endl;
// validate arguments
if (!resample_utils::validate_arguments(input_trajectory.points, resampled_arclength)) {
return input_trajectory;
}

Expand Down Expand Up @@ -411,9 +380,6 @@ autoware_auto_planning_msgs::msg::Trajectory resampleTrajectory(
const auto & curr_pt = input_trajectory.points.at(i);
const double ds =
tier4_autoware_utils::calcDistance2d(prev_pt.pose.position, curr_pt.pose.position);
if (ds < CLOSE_S_THRESHOLD) {
continue;
}
input_arclength.push_back(ds + input_arclength.back());
input_pose.push_back(curr_pt.pose);
v_lon.push_back(curr_pt.longitudinal_velocity_mps);
Expand Down Expand Up @@ -474,16 +440,13 @@ autoware_auto_planning_msgs::msg::Trajectory resampleTrajectory(
const double resample_interval, const bool use_lerp_for_xy, const bool use_lerp_for_z,
const bool use_zero_order_hold_for_twist, const bool resample_input_trajectory_stop_point)
{
const double input_trajectory_len = motion_utils::calcArcLength(input_trajectory.points);
// Check vector size and if out_arclength have the end point of the trajectory
if (
input_trajectory.points.size() < 2 || input_trajectory_len < 1e-3 || resample_interval < 1e-3) {
std::cerr << "[motion_utils]: input trajectory size, input_trajectory length or resample "
"interval is invalid"
<< std::endl;
// validate arguments
if (!resample_utils::validate_arguments(input_trajectory.points, resample_interval)) {
return input_trajectory;
}

const double input_trajectory_len = motion_utils::calcArcLength(input_trajectory.points);

std::vector<double> resampling_arclength;
for (double s = 0.0; s < input_trajectory_len; s += resample_interval) {
resampling_arclength.push_back(s);
Expand All @@ -494,7 +457,7 @@ autoware_auto_planning_msgs::msg::Trajectory resampleTrajectory(
}

// Insert terminal point
if (input_trajectory_len - resampling_arclength.back() < 1e-3) {
if (input_trajectory_len - resampling_arclength.back() < motion_utils::overlap_threshold) {
resampling_arclength.back() = input_trajectory_len;
} else {
resampling_arclength.push_back(input_trajectory_len);
Expand All @@ -513,9 +476,9 @@ autoware_auto_planning_msgs::msg::Trajectory resampleTrajectory(
std::fabs(*distance_to_stop_point - resampling_arclength.at(i - 1));
const double dist_to_following_point =
std::fabs(resampling_arclength.at(i) - *distance_to_stop_point);
if (dist_to_prev_point < 1e-3) {
if (dist_to_prev_point < motion_utils::overlap_threshold) {
resampling_arclength.at(i - 1) = *distance_to_stop_point;
} else if (dist_to_following_point < 1e-3) {
} else if (dist_to_following_point < motion_utils::overlap_threshold) {
resampling_arclength.at(i) = *distance_to_stop_point;
} else {
resampling_arclength.insert(resampling_arclength.begin() + i, *distance_to_stop_point);
Expand Down
Loading

0 comments on commit 2b86874

Please sign in to comment.