eigen.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  16. // Third party copyrights are property of their respective owners.
  17. //
  18. // Redistribution and use in source and binary forms, with or without modification,
  19. // are permitted provided that the following conditions are met:
  20. //
  21. // * Redistribution's of source code must retain the above copyright notice,
  22. // this list of conditions and the following disclaimer.
  23. //
  24. // * Redistribution's in binary form must reproduce the above copyright notice,
  25. // this list of conditions and the following disclaimer in the documentation
  26. // and/or other materials provided with the distribution.
  27. //
  28. // * The name of the copyright holders may not be used to endorse or promote products
  29. // derived from this software without specific prior written permission.
  30. //
  31. // This software is provided by the copyright holders and contributors "as is" and
  32. // any express or implied warranties, including, but not limited to, the implied
  33. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  34. // In no event shall the Intel Corporation or contributors be liable for any direct,
  35. // indirect, incidental, special, exemplary, or consequential damages
  36. // (including, but not limited to, procurement of substitute goods or services;
  37. // loss of use, data, or profits; or business interruption) however caused
  38. // and on any theory of liability, whether in contract, strict liability,
  39. // or tort (including negligence or otherwise) arising in any way out of
  40. // the use of this software, even if advised of the possibility of such damage.
  41. //
  42. //M*/
  43. #ifndef OPENCV_CORE_EIGEN_HPP
  44. #define OPENCV_CORE_EIGEN_HPP
  45. #include "opencv2/core.hpp"
  46. #if defined _MSC_VER && _MSC_VER >= 1200
  47. #pragma warning( disable: 4714 ) //__forceinline is not inlined
  48. #pragma warning( disable: 4127 ) //conditional expression is constant
  49. #pragma warning( disable: 4244 ) //conversion from '__int64' to 'int', possible loss of data
  50. #endif
  51. namespace cv
  52. {
  53. //! @addtogroup core_eigen
  54. //! @{
  55. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  56. void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, OutputArray dst )
  57. {
  58. if( !(src.Flags & Eigen::RowMajorBit) )
  59. {
  60. Mat _src(src.cols(), src.rows(), traits::Type<_Tp>::value,
  61. (void*)src.data(), src.outerStride()*sizeof(_Tp));
  62. transpose(_src, dst);
  63. }
  64. else
  65. {
  66. Mat _src(src.rows(), src.cols(), traits::Type<_Tp>::value,
  67. (void*)src.data(), src.outerStride()*sizeof(_Tp));
  68. _src.copyTo(dst);
  69. }
  70. }
  71. // Matx case
  72. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  73. void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src,
  74. Matx<_Tp, _rows, _cols>& dst )
  75. {
  76. if( !(src.Flags & Eigen::RowMajorBit) )
  77. {
  78. dst = Matx<_Tp, _cols, _rows>(static_cast<const _Tp*>(src.data())).t();
  79. }
  80. else
  81. {
  82. dst = Matx<_Tp, _rows, _cols>(static_cast<const _Tp*>(src.data()));
  83. }
  84. }
  85. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  86. void cv2eigen( const Mat& src,
  87. Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
  88. {
  89. CV_DbgAssert(src.rows == _rows && src.cols == _cols);
  90. if( !(dst.Flags & Eigen::RowMajorBit) )
  91. {
  92. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  93. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  94. if( src.type() == _dst.type() )
  95. transpose(src, _dst);
  96. else if( src.cols == src.rows )
  97. {
  98. src.convertTo(_dst, _dst.type());
  99. transpose(_dst, _dst);
  100. }
  101. else
  102. Mat(src.t()).convertTo(_dst, _dst.type());
  103. }
  104. else
  105. {
  106. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  107. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  108. src.convertTo(_dst, _dst.type());
  109. }
  110. }
  111. // Matx case
  112. template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
  113. void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
  114. Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
  115. {
  116. if( !(dst.Flags & Eigen::RowMajorBit) )
  117. {
  118. const Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
  119. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  120. transpose(src, _dst);
  121. }
  122. else
  123. {
  124. const Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
  125. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  126. Mat(src).copyTo(_dst);
  127. }
  128. }
  129. template<typename _Tp> static inline
  130. void cv2eigen( const Mat& src,
  131. Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
  132. {
  133. dst.resize(src.rows, src.cols);
  134. if( !(dst.Flags & Eigen::RowMajorBit) )
  135. {
  136. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  137. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  138. if( src.type() == _dst.type() )
  139. transpose(src, _dst);
  140. else if( src.cols == src.rows )
  141. {
  142. src.convertTo(_dst, _dst.type());
  143. transpose(_dst, _dst);
  144. }
  145. else
  146. Mat(src.t()).convertTo(_dst, _dst.type());
  147. }
  148. else
  149. {
  150. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  151. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  152. src.convertTo(_dst, _dst.type());
  153. }
  154. }
  155. // Matx case
  156. template<typename _Tp, int _rows, int _cols> static inline
  157. void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
  158. Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
  159. {
  160. dst.resize(_rows, _cols);
  161. if( !(dst.Flags & Eigen::RowMajorBit) )
  162. {
  163. const Mat _dst(_cols, _rows, traits::Type<_Tp>::value,
  164. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  165. transpose(src, _dst);
  166. }
  167. else
  168. {
  169. const Mat _dst(_rows, _cols, traits::Type<_Tp>::value,
  170. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  171. Mat(src).copyTo(_dst);
  172. }
  173. }
  174. template<typename _Tp> static inline
  175. void cv2eigen( const Mat& src,
  176. Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
  177. {
  178. CV_Assert(src.cols == 1);
  179. dst.resize(src.rows);
  180. if( !(dst.Flags & Eigen::RowMajorBit) )
  181. {
  182. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  183. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  184. if( src.type() == _dst.type() )
  185. transpose(src, _dst);
  186. else
  187. Mat(src.t()).convertTo(_dst, _dst.type());
  188. }
  189. else
  190. {
  191. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  192. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  193. src.convertTo(_dst, _dst.type());
  194. }
  195. }
  196. // Matx case
  197. template<typename _Tp, int _rows> static inline
  198. void cv2eigen( const Matx<_Tp, _rows, 1>& src,
  199. Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
  200. {
  201. dst.resize(_rows);
  202. if( !(dst.Flags & Eigen::RowMajorBit) )
  203. {
  204. const Mat _dst(1, _rows, traits::Type<_Tp>::value,
  205. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  206. transpose(src, _dst);
  207. }
  208. else
  209. {
  210. const Mat _dst(_rows, 1, traits::Type<_Tp>::value,
  211. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  212. src.copyTo(_dst);
  213. }
  214. }
  215. template<typename _Tp> static inline
  216. void cv2eigen( const Mat& src,
  217. Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
  218. {
  219. CV_Assert(src.rows == 1);
  220. dst.resize(src.cols);
  221. if( !(dst.Flags & Eigen::RowMajorBit) )
  222. {
  223. const Mat _dst(src.cols, src.rows, traits::Type<_Tp>::value,
  224. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  225. if( src.type() == _dst.type() )
  226. transpose(src, _dst);
  227. else
  228. Mat(src.t()).convertTo(_dst, _dst.type());
  229. }
  230. else
  231. {
  232. const Mat _dst(src.rows, src.cols, traits::Type<_Tp>::value,
  233. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  234. src.convertTo(_dst, _dst.type());
  235. }
  236. }
  237. //Matx
  238. template<typename _Tp, int _cols> static inline
  239. void cv2eigen( const Matx<_Tp, 1, _cols>& src,
  240. Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
  241. {
  242. dst.resize(_cols);
  243. if( !(dst.Flags & Eigen::RowMajorBit) )
  244. {
  245. const Mat _dst(_cols, 1, traits::Type<_Tp>::value,
  246. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  247. transpose(src, _dst);
  248. }
  249. else
  250. {
  251. const Mat _dst(1, _cols, traits::Type<_Tp>::value,
  252. dst.data(), (size_t)(dst.outerStride()*sizeof(_Tp)));
  253. Mat(src).copyTo(_dst);
  254. }
  255. }
  256. //! @}
  257. } // cv
  258. #endif