startIdx.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // This is an advanced implementation of the algorithm described in the
  2. // following paper:
  3. // C. Hertzberg, R. Wagner, U. Frese, and L. Schroder. Integratinggeneric sensor fusion algorithms with sound state representationsthrough encapsulation of manifolds.
  4. // CoRR, vol. abs/1107.1119, 2011.[Online]. Available: http://arxiv.org/abs/1107.1119
  5. /*
  6. * Copyright (c) 2019--2023, The University of Hong Kong
  7. * All rights reserved.
  8. *
  9. * Modifier: Dongjiao HE <hdj65822@connect.hku.hk>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * * Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * * Redistributions in binary form must reproduce the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer in the documentation and/or other materials provided
  20. * with the distribution.
  21. * * Neither the name of the Universitaet Bremen nor the names of its
  22. * contributors may be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /*
  39. * Copyright (c) 2008--2011, Universitaet Bremen
  40. * All rights reserved.
  41. *
  42. * Author: Christoph Hertzberg <chtz@informatik.uni-bremen.de>
  43. *
  44. * Redistribution and use in source and binary forms, with or without
  45. * modification, are permitted provided that the following conditions
  46. * are met:
  47. *
  48. * * Redistributions of source code must retain the above copyright
  49. * notice, this list of conditions and the following disclaimer.
  50. * * Redistributions in binary form must reproduce the above
  51. * copyright notice, this list of conditions and the following
  52. * disclaimer in the documentation and/or other materials provided
  53. * with the distribution.
  54. * * Neither the name of the Universitaet Bremen nor the names of its
  55. * contributors may be used to endorse or promote products derived
  56. * from this software without specific prior written permission.
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  59. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  60. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  61. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  62. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  63. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  64. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  65. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  66. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  67. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  68. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  69. * POSSIBILITY OF SUCH DAMAGE.
  70. */
  71. /**
  72. * @file mtk/startIdx.hpp
  73. * @brief Tools to access sub-elements of compound manifolds.
  74. */
  75. #ifndef GET_START_INDEX_H_
  76. #define GET_START_INDEX_H_
  77. #include <Eigen/Core>
  78. #include "src/SubManifold.hpp"
  79. #include "src/vectview.hpp"
  80. namespace MTK {
  81. /**
  82. * \defgroup SubManifolds Accessing Submanifolds
  83. * For compound manifolds constructed using MTK_BUILD_MANIFOLD, member pointers
  84. * can be used to get sub-vectors or matrix-blocks of a corresponding big matrix.
  85. * E.g. for a type @a pose consisting of @a orient and @a trans the member pointers
  86. * @c &pose::orient and @c &pose::trans give all required information and are still
  87. * valid if the base type gets extended or the actual types of @a orient and @a trans
  88. * change (e.g. from 2D to 3D).
  89. *
  90. * @todo Maybe require manifolds to typedef MatrixType and VectorType, etc.
  91. */
  92. //@{
  93. /**
  94. * Determine the index of a sub-variable within a compound variable.
  95. */
  96. template<class Base, class T, int idx, int dim>
  97. int getStartIdx( MTK::SubManifold<T, idx, dim> Base::*)
  98. {
  99. return idx;
  100. }
  101. template<class Base, class T, int idx, int dim>
  102. int getStartIdx_( MTK::SubManifold<T, idx, dim> Base::*)
  103. {
  104. return dim;
  105. }
  106. /**
  107. * Determine the degrees of freedom of a sub-variable within a compound variable.
  108. */
  109. template<class Base, class T, int idx, int dim>
  110. int getDof( MTK::SubManifold<T, idx, dim> Base::*)
  111. {
  112. return T::DOF;
  113. }
  114. template<class Base, class T, int idx, int dim>
  115. int getDim( MTK::SubManifold<T, idx, dim> Base::*)
  116. {
  117. return T::DIM;
  118. }
  119. /**
  120. * set the diagonal elements of a covariance matrix corresponding to a sub-variable
  121. */
  122. template<class Base, class T, int idx, int dim>
  123. void setDiagonal(Eigen::Matrix<typename Base::scalar, Base::DOF, Base::DOF> &cov,
  124. MTK::SubManifold<T, idx, dim> Base::*, const typename Base::scalar &val)
  125. {
  126. cov.diagonal().template segment<T::DOF>(idx).setConstant(val);
  127. }
  128. template<class Base, class T, int idx, int dim>
  129. void setDiagonal_(Eigen::Matrix<typename Base::scalar, Base::DIM, Base::DIM> &cov,
  130. MTK::SubManifold<T, idx, dim> Base::*, const typename Base::scalar &val)
  131. {
  132. cov.diagonal().template segment<T::DIM>(dim).setConstant(val);
  133. }
  134. /**
  135. * Get the subblock of corresponding to two members, i.e.
  136. * \code
  137. * Eigen::Matrix<double, Pose::DOF, Pose::DOF> m;
  138. * MTK::subblock(m, &Pose::orient, &Pose::trans) = some_expression;
  139. * MTK::subblock(m, &Pose::trans, &Pose::orient) = some_expression.trans();
  140. * \endcode
  141. * lets you modify mixed covariance entries in a bigger covariance matrix.
  142. */
  143. template<class Base, class T1, int idx1, int dim1, class T2, int idx2, int dim2>
  144. typename MTK::internal::CovBlock<Base, T1, T2>::Type
  145. subblock(Eigen::Matrix<typename Base::scalar, Base::DOF, Base::DOF> &cov,
  146. MTK::SubManifold<T1, idx1, dim1> Base::*, MTK::SubManifold<T2, idx2, dim2> Base::*)
  147. {
  148. return cov.template block<T1::DOF, T2::DOF>(idx1, idx2);
  149. }
  150. template<class Base, class T1, int idx1, int dim1, class T2, int idx2, int dim2>
  151. typename MTK::internal::CovBlock_<Base, T1, T2>::Type
  152. subblock_(Eigen::Matrix<typename Base::scalar, Base::DIM, Base::DIM> &cov,
  153. MTK::SubManifold<T1, idx1, dim1> Base::*, MTK::SubManifold<T2, idx2, dim2> Base::*)
  154. {
  155. return cov.template block<T1::DIM, T2::DIM>(dim1, dim2);
  156. }
  157. template<typename Base1, typename Base2, typename T1, typename T2, int idx1, int idx2, int dim1, int dim2>
  158. typename MTK::internal::CrossCovBlock<Base1, Base2, T1, T2>::Type
  159. subblock(Eigen::Matrix<typename Base1::scalar, Base1::DOF, Base2::DOF> &cov, MTK::SubManifold<T1, idx1, dim1> Base1::*, MTK::SubManifold<T2, idx2, dim2> Base2::*)
  160. {
  161. return cov.template block<T1::DOF, T2::DOF>(idx1, idx2);
  162. }
  163. template<typename Base1, typename Base2, typename T1, typename T2, int idx1, int idx2, int dim1, int dim2>
  164. typename MTK::internal::CrossCovBlock_<Base1, Base2, T1, T2>::Type
  165. subblock_(Eigen::Matrix<typename Base1::scalar, Base1::DIM, Base2::DIM> &cov, MTK::SubManifold<T1, idx1, dim1> Base1::*, MTK::SubManifold<T2, idx2, dim2> Base2::*)
  166. {
  167. return cov.template block<T1::DIM, T2::DIM>(dim1, dim2);
  168. }
  169. /**
  170. * Get the subblock of corresponding to a member, i.e.
  171. * \code
  172. * Eigen::Matrix<double, Pose::DOF, Pose::DOF> m;
  173. * MTK::subblock(m, &Pose::orient) = some_expression;
  174. * \endcode
  175. * lets you modify covariance entries in a bigger covariance matrix.
  176. */
  177. template<class Base, class T, int idx, int dim>
  178. typename MTK::internal::CovBlock_<Base, T, T>::Type
  179. subblock_(Eigen::Matrix<typename Base::scalar, Base::DIM, Base::DIM> &cov,
  180. MTK::SubManifold<T, idx, dim> Base::*)
  181. {
  182. return cov.template block<T::DIM, T::DIM>(dim, dim);
  183. }
  184. template<class Base, class T, int idx, int dim>
  185. typename MTK::internal::CovBlock<Base, T, T>::Type
  186. subblock(Eigen::Matrix<typename Base::scalar, Base::DOF, Base::DOF> &cov,
  187. MTK::SubManifold<T, idx, dim> Base::*)
  188. {
  189. return cov.template block<T::DOF, T::DOF>(idx, idx);
  190. }
  191. template<typename Base>
  192. class get_cov {
  193. public:
  194. typedef Eigen::Matrix<typename Base::scalar, Base::DOF, Base::DOF> type;
  195. typedef const Eigen::Matrix<typename Base::scalar, Base::DOF, Base::DOF> const_type;
  196. };
  197. template<typename Base>
  198. class get_cov_ {
  199. public:
  200. typedef Eigen::Matrix<typename Base::scalar, Base::DIM, Base::DIM> type;
  201. typedef const Eigen::Matrix<typename Base::scalar, Base::DIM, Base::DIM> const_type;
  202. };
  203. template<typename Base1, typename Base2>
  204. class get_cross_cov {
  205. public:
  206. typedef Eigen::Matrix<typename Base1::scalar, Base1::DOF, Base2::DOF> type;
  207. typedef const type const_type;
  208. };
  209. template<typename Base1, typename Base2>
  210. class get_cross_cov_ {
  211. public:
  212. typedef Eigen::Matrix<typename Base1::scalar, Base1::DIM, Base2::DIM> type;
  213. typedef const type const_type;
  214. };
  215. template<class Base, class T, int idx, int dim>
  216. vectview<typename Base::scalar, T::DIM>
  217. subvector_impl_(vectview<typename Base::scalar, Base::DIM> vec, SubManifold<T, idx, dim> Base::*)
  218. {
  219. return vec.template segment<T::DIM>(dim);
  220. }
  221. template<class Base, class T, int idx, int dim>
  222. vectview<typename Base::scalar, T::DOF>
  223. subvector_impl(vectview<typename Base::scalar, Base::DOF> vec, SubManifold<T, idx, dim> Base::*)
  224. {
  225. return vec.template segment<T::DOF>(idx);
  226. }
  227. /**
  228. * Get the subvector corresponding to a sub-manifold from a bigger vector.
  229. */
  230. template<class Scalar, int BaseDIM, class Base, class T, int idx, int dim>
  231. vectview<Scalar, T::DIM>
  232. subvector_(vectview<Scalar, BaseDIM> vec, SubManifold<T, idx, dim> Base::* ptr)
  233. {
  234. return subvector_impl_(vec, ptr);
  235. }
  236. template<class Scalar, int BaseDOF, class Base, class T, int idx, int dim>
  237. vectview<Scalar, T::DOF>
  238. subvector(vectview<Scalar, BaseDOF> vec, SubManifold<T, idx, dim> Base::* ptr)
  239. {
  240. return subvector_impl(vec, ptr);
  241. }
  242. /**
  243. * @todo This should be covered already by subvector(vectview<typename Base::scalar,Base::DOF> vec,SubManifold<T,idx> Base::*)
  244. */
  245. template<class Scalar, int BaseDOF, class Base, class T, int idx, int dim>
  246. vectview<Scalar, T::DOF>
  247. subvector(Eigen::Matrix<Scalar, BaseDOF, 1>& vec, SubManifold<T, idx, dim> Base::* ptr)
  248. {
  249. return subvector_impl(vectview<Scalar, BaseDOF>(vec), ptr);
  250. }
  251. template<class Scalar, int BaseDIM, class Base, class T, int idx, int dim>
  252. vectview<Scalar, T::DIM>
  253. subvector_(Eigen::Matrix<Scalar, BaseDIM, 1>& vec, SubManifold<T, idx, dim> Base::* ptr)
  254. {
  255. return subvector_impl_(vectview<Scalar, BaseDIM>(vec), ptr);
  256. }
  257. template<class Scalar, int BaseDIM, class Base, class T, int idx, int dim>
  258. vectview<const Scalar, T::DIM>
  259. subvector_(const Eigen::Matrix<Scalar, BaseDIM, 1>& vec, SubManifold<T, idx, dim> Base::* ptr)
  260. {
  261. return subvector_impl_(vectview<const Scalar, BaseDIM>(vec), ptr);
  262. }
  263. template<class Scalar, int BaseDOF, class Base, class T, int idx, int dim>
  264. vectview<const Scalar, T::DOF>
  265. subvector(const Eigen::Matrix<Scalar, BaseDOF, 1>& vec, SubManifold<T, idx, dim> Base::* ptr)
  266. {
  267. return subvector_impl(vectview<const Scalar, BaseDOF>(vec), ptr);
  268. }
  269. /**
  270. * const version of subvector(vectview<typename Base::scalar,Base::DOF> vec,SubManifold<T,idx> Base::*)
  271. */
  272. template<class Base, class T, int idx, int dim>
  273. vectview<const typename Base::scalar, T::DOF>
  274. subvector_impl(const vectview<const typename Base::scalar, Base::DOF> cvec, SubManifold<T, idx, dim> Base::*)
  275. {
  276. return cvec.template segment<T::DOF>(idx);
  277. }
  278. template<class Base, class T, int idx, int dim>
  279. vectview<const typename Base::scalar, T::DIM>
  280. subvector_impl_(const vectview<const typename Base::scalar, Base::DIM> cvec, SubManifold<T, idx, dim> Base::*)
  281. {
  282. return cvec.template segment<T::DIM>(dim);
  283. }
  284. template<class Scalar, int BaseDOF, class Base, class T, int idx, int dim>
  285. vectview<const Scalar, T::DOF>
  286. subvector(const vectview<const Scalar, BaseDOF> cvec, SubManifold<T, idx, dim> Base::* ptr)
  287. {
  288. return subvector_impl(cvec, ptr);
  289. }
  290. } // namespace MTK
  291. #endif // GET_START_INDEX_H_