OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
StateHistory.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_StateHistory_h_GUID_07D9DAEE_6733_46AA_772C_43A51B2BAC10
26 #define INCLUDED_StateHistory_h_GUID_07D9DAEE_6733_46AA_772C_43A51B2BAC10
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <osvr/Util/TimeValue.h>
34 
35 // Standard includes
36 #include <array>
37 #include <vector>
38 #include <type_traits>
39 
40 namespace osvr {
41 namespace kalman {
42  namespace pose_externalized_rotation {
43  // forward declaration
44  class State;
45  } // namespace pose_externalized_rotation
46  namespace orient_externalized_rotation {
47  // forward declaration
48  class State;
49  }
50 } // namespace kalman
51 } // namespace osvr
52 
53 namespace osvr {
54 namespace vbtracker {
55 
56  namespace detail {
59  template <typename StateType>
60  struct StateHasExternalQuaternion : std::false_type {};
61  template <>
63  kalman::pose_externalized_rotation::State> : std::true_type {};
64  template <>
66  kalman::orient_externalized_rotation::State> : std::true_type {};
67 
70  template <typename State> class StateHistoryEntryBase {
71  using StateVec = kalman::types::DimVector<State>;
72  using StateMatrix = kalman::types::DimSquareMatrix<State>;
73 
74  // Using std::array here so that we can easily stick this in a
75  // vector
76  // without worrying about alignment.
77  using StateDim = kalman::types::Dimension<State>;
78  using StateVectorBackup =
79  std::array<kalman::types::Scalar, StateDim::value>;
80  using StateCovarianceBackup =
81  std::array<kalman::types::Scalar,
82  StateDim::value * StateDim::value>;
83 
84  public:
86  explicit StateHistoryEntryBase(State const &state) {
87  StateVec::Map(m_stateVector.data()) = state.stateVector();
88  StateMatrix::Map(m_covariance.data()) = state.errorCovariance();
89  }
90 
91  void restore(State &state) const {
92  state.setStateVector(StateVec::Map(m_stateVector.data()));
93  state.setErrorCovariance(StateMatrix::Map(m_covariance.data()));
94  }
95 
96  private:
97  util::time::TimeValue m_timestamp;
98  StateVectorBackup m_stateVector;
99  StateCovarianceBackup m_covariance;
100  };
101  struct NoExternalState;
102  struct HasExternalQuaternion;
104  template <typename State>
105  using StateHistoryEntryTagSelector = typename std::conditional<
106  detail::StateHasExternalQuaternion<State>::value,
107  HasExternalQuaternion, NoExternalState>::type;
108  } // namespace detail
109 
114  template <typename State,
115  typename Tag = detail::StateHistoryEntryTagSelector<State>>
118 
119  public:
120  explicit StateHistoryEntry(State const &state) : Base(state) {}
121  };
122 
123  template <typename State>
124  class StateHistoryEntry<State, detail::HasExternalQuaternion> {
126 
127  public:
128  explicit StateHistoryEntry(State const &state) : m_baseEntry(state) {
130  Eigen::Vector4d::Map(m_quatBackup.data()) =
131  state.getQuaternion().coeffs();
132  }
133 
134  void restore(State &state) const {
135  m_baseEntry.restore(state);
137  Eigen::Quaterniond quat;
138  quat.coeffs() = Eigen::Vector4d::Map(m_quatBackup.data());
139  state.setQuaternion(quat);
140  }
141 
142  private:
143  BaseEntry m_baseEntry;
144  std::array<kalman::types::Scalar, 4> m_quatBackup;
145  };
146 } // namespace vbtracker
147 } // namespace osvr
148 
149 #endif // INCLUDED_StateHistory_h_GUID_07D9DAEE_6733_46AA_772C_43A51B2BAC10
typename detail::Dimension_impl< T >::type Dimension
Vector< Dimension< T >::value > DimVector
A vector of length = dimension of T.
StateHistoryEntryBase(State const &state)
Constructor - saves the state vector and error covariance.
Definition: StateHistory.h:86
SquareMatrix< Dimension< T >::value > DimSquareMatrix
A square matrix, n x n, where n is the dimension of T.
Header providing a C++ wrapper around TimeValueC.h.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
double Scalar
Common scalar type.