Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[platform/upstream/opencv.git] / modules / core / include / opencv2 / core / matx.hpp
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 #ifndef OPENCV_CORE_MATX_HPP
45 #define OPENCV_CORE_MATX_HPP
46
47 #ifndef __cplusplus
48 #  error matx.hpp header must be compiled as C++
49 #endif
50
51 #include "opencv2/core/cvdef.h"
52 #include "opencv2/core/base.hpp"
53 #include "opencv2/core/traits.hpp"
54 #include "opencv2/core/saturate.hpp"
55
56 #include <initializer_list>
57
58 namespace cv
59 {
60
61 //! @addtogroup core_basic
62 //! @{
63
64 ////////////////////////////// Small Matrix ///////////////////////////
65
66 //! @cond IGNORED
67 // FIXIT Remove this (especially CV_EXPORTS modifier)
68 struct CV_EXPORTS Matx_AddOp { Matx_AddOp() {} Matx_AddOp(const Matx_AddOp&) {} };
69 struct CV_EXPORTS Matx_SubOp { Matx_SubOp() {} Matx_SubOp(const Matx_SubOp&) {} };
70 struct CV_EXPORTS Matx_ScaleOp { Matx_ScaleOp() {} Matx_ScaleOp(const Matx_ScaleOp&) {} };
71 struct CV_EXPORTS Matx_MulOp { Matx_MulOp() {} Matx_MulOp(const Matx_MulOp&) {} };
72 struct CV_EXPORTS Matx_DivOp { Matx_DivOp() {} Matx_DivOp(const Matx_DivOp&) {} };
73 struct CV_EXPORTS Matx_MatMulOp { Matx_MatMulOp() {} Matx_MatMulOp(const Matx_MatMulOp&) {} };
74 struct CV_EXPORTS Matx_TOp { Matx_TOp() {} Matx_TOp(const Matx_TOp&) {} };
75 //! @endcond
76
77 /** @brief Template class for small matrices whose type and size are known at compilation time
78
79 If you need a more flexible type, use Mat . The elements of the matrix M are accessible using the
80 M(i,j) notation. Most of the common matrix operations (see also @ref MatrixExpressions ) are
81 available. To do an operation on Matx that is not implemented, you can easily convert the matrix to
82 Mat and backwards:
83 @code{.cpp}
84     Matx33f m(1, 2, 3,
85               4, 5, 6,
86               7, 8, 9);
87     cout << sum(Mat(m*m.t())) << endl;
88 @endcode
89 Except of the plain constructor which takes a list of elements, Matx can be initialized from a C-array:
90 @code{.cpp}
91     float values[] = { 1, 2, 3};
92     Matx31f m(values);
93 @endcode
94 In case if C++11 features are available, std::initializer_list can be also used to initialize Matx:
95 @code{.cpp}
96     Matx31f m = { 1, 2, 3};
97 @endcode
98  */
99 template<typename _Tp, int m, int n> class Matx
100 {
101 public:
102     enum {
103            rows     = m,
104            cols     = n,
105            channels = rows*cols,
106 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
107            depth    = traits::Type<_Tp>::value,
108            type     = CV_MAKETYPE(depth, channels),
109 #endif
110            shortdim = (m < n ? m : n)
111          };
112
113     typedef _Tp                           value_type;
114     typedef Matx<_Tp, m, n>               mat_type;
115     typedef Matx<_Tp, shortdim, 1> diag_type;
116
117     //! default constructor
118     Matx();
119
120     explicit Matx(_Tp v0); //!< 1x1 matrix
121     Matx(_Tp v0, _Tp v1); //!< 1x2 or 2x1 matrix
122     Matx(_Tp v0, _Tp v1, _Tp v2); //!< 1x3 or 3x1 matrix
123     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 1x4, 2x2 or 4x1 matrix
124     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 1x5 or 5x1 matrix
125     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 1x6, 2x3, 3x2 or 6x1 matrix
126     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 1x7 or 7x1 matrix
127     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 1x8, 2x4, 4x2 or 8x1 matrix
128     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 1x9, 3x3 or 9x1 matrix
129     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
130     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
131          _Tp v4, _Tp v5, _Tp v6, _Tp v7,
132          _Tp v8, _Tp v9, _Tp v10, _Tp v11); //!< 1x12, 2x6, 3x4, 4x3, 6x2 or 12x1 matrix
133     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
134          _Tp v4, _Tp v5, _Tp v6, _Tp v7,
135          _Tp v8, _Tp v9, _Tp v10, _Tp v11,
136          _Tp v12, _Tp v13); //!< 1x14, 2x7, 7x2 or 14x1 matrix
137     Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
138          _Tp v4, _Tp v5, _Tp v6, _Tp v7,
139          _Tp v8, _Tp v9, _Tp v10, _Tp v11,
140          _Tp v12, _Tp v13, _Tp v14, _Tp v15); //!< 1x16, 4x4 or 16x1 matrix
141     explicit Matx(const _Tp* vals); //!< initialize from a plain array
142
143     Matx(std::initializer_list<_Tp>); //!< initialize from an initializer list
144
145     static Matx all(_Tp alpha);
146     static Matx zeros();
147     static Matx ones();
148     static Matx eye();
149     static Matx diag(const diag_type& d);
150     static Matx randu(_Tp a, _Tp b);
151     static Matx randn(_Tp a, _Tp b);
152
153     //! dot product computed with the default precision
154     _Tp dot(const Matx<_Tp, m, n>& v) const;
155
156     //! dot product computed in double-precision arithmetics
157     double ddot(const Matx<_Tp, m, n>& v) const;
158
159     //! conversion to another data type
160     template<typename T2> operator Matx<T2, m, n>() const;
161
162     //! change the matrix shape
163     template<int m1, int n1> Matx<_Tp, m1, n1> reshape() const;
164
165     //! extract part of the matrix
166     template<int m1, int n1> Matx<_Tp, m1, n1> get_minor(int i, int j) const;
167
168     //! extract the matrix row
169     Matx<_Tp, 1, n> row(int i) const;
170
171     //! extract the matrix column
172     Matx<_Tp, m, 1> col(int i) const;
173
174     //! extract the matrix diagonal
175     diag_type diag() const;
176
177     //! transpose the matrix
178     Matx<_Tp, n, m> t() const;
179
180     //! invert the matrix
181     Matx<_Tp, n, m> inv(int method=DECOMP_LU, bool *p_is_ok = NULL) const;
182
183     //! solve linear system
184     template<int l> Matx<_Tp, n, l> solve(const Matx<_Tp, m, l>& rhs, int flags=DECOMP_LU) const;
185     Vec<_Tp, n> solve(const Vec<_Tp, m>& rhs, int method) const;
186
187     //! multiply two matrices element-wise
188     Matx<_Tp, m, n> mul(const Matx<_Tp, m, n>& a) const;
189
190     //! divide two matrices element-wise
191     Matx<_Tp, m, n> div(const Matx<_Tp, m, n>& a) const;
192
193     //! element access
194     const _Tp& operator ()(int i, int j) const;
195     _Tp& operator ()(int i, int j);
196
197     //! 1D element access
198     const _Tp& operator ()(int i) const;
199     _Tp& operator ()(int i);
200
201     Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp);
202     Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp);
203     template<typename _T2> Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp);
204     Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp);
205     Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp);
206     template<int l> Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp);
207     Matx(const Matx<_Tp, n, m>& a, Matx_TOp);
208
209     _Tp val[m*n]; //< matrix elements
210 };
211
212 typedef Matx<float, 1, 2> Matx12f;
213 typedef Matx<double, 1, 2> Matx12d;
214 typedef Matx<float, 1, 3> Matx13f;
215 typedef Matx<double, 1, 3> Matx13d;
216 typedef Matx<float, 1, 4> Matx14f;
217 typedef Matx<double, 1, 4> Matx14d;
218 typedef Matx<float, 1, 6> Matx16f;
219 typedef Matx<double, 1, 6> Matx16d;
220
221 typedef Matx<float, 2, 1> Matx21f;
222 typedef Matx<double, 2, 1> Matx21d;
223 typedef Matx<float, 3, 1> Matx31f;
224 typedef Matx<double, 3, 1> Matx31d;
225 typedef Matx<float, 4, 1> Matx41f;
226 typedef Matx<double, 4, 1> Matx41d;
227 typedef Matx<float, 6, 1> Matx61f;
228 typedef Matx<double, 6, 1> Matx61d;
229
230 typedef Matx<float, 2, 2> Matx22f;
231 typedef Matx<double, 2, 2> Matx22d;
232 typedef Matx<float, 2, 3> Matx23f;
233 typedef Matx<double, 2, 3> Matx23d;
234 typedef Matx<float, 3, 2> Matx32f;
235 typedef Matx<double, 3, 2> Matx32d;
236
237 typedef Matx<float, 3, 3> Matx33f;
238 typedef Matx<double, 3, 3> Matx33d;
239
240 typedef Matx<float, 3, 4> Matx34f;
241 typedef Matx<double, 3, 4> Matx34d;
242 typedef Matx<float, 4, 3> Matx43f;
243 typedef Matx<double, 4, 3> Matx43d;
244
245 typedef Matx<float, 4, 4> Matx44f;
246 typedef Matx<double, 4, 4> Matx44d;
247 typedef Matx<float, 6, 6> Matx66f;
248 typedef Matx<double, 6, 6> Matx66d;
249
250 /*!
251   traits
252 */
253 template<typename _Tp, int m, int n> class DataType< Matx<_Tp, m, n> >
254 {
255 public:
256     typedef Matx<_Tp, m, n>                               value_type;
257     typedef Matx<typename DataType<_Tp>::work_type, m, n> work_type;
258     typedef _Tp                                           channel_type;
259     typedef value_type                                    vec_type;
260
261     enum { generic_type = 0,
262            channels     = m * n,
263            fmt          = traits::SafeFmt<channel_type>::fmt + ((channels - 1) << 8)
264 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
265            ,depth        = DataType<channel_type>::depth
266            ,type         = CV_MAKETYPE(depth, channels)
267 #endif
268          };
269 };
270
271 namespace traits {
272 template<typename _Tp, int m, int n>
273 struct Depth< Matx<_Tp, m, n> > { enum { value = Depth<_Tp>::value }; };
274 template<typename _Tp, int m, int n>
275 struct Type< Matx<_Tp, m, n> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, n*m) }; };
276 } // namespace
277
278
279 /** @brief  Comma-separated Matrix Initializer
280 */
281 template<typename _Tp, int m, int n> class MatxCommaInitializer
282 {
283 public:
284     MatxCommaInitializer(Matx<_Tp, m, n>* _mtx);
285     template<typename T2> MatxCommaInitializer<_Tp, m, n>& operator , (T2 val);
286     Matx<_Tp, m, n> operator *() const;
287
288     Matx<_Tp, m, n>* dst;
289     int idx;
290 };
291
292 /*
293  Utility methods
294 */
295 template<typename _Tp, int m> static double determinant(const Matx<_Tp, m, m>& a);
296 template<typename _Tp, int m, int n> static double trace(const Matx<_Tp, m, n>& a);
297 template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M);
298 template<typename _Tp, int m, int n> static double norm(const Matx<_Tp, m, n>& M, int normType);
299
300
301
302 /////////////////////// Vec (used as element of multi-channel images /////////////////////
303
304 /** @brief Template class for short numerical vectors, a partial case of Matx
305
306 This template class represents short numerical vectors (of 1, 2, 3, 4 ... elements) on which you
307 can perform basic arithmetical operations, access individual elements using [] operator etc. The
308 vectors are allocated on stack, as opposite to std::valarray, std::vector, cv::Mat etc., which
309 elements are dynamically allocated in the heap.
310
311 The template takes 2 parameters:
312 @tparam _Tp element type
313 @tparam cn the number of elements
314
315 In addition to the universal notation like Vec<float, 3>, you can use shorter aliases
316 for the most popular specialized variants of Vec, e.g. Vec3f ~ Vec<float, 3>.
317
318 It is possible to convert Vec\<T,2\> to/from Point_, Vec\<T,3\> to/from Point3_ , and Vec\<T,4\>
319 to CvScalar or Scalar_. Use operator[] to access the elements of Vec.
320
321 All the expected vector operations are also implemented:
322 -   v1 = v2 + v3
323 -   v1 = v2 - v3
324 -   v1 = v2 \* scale
325 -   v1 = scale \* v2
326 -   v1 = -v2
327 -   v1 += v2 and other augmenting operations
328 -   v1 == v2, v1 != v2
329 -   norm(v1) (euclidean norm)
330 The Vec class is commonly used to describe pixel types of multi-channel arrays. See Mat for details.
331 */
332 template<typename _Tp, int cn> class Vec : public Matx<_Tp, cn, 1>
333 {
334 public:
335     typedef _Tp value_type;
336     enum {
337            channels = cn,
338 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
339            depth    = Matx<_Tp, cn, 1>::depth,
340            type     = CV_MAKETYPE(depth, channels),
341 #endif
342            _dummy_enum_finalizer = 0
343          };
344
345     //! default constructor
346     Vec();
347
348     Vec(_Tp v0); //!< 1-element vector constructor
349     Vec(_Tp v0, _Tp v1); //!< 2-element vector constructor
350     Vec(_Tp v0, _Tp v1, _Tp v2); //!< 3-element vector constructor
351     Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3); //!< 4-element vector constructor
352     Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4); //!< 5-element vector constructor
353     Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5); //!< 6-element vector constructor
354     Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6); //!< 7-element vector constructor
355     Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7); //!< 8-element vector constructor
356     Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8); //!< 9-element vector constructor
357     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
358     Vec(_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); //!< 14-element vector constructor
359     explicit Vec(const _Tp* values);
360
361     Vec(std::initializer_list<_Tp>);
362
363     Vec(const Vec<_Tp, cn>& v);
364
365     static Vec all(_Tp alpha);
366
367     //! per-element multiplication
368     Vec mul(const Vec<_Tp, cn>& v) const;
369
370     //! conjugation (makes sense for complex numbers and quaternions)
371     Vec conj() const;
372
373     /*!
374       cross product of the two 3D vectors.
375
376       For other dimensionalities the exception is raised
377     */
378     Vec cross(const Vec& v) const;
379     //! conversion to another data type
380     template<typename T2> operator Vec<T2, cn>() const;
381
382     /*! element access */
383     const _Tp& operator [](int i) const;
384     _Tp& operator[](int i);
385     const _Tp& operator ()(int i) const;
386     _Tp& operator ()(int i);
387
388     Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp);
389     Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp);
390     template<typename _T2> Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp);
391 };
392
393 /** @name Shorter aliases for the most popular specializations of Vec<T,n>
394   @{
395 */
396 typedef Vec<uchar, 2> Vec2b;
397 typedef Vec<uchar, 3> Vec3b;
398 typedef Vec<uchar, 4> Vec4b;
399
400 typedef Vec<short, 2> Vec2s;
401 typedef Vec<short, 3> Vec3s;
402 typedef Vec<short, 4> Vec4s;
403
404 typedef Vec<ushort, 2> Vec2w;
405 typedef Vec<ushort, 3> Vec3w;
406 typedef Vec<ushort, 4> Vec4w;
407
408 typedef Vec<int, 2> Vec2i;
409 typedef Vec<int, 3> Vec3i;
410 typedef Vec<int, 4> Vec4i;
411 typedef Vec<int, 6> Vec6i;
412 typedef Vec<int, 8> Vec8i;
413
414 typedef Vec<float, 2> Vec2f;
415 typedef Vec<float, 3> Vec3f;
416 typedef Vec<float, 4> Vec4f;
417 typedef Vec<float, 6> Vec6f;
418
419 typedef Vec<double, 2> Vec2d;
420 typedef Vec<double, 3> Vec3d;
421 typedef Vec<double, 4> Vec4d;
422 typedef Vec<double, 6> Vec6d;
423 /** @} */
424
425 /*!
426   traits
427 */
428 template<typename _Tp, int cn> class DataType< Vec<_Tp, cn> >
429 {
430 public:
431     typedef Vec<_Tp, cn>                               value_type;
432     typedef Vec<typename DataType<_Tp>::work_type, cn> work_type;
433     typedef _Tp                                        channel_type;
434     typedef value_type                                 vec_type;
435
436     enum { generic_type = 0,
437            channels     = cn,
438            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
439 #ifdef OPENCV_TRAITS_ENABLE_DEPRECATED
440            depth        = DataType<channel_type>::depth,
441            type         = CV_MAKETYPE(depth, channels),
442 #endif
443            _dummy_enum_finalizer = 0
444          };
445 };
446
447 namespace traits {
448 template<typename _Tp, int cn>
449 struct Depth< Vec<_Tp, cn> > { enum { value = Depth<_Tp>::value }; };
450 template<typename _Tp, int cn>
451 struct Type< Vec<_Tp, cn> > { enum { value = CV_MAKETYPE(Depth<_Tp>::value, cn) }; };
452 } // namespace
453
454
455 /** @brief  Comma-separated Vec Initializer
456 */
457 template<typename _Tp, int m> class VecCommaInitializer : public MatxCommaInitializer<_Tp, m, 1>
458 {
459 public:
460     VecCommaInitializer(Vec<_Tp, m>* _vec);
461     template<typename T2> VecCommaInitializer<_Tp, m>& operator , (T2 val);
462     Vec<_Tp, m> operator *() const;
463 };
464
465 template<typename _Tp, int cn> static Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v);
466
467 //! @} core_basic
468
469 //! @cond IGNORED
470
471 ///////////////////////////////////// helper classes /////////////////////////////////////
472 namespace internal
473 {
474
475 template<typename _Tp, int m> struct Matx_DetOp
476 {
477     double operator ()(const Matx<_Tp, m, m>& a) const
478     {
479         Matx<_Tp, m, m> temp = a;
480         double p = LU(temp.val, m*sizeof(_Tp), m, 0, 0, 0);
481         if( p == 0 )
482             return p;
483         for( int i = 0; i < m; i++ )
484             p *= temp(i, i);
485         return p;
486     }
487 };
488
489 template<typename _Tp> struct Matx_DetOp<_Tp, 1>
490 {
491     double operator ()(const Matx<_Tp, 1, 1>& a) const
492     {
493         return a(0,0);
494     }
495 };
496
497 template<typename _Tp> struct Matx_DetOp<_Tp, 2>
498 {
499     double operator ()(const Matx<_Tp, 2, 2>& a) const
500     {
501         return a(0,0)*a(1,1) - a(0,1)*a(1,0);
502     }
503 };
504
505 template<typename _Tp> struct Matx_DetOp<_Tp, 3>
506 {
507     double operator ()(const Matx<_Tp, 3, 3>& a) const
508     {
509         return a(0,0)*(a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
510             a(0,1)*(a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
511             a(0,2)*(a(1,0)*a(2,1) - a(2,0)*a(1,1));
512     }
513 };
514
515 template<typename _Tp> Vec<_Tp, 2> inline conjugate(const Vec<_Tp, 2>& v)
516 {
517     return Vec<_Tp, 2>(v[0], -v[1]);
518 }
519
520 template<typename _Tp> Vec<_Tp, 4> inline conjugate(const Vec<_Tp, 4>& v)
521 {
522     return Vec<_Tp, 4>(v[0], -v[1], -v[2], -v[3]);
523 }
524
525 } // internal
526
527
528
529 ////////////////////////////////// Matx Implementation ///////////////////////////////////
530
531 template<typename _Tp, int m, int n> inline
532 Matx<_Tp, m, n>::Matx()
533 {
534     for(int i = 0; i < channels; i++) val[i] = _Tp(0);
535 }
536
537 template<typename _Tp, int m, int n> inline
538 Matx<_Tp, m, n>::Matx(_Tp v0)
539 {
540     val[0] = v0;
541     for(int i = 1; i < channels; i++) val[i] = _Tp(0);
542 }
543
544 template<typename _Tp, int m, int n> inline
545 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1)
546 {
547     CV_StaticAssert(channels >= 2, "Matx should have at least 2 elements.");
548     val[0] = v0; val[1] = v1;
549     for(int i = 2; i < channels; i++) val[i] = _Tp(0);
550 }
551
552 template<typename _Tp, int m, int n> inline
553 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2)
554 {
555     CV_StaticAssert(channels >= 3, "Matx should have at least 3 elements.");
556     val[0] = v0; val[1] = v1; val[2] = v2;
557     for(int i = 3; i < channels; i++) val[i] = _Tp(0);
558 }
559
560 template<typename _Tp, int m, int n> inline
561 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
562 {
563     CV_StaticAssert(channels >= 4, "Matx should have at least 4 elements.");
564     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
565     for(int i = 4; i < channels; i++) val[i] = _Tp(0);
566 }
567
568 template<typename _Tp, int m, int n> inline
569 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
570 {
571     CV_StaticAssert(channels >= 5, "Matx should have at least 5 elements.");
572     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4;
573     for(int i = 5; i < channels; i++) val[i] = _Tp(0);
574 }
575
576 template<typename _Tp, int m, int n> inline
577 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
578 {
579     CV_StaticAssert(channels >= 6, "Matx should have at least 6 elements.");
580     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
581     val[4] = v4; val[5] = v5;
582     for(int i = 6; i < channels; i++) val[i] = _Tp(0);
583 }
584
585 template<typename _Tp, int m, int n> inline
586 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
587 {
588     CV_StaticAssert(channels >= 7, "Matx should have at least 7 elements.");
589     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
590     val[4] = v4; val[5] = v5; val[6] = v6;
591     for(int i = 7; i < channels; i++) val[i] = _Tp(0);
592 }
593
594 template<typename _Tp, int m, int n> inline
595 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
596 {
597     CV_StaticAssert(channels >= 8, "Matx should have at least 8 elements.");
598     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
599     val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
600     for(int i = 8; i < channels; i++) val[i] = _Tp(0);
601 }
602
603 template<typename _Tp, int m, int n> inline
604 Matx<_Tp, m, n>::Matx(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
605 {
606     CV_StaticAssert(channels >= 9, "Matx should have at least 9 elements.");
607     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
608     val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
609     val[8] = v8;
610     for(int i = 9; i < channels; i++) val[i] = _Tp(0);
611 }
612
613 template<typename _Tp, int m, int n> inline
614 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)
615 {
616     CV_StaticAssert(channels >= 10, "Matx should have at least 10 elements.");
617     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
618     val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
619     val[8] = v8; val[9] = v9;
620     for(int i = 10; i < channels; i++) val[i] = _Tp(0);
621 }
622
623
624 template<typename _Tp, int m, int n> inline
625 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)
626 {
627     CV_StaticAssert(channels >= 12, "Matx should have at least 12 elements.");
628     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
629     val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
630     val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
631     for(int i = 12; i < channels; i++) val[i] = _Tp(0);
632 }
633
634 template<typename _Tp, int m, int n> inline
635 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)
636 {
637     CV_StaticAssert(channels >= 14, "Matx should have at least 14 elements.");
638     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
639     val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
640     val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
641     val[12] = v12; val[13] = v13;
642     for (int i = 14; i < channels; i++) val[i] = _Tp(0);
643 }
644
645
646 template<typename _Tp, int m, int n> inline
647 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)
648 {
649     CV_StaticAssert(channels >= 16, "Matx should have at least 16 elements.");
650     val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
651     val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
652     val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
653     val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15;
654     for(int i = 16; i < channels; i++) val[i] = _Tp(0);
655 }
656
657 template<typename _Tp, int m, int n> inline
658 Matx<_Tp, m, n>::Matx(const _Tp* values)
659 {
660     for( int i = 0; i < channels; i++ ) val[i] = values[i];
661 }
662
663 template<typename _Tp, int m, int n> inline
664 Matx<_Tp, m, n>::Matx(std::initializer_list<_Tp> list)
665 {
666     CV_DbgAssert(list.size() == channels);
667     int i = 0;
668     for(const auto& elem : list)
669     {
670         val[i++] = elem;
671     }
672 }
673
674 template<typename _Tp, int m, int n> inline
675 Matx<_Tp, m, n> Matx<_Tp, m, n>::all(_Tp alpha)
676 {
677     Matx<_Tp, m, n> M;
678     for( int i = 0; i < m*n; i++ ) M.val[i] = alpha;
679     return M;
680 }
681
682 template<typename _Tp, int m, int n> inline
683 Matx<_Tp,m,n> Matx<_Tp,m,n>::zeros()
684 {
685     return all(0);
686 }
687
688 template<typename _Tp, int m, int n> inline
689 Matx<_Tp,m,n> Matx<_Tp,m,n>::ones()
690 {
691     return all(1);
692 }
693
694 template<typename _Tp, int m, int n> inline
695 Matx<_Tp,m,n> Matx<_Tp,m,n>::eye()
696 {
697     Matx<_Tp,m,n> M;
698     for(int i = 0; i < shortdim; i++)
699         M(i,i) = 1;
700     return M;
701 }
702
703 template<typename _Tp, int m, int n> inline
704 _Tp Matx<_Tp, m, n>::dot(const Matx<_Tp, m, n>& M) const
705 {
706     _Tp s = 0;
707     for( int i = 0; i < channels; i++ ) s += val[i]*M.val[i];
708     return s;
709 }
710
711 template<typename _Tp, int m, int n> inline
712 double Matx<_Tp, m, n>::ddot(const Matx<_Tp, m, n>& M) const
713 {
714     double s = 0;
715     for( int i = 0; i < channels; i++ ) s += (double)val[i]*M.val[i];
716     return s;
717 }
718
719 template<typename _Tp, int m, int n> inline
720 Matx<_Tp,m,n> Matx<_Tp,m,n>::diag(const typename Matx<_Tp,m,n>::diag_type& d)
721 {
722     Matx<_Tp,m,n> M;
723     for(int i = 0; i < shortdim; i++)
724         M(i,i) = d(i, 0);
725     return M;
726 }
727
728 template<typename _Tp, int m, int n> template<typename T2>
729 inline Matx<_Tp, m, n>::operator Matx<T2, m, n>() const
730 {
731     Matx<T2, m, n> M;
732     for( int i = 0; i < m*n; i++ ) M.val[i] = saturate_cast<T2>(val[i]);
733     return M;
734 }
735
736 template<typename _Tp, int m, int n> template<int m1, int n1> inline
737 Matx<_Tp, m1, n1> Matx<_Tp, m, n>::reshape() const
738 {
739     CV_StaticAssert(m1*n1 == m*n, "Input and destnarion matrices must have the same number of elements");
740     return (const Matx<_Tp, m1, n1>&)*this;
741 }
742
743 template<typename _Tp, int m, int n>
744 template<int m1, int n1> inline
745 Matx<_Tp, m1, n1> Matx<_Tp, m, n>::get_minor(int i, int j) const
746 {
747     CV_DbgAssert(0 <= i && i+m1 <= m && 0 <= j && j+n1 <= n);
748     Matx<_Tp, m1, n1> s;
749     for( int di = 0; di < m1; di++ )
750         for( int dj = 0; dj < n1; dj++ )
751             s(di, dj) = (*this)(i+di, j+dj);
752     return s;
753 }
754
755 template<typename _Tp, int m, int n> inline
756 Matx<_Tp, 1, n> Matx<_Tp, m, n>::row(int i) const
757 {
758     CV_DbgAssert((unsigned)i < (unsigned)m);
759     return Matx<_Tp, 1, n>(&val[i*n]);
760 }
761
762 template<typename _Tp, int m, int n> inline
763 Matx<_Tp, m, 1> Matx<_Tp, m, n>::col(int j) const
764 {
765     CV_DbgAssert((unsigned)j < (unsigned)n);
766     Matx<_Tp, m, 1> v;
767     for( int i = 0; i < m; i++ )
768         v.val[i] = val[i*n + j];
769     return v;
770 }
771
772 template<typename _Tp, int m, int n> inline
773 typename Matx<_Tp, m, n>::diag_type Matx<_Tp, m, n>::diag() const
774 {
775     diag_type d;
776     for( int i = 0; i < shortdim; i++ )
777         d.val[i] = val[i*n + i];
778     return d;
779 }
780
781 template<typename _Tp, int m, int n> inline
782 const _Tp& Matx<_Tp, m, n>::operator()(int i, int j) const
783 {
784     CV_DbgAssert( (unsigned)i < (unsigned)m && (unsigned)j < (unsigned)n );
785     return this->val[i*n + j];
786 }
787
788 template<typename _Tp, int m, int n> inline
789 _Tp& Matx<_Tp, m, n>::operator ()(int i, int j)
790 {
791     CV_DbgAssert( (unsigned)i < (unsigned)m && (unsigned)j < (unsigned)n );
792     return val[i*n + j];
793 }
794
795 template<typename _Tp, int m, int n> inline
796 const _Tp& Matx<_Tp, m, n>::operator ()(int i) const
797 {
798     CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
799     CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
800     return val[i];
801 }
802
803 template<typename _Tp, int m, int n> inline
804 _Tp& Matx<_Tp, m, n>::operator ()(int i)
805 {
806     CV_StaticAssert(m == 1 || n == 1, "Single index indexation requires matrix to be a column or a row");
807     CV_DbgAssert( (unsigned)i < (unsigned)(m+n-1) );
808     return val[i];
809 }
810
811 template<typename _Tp, int m, int n> inline
812 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_AddOp)
813 {
814     for( int i = 0; i < channels; i++ )
815         val[i] = saturate_cast<_Tp>(a.val[i] + b.val[i]);
816 }
817
818 template<typename _Tp, int m, int n> inline
819 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_SubOp)
820 {
821     for( int i = 0; i < channels; i++ )
822         val[i] = saturate_cast<_Tp>(a.val[i] - b.val[i]);
823 }
824
825 template<typename _Tp, int m, int n> template<typename _T2> inline
826 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, _T2 alpha, Matx_ScaleOp)
827 {
828     for( int i = 0; i < channels; i++ )
829         val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
830 }
831
832 template<typename _Tp, int m, int n> inline
833 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_MulOp)
834 {
835     for( int i = 0; i < channels; i++ )
836         val[i] = saturate_cast<_Tp>(a.val[i] * b.val[i]);
837 }
838
839 template<typename _Tp, int m, int n> inline
840 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b, Matx_DivOp)
841 {
842     for( int i = 0; i < channels; i++ )
843         val[i] = saturate_cast<_Tp>(a.val[i] / b.val[i]);
844 }
845
846 template<typename _Tp, int m, int n> template<int l> inline
847 Matx<_Tp,m,n>::Matx(const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b, Matx_MatMulOp)
848 {
849     for( int i = 0; i < m; i++ )
850         for( int j = 0; j < n; j++ )
851         {
852             _Tp s = 0;
853             for( int k = 0; k < l; k++ )
854                 s += a(i, k) * b(k, j);
855             val[i*n + j] = s;
856         }
857 }
858
859 template<typename _Tp, int m, int n> inline
860 Matx<_Tp,m,n>::Matx(const Matx<_Tp, n, m>& a, Matx_TOp)
861 {
862     for( int i = 0; i < m; i++ )
863         for( int j = 0; j < n; j++ )
864             val[i*n + j] = a(j, i);
865 }
866
867 template<typename _Tp, int m, int n> inline
868 Matx<_Tp, m, n> Matx<_Tp, m, n>::mul(const Matx<_Tp, m, n>& a) const
869 {
870     return Matx<_Tp, m, n>(*this, a, Matx_MulOp());
871 }
872
873 template<typename _Tp, int m, int n> inline
874 Matx<_Tp, m, n> Matx<_Tp, m, n>::div(const Matx<_Tp, m, n>& a) const
875 {
876     return Matx<_Tp, m, n>(*this, a, Matx_DivOp());
877 }
878
879 template<typename _Tp, int m, int n> inline
880 Matx<_Tp, n, m> Matx<_Tp, m, n>::t() const
881 {
882     return Matx<_Tp, n, m>(*this, Matx_TOp());
883 }
884
885 template<typename _Tp, int m, int n> inline
886 Vec<_Tp, n> Matx<_Tp, m, n>::solve(const Vec<_Tp, m>& rhs, int method) const
887 {
888     Matx<_Tp, n, 1> x = solve((const Matx<_Tp, m, 1>&)(rhs), method);
889     return (Vec<_Tp, n>&)(x);
890 }
891
892 template<typename _Tp, int m> static inline
893 double determinant(const Matx<_Tp, m, m>& a)
894 {
895     return cv::internal::Matx_DetOp<_Tp, m>()(a);
896 }
897
898 template<typename _Tp, int m, int n> static inline
899 double trace(const Matx<_Tp, m, n>& a)
900 {
901     _Tp s = 0;
902     for( int i = 0; i < std::min(m, n); i++ )
903         s += a(i,i);
904     return s;
905 }
906
907 template<typename _Tp, int m, int n> static inline
908 double norm(const Matx<_Tp, m, n>& M)
909 {
910     return std::sqrt(normL2Sqr<_Tp, double>(M.val, m*n));
911 }
912
913 template<typename _Tp, int m, int n> static inline
914 double norm(const Matx<_Tp, m, n>& M, int normType)
915 {
916     switch(normType) {
917     case NORM_INF:
918         return (double)normInf<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
919     case NORM_L1:
920         return (double)normL1<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
921     case NORM_L2SQR:
922         return (double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n);
923     default:
924     case NORM_L2:
925         return std::sqrt((double)normL2Sqr<_Tp, typename DataType<_Tp>::work_type>(M.val, m*n));
926     }
927 }
928
929
930
931 //////////////////////////////// matx comma initializer //////////////////////////////////
932
933 template<typename _Tp, typename _T2, int m, int n> static inline
934 MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val)
935 {
936     MatxCommaInitializer<_Tp, m, n> commaInitializer((Matx<_Tp, m, n>*)&mtx);
937     return (commaInitializer, val);
938 }
939
940 template<typename _Tp, int m, int n> inline
941 MatxCommaInitializer<_Tp, m, n>::MatxCommaInitializer(Matx<_Tp, m, n>* _mtx)
942     : dst(_mtx), idx(0)
943 {}
944
945 template<typename _Tp, int m, int n> template<typename _T2> inline
946 MatxCommaInitializer<_Tp, m, n>& MatxCommaInitializer<_Tp, m, n>::operator , (_T2 value)
947 {
948     CV_DbgAssert( idx < m*n );
949     dst->val[idx++] = saturate_cast<_Tp>(value);
950     return *this;
951 }
952
953 template<typename _Tp, int m, int n> inline
954 Matx<_Tp, m, n> MatxCommaInitializer<_Tp, m, n>::operator *() const
955 {
956     CV_DbgAssert( idx == n*m );
957     return *dst;
958 }
959
960
961
962 /////////////////////////////////// Vec Implementation ///////////////////////////////////
963
964 template<typename _Tp, int cn> inline
965 Vec<_Tp, cn>::Vec() {}
966
967 template<typename _Tp, int cn> inline
968 Vec<_Tp, cn>::Vec(_Tp v0)
969     : Matx<_Tp, cn, 1>(v0) {}
970
971 template<typename _Tp, int cn> inline
972 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1)
973     : Matx<_Tp, cn, 1>(v0, v1) {}
974
975 template<typename _Tp, int cn> inline
976 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
977     : Matx<_Tp, cn, 1>(v0, v1, v2) {}
978
979 template<typename _Tp, int cn> inline
980 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
981     : Matx<_Tp, cn, 1>(v0, v1, v2, v3) {}
982
983 template<typename _Tp, int cn> inline
984 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
985     : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4) {}
986
987 template<typename _Tp, int cn> inline
988 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
989     : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5) {}
990
991 template<typename _Tp, int cn> inline
992 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6)
993     : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6) {}
994
995 template<typename _Tp, int cn> inline
996 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7)
997     : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7) {}
998
999 template<typename _Tp, int cn> inline
1000 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8)
1001     : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8) {}
1002
1003 template<typename _Tp, int cn> inline
1004 Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5, _Tp v6, _Tp v7, _Tp v8, _Tp v9)
1005     : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {}
1006
1007 template<typename _Tp, int cn> inline
1008 Vec<_Tp, cn>::Vec(_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)
1009     : Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13) {}
1010
1011 template<typename _Tp, int cn> inline
1012 Vec<_Tp, cn>::Vec(const _Tp* values)
1013     : Matx<_Tp, cn, 1>(values) {}
1014
1015 template<typename _Tp, int cn> inline
1016 Vec<_Tp, cn>::Vec(std::initializer_list<_Tp> list)
1017     : Matx<_Tp, cn, 1>(list) {}
1018
1019 template<typename _Tp, int cn> inline
1020 Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& m)
1021     : Matx<_Tp, cn, 1>(m.val) {}
1022
1023 template<typename _Tp, int cn> inline
1024 Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_AddOp op)
1025     : Matx<_Tp, cn, 1>(a, b, op) {}
1026
1027 template<typename _Tp, int cn> inline
1028 Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, const Matx<_Tp, cn, 1>& b, Matx_SubOp op)
1029     : Matx<_Tp, cn, 1>(a, b, op) {}
1030
1031 template<typename _Tp, int cn> template<typename _T2> inline
1032 Vec<_Tp, cn>::Vec(const Matx<_Tp, cn, 1>& a, _T2 alpha, Matx_ScaleOp op)
1033     : Matx<_Tp, cn, 1>(a, alpha, op) {}
1034
1035 template<typename _Tp, int cn> inline
1036 Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha)
1037 {
1038     Vec v;
1039     for( int i = 0; i < cn; i++ ) v.val[i] = alpha;
1040     return v;
1041 }
1042
1043 template<typename _Tp, int cn> inline
1044 Vec<_Tp, cn> Vec<_Tp, cn>::mul(const Vec<_Tp, cn>& v) const
1045 {
1046     Vec<_Tp, cn> w;
1047     for( int i = 0; i < cn; i++ ) w.val[i] = saturate_cast<_Tp>(this->val[i]*v.val[i]);
1048     return w;
1049 }
1050
1051 template<> inline
1052 Vec<float, 2> Vec<float, 2>::conj() const
1053 {
1054     return cv::internal::conjugate(*this);
1055 }
1056
1057 template<> inline
1058 Vec<double, 2> Vec<double, 2>::conj() const
1059 {
1060     return cv::internal::conjugate(*this);
1061 }
1062
1063 template<> inline
1064 Vec<float, 4> Vec<float, 4>::conj() const
1065 {
1066     return cv::internal::conjugate(*this);
1067 }
1068
1069 template<> inline
1070 Vec<double, 4> Vec<double, 4>::conj() const
1071 {
1072     return cv::internal::conjugate(*this);
1073 }
1074
1075 template<typename _Tp, int cn> inline
1076 Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>&) const
1077 {
1078     CV_StaticAssert(cn == 3, "for arbitrary-size vector there is no cross-product defined");
1079     return Vec<_Tp, cn>();
1080 }
1081
1082 template<> inline
1083 Vec<float, 3> Vec<float, 3>::cross(const Vec<float, 3>& v) const
1084 {
1085     return Vec<float,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
1086                      this->val[2]*v.val[0] - this->val[0]*v.val[2],
1087                      this->val[0]*v.val[1] - this->val[1]*v.val[0]);
1088 }
1089
1090 template<> inline
1091 Vec<double, 3> Vec<double, 3>::cross(const Vec<double, 3>& v) const
1092 {
1093     return Vec<double,3>(this->val[1]*v.val[2] - this->val[2]*v.val[1],
1094                      this->val[2]*v.val[0] - this->val[0]*v.val[2],
1095                      this->val[0]*v.val[1] - this->val[1]*v.val[0]);
1096 }
1097
1098 template<typename _Tp, int cn> template<typename T2> inline
1099 Vec<_Tp, cn>::operator Vec<T2, cn>() const
1100 {
1101     Vec<T2, cn> v;
1102     for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast<T2>(this->val[i]);
1103     return v;
1104 }
1105
1106 template<typename _Tp, int cn> inline
1107 const _Tp& Vec<_Tp, cn>::operator [](int i) const
1108 {
1109     CV_DbgAssert( (unsigned)i < (unsigned)cn );
1110     return this->val[i];
1111 }
1112
1113 template<typename _Tp, int cn> inline
1114 _Tp& Vec<_Tp, cn>::operator [](int i)
1115 {
1116     CV_DbgAssert( (unsigned)i < (unsigned)cn );
1117     return this->val[i];
1118 }
1119
1120 template<typename _Tp, int cn> inline
1121 const _Tp& Vec<_Tp, cn>::operator ()(int i) const
1122 {
1123     CV_DbgAssert( (unsigned)i < (unsigned)cn );
1124     return this->val[i];
1125 }
1126
1127 template<typename _Tp, int cn> inline
1128 _Tp& Vec<_Tp, cn>::operator ()(int i)
1129 {
1130     CV_DbgAssert( (unsigned)i < (unsigned)cn );
1131     return this->val[i];
1132 }
1133
1134 template<typename _Tp, int cn> inline
1135 Vec<_Tp, cn> normalize(const Vec<_Tp, cn>& v)
1136 {
1137     double nv = norm(v);
1138     return v * (nv ? 1./nv : 0.);
1139 }
1140
1141
1142
1143 //////////////////////////////// vec comma initializer //////////////////////////////////
1144
1145
1146 template<typename _Tp, typename _T2, int cn> static inline
1147 VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val)
1148 {
1149     VecCommaInitializer<_Tp, cn> commaInitializer((Vec<_Tp, cn>*)&vec);
1150     return (commaInitializer, val);
1151 }
1152
1153 template<typename _Tp, int cn> inline
1154 VecCommaInitializer<_Tp, cn>::VecCommaInitializer(Vec<_Tp, cn>* _vec)
1155     : MatxCommaInitializer<_Tp, cn, 1>(_vec)
1156 {}
1157
1158 template<typename _Tp, int cn> template<typename _T2> inline
1159 VecCommaInitializer<_Tp, cn>& VecCommaInitializer<_Tp, cn>::operator , (_T2 value)
1160 {
1161     CV_DbgAssert( this->idx < cn );
1162     this->dst->val[this->idx++] = saturate_cast<_Tp>(value);
1163     return *this;
1164 }
1165
1166 template<typename _Tp, int cn> inline
1167 Vec<_Tp, cn> VecCommaInitializer<_Tp, cn>::operator *() const
1168 {
1169     CV_DbgAssert( this->idx == cn );
1170     return *this->dst;
1171 }
1172
1173 //! @endcond
1174
1175 ///////////////////////////// Matx out-of-class operators ////////////////////////////////
1176
1177 //! @relates cv::Matx
1178 //! @{
1179
1180 template<typename _Tp1, typename _Tp2, int m, int n> static inline
1181 Matx<_Tp1, m, n>& operator += (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
1182 {
1183     for( int i = 0; i < m*n; i++ )
1184         a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
1185     return a;
1186 }
1187
1188 template<typename _Tp1, typename _Tp2, int m, int n> static inline
1189 Matx<_Tp1, m, n>& operator -= (Matx<_Tp1, m, n>& a, const Matx<_Tp2, m, n>& b)
1190 {
1191     for( int i = 0; i < m*n; i++ )
1192         a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
1193     return a;
1194 }
1195
1196 template<typename _Tp, int m, int n> static inline
1197 Matx<_Tp, m, n> operator + (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1198 {
1199     return Matx<_Tp, m, n>(a, b, Matx_AddOp());
1200 }
1201
1202 template<typename _Tp, int m, int n> static inline
1203 Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1204 {
1205     return Matx<_Tp, m, n>(a, b, Matx_SubOp());
1206 }
1207
1208 template<typename _Tp, int m, int n> static inline
1209 Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, int alpha)
1210 {
1211     for( int i = 0; i < m*n; i++ )
1212         a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
1213     return a;
1214 }
1215
1216 template<typename _Tp, int m, int n> static inline
1217 Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, float alpha)
1218 {
1219     for( int i = 0; i < m*n; i++ )
1220         a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
1221     return a;
1222 }
1223
1224 template<typename _Tp, int m, int n> static inline
1225 Matx<_Tp, m, n>& operator *= (Matx<_Tp, m, n>& a, double alpha)
1226 {
1227     for( int i = 0; i < m*n; i++ )
1228         a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
1229     return a;
1230 }
1231
1232 template<typename _Tp, int m, int n> static inline
1233 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, int alpha)
1234 {
1235     return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1236 }
1237
1238 template<typename _Tp, int m, int n> static inline
1239 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, float alpha)
1240 {
1241     return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1242 }
1243
1244 template<typename _Tp, int m, int n> static inline
1245 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, n>& a, double alpha)
1246 {
1247     return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1248 }
1249
1250 template<typename _Tp, int m, int n> static inline
1251 Matx<_Tp, m, n> operator * (int alpha, const Matx<_Tp, m, n>& a)
1252 {
1253     return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1254 }
1255
1256 template<typename _Tp, int m, int n> static inline
1257 Matx<_Tp, m, n> operator * (float alpha, const Matx<_Tp, m, n>& a)
1258 {
1259     return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1260 }
1261
1262 template<typename _Tp, int m, int n> static inline
1263 Matx<_Tp, m, n> operator * (double alpha, const Matx<_Tp, m, n>& a)
1264 {
1265     return Matx<_Tp, m, n>(a, alpha, Matx_ScaleOp());
1266 }
1267
1268 template<typename _Tp, int m, int n> static inline
1269 Matx<_Tp, m, n> operator - (const Matx<_Tp, m, n>& a)
1270 {
1271     return Matx<_Tp, m, n>(a, -1, Matx_ScaleOp());
1272 }
1273
1274 template<typename _Tp, int m, int n, int l> static inline
1275 Matx<_Tp, m, n> operator * (const Matx<_Tp, m, l>& a, const Matx<_Tp, l, n>& b)
1276 {
1277     return Matx<_Tp, m, n>(a, b, Matx_MatMulOp());
1278 }
1279
1280 template<typename _Tp, int m, int n> static inline
1281 Vec<_Tp, m> operator * (const Matx<_Tp, m, n>& a, const Vec<_Tp, n>& b)
1282 {
1283     Matx<_Tp, m, 1> c(a, b, Matx_MatMulOp());
1284     return (const Vec<_Tp, m>&)(c);
1285 }
1286
1287 template<typename _Tp, int m, int n> static inline
1288 bool operator == (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1289 {
1290     for( int i = 0; i < m*n; i++ )
1291         if( a.val[i] != b.val[i] ) return false;
1292     return true;
1293 }
1294
1295 template<typename _Tp, int m, int n> static inline
1296 bool operator != (const Matx<_Tp, m, n>& a, const Matx<_Tp, m, n>& b)
1297 {
1298     return !(a == b);
1299 }
1300
1301 //! @}
1302
1303 ////////////////////////////// Vec out-of-class operators ////////////////////////////////
1304
1305 //! @relates cv::Vec
1306 //! @{
1307
1308 template<typename _Tp1, typename _Tp2, int cn> static inline
1309 Vec<_Tp1, cn>& operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
1310 {
1311     for( int i = 0; i < cn; i++ )
1312         a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
1313     return a;
1314 }
1315
1316 template<typename _Tp1, typename _Tp2, int cn> static inline
1317 Vec<_Tp1, cn>& operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
1318 {
1319     for( int i = 0; i < cn; i++ )
1320         a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
1321     return a;
1322 }
1323
1324 template<typename _Tp, int cn> static inline
1325 Vec<_Tp, cn> operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
1326 {
1327     return Vec<_Tp, cn>(a, b, Matx_AddOp());
1328 }
1329
1330 template<typename _Tp, int cn> static inline
1331 Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
1332 {
1333     return Vec<_Tp, cn>(a, b, Matx_SubOp());
1334 }
1335
1336 template<typename _Tp, int cn> static inline
1337 Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, int alpha)
1338 {
1339     for( int i = 0; i < cn; i++ )
1340         a[i] = saturate_cast<_Tp>(a[i]*alpha);
1341     return a;
1342 }
1343
1344 template<typename _Tp, int cn> static inline
1345 Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, float alpha)
1346 {
1347     for( int i = 0; i < cn; i++ )
1348         a[i] = saturate_cast<_Tp>(a[i]*alpha);
1349     return a;
1350 }
1351
1352 template<typename _Tp, int cn> static inline
1353 Vec<_Tp, cn>& operator *= (Vec<_Tp, cn>& a, double alpha)
1354 {
1355     for( int i = 0; i < cn; i++ )
1356         a[i] = saturate_cast<_Tp>(a[i]*alpha);
1357     return a;
1358 }
1359
1360 template<typename _Tp, int cn> static inline
1361 Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, int alpha)
1362 {
1363     double ialpha = 1./alpha;
1364     for( int i = 0; i < cn; i++ )
1365         a[i] = saturate_cast<_Tp>(a[i]*ialpha);
1366     return a;
1367 }
1368
1369 template<typename _Tp, int cn> static inline
1370 Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, float alpha)
1371 {
1372     float ialpha = 1.f/alpha;
1373     for( int i = 0; i < cn; i++ )
1374         a[i] = saturate_cast<_Tp>(a[i]*ialpha);
1375     return a;
1376 }
1377
1378 template<typename _Tp, int cn> static inline
1379 Vec<_Tp, cn>& operator /= (Vec<_Tp, cn>& a, double alpha)
1380 {
1381     double ialpha = 1./alpha;
1382     for( int i = 0; i < cn; i++ )
1383         a[i] = saturate_cast<_Tp>(a[i]*ialpha);
1384     return a;
1385 }
1386
1387 template<typename _Tp, int cn> static inline
1388 Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, int alpha)
1389 {
1390     return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1391 }
1392
1393 template<typename _Tp, int cn> static inline
1394 Vec<_Tp, cn> operator * (int alpha, const Vec<_Tp, cn>& a)
1395 {
1396     return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1397 }
1398
1399 template<typename _Tp, int cn> static inline
1400 Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, float alpha)
1401 {
1402     return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1403 }
1404
1405 template<typename _Tp, int cn> static inline
1406 Vec<_Tp, cn> operator * (float alpha, const Vec<_Tp, cn>& a)
1407 {
1408     return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1409 }
1410
1411 template<typename _Tp, int cn> static inline
1412 Vec<_Tp, cn> operator * (const Vec<_Tp, cn>& a, double alpha)
1413 {
1414     return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1415 }
1416
1417 template<typename _Tp, int cn> static inline
1418 Vec<_Tp, cn> operator * (double alpha, const Vec<_Tp, cn>& a)
1419 {
1420     return Vec<_Tp, cn>(a, alpha, Matx_ScaleOp());
1421 }
1422
1423 template<typename _Tp, int cn> static inline
1424 Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, int alpha)
1425 {
1426     return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
1427 }
1428
1429 template<typename _Tp, int cn> static inline
1430 Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, float alpha)
1431 {
1432     return Vec<_Tp, cn>(a, 1.f/alpha, Matx_ScaleOp());
1433 }
1434
1435 template<typename _Tp, int cn> static inline
1436 Vec<_Tp, cn> operator / (const Vec<_Tp, cn>& a, double alpha)
1437 {
1438     return Vec<_Tp, cn>(a, 1./alpha, Matx_ScaleOp());
1439 }
1440
1441 template<typename _Tp, int cn> static inline
1442 Vec<_Tp, cn> operator - (const Vec<_Tp, cn>& a)
1443 {
1444     Vec<_Tp,cn> t;
1445     for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]);
1446     return t;
1447 }
1448
1449 template<typename _Tp> inline Vec<_Tp, 4> operator * (const Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
1450 {
1451     return Vec<_Tp, 4>(saturate_cast<_Tp>(v1[0]*v2[0] - v1[1]*v2[1] - v1[2]*v2[2] - v1[3]*v2[3]),
1452                        saturate_cast<_Tp>(v1[0]*v2[1] + v1[1]*v2[0] + v1[2]*v2[3] - v1[3]*v2[2]),
1453                        saturate_cast<_Tp>(v1[0]*v2[2] - v1[1]*v2[3] + v1[2]*v2[0] + v1[3]*v2[1]),
1454                        saturate_cast<_Tp>(v1[0]*v2[3] + v1[1]*v2[2] - v1[2]*v2[1] + v1[3]*v2[0]));
1455 }
1456
1457 template<typename _Tp> inline Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& v1, const Vec<_Tp, 4>& v2)
1458 {
1459     v1 = v1 * v2;
1460     return v1;
1461 }
1462
1463 //! @}
1464
1465 } // cv
1466
1467 #endif // OPENCV_CORE_MATX_HPP