1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #ifndef _OPENCV_CORE_OPERATIONS_H_
44 #define _OPENCV_CORE_OPERATIONS_H_
49 #endif // SKIP_INCLUDES
53 /////// exchange-add operation for atomic operations on reference counters ///////
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>
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 WIN64 && !defined _WIN64
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; }
118 /////////////// saturate_cast (used in image & signal processing) ///////////////////
120 template<typename _Tp> static inline _Tp saturate_cast(uchar v) { return _Tp(v); }
121 template<typename _Tp> static inline _Tp saturate_cast(schar v) { return _Tp(v); }
122 template<typename _Tp> static inline _Tp saturate_cast(ushort v) { return _Tp(v); }
123 template<typename _Tp> static inline _Tp saturate_cast(short v) { return _Tp(v); }
124 template<typename _Tp> static inline _Tp saturate_cast(unsigned v) { return _Tp(v); }
125 template<typename _Tp> static inline _Tp saturate_cast(int v) { return _Tp(v); }
126 template<typename _Tp> static inline _Tp saturate_cast(float v) { return _Tp(v); }
127 template<typename _Tp> static inline _Tp saturate_cast(double v) { return _Tp(v); }
129 template<> inline uchar saturate_cast<uchar>(schar v)
130 { return (uchar)std::max((int)v, 0); }
131 template<> inline uchar saturate_cast<uchar>(ushort v)
132 { return (uchar)std::min((unsigned)v, (unsigned)UCHAR_MAX); }
133 template<> inline uchar saturate_cast<uchar>(int v)
134 { return (uchar)((unsigned)v <= UCHAR_MAX ? v : v > 0 ? UCHAR_MAX : 0); }
135 template<> inline uchar saturate_cast<uchar>(short v)
136 { return saturate_cast<uchar>((int)v); }
137 template<> inline uchar saturate_cast<uchar>(unsigned v)
138 { return (uchar)std::min(v, (unsigned)UCHAR_MAX); }
139 template<> inline uchar saturate_cast<uchar>(float v)
140 { int iv = cvRound(v); return saturate_cast<uchar>(iv); }
141 template<> inline uchar saturate_cast<uchar>(double v)
142 { int iv = cvRound(v); return saturate_cast<uchar>(iv); }
144 template<> inline schar saturate_cast<schar>(uchar v)
145 { return (schar)std::min((int)v, SCHAR_MAX); }
146 template<> inline schar saturate_cast<schar>(ushort v)
147 { return (schar)std::min((unsigned)v, (unsigned)SCHAR_MAX); }
148 template<> inline schar saturate_cast<schar>(int v)
150 return (schar)((unsigned)(v-SCHAR_MIN) <= (unsigned)UCHAR_MAX ?
151 v : v > 0 ? SCHAR_MAX : SCHAR_MIN);
153 template<> inline schar saturate_cast<schar>(short v)
154 { return saturate_cast<schar>((int)v); }
155 template<> inline schar saturate_cast<schar>(unsigned v)
156 { return (schar)std::min(v, (unsigned)SCHAR_MAX); }
158 template<> inline schar saturate_cast<schar>(float v)
159 { int iv = cvRound(v); return saturate_cast<schar>(iv); }
160 template<> inline schar saturate_cast<schar>(double v)
161 { int iv = cvRound(v); return saturate_cast<schar>(iv); }
163 template<> inline ushort saturate_cast<ushort>(schar v)
164 { return (ushort)std::max((int)v, 0); }
165 template<> inline ushort saturate_cast<ushort>(short v)
166 { return (ushort)std::max((int)v, 0); }
167 template<> inline ushort saturate_cast<ushort>(int v)
168 { return (ushort)((unsigned)v <= (unsigned)USHRT_MAX ? v : v > 0 ? USHRT_MAX : 0); }
169 template<> inline ushort saturate_cast<ushort>(unsigned v)
170 { return (ushort)std::min(v, (unsigned)USHRT_MAX); }
171 template<> inline ushort saturate_cast<ushort>(float v)
172 { int iv = cvRound(v); return saturate_cast<ushort>(iv); }
173 template<> inline ushort saturate_cast<ushort>(double v)
174 { int iv = cvRound(v); return saturate_cast<ushort>(iv); }
176 template<> inline short saturate_cast<short>(ushort v)
177 { return (short)std::min((int)v, SHRT_MAX); }
178 template<> inline short saturate_cast<short>(int v)
180 return (short)((unsigned)(v - SHRT_MIN) <= (unsigned)USHRT_MAX ?
181 v : v > 0 ? SHRT_MAX : SHRT_MIN);
183 template<> inline short saturate_cast<short>(unsigned v)
184 { return (short)std::min(v, (unsigned)SHRT_MAX); }
185 template<> inline short saturate_cast<short>(float v)
186 { int iv = cvRound(v); return saturate_cast<short>(iv); }
187 template<> inline short saturate_cast<short>(double v)
188 { int iv = cvRound(v); return saturate_cast<short>(iv); }
190 template<> inline int saturate_cast<int>(float v) { return cvRound(v); }
191 template<> inline int saturate_cast<int>(double v) { return cvRound(v); }
193 // we intentionally do not clip negative numbers, to make -1 become 0xffffffff etc.
194 template<> inline unsigned saturate_cast<unsigned>(float v){ return cvRound(v); }
195 template<> inline unsigned saturate_cast<unsigned>(double v) { return cvRound(v); }
198 /////////////////////////// short vector (Vec) /////////////////////////////
200 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec() {}
201 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0)
204 for(int i = 1; i < cn; i++) val[i] = _Tp();
207 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1)
210 val[0] = v0; val[1] = v1;
211 for(int i = 2; i < cn; i++) val[i] = _Tp(0);
214 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2)
217 val[0] = v0; val[1] = v1; val[2] = v2;
218 for(int i = 3; i < cn; i++) val[i] = _Tp(0);
221 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
224 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
225 for(int i = 4; i < cn; i++) val[i] = _Tp(0);
228 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3, _Tp v4)
231 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3; val[4] = v4;
232 for(int i = 5; i < cn; i++) val[i] = _Tp(0);
235 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
239 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
240 val[4] = v4; val[5] = v5;
241 for(int i = 6; i < cn; i++) val[i] = _Tp(0);
244 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
245 _Tp v4, _Tp v5, _Tp v6)
248 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
249 val[4] = v4; val[5] = v5; val[6] = v6;
250 for(int i = 7; i < cn; i++) val[i] = _Tp(0);
253 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
254 _Tp v4, _Tp v5, _Tp v6, _Tp v7)
257 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
258 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
259 for(int i = 8; i < cn; i++) val[i] = _Tp(0);
262 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
263 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
267 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
268 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
270 for(int i = 9; i < cn; i++) val[i] = _Tp(0);
273 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(_Tp v0, _Tp v1, _Tp v2, _Tp v3,
274 _Tp v4, _Tp v5, _Tp v6, _Tp v7,
278 val[0] = v0; val[1] = v1; val[2] = v2; val[3] = v3;
279 val[4] = v4; val[5] = v5; val[6] = v6; val[7] = v7;
280 val[8] = v8; val[9] = v9;
281 for(int i = 10; i < cn; i++) val[i] = _Tp(0);
285 template<typename _Tp, int cn> inline Vec<_Tp, cn>::Vec(const Vec<_Tp, cn>& v)
287 for( int i = 0; i < cn; i++ ) val[i] = v.val[i];
290 template<typename _Tp, int cn> inline Vec<_Tp, cn> Vec<_Tp, cn>::all(_Tp alpha)
293 for( int i = 0; i < cn; i++ ) v.val[i] = alpha;
297 template<typename _Tp, int cn> inline _Tp Vec<_Tp, cn>::dot(const Vec<_Tp, cn>& v) const
300 for( int i = 0; i < cn; i++ ) s += val[i]*v.val[i];
304 template<typename _Tp, int cn> inline double Vec<_Tp, cn>::ddot(const Vec<_Tp, cn>& v) const
307 for( int i = 0; i < cn; i++ ) s += (double)val[i]*v.val[i];
311 template<typename _Tp, int cn> inline Vec<_Tp, cn> Vec<_Tp, cn>::cross(const Vec<_Tp, cn>& v) const
313 return Vec<_Tp, cn>(); // for arbitrary-size vector there is no cross-product defined
316 template<typename _Tp, int cn> template<typename T2>
317 inline Vec<_Tp, cn>::operator Vec<T2, cn>() const
320 for( int i = 0; i < cn; i++ ) v.val[i] = saturate_cast<T2>(val[i]);
324 template<typename _Tp, int cn> inline Vec<_Tp, cn>::operator CvScalar() const
326 CvScalar s = {{0,0,0,0}};
328 for( i = 0; i < std::min(cn, 4); i++ ) s.val[i] = val[i];
329 for( ; i < 4; i++ ) s.val[i] = 0;
333 template<typename _Tp, int cn> inline _Tp Vec<_Tp, cn>::operator [](int i) const { return val[i]; }
334 template<typename _Tp, int cn> inline _Tp& Vec<_Tp, cn>::operator[](int i) { return val[i]; }
336 template<typename _Tp1, typename _Tp2, int cn> static inline Vec<_Tp1, cn>&
337 operator += (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
339 for( int i = 0; i < cn; i++ )
340 a.val[i] = saturate_cast<_Tp1>(a.val[i] + b.val[i]);
344 template<typename _Tp1, typename _Tp2, int cn> static inline Vec<_Tp1, cn>&
345 operator -= (Vec<_Tp1, cn>& a, const Vec<_Tp2, cn>& b)
347 for( int i = 0; i < cn; i++ )
348 a.val[i] = saturate_cast<_Tp1>(a.val[i] - b.val[i]);
352 template<typename _Tp, int cn> static inline Vec<_Tp, cn>
353 operator + (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
359 template<typename _Tp, int cn> static inline Vec<_Tp, cn>
360 operator - (const Vec<_Tp, cn>& a, const Vec<_Tp, cn>& b)
366 template<typename _Tp> static inline
367 Vec<_Tp, 2>& operator *= (Vec<_Tp, 2>& a, _Tp alpha)
369 a[0] *= alpha; a[1] *= alpha;
373 template<typename _Tp> static inline
374 Vec<_Tp, 3>& operator *= (Vec<_Tp, 3>& a, _Tp alpha)
376 a[0] *= alpha; a[1] *= alpha; a[2] *= alpha;
380 template<typename _Tp> static inline
381 Vec<_Tp, 4>& operator *= (Vec<_Tp, 4>& a, _Tp alpha)
383 a[0] *= alpha; a[1] *= alpha; a[2] *= alpha; a[3] *= alpha;
387 template<typename _Tp, int cn> static inline Vec<_Tp, cn>
388 operator * (const Vec<_Tp, cn>& a, _Tp alpha)
394 template<typename _Tp, int cn> static inline Vec<_Tp, cn>
395 operator * (_Tp alpha, const Vec<_Tp, cn>& a)
400 template<typename _Tp, int cn> static inline Vec<_Tp, cn>
401 operator - (const Vec<_Tp, cn>& a)
404 for( int i = 0; i < cn; i++ ) t.val[i] = saturate_cast<_Tp>(-a.val[i]);
408 template<> inline Vec<float, 3> Vec<float, 3>::cross(const Vec<float, 3>& v) const
410 return Vec<float,3>(val[1]*v.val[2] - val[2]*v.val[1],
411 val[2]*v.val[0] - val[0]*v.val[2],
412 val[0]*v.val[1] - val[1]*v.val[0]);
415 template<> inline Vec<double, 3> Vec<double, 3>::cross(const Vec<double, 3>& v) const
417 return Vec<double,3>(val[1]*v.val[2] - val[2]*v.val[1],
418 val[2]*v.val[0] - val[0]*v.val[2],
419 val[0]*v.val[1] - val[1]*v.val[0]);
422 template<typename T1, typename T2> static inline
423 Vec<T1, 2>& operator += (Vec<T1, 2>& a, const Vec<T2, 2>& b)
425 a[0] = saturate_cast<T1>(a[0] + b[0]);
426 a[1] = saturate_cast<T1>(a[1] + b[1]);
430 template<typename T1, typename T2> static inline
431 Vec<T1, 3>& operator += (Vec<T1, 3>& a, const Vec<T2, 3>& b)
433 a[0] = saturate_cast<T1>(a[0] + b[0]);
434 a[1] = saturate_cast<T1>(a[1] + b[1]);
435 a[2] = saturate_cast<T1>(a[2] + b[2]);
439 template<typename T1, typename T2> static inline
440 Vec<T1, 4>& operator += (Vec<T1, 4>& a, const Vec<T2, 4>& b)
442 a[0] = saturate_cast<T1>(a[0] + b[0]);
443 a[1] = saturate_cast<T1>(a[1] + b[1]);
444 a[2] = saturate_cast<T1>(a[2] + b[2]);
445 a[3] = saturate_cast<T1>(a[3] + b[3]);
449 template<typename T1, int n> static inline
450 double norm(const Vec<T1, n>& a)
453 for( int i = 0; i < n; i++ )
454 s += (double)a.val[i]*a.val[i];
458 template<typename T1, int n> static inline
459 bool operator == (const Vec<T1, n>& a, const Vec<T1, n>& b)
461 for( int i = 0; i < n; i++ )
462 if( a[i] != b[i] ) return false;
466 template<typename T1, int n> static inline
467 bool operator != (const Vec<T1, n>& a, const Vec<T1, n>& b)
472 //////////////////////////////// Complex //////////////////////////////
474 template<typename _Tp> inline Complex<_Tp>::Complex() : re(0), im(0) {}
475 template<typename _Tp> inline Complex<_Tp>::Complex( _Tp _re, _Tp _im ) : re(_re), im(_im) {}
476 template<typename _Tp> template<typename T2> inline Complex<_Tp>::operator Complex<T2>() const
477 { return Complex<T2>(saturate_cast<T2>(re), saturate_cast<T2>(im)); }
478 template<typename _Tp> inline Complex<_Tp> Complex<_Tp>::conj() const
479 { return Complex<_Tp>(re, -im); }
481 template<typename _Tp> static inline
482 bool operator == (const Complex<_Tp>& a, const Complex<_Tp>& b)
483 { return a.re == b.re && a.im == b.im; }
485 template<typename _Tp> static inline
486 Complex<_Tp> operator + (const Complex<_Tp>& a, const Complex<_Tp>& b)
487 { return Complex<_Tp>( a.re + b.re, a.im + b.im ); }
489 template<typename _Tp> static inline
490 Complex<_Tp>& operator += (Complex<_Tp>& a, const Complex<_Tp>& b)
491 { a.re += b.re; a.im += b.im; return a; }
493 template<typename _Tp> static inline
494 Complex<_Tp> operator - (const Complex<_Tp>& a, const Complex<_Tp>& b)
495 { return Complex<_Tp>( a.re - b.re, a.im - b.im ); }
497 template<typename _Tp> static inline
498 Complex<_Tp>& operator -= (Complex<_Tp>& a, const Complex<_Tp>& b)
499 { a.re -= b.re; a.im -= b.im; return a; }
501 template<typename _Tp> static inline
502 Complex<_Tp> operator - (const Complex<_Tp>& a)
503 { return Complex<_Tp>(-a.re, -a.im); }
505 template<typename _Tp> static inline
506 Complex<_Tp> operator * (const Complex<_Tp>& a, const Complex<_Tp>& b)
507 { return Complex<_Tp>( a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re ); }
509 template<typename _Tp> static inline
510 Complex<_Tp> operator * (const Complex<_Tp>& a, _Tp b)
511 { return Complex<_Tp>( a.re*b, a.im*b ); }
513 template<typename _Tp> static inline
514 Complex<_Tp> operator * (_Tp b, const Complex<_Tp>& a)
515 { return Complex<_Tp>( a.re*b, a.im*b ); }
517 template<typename _Tp> static inline
518 Complex<_Tp> operator + (const Complex<_Tp>& a, _Tp b)
519 { return Complex<_Tp>( a.re + b, a.im ); }
521 template<typename _Tp> static inline
522 Complex<_Tp> operator - (const Complex<_Tp>& a, _Tp b)
523 { return Complex<_Tp>( a.re - b, a.im ); }
525 template<typename _Tp> static inline
526 Complex<_Tp> operator + (_Tp b, const Complex<_Tp>& a)
527 { return Complex<_Tp>( a.re + b, a.im ); }
529 template<typename _Tp> static inline
530 Complex<_Tp> operator - (_Tp b, const Complex<_Tp>& a)
531 { return Complex<_Tp>( b - a.re, -a.im ); }
533 template<typename _Tp> static inline
534 Complex<_Tp>& operator += (Complex<_Tp>& a, _Tp b)
535 { a.re += b; return a; }
537 template<typename _Tp> static inline
538 Complex<_Tp>& operator -= (Complex<_Tp>& a, _Tp b)
539 { a.re -= b; return a; }
541 template<typename _Tp> static inline
542 Complex<_Tp>& operator *= (Complex<_Tp>& a, _Tp b)
543 { a.re *= b; a.im *= b; return a; }
545 template<typename _Tp> static inline
546 double abs(const Complex<_Tp>& a)
547 { return std::sqrt( (double)a.re*a.re + (double)a.im*a.im); }
549 template<typename _Tp> static inline
550 Complex<_Tp> operator / (const Complex<_Tp>& a, const Complex<_Tp>& b)
552 double t = 1./((double)b.re*b.re + (double)b.im*b.im);
553 return Complex<_Tp>( (_Tp)((a.re*b.re + a.im*b.im)*t),
554 (_Tp)((-a.re*b.im + a.im*b.re)*t) );
557 template<typename _Tp> static inline
558 Complex<_Tp>& operator /= (Complex<_Tp>& a, const Complex<_Tp>& b)
563 template<typename _Tp> static inline
564 Complex<_Tp> operator / (const Complex<_Tp>& a, _Tp b)
567 return Complex<_Tp>( a.re*t, a.im*t );
570 template<typename _Tp> static inline
571 Complex<_Tp> operator / (_Tp b, const Complex<_Tp>& a)
573 return Complex<_Tp>(b)/a;
576 template<typename _Tp> static inline
577 Complex<_Tp> operator /= (const Complex<_Tp>& a, _Tp b)
580 a.re *= t; a.im *= t; return a;
583 //////////////////////////////// 2D Point ////////////////////////////////
585 template<typename _Tp> inline Point_<_Tp>::Point_() : x(0), y(0) {}
586 template<typename _Tp> inline Point_<_Tp>::Point_(_Tp _x, _Tp _y) : x(_x), y(_y) {}
587 template<typename _Tp> inline Point_<_Tp>::Point_(const Point_& pt) : x(pt.x), y(pt.y) {}
588 template<typename _Tp> inline Point_<_Tp>::Point_(const CvPoint& pt) : x((_Tp)pt.x), y((_Tp)pt.y) {}
589 template<typename _Tp> inline Point_<_Tp>::Point_(const CvPoint2D32f& pt)
590 : x(saturate_cast<_Tp>(pt.x)), y(saturate_cast<_Tp>(pt.y)) {}
591 template<typename _Tp> inline Point_<_Tp>::Point_(const Size_<_Tp>& sz) : x(sz.width), y(sz.height) {}
592 template<typename _Tp> inline Point_<_Tp>::Point_(const Vec<_Tp,2>& v) : x(v[0]), y(v[1]) {}
593 template<typename _Tp> inline Point_<_Tp>& Point_<_Tp>::operator = (const Point_& pt)
594 { x = pt.x; y = pt.y; return *this; }
596 template<typename _Tp> template<typename _Tp2> inline Point_<_Tp>::operator Point_<_Tp2>() const
597 { return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y)); }
598 template<typename _Tp> inline Point_<_Tp>::operator CvPoint() const
599 { return cvPoint(saturate_cast<int>(x), saturate_cast<int>(y)); }
600 template<typename _Tp> inline Point_<_Tp>::operator CvPoint2D32f() const
601 { return cvPoint2D32f((float)x, (float)y); }
602 template<typename _Tp> inline Point_<_Tp>::operator Vec<_Tp, 2>() const
603 { return Vec<_Tp, 2>(x, y); }
605 template<typename _Tp> inline _Tp Point_<_Tp>::dot(const Point_& pt) const
606 { return x*pt.x + y*pt.y; }
607 template<typename _Tp> inline double Point_<_Tp>::ddot(const Point_& pt) const
608 { return (double)x*pt.x + (double)y*pt.y; }
610 template<typename _Tp> static inline Point_<_Tp>&
611 operator += (Point_<_Tp>& a, const Point_<_Tp>& b) { a.x += b.x; a.y += b.y; return a; }
613 template<typename _Tp> static inline Point_<_Tp>&
614 operator -= (Point_<_Tp>& a, const Point_<_Tp>& b) { a.x -= b.x; a.y -= b.y; return a; }
616 template<typename _Tp> static inline Point_<_Tp>&
617 operator *= (Point_<_Tp>& a, _Tp b) { a.x *= b; a.y *= b; return a; }
619 template<typename _Tp> static inline double norm(const Point_<_Tp>& pt)
620 { return std::sqrt((double)pt.x*pt.x + (double)pt.y*pt.y); }
622 template<typename _Tp> static inline bool operator == (const Point_<_Tp>& a, const Point_<_Tp>& b)
623 { return a.x == b.x && a.y == b.y; }
625 template<typename _Tp> static inline bool operator != (const Point_<_Tp>& a, const Point_<_Tp>& b)
626 { return !(a == b); }
628 template<typename _Tp> static inline Point_<_Tp> operator + (const Point_<_Tp>& a, const Point_<_Tp>& b)
629 { return Point_<_Tp>( a.x + b.x, a.y + b.y ); }
631 template<typename _Tp> static inline Point_<_Tp> operator - (const Point_<_Tp>& a, const Point_<_Tp>& b)
632 { return Point_<_Tp>( a.x - b.x, a.y - b.y ); }
634 template<typename _Tp> static inline Point_<_Tp> operator - (const Point_<_Tp>& a)
635 { return Point_<_Tp>( -a.x, -a.y ); }
637 template<typename _Tp> static inline Point_<_Tp> operator * (const Point_<_Tp>& a, _Tp b)
638 { return Point_<_Tp>( a.x*b, a.y*b ); }
640 template<typename _Tp> static inline Point_<_Tp> operator * (_Tp a, const Point_<_Tp>& b)
641 { return Point_<_Tp>( a*b.x, a*b.y ); }
643 //////////////////////////////// 3D Point ////////////////////////////////
645 template<typename _Tp> inline Point3_<_Tp>::Point3_() : x(0), y(0), z(0) {}
646 template<typename _Tp> inline Point3_<_Tp>::Point3_(_Tp _x, _Tp _y, _Tp _z) : x(_x), y(_y), z(_z) {}
647 template<typename _Tp> inline Point3_<_Tp>::Point3_(const Point3_& pt) : x(pt.x), y(pt.y), z(pt.z) {}
648 template<typename _Tp> inline Point3_<_Tp>::Point3_(const Point_<_Tp>& pt) : x(pt.x), y(pt.y), z(_Tp()) {}
649 template<typename _Tp> inline Point3_<_Tp>::Point3_(const CvPoint3D32f& pt) :
650 x(saturate_cast<_Tp>(pt.x)), y(saturate_cast<_Tp>(pt.y)), z(saturate_cast<_Tp>(pt.z)) {}
651 template<typename _Tp> inline Point3_<_Tp>::Point3_(const Vec<_Tp, 3>& v) : x(v[0]), y(v[1]), z(v[2]) {}
653 template<typename _Tp> template<typename _Tp2> inline Point3_<_Tp>::operator Point3_<_Tp2>() const
654 { return Point3_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(z)); }
656 template<typename _Tp> inline Point3_<_Tp>::operator CvPoint3D32f() const
657 { return cvPoint3D32f((float)x, (float)y, (float)z); }
659 template<typename _Tp> inline Point3_<_Tp>::operator Vec<_Tp, 3>() const
660 { return Vec<_Tp, 3>(x, y, z); }
662 template<typename _Tp> inline Point3_<_Tp>& Point3_<_Tp>::operator = (const Point3_& pt)
663 { x = pt.x; y = pt.y; z = pt.z; return *this; }
665 template<typename _Tp> inline _Tp Point3_<_Tp>::dot(const Point3_& pt) const
666 { return x*pt.x + y*pt.y + z*pt.z; }
667 template<typename _Tp> inline double Point3_<_Tp>::ddot(const Point3_& pt) const
668 { return (double)x*pt.x + (double)y*pt.y + (double)z*pt.z; }
670 template<typename _Tp> static inline Point3_<_Tp>&
671 operator += (Point3_<_Tp>& a, const Point3_<_Tp>& b) { a.x += b.x; a.y += b.y; a.z += b.z; return a; }
672 template<typename _Tp> static inline Point3_<_Tp>&
673 operator -= (Point3_<_Tp>& a, const Point3_<_Tp>& b) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; }
675 template<typename _Tp> static inline double norm(const Point3_<_Tp>& pt)
676 { return std::sqrt((double)pt.x*pt.x + (double)pt.y*pt.y + (double)pt.z*pt.z); }
678 template<typename _Tp> static inline bool operator == (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
679 { return a.x == b.x && a.y == b.y && a.z == b.z; }
681 template<typename _Tp> static inline Point3_<_Tp> operator + (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
682 { return Point3_<_Tp>( a.x + b.x, a.y + b.y, a.z + b.z ); }
684 template<typename _Tp> static inline Point3_<_Tp> operator - (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
685 { return Point3_<_Tp>( a.x - b.x, a.y - b.y, a.z - b.z ); }
687 template<typename _Tp> static inline Point3_<_Tp> operator - (const Point3_<_Tp>& a)
688 { return Point3_<_Tp>( -a.x, -a.y, -a.z ); }
690 template<typename _Tp> static inline Point3_<_Tp> operator * (const Point3_<_Tp>& a, _Tp b)
691 { return Point3_<_Tp>( a.x*b, a.y*b, a.z*b ); }
693 template<typename _Tp> static inline Point3_<_Tp> operator * (_Tp a, const Point3_<_Tp>& b)
694 { return Point3_<_Tp>( a*b.x, a*b.y, a*b.z ); }
696 //////////////////////////////// Size ////////////////////////////////
698 template<typename _Tp> inline Size_<_Tp>::Size_()
699 : width(0), height(0) {}
700 template<typename _Tp> inline Size_<_Tp>::Size_(_Tp _width, _Tp _height)
701 : width(_width), height(_height) {}
702 template<typename _Tp> inline Size_<_Tp>::Size_(const Size_& sz)
703 : width(sz.width), height(sz.height) {}
704 template<typename _Tp> inline Size_<_Tp>::Size_(const CvSize& sz)
705 : width(saturate_cast<_Tp>(sz.width)), height(saturate_cast<_Tp>(sz.height)) {}
706 template<typename _Tp> inline Size_<_Tp>::Size_(const CvSize2D32f& sz)
707 : width(saturate_cast<_Tp>(sz.width)), height(saturate_cast<_Tp>(sz.height)) {}
708 template<typename _Tp> inline Size_<_Tp>::Size_(const Point_<_Tp>& pt) : width(pt.x), height(pt.y) {}
710 template<typename _Tp> template<typename _Tp2> inline Size_<_Tp>::operator Size_<_Tp2>() const
711 { return Size_<_Tp2>(saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); }
712 template<typename _Tp> inline Size_<_Tp>::operator CvSize() const
713 { return cvSize(saturate_cast<int>(width), saturate_cast<int>(height)); }
714 template<typename _Tp> inline Size_<_Tp>::operator CvSize2D32f() const
715 { return cvSize2D32f((float)width, (float)height); }
717 template<typename _Tp> inline Size_<_Tp>& Size_<_Tp>::operator = (const Size_<_Tp>& sz)
718 { width = sz.width; height = sz.height; return *this; }
719 template<typename _Tp> static inline Size_<_Tp> operator * (const Size_<_Tp>& a, _Tp b)
720 { return Size_<_Tp>(a.width * b, a.height * b); }
721 template<typename _Tp> static inline Size_<_Tp> operator + (const Size_<_Tp>& a, const Size_<_Tp>& b)
722 { return Size_<_Tp>(a.width + b.width, a.height + b.height); }
723 template<typename _Tp> static inline Size_<_Tp> operator - (const Size_<_Tp>& a, const Size_<_Tp>& b)
724 { return Size_<_Tp>(a.width - b.width, a.height - b.height); }
725 template<typename _Tp> inline _Tp Size_<_Tp>::area() const { return width*height; }
727 template<typename _Tp> static inline Size_<_Tp>& operator += (Size_<_Tp>& a, const Size_<_Tp>& b)
728 { a.width += b.width; a.height += b.height; return a; }
729 template<typename _Tp> static inline Size_<_Tp>& operator -= (Size_<_Tp>& a, const Size_<_Tp>& b)
730 { a.width -= b.width; a.height -= b.height; return a; }
732 template<typename _Tp> static inline bool operator == (const Size_<_Tp>& a, const Size_<_Tp>& b)
733 { return a.width == b.width && a.height == b.height; }
734 template<typename _Tp> static inline bool operator != (const Size_<_Tp>& a, const Size_<_Tp>& b)
735 { return a.width != b.width || a.height != b.height; }
737 //////////////////////////////// Rect ////////////////////////////////
740 template<typename _Tp> inline Rect_<_Tp>::Rect_() : x(0), y(0), width(0), height(0) {}
741 template<typename _Tp> inline Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height) : x(_x), y(_y), width(_width), height(_height) {}
742 template<typename _Tp> inline Rect_<_Tp>::Rect_(const Rect_<_Tp>& r) : x(r.x), y(r.y), width(r.width), height(r.height) {}
743 template<typename _Tp> inline Rect_<_Tp>::Rect_(const CvRect& r) : x((_Tp)r.x), y((_Tp)r.y), width((_Tp)r.width), height((_Tp)r.height) {}
744 template<typename _Tp> inline Rect_<_Tp>::Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz) :
745 x(org.x), y(org.y), width(sz.width), height(sz.height) {}
746 template<typename _Tp> inline Rect_<_Tp>::Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2)
748 x = std::min(pt1.x, pt2.x); y = std::min(pt1.y, pt2.y);
749 width = std::max(pt1.x, pt2.x) - x; height = std::max(pt1.y, pt2.y) - y;
751 template<typename _Tp> inline Rect_<_Tp>& Rect_<_Tp>::operator = ( const Rect_<_Tp>& r )
752 { x = r.x; y = r.y; width = r.width; height = r.height; return *this; }
754 template<typename _Tp> inline Point_<_Tp> Rect_<_Tp>::tl() const { return Point_<_Tp>(x,y); }
755 template<typename _Tp> inline Point_<_Tp> Rect_<_Tp>::br() const { return Point_<_Tp>(x+width, y+height); }
757 template<typename _Tp> static inline Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Point_<_Tp>& b )
758 { a.x += b.x; a.y += b.y; return a; }
759 template<typename _Tp> static inline Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Point_<_Tp>& b )
760 { a.x -= b.x; a.y -= b.y; return a; }
762 template<typename _Tp> static inline Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Size_<_Tp>& b )
763 { a.width += b.width; a.height += b.height; return a; }
765 template<typename _Tp> static inline Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Size_<_Tp>& b )
766 { a.width -= b.width; a.height -= b.height; return a; }
768 template<typename _Tp> static inline Rect_<_Tp>& operator &= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
770 _Tp x1 = std::max(a.x, b.x), y1 = std::max(a.y, b.y);
771 a.width = std::min(a.x + a.width, b.x + b.width) - x1;
772 a.height = std::min(a.y + a.height, b.y + b.height) - y1;
774 if( a.width <= 0 || a.height <= 0 )
779 template<typename _Tp> static inline Rect_<_Tp>& operator |= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
781 _Tp x1 = std::min(a.x, b.x), y1 = std::min(a.y, b.y);
782 a.width = std::max(a.x + a.width, b.x + b.width) - x1;
783 a.height = std::max(a.y + a.height, b.y + b.height) - y1;
788 template<typename _Tp> inline Size_<_Tp> Rect_<_Tp>::size() const { return Size_<_Tp>(width, height); }
789 template<typename _Tp> inline _Tp Rect_<_Tp>::area() const { return width*height; }
791 template<typename _Tp> template<typename _Tp2> inline Rect_<_Tp>::operator Rect_<_Tp2>() const
792 { return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y),
793 saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height)); }
794 template<typename _Tp> inline Rect_<_Tp>::operator CvRect() const
795 { return cvRect(saturate_cast<int>(x), saturate_cast<int>(y),
796 saturate_cast<int>(width), saturate_cast<int>(height)); }
798 template<typename _Tp> inline bool Rect_<_Tp>::contains(const Point_<_Tp>& pt) const
799 { return x <= pt.x && pt.x < x + width && y <= pt.y && pt.y < y + height; }
801 template<typename _Tp> static inline bool operator == (const Rect_<_Tp>& a, const Rect_<_Tp>& b)
803 return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height;
806 template<typename _Tp> static inline Rect_<_Tp> operator + (const Rect_<_Tp>& a, const Point_<_Tp>& b)
808 return Rect_<_Tp>( a.x + b.x, a.y + b.y, a.width, a.height );
811 template<typename _Tp> static inline Rect_<_Tp> operator - (const Rect_<_Tp>& a, const Point_<_Tp>& b)
813 return Rect_<_Tp>( a.x - b.x, a.y - b.y, a.width, a.height );
816 template<typename _Tp> static inline Rect_<_Tp> operator + (const Rect_<_Tp>& a, const Size_<_Tp>& b)
818 return Rect_<_Tp>( a.x, a.y, a.width + b.width, a.height + b.height );
821 template<typename _Tp> static inline Rect_<_Tp> operator & (const Rect_<_Tp>& a, const Rect_<_Tp>& b)
827 template<typename _Tp> static inline Rect_<_Tp> operator | (const Rect_<_Tp>& a, const Rect_<_Tp>& b)
833 template<typename _Tp> inline bool Point_<_Tp>::inside( const Rect_<_Tp>& r ) const
835 return r.contains(*this);
838 inline RotatedRect::RotatedRect() { angle = 0; }
839 inline RotatedRect::RotatedRect(const Point2f& _center, const Size2f& _size, float _angle)
840 : center(_center), size(_size), angle(_angle) {}
841 inline RotatedRect::RotatedRect(const CvBox2D& box)
842 : center(box.center), size(box.size), angle(box.angle) {}
843 inline RotatedRect::operator CvBox2D() const
845 CvBox2D box; box.center = center; box.size = size; box.angle = angle;
848 inline void RotatedRect::points(Point2f pt[]) const
850 double _angle = angle*CV_PI/180.;
851 float a = (float)cos(_angle)*0.5f;
852 float b = (float)sin(_angle)*0.5f;
854 pt[0].x = center.x - a*size.height - b*size.width;
855 pt[0].y = center.y + b*size.height - a*size.width;
856 pt[1].x = center.x + a*size.height - b*size.width;
857 pt[1].y = center.y - b*size.height - a*size.width;
858 pt[2].x = 2*center.x - pt[0].x;
859 pt[2].y = 2*center.y - pt[0].y;
860 pt[3].x = 2*center.x - pt[1].x;
861 pt[3].y = 2*center.y - pt[1].y;
864 inline Rect RotatedRect::boundingRect() const
868 Rect r(cvFloor(min(min(min(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
869 cvFloor(min(min(min(pt[0].y, pt[1].y), pt[2].y), pt[3].y)),
870 cvCeil(max(max(max(pt[0].x, pt[1].x), pt[2].x), pt[3].x)),
871 cvCeil(max(max(max(pt[0].y, pt[1].y), pt[2].y), pt[3].y)));
877 //////////////////////////////// Scalar_ ///////////////////////////////
879 template<typename _Tp> inline Scalar_<_Tp>::Scalar_()
880 { this->val[0] = this->val[1] = this->val[2] = this->val[3] = 0; }
882 template<typename _Tp> inline Scalar_<_Tp>::Scalar_(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
883 { this->val[0] = v0; this->val[1] = v1; this->val[2] = v2; this->val[3] = v3; }
885 template<typename _Tp> inline Scalar_<_Tp>::Scalar_(const CvScalar& s)
887 this->val[0] = saturate_cast<_Tp>(s.val[0]);
888 this->val[1] = saturate_cast<_Tp>(s.val[1]);
889 this->val[2] = saturate_cast<_Tp>(s.val[2]);
890 this->val[3] = saturate_cast<_Tp>(s.val[3]);
893 template<typename _Tp> inline Scalar_<_Tp>::Scalar_(_Tp v0)
894 { this->val[0] = v0; this->val[1] = this->val[2] = this->val[3] = 0; }
896 template<typename _Tp> inline Scalar_<_Tp> Scalar_<_Tp>::all(_Tp v0)
897 { return Scalar_<_Tp>(v0, v0, v0, v0); }
898 template<typename _Tp> inline Scalar_<_Tp>::operator CvScalar() const
899 { return cvScalar(this->val[0], this->val[1], this->val[2], this->val[3]); }
901 template<typename _Tp> template<typename T2> inline Scalar_<_Tp>::operator Scalar_<T2>() const
903 return Scalar_<T2>(saturate_cast<T2>(this->val[0]),
904 saturate_cast<T2>(this->val[1]),
905 saturate_cast<T2>(this->val[2]),
906 saturate_cast<T2>(this->val[3]));
909 template<typename _Tp> static inline Scalar_<_Tp>& operator += (Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
911 a.val[0] = saturate_cast<_Tp>(a.val[0] + b.val[0]);
912 a.val[1] = saturate_cast<_Tp>(a.val[1] + b.val[1]);
913 a.val[2] = saturate_cast<_Tp>(a.val[2] + b.val[2]);
914 a.val[3] = saturate_cast<_Tp>(a.val[3] + b.val[3]);
918 template<typename _Tp> static inline Scalar_<_Tp>& operator -= (Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
920 a.val[0] = saturate_cast<_Tp>(a.val[0] - b.val[0]);
921 a.val[1] = saturate_cast<_Tp>(a.val[1] - b.val[1]);
922 a.val[2] = saturate_cast<_Tp>(a.val[2] - b.val[2]);
923 a.val[3] = saturate_cast<_Tp>(a.val[3] - b.val[3]);
927 template<typename _Tp> static inline Scalar_<_Tp>& operator *= ( Scalar_<_Tp>& a, _Tp v )
929 a.val[0] = saturate_cast<_Tp>(a.val[0] * v);
930 a.val[1] = saturate_cast<_Tp>(a.val[1] * v);
931 a.val[2] = saturate_cast<_Tp>(a.val[2] * v);
932 a.val[3] = saturate_cast<_Tp>(a.val[3] * v);
936 template<typename _Tp> inline Scalar_<_Tp> Scalar_<_Tp>::mul(const Scalar_<_Tp>& t, double scale ) const
938 return Scalar_<_Tp>( saturate_cast<_Tp>(this->val[0]*t.val[0]*scale),
939 saturate_cast<_Tp>(this->val[1]*t.val[1]*scale),
940 saturate_cast<_Tp>(this->val[2]*t.val[2]*scale),
941 saturate_cast<_Tp>(this->val[3]*t.val[3]*scale));
944 template<typename _Tp> static inline bool operator == ( const Scalar_<_Tp>& a, const Scalar_<_Tp>& b )
946 return a.val[0] == b.val[0] && a.val[1] == b.val[1] &&
947 a.val[2] == b.val[2] && a.val[3] == b.val[3];
950 template<typename _Tp> static inline bool operator != ( const Scalar_<_Tp>& a, const Scalar_<_Tp>& b )
952 return a.val[0] != b.val[0] || a.val[1] != b.val[1] ||
953 a.val[2] != b.val[2] || a.val[3] != b.val[3];
956 template<typename _Tp> template<typename T2> inline void Scalar_<_Tp>::convertTo(T2* buf, int cn, int unroll_to) const
960 for( i = 0; i < cn; i++ )
961 buf[i] = saturate_cast<T2>(this->val[i]);
962 for( ; i < unroll_to; i++ )
966 static inline void scalarToRawData(const Scalar& s, void* buf, int type, int unroll_to=0)
968 int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type);
972 s.convertTo((uchar*)buf, cn, unroll_to);
975 s.convertTo((schar*)buf, cn, unroll_to);
978 s.convertTo((ushort*)buf, cn, unroll_to);
981 s.convertTo((short*)buf, cn, unroll_to);
984 s.convertTo((int*)buf, cn, unroll_to);
987 s.convertTo((float*)buf, cn, unroll_to);
990 s.convertTo((double*)buf, cn, unroll_to);
993 CV_Error(CV_StsUnsupportedFormat,"");
998 template<typename _Tp> static inline Scalar_<_Tp> operator + (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
1000 return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] + b.val[0]),
1001 saturate_cast<_Tp>(a.val[1] + b.val[1]),
1002 saturate_cast<_Tp>(a.val[2] + b.val[2]),
1003 saturate_cast<_Tp>(a.val[3] + b.val[3]));
1006 template<typename _Tp> static inline Scalar_<_Tp> operator - (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
1008 return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] - b.val[0]),
1009 saturate_cast<_Tp>(a.val[1] - b.val[1]),
1010 saturate_cast<_Tp>(a.val[2] - b.val[2]),
1011 saturate_cast<_Tp>(a.val[3] - b.val[3]));
1014 template<typename _Tp> static inline Scalar_<_Tp> operator * (const Scalar_<_Tp>& a, _Tp alpha)
1016 return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] * alpha),
1017 saturate_cast<_Tp>(a.val[1] * alpha),
1018 saturate_cast<_Tp>(a.val[2] * alpha),
1019 saturate_cast<_Tp>(a.val[3] * alpha));
1022 template<typename _Tp> static inline Scalar_<_Tp> operator * (_Tp alpha, const Scalar_<_Tp>& a)
1027 template<typename _Tp> static inline Scalar_<_Tp> operator - (const Scalar_<_Tp>& a)
1029 return Scalar_<_Tp>(saturate_cast<_Tp>(-a.val[0]), saturate_cast<_Tp>(-a.val[1]),
1030 saturate_cast<_Tp>(-a.val[2]), saturate_cast<_Tp>(-a.val[3]));
1033 //////////////////////////////// Range /////////////////////////////////
1035 inline Range::Range() : start(0), end(0) {}
1036 inline Range::Range(int _start, int _end) : start(_start), end(_end) {}
1037 inline Range::Range(const CvSlice& slice) : start(slice.start_index), end(slice.end_index)
1039 if( start == 0 && end == CV_WHOLE_SEQ_END_INDEX )
1040 *this = Range::all();
1043 inline int Range::size() const { return end - start; }
1044 inline bool Range::empty() const { return start == end; }
1045 inline Range Range::all() { return Range(INT_MIN, INT_MAX); }
1047 static inline bool operator == (const Range& r1, const Range& r2)
1048 { return r1.start == r2.start && r1.end == r2.end; }
1050 static inline bool operator != (const Range& r1, const Range& r2)
1051 { return !(r1 == r2); }
1053 static inline bool operator !(const Range& r)
1054 { return r.start == r.end; }
1056 static inline Range operator & (const Range& r1, const Range& r2)
1058 Range r(std::max(r1.start, r2.start), std::min(r2.start, r2.end));
1059 r.end = std::max(r.end, r.start);
1063 static inline Range& operator &= (Range& r1, const Range& r2)
1069 static inline Range operator + (const Range& r1, int delta)
1071 return Range(r1.start + delta, r1.end + delta);
1074 static inline Range operator + (int delta, const Range& r1)
1076 return Range(r1.start + delta, r1.end + delta);
1079 static inline Range operator - (const Range& r1, int delta)
1081 return r1 + (-delta);
1084 inline Range::operator CvSlice() const
1085 { return *this != Range::all() ? cvSlice(start, end) : CV_WHOLE_SEQ; }
1089 //////////////////////////////// Vector ////////////////////////////////
1091 // template vector class. It is similar to STL's vector,
1092 // with a few important differences:
1093 // 1) it can be created on top of user-allocated data w/o copying it
1094 // 2) vector b = a means copying the header,
1095 // not the underlying data (use clone() to make a deep copy)
1096 template <typename _Tp> class CV_EXPORTS Vector
1099 typedef _Tp value_type;
1100 typedef _Tp* iterator;
1101 typedef const _Tp* const_iterator;
1102 typedef _Tp& reference;
1103 typedef const _Tp& const_reference;
1105 struct CV_EXPORTS Hdr
1107 Hdr() : data(0), datastart(0), refcount(0), size(0), capacity(0) {};
1116 Vector(size_t _size) { resize(_size); }
1117 Vector(size_t _size, const _Tp& val)
1120 for(size_t i = 0; i < _size; i++)
1123 Vector(_Tp* _data, size_t _size, bool _copyData=false)
1124 { set(_data, _size, _copyData); }
1126 template<int n> Vector(const Vec<_Tp, n>& vec)
1127 { set((_Tp*)&vec.val[0], n, true); }
1129 Vector(const std::vector<_Tp>& vec, bool _copyData=false)
1130 { set((_Tp*)&vec[0], vec.size(), _copyData); }
1132 Vector(const Vector& d) { *this = d; }
1134 Vector(const Vector& d, const Range& r)
1136 if( r == Range::all() )
1137 r = Range(0, d.size());
1138 if( r.size() > 0 && r.start >= 0 && r.end <= d.size() )
1140 if( d.hdr.refcount )
1141 CV_XADD(d.hdr.refcount, 1);
1142 hdr.refcount = d.hdr.refcount;
1143 hdr.datastart = d.hdr.datastart;
1144 hdr.data = d.hdr.data + r.start;
1145 hdr.capacity = hdr.size = r.size();
1149 Vector<_Tp>& operator = (const Vector& d)
1153 if( d.hdr.refcount )
1154 CV_XADD(d.hdr.refcount, 1);
1161 ~Vector() { release(); }
1163 Vector<_Tp> clone() const
1164 { return hdr.data ? Vector<_Tp>(hdr.data, hdr.size, true) : Vector<_Tp>(); }
1166 void copyTo(Vector<_Tp>& vec) const
1168 size_t i, sz = size();
1170 const _Tp* src = hdr.data;
1171 _Tp* dst = vec.hdr.data;
1172 for( i = 0; i < sz; i++ )
1176 void copyTo(std::vector<_Tp>& vec) const
1178 size_t i, sz = size();
1180 const _Tp* src = hdr.data;
1181 _Tp* dst = sz ? &vec[0] : 0;
1182 for( i = 0; i < sz; i++ )
1186 operator CvMat() const
1187 { return cvMat((int)size(), 1, type(), (void*)hdr.data); }
1189 _Tp& operator [] (size_t i) { CV_DbgAssert( i < size() ); return hdr.data[i]; }
1190 const _Tp& operator [] (size_t i) const { CV_DbgAssert( i < size() ); return hdr.data[i]; }
1191 Vector operator() (const Range& r) const { return Vector(*this, r); }
1192 _Tp& back() { CV_DbgAssert(!empty()); return hdr.data[hdr.size-1]; }
1193 const _Tp& back() const { CV_DbgAssert(!empty()); return hdr.data[hdr.size-1]; }
1194 _Tp& front() { CV_DbgAssert(!empty()); return hdr.data[0]; }
1195 const _Tp& front() const { CV_DbgAssert(!empty()); return hdr.data[0]; }
1197 _Tp* begin() { return hdr.data; }
1198 _Tp* end() { return hdr.data + hdr.size; }
1199 const _Tp* begin() const { return hdr.data; }
1200 const _Tp* end() const { return hdr.data + hdr.size; }
1202 void addref() { if( hdr.refcount ) CV_XADD(hdr.refcount, 1); }
1205 if( hdr.refcount && CV_XADD(hdr.refcount, -1) == 1 )
1206 deallocate<_Tp>(hdr.datastart, hdr.capacity);
1210 void set(_Tp* _data, size_t _size, bool _copyData=false)
1215 hdr.data = hdr.datastart = _data;
1216 hdr.size = hdr.capacity = _size;
1222 for( size_t i = 0; i < _size; i++ )
1223 hdr.data[i] = _data[i];
1228 void reserve(size_t newCapacity)
1232 size_t i, oldSize = hdr.size;
1233 if( (!hdr.refcount || *hdr.refcount == 1) && hdr.capacity >= newCapacity )
1235 newCapacity = std::max(newCapacity, oldSize);
1236 size_t datasize = alignSize(newCapacity*sizeof(_Tp), (size_t)sizeof(*newRefcount));
1237 newData = (_Tp*)fastMalloc(datasize + sizeof(*newRefcount));
1238 for( i = 0; i < oldSize; i++ )
1239 ::new(newData + i) _Tp(hdr.data[i]);
1241 for( ; i < newCapacity; i++ )
1242 ::new(newData + i) _Tp(dummy);
1243 newRefcount = (int*)((uchar*)newData + datasize);
1246 hdr.data = hdr.datastart = newData;
1247 hdr.capacity = newCapacity;
1249 hdr.refcount = newRefcount;
1252 void resize(size_t newSize)
1255 newSize = std::max(newSize, (size_t)0);
1256 if( (!hdr.refcount || *hdr.refcount == 1) && hdr.size == newSize )
1258 if( newSize > hdr.capacity )
1259 reserve(std::max(newSize, std::max((size_t)4, hdr.capacity*2)));
1260 for( i = hdr.size; i < newSize; i++ )
1261 hdr.data[i] = _Tp();
1265 Vector<_Tp>& push_back(const _Tp& elem)
1267 if( hdr.size == hdr.capacity )
1268 reserve( std::max((size_t)4, hdr.capacity*2) );
1269 hdr.data[hdr.size++] = elem;
1273 Vector<_Tp>& pop_back()
1280 size_t size() const { return hdr.size; }
1281 size_t capacity() const { return hdr.capacity; }
1282 bool empty() const { return hdr.size == 0; }
1283 void clear() { resize(0); }
1284 int type() const { return DataType<_Tp>::type; }
1291 template<typename _Tp> inline typename DataType<_Tp>::work_type
1292 dot(const Vector<_Tp>& v1, const Vector<_Tp>& v2)
1294 typedef typename DataType<_Tp>::work_type _Tw;
1295 size_t i, n = v1.size();
1296 assert(v1.size() == v2.size());
1299 const _Tp *ptr1 = &v1[0], *ptr2 = &v2[0];
1300 for( i = 0; i <= n - 4; i += 4 )
1301 s += (_Tw)ptr1[i]*ptr2[i] + (_Tw)ptr1[i+1]*ptr2[i+1] +
1302 (_Tw)ptr1[i+2]*ptr2[i+2] + (_Tw)ptr1[i+3]*ptr2[i+3];
1304 s += (_Tw)ptr1[i]*ptr2[i];
1308 // Multiply-with-Carry RNG
1309 inline RNG::RNG() { state = 0xffffffff; }
1310 inline RNG::RNG(uint64 _state) { state = _state ? _state : 0xffffffff; }
1311 inline unsigned RNG::next()
1313 state = (uint64)(unsigned)state*A + (unsigned)(state >> 32);
1314 return (unsigned)state;
1317 inline RNG::operator uchar() { return (uchar)next(); }
1318 inline RNG::operator schar() { return (schar)next(); }
1319 inline RNG::operator ushort() { return (ushort)next(); }
1320 inline RNG::operator short() { return (short)next(); }
1321 inline RNG::operator unsigned() { return next(); }
1322 inline RNG::operator int() { return (int)next(); }
1324 inline RNG::operator float() { return next()*2.3283064365386962890625e-10f; }
1325 inline RNG::operator double()
1327 unsigned t = next();
1328 return (((uint64)t << 32) | next())*5.4210108624275221700372640043497e-20;
1330 inline int RNG::uniform(int a, int b) { return a == b ? a : next()%(b - a) + a; }
1331 inline float RNG::uniform(float a, float b) { return ((float)*this)*(b - a) + a; }
1332 inline double RNG::uniform(double a, double b) { return ((float)*this)*(b - a) + a; }
1335 inline TermCriteria::TermCriteria() : type(0), maxCount(0), epsilon(0) {}
1336 inline TermCriteria::TermCriteria(int _type, int _maxCount, double _epsilon)
1337 : type(_type), maxCount(_maxCount), epsilon(_epsilon) {}
1338 inline TermCriteria::TermCriteria(const CvTermCriteria& criteria)
1339 : type(criteria.type), maxCount(criteria.max_iter), epsilon(criteria.epsilon) {}
1340 inline TermCriteria::operator CvTermCriteria() const
1341 { return cvTermCriteria(type, maxCount, epsilon); }
1343 inline uchar* LineIterator::operator *() { return ptr; }
1344 inline LineIterator& LineIterator::operator ++()
1346 int mask = err < 0 ? -1 : 0;
1347 err += minusDelta + (plusDelta & mask);
1348 ptr += minusStep + (plusStep & mask);
1351 inline LineIterator LineIterator::operator ++(int)
1353 LineIterator it = *this;
1359 template<typename _Tp> inline VectorCommaInitializer_<_Tp>::
1360 VectorCommaInitializer_(vector<_Tp>* _vec) : vec(_vec), idx(0) {}
1362 template<typename _Tp> template<typename T2> inline VectorCommaInitializer_<_Tp>&
1363 VectorCommaInitializer_<_Tp>::operator , (T2 val)
1365 if( (size_t)idx < vec->size() )
1366 (*vec)[idx] = _Tp(val);
1368 vec->push_back(_Tp(val));
1373 template<typename _Tp> inline VectorCommaInitializer_<_Tp>::operator vector<_Tp>() const
1376 template<typename _Tp> inline vector<_Tp> VectorCommaInitializer_<_Tp>::operator *() const
1379 template<typename _Tp, typename T2> static inline VectorCommaInitializer_<_Tp>
1380 operator << (const vector<_Tp>& vec, T2 val)
1382 VectorCommaInitializer_<_Tp> commaInitializer((vector<_Tp>*)&vec);
1383 return (commaInitializer, val);
1387 /////////////////////////////// AutoBuffer ////////////////////////////////////////
1389 template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::AutoBuffer()
1390 : ptr(buf), size(fixed_size) {}
1392 template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::AutoBuffer(size_t _size)
1393 : ptr(buf), size(fixed_size) { allocate(_size); }
1395 template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::~AutoBuffer()
1398 template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size>::allocate(size_t _size)
1403 if(_size > fixed_size)
1405 ptr = cv::allocate<_Tp>(_size);
1410 template<typename _Tp, size_t fixed_size> inline void AutoBuffer<_Tp, fixed_size>::deallocate()
1414 cv::deallocate<_Tp>(ptr, size);
1420 template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::operator _Tp* ()
1423 template<typename _Tp, size_t fixed_size> inline AutoBuffer<_Tp, fixed_size>::operator const _Tp* () const
1427 /////////////////////////////////// Ptr ////////////////////////////////////////
1429 template<typename _Tp> inline Ptr<_Tp>::Ptr() : obj(0), refcount(0) {}
1430 template<typename _Tp> inline Ptr<_Tp>::Ptr(_Tp* _obj) : obj(_obj)
1434 refcount = (int*)fastMalloc(sizeof(*refcount));
1441 template<typename _Tp> inline void Ptr<_Tp>::addref()
1442 { if( refcount ) CV_XADD(refcount, 1); }
1444 template<typename _Tp> inline void Ptr<_Tp>::release()
1446 if( refcount && CV_XADD(refcount, -1) == 1 )
1455 template<typename _Tp> inline void Ptr<_Tp>::delete_obj()
1457 if( obj ) delete obj;
1460 template<typename _Tp> inline Ptr<_Tp>::~Ptr() { release(); }
1462 template<typename _Tp> inline Ptr<_Tp>::Ptr(const Ptr<_Tp>& ptr)
1465 refcount = ptr.refcount;
1469 template<typename _Tp> inline Ptr<_Tp>& Ptr<_Tp>::operator = (const Ptr<_Tp>& ptr)
1471 int* _refcount = ptr.refcount;
1473 CV_XADD(_refcount, 1);
1476 refcount = _refcount;
1480 template<typename _Tp> inline _Tp* Ptr<_Tp>::operator -> () { return obj; }
1481 template<typename _Tp> inline const _Tp* Ptr<_Tp>::operator -> () const { return obj; }
1483 template<typename _Tp> inline Ptr<_Tp>::operator _Tp* () { return obj; }
1484 template<typename _Tp> inline Ptr<_Tp>::operator const _Tp*() const { return obj; }
1486 template<typename _Tp> inline bool Ptr<_Tp>::empty() const { return obj == 0; }
1488 //// specializied implementations of Ptr::delete_obj() for classic OpenCV types
1490 template<> inline void Ptr<CvMat>::delete_obj()
1491 { cvReleaseMat(&obj); }
1493 template<> inline void Ptr<IplImage>::delete_obj()
1494 { cvReleaseImage(&obj); }
1496 template<> inline void Ptr<CvMatND>::delete_obj()
1497 { cvReleaseMatND(&obj); }
1499 template<> inline void Ptr<CvSparseMat>::delete_obj()
1500 { cvReleaseSparseMat(&obj); }
1502 template<> inline void Ptr<CvMemStorage>::delete_obj()
1503 { cvReleaseMemStorage(&obj); }
1505 template<> inline void Ptr<CvFileStorage>::delete_obj()
1506 { cvReleaseFileStorage(&obj); }
1508 //////////////////////////////////////// XML & YAML I/O ////////////////////////////////////
1510 static inline void write( FileStorage& fs, const string& name, int value )
1511 { cvWriteInt( *fs, name.size() ? name.c_str() : 0, value ); }
1513 static inline void write( FileStorage& fs, const string& name, float value )
1514 { cvWriteReal( *fs, name.size() ? name.c_str() : 0, value ); }
1516 static inline void write( FileStorage& fs, const string& name, double value )
1517 { cvWriteReal( *fs, name.size() ? name.c_str() : 0, value ); }
1519 static inline void write( FileStorage& fs, const string& name, const string& value )
1520 { cvWriteString( *fs, name.size() ? name.c_str() : 0, value.c_str() ); }
1522 template<typename _Tp> static inline void write(FileStorage& fs, const _Tp& value)
1523 { write(fs, string(), value); }
1525 template<> inline void write(FileStorage& fs, const int& value )
1526 { cvWriteInt( *fs, 0, value ); }
1528 template<> inline void write(FileStorage& fs, const float& value )
1529 { cvWriteReal( *fs, 0, value ); }
1531 template<> inline void write(FileStorage& fs, const double& value )
1532 { cvWriteReal( *fs, 0, value ); }
1534 template<> inline void write(FileStorage& fs, const string& value )
1535 { cvWriteString( *fs, 0, value.c_str() ); }
1537 template<typename _Tp> inline void write(FileStorage& fs, const Point_<_Tp>& pt )
1543 template<typename _Tp> inline void write(FileStorage& fs, const Point3_<_Tp>& pt )
1550 template<typename _Tp> inline void write(FileStorage& fs, const Size_<_Tp>& sz )
1552 write(fs, sz.width);
1553 write(fs, sz.height);
1556 template<typename _Tp> inline void write(FileStorage& fs, const Complex<_Tp>& c )
1562 template<typename _Tp> inline void write(FileStorage& fs, const Rect_<_Tp>& r )
1567 write(fs, r.height);
1570 template<typename _Tp, int cn> inline void write(FileStorage& fs, const Vec<_Tp, cn>& v )
1572 for(int i = 0; i < cn; i++)
1573 write(fs, v.val[i]);
1576 template<typename _Tp> inline void write(FileStorage& fs, const Scalar_<_Tp>& s )
1578 write(fs, s.val[0]);
1579 write(fs, s.val[1]);
1580 write(fs, s.val[2]);
1581 write(fs, s.val[3]);
1584 inline void write(FileStorage& fs, const Range& r )
1590 class CV_EXPORTS WriteStructContext
1593 WriteStructContext(FileStorage& _fs, const string& name,
1594 int flags, const string& typeName=string()) : fs(&_fs)
1596 cvStartWriteStruct(**fs, !name.empty() ? name.c_str() : 0, flags,
1597 !typeName.empty() ? typeName.c_str() : 0);
1599 ~WriteStructContext() { cvEndWriteStruct(**fs); }
1603 template<typename _Tp> inline void write(FileStorage& fs, const string& name, const Point_<_Tp>& pt )
1605 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1610 template<typename _Tp> inline void write(FileStorage& fs, const string& name, const Point3_<_Tp>& pt )
1612 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1618 template<typename _Tp> inline void write(FileStorage& fs, const string& name, const Size_<_Tp>& sz )
1620 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1621 write(fs, sz.width);
1622 write(fs, sz.height);
1625 template<typename _Tp> inline void write(FileStorage& fs, const string& name, const Complex<_Tp>& c )
1627 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1632 template<typename _Tp> inline void write(FileStorage& fs, const string& name, const Rect_<_Tp>& r )
1634 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1638 write(fs, r.height);
1641 template<typename _Tp, int cn> inline void write(FileStorage& fs, const string& name, const Vec<_Tp, cn>& v )
1643 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1644 for(int i = 0; i < cn; i++)
1645 write(fs, v.val[i]);
1648 template<typename _Tp> inline void write(FileStorage& fs, const string& name, const Scalar_<_Tp>& s )
1650 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1651 write(fs, s.val[0]);
1652 write(fs, s.val[1]);
1653 write(fs, s.val[2]);
1654 write(fs, s.val[3]);
1657 inline void write(FileStorage& fs, const string& name, const Range& r )
1659 WriteStructContext ws(fs, name, CV_NODE_SEQ+CV_NODE_FLOW);
1664 template<typename _Tp, int numflag> class CV_EXPORTS VecWriterProxy
1667 VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
1668 void operator()(const vector<_Tp>& vec) const
1670 size_t i, count = vec.size();
1671 for( i = 0; i < count; i++ )
1672 write( *fs, vec[i] );
1677 template<typename _Tp> class CV_EXPORTS VecWriterProxy<_Tp,1>
1680 VecWriterProxy( FileStorage* _fs ) : fs(_fs) {}
1681 void operator()(const vector<_Tp>& vec) const
1683 int _fmt = DataType<_Tp>::fmt;
1684 char fmt[] = { (char)((_fmt>>8)+'1'), (char)_fmt, '\0' };
1685 fs->writeRaw( string(fmt), (uchar*)&vec[0], vec.size()*sizeof(_Tp) );
1691 template<typename _Tp> static inline void write( FileStorage& fs, const vector<_Tp>& vec )
1693 VecWriterProxy<_Tp, DataType<_Tp>::fmt != 0> w(&fs);
1697 template<typename _Tp> static inline FileStorage&
1698 operator << ( FileStorage& fs, const vector<_Tp>& vec )
1700 VecWriterProxy<_Tp, DataType<_Tp>::fmt != 0> w(&fs);
1705 CV_EXPORTS void write( FileStorage& fs, const string& name, const Mat& value );
1706 CV_EXPORTS void write( FileStorage& fs, const string& name, const MatND& value );
1707 CV_EXPORTS void write( FileStorage& fs, const string& name, const SparseMat& value );
1709 template<typename _Tp> static inline FileStorage& operator << (FileStorage& fs, const _Tp& value)
1711 if( !fs.isOpened() )
1713 if( fs.state == FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP )
1714 CV_Error( CV_StsError, "No element name has been given" );
1715 write( fs, fs.elname, value );
1716 if( fs.state & FileStorage::INSIDE_MAP )
1717 fs.state = FileStorage::NAME_EXPECTED + FileStorage::INSIDE_MAP;
1721 CV_EXPORTS FileStorage& operator << (FileStorage& fs, const string& str);
1723 static inline FileStorage& operator << (FileStorage& fs, const char* str)
1724 { return (fs << string(str)); }
1726 inline FileNode FileStorage::operator[](const string& nodename) const
1728 return FileNode(fs, cvGetFileNodeByName(fs, 0, nodename.c_str()));
1730 inline FileNode FileStorage::operator[](const char* nodename) const
1732 return FileNode(fs, cvGetFileNodeByName(fs, 0, nodename));
1735 inline FileNode::FileNode() : fs(0), node(0) {}
1736 inline FileNode::FileNode(const CvFileStorage* _fs, const CvFileNode* _node)
1737 : fs(_fs), node(_node) {}
1739 inline FileNode::FileNode(const FileNode& _node) : fs(_node.fs), node(_node.node) {}
1740 inline FileNode FileNode::operator[](const string& nodename) const
1742 return FileNode(fs, cvGetFileNodeByName(fs, node, nodename.c_str()));
1744 inline FileNode FileNode::operator[](const char* nodename) const
1746 return FileNode(fs, cvGetFileNodeByName(fs, node, nodename));
1749 inline FileNode FileNode::operator[](int i) const
1751 return isSeq() ? FileNode(fs, (CvFileNode*)cvGetSeqElem(node->data.seq, i)) :
1752 i == 0 ? *this : FileNode();
1755 inline int FileNode::type() const { return !node ? NONE : (node->tag & TYPE_MASK); }
1756 inline bool FileNode::empty() const { return node == 0; }
1757 inline bool FileNode::isNone() const { return type() == NONE; }
1758 inline bool FileNode::isSeq() const { return type() == SEQ; }
1759 inline bool FileNode::isMap() const { return type() == MAP; }
1760 inline bool FileNode::isInt() const { return type() == INT; }
1761 inline bool FileNode::isReal() const { return type() == REAL; }
1762 inline bool FileNode::isString() const { return type() == STR; }
1763 inline bool FileNode::isNamed() const { return !node ? false : (node->tag & NAMED) != 0; }
1764 inline string FileNode::name() const
1767 return !node || (str = cvGetFileNodeName(node)) == 0 ? string() : string(str);
1769 inline size_t FileNode::size() const
1772 return t == MAP ? ((CvSet*)node->data.map)->active_count :
1773 t == SEQ ? node->data.seq->total : node != 0;
1776 inline CvFileNode* FileNode::operator *() { return (CvFileNode*)node; }
1777 inline const CvFileNode* FileNode::operator* () const { return node; }
1779 static inline void read(const FileNode& node, bool& value, bool default_value)
1780 { value = cvReadInt(node.node, default_value) != 0; }
1782 static inline void read(const FileNode& node, uchar& value, uchar default_value)
1783 { value = saturate_cast<uchar>(cvReadInt(node.node, default_value)); }
1785 static inline void read(const FileNode& node, schar& value, schar default_value)
1786 { value = saturate_cast<schar>(cvReadInt(node.node, default_value)); }
1788 static inline void read(const FileNode& node, ushort& value, ushort default_value)
1789 { value = saturate_cast<ushort>(cvReadInt(node.node, default_value)); }
1791 static inline void read(const FileNode& node, short& value, short default_value)
1792 { value = saturate_cast<short>(cvReadInt(node.node, default_value)); }
1794 static inline void read(const FileNode& node, int& value, int default_value)
1795 { value = cvReadInt(node.node, default_value); }
1797 static inline void read(const FileNode& node, float& value, float default_value)
1798 { value = (float)cvReadReal(node.node, default_value); }
1800 static inline void read(const FileNode& node, double& value, double default_value)
1801 { value = cvReadReal(node.node, default_value); }
1803 static inline void read(const FileNode& node, string& value, const string& default_value)
1804 { value = string(cvReadString(node.node, default_value.c_str())); }
1806 CV_EXPORTS void read(const FileNode& node, Mat& mat, const Mat& default_mat=Mat() );
1807 CV_EXPORTS void read(const FileNode& node, MatND& mat, const MatND& default_mat=MatND() );
1808 CV_EXPORTS void read(const FileNode& node, SparseMat& mat, const SparseMat& default_mat=SparseMat() );
1810 inline FileNode::operator int() const
1812 return cvReadInt(node, 0);
1814 inline FileNode::operator float() const
1816 return (float)cvReadReal(node, 0);
1818 inline FileNode::operator double() const
1820 return cvReadReal(node, 0);
1822 inline FileNode::operator string() const
1824 return string(cvReadString(node, ""));
1827 inline void FileNode::readRaw( const string& fmt, uchar* vec, size_t len ) const
1829 begin().readRaw( fmt, vec, len );
1832 template<typename _Tp, int numflag> class CV_EXPORTS VecReaderProxy
1835 VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
1836 void operator()(vector<_Tp>& vec, size_t count) const
1838 count = std::min(count, it->remaining);
1840 for( size_t i = 0; i < count; i++, ++(*it) )
1841 read(**it, vec[i], _Tp());
1843 FileNodeIterator* it;
1846 template<typename _Tp> class CV_EXPORTS VecReaderProxy<_Tp,1>
1849 VecReaderProxy( FileNodeIterator* _it ) : it(_it) {}
1850 void operator()(vector<_Tp>& vec, size_t count) const
1852 size_t remaining = it->remaining, cn = DataType<_Tp>::channels;
1853 int _fmt = DataType<_Tp>::fmt;
1854 char fmt[] = { (char)((_fmt>>8)+'1'), (char)_fmt, '\0' };
1855 count = std::min(count, remaining/cn);
1857 it->readRaw( string(fmt), (uchar*)&vec[0], count*sizeof(_Tp) );
1859 FileNodeIterator* it;
1862 template<typename _Tp> static inline void
1863 read( FileNodeIterator& it, vector<_Tp>& vec, size_t maxCount=(size_t)INT_MAX )
1865 VecReaderProxy<_Tp, DataType<_Tp>::fmt != 0> r(&it);
1869 template<typename _Tp> static inline void
1870 read( FileNode& node, vector<_Tp>& vec, const vector<_Tp>& default_value=vector<_Tp>() )
1872 read( node.begin(), vec );
1875 inline FileNodeIterator FileNode::begin() const
1877 return FileNodeIterator(fs, node);
1880 inline FileNodeIterator FileNode::end() const
1882 return FileNodeIterator(fs, node, size());
1885 inline FileNode FileNodeIterator::operator *() const
1886 { return FileNode(fs, (const CvFileNode*)reader.ptr); }
1888 inline FileNode FileNodeIterator::operator ->() const
1889 { return FileNode(fs, (const CvFileNode*)reader.ptr); }
1891 template<typename _Tp> static inline FileNodeIterator& operator >> (FileNodeIterator& it, _Tp& value)
1892 { read( *it, value, _Tp()); return ++it; }
1894 template<typename _Tp> static inline
1895 FileNodeIterator& operator >> (FileNodeIterator& it, vector<_Tp>& vec)
1897 VecReaderProxy<_Tp, DataType<_Tp>::fmt != 0> r(&it);
1898 r(vec, (size_t)INT_MAX);
1902 template<typename _Tp> static inline void operator >> (const FileNode& n, _Tp& value)
1903 { FileNodeIterator it = n.begin(); it >> value; }
1905 static inline bool operator == (const FileNodeIterator& it1, const FileNodeIterator& it2)
1907 return it1.fs == it2.fs && it1.container == it2.container &&
1908 it1.reader.ptr == it2.reader.ptr && it1.remaining == it2.remaining;
1911 static inline bool operator != (const FileNodeIterator& it1, const FileNodeIterator& it2)
1913 return !(it1 == it2);
1916 static inline ptrdiff_t operator - (const FileNodeIterator& it1, const FileNodeIterator& it2)
1918 return it2.remaining - it1.remaining;
1921 static inline bool operator < (const FileNodeIterator& it1, const FileNodeIterator& it2)
1923 return it1.remaining > it2.remaining;
1926 inline FileNode FileStorage::getFirstTopLevelNode() const
1928 FileNode r = root();
1929 FileNodeIterator it = r.begin();
1930 return it != r.end() ? *it : FileNode();
1933 //////////////////////////////////////// Various algorithms ////////////////////////////////////
1935 template<typename _Tp> static inline _Tp gcd(_Tp a, _Tp b)
1948 /****************************************************************************************\
1950 Generic implementation of QuickSort algorithm
1951 Use it as: vector<_Tp> a; ... sort(a,<less_than_predictor>);
1953 The current implementation was derived from *BSD system qsort():
1955 * Copyright (c) 1992, 1993
1956 * The Regents of the University of California. All rights reserved.
1958 * Redistribution and use in source and binary forms, with or without
1959 * modification, are permitted provided that the following conditions
1961 * 1. Redistributions of source code must retain the above copyright
1962 * notice, this list of conditions and the following disclaimer.
1963 * 2. Redistributions in binary form must reproduce the above copyright
1964 * notice, this list of conditions and the following disclaimer in the
1965 * documentation and/or other materials provided with the distribution.
1966 * 3. All advertising materials mentioning features or use of this software
1967 * must display the following acknowledgement:
1968 * This product includes software developed by the University of
1969 * California, Berkeley and its contributors.
1970 * 4. Neither the name of the University nor the names of its contributors
1971 * may be used to endorse or promote products derived from this software
1972 * without specific prior written permission.
1974 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1975 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1976 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1977 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1978 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1979 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1980 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1981 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1982 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1983 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1986 \****************************************************************************************/
1988 template<typename _Tp, class _LT> void sort( vector<_Tp>& vec, _LT LT=_LT() )
1990 int isort_thresh = 7;
2000 size_t total = vec.size();
2007 stack[0].ub = arr + (total - 1);
2011 _Tp* left = stack[sp].lb;
2012 _Tp* right = stack[sp--].ub;
2016 int i, n = (int)(right - left) + 1, m;
2020 if( n <= isort_thresh )
2023 for( ptr = left + 1; ptr <= right; ptr++ )
2025 for( ptr2 = ptr; ptr2 > left && LT(ptr2[0],ptr2[-1]); ptr2--)
2026 std::swap( ptr2[0], ptr2[-1] );
2044 pivot = left + (n/2);
2049 a = left, b = left + d, c = left + 2*d;
2050 left = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2051 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2053 a = pivot - d, b = pivot, c = pivot + d;
2054 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2055 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2057 a = right - 2*d, b = right - d, c = right;
2058 right = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2059 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2062 a = left, b = pivot, c = right;
2063 pivot = LT(*a, *b) ? (LT(*b, *c) ? b : (LT(*a, *c) ? c : a))
2064 : (LT(*c, *b) ? b : (LT(*a, *c) ? a : c));
2065 if( pivot != left0 )
2067 std::swap( *pivot, *left0 );
2070 left = left1 = left0 + 1;
2071 right = right1 = right0;
2075 while( left <= right && !LT(*pivot, *left) )
2077 if( !LT(*left, *pivot) )
2080 std::swap( *left1, *left );
2087 while( left <= right && !LT(*right, *pivot) )
2089 if( !LT(*pivot, *right) )
2091 if( right < right1 )
2092 std::swap( *right1, *right );
2101 std::swap( *left, *right );
2109 left = left0, right = right0;
2113 n = std::min( (int)(left1 - left0), (int)(left - left1) );
2114 for( i = 0; i < n; i++ )
2115 std::swap( left0[i], left[i-n] );
2117 n = std::min( (int)(right0 - right1), (int)(right1 - right) );
2118 for( i = 0; i < n; i++ )
2119 std::swap( left[i], right0[i-n+1] );
2120 n = (int)(left - left1);
2121 m = (int)(right1 - right);
2128 stack[++sp].lb = left0;
2129 stack[sp].ub = left0 + n - 1;
2130 left = right0 - m + 1, right = right0;
2134 stack[++sp].lb = right0 - m + 1;
2135 stack[sp].ub = right0;
2136 left = left0, right = left0 + n - 1;
2140 left = left0, right = left0 + n - 1;
2143 left = right0 - m + 1, right = right0;
2151 template<typename _Tp> class CV_EXPORTS LessThan
2154 bool operator()(const _Tp& a, const _Tp& b) const { return a < b; }
2157 template<typename _Tp> class CV_EXPORTS GreaterEq
2160 bool operator()(const _Tp& a, const _Tp& b) const { return a >= b; }
2163 template<typename _Tp> class CV_EXPORTS LessThanIdx
2166 LessThanIdx( const _Tp* _arr ) : arr(_arr) {}
2167 bool operator()(int a, int b) const { return arr[a] < arr[b]; }
2171 template<typename _Tp> class CV_EXPORTS GreaterEqIdx
2174 GreaterEqIdx( const _Tp* _arr ) : arr(_arr) {}
2175 bool operator()(int a, int b) const { return arr[a] >= arr[b]; }
2180 // This function splits the input sequence or set into one or more equivalence classes and
2181 // returns the vector of labels - 0-based class indexes for each element.
2182 // predicate(a,b) returns true if the two sequence elements certainly belong to the same class.
2184 // The algorithm is described in "Introduction to Algorithms"
2185 // by Cormen, Leiserson and Rivest, the chapter "Data structures for disjoint sets"
2186 template<typename _Tp, class _EqPredicate> int
2187 partition( const vector<_Tp>& _vec, vector<int>& labels,
2188 _EqPredicate predicate=_EqPredicate())
2190 int i, j, N = (int)_vec.size();
2191 const _Tp* vec = &_vec[0];
2196 vector<int> _nodes(N*2);
2197 int (*nodes)[2] = (int(*)[2])&_nodes[0];
2199 // The first O(N) pass: create N single-vertex trees
2200 for(i = 0; i < N; i++)
2202 nodes[i][PARENT]=-1;
2206 // The main O(N^2) pass: merge connected components
2207 for( i = 0; i < N; i++ )
2212 while( nodes[root][PARENT] >= 0 )
2213 root = nodes[root][PARENT];
2215 for( j = 0; j < N; j++ )
2217 if( i == j || !predicate(vec[i], vec[j]))
2221 while( nodes[root2][PARENT] >= 0 )
2222 root2 = nodes[root2][PARENT];
2227 int rank = nodes[root][RANK], rank2 = nodes[root2][RANK];
2229 nodes[root2][PARENT] = root;
2232 nodes[root][PARENT] = root2;
2233 nodes[root2][RANK] += rank == rank2;
2236 assert( nodes[root][PARENT] < 0 );
2240 // compress the path from node2 to root
2241 while( (parent = nodes[k][PARENT]) >= 0 )
2243 nodes[k][PARENT] = root;
2247 // compress the path from node to root
2249 while( (parent = nodes[k][PARENT]) >= 0 )
2251 nodes[k][PARENT] = root;
2258 // Final O(N) pass: enumerate classes
2262 for( i = 0; i < N; i++ )
2265 while( nodes[root][PARENT] >= 0 )
2266 root = nodes[root][PARENT];
2267 // re-use the rank as the class label
2268 if( nodes[root][RANK] >= 0 )
2269 nodes[root][RANK] = ~nclasses++;
2270 labels[i] = ~nodes[root][RANK];
2276 //////////////////////////////////////////////////////////////////////////////
2278 template<typename _Tp> inline Seq<_Tp>::Seq() : seq(0) {}
2279 template<typename _Tp> inline Seq<_Tp>::Seq( const CvSeq* _seq ) : seq((CvSeq*)_seq)
2281 CV_Assert(!_seq || _seq->elem_size == sizeof(_Tp));
2284 template<typename _Tp> inline Seq<_Tp>::Seq( MemStorage& storage,
2287 CV_Assert(headerSize >= (int)sizeof(CvSeq));
2288 seq = cvCreateSeq(DataType<_Tp>::type, headerSize, sizeof(_Tp), storage);
2291 template<typename _Tp> inline _Tp& Seq<_Tp>::operator [](int idx)
2292 { return *(_Tp*)cvGetSeqElem(seq, idx); }
2294 template<typename _Tp> inline const _Tp& Seq<_Tp>::operator [](int idx) const
2295 { return *(_Tp*)cvGetSeqElem(seq, idx); }
2297 template<typename _Tp> inline SeqIterator<_Tp> Seq<_Tp>::begin() const
2298 { return SeqIterator<_Tp>(*this); }
2300 template<typename _Tp> inline SeqIterator<_Tp> Seq<_Tp>::end() const
2301 { return SeqIterator<_Tp>(*this, true); }
2303 template<typename _Tp> inline size_t Seq<_Tp>::size() const
2304 { return seq ? seq->total : 0; }
2306 template<typename _Tp> inline int Seq<_Tp>::type() const
2307 { return seq ? CV_MAT_TYPE(seq->flags) : 0; }
2309 template<typename _Tp> inline int Seq<_Tp>::depth() const
2310 { return seq ? CV_MAT_DEPTH(seq->flags) : 0; }
2312 template<typename _Tp> inline int Seq<_Tp>::channels() const
2313 { return seq ? CV_MAT_CN(seq->flags) : 0; }
2315 template<typename _Tp> inline size_t Seq<_Tp>::elemSize() const
2316 { return seq ? seq->elem_size : 0; }
2318 template<typename _Tp> inline size_t Seq<_Tp>::index(const _Tp& elem) const
2319 { return cvSeqElemIdx(seq, &elem); }
2321 template<typename _Tp> inline void Seq<_Tp>::push_back(const _Tp& elem)
2322 { cvSeqPush(seq, &elem); }
2324 template<typename _Tp> inline void Seq<_Tp>::push_front(const _Tp& elem)
2325 { cvSeqPushFront(seq, &elem); }
2327 template<typename _Tp> inline void Seq<_Tp>::push_back(const _Tp* elem, size_t count)
2328 { cvSeqPushMulti(seq, elem, (int)count, 0); }
2330 template<typename _Tp> inline void Seq<_Tp>::push_front(const _Tp* elem, size_t count)
2331 { cvSeqPushMulti(seq, elem, (int)count, 1); }
2333 template<typename _Tp> inline _Tp& Seq<_Tp>::back()
2334 { return *(_Tp*)cvGetSeqElem(seq, -1); }
2336 template<typename _Tp> inline const _Tp& Seq<_Tp>::back() const
2337 { return *(const _Tp*)cvGetSeqElem(seq, -1); }
2339 template<typename _Tp> inline _Tp& Seq<_Tp>::front()
2340 { return *(_Tp*)cvGetSeqElem(seq, 0); }
2342 template<typename _Tp> inline const _Tp& Seq<_Tp>::front() const
2343 { return *(const _Tp*)cvGetSeqElem(seq, 0); }
2345 template<typename _Tp> inline bool Seq<_Tp>::empty() const
2346 { return !seq || seq->total == 0; }
2348 template<typename _Tp> inline void Seq<_Tp>::clear()
2349 { if(seq) cvClearSeq(seq); }
2351 template<typename _Tp> inline void Seq<_Tp>::pop_back()
2354 template<typename _Tp> inline void Seq<_Tp>::pop_front()
2355 { cvSeqPopFront(seq); }
2357 template<typename _Tp> inline void Seq<_Tp>::pop_back(_Tp* elem, size_t count)
2358 { cvSeqPopMulti(seq, elem, (int)count, 0); }
2360 template<typename _Tp> inline void Seq<_Tp>::pop_front(_Tp* elem, size_t count)
2361 { cvSeqPopMulti(seq, elem, (int)count, 1); }
2363 template<typename _Tp> inline void Seq<_Tp>::insert(int idx, const _Tp& elem)
2364 { cvSeqInsert(seq, idx, &elem); }
2366 template<typename _Tp> inline void Seq<_Tp>::insert(int idx, const _Tp* elems, size_t count)
2368 CvMat m = cvMat(1, count, DataType<_Tp>::type, elems);
2369 cvSeqInsertSlice(seq, idx, &m);
2372 template<typename _Tp> inline void Seq<_Tp>::remove(int idx)
2373 { cvSeqRemove(seq, idx); }
2375 template<typename _Tp> inline void Seq<_Tp>::remove(const Range& r)
2376 { cvSeqRemoveSlice(seq, r); }
2378 template<typename _Tp> inline void Seq<_Tp>::copyTo(vector<_Tp>& vec, const Range& range) const
2380 size_t len = !seq ? 0 : range == Range::all() ? seq->total : range.end - range.start;
2383 cvCvtSeqToArray(seq, &vec[0], range);
2386 template<typename _Tp> inline Seq<_Tp>::operator vector<_Tp>() const
2393 template<typename _Tp> inline SeqIterator<_Tp>::SeqIterator()
2394 { memset(this, 0, sizeof(*this)); }
2396 template<typename _Tp> inline SeqIterator<_Tp>::SeqIterator(const Seq<_Tp>& seq, bool seekEnd)
2398 cvStartReadSeq(seq.seq, this);
2400 index = seq.seq->total;
2403 template<typename _Tp> inline void SeqIterator<_Tp>::seek(size_t pos)
2405 cvSetSeqReaderPos(this, (int)pos, false);
2409 template<typename _Tp> inline size_t SeqIterator<_Tp>::tell() const
2412 template<typename _Tp> inline _Tp& SeqIterator<_Tp>::operator *()
2413 { return *(_Tp*)ptr; }
2415 template<typename _Tp> inline const _Tp& SeqIterator<_Tp>::operator *() const
2416 { return *(const _Tp*)ptr; }
2418 template<typename _Tp> inline SeqIterator<_Tp>& SeqIterator<_Tp>::operator ++()
2420 CV_NEXT_SEQ_ELEM(sizeof(_Tp), *this);
2421 if( ++index >= seq->total*2 )
2426 template<typename _Tp> inline SeqIterator<_Tp> SeqIterator<_Tp>::operator ++(int) const
2428 SeqIterator<_Tp> it = *this;
2433 template<typename _Tp> inline SeqIterator<_Tp>& SeqIterator<_Tp>::operator --()
2435 CV_PREV_SEQ_ELEM(sizeof(_Tp), *this);
2437 index = seq->total*2-1;
2441 template<typename _Tp> inline SeqIterator<_Tp> SeqIterator<_Tp>::operator --(int) const
2443 SeqIterator<_Tp> it = *this;
2448 template<typename _Tp> inline SeqIterator<_Tp>& SeqIterator<_Tp>::operator +=(int delta)
2450 cvSetSeqReaderPos(this, delta, 1);
2452 int n = seq->total*2;
2460 template<typename _Tp> inline SeqIterator<_Tp>& SeqIterator<_Tp>::operator -=(int delta)
2462 return (*this += -delta);
2465 template<typename _Tp> inline ptrdiff_t operator - (const SeqIterator<_Tp>& a,
2466 const SeqIterator<_Tp>& b)
2468 ptrdiff_t delta = a.index - b.index, n = a.seq->total;
2469 if( std::abs(delta) > n )
2470 delta += delta < 0 ? n : -n;
2474 template<typename _Tp> inline bool operator == (const SeqIterator<_Tp>& a,
2475 const SeqIterator<_Tp>& b)
2477 return a.seq == b.seq && a.index == b.index;
2480 template<typename _Tp> inline bool operator != (const SeqIterator<_Tp>& a,
2481 const SeqIterator<_Tp>& b)
2488 #endif // __cplusplus
2489 #endif // _OPENCV_CORE_OPERATIONS_H_