include/opencv2/core/eigen.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                          License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_CORE_EIGEN_HPP__
00044 #define __OPENCV_CORE_EIGEN_HPP__
00045 
00046 #ifdef __cplusplus
00047 
00048 #include "opencv2/core/core_c.h"
00049 #include "opencv2/core/core.hpp"
00050 
00051 #if defined _MSC_VER && _MSC_VER >= 1200
00052 #pragma warning( disable: 4714 ) //__forceinline is not inlined
00053 #pragma warning( disable: 4127 ) //conditional expression is constant
00054 #pragma warning( disable: 4244 ) //conversion from '__int64' to 'int', possible loss of data
00055 #endif
00056 
00057 namespace cv
00058 {
00059 
00060 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols>
00061 void eigen2cv( const Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& src, Mat& dst )
00062 {
00063     if( !(src.Flags & Eigen::RowMajorBit) )
00064     {
00065         Mat _src(src.cols(), src.rows(), DataType<_Tp>::type,
00066               (void*)src.data(), src.stride()*sizeof(_Tp));
00067         transpose(_src, dst);
00068     }
00069     else
00070     {
00071         Mat _src(src.rows(), src.cols(), DataType<_Tp>::type,
00072                  (void*)src.data(), src.stride()*sizeof(_Tp));
00073         _src.copyTo(dst);
00074     }
00075 }
00076 
00077 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols>
00078 void cv2eigen( const Mat& src,
00079                Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
00080 {
00081     CV_DbgAssert(src.rows == _rows && src.cols == _cols);
00082     if( !(dst.Flags & Eigen::RowMajorBit) )
00083     {
00084         Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
00085                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00086         if( src.type() == _dst.type() )
00087             transpose(src, _dst);
00088         else if( src.cols == src.rows )
00089         {
00090             src.convertTo(_dst, _dst.type());
00091             transpose(_dst, _dst);
00092         }
00093         else
00094             Mat(src.t()).convertTo(_dst, _dst.type());
00095         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00096     }
00097     else
00098     {
00099         Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
00100                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00101         src.convertTo(_dst, _dst.type());
00102         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00103     }
00104 }
00105 
00106 // Matx case
00107 template<typename _Tp, int _rows, int _cols, int _options, int _maxRows, int _maxCols>
00108 void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
00109                Eigen::Matrix<_Tp, _rows, _cols, _options, _maxRows, _maxCols>& dst )
00110 {
00111     if( !(dst.Flags & Eigen::RowMajorBit) )
00112     {
00113         Mat _dst(_cols, _rows, DataType<_Tp>::type,
00114                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00115         transpose(src, _dst);
00116         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00117     }
00118     else
00119     {
00120         Mat _dst(_rows, _cols, DataType<_Tp>::type,
00121                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00122         Mat(src).copyTo(_dst);
00123         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00124     }
00125 }
00126 
00127 template<typename _Tp>
00128 void cv2eigen( const Mat& src,
00129                Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
00130 {
00131     dst.resize(src.rows, src.cols);
00132     if( !(dst.Flags & Eigen::RowMajorBit) )
00133     {
00134         Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
00135              dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00136         if( src.type() == _dst.type() )
00137             transpose(src, _dst);
00138         else if( src.cols == src.rows )
00139         {
00140             src.convertTo(_dst, _dst.type());
00141             transpose(_dst, _dst);
00142         }
00143         else
00144             Mat(src.t()).convertTo(_dst, _dst.type());
00145         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00146     }
00147     else
00148     {
00149         Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
00150                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00151         src.convertTo(_dst, _dst.type());
00152         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00153     }
00154 }
00155 
00156 // Matx case
00157 template<typename _Tp, int _rows, int _cols>
00158 void cv2eigen( const Matx<_Tp, _rows, _cols>& src,
00159                Eigen::Matrix<_Tp, Eigen::Dynamic, Eigen::Dynamic>& dst )
00160 {
00161     dst.resize(_rows, _cols);
00162     if( !(dst.Flags & Eigen::RowMajorBit) )
00163     {
00164         Mat _dst(_cols, _rows, DataType<_Tp>::type,
00165              dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00166         transpose(src, _dst);
00167         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00168     }
00169     else
00170     {
00171         Mat _dst(_rows, _cols, DataType<_Tp>::type,
00172                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00173         Mat(src).copyTo(_dst);
00174         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00175     }
00176 }
00177 
00178 template<typename _Tp>
00179 void cv2eigen( const Mat& src,
00180                Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
00181 {
00182     CV_Assert(src.cols == 1);
00183     dst.resize(src.rows);
00184 
00185     if( !(dst.Flags & Eigen::RowMajorBit) )
00186     {
00187         Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
00188                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00189         if( src.type() == _dst.type() )
00190             transpose(src, _dst);
00191         else
00192             Mat(src.t()).convertTo(_dst, _dst.type());
00193         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00194     }
00195     else
00196     {
00197         Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
00198                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00199         src.convertTo(_dst, _dst.type());
00200         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00201     }
00202 }
00203 
00204 // Matx case
00205 template<typename _Tp, int _rows>
00206 void cv2eigen( const Matx<_Tp, _rows, 1>& src,
00207                Eigen::Matrix<_Tp, Eigen::Dynamic, 1>& dst )
00208 {
00209     dst.resize(_rows);
00210 
00211     if( !(dst.Flags & Eigen::RowMajorBit) )
00212     {
00213         Mat _dst(1, _rows, DataType<_Tp>::type,
00214                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00215         transpose(src, _dst);
00216         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00217     }
00218     else
00219     {
00220         Mat _dst(_rows, 1, DataType<_Tp>::type,
00221                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00222         src.copyTo(_dst);
00223         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00224     }
00225 }
00226 
00227 
00228 template<typename _Tp>
00229 void cv2eigen( const Mat& src,
00230                Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
00231 {
00232     CV_Assert(src.rows == 1);
00233     dst.resize(src.cols);
00234     if( !(dst.Flags & Eigen::RowMajorBit) )
00235     {
00236         Mat _dst(src.cols, src.rows, DataType<_Tp>::type,
00237                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00238         if( src.type() == _dst.type() )
00239             transpose(src, _dst);
00240         else
00241             Mat(src.t()).convertTo(_dst, _dst.type());
00242         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00243     }
00244     else
00245     {
00246         Mat _dst(src.rows, src.cols, DataType<_Tp>::type,
00247                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00248         src.convertTo(_dst, _dst.type());
00249         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00250     }
00251 }
00252 
00253 //Matx
00254 template<typename _Tp, int _cols>
00255 void cv2eigen( const Matx<_Tp, 1, _cols>& src,
00256                Eigen::Matrix<_Tp, 1, Eigen::Dynamic>& dst )
00257 {
00258     dst.resize(_cols);
00259     if( !(dst.Flags & Eigen::RowMajorBit) )
00260     {
00261         Mat _dst(_cols, 1, DataType<_Tp>::type,
00262                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00263         transpose(src, _dst);
00264         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00265     }
00266     else
00267     {
00268         Mat _dst(1, _cols, DataType<_Tp>::type,
00269                  dst.data(), (size_t)(dst.stride()*sizeof(_Tp)));
00270         Mat(src).copyTo(_dst);
00271         CV_DbgAssert(_dst.data == (uchar*)dst.data());
00272     }
00273 }
00274 
00275 
00276 }
00277 
00278 #endif
00279 
00280 #endif
00281