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 KALMAN_FILTER__GENERIC_STATE_HPP_
23 #define KALMAN_FILTER__GENERIC_STATE_HPP_
24 
25 #include <common/type_traits.hpp>
26 #include <common/types.hpp>
30 
31 #include <Eigen/Core>
32 
33 #include <tuple>
34 
35 namespace autoware
36 {
37 namespace prediction
38 {
39 
41 template<typename StateT>
42 struct is_state;
43 
49 template<typename ScalarT, typename ... VariableTs>
50 class KALMAN_FILTER_PUBLIC GenericState
51 {
52  static_assert(
54  "\n\nState can only be generated from variables, i.e. types that inherit from Variable.\n\n");
55  static_assert(
56  std::is_arithmetic<ScalarT>::value, "\n\nThe provided scalar type is not arithmetic.\n\n");
57  static_assert(
58  sizeof...(VariableTs) > 0, "\n\nCannot create state without variables.\n\n");
59  // Hide this under private to make sure it is never ODR-used.
60  constexpr static std::int32_t kSize = sizeof...(VariableTs);
61 
62 public:
63  using Variables = std::tuple<VariableTs...>;
64  using Vector = Eigen::Matrix<ScalarT, kSize, 1>;
65  using Matrix = Eigen::Matrix<ScalarT, kSize, kSize>;
66  using Scalar = ScalarT;
67 
71  GenericState() = default;
72 
78  explicit GenericState(const Vector & vector)
79  : m_state{vector} {}
80 
86  inline Vector & vector() noexcept {return m_state;}
92  inline const Vector & vector() const noexcept {return m_state;}
93 
101  inline ScalarT & operator[](const Eigen::Index idx) {return m_state[idx];}
109  inline const ScalarT & operator[](const Eigen::Index idx) const {return m_state[idx];}
110 
118  template<typename VariableT>
119  inline ScalarT & at() noexcept
120  {
121  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
123  }
131  template<typename VariableT>
132  inline ScalarT & at(const VariableT) noexcept
133  {
134  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
136  }
144  template<typename VariableT>
145  inline const ScalarT & at() const noexcept
146  {
147  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
149  }
157  template<typename VariableT>
158  inline const ScalarT & at(const VariableT) const noexcept
159  {
160  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
162  }
163 
164 
193  template<typename OtherStateT>
194  inline OtherStateT copy_into(OtherStateT other_state = OtherStateT{}) const noexcept
195  {
196  static_assert(
198  "\n\nOtherStateT is not a GenericState.\n\n");
199  auto copy_variables = [&other_state, this](auto variable) {
200  other_state.at(variable) = this->at(variable);
201  };
202  const auto & variables_tuple =
203  std::conditional_t<(OtherStateT::size() > kSize), GenericState, OtherStateT>::variables();
204  common::type_traits::visit(variables_tuple, copy_variables);
205  return other_state;
206  }
207 
215  template<typename VariableT>
216  inline constexpr static Eigen::Index index_of() noexcept
217  {
218  static_assert(is_variable<VariableT>::value, "Type is not a Variable.");
220  }
221 
229  inline constexpr static Eigen::Index size() noexcept {return kSize;}
230 
232  inline constexpr static Variables variables() noexcept{return Variables{};}
233 
235  friend constexpr bool operator==(const GenericState & lhs, const GenericState & rhs)
236  {
237  return lhs.m_state.isApprox(rhs.m_state);
238  }
239 
243  friend std::ostream & operator<<(std::ostream & out, const GenericState & state)
244  {
245  out << "State:";
246  auto print = [&out, &state](auto element) {
247  out << "\n " << helper_functions::get_type_name(element) << ": " << state.at(element);
248  };
250  return out;
251  }
252 
253 private:
255  Vector m_state{Vector::Zero()};
256 };
257 
263 template<typename StateT>
264 struct KALMAN_FILTER_PUBLIC is_state : public std::false_type {};
265 
272 template<typename ScalarT, typename ... VariableTs>
273 struct KALMAN_FILTER_PUBLIC is_state<GenericState<ScalarT, VariableTs...>>
274  : public std::true_type {};
275 
277 template<typename ... Ts>
279 
281 template<typename ... Ts>
283 
284 } // namespace prediction
285 } // namespace autoware
286 
287 #endif // KALMAN_FILTER__GENERIC_STATE_HPP_
A class to compute a conjunction over given traits.
Definition: type_traits.hpp:101
std::tuple< VariableTs... > Variables
Definition: generic_state.hpp:63
double float64_t
Definition: types.hpp:37
ScalarT Scalar
Definition: generic_state.hpp:66
float float32_t
Definition: types.hpp:36
This file includes common type definition.
ScalarT & at() noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:119
Find an index of a type in a tuple.
Definition: type_traits.hpp:34
static constexpr Eigen::Index index_of() noexcept
Get an index of the variable in the state.
Definition: generic_state.hpp:216
OtherStateT copy_into(OtherStateT other_state=OtherStateT{}) const noexcept
Copy values of this state into another state.
Definition: generic_state.hpp:194
A trait to check if a type is a variable by checking if it inherits from Variable.
Definition: variable.hpp:53
const Vector & vector() const noexcept
Get underlying Eigen vector.
Definition: generic_state.hpp:92
std::size_t Index
Index type for identifying bins of the hash/lattice.
Definition: spatial_hash_config.hpp:49
static constexpr Eigen::Index size() noexcept
Get the size of the state.
Definition: generic_state.hpp:229
const ScalarT & at(const VariableT) const noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:158
GenericState(const Vector &vector)
Constructs a new instance from an Eigen vector.
Definition: generic_state.hpp:78
friend constexpr bool operator==(const GenericState &lhs, const GenericState &rhs)
An equality operator.
Definition: generic_state.hpp:235
friend std::ostream & operator<<(std::ostream &out, const GenericState &state)
Allow using << operator with this class.
Definition: generic_state.hpp:243
A representation of a generic state vectors with specified variables.
Definition: generic_state.hpp:50
const ScalarT & operator[](const Eigen::Index idx) const
Get an element of the vector.
Definition: generic_state.hpp:109
Eigen::Matrix< ScalarT, kSize, 1 > Vector
Definition: generic_state.hpp:64
ScalarT & at(const VariableT) noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:132
const ScalarT & at() const noexcept
Get vector element at a given variable.
Definition: generic_state.hpp:145
COMMON_PUBLIC std::enable_if_t< I==sizeof...(TypesT)> visit(std::tuple< TypesT... > &, Callable)
Visit every element in a tuple.
Definition: type_traits.hpp:62
static constexpr Variables variables() noexcept
Get a variables tuple used for iteration over all variables.
Definition: generic_state.hpp:232
Vector & vector() noexcept
Get underlying Eigen vector.
Definition: generic_state.hpp:86
Eigen::Matrix< ScalarT, kSize, kSize > Matrix
Definition: generic_state.hpp:65
COMMON_PUBLIC std::string get_type_name()
Get a demangled name of a type.
Definition: type_name.hpp:34
This file defines the common variables used within the filter implementations.
ScalarT & operator[](const Eigen::Index idx)
Get an element of the vector.
Definition: generic_state.hpp:101
Forward-declare is_state trait.
Definition: generic_state.hpp:42
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24