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
44
45 #ifndef __OPENCV_CORE_EIGEN_HPP__
46 #define __OPENCV_CORE_EIGEN_HPP__
47
48 #include "opencv2/core.hpp"
49
50 #if defined _MSC_VER && _MSC_VER >= 1200
51 #pragma warning( disable: 4714 ) //__forceinline is not inlined
52 #pragma warning( disable: 4127 ) //conditional expression is constant
53 #pragma warning( disable: 4244 ) //conversion from '__int64' to 'int', possible loss of data
54 #endif
55
56 namespace cv
57 {
58
59 //! @addtogroup core_eigen
60 //! @{
61
62 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
eigen2cv(const Eigen::Matrix<_Tp,_rows,_cols,_options,_maxRows,_maxCols> & src,Mat & dst)63 void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, Mat& dst )
64 {
65 if( !(src.Flags & Eigen::RowMajorBit) )
66 {
67 Mat _src(src.cols(), src.rows(), DataType<_Tp>::type,
68 (void*)src.data(), src.stride()*sizeof(_Tp));
69 transpose(_src, dst);
70 }
71 else
72 {
73 Mat _src(src.rows(), src.cols(), DataType<_Tp>::type,
74 (void*)src.data(), src.stride()*sizeof(_Tp));
75 _src.copyTo(dst);
76 }
77 }
78
79 // Matx case
80 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
eigen2cv(const Eigen::Matrix<_Tp,_rows,_cols,_options,_maxRows,_maxCols> & src,Matx<_Tp,_rows,_cols> & dst)81 void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src,
82 Matx<_Tp, _rows, _cols>& dst )
83 {
84 if( !(src.Flags & Eigen::RowMajorBit) )
85 {
86 dst = Matx<_Tp, _cols, _rows>(static_cast<const _Tp*>(src.data())).t();
87 }
88 else
89 {
90 dst = Matx<_Tp, _rows, _cols>(static_cast<const _Tp*>(src.data()));
91 }
92 }
93
94 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
cv2eigen(const Mat & src,Eigen::Matrix<_Tp,_rows,_cols,_options,_maxRows,_maxCols> & dst)95 void cv2eigen( const Mat& src,
96 Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
97 {
98 CV_DbgAssert(src.rows == _rows && src.cols == _cols);
99 if( !(dst.Flags & Eigen::RowMajorBit) )
100 {
101 const Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
102 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
103 if( src.type() == _dst.type() )
104 transpose(src, _dst);
105 else if( src.cols == src.rows )
106 {
107 src.convertTo(_dst, _dst.type());
108 transpose(_dst, _dst);
109 }
110 else
111 Mat(src.t()).convertTo(_dst, _dst.type());
112 }
113 else
114 {
115 const Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
116 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
117 src.convertTo(_dst, _dst.type());
118 }
119 }
120
121 // Matx case
122 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols> static inline
cv2eigen(const Matx<_Tp,_rows,_cols> & src,Eigen::Matrix<_Tp,_rows,_cols,_options,_maxRows,_maxCols> & dst)123 void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
124 Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
125 {
126 if( !(dst.Flags & Eigen::RowMajorBit) )
127 {
128 const Mat _dst(_cols, _rows, DataType<_Tp>::type,
129 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
130 transpose(src, _dst);
131 }
132 else
133 {
134 const Mat _dst(_rows, _cols, DataType<_Tp>::type,
135 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
136 Mat(src).copyTo(_dst);
137 }
138 }
139
140 template<typename _Tp> static inline
cv2eigen(const Mat & src,Eigen::Matrix<_Tp,Eigen::Dynamic,Eigen::Dynamic> & dst)141 void cv2eigen( const Mat& src,
142 Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
143 {
144 dst.resize(src.rows, src.cols);
145 if( !(dst.Flags & Eigen::RowMajorBit) )
146 {
147 const Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
148 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
149 if( src.type() == _dst.type() )
150 transpose(src, _dst);
151 else if( src.cols == src.rows )
152 {
153 src.convertTo(_dst, _dst.type());
154 transpose(_dst, _dst);
155 }
156 else
157 Mat(src.t()).convertTo(_dst, _dst.type());
158 }
159 else
160 {
161 const Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
162 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
163 src.convertTo(_dst, _dst.type());
164 }
165 }
166
167 // Matx case
168 template<typename _Tp, int _rows, int _cols> static inline
cv2eigen(const Matx<_Tp,_rows,_cols> & src,Eigen::Matrix<_Tp,Eigen::Dynamic,Eigen::Dynamic> & dst)169 void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
170 Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
171 {
172 dst.resize(_rows, _cols);
173 if( !(dst.Flags & Eigen::RowMajorBit) )
174 {
175 const Mat _dst(_cols, _rows, DataType<_Tp>::type,
176 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
177 transpose(src, _dst);
178 }
179 else
180 {
181 const Mat _dst(_rows, _cols, DataType<_Tp>::type,
182 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
183 Mat(src).copyTo(_dst);
184 }
185 }
186
187 template<typename _Tp> static inline
cv2eigen(const Mat & src,Eigen::Matrix<_Tp,Eigen::Dynamic,1> & dst)188 void cv2eigen( const Mat& src,
189 Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
190 {
191 CV_Assert(src.cols == 1);
192 dst.resize(src.rows);
193
194 if( !(dst.Flags & Eigen::RowMajorBit) )
195 {
196 const Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
197 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
198 if( src.type() == _dst.type() )
199 transpose(src, _dst);
200 else
201 Mat(src.t()).convertTo(_dst, _dst.type());
202 }
203 else
204 {
205 const Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
206 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
207 src.convertTo(_dst, _dst.type());
208 }
209 }
210
211 // Matx case
212 template<typename _Tp, int _rows> static inline
cv2eigen(const Matx<_Tp,_rows,1> & src,Eigen::Matrix<_Tp,Eigen::Dynamic,1> & dst)213 void cv2eigen( const Matx<_Tp, _rows, 1>& src,
214 Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
215 {
216 dst.resize(_rows);
217
218 if( !(dst.Flags & Eigen::RowMajorBit) )
219 {
220 const Mat _dst(1, _rows, DataType<_Tp>::type,
221 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
222 transpose(src, _dst);
223 }
224 else
225 {
226 const Mat _dst(_rows, 1, DataType<_Tp>::type,
227 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
228 src.copyTo(_dst);
229 }
230 }
231
232
233 template<typename _Tp> static inline
cv2eigen(const Mat & src,Eigen::Matrix<_Tp,1,Eigen::Dynamic> & dst)234 void cv2eigen( const Mat& src,
235 Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
236 {
237 CV_Assert(src.rows == 1);
238 dst.resize(src.cols);
239 if( !(dst.Flags & Eigen::RowMajorBit) )
240 {
241 const Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
242 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
243 if( src.type() == _dst.type() )
244 transpose(src, _dst);
245 else
246 Mat(src.t()).convertTo(_dst, _dst.type());
247 }
248 else
249 {
250 const Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
251 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
252 src.convertTo(_dst, _dst.type());
253 }
254 }
255
256 //Matx
257 template<typename _Tp, int _cols> static inline
cv2eigen(const Matx<_Tp,1,_cols> & src,Eigen::Matrix<_Tp,1,Eigen::Dynamic> & dst)258 void cv2eigen( const Matx<_Tp, 1, _cols>& src,
259 Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
260 {
261 dst.resize(_cols);
262 if( !(dst.Flags & Eigen::RowMajorBit) )
263 {
264 const Mat _dst(_cols, 1, DataType<_Tp>::type,
265 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
266 transpose(src, _dst);
267 }
268 else
269 {
270 const Mat _dst(1, _cols, DataType<_Tp>::type,
271 dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
272 Mat(src).copyTo(_dst);
273 }
274 }
275
276 //! @}
277
278 } // cv
279
280 #endif
281