33 #include <Eigen/Dense>
39 #ifdef FUSION_MAX_VECTOR_SIZE
40 #undef FUSION_MAX_VECTOR_SIZE
42 #define FUSION_MAX_VECTOR_SIZE 15 // Maximum number of statevariables
44 #include <boost/lexical_cast.hpp>
45 #include <boost/preprocessor/punctuation/comma.hpp>
46 #include <boost/fusion/include/vector.hpp>
47 #include <boost/fusion/include/size.hpp>
48 #include <boost/fusion/include/at_c.hpp>
49 #include <boost/fusion/include/at.hpp>
50 #include <boost/fusion/include/for_each.hpp>
51 #include <boost/fusion/include/equal_to.hpp>
52 #include <boost/fusion/include/begin.hpp>
53 #include <boost/fusion/include/end.hpp>
54 #include <boost/fusion/include/next.hpp>
55 #include <boost/mpl/bool.hpp>
56 #include <boost/fusion/include/at_key.hpp>
84 template<
typename T>
struct echoStateVarType;
89 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
90 struct echoStateVarType<
91 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME, STATE_T,
94 return "const ref Eigen::Matrix<double, " + boost::lexical_cast
95 < std::string > (N) +
", 1>";
101 template<
int NAME,
int STATE_T,
int OPTIONS>
102 struct echoStateVarType<
103 const msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS>&> {
105 return "const ref Eigen::Quaterniond";
111 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
112 struct echoStateVarType<
113 msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME, STATE_T, OPTIONS> > {
115 return "Eigen::Matrix<double, " + boost::lexical_cast < std::string
122 template<
int NAME,
int STATE_T,
int OPTIONS>
123 struct echoStateVarType<
124 msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS> > {
126 return "Eigen::Quaterniond";
133 struct CorrectionStateLengthForType;
134 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
135 struct CorrectionStateLengthForType<
136 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME, STATE_T,
142 template<
int NAME,
int STATE_T,
int OPTIONS>
143 struct CorrectionStateLengthForType<
144 const msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS>&> {
150 struct CorrectionStateLengthForType<const mpl_::void_&> {
158 struct StateLengthForType;
159 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
160 struct StateLengthForType<
161 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME, STATE_T,
167 template<
int NAME,
int STATE_T,
int OPTIONS>
168 struct StateLengthForType<
169 const msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS>&> {
175 struct StateLengthForType<const mpl_::void_&> {
183 struct CoreStateLengthForType;
184 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
185 struct CoreStateLengthForType<
186 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME, STATE_T,
193 template<
int NAME,
int STATE_T,
int OPTIONS>
194 struct CoreStateLengthForType<
195 const msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS>&> {
201 template<
int NAME,
int OPTIONS,
int N>
202 struct CoreStateLengthForType<
203 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME,
204 msf_core::CoreStateWithoutPropagation, OPTIONS>&> {
209 template<
int NAME,
int OPTIONS>
210 struct CoreStateLengthForType<
211 const msf_core::StateVar_T<Eigen::Quaterniond, NAME,
212 msf_core::CoreStateWithoutPropagation, OPTIONS>&> {
217 template<
int NAME,
int OPTIONS,
int N>
218 struct CoreStateLengthForType<
219 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME,
220 msf_core::CoreStateWithPropagation, OPTIONS>&> {
225 template<
int NAME,
int OPTIONS>
226 struct CoreStateLengthForType<
227 const msf_core::StateVar_T<Eigen::Quaterniond, NAME,
228 msf_core::CoreStateWithPropagation, OPTIONS>&> {
234 struct CoreStateLengthForType<const mpl_::void_&> {
243 struct PropagatedCoreStateLengthForType;
244 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
245 struct PropagatedCoreStateLengthForType<
246 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME, STATE_T,
253 template<
int NAME,
int STATE_T,
int OPTIONS>
254 struct PropagatedCoreStateLengthForType<
255 const msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS>&> {
261 template<
int NAME,
int OPTIONS,
int N>
262 struct PropagatedCoreStateLengthForType<
263 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME,
264 msf_core::CoreStateWithPropagation, OPTIONS>&> {
269 template<
int NAME,
int OPTIONS>
270 struct PropagatedCoreStateLengthForType<
271 const msf_core::StateVar_T<Eigen::Quaterniond, NAME,
272 msf_core::CoreStateWithPropagation, OPTIONS>&> {
278 struct PropagatedCoreStateLengthForType<const mpl_::void_&> {
287 struct PropagatedCoreErrorStateLengthForType;
288 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
289 struct PropagatedCoreErrorStateLengthForType<
290 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME, STATE_T,
297 template<
int NAME,
int STATE_T,
int OPTIONS>
298 struct PropagatedCoreErrorStateLengthForType<
299 const msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS>&> {
305 template<
int NAME,
int OPTIONS,
int N>
306 struct PropagatedCoreErrorStateLengthForType<
307 const msf_core::StateVar_T<Eigen::Matrix<double, N, 1>, NAME,
308 msf_core::CoreStateWithPropagation, OPTIONS>&> {
313 template<
int NAME,
int OPTIONS>
314 struct PropagatedCoreErrorStateLengthForType<
315 const msf_core::StateVar_T<Eigen::Quaterniond, NAME,
316 msf_core::CoreStateWithPropagation, OPTIONS>&> {
322 struct PropagatedCoreErrorStateLengthForType<const mpl_::void_&> {
330 struct getEnumStateName;
331 template<
typename U,
int NAME,
int STATE_T,
int OPTIONS>
332 struct getEnumStateName<const msf_core::StateVar_T<U, NAME, STATE_T, OPTIONS>&> {
337 template<
typename U,
int NAME,
int STATE_T,
int OPTIONS>
338 struct getEnumStateName<msf_core::StateVar_T<U, NAME, STATE_T, OPTIONS> > {
343 template<>
struct getEnumStateName<const mpl_::void_&> {
349 template<>
struct getEnumStateName<mpl_::void_> {
357 struct isQuaternionType;
358 template<
int NAME,
int STATE_T,
int OPTIONS,
int M,
int N>
359 struct isQuaternionType<
360 const msf_core::StateVar_T<Eigen::Matrix<double, M, N>, NAME, STATE_T,
366 template<
int NAME,
int STATE_T,
int OPTIONS,
int M,
int N>
367 struct isQuaternionType<
368 msf_core::StateVar_T<Eigen::Matrix<double, M, N>, NAME, STATE_T, OPTIONS> > {
373 template<
int NAME,
int STATE_T,
int OPTIONS>
374 struct isQuaternionType<
375 const msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS>&> {
380 template<
int NAME,
int STATE_T,
int OPTIONS>
381 struct isQuaternionType<
382 msf_core::StateVar_T<Eigen::Quaterniond, NAME, STATE_T, OPTIONS> > {
388 struct isQuaternionType<const mpl_::void_&> {
394 struct isQuaternionType<mpl_::void_> {
404 struct getStateIsNonTemporalDrifting;
405 template<
typename U,
int NAME,
int STATE_T,
int OPTIONS>
406 struct getStateIsNonTemporalDrifting<
407 const msf_core::StateVar_T<U, NAME, STATE_T, OPTIONS>&> {
413 template<
typename U,
int NAME,
int STATE_T,
int OPTIONS>
414 struct getStateIsNonTemporalDrifting<
415 msf_core::StateVar_T<U, NAME, STATE_T, OPTIONS> > {
421 template<>
struct getStateIsNonTemporalDrifting<const mpl_::void_&> {
426 template<>
struct getStateIsNonTemporalDrifting<mpl_::void_> {
436 template<
typename Sequence,
template<
typename >
class Counter,
typename First,
437 typename Last,
bool T>
438 struct countStatesLinear;
439 template<
typename Sequence,
template<
typename >
class Counter,
typename First,
441 struct countStatesLinear<Sequence, Counter, First, Last, true> {
446 template<
typename Sequence,
template<
typename >
class Counter,
typename First,
448 struct countStatesLinear<Sequence, Counter, First, Last, false> {
449 typedef typename boost::fusion::result_of::next<First>::type Next;
450 typedef typename boost::fusion::result_of::deref<First>::type current_Type;
452 value = Counter<current_Type>::value
453 + countStatesLinear<Sequence, Counter, Next, Last,
454 SameType<typename msf_tmp::StripConstReference<First>::result_t,
462 template<
typename Sequence,
typename First,
typename Last,
int CurrentIdx,
464 struct CheckStateIndexing;
465 template<
typename Sequence,
typename First,
typename Last,
int CurrentIdx>
466 struct CheckStateIndexing<Sequence, First, Last, CurrentIdx, true> {
471 template<
typename Sequence,
typename First,
typename Last,
int CurrentIdx>
472 struct CheckStateIndexing<Sequence, First, Last, CurrentIdx, false> {
473 typedef typename boost::fusion::result_of::next<First>::type Next;
474 typedef typename boost::fusion::result_of::deref<First>::type current_Type;
477 idxInEnum = boost::mpl::if_c<getEnumStateName<current_Type>::value != -1,
479 boost::mpl::int_<getEnumStateName<current_Type>::value>,
481 boost::mpl::int_<CurrentIdx> >::type::value,
483 idxInState = CurrentIdx,
485 indexingerrors = boost::mpl::if_c<idxInEnum == idxInState,
487 boost::mpl::int_<0>, boost::mpl::int_<1> >::type::value +
489 CheckStateIndexing<Sequence, Next, Last, CurrentIdx + 1,
490 SameType<typename msf_tmp::StripConstReference<First>::result_t,
497 static_assert(indexingerrors==0,
"Error the ordering in the enum defining the names _ "
498 "of the states is not the same ordering as in the type vector for the states");
505 template<
typename TypeList,
int INDEX>
506 struct getEnumStateType {
507 typedef typename boost::fusion::result_of::at_c<TypeList, INDEX>::type value;
513 template<
typename TypeList>
514 struct getEnumStateType<TypeList, -1> {
515 typedef mpl_::void_ value;
521 template<
typename Sequence,
typename StateVarT,
522 template<
typename >
class OffsetCalculator,
typename First,
typename Last,
523 bool TypeFound,
int CurrentVal,
bool EndOfList>
524 struct ComputeStartIndex;
525 template<
typename Sequence,
typename StateVarT,
526 template<
typename >
class OffsetCalculator,
typename First,
typename Last,
528 struct ComputeStartIndex<Sequence, StateVarT, OffsetCalculator, First, Last,
529 false, CurrentVal, true> {
534 template<
typename Sequence,
typename StateVarT,
535 template<
typename >
class OffsetCalculator,
typename First,
typename Last,
536 bool TypeFound,
int CurrentVal>
537 struct ComputeStartIndex<Sequence, StateVarT, OffsetCalculator, First, Last,
538 TypeFound, CurrentVal, false> {
543 template<
typename Sequence,
typename StateVarT,
544 template<
typename >
class OffsetCalculator,
typename First,
typename Last,
546 struct ComputeStartIndex<Sequence, StateVarT, OffsetCalculator, First, Last,
547 false, CurrentVal, false> {
548 typedef typename boost::fusion::result_of::next<First>::type Next;
549 typedef typename boost::fusion::result_of::deref<First>::type currentType;
553 + ComputeStartIndex<Sequence, StateVarT, OffsetCalculator, Next, Last,
557 OffsetCalculator<currentType>::value,
558 SameType<typename msf_tmp::StripConstReference<First>::result_t,
567 template<
typename Sequence,
typename First,
typename Last,
int CurrentBestIdx,
568 bool foundQuaternion,
bool EndOfList>
569 struct FindBestNonTemporalDriftingStateImpl;
570 template<
typename Sequence,
typename First,
typename Last,
int CurrentBestIdx,
571 bool foundQuaternion>
572 struct FindBestNonTemporalDriftingStateImpl<Sequence, First, Last,
573 CurrentBestIdx, foundQuaternion, true> {
575 value = CurrentBestIdx
578 template<
typename Sequence,
typename First,
typename Last,
int CurrentBestIdx,
579 bool foundQuaternion>
580 struct FindBestNonTemporalDriftingStateImpl<Sequence, First, Last,
581 CurrentBestIdx, foundQuaternion, false> {
582 typedef typename boost::fusion::result_of::next<First>::type Next;
583 typedef typename boost::fusion::result_of::deref<First>::type current_Type;
585 idxInState = getEnumStateName<
588 IsCurrentStateAQuaternion = isQuaternionType<
590 IsCurrentStateNonTemporalDrifting = getStateIsNonTemporalDrifting<
592 isQuaternionAndNonTemporalDrifting = IsCurrentStateAQuaternion ==
true
593 && IsCurrentStateNonTemporalDrifting ==
true,
594 isNonTemporalDriftingPositionAndNoQuaternionHasBeenFound = IsCurrentStateNonTemporalDrifting
595 ==
true && foundQuaternion ==
false,
597 currBestIdx = boost::mpl::if_c<isQuaternionAndNonTemporalDrifting,
599 boost::mpl::int_<idxInState>,
602 isNonTemporalDriftingPositionAndNoQuaternionHasBeenFound,
605 boost::mpl::int_<idxInState>,
607 boost::mpl::int_<CurrentBestIdx> >::type::value> >::type::value,
609 value = FindBestNonTemporalDriftingStateImpl<Sequence, Next, Last,
610 currBestIdx, foundQuaternion || isQuaternionAndNonTemporalDrifting,
611 SameType<typename msf_tmp::StripConstReference<First>::result_t,
623 template<
typename Sequence>
625 typedef typename boost::fusion::result_of::begin<Sequence const>::type
First;
626 typedef typename boost::fusion::result_of::end<Sequence const>::type
Last;
649 template<
typename Sequence>
651 typedef typename boost::fusion::result_of::begin<Sequence const>::type
First;
652 typedef typename boost::fusion::result_of::end<Sequence const>::type
Last;
658 "indexing of the state vector is not the same as in the enum,"
659 " but this must be the same");
673 template<
typename Sequence,
template<
typename >
class Counter>
675 typedef typename boost::fusion::result_of::begin<Sequence const>::type
First;
676 typedef typename boost::fusion::result_of::end<Sequence const>::type
Last;
689 template<
typename Sequence,
typename StateVarT,
690 template<
typename >
class Counter>
692 typedef typename boost::fusion::result_of::begin<Sequence const>::type
First;
693 typedef typename boost::fusion::result_of::end<Sequence const>::type
Last;
694 typedef typename boost::fusion::result_of::deref<First>::type
currentType;
708 template<
typename Sequence,
int StateEnum>
710 typedef typename boost::fusion::result_of::begin<Sequence const>::type
First;
711 typedef typename boost::fusion::result_of::end<Sequence const>::type
Last;
712 typedef typename boost::fusion::result_of::deref<First>::type
currentType;
715 typename msf_tmp::getEnumStateType<Sequence, StateEnum>::value,
716 msf_tmp::CorrectionStateLengthForType>
::value
724 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
731 template<
int NAME,
int STATE_T,
int OPTIONS>
743 template<
typename stateVarT>
748 template<
typename T,
int NAME,
int STATE_T,
int OPTIONS>
750 if (
oldstate_.template getStateVar<NAME>().hasResetValue) {
752 t =
oldstate_.template getStateVar<NAME>();
764 template<
typename stateVarT>
769 template<
typename T,
int NAME,
int OPTIONS>
774 template<
typename T,
int NAME,
int STATE_T,
int OPTIONS>
779 NAME, STATE_T> >::value ==
false),
780 "The state variable is of pointer type, but this method assumes we can "
781 "copy without dereferencing");
785 "copyNonPropagationStates was instantiated for a core state. "
786 "This is an error.");
788 t =
oldstate_.template getStateVar<NAME>();
798 template<
typename stateList_T>
802 msf_tmp::CorrectionStateLengthForType>::value
809 template<
typename T,
int NAME,
int STATE_T,
int OPTIONS>
814 msf_tmp::CorrectionStateLengthForType>::value,
815 sizeInCorrection = var_T::sizeInCorrection_
819 static_cast<int>(var_T::statetype_) !=
821 static_cast<int>(var_T::statetype_) !=
823 "copyQBlocksFromAuxiliaryStatesToQ was instantiated for a core state. "
824 "This is an error.");
827 static_cast<int>(sizeInCorrection) ==
828 static_cast<int>(var_T::Q_T::ColsAtCompileTime) &&
829 static_cast<int>(sizeInCorrection) ==
830 static_cast<int>(var_T::Q_T::RowsAtCompileTime),
831 "copyQBlocksFromAuxiliaryStatesToQ size of Matrix Q stored with the stateVar,"
832 "is not the same as the reported dimension in the correction vector");
834 Q_.template block<sizeInCorrection, sizeInCorrection>(startIdxInCorrection,
835 startIdxInCorrection) =
838 template<
typename T,
int NAME,
int OPTIONS>
844 template<
typename T,
int NAME,
int OPTIONS>
858 template<
typename T,
typename stateList_T>
861 :
data_(correction) {
863 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
870 msf_tmp::CorrectionStateLengthForType>::value
873 t.state_ = t.state_.cwiseProduct(
874 data_.template block<var_T::sizeInCorrection_, 1>(
875 startIdxInCorrection, 0));
878 +
data_.template block<var_T::sizeInCorrection_, 1>(
879 startIdxInCorrection, 0);
882 template<
int NAME,
int STATE_T,
int OPTIONS>
888 msf_tmp::CorrectionStateLengthForType>::value
893 "You defined the Quaternion correction to be multiplicative, but this "
894 "is anyway done and not an option");
897 data_.template block<var_T::sizeInCorrection_, 1>(startIdxInCorrection,
909 template<
typename T,
typename stateList_T>
912 :
data_(statearray) {
914 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
922 msf_tmp::StateLengthForType>::value
925 static_cast<int>(var_T::statetype_) ==
927 static_cast<int>(var_T::statetype_) ==
929 "CoreStatetoDoubleArray: A new statetype was defined, but I don't know "
930 "whether this should be written to the double array for core states");
931 for (
int i = 0; i < var_T::sizeInState_; ++i) {
932 data_[startIdxInState + i] = t.state_[i];
935 template<
int NAME,
int STATE_T,
int OPTIONS>
942 msf_tmp::StateLengthForType>::value
945 static_cast<int>(var_T::statetype_) ==
947 static_cast<int>(var_T::statetype_) ==
949 "CoreStatetoDoubleArray: A new statetype was defined, but I don't know "
950 "whether this should be written to the double array for core states");
957 template<
int NAME,
int OPTIONS,
int N>
963 template<
int NAME,
int OPTIONS>
970 template<
int NAME,
int OPTIONS,
int N>
976 template<
int NAME,
int OPTIONS>
990 template<
typename T,
typename stateList_T>
995 template<
typename VALUE_T,
int NAME,
int STATE_T,
int OPTIONS>
1002 msf_tmp::CorrectionStateLengthForType>::value,
1003 lengthInState = var_T::sizeInCorrection_
1006 typename T::value_type(NAME, startIdxInState, lengthInState));
1016 template<
typename stateVector_T,
int INDEX>
1018 typedef typename msf_tmp::getEnumStateType<stateVector_T, INDEX>::value
state_type;
1021 msf_tmp::StateLengthForType>
::value,
1029 template<
typename stateVector_T,
int INDEX>
1031 typedef typename msf_tmp::getEnumStateType<stateVector_T, INDEX>::value
state_type;
1034 msf_tmp::CorrectionStateLengthForType>
::value,
1041 template<
typename STREAM,
typename stateList_T>
1046 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
1054 msf_tmp::StateLengthForType>::value
1056 data_ << NAME <<
" : [" << startIdxInState <<
"-" << startIdxInState + N - 1
1057 <<
"]\t : Matrix<" << N <<
", 1> : [" << t.state_.transpose()
1058 <<
"]" << std::endl;
1060 template<
int NAME,
int STATE_T,
int OPTIONS>
1067 msf_tmp::StateLengthForType>::value
1069 data_ << NAME <<
" : [" << startIdxInState <<
"-" << startIdxInState + 3
1080 template<
typename T,
typename stateList_T>
1083 :
data_(statearray) {
1085 template<
int NAME,
int N,
int STATE_T,
int OPTIONS>
1093 msf_tmp::StateLengthForType>::value
1095 for (
int i = 0; i < var_T::sizeInState_; ++i) {
1096 data_[startIdxInState + i] = t.state_[i];
1099 template<
int NAME,
int STATE_T,
int OPTIONS>
1106 msf_tmp::StateLengthForType>::value
1119 #endif // MSF_TMP_H_