Autoware.Auto
generic_state.hpp
Go to the documentation of this file.
1 // Copyright 2021 the Autoware Foundation
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 // Developed by Apex.AI, Inc.
16 
21 
22 #ifndef STATE_VECTOR__GENERIC_STATE_HPP_
23 #define STATE_VECTOR__GENERIC_STATE_HPP_
24 
25 #include <common/type_traits.hpp>
26 #include <common/types.hpp>
31 
32 #pragma GCC diagnostic push
33 #pragma GCC diagnostic ignored "-Wold-style-cast"
34 #pragma GCC diagnostic ignored "-Wsign-conversion"
35 #pragma GCC diagnostic ignored "-Wconversion"
36 #pragma GCC diagnostic ignored "-Wuseless-cast"
37 #include <Eigen/Core>
38 #pragma GCC diagnostic pop
39 
40 #include <tuple>
41 
42 namespace autoware
43 {
44 namespace common
45 {
46 namespace state_vector
47 {
48 
50 template<typename StateT>
51 struct is_state;
52 
53 
59 template<typename ScalarT, typename ... VariableTs>
60 class STATE_VECTOR_PUBLIC GenericState
61 {
62  static_assert(
64  "\n\nState can only be generated from variables, i.e. types that inherit from Variable.\n\n");
65  static_assert(
66  std::is_arithmetic<ScalarT>::value, "\n\nThe provided scalar type is not arithmetic.\n\n");
67  static_assert(
68  sizeof...(VariableTs) > 0, "\n\nCannot create state without variables.\n\n");
69  // Hide this under private to make sure it is never ODR-used.
70  constexpr static std::int32_t kSize = sizeof...(VariableTs);
71 
72 public:
73  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
74  using Variables = std::tuple<VariableTs...>;
75  using Vector = Eigen::Matrix<ScalarT, kSize, 1>;
76  using Matrix = Eigen::Matrix<ScalarT, kSize, kSize>;
77  using Scalar = ScalarT;
78 
82  GenericState() = default;
83 
89  explicit GenericState(const Vector & vector)
90  : m_state{vector} {}
91 
97  inline Vector & vector() noexcept {return m_state;}
103  inline const Vector & vector() const noexcept {return m_state;}
104 
112  inline ScalarT & operator[](const Eigen::Index idx) {return m_state[idx];}
120  inline const ScalarT & operator[](const Eigen::Index idx) const {return m_state[idx];}
121 
129  template<typename VariableT>
130  inline ScalarT & at() noexcept
131  {
132  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
134  }
142  template<typename VariableT>
143  inline ScalarT & at(const VariableT) noexcept
144  {
145  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
147  }
155  template<typename VariableT>
156  inline const ScalarT & at() const noexcept
157  {
158  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
160  }
168  template<typename VariableT>
169  inline const ScalarT & at(const VariableT) const noexcept
170  {
171  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
173  }
174 
175 
199  template<typename OtherStateT>
200  inline OtherStateT copy_into(OtherStateT other_state = OtherStateT{}) const noexcept
201  {
202  static_assert(
204  "\n\nOtherStateT is not a GenericState.\n\n");
205  auto copy_variables = [&other_state, this](auto variable) {
206  other_state.at(variable) = this->at(variable);
207  };
208  using CommonVariablesTuple = typename autoware::common::type_traits::intersect<
209  Variables, typename OtherStateT::Variables>::type;
210  common::type_traits::visit(CommonVariablesTuple{}, copy_variables);
211  return other_state;
212  }
213 
221  template<typename VariableT>
222  inline constexpr static Eigen::Index index_of() noexcept
223  {
224  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
226  }
227 
235  inline constexpr static Eigen::Index size() noexcept {return kSize;}
236 
238  inline constexpr static Variables variables() noexcept{return Variables{};}
239 
241  friend constexpr bool operator==(const GenericState & lhs, const GenericState & rhs)
242  {
243  return lhs.m_state.isApprox(rhs.m_state);
244  }
245 
249  friend std::ostream & operator<<(std::ostream & out, const GenericState & state)
250  {
251  out << "State:";
252  auto print = [&out, &state](auto element) {
253  out << "\n " <<
254  autoware::helper_functions::get_type_name(element) << ": " << state.at(element);
255  };
257  return out;
258  }
259 
267  inline GenericState & operator+=(const GenericState & rhs) noexcept
268  {
269  return this->operator+=(rhs.vector());
270  }
271 
282  inline GenericState & operator+=(const Vector & rhs) noexcept
283  {
284  this->vector() += rhs;
285  return *this;
286  }
287 
295  inline GenericState & operator-=(const GenericState & rhs) noexcept
296  {
297  return this->operator-=(rhs.vector());
298  }
299 
310  inline GenericState & operator-=(const Vector & rhs) noexcept
311  {
312  this->vector() -= rhs;
313  return *this;
314  }
315 
316 
327  template<typename T>
328  inline friend GenericState operator-(GenericState lhs, const T & rhs) noexcept
329  {
330  lhs -= rhs;
331  return lhs;
332  }
333 
344  template<typename T>
345  inline friend GenericState operator+(GenericState lhs, const T & rhs) noexcept
346  {
347  lhs += rhs;
348  return lhs;
349  }
350 
355  constexpr void wrap_all_angles() noexcept
356  {
357  const auto wrap_angles = [this](auto variable) {
358  using VariableT = decltype(variable);
360  this->at(variable) = common::helper_functions::wrap_angle(this->at(variable));
361  }
362  };
364  }
365 
370  constexpr friend GenericState wrap_all_angles(GenericState state) noexcept
371  {
372  state.wrap_all_angles();
373  return state;
374  }
375 
381  template<typename NewScalarT>
382  GenericState<NewScalarT, VariableTs...> cast() const noexcept
383  {
384  return GenericState<NewScalarT, VariableTs...>{m_state.template cast<NewScalarT>()};
385  }
386 
387 private:
389  Vector m_state{Vector::Zero()};
390 };
391 
397 template<typename StateT>
398 struct STATE_VECTOR_PUBLIC is_state : public std::false_type {};
399 
406 template<typename ScalarT, typename ... VariableTs>
407 struct STATE_VECTOR_PUBLIC is_state<GenericState<ScalarT, VariableTs...>>
408  : public std::true_type {};
409 
411 template<typename ... Ts>
413 
415 template<typename ... Ts>
417 
418 } // namespace state_vector
419 } // namespace common
420 } // namespace autoware
421 
422 #endif // STATE_VECTOR__GENERIC_STATE_HPP_
common_variables.hpp
This file defines the common variables used within the filter implementations.
autoware::common::state_vector::GenericState::operator-=
GenericState & operator-=(const Vector &rhs) noexcept
Subtraction assignment operator.
Definition: generic_state.hpp:310
autoware::common::state_vector::GenericState::Scalar
ScalarT Scalar
Definition: generic_state.hpp:77
autoware::common::state_vector::GenericState::at
ScalarT & at(const VariableT) noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:143
autoware::common::state_vector::is_angle
A trait to check if a variable represents an angle.
Definition: variable.hpp:64
autoware::common::state_vector::is_state
Forward-declare is_state trait.
Definition: generic_state.hpp:51
type_traits.hpp
autoware::common::state_vector::GenericState::Variables
std::tuple< VariableTs... > Variables
Definition: generic_state.hpp:74
autoware::common::state_vector::GenericState::at
const ScalarT & at(const VariableT) const noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:169
types.hpp
This file includes common type definition.
autoware::common::state_vector::GenericState::Matrix
Eigen::Matrix< ScalarT, kSize, kSize > Matrix
Definition: generic_state.hpp:76
autoware::common::state_vector::GenericState::operator+
friend GenericState operator+(GenericState lhs, const T &rhs) noexcept
Addition operator.
Definition: generic_state.hpp:345
autoware::common::state_vector::is_variable
A trait to check if a type is a variable by checking if it inherits from Variable.
Definition: variable.hpp:55
autoware::common::state_vector::GenericState::size
constexpr static Eigen::Index size() noexcept
Get the size of the state.
Definition: generic_state.hpp:235
autoware::common::state_vector::GenericState::vector
const Vector & vector() const noexcept
Get underlying Eigen vector.
Definition: generic_state.hpp:103
angle_utils.hpp
autoware::common::state_vector::GenericState::wrap_all_angles
constexpr friend GenericState wrap_all_angles(GenericState state) noexcept
Wrap all variables that represent angles using the common::helper_functions::wrap_angle.
Definition: generic_state.hpp:370
autoware::common::state_vector::GenericState
A representation of a generic state vectors with specified variables.
Definition: generic_state.hpp:60
autoware::common::type_traits::conjunction
A class to compute a conjunction over given traits.
Definition: type_traits.hpp:119
autoware::common::state_vector::GenericState::copy_into
OtherStateT copy_into(OtherStateT other_state=OtherStateT{}) const noexcept
Copy values of this state into another state.
Definition: generic_state.hpp:200
autoware::common::state_vector::GenericState::vector
Vector & vector() noexcept
Get underlying Eigen vector.
Definition: generic_state.hpp:97
autoware::common::state_vector::GenericState::operator+=
GenericState & operator+=(const GenericState &rhs) noexcept
Addition assignment operator.
Definition: generic_state.hpp:267
autoware::common::helper_functions::wrap_angle
constexpr T wrap_angle(T angle) noexcept
Wrap angle to the [-pi, pi] range.
Definition: angle_utils.hpp:50
autoware::helper_functions::get_type_name
COMMON_PUBLIC std::string get_type_name()
Get a demangled name of a type.
Definition: type_name.hpp:36
autoware::common::state_vector::GenericState::operator-
friend GenericState operator-(GenericState lhs, const T &rhs) noexcept
Subtraction operator.
Definition: generic_state.hpp:328
autoware::common::state_vector::GenericState::at
const ScalarT & at() const noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:156
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
autoware::common::type_traits::index
Find an index of a type in a tuple.
Definition: type_traits.hpp:52
autoware::common::state_vector::GenericState::Vector
Eigen::Matrix< ScalarT, kSize, 1 > Vector
Definition: generic_state.hpp:75
autoware::common::state_vector::GenericState::operator[]
ScalarT & operator[](const Eigen::Index idx)
Get an element of the vector.
Definition: generic_state.hpp:112
autoware::common::state_vector::GenericState::operator-=
GenericState & operator-=(const GenericState &rhs) noexcept
Subtraction assignment operator.
Definition: generic_state.hpp:295
autoware::common::state_vector::GenericState::wrap_all_angles
constexpr void wrap_all_angles() noexcept
Wrap all variables that represent angles using the common::helper_functions::wrap_angle.
Definition: generic_state.hpp:355
autoware::common::types::float32_t
float float32_t
Definition: types.hpp:45
visibility_control.hpp
autoware::common::state_vector::GenericState::at
ScalarT & at() noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:130
autoware::common::state_vector::GenericState::operator+=
GenericState & operator+=(const Vector &rhs) noexcept
Addition assignment operator.
Definition: generic_state.hpp:282
autoware::common::state_vector::GenericState::operator==
constexpr friend bool operator==(const GenericState &lhs, const GenericState &rhs)
An equality operator.
Definition: generic_state.hpp:241
autoware::common::state_vector::GenericState::variables
constexpr static Variables variables() noexcept
Get a variables tuple used for iteration over all variables.
Definition: generic_state.hpp:238
autoware::common::state_vector::GenericState::operator[]
const ScalarT & operator[](const Eigen::Index idx) const
Get an element of the vector.
Definition: generic_state.hpp:120
autoware::common::state_vector::GenericState::cast
GenericState< NewScalarT, VariableTs... > cast() const noexcept
Cast to another scalar type.
Definition: generic_state.hpp:382
autoware::common::type_traits::intersect
A trait used to intersect types stored in tuples at compile time. The resulting typedef type will hol...
Definition: type_traits.hpp:180
autoware::common::state_vector::GenericState::index_of
constexpr static Eigen::Index index_of() noexcept
Get an index of the variable in the state.
Definition: generic_state.hpp:222
autoware::common::types::float64_t
double float64_t
Definition: types.hpp:47
type_name.hpp
autoware::common::state_vector::GenericState::operator<<
friend std::ostream & operator<<(std::ostream &out, const GenericState &state)
Allow using << operator with this class.
Definition: generic_state.hpp:249
autoware::common::state_vector::GenericState::GenericState
GenericState(const Vector &vector)
Constructs a new instance from an Eigen vector.
Definition: generic_state.hpp:89
autoware::common::geometry::spatial_hash::Index
std::size_t Index
Index type for identifying bins of the hash/lattice.
Definition: spatial_hash_config.hpp:49
autoware::common::type_traits::visit
constexpr COMMON_PUBLIC std::enable_if_t< I==sizeof...(TypesT)> visit(std::tuple< TypesT... > &, Callable) noexcept
Visit every element in a tuple.
Definition: type_traits.hpp:80