43 #ifndef __OPENCV_CORE_OPERATIONS_HPP__
44 #define __OPENCV_CORE_OPERATIONS_HPP__
49 #endif // SKIP_INCLUDES
56 #if __GNUC__*10 + __GNUC_MINOR__ >= 42
58 #if !defined WIN32 && (defined __i486__ || defined __i586__ || \
59 defined __i686__ || defined __MMX__ || defined __SSE__ || defined __ppc__)
60 #define CV_XADD __sync_fetch_and_add
62 #include <ext/atomicity.h>
63 #define CV_XADD __gnu_cxx::__exchange_and_add
67 #include <bits/atomicity.h>
68 #if __GNUC__*10 + __GNUC_MINOR__ >= 34
69 #define CV_XADD __gnu_cxx::__exchange_and_add
71 #define CV_XADD __exchange_and_add
75 #elif defined WIN32 || defined _WIN32
77 #if defined _MSC_VER && defined _M_IX86
78 static inline int CV_XADD(
int* addr,
int delta )
94 #define CV_XADD(addr,delta) InterlockedExchangeAdd((LONG volatile*)(addr), (delta))
99 template<
typename _Tp>
static inline _Tp CV_XADD(_Tp* addr, _Tp
delta)
100 {
int tmp = *addr; *addr +=
delta;
return tmp; }
121 template<
typename _Tp>
static inline _Tp saturate_cast(
uchar v) {
return _Tp(v); }
122 template<
typename _Tp>
static inline _Tp saturate_cast(
schar v) {
return _Tp(v); }
123 template<
typename _Tp>
static inline _Tp saturate_cast(
ushort v) {
return _Tp(v); }
124 template<
typename _Tp>
static inline _Tp saturate_cast(
short v) {
return _Tp(v); }
125 template<
typename _Tp>
static inline _Tp saturate_cast(
unsigned v) {
return _Tp(v); }
126 template<
typename _Tp>
static inline _Tp saturate_cast(
int v) {
return _Tp(v); }
127 template<
typename _Tp>
static inline _Tp saturate_cast(
float v) {
return _Tp(v); }
128 template<
typename _Tp>
static inline _Tp saturate_cast(
double v) {
return _Tp(v); }
133 {
return (
uchar)
std::min((
unsigned)v, (
unsigned)UCHAR_MAX); }
135 {
return (
uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
137 {
return saturate_cast<
uchar>((int)v); }
141 {
int iv =
cvRound(v);
return saturate_cast<
uchar>(iv); }
143 {
int iv =
cvRound(v);
return saturate_cast<
uchar>(iv); }
148 {
return (
schar)
std::min((
unsigned)v, (
unsigned)SCHAR_MAX); }
151 return (
schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ?
152 v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
155 {
return saturate_cast<
schar>((int)v); }
160 {
int iv =
cvRound(v);
return saturate_cast<
schar>(iv); }
162 {
int iv =
cvRound(v);
return saturate_cast<
schar>(iv); }
169 {
return (
ushort)((unsigned)v <= (
unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }
177 template<>
inline short saturate_cast<
short>(
ushort v)
178 {
return (
short)
std::min((
int)v, SHRT_MAX); }
179 template<>
inline short saturate_cast<
short>(
int v)
181 return (
short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ?
182 v : v > 0 ? SHRT_MAX : SHRT_MIN);
184 template<>
inline short saturate_cast<
short>(
unsigned v)
185 {
return (
short)
std::min(v, (
unsigned)SHRT_MAX); }
186 template<>
inline short saturate_cast<
short>(
float v)
187 {
int iv =
cvRound(v);
return saturate_cast<
short>(iv); }
188 template<>
inline short saturate_cast<
short>(
double v)
189 {
int iv =
cvRound(v);
return saturate_cast<
short>(iv); }
191 template<>
inline int saturate_cast<
int>(
float v) {
return cvRound(v); }
192 template<>
inline int saturate_cast<
int>(
double v) {
return cvRound(v); }
195 template<>
inline unsigned saturate_cast<
unsigned>(
float v){
return cvRound(v); }
196 template<>
inline unsigned saturate_cast<
unsigned>(
double v) {
return cvRound(v); }
204 for(
int i = 0; i < channels; i++) val[i] = _Tp(0);
210 for(
int i = 1; i < channels; i++) val[i] = _Tp(0);
215 assert(channels >= 2);
216 val[0] = v0; val[1] = v1;
217 for(
int i = 2; i < channels; i++) val[i] = _Tp(0);
222 assert(channels >= 3);
223 val[0] = v0; val[1] = v1; val[2] = v2;
224 for(
int i = 3; i < channels; i++) val[i] = _Tp(0);
229 assert(channels >= 4);
230 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
231 for(
int i = 4; i < channels; i++) val[i] = _Tp(0);
236 assert(channels >= 5);
237 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4;
238 for(
int i = 5; i < channels; i++) val[i] = _Tp(0);
244 assert(channels >= 6);
245 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
246 val[4] = v4; val[5] = v5;
247 for(
int i = 6; i < channels; i++) val[i] = _Tp(0);
251 _Tp v4, _Tp v5, _Tp v6)
253 assert(channels >= 7);
254 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
255 val[4] = v4; val[5] = v5; val[6] = v6;
256 for(
int i = 7; i < channels; i++) val[i] = _Tp(0);
260 _Tp v4, _Tp v5, _Tp v6, _Tp v7)
262 assert(channels >= 8);
263 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
264 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
265 for(
int i = 8; i < channels; i++) val[i] = _Tp(0);
269 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
272 assert(channels >= 9);
273 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
274 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
276 for(
int i = 9; i < channels; i++) val[i] = _Tp(0);
280 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
283 assert(channels >= 10);
284 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
285 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
286 val[8] = v8; val[9] = v9;
287 for(
int i = 10; i < channels; i++) val[i] = _Tp(0);
291 template<
typename _Tp,
int m,
int n>
293 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
294 _Tp v8, _Tp v9, _Tp v10, _Tp v11)
296 assert(channels == 12);
297 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
298 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
299 val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
302 template<
typename _Tp,
int m,
int n>
304 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
305 _Tp v8, _Tp v9, _Tp v10, _Tp v11,
306 _Tp v12, _Tp v13, _Tp v14, _Tp v15)
308 assert(channels == 16);
309 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
310 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
311 val[8] = v8; val[9] = v9; val[10] = v10; val[11] = v11;
312 val[12] = v12; val[13] = v13; val[14] = v14; val[15] = v15;
317 for(
int i = 0; i < channels; i++ ) val[i] = values[i];
323 for(
int i = 0; i < m*n; i++ ) M.
val[i] = alpha;
327 template<
typename _Tp,
int m,
int n>
inline
333 template<
typename _Tp,
int m,
int n>
inline
339 template<
typename _Tp,
int m,
int n>
inline
343 for(
int i = 0; i <
MIN(m,n); i++)
351 for(
int i = 0; i < m*n; i++ ) s += val[i]*M.
val[i];
359 for(
int i = 0; i < m*n; i++ ) s += (
double)val[i]*M.
val[i];
365 template<
typename _Tp,
int m,
int n>
inline
369 for(
int i = 0; i <
MIN(m,n); i++)
374 template<
typename _Tp,
int m,
int n>
inline
383 template<
typename _Tp,
int m,
int n>
inline
392 template<
typename _Tp,
int m,
int n>
template<
typename T2>
396 for(
int i = 0; i < m*n; i++ ) M.
val[i] = saturate_cast<T2>(val[i]);
401 template<
typename _Tp,
int m,
int n>
template<
int m1,
int n1>
inline
409 template<
typename _Tp,
int m,
int n>
410 template<
int m1,
int n1>
inline
413 CV_DbgAssert(0 <= i && i+m1 <= m && 0 <= j && j+n1 <= n);
415 for(
int di = 0; di < m1; di++ )
416 for(
int dj = 0; dj < n1; dj++ )
417 s(di, dj) = (*this)(i+di, j+dj);
422 template<
typename _Tp,
int m,
int n>
inline
430 template<
typename _Tp,
int m,
int n>
inline
435 for(
int i = 0; i < m; i++ )
441 template<
typename _Tp,
int m,
int n>
inline
445 for(
int i = 0; i <
MIN(m, n); i++ )
446 d.
val[i] = val[i*n + i];
451 template<
typename _Tp,
int m,
int n>
inline
454 CV_DbgAssert( (
unsigned)i < (
unsigned)m && (
unsigned)j < (
unsigned)n );
455 return this->val[i*n + j];
459 template<
typename _Tp,
int m,
int n>
inline
462 CV_DbgAssert( (
unsigned)i < (
unsigned)m && (
unsigned)j < (
unsigned)n );
467 template<
typename _Tp,
int m,
int n>
inline
470 CV_DbgAssert( (m == 1 || n == 1) && (
unsigned)i < (
unsigned)(m+n-1) );
475 template<
typename _Tp,
int m,
int n>
inline
478 CV_DbgAssert( (m == 1 || n == 1) && (
unsigned)i < (
unsigned)(m+n-1) );
483 template<
typename _Tp1,
typename _Tp2,
int m,
int n>
static inline
486 for(
int i = 0; i < m*n; i++ )
487 a.
val[i] = saturate_cast<_Tp1>(a.
val[i] + b.
val[i]);
492 template<
typename _Tp1,
typename _Tp2,
int m,
int n>
static inline
495 for(
int i = 0; i < m*n; i++ )
496 a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
501 template<
typename _Tp,
int m,
int n>
inline
504 for(
int i = 0; i < m*n; i++ )
505 val[i] = saturate_cast<_Tp>(a.
val[i] + b.
val[i]);
509 template<
typename _Tp,
int m,
int n>
inline
512 for(
int i = 0; i < m*n; i++ )
513 val[i] = saturate_cast<_Tp>(a.
val[i] - b.
val[i]);
517 template<
typename _Tp,
int m,
int n>
template<
typename _T2>
inline
520 for(
int i = 0; i < m*n; i++ )
521 val[i] = saturate_cast<_Tp>(a.
val[i] * alpha);
525 template<
typename _Tp,
int m,
int n>
inline
528 for(
int i = 0; i < m*n; i++ )
529 val[i] = saturate_cast<_Tp>(a.
val[i] * b.
val[i]);
533 template<
typename _Tp,
int m,
int n>
template<
int l>
inline
536 for(
int i = 0; i < m; i++ )
537 for(
int j = 0; j < n; j++ )
540 for(
int k = 0; k < l; k++ )
541 s += a(i, k) * b(k, j);
547 template<
typename _Tp,
int m,
int n>
inline
550 for(
int i = 0; i < m; i++ )
551 for(
int j = 0; j < n; j++ )
552 val[i*n + j] = a(j, i);
556 template<
typename _Tp,
int m,
int n>
static inline
563 template<
typename _Tp,
int m,
int n>
static inline
570 template<
typename _Tp,
int m,
int n>
static inline
573 for(
int i = 0; i < m*n; i++ )
574 a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
577 template<
typename _Tp,
int m,
int n>
static inline
580 for(
int i = 0; i < m*n; i++ )
581 a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
584 template<
typename _Tp,
int m,
int n>
static inline
587 for(
int i = 0; i < m*n; i++ )
588 a.val[i] = saturate_cast<_Tp>(a.val[i] * alpha);
591 template<
typename _Tp,
int m,
int n>
static inline
597 template<
typename _Tp,
int m,
int n>
static inline
603 template<
typename _Tp,
int m,
int n>
static inline
609 template<
typename _Tp,
int m,
int n>
static inline
615 template<
typename _Tp,
int m,
int n>
static inline
621 template<
typename _Tp,
int m,
int n>
static inline
627 template<
typename _Tp,
int m,
int n>
static inline
634 template<
typename _Tp,
int m,
int n,
int l>
static inline
641 template<
typename _Tp>
static inline
648 template<
typename _Tp>
static inline
655 template<
typename _Tp>
static inline
658 return Point3_<_Tp>(a*
Vec<_Tp,3>(b.x, b.y, 1));
662 template<
typename _Tp>
static inline
669 template<
typename _Tp>
static inline
676 template<
typename _Tp,
int m,
int n>
inline
694 double p =
LU(temp.
val, m, 0, 0);
697 for(
int i = 0; i < m; i++ )
717 return a(0,0)*a(1,1) - a(0,1)*a(1,0);
726 return a(0,0)*(a(1,1)*a(2,2) - a(2,1)*a(1,2)) -
727 a(0,1)*(a(1,0)*a(2,2) - a(2,0)*a(1,2)) +
728 a(0,2)*(a(1,0)*a(2,1) - a(2,0)*a(1,1));
732 template<
typename _Tp,
int m>
static inline
735 return Matx_DetOp<_Tp, m>()(a);
739 template<
typename _Tp,
int m,
int n>
static inline
743 for(
int i = 0; i <
std::min(m, n); i++ )
749 template<
typename _Tp,
int m,
int n>
inline
763 for(
int i = 0; i < m; i++ )
769 return LU(temp.
val, m, b.
val, m) != 0;
799 b(0,0) = (a(1,1) * a(2,2) - a(1,2) * a(2,1)) * d;
800 b(0,1) = (a(0,2) * a(2,1) - a(0,1) * a(2,2)) * d;
801 b(0,2) = (a(0,1) * a(1,2) - a(0,2) * a(1,1)) * d;
803 b(1,0) = (a(1,2) * a(2,0) - a(1,0) * a(2,2)) * d;
804 b(1,1) = (a(0,0) * a(2,2) - a(0,2) * a(2,0)) * d;
805 b(1,2) = (a(0,2) * a(1,0) - a(0,0) * a(1,2)) * d;
807 b(2,0) = (a(1,0) * a(2,1) - a(1,1) * a(2,0)) * d;
808 b(2,1) = (a(0,1) * a(2,0) - a(0,0) * a(2,1)) * d;
809 b(2,2) = (a(0,0) * a(1,1) - a(0,1) * a(1,0)) * d;
815 template<
typename _Tp,
int m,
int n>
inline
824 Mat A(*
this,
false), B(b,
false);
825 ok =
invert(A, B, method);
841 return LU(temp.
val, m, x.
val, n) != 0;
855 x(0) = (b(0)*a(1,1) - b(1)*a(0,1))*d;
856 x(1) = (b(1)*a(0,0) - b(0)*a(1,0))*d;
871 x(0) = d*(b(0)*(a(1,1)*a(2,2) - a(1,2)*a(2,1)) -
872 a(0,1)*(b(1)*a(2,2) - a(1,2)*b(2)) +
873 a(0,2)*(b(1)*a(2,1) - a(1,1)*b(2)));
875 x(1) = d*(a(0,0)*(b(1)*a(2,2) - a(1,2)*b(2)) -
876 b(0)*(a(1,0)*a(2,2) - a(1,2)*a(2,0)) +
877 a(0,2)*(a(1,0)*b(2) - b(1)*a(2,0)));
879 x(2) = d*(a(0,0)*(a(1,1)*b(2) - b(1)*a(2,1)) -
880 a(0,1)*(a(1,0)*b(2) - b(1)*a(2,0)) +
881 b(0)*(a(1,0)*a(2,1) - a(1,1)*a(2,0)));
887 template<
typename _Tp,
int m,
int n>
template<
int l>
inline
896 Mat A(*
this,
false), B(rhs,
false), X(x,
false);
904 template<
typename _Tp,
int m,
int n>
static inline
908 for(
int i = 0; i < m*n; i++ )
909 s += (
double)M.
val[i]*M.
val[i];
914 template<
typename _Tp,
int m,
int n>
static inline
920 for(
int i = 0; i < m*n; i++ )
928 for(
int i = 0; i < m*n; i++ )
938 template<
typename _Tp,
int m,
int n>
static inline
941 for(
int i = 0; i < m*n; i++ )
942 if( a.val[i] != b.val[i] )
return false;
946 template<
typename _Tp,
int m,
int n>
static inline
953 template<
typename _Tp,
typename _T2,
int m,
int n>
static inline
954 MatxCommaInitializer<_Tp, m, n> operator << (const Matx<_Tp, m, n>& mtx, _T2 val)
956 MatxCommaInitializer<_Tp, m, n> commaInitializer((
Matx<_Tp, m, n>*)&mtx);
957 return (commaInitializer, val);
960 template<
typename _Tp,
int m,
int n>
inline
965 template<
typename _Tp,
int m,
int n>
template<
typename _T2>
inline
969 dst->val[idx++] = saturate_cast<_Tp>(value);
973 template<
typename _Tp,
int m,
int n>
inline
986 :
Matx<_Tp, cn, 1>(v0)
990 :
Matx<_Tp, cn, 1>(v0, v1)
994 :
Matx<_Tp, cn, 1>(v0, v1, v2)
998 :
Matx<_Tp, cn, 1>(v0, v1, v2, v3)
1002 :
Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4)
1005 template<
typename _Tp,
int cn>
inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4, _Tp v5)
1006 :
Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5)
1010 _Tp v4, _Tp v5, _Tp v6)
1011 :
Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6)
1015 _Tp v4, _Tp v5, _Tp v6, _Tp v7)
1016 :
Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7)
1020 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
1022 :
Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8)
1026 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
1028 :
Matx<_Tp, cn, 1>(v0, v1, v2, v3, v4, v5, v6, v7, v8, v9)
1032 :
Matx<_Tp, cn, 1>(values)
1037 :
Matx<_Tp, cn, 1>(v.val)
1043 for(
int i = 0; i < cn; i++ ) v.
val[i] = alpha;
1050 for(
int i = 0; i < cn; i++ ) w.
val[i] = saturate_cast<_Tp>(this->val[i]*v.
val[i]);
1060 template<
typename _Tp,
int cn>
template<
typename T2>
1064 for(
int i = 0; i < cn; i++ ) v.
val[i] = saturate_cast<T2>(this->val[i]);
1072 for( i = 0; i <
std::min(cn, 4); i++ ) s.
val[i] = this->val[i];
1073 for( ; i < 4; i++ ) s.
val[i] = 0;
1080 return this->val[i];
1086 return this->val[i];
1092 return this->val[i];
1098 return this->val[i];
1101 template<
typename _Tp1,
typename _Tp2,
int cn>
static inline Vec<_Tp1, cn>&
1104 for(
int i = 0; i < cn; i++ )
1105 a.
val[i] = saturate_cast<_Tp1>(a.
val[i] + b.
val[i]);
1109 template<
typename _Tp1,
typename _Tp2,
int cn>
static inline Vec<_Tp1, cn>&
1112 for(
int i = 0; i < cn; i++ )
1113 a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
1117 template<
typename _Tp,
int cn>
static inline Vec<_Tp, cn>
1124 template<
typename _Tp,
int cn>
static inline Vec<_Tp, cn>
1131 template<
typename _Tp>
static inline
1134 a[0] *= alpha; a[1] *= alpha;
1138 template<
typename _Tp>
static inline
1141 a[0] *= alpha; a[1] *= alpha; a[2] *= alpha;
1145 template<
typename _Tp>
static inline
1148 a[0] *= alpha; a[1] *= alpha; a[2] *= alpha; a[3] *= alpha;
1152 template<
typename _Tp,
int cn>
static inline Vec<_Tp, cn>
1159 template<
typename _Tp,
int cn>
static inline Vec<_Tp, cn>
1169 return Vec<_Tp, 4>(saturate_cast<_Tp>(a[0]*b[0] - a[1]*b[1] - a[2]*b[2] - a[3]*b[3]),
1170 saturate_cast<_Tp>(a[0]*b[1] + a[1]*b[0] + a[2]*b[3] - a[3]*b[2]),
1171 saturate_cast<_Tp>(a[0]*b[2] - a[1]*b[3] + a[2]*b[0] - a[3]*b[1]),
1172 saturate_cast<_Tp>(a[0]*b[3] + a[1]*b[2] - a[2]*b[1] - a[3]*b[0]));
1184 template<
typename _Tp,
int cn>
static inline Vec<_Tp, cn>
1188 for(
int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]);
1195 val[2]*v.val[0] - val[0]*v.val[2],
1196 val[0]*v.val[1] - val[1]*v.val[0]);
1202 val[2]*v.val[0] - val[0]*v.val[2],
1203 val[0]*v.val[1] - val[1]*v.val[0]);
1206 template<
typename T1,
typename T2>
static inline
1209 a[0] = saturate_cast<T1>(a[0] + b[0]);
1210 a[1] = saturate_cast<T1>(a[1] + b[1]);
1214 template<
typename T1,
typename T2>
static inline
1217 a[0] = saturate_cast<T1>(a[0] + b[0]);
1218 a[1] = saturate_cast<T1>(a[1] + b[1]);
1219 a[2] = saturate_cast<T1>(a[2] + b[2]);
1224 template<
typename T1,
typename T2>
static inline
1227 a[0] = saturate_cast<T1>(a[0] + b[0]);
1228 a[1] = saturate_cast<T1>(a[1] + b[1]);
1229 a[2] = saturate_cast<T1>(a[2] + b[2]);
1230 a[3] = saturate_cast<T1>(a[3] + b[3]);
1235 template<
typename _Tp,
typename _T2,
int cn>
static inline
1236 VecCommaInitializer<_Tp, cn> operator << (const Vec<_Tp, cn>& vec, _T2 val)
1238 VecCommaInitializer<_Tp, cn> commaInitializer((
Vec<_Tp, cn>*)&vec);
1239 return (commaInitializer, val);
1242 template<
typename _Tp,
int cn>
inline
1247 template<
typename _Tp,
int cn>
template<
typename _T2>
inline
1251 this->
dst->val[this->idx++] = saturate_cast<_Tp>(value);
1255 template<
typename _Tp,
int cn>
inline
1267 {
return Complex<T2>(saturate_cast<T2>(re), saturate_cast<T2>(im)); }
1271 template<
typename _Tp>
static inline
1273 {
return a.
re == b.
re && a.
im == b.
im; }
1275 template<
typename _Tp>
static inline
1276 bool operator != (
const Complex<_Tp>& a,
const Complex<_Tp>& b)
1277 {
return a.re != b.re || a.im != b.im; }
1279 template<
typename _Tp>
static inline
1280 Complex<_Tp>
operator + (
const Complex<_Tp>& a,
const Complex<_Tp>& b)
1281 {
return Complex<_Tp>( a.re + b.re, a.im + b.im ); }
1283 template<
typename _Tp>
static inline
1284 Complex<_Tp>& operator += (Complex<_Tp>& a,
const Complex<_Tp>& b)
1285 { a.re += b.re; a.im += b.im;
return a; }
1287 template<
typename _Tp>
static inline
1288 Complex<_Tp>
operator - (
const Complex<_Tp>& a,
const Complex<_Tp>& b)
1289 {
return Complex<_Tp>( a.re - b.re, a.im - b.im ); }
1291 template<
typename _Tp>
static inline
1292 Complex<_Tp>& operator -= (Complex<_Tp>& a,
const Complex<_Tp>& b)
1293 { a.re -= b.re; a.im -= b.im;
return a; }
1295 template<
typename _Tp>
static inline
1296 Complex<_Tp>
operator - (
const Complex<_Tp>& a)
1297 {
return Complex<_Tp>(-a.re, -a.im); }
1299 template<
typename _Tp>
static inline
1300 Complex<_Tp>
operator * (
const Complex<_Tp>& a,
const Complex<_Tp>& b)
1301 {
return Complex<_Tp>( a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re ); }
1303 template<
typename _Tp>
static inline
1304 Complex<_Tp>
operator * (
const Complex<_Tp>& a, _Tp b)
1305 {
return Complex<_Tp>( a.re*b, a.im*b ); }
1307 template<
typename _Tp>
static inline
1308 Complex<_Tp>
operator * (_Tp b,
const Complex<_Tp>& a)
1309 {
return Complex<_Tp>( a.re*b, a.im*b ); }
1311 template<
typename _Tp>
static inline
1312 Complex<_Tp>
operator + (
const Complex<_Tp>& a, _Tp b)
1313 {
return Complex<_Tp>( a.re + b, a.im ); }
1315 template<
typename _Tp>
static inline
1316 Complex<_Tp>
operator - (
const Complex<_Tp>& a, _Tp b)
1317 {
return Complex<_Tp>( a.re - b, a.im ); }
1319 template<
typename _Tp>
static inline
1320 Complex<_Tp>
operator + (_Tp b,
const Complex<_Tp>& a)
1321 {
return Complex<_Tp>( a.re + b, a.im ); }
1323 template<
typename _Tp>
static inline
1324 Complex<_Tp>
operator - (_Tp b,
const Complex<_Tp>& a)
1325 {
return Complex<_Tp>( b - a.re, -a.im ); }
1327 template<
typename _Tp>
static inline
1328 Complex<_Tp>& operator += (Complex<_Tp>& a, _Tp b)
1329 { a.re += b;
return a; }
1331 template<
typename _Tp>
static inline
1332 Complex<_Tp>& operator -= (Complex<_Tp>& a, _Tp b)
1333 { a.re -= b;
return a; }
1335 template<
typename _Tp>
static inline
1336 Complex<_Tp>& operator *= (Complex<_Tp>& a, _Tp b)
1337 { a.re *= b; a.im *= b;
return a; }
1339 template<
typename _Tp>
static inline
1340 double abs(
const Complex<_Tp>& a)
1341 {
return std::sqrt( (
double)a.re*a.re + (
double)a.im*a.im); }
1343 template<
typename _Tp>
static inline
1344 Complex<_Tp>
operator / (
const Complex<_Tp>& a,
const Complex<_Tp>& b)
1346 double t = 1./((double)b.re*b.re + (
double)b.im*b.im);
1347 return Complex<_Tp>( (_Tp)((a.re*b.re + a.im*b.im)*t),
1348 (_Tp)((-a.re*b.im + a.im*b.re)*t) );
1351 template<
typename _Tp>
static inline
1352 Complex<_Tp>& operator /= (Complex<_Tp>& a,
const Complex<_Tp>& b)
1357 template<
typename _Tp>
static inline
1358 Complex<_Tp>
operator / (
const Complex<_Tp>& a, _Tp b)
1361 return Complex<_Tp>( a.re*t, a.im*t );
1364 template<
typename _Tp>
static inline
1365 Complex<_Tp>
operator / (_Tp b,
const Complex<_Tp>& a)
1367 return Complex<_Tp>(b)/a;
1370 template<
typename _Tp>
static inline
1371 Complex<_Tp> operator /= (
const Complex<_Tp>& a, _Tp b)
1374 a.re *= t; a.im *= t;
return a;
1384 :
x(saturate_cast<_Tp>(pt.
x)),
y(saturate_cast<_Tp>(pt.
y)) {}
1388 {
x = pt.
x;
y = pt.
y;
return *
this; }
1391 {
return Point_<_Tp2>(saturate_cast<_Tp2>(
x), saturate_cast<_Tp2>(
y)); }
1400 {
return saturate_cast<_Tp>(
x*pt.
x +
y*pt.
y); }
1402 {
return (
double)
x*pt.
x + (double)
y*pt.
y; }
1407 a.
x = saturate_cast<_Tp>(a.
x + b.
x);
1408 a.
y = saturate_cast<_Tp>(a.
y + b.
y);
1415 a.x = saturate_cast<_Tp>(a.x - b.x);
1416 a.y = saturate_cast<_Tp>(a.y - b.y);
1423 a.x = saturate_cast<_Tp>(a.x*b);
1424 a.y = saturate_cast<_Tp>(a.y*b);
1431 a.x = saturate_cast<_Tp>(a.x*b);
1432 a.y = saturate_cast<_Tp>(a.y*b);
1439 a.x = saturate_cast<_Tp>(a.x*b);
1440 a.y = saturate_cast<_Tp>(a.y*b);
1444 template<
typename _Tp>
static inline double norm(
const Point_<_Tp>& pt)
1445 {
return std::sqrt((
double)pt.x*pt.x + (
double)pt.y*pt.y); }
1448 {
return a.x == b.x && a.y == b.y; }
1451 {
return a.x != b.x || a.y != b.y; }
1454 {
return Point_<_Tp>( saturate_cast<_Tp>(a.x + b.x), saturate_cast<_Tp>(a.y + b.y) ); }
1457 {
return Point_<_Tp>( saturate_cast<_Tp>(a.x - b.x), saturate_cast<_Tp>(a.y - b.y) ); }
1460 {
return Point_<_Tp>( saturate_cast<_Tp>(-a.x), saturate_cast<_Tp>(-a.y) ); }
1463 {
return Point_<_Tp>( saturate_cast<_Tp>(a.x*b), saturate_cast<_Tp>(a.y*b) ); }
1466 {
return Point_<_Tp>( saturate_cast<_Tp>(b.x*a), saturate_cast<_Tp>(b.y*a) ); }
1469 {
return Point_<_Tp>( saturate_cast<_Tp>(a.x*b), saturate_cast<_Tp>(a.y*b) ); }
1472 {
return Point_<_Tp>( saturate_cast<_Tp>(b.x*a), saturate_cast<_Tp>(b.y*a) ); }
1475 {
return Point_<_Tp>( saturate_cast<_Tp>(a.x*b), saturate_cast<_Tp>(a.y*b) ); }
1478 {
return Point_<_Tp>( saturate_cast<_Tp>(b.x*a), saturate_cast<_Tp>(b.y*a) ); }
1487 x(saturate_cast<_Tp>(pt.
x)),
y(saturate_cast<_Tp>(pt.
y)), z(saturate_cast<_Tp>(pt.z)) {}
1491 {
return Point3_<_Tp2>(saturate_cast<_Tp2>(
x), saturate_cast<_Tp2>(
y), saturate_cast<_Tp2>(z)); }
1500 {
x = pt.
x;
y = pt.
y; z = pt.
z;
return *
this; }
1503 {
return saturate_cast<_Tp>(
x*pt.
x +
y*pt.
y + z*pt.
z); }
1505 {
return (
double)
x*pt.
x + (double)
y*pt.
y + (
double)z*pt.
z; }
1515 a.
x = saturate_cast<_Tp>(a.
x + b.
x);
1516 a.
y = saturate_cast<_Tp>(a.
y + b.
y);
1517 a.
z = saturate_cast<_Tp>(a.
z + b.
z);
1521 template<
typename _Tp>
static inline Point3_<_Tp>&
1522 operator -= (Point3_<_Tp>& a,
const Point3_<_Tp>& b)
1524 a.
x = saturate_cast<_Tp>(a.x - b.x);
1525 a.y = saturate_cast<_Tp>(a.y - b.y);
1526 a.z = saturate_cast<_Tp>(a.z - b.z);
1530 template<
typename _Tp>
static inline Point3_<_Tp>&
1531 operator *= (Point3_<_Tp>& a,
int b)
1533 a.x = saturate_cast<_Tp>(a.x*b);
1534 a.y = saturate_cast<_Tp>(a.y*b);
1535 a.z = saturate_cast<_Tp>(a.z*b);
1539 template<
typename _Tp>
static inline Point3_<_Tp>&
1540 operator *= (Point3_<_Tp>& a,
float b)
1542 a.x = saturate_cast<_Tp>(a.x*b);
1543 a.y = saturate_cast<_Tp>(a.y*b);
1544 a.z = saturate_cast<_Tp>(a.z*b);
1548 template<
typename _Tp>
static inline Point3_<_Tp>&
1549 operator *= (Point3_<_Tp>& a,
double b)
1551 a.x = saturate_cast<_Tp>(a.x*b);
1552 a.y = saturate_cast<_Tp>(a.y*b);
1553 a.z = saturate_cast<_Tp>(a.z*b);
1557 template<
typename _Tp>
static inline double norm(
const Point3_<_Tp>& pt)
1558 {
return std::sqrt((
double)pt.x*pt.x + (
double)pt.y*pt.y + (
double)pt.z*pt.z); }
1560 template<
typename _Tp>
static inline bool operator == (
const Point3_<_Tp>& a,
const Point3_<_Tp>& b)
1561 {
return a.x == b.x && a.y == b.y && a.z == b.z; }
1563 template<
typename _Tp>
static inline bool operator != (
const Point3_<_Tp>& a,
const Point3_<_Tp>& b)
1564 {
return a.x != b.x || a.y != b.y || a.z != b.z; }
1566 template<
typename _Tp>
static inline Point3_<_Tp>
operator + (
const Point3_<_Tp>& a,
const Point3_<_Tp>& b)
1567 {
return Point3_<_Tp>( saturate_cast<_Tp>(a.x + b.x),
1568 saturate_cast<_Tp>(a.y + b.y),
1569 saturate_cast<_Tp>(a.z + b.z)); }
1571 template<
typename _Tp>
static inline Point3_<_Tp>
operator - (
const Point3_<_Tp>& a,
const Point3_<_Tp>& b)
1572 {
return Point3_<_Tp>( saturate_cast<_Tp>(a.x - b.x),
1573 saturate_cast<_Tp>(a.y - b.y),
1574 saturate_cast<_Tp>(a.z - b.z)); }
1576 template<
typename _Tp>
static inline Point3_<_Tp>
operator - (
const Point3_<_Tp>& a)
1577 {
return Point3_<_Tp>( saturate_cast<_Tp>(-a.x),
1578 saturate_cast<_Tp>(-a.y),
1579 saturate_cast<_Tp>(-a.z) ); }
1581 template<
typename _Tp>
static inline Point3_<_Tp>
operator * (
const Point3_<_Tp>& a,
int b)
1582 {
return Point3_<_Tp>( saturate_cast<_Tp>(a.x*b),
1583 saturate_cast<_Tp>(a.y*b),
1584 saturate_cast<_Tp>(a.z*b) ); }
1586 template<
typename _Tp>
static inline Point3_<_Tp>
operator * (
int a,
const Point3_<_Tp>& b)
1587 {
return Point3_<_Tp>( saturate_cast<_Tp>(b.x*a),
1588 saturate_cast<_Tp>(b.y*a),
1589 saturate_cast<_Tp>(b.z*a) ); }
1591 template<
typename _Tp>
static inline Point3_<_Tp>
operator * (
const Point3_<_Tp>& a,
float b)
1592 {
return Point3_<_Tp>( saturate_cast<_Tp>(a.x*b),
1593 saturate_cast<_Tp>(a.y*b),
1594 saturate_cast<_Tp>(a.z*b) ); }
1596 template<
typename _Tp>
static inline Point3_<_Tp>
operator * (
float a,
const Point3_<_Tp>& b)
1597 {
return Point3_<_Tp>( saturate_cast<_Tp>(b.x*a),
1598 saturate_cast<_Tp>(b.y*a),
1599 saturate_cast<_Tp>(b.z*a) ); }
1601 template<
typename _Tp>
static inline Point3_<_Tp>
operator * (
const Point3_<_Tp>& a,
double b)
1602 {
return Point3_<_Tp>( saturate_cast<_Tp>(a.x*b),
1603 saturate_cast<_Tp>(a.y*b),
1604 saturate_cast<_Tp>(a.z*b) ); }
1606 template<
typename _Tp>
static inline Point3_<_Tp>
operator * (
double a,
const Point3_<_Tp>& b)
1607 {
return Point3_<_Tp>( saturate_cast<_Tp>(b.x*a),
1608 saturate_cast<_Tp>(b.y*a),
1609 saturate_cast<_Tp>(b.z*a) ); }
1614 : width(0), height(0) {}
1616 : width(_width), height(_height) {}
1618 : width(sz.width), height(sz.height) {}
1620 : width(saturate_cast<_Tp>(sz.width)), height(saturate_cast<_Tp>(sz.height)) {}
1622 : width(saturate_cast<_Tp>(sz.width)), height(saturate_cast<_Tp>(sz.height)) {}
1626 {
return Size_<_Tp2>(saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); }
1630 {
return cvSize2D32f((
float)width, (
float)height); }
1633 { width = sz.
width; height = sz.
height;
return *
this; }
1637 {
return Size_<_Tp>(a.width + b.width, a.height + b.height); }
1639 {
return Size_<_Tp>(a.width - b.width, a.height - b.height); }
1645 { a.width -= b.width; a.height -= b.height;
return a; }
1648 {
return a.width == b.width && a.height == b.height; }
1650 {
return a.width != b.width || a.height != b.height; }
1656 template<
typename _Tp>
inline Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height) :
x(_x),
y(_y), width(_width), height(_height) {}
1660 x(org.
x),
y(org.
y), width(sz.width), height(sz.height) {}
1667 {
x = r.
x;
y = r.
y; width = r.
width; height = r.
height;
return *
this; }
1673 { a.
x += b.
x; a.
y += b.
y;
return a; }
1675 { a.x -= b.x; a.y -= b.y;
return a; }
1678 { a.width += b.width; a.height += b.height;
return a; }
1681 { a.width -= b.width; a.height -= b.height;
return a; }
1686 a.width =
std::min(a.x + a.width, b.x + b.width) - x1;
1687 a.height =
std::min(a.y + a.height, b.y + b.height) - y1;
1689 if( a.width <= 0 || a.height <= 0 )
1697 a.width =
std::max(a.x + a.width, b.x + b.width) - x1;
1698 a.height =
std::max(a.y + a.height, b.y + b.height) - y1;
1707 {
return Rect_<_Tp2>(saturate_cast<_Tp2>(
x), saturate_cast<_Tp2>(
y),
1708 saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); }
1714 {
return x <= pt.
x && pt.
x <
x + width &&
y <= pt.
y && pt.
y < y + height; }
1723 return a.x != b.x || a.y != b.y || a.width != b.width || a.height != b.height;
1728 return Rect_<_Tp>( a.x + b.x, a.y + b.y, a.width, a.height );
1733 return Rect_<_Tp>( a.x - b.x, a.y - b.y, a.width, a.height );
1738 return Rect_<_Tp>( a.x, a.y, a.width + b.width, a.height + b.height );
1760 : center(_center), size(_size), angle(_angle) {}
1762 : center(box.center), size(box.size), angle(box.angle) {}
1772 { this->val[0] = this->val[1] = this->val[2] = this->val[3] = 0; }
1775 { this->val[0] = v0; this->val[1] = v1; this->val[2] = v2; this->val[3] = v3; }
1779 this->val[0] = saturate_cast<_Tp>(s.
val[0]);
1780 this->val[1] = saturate_cast<_Tp>(s.
val[1]);
1781 this->val[2] = saturate_cast<_Tp>(s.
val[2]);
1782 this->val[3] = saturate_cast<_Tp>(s.
val[3]);
1786 { this->val[0] = v0; this->val[1] = this->val[2] = this->val[3] = 0; }
1791 {
return cvScalar(this->val[0], this->val[1], this->val[2], this->val[3]); }
1795 return Scalar_<T2>(saturate_cast<T2>(this->val[0]),
1796 saturate_cast<T2>(this->val[1]),
1797 saturate_cast<T2>(this->val[2]),
1798 saturate_cast<T2>(this->val[3]));
1803 a.
val[0] = saturate_cast<_Tp>(a.
val[0] + b.
val[0]);
1804 a.
val[1] = saturate_cast<_Tp>(a.
val[1] + b.
val[1]);
1805 a.
val[2] = saturate_cast<_Tp>(a.
val[2] + b.
val[2]);
1806 a.
val[3] = saturate_cast<_Tp>(a.
val[3] + b.
val[3]);
1810 template<
typename _Tp>
static inline Scalar_<_Tp>& operator -= (Scalar_<_Tp>& a,
const Scalar_<_Tp>& b)
1812 a.
val[0] = saturate_cast<_Tp>(a.val[0] - b.val[0]);
1813 a.val[1] = saturate_cast<_Tp>(a.val[1] - b.val[1]);
1814 a.val[2] = saturate_cast<_Tp>(a.val[2] - b.val[2]);
1815 a.val[3] = saturate_cast<_Tp>(a.val[3] - b.val[3]);
1819 template<
typename _Tp>
static inline Scalar_<_Tp>& operator *= ( Scalar_<_Tp>& a, _Tp v )
1821 a.val[0] = saturate_cast<_Tp>(a.val[0] * v);
1822 a.val[1] = saturate_cast<_Tp>(a.val[1] * v);
1823 a.val[2] = saturate_cast<_Tp>(a.val[2] * v);
1824 a.val[3] = saturate_cast<_Tp>(a.val[3] * v);
1831 saturate_cast<_Tp>(this->val[1]*t.
val[1]*scale),
1832 saturate_cast<_Tp>(this->val[2]*t.
val[2]*
scale),
1833 saturate_cast<_Tp>(this->val[3]*t.
val[3]*scale));
1842 template<
typename _Tp>
static inline bool operator != (
const Scalar_<_Tp>& a,
const Scalar_<_Tp>& b )
1844 return a.val[0] != b.val[0] || a.val[1] != b.val[1] ||
1845 a.val[2] != b.val[2] || a.val[3] != b.val[3];
1848 template<
typename _Tp>
static inline Scalar_<_Tp>
operator + (
const Scalar_<_Tp>& a,
const Scalar_<_Tp>& b)
1850 return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] + b.val[0]),
1851 saturate_cast<_Tp>(a.val[1] + b.val[1]),
1852 saturate_cast<_Tp>(a.val[2] + b.val[2]),
1853 saturate_cast<_Tp>(a.val[3] + b.val[3]));
1856 template<
typename _Tp>
static inline Scalar_<_Tp>
operator - (
const Scalar_<_Tp>& a,
const Scalar_<_Tp>& b)
1858 return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] - b.val[0]),
1859 saturate_cast<_Tp>(a.val[1] - b.val[1]),
1860 saturate_cast<_Tp>(a.val[2] - b.val[2]),
1861 saturate_cast<_Tp>(a.val[3] - b.val[3]));
1864 template<
typename _Tp>
static inline Scalar_<_Tp>
operator * (
const Scalar_<_Tp>& a, _Tp alpha)
1866 return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] * alpha),
1867 saturate_cast<_Tp>(a.val[1] * alpha),
1868 saturate_cast<_Tp>(a.val[2] * alpha),
1869 saturate_cast<_Tp>(a.val[3] * alpha));
1872 template<
typename _Tp>
static inline Scalar_<_Tp>
operator * (_Tp alpha,
const Scalar_<_Tp>& a)
1877 template<
typename _Tp>
static inline Scalar_<_Tp>
operator - (
const Scalar_<_Tp>& a)
1879 return Scalar_<_Tp>(saturate_cast<_Tp>(-a.val[0]), saturate_cast<_Tp>(-a.val[1]),
1880 saturate_cast<_Tp>(-a.val[2]), saturate_cast<_Tp>(-a.val[3]));
1884 template<
typename _Tp>
static inline Scalar_<_Tp>
1885 operator * (
const Scalar_<_Tp>& a,
const Scalar_<_Tp>& b)
1887 return Scalar_<_Tp>(saturate_cast<_Tp>(a[0]*b[0] - a[1]*b[1] - a[2]*b[2] - a[3]*b[3]),
1888 saturate_cast<_Tp>(a[0]*b[1] + a[1]*b[0] + a[2]*b[3] - a[3]*b[2]),
1889 saturate_cast<_Tp>(a[0]*b[2] - a[1]*b[3] + a[2]*b[0] - a[3]*b[1]),
1890 saturate_cast<_Tp>(a[0]*b[3] + a[1]*b[2] - a[2]*b[1] - a[3]*b[0]));
1893 template<
typename _Tp>
static inline Scalar_<_Tp>&
1894 operator *= (Scalar_<_Tp>& a,
const Scalar_<_Tp>& b)
1903 saturate_cast<_Tp>(-this->val[1]),
1904 saturate_cast<_Tp>(-this->val[2]),
1905 saturate_cast<_Tp>(-this->val[3]));
1910 return this->val[1] == 0 && this->val[2] == 0 && this->val[3] == 0;
1913 template<
typename _Tp>
static inline
1917 saturate_cast<_Tp>(a.
val[1] / alpha),
1918 saturate_cast<_Tp>(a.
val[2] / alpha),
1919 saturate_cast<_Tp>(a.
val[3] / alpha));
1922 template<
typename _Tp>
static inline
1923 Scalar_<float>
operator / (
const Scalar_<float>& a,
float alpha)
1926 return Scalar_<float>(a.val[0]*s, a.val[1]*s, a.val[2]*s, a.val[3]*s);
1929 template<
typename _Tp>
static inline
1930 Scalar_<double>
operator / (
const Scalar_<double>& a,
double alpha)
1933 return Scalar_<double>(a.val[0]*s, a.val[1]*s, a.val[2]*s, a.val[3]*s);
1936 template<
typename _Tp>
static inline
1937 Scalar_<_Tp>& operator /= (Scalar_<_Tp>& a, _Tp alpha)
1943 template<
typename _Tp>
static inline
1944 Scalar_<_Tp>
operator / (_Tp a,
const Scalar_<_Tp>& b)
1946 _Tp s = a/(b[0]*b[0] + b[1]*b[1] + b[2]*b[2] + b[3]*b[3]);
1950 template<
typename _Tp>
static inline
1951 Scalar_<_Tp>
operator / (
const Scalar_<_Tp>& a,
const Scalar_<_Tp>& b)
1953 return a*((_Tp)1/b);
1956 template<
typename _Tp>
static inline
1957 Scalar_<_Tp>& operator /= (Scalar_<_Tp>& a,
const Scalar_<_Tp>& b)
1980 static inline bool operator != (
const Range& r1,
const Range& r2)
1981 {
return !(r1 == r2); }
1983 static inline bool operator !(
const Range& r)
1984 {
return r.start == r.end; }
1986 static inline Range
operator & (
const Range& r1,
const Range& r2)
1993 static inline Range& operator &= (Range& r1,
const Range& r2)
2001 return Range(r1.start + delta, r1.end + delta);
2006 return Range(r1.start + delta, r1.end + delta);
2011 return r1 + (-
delta);
2037 Hdr() : data(0), datastart(0), refcount(0), size(0), capacity(0) {};
2050 for(
size_t i = 0; i < _size; i++)
2053 Vector(_Tp* _data,
size_t _size,
bool _copyData=
false)
2054 {
set(_data, _size, _copyData); }
2057 {
set((_Tp*)&vec.
val[0], n,
true); }
2059 Vector(
const std::vector<_Tp>& vec,
bool _copyData=
false)
2060 {
set((_Tp*)&vec[0], vec.size(), _copyData); }
2076 hdr.capacity = hdr.size = r.
size();
2099 size_t i, sz = size();
2101 const _Tp* src = hdr.data;
2103 for( i = 0; i < sz; i++ )
2107 void copyTo(std::vector<_Tp>& vec)
const
2109 size_t i, sz = size();
2111 const _Tp* src = hdr.data;
2112 _Tp*
dst = sz ? &vec[0] : 0;
2113 for( i = 0; i < sz; i++ )
2118 {
return cvMat((
int)size(), 1, type(), (
void*)hdr.data); }
2120 _Tp& operator [] (
size_t i) {
CV_DbgAssert( i < size() );
return hdr.data[i]; }
2121 const _Tp& operator [] (
size_t i)
const {
CV_DbgAssert( i < size() );
return hdr.data[i]; }
2129 _Tp*
end() {
return hdr.data + hdr.size; }
2130 const _Tp*
begin()
const {
return hdr.data; }
2131 const _Tp*
end()
const {
return hdr.data + hdr.size; }
2133 void addref() {
if( hdr.refcount ) CV_XADD(hdr.refcount, 1); }
2136 if( hdr.refcount && CV_XADD(hdr.refcount, -1) == 1 )
2138 delete[] hdr.datastart;
2139 delete hdr.refcount;
2144 void set(_Tp* _data,
size_t _size,
bool _copyData=
false)
2149 hdr.data = hdr.datastart = _data;
2150 hdr.size = hdr.capacity = _size;
2156 for(
size_t i = 0; i < _size; i++ )
2157 hdr.data[i] = _data[i];
2162 void reserve(
size_t newCapacity)
2166 size_t i, oldSize = hdr.size;
2167 if( (!hdr.refcount || *hdr.refcount == 1) && hdr.capacity >= newCapacity )
2169 newCapacity =
std::max(newCapacity, oldSize);
2170 newData =
new _Tp[newCapacity];
2171 newRefcount =
new int(1);
2172 for( i = 0; i < oldSize; i++ )
2173 newData[i] = hdr.data[i];
2175 hdr.data = hdr.datastart = newData;
2176 hdr.capacity = newCapacity;
2178 hdr.refcount = newRefcount;
2184 newSize =
std::max(newSize, (
size_t)0);
2185 if( (!hdr.refcount || *hdr.refcount == 1) && hdr.size == newSize )
2187 if( newSize > hdr.capacity )
2189 for( i = hdr.size; i < newSize; i++ )
2190 hdr.data[i] = _Tp();
2196 if( hdr.size == hdr.capacity )
2197 reserve(
std::max((
size_t)4, hdr.capacity*2) );
2198 hdr.data[hdr.size++] = elem;
2209 size_t size()
const {
return hdr.size; }
2211 bool empty()
const {
return hdr.size == 0; }
2224 size_t i, n = v1.
size();
2228 const _Tp *ptr1 = &v1[0], *ptr2 = &v2[0];
2229 for( i = 0; i <= n - 4; i += 4 )
2230 s += (_Tw)ptr1[i]*ptr2[i] + (_Tw)ptr1[i+1]*ptr2[i+1] +
2231 (_Tw)ptr1[i+2]*ptr2[i+2] + (_Tw)ptr1[i+3]*ptr2[i+3];
2233 s += (_Tw)ptr1[i]*ptr2[i];
2243 return (
unsigned)
state;
2249 inline RNG::operator short() {
return (
short)next(); }
2250 inline RNG::operator unsigned() {
return next(); }
2253 inline RNG::operator int() {
return (
int)next(); }
2255 inline RNG::operator float() {
return next()*2.3283064365386962890625e-10f; }
2256 inline RNG::operator double()
2258 unsigned t = next();
2259 return (((
uint64)t << 32) | next())*5.4210108624275221700372640043497e-20;
2262 inline float RNG::uniform(
float a,
float b) {
return ((
float)*
this)*(b - a) + a; }
2263 inline double RNG::uniform(
double a,
double b) {
return ((
float)*
this)*(b - a) + a; }
2267 : type(_type), maxCount(_maxCount), epsilon(_epsilon) {}
2269 : type(criteria.type), maxCount(criteria.max_iter), epsilon(criteria.epsilon) {}
2298 : ptr(buf), size(fixed_size) {}
2301 : ptr(buf), size(fixed_size) {
allocate(_size); }
2311 if(_size > fixed_size)
2313 ptr = cv::allocate<_Tp>(_size);
2322 cv::deallocate<_Tp>(ptr, size);
2350 {
if( refcount ) CV_XADD(refcount, 1); }
2354 if( refcount && CV_XADD(refcount, -1) == 1 )
2365 if( obj )
delete obj;
2381 CV_XADD(_refcount, 1);
2384 refcount = _refcount;
2413 {
write(fs,
string(), value); }
2475 for(
int i = 0; i < cn; i++)
2497 int flags,
const string& typeName=
string());
2543 for(
int i = 0; i < cn; i++)
2567 void operator()(
const vector<_Tp>& vec)
const
2569 size_t i, count = vec.size();
2570 for( i = 0; i < count; i++ )
2571 write( *fs, vec[i] );
2580 void operator()(
const vector<_Tp>& vec)
const
2583 char fmt[] = { (char)((_fmt>>8)+
'1'), (
char)_fmt,
'\0' };
2584 fs->writeRaw(
string(fmt), (
uchar*)&vec[0], vec.size()*
sizeof(_Tp) );
2590 template<
typename _Tp>
static inline void write(
FileStorage& fs,
const vector<_Tp>& vec )
2596 template<
typename _Tp>
static inline FileStorage&
2597 operator << ( FileStorage& fs, const vector<_Tp>& vec )
2599 VecWriterProxy<_Tp, DataType<_Tp>::fmt != 0> w(&fs);
2604 CV_EXPORTS_W void write( FileStorage& fs,
const string& name,
const Mat& value );
2605 CV_EXPORTS void write( FileStorage& fs,
const string& name,
const SparseMat& value );
2607 template<
typename _Tp>
static inline FileStorage& operator << (FileStorage& fs,
const _Tp& value)
2609 if( !fs.isOpened() )
2613 write( fs, fs.elname, value );
2619 CV_EXPORTS FileStorage& operator << (FileStorage& fs,
const string& str);
2621 static inline FileStorage& operator << (FileStorage& fs,
const char* str)
2622 {
return (fs <<
string(str)); }
2626 : fs(_fs), node(_node) {}
2649 static inline void read(
const FileNode& node,
int& value,
int default_value)
2651 value = !node.
node ? default_value :
2656 static inline void read(
const FileNode& node,
bool& value,
bool default_value)
2658 int temp; read(node, temp, (
int)default_value);
2662 static inline void read(
const FileNode& node,
uchar& value,
uchar default_value)
2664 int temp; read(node, temp, (
int)default_value);
2665 value = saturate_cast<
uchar>(temp);
2668 static inline void read(
const FileNode& node,
schar& value,
schar default_value)
2670 int temp; read(node, temp, (
int)default_value);
2671 value = saturate_cast<
schar>(temp);
2674 static inline void read(
const FileNode& node,
ushort& value,
ushort default_value)
2676 int temp; read(node, temp, (
int)default_value);
2677 value = saturate_cast<
ushort>(temp);
2680 static inline void read(
const FileNode& node,
short& value,
short default_value)
2682 int temp; read(node, temp, (
int)default_value);
2683 value = saturate_cast<
short>(temp);
2686 static inline void read(
const FileNode& node,
float& value,
float default_value)
2688 value = !node.node ? default_value :
2693 static inline void read(
const FileNode& node,
double& value,
double default_value)
2695 value = !node.node ? default_value :
2700 static inline void read(
const FileNode& node,
string& value,
const string& default_value)
2702 value = !node.node ? default_value :
CV_NODE_IS_STRING(node.node->tag) ? string(node.node->data.str.ptr) : string(
"");
2705 CV_EXPORTS_W void read(
const FileNode& node, Mat& mat,
const Mat& default_mat=Mat() );
2706 CV_EXPORTS void read(
const FileNode& node, SparseMat& mat,
const SparseMat& default_mat=SparseMat() );
2708 inline FileNode::operator int()
const
2711 read(*
this, value, 0);
2714 inline FileNode::operator float()
const
2717 read(*
this, value, 0.f);
2720 inline FileNode::operator double()
const
2723 read(*
this, value, 0.);
2726 inline FileNode::operator string()
const
2729 read(*
this, value, value);
2742 void operator()(vector<_Tp>& vec,
size_t count)
const
2744 count =
std::min(count, it->remaining);
2746 for(
size_t i = 0; i < count; i++, ++(*it) )
2747 read(**it, vec[i], _Tp());
2756 void operator()(vector<_Tp>& vec,
size_t count)
const
2760 char fmt[] = { (char)((_fmt>>8)+
'1'), (
char)_fmt,
'\0' };
2761 count =
std::min(count, remaining/cn);
2763 it->readRaw(
string(fmt), (
uchar*)&vec[0], count*
sizeof(_Tp) );
2768 template<
typename _Tp>
static inline void
2769 read(
FileNodeIterator& it, vector<_Tp>& vec,
size_t maxCount=(
size_t)INT_MAX )
2775 template<
typename _Tp>
static inline void
2776 read( FileNode& node, vector<_Tp>& vec,
const vector<_Tp>& default_value=vector<_Tp>() )
2778 read( node.begin(), vec );
2798 { read( *it, value, _Tp());
return ++it; }
2800 template<
typename _Tp>
static inline
2801 FileNodeIterator& operator >> (FileNodeIterator& it, vector<_Tp>& vec)
2803 VecReaderProxy<_Tp, DataType<_Tp>::fmt != 0> r(&it);
2804 r(vec, (
size_t)INT_MAX);
2808 template<
typename _Tp>
static inline void operator >> (
const FileNode& n, _Tp& value)
2809 { FileNodeIterator it = n.begin(); it >> value; }
2811 static inline bool operator == (
const FileNodeIterator& it1,
const FileNodeIterator& it2)
2813 return it1.fs == it2.fs && it1.container == it2.container &&
2814 it1.reader.ptr == it2.reader.ptr && it1.remaining == it2.remaining;
2817 static inline bool operator != (
const FileNodeIterator& it1,
const FileNodeIterator& it2)
2819 return !(it1 == it2);
2822 static inline ptrdiff_t
operator - (
const FileNodeIterator& it1,
const FileNodeIterator& it2)
2824 return it2.remaining - it1.remaining;
2827 static inline bool operator < (
const FileNodeIterator& it1,
const FileNodeIterator& it2)
2829 return it1.remaining > it2.remaining;
2841 template<
typename _Tp>
static inline _Tp gcd(_Tp a, _Tp b)
2894 template<
typename _Tp,
class _LT>
void sort( vector<_Tp>& vec, _LT LT=_LT() )
2896 int isort_thresh = 7;
2905 size_t total = vec.size();
2912 stack[0].ub = arr + (total - 1);
2916 _Tp* left = stack[sp].lb;
2917 _Tp* right = stack[sp--].ub;
2921 int i, n = (int)(right - left) + 1, m;
2925 if( n <= isort_thresh )
2928 for( ptr = left + 1; ptr <= right; ptr++ )
2930 for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--)
2949 pivot = left + (n/2);
2954 a = left, b = left + d, c = left + 2*d;
2955 left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2956 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2958 a = pivot - d, b = pivot, c = pivot + d;
2959 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2960 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2962 a = right - 2*d, b = right - d, c = right;
2963 right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2964 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2967 a = left, b = pivot, c = right;
2968 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2969 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2970 if( pivot != left0 )
2975 left = left1 = left0 + 1;
2976 right = right1 = right0;
2980 while( left <= right && !LT(*pivot, *left) )
2982 if( !LT(*left, *pivot) )
2992 while( left <= right && !LT(*right, *pivot) )
2994 if( !LT(*pivot, *right) )
2996 if( right < right1 )
3014 left = left0, right = right0;
3018 n =
std::min( (
int)(left1 - left0), (
int)(left - left1) );
3019 for( i = 0; i < n; i++ )
3022 n =
std::min( (
int)(right0 - right1), (
int)(right1 - right) );
3023 for( i = 0; i < n; i++ )
3025 n = (int)(left - left1);
3026 m = (int)(right1 - right);
3033 stack[++sp].lb = left0;
3034 stack[sp].ub = left0 + n - 1;
3035 left = right0 - m + 1, right = right0;
3039 stack[++sp].lb = right0 - m + 1;
3040 stack[sp].ub = right0;
3041 left = left0, right = left0 + n - 1;
3045 left = left0, right = left0 + n - 1;
3048 left = right0 - m + 1, right = right0;
3059 bool operator()(
const _Tp& a,
const _Tp& b)
const {
return a < b; }
3065 bool operator()(
const _Tp& a,
const _Tp& b)
const {
return a >= b; }
3091 template<
typename _Tp,
class _EqPredicate>
int
3093 _EqPredicate predicate=_EqPredicate())
3095 int i, j, N = (int)_vec.size();
3096 const _Tp* vec = &_vec[0];
3101 vector<int> _nodes(N*2);
3102 int (*nodes)[2] = (int(*)[2])&_nodes[0];
3105 for(i = 0; i < N; i++)
3107 nodes[i][PARENT]=-1;
3112 for( i = 0; i < N; i++ )
3117 while( nodes[root][PARENT] >= 0 )
3118 root = nodes[root][PARENT];
3120 for( j = 0; j < N; j++ )
3122 if( i == j || !predicate(vec[i], vec[j]))
3126 while( nodes[root2][PARENT] >= 0 )
3127 root2 = nodes[root2][PARENT];
3132 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
3134 nodes[root2][PARENT] = root;
3137 nodes[root][PARENT] = root2;
3138 nodes[root2][RANK] += rank == rank2;
3141 assert( nodes[root][PARENT] < 0 );
3146 while( (parent = nodes[k][PARENT]) >= 0 )
3148 nodes[k][PARENT] = root;
3154 while( (parent = nodes[k][PARENT]) >= 0 )
3156 nodes[k][PARENT] = root;
3167 for( i = 0; i < N; i++ )
3170 while( nodes[root][PARENT] >= 0 )
3171 root = nodes[root][PARENT];
3173 if( nodes[root][RANK] >= 0 )
3174 nodes[root][RANK] = ~nclasses++;
3175 labels[i] = ~nodes[root][RANK];
3190 int count,
int in_front=0 );
3200 CV_Assert(!_seq || _seq->elem_size ==
sizeof(_Tp));
3223 {
return seq ? seq->total : 0; }
3232 {
return seq ?
CV_MAT_CN(seq->flags) : 0; }
3235 {
return seq ? seq->elem_size : 0; }
3265 {
return !seq || seq->total == 0; }
3283 { seqInsert(seq, idx, &elem); }
3299 size_t len = !seq ? 0 : range ==
Range::all() ? seq->total : range.
end - range.
start;
3313 { memset(
this, 0,
sizeof(*
this)); }
3318 index = seekEnd ? seq.
seq->total : 0;
3331 {
return *(_Tp*)ptr; }
3334 {
return *(
const _Tp*)ptr; }
3339 if( ++index >= seq->total*2 )
3355 index = seq->total*2-1;
3370 int n = seq->
total*2;
3380 return (*
this += -delta);
3387 if(
std::abs(static_cast<long>(delta)) > n )
3388 delta += delta < 0 ? n : -n;
3408 static int isInstance(
const void* ptr)
3410 static _ClsName dummy;
3411 return *(
const void**)&dummy == *(
const void**)ptr;
3413 static void release(
void** dbptr)
3417 delete (_ClsName*)*dbptr;
3424 _ClsName* obj =
new _ClsName;
3437 ((
const _ClsName*)ptr)->write(fs,
string(name));
3441 static void* clone(
const void* ptr)
3445 return new _ClsName(*(
const _ClsName*)ptr);
3454 virtual void write(std::ostream& out,
const Mat& m,
const int* params=0,
int nparams=0)
const = 0;
3455 virtual void write(std::ostream& out,
const void* data,
int nelems,
int type,
3456 const int* params=0,
int nparams=0)
const = 0;
3457 static const Formatter*
get(
const char* fmt=
"");
3465 const vector<int>& params);
3467 const int* params=0);
3476 template<
typename _Tp>
inline std::ostream& operator<<(std::ostream& out, const Point_<_Tp>& p)
3478 out <<
"[" << p.x <<
", " << p.y <<
"]";
3484 template<
typename _Tp>
inline std::ostream& operator<<(std::ostream& out, const Point3_<_Tp>& p)
3486 out <<
"[" << p.x <<
", " << p.y <<
", " << p.z <<
"]";
3490 static inline Formatted
format(
const Mat& mtx,
const char* fmt,
3491 const vector<int>& params=vector<int>())
3496 template<
typename _Tp>
static inline Formatted
format(
const vector<
Point_<_Tp> >& vec,
3497 const char* fmt,
const vector<int>& params=vector<int>())
3502 template<
typename _Tp>
static inline Formatted
format(
const vector<Point3_<_Tp> >& vec,
3503 const char* fmt,
const vector<int>& params=vector<int>())
3515 static inline std::ostream& operator << (std::ostream& out,
const Mat& mtx)
3528 static inline std::ostream& operator << (std::ostream& out,
const Formatted& fmtd)
3530 fmtd.fmt->write(out, fmtd.mtx);
3535 template<
typename _Tp>
static inline std::ostream& operator << (std::ostream& out,
3543 template<
typename _Tp>
static inline std::ostream& operator << (std::ostream& out,
3544 const vector<Point3_<_Tp> >& vec)
3552 #endif // __cplusplus