scenario_simulator_v2 C++ API
operator.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__VECTOR3__OPERATOR_HPP_
16 #define GEOMETRY__VECTOR3__OPERATOR_HPP_
17 
18 #include <Eigen/Dense>
20 #include <geometry_msgs/msg/point.hpp>
21 #include <geometry_msgs/msg/quaternion.hpp>
22 #include <geometry_msgs/msg/vector3.hpp>
23 #include <limits>
24 
25 namespace math
26 {
27 namespace geometry
28 {
29 template <
30  typename T, std::enable_if_t<std::conjunction_v<IsLikeVector3<T>>, std::nullptr_t> = nullptr>
31 auto operator+(const T & v, const Eigen::Vector3d & eigen_v) -> T
32 {
33  T result;
34  result.x = v.x + eigen_v.x();
35  result.y = v.y + eigen_v.y();
36  result.z = v.z + eigen_v.z();
37  return result;
38 }
39 
40 template <
41  typename T, typename U,
42  std::enable_if_t<std::conjunction_v<IsLikeVector3<T>, IsLikeVector3<U>>, std::nullptr_t> =
43  nullptr>
44 auto operator+(const T & a, const U & b)
45 {
46  if constexpr (std::is_same_v<T, geometry_msgs::msg::Vector3>) {
48  v.x = a.x + b.x;
49  v.y = a.y + b.y;
50  v.z = a.z + b.z;
51  return v;
52  } else {
54  v.x = a.x + b.x;
55  v.y = a.y + b.y;
56  v.z = a.z + b.z;
57  return v;
58  }
59 }
60 
61 template <
62  typename T, typename U,
63  std::enable_if_t<std::conjunction_v<IsLikeVector3<T>, IsLikeVector3<U>>, std::nullptr_t> =
64  nullptr>
65 auto operator-(const T & a, const U & b)
66 {
67  if constexpr (std::is_same_v<T, geometry_msgs::msg::Vector3>) {
69  v.x = a.x - b.x;
70  v.y = a.y - b.y;
71  v.z = a.z - b.z;
72  return v;
73  } else {
75  v.x = a.x - b.x;
76  v.y = a.y - b.y;
77  v.z = a.z - b.z;
78  return v;
79  }
80 }
81 
82 template <
83  typename T, typename U,
84  std::enable_if_t<std::conjunction_v<IsLikeVector3<T>, std::is_scalar<U>>, std::nullptr_t> =
85  nullptr>
86 auto operator*(const T & a, const U & b)
87 {
88  if constexpr (std::is_same_v<T, geometry_msgs::msg::Vector3>) {
90  v.x = a.x * b;
91  v.y = a.y * b;
92  v.z = a.z * b;
93  return v;
94  } else {
96  v.x = a.x * b;
97  v.y = a.y * b;
98  v.z = a.z * b;
99  return v;
100  }
101 }
102 
103 template <
104  typename T, typename U,
105  std::enable_if_t<std::conjunction_v<IsLikeVector3<T>, std::is_scalar<U>>, std::nullptr_t> =
106  nullptr>
107 auto operator/(const T & a, const U & b)
108 {
109  if constexpr (std::is_same_v<T, geometry_msgs::msg::Vector3>) {
111  v.x = a.x / b;
112  v.y = a.y / b;
113  v.z = a.z / b;
114  return v;
115  } else {
117  v.x = a.x / b;
118  v.y = a.y / b;
119  v.z = a.z / b;
120  return v;
121  }
122 }
123 
124 template <
125  typename T, typename U,
126  std::enable_if_t<std::conjunction_v<IsLikeVector3<T>, IsLikeVector3<U>>, std::nullptr_t> =
127  nullptr>
128 auto operator+=(T & a, const U & b) -> decltype(auto)
129 {
130  a.x += b.x;
131  a.y += b.y;
132  a.z += b.z;
133  return a;
134 }
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 }
145 } // namespace geometry
146 } // namespace math
147 
148 #endif // GEOMETRY__VECTOR3__OPERATOR_HPP_
auto operator==(const T &a, const U &b) -> bool
Definition: operator.hpp:140
auto operator/(const T &a, const U &b)
Definition: operator.hpp:107
auto operator*(const T &a, const U &b)
Definition: operator.hpp:57
auto operator+=(T &a, const U &b) -> decltype(auto)
Definition: operator.hpp:71
auto operator-(const T &a, const U &b)
Definition: operator.hpp:43
auto operator+(const T &a, const U &b)
Definition: operator.hpp:29
Definition: bounding_box.hpp:32
geometry_msgs::msg::Point Point
Definition: lanelet_wrapper.hpp:65
geometry_msgs::msg::Vector3 Vector3
Definition: lanelet_wrapper.hpp:69