matx.hpp 45 KB

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