forked from autowarefoundation/autoware.universe
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(autoware_motion_utils): add spherical linear interpolator (autow…
…arefoundation#9175) Signed-off-by: Y.Hisaki <yhisaki31@gmail.com>
- Loading branch information
Showing
5 changed files
with
191 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...tils/include/autoware/motion_utils/trajectory_container/interpolator/spherical_linear.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2024 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 AUTOWARE__MOTION_UTILS__TRAJECTORY_CONTAINER__INTERPOLATOR__SPHERICAL_LINEAR_HPP_ | ||
#define AUTOWARE__MOTION_UTILS__TRAJECTORY_CONTAINER__INTERPOLATOR__SPHERICAL_LINEAR_HPP_ | ||
|
||
#include "autoware/motion_utils/trajectory_container/interpolator/detail/interpolator_mixin.hpp" | ||
|
||
#include <geometry_msgs/msg/quaternion.hpp> | ||
|
||
#include <vector> | ||
|
||
namespace autoware::motion_utils::trajectory_container::interpolator | ||
{ | ||
|
||
/** | ||
* @brief Class for SphericalLinear interpolation. | ||
* | ||
* This class provides methods to perform SphericalLinear interpolation on a set of data points. | ||
*/ | ||
class SphericalLinear | ||
: public detail::InterpolatorMixin<SphericalLinear, geometry_msgs::msg::Quaternion> | ||
{ | ||
private: | ||
std::vector<geometry_msgs::msg::Quaternion> quaternions_; | ||
|
||
/** | ||
* @brief Build the interpolator with the given values. | ||
* | ||
* @param bases The bases values. | ||
* @param values The values to interpolate. | ||
* @return True if the interpolator was built successfully, false otherwise. | ||
*/ | ||
void build_impl( | ||
const std::vector<double> & bases, | ||
const std::vector<geometry_msgs::msg::Quaternion> & quaternions) override; | ||
|
||
/** | ||
* @brief Compute the interpolated value at the given point. | ||
* | ||
* @param s The point at which to compute the interpolated value. | ||
* @return The interpolated value. | ||
*/ | ||
[[nodiscard]] geometry_msgs::msg::Quaternion compute_impl(const double & s) const override; | ||
|
||
public: | ||
/** | ||
* @brief Default constructor. | ||
*/ | ||
SphericalLinear() = default; | ||
|
||
/** | ||
* @brief Get the minimum number of required points for the interpolator. | ||
* | ||
* @return The minimum number of required points. | ||
*/ | ||
[[nodiscard]] size_t minimum_required_points() const override; | ||
}; | ||
|
||
} // namespace autoware::motion_utils::trajectory_container::interpolator | ||
|
||
#endif // AUTOWARE__MOTION_UTILS__TRAJECTORY_CONTAINER__INTERPOLATOR__SPHERICAL_LINEAR_HPP_ |
64 changes: 64 additions & 0 deletions
64
common/autoware_motion_utils/src/trajectory_container/interpolator/spherical_linear.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2024 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. | ||
|
||
#include "autoware/motion_utils/trajectory_container/interpolator/spherical_linear.hpp" | ||
|
||
#include <Eigen/Geometry> | ||
|
||
#include <vector> | ||
|
||
namespace autoware::motion_utils::trajectory_container::interpolator | ||
{ | ||
|
||
void SphericalLinear::build_impl( | ||
const std::vector<double> & bases, | ||
const std::vector<geometry_msgs::msg::Quaternion> & quaternions) | ||
{ | ||
this->bases_ = bases; | ||
this->quaternions_ = quaternions; | ||
} | ||
|
||
geometry_msgs::msg::Quaternion SphericalLinear::compute_impl(const double & s) const | ||
{ | ||
const int32_t idx = this->get_index(s); | ||
const double x0 = this->bases_.at(idx); | ||
const double x1 = this->bases_.at(idx + 1); | ||
const geometry_msgs::msg::Quaternion y0 = this->quaternions_.at(idx); | ||
const geometry_msgs::msg::Quaternion y1 = this->quaternions_.at(idx + 1); | ||
|
||
// Spherical linear interpolation (Slerp) | ||
const double t = (s - x0) / (x1 - x0); | ||
|
||
// Convert quaternions to Eigen vectors for calculation | ||
Eigen::Quaterniond q0(y0.w, y0.x, y0.y, y0.z); | ||
Eigen::Quaterniond q1(y1.w, y1.x, y1.y, y1.z); | ||
|
||
// Perform Slerp | ||
Eigen::Quaterniond q_slerp = q0.slerp(t, q1); | ||
|
||
// Convert the result back to geometry_msgs::msg::Quaternion | ||
geometry_msgs::msg::Quaternion result; | ||
result.w = q_slerp.w(); | ||
result.x = q_slerp.x(); | ||
result.y = q_slerp.y(); | ||
result.z = q_slerp.z(); | ||
|
||
return result; | ||
} | ||
|
||
size_t SphericalLinear::minimum_required_points() const | ||
{ | ||
return 2; | ||
} | ||
} // namespace autoware::motion_utils::trajectory_container::interpolator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters