OSVR-Core
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 {
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<
107  HasExternalQuaternion, NoExternalState>::type;
108  } // namespace detail
109 
114  template <typename 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
Given a state or measurement, get the dimension as a std::integral_constant.
Definition: FlexibleKalmanBase.h:87
const Coefficients & coeffs() const
Definition: Quaternion.h:93
void setQuaternion(Eigen::Quaterniond const &quaternion)
Intended for startup use.
Definition: PoseState.h:189
void restore(State &state) const
Definition: StateHistory.h:134
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
StateHistoryEntryBase(State const &state)
Constructor - saves the state vector and error covariance.
Definition: StateHistory.h:86
Does the specified state type also have an "external" quaternion we should save and restore...
Definition: StateHistory.h:60
Definition: newuoa.h:1888
StateHistoryEntry(State const &state)
Definition: StateHistory.h:128
StateVector const & stateVector() const
xhat
Definition: PoseState.h:177
State history entry template - default implementation is for those that don&#39;t have added extras...
Definition: StateHistory.h:116
Base state history entry - handles standard states with everything in the state vector and error cova...
Definition: StateHistory.h:70
Header providing a C++ wrapper around TimeValueC.h.
StateSquareMatrix const & errorCovariance() const
P.
Definition: PoseState.h:183
Definition: Quaternion.h:47
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
void setStateVector(StateVector const &state)
set xhat
Definition: PoseState.h:175
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48
typename std::conditional< detail::StateHasExternalQuaternion< State >::value, HasExternalQuaternion, NoExternalState >::type StateHistoryEntryTagSelector
Select the tag for tag dispatching.
Definition: StateHistory.h:107