OpenKalman
TransformBase.hpp
1 /* This file is part of OpenKalman, a header-only C++ library for
2  * Kalman filters and other recursive filters.
3  *
4  * Copyright (c) 2020-2021 Christopher Lee Ogden <ogden@gatech.edu>
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
9  */
10 
17 #ifndef OPENKALMAN_TRANSFORMBASE_HPP
18 #define OPENKALMAN_TRANSFORMBASE_HPP
19 
21 
22 namespace OpenKalman::internal
23 {
29  template<typename Derived>
30  struct TransformBase;
31 
32 
33  template<typename Derived>
34  struct TransformBase
35  {
36 
45 #ifdef __cpp_concepts
46  template<distribution InputDist, tuple_like T, tuple_like...Ts>
47 #else
48  template<typename InputDist, typename T, typename...Ts, std::enable_if_t<
49  distribution<InputDist> and (tuple_like<T> and ... and tuple_like<Ts>), int> = 0>
50 #endif
51  auto operator()(const InputDist& x, const T& t, const Ts&...ts) const
52  {
53  auto y = std::apply([&](const auto&...args) { return static_cast<const Derived&>(*this)(x, args...); }, t);
54 
55  if constexpr (sizeof...(Ts) > 0)
56  {
57  return static_cast<const Derived&>(*this)(y, ts...);
58  }
59  else
60  {
61  return y;
62  }
63  }
64 
65 
74 #ifdef __cpp_concepts
75  template<distribution InputDist, tuple_like T, tuple_like...Ts>
76 #else
77  template<typename InputDist, typename T, typename...Ts, std::enable_if_t<
78  distribution<InputDist> and (tuple_like<T> and ... and tuple_like<Ts>), int> = 0>
79 #endif
80  auto transform_with_cross_covariance(const InputDist& x, const T& t, const Ts&...ts) const
81  {
82  if constexpr (sizeof...(Ts) > 0)
83  {
84  auto y = std::apply([&](const auto&...args) { return static_cast<const Derived&>(*this)(x, args...); }, t);
85  return static_cast<const Derived&>(*this).transform_with_cross_covariance(y, ts...);
86  }
87  else
88  {
89  return std::apply([&](const auto&...args) {
90  return static_cast<const Derived&>(*this).transform_with_cross_covariance(x, args...);
91  }, t);
92  }
93  }
94 
95  };
96 
97 }
98 
99 #endif //OPENKALMAN_TRANSFORMBASE_HPP
Definition for collections::tuple_like.
auto operator()(const InputDist &x, const T &t, const Ts &...ts) const
Perform one or more consecutive transforms.
Definition: TransformBase.hpp:51
Definition: TransformBase.hpp:30
constexpr bool distribution
T is a statistical distribution of any kind that is defined in OpenKalman.
Definition: object-types.hpp:193
Definition: basics.hpp:48
constexpr bool tuple_like
T is a non-empty tuple, pair, array, or other type that acts like a tuple.
Definition: tuple_like.hpp:51
auto transform_with_cross_covariance(const InputDist &x, const T &t, const Ts &...ts) const
Perform one or more consecutive transforms, also returning the cross-covariance.
Definition: TransformBase.hpp:80