/*M/////////////////////////////////////////////////////////////////////////////////////// // // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. // // By downloading, copying, installing or using the software you agree to this license. // If you do not agree to this license, do not download, install, // copy or use the software. // // // License Agreement // For Open Source Computer Vision Library // // Copyright (C) 2000-2008, Intel Corporation, all rights reserved. // Copyright (C) 2009, Willow Garage Inc., all rights reserved. // Copyright (C) 2013, OpenCV Foundation, all rights reserved. // Third party copyrights are property of their respective owners. // // Redistribution and use in source and binary forms, with or without modification, // are permitted provided that the following conditions are met: // // * Redistribution's of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // // * Redistribution's in binary form must reproduce the above copyright notice, // this list of conditions and the following disclaimer in the documentation // and/or other materials provided with the distribution. // // * The name of the copyright holders may not be used to endorse or promote products // derived from this software without specific prior written permission. // // This software is provided by the copyright holders and contributors "as is" and // any express or implied warranties, including, but not limited to, the implied // warranties of merchantability and fitness for a particular purpose are disclaimed. // In no event shall the Intel Corporation or contributors be liable for any direct, // indirect, incidental, special, exemplary, or consequential damages // (including, but not limited to, procurement of substitute goods or services; // loss of use, data, or profits; or business interruption) however caused // and on any theory of liability, whether in contract, strict liability, // or tort (including negligence or otherwise) arising in any way out of // the use of this software, even if advised of the possibility of such damage. // //M*/ #ifndef __OPENCV_CORE_MATX_HPP__ #define __OPENCV_CORE_MATX_HPP__ #ifndef __cplusplus # error matx.hpp header must be compiled as C++ #endif #include "opencv2/core/cvdef.h" #include "opencv2/core/base.hpp" #include "opencv2/core/traits.hpp" namespace cv { //! @addtogroup core_basic //! @{ ////////////////////////////// Small Matrix /////////////////////////// //! @cond IGNORED struct CV_EXPORTS Matx_AddOp {}; struct CV_EXPORTS Matx_SubOp {}; struct CV_EXPORTS Matx_ScaleOp {}; struct CV_EXPORTS Matx_MulOp {}; struct CV_EXPORTS Matx_DivOp {}; struct CV_EXPORTS Matx_MatMulOp {}; struct CV_EXPORTS Matx_TOp {}; //! @endcond /** @brief Template class for small matrices whose type and size are known at compilation time If you need a more flexible type, use Mat . The elements of the matrix M are accessible using the M(i,j) notation. Most of the common matrix operations (see also @ref MatrixExpressions ) are available. To do an operation on Matx that is not implemented, you can easily convert the matrix to Mat and backwards: @code Matx33f m(1, 2, 3, 4, 5, 6, 7, 8, 9); cout << sum(Mat(m*m.t())) << endl; @endcode */ template class Matx { public: enum { depth = DataType<_Tp>::depth, rows = m, cols = n, channels = rows*cols, type = CV_MAKETYPE(depth, channels), shortdim = (m < n ? m : n) }; typedef _Tp value_type; typedef Matx<_Tp, m, n> mat_type; typedef Matx<_Tp, shortdim, 1> diag_type; //! default constructor Matx(); Matx(_Tp v0); //!< 1x1 matrix Matx(_Tp v0, _Tp v1); //!< 1x2 or 2x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2); //!< 1x3 or 3x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 1x4, 2x2 or 4x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 1x5 or 5x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 1x6, 2x3, 3x2 or 6x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 1x7 or 7x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 1x8, 2x4, 4x2 or 8x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 1x9, 3x3 or 9x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 1x10, 2x5 or 5x2 or 10x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11); //!< 1x12, 2x6, 3x4, 4x3, 6x2 or 12x1 matrix Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13, _Tp v14, _Tp v15); //!< 1x16, 4x4 or 16x1 matrix explicit Matx(const _Tp* vals); //!< initialize from a plain array static Matx all(_Tp alpha); static Matx zeros(); static Matx ones(); static Matx eye(); static Matx diag(const diag_type& d); static Matx randu(_Tp a, _Tp b); static Matx randn(_Tp a, _Tp b); //! dot product computed with the default precision _Tp dot(const Matx<_Tp, m, n>& v) const; //! dot product computed in double-precision arithmetics double ddot(const Matx<_Tp, m, n>& v) const; //! conversion to another data type template operator Matx() const; //! change the matrix shape template Matx<_Tp, m1, n1> reshape() const; //! extract part of the matrix template Matx<_Tp, m1, n1> get_minor(int i, int j) const; //! extract the matrix row Matx<_Tp, 1, n> row(int i) const; //! extract the matrix column Matx<_Tp, m, 1> col(int i) const; //! extract the matrix diagonal diag_type diag() const; //! transpose the matrix Matx<_Tp, n, m> t() const; //! invert the matrix Matx<_Tp, n, m> inv(int method=DECOMP_LU, bool *p_is_ok = NULL) const; //! solve linear system template Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU) const; Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method) const; //! multiply two matrices element-wise Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) const; //! divide two matrices element-wise Matx<_Tp, m, n> div(const Matx<_Tp, m, n>& a) const; //! element access const _Tp& operator ()(int i, int j) const; _Tp& operator ()(int i, int j); //! 1D element access const _Tp& operator ()(int i) const; _Tp& operator ()(int i); Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp); Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp); template Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp); Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp); Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp); template Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp); Matx(const Matx<_Tp, n, m>& a, Matx_TOp); _Tp val[m*n]; //< matrix elements }; typedef Matx Matx12f; typedef Matx Matx12d; typedef Matx Matx13f; typedef Matx Matx13d; typedef Matx Matx14f; typedef Matx Matx14d; typedef Matx Matx16f; typedef Matx Matx16d; typedef Matx Matx21f; typedef Matx Matx21d; typedef Matx Matx31f; typedef Matx Matx31d; typedef Matx Matx41f; typedef Matx Matx41d; typedef Matx Matx61f; typedef Matx Matx61d; typedef Matx Matx22f; typedef Matx Matx22d; typedef Matx Matx23f; typedef Matx Matx23d; typedef Matx Matx32f; typedef Matx Matx32d; typedef Matx Matx33f; typedef Matx Matx33d; typedef Matx Matx34f; typedef Matx Matx34d; typedef Matx Matx43f; typedef Matx Matx43d; typedef Matx Matx44f; typedef Matx Matx44d; typedef Matx Matx66f; typedef Matx Matx66d; /*! traits */ template class DataType< Matx<_Tp, m, n> > { public: typedef Matx<_Tp, m, n> value_type; typedef Matx::work_type, m, n> work_type; typedef _Tp channel_type; typedef value_type vec_type; enum { generic_type = 0, depth = DataType::depth, channels = m * n, fmt = DataType::fmt + ((channels - 1) << 8), type = CV_MAKETYPE(depth, channels) }; }; /** @brief Comma-separated Matrix Initializer */ template class MatxCommaInitializer { public: MatxCommaInitializer(Matx<_Tp, m, n>* _mtx); template MatxCommaInitializer<_Tp, m, n>& operator , (T2 val); Matx<_Tp, m, n> operator *() const; Matx<_Tp, m, n>* dst; int idx; }; /* Utility methods */ template static double determinant(const Matx<_Tp, m, m>& a); template static double trace(const Matx<_Tp, m, n>& a); template static double norm(const Matx<_Tp, m, n>& M); template static double norm(const Matx<_Tp, m, n>& M, int normType); /////////////////////// Vec (used as element of multi-channel images ///////////////////// /** @brief Template class for short numerical vectors, a partial case of Matx This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements) on which you can perform basic arithmetical operations, access individual elements using [] operator etc. The vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc., which elements are dynamically allocated in the heap. The template takes 2 parameters: @tparam _Tp element type @tparam cn the number of elements In addition to the universal notation like Vec, you can use shorter aliases for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec. It is possible to convert Vec\ to/from Point_, Vec\ to/from Point3_ , and Vec\ to CvScalar or Scalar_. Use operator[] to access the elements of Vec. All the expected vector operations are also implemented: - v1 = v2 + v3 - v1 = v2 - v3 - v1 = v2 \* scale - v1 = scale \* v2 - v1 = -v2 - v1 += v2 and other augmenting operations - v1 == v2, v1 != v2 - norm(v1) (euclidean norm) The Vec class is commonly used to describe pixel types of multi-channel arrays. See Mat for details. */ template class Vec : public Matx<_Tp, cn, 1> { public: typedef _Tp value_type; enum { depth = Matx<_Tp, cn, 1>::depth, channels = cn, type = CV_MAKETYPE(depth, channels) }; //! default constructor Vec(); Vec(_Tp v0); //!< 1-element vector constructor Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9); //!< 10-element vector constructor explicit Vec(const _Tp* values); Vec(const Vec<_Tp, cn>& v); static Vec all(_Tp alpha); //! per-element multiplication Vec mul(const Vec<_Tp, cn>& v) const; //! conjugation (makes sense for complex numbers and quaternions) Vec conj() const; /*! cross product of the two 3D vectors. For other dimensionalities the exception is raised */ Vec cross(const Vec& v) const; //! conversion to another data type template operator Vec() const; /*! element access */ const _Tp& operator [](int i) const; _Tp& operator[](int i); const _Tp& operator ()(int i) const; _Tp& operator ()(int i); Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp); Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp); template Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp); }; /** @name Shorter aliases for the most popular specializations of Vec @{ */ typedef Vec Vec2b; typedef Vec Vec3b; typedef Vec Vec4b; typedef Vec Vec2s; typedef Vec Vec3s; typedef Vec Vec4s; typedef Vec Vec2w; typedef Vec Vec3w; typedef Vec Vec4w; typedef Vec Vec2i; typedef Vec Vec3i; typedef Vec Vec4i; typedef Vec Vec6i; typedef Vec Vec8i; typedef Vec Vec2f; typedef Vec Vec3f; typedef Vec Vec4f; typedef Vec Vec6f; typedef Vec Vec2d; typedef Vec Vec3d; typedef Vec Vec4d; typedef Vec Vec6d; /** @} */ /*! traits */ template class DataType< Vec<_Tp, cn> > { public: typedef Vec<_Tp, cn> value_type; typedef Vec::work_type, cn> work_type; typedef _Tp channel_type; typedef value_type vec_type; enum { generic_type = 0, depth = DataType::depth, channels = cn, fmt = DataType::fmt + ((channels - 1) << 8), type = CV_MAKETYPE(depth, channels) }; }; /** @brief Comma-separated Vec Initializer */ template class VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1> { public: VecCommaInitializer(Vec<_Tp, m>* _vec); template VecCommaInitializer<_Tp, m>& operator , (T2 val); Vec<_Tp, m> operator *() const; }; template static Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v); //! @} core_basic //! @cond IGNORED ///////////////////////////////////// helper classes ///////////////////////////////////// namespace internal { template struct Matx_DetOp { double operator ()(const Matx<_Tp, m, m>& a) const { Matx<_Tp, m, m> temp = a; double p = LU(temp.val, m*sizeof(_Tp), m, 0, 0, 0); if( p == 0 ) return p; for( int i = 0; i < m; i++ ) p *= temp(i, i); return 1./p; } }; template struct Matx_DetOp<_Tp, 1> { double operator ()(const Matx<_Tp, 1, 1>& a) const { return a(0,0); } }; template struct Matx_DetOp<_Tp, 2> { double operator ()(const Matx<_Tp, 2, 2>& a) const { return a(0,0)*a(1,1) - a(0,1)*a(1,0); } }; template struct Matx_DetOp<_Tp, 3> { double operator ()(const Matx<_Tp, 3, 3>& a) const { return a(0,0)*(a(1,1)*a(2,2) - a(2,1)*a(1,2)) - a(0,1)*(a(1,0)*a(2,2) - a(2,0)*a(1,2)) + a(0,2)*(a(1,0)*a(2,1) - a(2,0)*a(1,1)); } }; template Vec<_Tp, 2> inline conjugate(const Vec<_Tp, 2>& v) { return Vec<_Tp, 2>(v[0], -v[1]); } template Vec<_Tp, 4> inline conjugate(const Vec<_Tp, 4>& v) { return Vec<_Tp, 4>(v[0], -v[1], -v[2], -v[3]); } } // internal ////////////////////////////////// Matx Implementation /////////////////////////////////// template inline Matx<_Tp, m, n>::Matx() { for(int i = 0; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0) { val[0] = v0; for(int i = 1; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1) { CV_StaticAssert(channels >= 2, "Matx should have at least 2 elaments."); val[0] = v0; val[1] = v1; for(int i = 2; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2) { CV_StaticAssert(channels >= 3, "Matx should have at least 3 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; for(int i = 3; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3) { CV_StaticAssert(channels >= 4, "Matx should have at least 4 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; for(int i = 4; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4) { CV_StaticAssert(channels >= 5, "Matx should have at least 5 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; for(int i = 5; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5) { CV_StaticAssert(channels >= 6, "Matx should have at least 6 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; for(int i = 6; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6) { CV_StaticAssert(channels >= 7, "Matx should have at least 7 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; for(int i = 7; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7) { CV_StaticAssert(channels >= 8, "Matx should have at least 8 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; for(int i = 8; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8) { CV_StaticAssert(channels >= 9, "Matx should have at least 9 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; val[8] = v8; for(int i = 9; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9) { CV_StaticAssert(channels >= 10, "Matx should have at least 10 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; val[8] = v8; val[9] = v9; for(int i = 10; i < channels; i++) val[i] = _Tp(0); } template inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11) { CV_StaticAssert(channels == 12, "Matx should have at least 12 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11; } template inline Matx<_Tp,m,n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9, _Tp v10, _Tp v11, _Tp v12, _Tp v13, _Tp v14, _Tp v15) { CV_StaticAssert(channels == 16, "Matx should have at least 16 elaments."); val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7; val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11; val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15; } template inline Matx<_Tp, m, n>::Matx(const _Tp* values) { for( int i = 0; i < channels; i++ ) val[i] = values[i]; } template inline Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha) { Matx<_Tp, m, n> M; for( int i = 0; i < m*n; i++ ) M.val[i] = alpha; return M; } template inline Matx<_Tp,m,n> Matx<_Tp,m,n>::zeros() { return all(0); } template inline Matx<_Tp,m,n> Matx<_Tp,m,n>::ones() { return all(1); } template inline Matx<_Tp,m,n> Matx<_Tp,m,n>::eye() { Matx<_Tp,m,n> M; for(int i = 0; i < shortdim; i++) M(i,i) = 1; return M; } template inline _Tp Matx<_Tp, m, n>::dot(const Matx<_Tp, m, n>& M) const { _Tp s = 0; for( int i = 0; i < channels; i++ ) s += val[i]*M.val[i]; return s; } template inline double Matx<_Tp, m, n>::ddot(const Matx<_Tp, m, n>& M) const { double s = 0; for( int i = 0; i < channels; i++ ) s += (double)val[i]*M.val[i]; return s; } template inline Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const typename Matx<_Tp,m,n>::diag_type& d) { Matx<_Tp,m,n> M; for(int i = 0; i < shortdim; i++) M(i,i) = d(i, 0); return M; } template template inline Matx<_Tp, m, n>::operator Matx() const { Matx M; for( int i = 0; i < m*n; i++ ) M.val[i] = saturate_cast(val[i]); return M; } template template inline Matx<_Tp, m1, n1> Matx<_Tp, m, n>::reshape() const { CV_StaticAssert(m1*n1 == m*n, "Input and destnarion matrices must have the same number of elements"); return (const Matx<_Tp, m1, n1>&)*this; } template template inline Matx<_Tp, m1, n1> Matx<_Tp, m, n>::get_minor(int i, int j) const { CV_DbgAssert(0 <= i && i+m1 <= m && 0 <= j && j+n1 <= n); Matx<_Tp, m1, n1> s; for( int di = 0; di < m1; di++ ) for( int dj = 0; dj < n1; dj++ ) s(di, dj) = (*this)(i+di, j+dj); return s; } template inline Matx<_Tp, 1, n> Matx<_Tp, m, n>::row(int i) const { CV_DbgAssert((unsigned)i < (unsigned)m); return Matx<_Tp, 1, n>(&val[i*n]); } template inline Matx<_Tp, m, 1> Matx<_Tp, m, n>::col(int j) const { CV_DbgAssert((unsigned)j < (unsigned)n); Matx<_Tp, m, 1> v; for( int i = 0; i < m; i++ ) v.val[i] = val[i*n + j]; return v; } template inline typename Matx<_Tp, m, n>::diag_type Matx<_Tp, m, n>::diag() const { diag_type d; for( int i = 0; i < shortdim; i++ ) d.val[i] = val[i*n + i]; return d; } template inline const _Tp& Matx<_Tp, m, n>::operator()(int i, int j) const { CV_DbgAssert( (unsigned)i < (unsigned)m && (unsigned)j < (unsigned)n ); return this->val[i*n + j]; } template inline _Tp& Matx<_Tp, m, n>::operator ()(int i, int j) { CV_DbgAssert( (unsigned)i < (unsigned)m && (unsigned)j < (unsigned)n ); return val[i*n + j]; } template inline const _Tp& Matx<_Tp, m, n>::operator ()(int i) const { CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row"); CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) ); return val[i]; } template inline _Tp& Matx<_Tp, m, n>::operator ()(int i) { CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row"); CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) ); return val[i]; } template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp) { for( int i = 0; i < channels; i++ ) val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]); } template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp) { for( int i = 0; i < channels; i++ ) val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]); } template template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp) { for( int i = 0; i < channels; i++ ) val[i] = saturate_cast<_Tp>(a.val[i] * alpha); } template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp) { for( int i = 0; i < channels; i++ ) val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]); } template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp) { for( int i = 0; i < channels; i++ ) val[i] = saturate_cast<_Tp>(a.val[i] / b.val[i]); } template template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp) { for( int i = 0; i < m; i++ ) for( int j = 0; j < n; j++ ) { _Tp s = 0; for( int k = 0; k < l; k++ ) s += a(i, k) * b(k, j); val[i*n + j] = s; } } template inline Matx<_Tp,m,n>::Matx(const Matx<_Tp, n, m>& a, Matx_TOp) { for( int i = 0; i < m; i++ ) for( int j = 0; j < n; j++ ) val[i*n + j] = a(j, i); } template inline Matx<_Tp, m, n> Matx<_Tp, m, n>::mul(const Matx<_Tp, m, n>& a) const { return Matx<_Tp, m, n>(*this, a, Matx_MulOp()); } template inline Matx<_Tp, m, n> Matx<_Tp, m, n>::div(const Matx<_Tp, m, n>& a) const { return Matx<_Tp, m, n>(*this, a, Matx_DivOp()); } template inline Matx<_Tp, n, m> Matx<_Tp, m, n>::t() const { return Matx<_Tp, n, m>(*this, Matx_TOp()); } template inline Vec<_Tp, n> Matx<_Tp, m, n>::solve(const Vec<_Tp, m>& rhs, int method) const { Matx<_Tp, n, 1> x = solve((const Matx<_Tp, m, 1>&)(rhs), method); return (Vec<_Tp, n>&)(x); } template static inline double determinant(const Matx<_Tp, m, m>& a) { return cv::internal::Matx_DetOp<_Tp, m>()(a); } template static inline double trace(const Matx<_Tp, m, n>& a) { _Tp s = 0; for( int i = 0; i < std::min(m, n); i++ ) s += a(i,i); return s; } template static inline double norm(const Matx<_Tp, m, n>& M) { return std::sqrt(normL2Sqr<_Tp, double>(M.val, m*n)); } template static inline double norm(const Matx<_Tp, m, n>& M, int normType) { return normType == NORM_INF ? (double)normInf<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n) : normType == NORM_L1 ? (double)normL1<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n) : std::sqrt((double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n)); } //////////////////////////////// matx comma initializer ////////////////////////////////// template static inline MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val) { MatxCommaInitializer<_Tp, m, n> commaInitializer((Matx<_Tp, m, n>*)&mtx); return (commaInitializer, val); } template inline MatxCommaInitializer<_Tp, m, n>::MatxCommaInitializer(Matx<_Tp, m, n>* _mtx) : dst(_mtx), idx(0) {} template template inline MatxCommaInitializer<_Tp, m, n>& MatxCommaInitializer<_Tp, m, n>::operator , (_T2 value) { CV_DbgAssert( idx < m*n ); dst->val[idx++] = saturate_cast<_Tp>(value); return *this; } template inline Matx<_Tp, m, n> MatxCommaInitializer<_Tp, m, n>::operator *() const { CV_DbgAssert( idx == n*m ); return *dst; } /////////////////////////////////// Vec Implementation /////////////////////////////////// template inline Vec<_Tp, cn>::Vec() {} template inline Vec<_Tp, cn>::Vec(_Tp v0) : Matx<_Tp, cn, 1>(v0) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1) : Matx<_Tp, cn, 1>(v0, v1) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2) : Matx<_Tp, cn, 1>(v0, v1, v2) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3) : Matx<_Tp, cn, 1>(v0, v1, v2, v3) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4) : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5) : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6) : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7) : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8) : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8) {} template inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9) : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {} template inline Vec<_Tp, cn>::Vec(const _Tp* values) : Matx<_Tp, cn, 1>(values) {} template inline Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& m) : Matx<_Tp, cn, 1>(m.val) {} template inline Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp op) : Matx<_Tp, cn, 1>(a, b, op) {} template inline Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp op) : Matx<_Tp, cn, 1>(a, b, op) {} template template inline Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp op) : Matx<_Tp, cn, 1>(a, alpha, op) {} template inline Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha) { Vec v; for( int i = 0; i < cn; i++ ) v.val[i] = alpha; return v; } template inline Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const { Vec<_Tp, cn> w; for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(this->val[i]*v.val[i]); return w; } template<> inline Vec Vec::conj() const { return cv::internal::conjugate(*this); } template<> inline Vec Vec::conj() const { return cv::internal::conjugate(*this); } template<> inline Vec Vec::conj() const { return cv::internal::conjugate(*this); } template<> inline Vec Vec::conj() const { return cv::internal::conjugate(*this); } template inline Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>&) const { CV_StaticAssert(cn == 3, "for arbitrary-size vector there is no cross-product defined"); return Vec<_Tp, cn>(); } template<> inline Vec Vec::cross(const Vec& v) const { return Vec(val[1]*v.val[2] - val[2]*v.val[1], val[2]*v.val[0] - val[0]*v.val[2], val[0]*v.val[1] - val[1]*v.val[0]); } template<> inline Vec Vec::cross(const Vec& v) const { return Vec(val[1]*v.val[2] - val[2]*v.val[1], val[2]*v.val[0] - val[0]*v.val[2], val[0]*v.val[1] - val[1]*v.val[0]); } template template inline Vec<_Tp, cn>::operator Vec() const { Vec v; for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast(this->val[i]); return v; } template inline const _Tp& Vec<_Tp, cn>::operator [](int i) const { CV_DbgAssert( (unsigned)i < (unsigned)cn ); return this->val[i]; } template inline _Tp& Vec<_Tp, cn>::operator [](int i) { CV_DbgAssert( (unsigned)i < (unsigned)cn ); return this->val[i]; } template inline const _Tp& Vec<_Tp, cn>::operator ()(int i) const { CV_DbgAssert( (unsigned)i < (unsigned)cn ); return this->val[i]; } template inline _Tp& Vec<_Tp, cn>::operator ()(int i) { CV_DbgAssert( (unsigned)i < (unsigned)cn ); return this->val[i]; } template inline Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v) { double nv = norm(v); return v * (nv ? 1./nv : 0.); } //////////////////////////////// matx comma initializer ////////////////////////////////// template static inline VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val) { VecCommaInitializer<_Tp, cn> commaInitializer((Vec<_Tp, cn>*)&vec); return (commaInitializer, val); } template inline VecCommaInitializer<_Tp, cn>::VecCommaInitializer(Vec<_Tp, cn>* _vec) : MatxCommaInitializer<_Tp, cn, 1>(_vec) {} template template inline VecCommaInitializer<_Tp, cn>& VecCommaInitializer<_Tp, cn>::operator , (_T2 value) { CV_DbgAssert( this->idx < cn ); this->dst->val[this->idx++] = saturate_cast<_Tp>(value); return *this; } template inline Vec<_Tp, cn> VecCommaInitializer<_Tp, cn>::operator *() const { CV_DbgAssert( this->idx == cn ); return *this->dst; } //! @endcond ///////////////////////////// Matx out-of-class operators //////////////////////////////// //! @relates cv::Matx //! @{ template static inline Matx<_Tp1, m, n>& operator += (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b) { for( int i = 0; i < m*n; i++ ) a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]); return a; } template static inline Matx<_Tp1, m, n>& operator -= (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b) { for( int i = 0; i < m*n; i++ ) a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]); return a; } template static inline Matx<_Tp, m, n> operator + (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b) { return Matx<_Tp, m, n>(a, b, Matx_AddOp()); } template static inline Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b) { return Matx<_Tp, m, n>(a, b, Matx_SubOp()); } template static inline Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, int alpha) { for( int i = 0; i < m*n; i++ ) a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha); return a; } template static inline Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, float alpha) { for( int i = 0; i < m*n; i++ ) a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha); return a; } template static inline Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, double alpha) { for( int i = 0; i < m*n; i++ ) a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha); return a; } template static inline Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, int alpha) { return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp()); } template static inline Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, float alpha) { return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp()); } template static inline Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, double alpha) { return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp()); } template static inline Matx<_Tp, m, n> operator * (int alpha, const Matx<_Tp, m, n>& a) { return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp()); } template static inline Matx<_Tp, m, n> operator * (float alpha, const Matx<_Tp, m, n>& a) { return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp()); } template static inline Matx<_Tp, m, n> operator * (double alpha, const Matx<_Tp, m, n>& a) { return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp()); } template static inline Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a) { return Matx<_Tp, m, n>(a, -1, Matx_ScaleOp()); } template static inline Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b) { return Matx<_Tp, m, n>(a, b, Matx_MatMulOp()); } template static inline Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b) { Matx<_Tp, m, 1> c(a, b, Matx_MatMulOp()); return (const Vec<_Tp, m>&)(c); } template static inline bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b) { for( int i = 0; i < m*n; i++ ) if( a.val[i] != b.val[i] ) return false; return true; } template static inline bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b) { return !(a == b); } //! @} ////////////////////////////// Vec out-of-class operators //////////////////////////////// //! @relates cv::Vec //! @{ template static inline Vec<_Tp1, cn>& operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b) { for( int i = 0; i < cn; i++ ) a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]); return a; } template static inline Vec<_Tp1, cn>& operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b) { for( int i = 0; i < cn; i++ ) a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]); return a; } template static inline Vec<_Tp, cn> operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b) { return Vec<_Tp, cn>(a, b, Matx_AddOp()); } template static inline Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b) { return Vec<_Tp, cn>(a, b, Matx_SubOp()); } template static inline Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, int alpha) { for( int i = 0; i < cn; i++ ) a[i] = saturate_cast<_Tp>(a[i]*alpha); return a; } template static inline Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, float alpha) { for( int i = 0; i < cn; i++ ) a[i] = saturate_cast<_Tp>(a[i]*alpha); return a; } template static inline Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, double alpha) { for( int i = 0; i < cn; i++ ) a[i] = saturate_cast<_Tp>(a[i]*alpha); return a; } template static inline Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, int alpha) { double ialpha = 1./alpha; for( int i = 0; i < cn; i++ ) a[i] = saturate_cast<_Tp>(a[i]*ialpha); return a; } template static inline Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, float alpha) { float ialpha = 1.f/alpha; for( int i = 0; i < cn; i++ ) a[i] = saturate_cast<_Tp>(a[i]*ialpha); return a; } template static inline Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, double alpha) { double ialpha = 1./alpha; for( int i = 0; i < cn; i++ ) a[i] = saturate_cast<_Tp>(a[i]*ialpha); return a; } template static inline Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, int alpha) { return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator * (int alpha, const Vec<_Tp, cn>& a) { return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, float alpha) { return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator * (float alpha, const Vec<_Tp, cn>& a) { return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, double alpha) { return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator * (double alpha, const Vec<_Tp, cn>& a) { return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, int alpha) { return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, float alpha) { return Vec<_Tp, cn>(a, 1.f/alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, double alpha) { return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp()); } template static inline Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a) { Vec<_Tp,cn> t; for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]); return t; } template inline Vec<_Tp, 4> operator * (const Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2) { return Vec<_Tp, 4>(saturate_cast<_Tp>(v1[0]*v2[0] - v1[1]*v2[1] - v1[2]*v2[2] - v1[3]*v2[3]), saturate_cast<_Tp>(v1[0]*v2[1] + v1[1]*v2[0] + v1[2]*v2[3] - v1[3]*v2[2]), saturate_cast<_Tp>(v1[0]*v2[2] - v1[1]*v2[3] + v1[2]*v2[0] + v1[3]*v2[1]), saturate_cast<_Tp>(v1[0]*v2[3] + v1[1]*v2[2] - v1[2]*v2[1] + v1[3]*v2[0])); } template inline Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2) { v1 = v1 * v2; return v1; } //! @} } // cv #endif // __OPENCV_CORE_MATX_HPP__