scenario_simulator_v2 C++ API
slerp.hpp
Go to the documentation of this file.
1 // Copyright 2015 TIER IV, Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GEOMETRY__QUATERNION__SLERP_HPP_
16 #define GEOMETRY__QUATERNION__SLERP_HPP_
17 
18 #include <cmath>
20 #include <geometry_msgs/msg/quaternion.hpp>
21 namespace math
22 {
23 namespace geometry
24 {
25 template <
26  typename T, typename U, typename V,
27  std::enable_if_t<
28  std::conjunction_v<IsLikeQuaternion<T>, IsLikeQuaternion<U>, std::is_scalar<V>>,
29  std::nullptr_t> = nullptr>
30 auto slerp(T quat1, U quat2, V t)
31 {
32  double qr = quat1.w * quat2.w + quat1.x * quat2.x + quat1.y * quat2.y + quat1.z * quat2.z;
33  double ss = 1.0 - qr * qr;
34  constexpr double e = std::numeric_limits<double>::epsilon();
35  if (std::fabs(ss) <= e) {
36  return geometry_msgs::build<geometry_msgs::msg::Quaternion>()
37  .x(quat1.x)
38  .y(quat1.y)
39  .z(quat1.z)
40  .w(quat1.w);
41  } else {
42  double sp = std::sqrt(ss);
43  double ph = std::acos(qr);
44  double pt = ph * t;
45  double t1 = std::sin(pt) / sp;
46  double t0 = std::sin(ph - pt) / sp;
47 
48  return geometry_msgs::build<geometry_msgs::msg::Quaternion>()
49  .x(quat1.x * t0 + quat2.x * t1)
50  .y(quat1.y * t0 + quat2.y * t1)
51  .z(quat1.z * t0 + quat2.z * t1)
52  .w(quat1.w * t0 + quat2.w * t1);
53  }
54 }
55 } // namespace geometry
56 } // namespace math
57 
58 #endif // GEOMETRY__QUATERNION__SLERP_HPP_
auto slerp(T quat1, U quat2, V t)
Definition: slerp.hpp:30
Definition: bounding_box.hpp:32