1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                          License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43 
44 #ifndef __OPENCV_CORE_TYPES_HPP__
45 #define __OPENCV_CORE_TYPES_HPP__
46 
47 #ifndef __cplusplus
48 #  error types.hpp header must be compiled as C++
49 #endif
50 
51 #include <climits>
52 #include <cfloat>
53 #include <vector>
54 
55 #include "opencv2/core/cvdef.h"
56 #include "opencv2/core/cvstd.hpp"
57 #include "opencv2/core/matx.hpp"
58 
59 namespace cv
60 {
61 
62 //! @addtogroup core_basic
63 //! @{
64 
65 //////////////////////////////// Complex //////////////////////////////
66 
67 /** @brief  A complex number class.
68 
69   The template class is similar and compatible with std::complex, however it provides slightly
70   more convenient access to the real and imaginary parts using through the simple field access, as opposite
71   to std::complex::real() and std::complex::imag().
72 */
73 template<typename _Tp> class Complex
74 {
75 public:
76 
77     //! constructors
78     Complex();
79     Complex( _Tp _re, _Tp _im = 0 );
80 
81     //! conversion to another data type
82     template<typename T2> operator Complex<T2>() const;
83     //! conjugation
84     Complex conj() const;
85 
86     _Tp re, im; //< the real and the imaginary parts
87 };
88 
89 typedef Complex<float> Complexf;
90 typedef Complex<double> Complexd;
91 
92 template<typename _Tp> class DataType< Complex<_Tp> >
93 {
94 public:
95     typedef Complex<_Tp> value_type;
96     typedef value_type   work_type;
97     typedef _Tp          channel_type;
98 
99     enum { generic_type = 0,
100            depth        = DataType<channel_type>::depth,
101            channels     = 2,
102            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
103            type         = CV_MAKETYPE(depth, channels) };
104 
105     typedef Vec<channel_type, channels> vec_type;
106 };
107 
108 
109 
110 //////////////////////////////// Point_ ////////////////////////////////
111 
112 /** @brief Template class for 2D points specified by its coordinates `x` and `y`.
113 
114 An instance of the class is interchangeable with C structures, CvPoint and CvPoint2D32f . There is
115 also a cast operator to convert point coordinates to the specified type. The conversion from
116 floating-point coordinates to integer coordinates is done by rounding. Commonly, the conversion
117 uses this operation for each of the coordinates. Besides the class members listed in the
118 declaration above, the following operations on points are implemented:
119 @code
120     pt1 = pt2 + pt3;
121     pt1 = pt2 - pt3;
122     pt1 = pt2 * a;
123     pt1 = a * pt2;
124     pt1 = pt2 / a;
125     pt1 += pt2;
126     pt1 -= pt2;
127     pt1 *= a;
128     pt1 /= a;
129     double value = norm(pt); // L2 norm
130     pt1 == pt2;
131     pt1 != pt2;
132 @endcode
133 For your convenience, the following type aliases are defined:
134 @code
135     typedef Point_<int> Point2i;
136     typedef Point2i Point;
137     typedef Point_<float> Point2f;
138     typedef Point_<double> Point2d;
139 @endcode
140 Example:
141 @code
142     Point2f a(0.3f, 0.f), b(0.f, 0.4f);
143     Point pt = (a + b)*10.f;
144     cout << pt.x << ", " << pt.y << endl;
145 @endcode
146 */
147 template<typename _Tp> class Point_
148 {
149 public:
150     typedef _Tp value_type;
151 
152     // various constructors
153     Point_();
154     Point_(_Tp _x, _Tp _y);
155     Point_(const Point_& pt);
156     Point_(const Size_<_Tp>& sz);
157     Point_(const Vec<_Tp, 2>& v);
158 
159     Point_& operator = (const Point_& pt);
160     //! conversion to another data type
161     template<typename _Tp2> operator Point_<_Tp2>() const;
162 
163     //! conversion to the old-style C structures
164     operator Vec<_Tp, 2>() const;
165 
166     //! dot product
167     _Tp dot(const Point_& pt) const;
168     //! dot product computed in double-precision arithmetics
169     double ddot(const Point_& pt) const;
170     //! cross-product
171     double cross(const Point_& pt) const;
172     //! checks whether the point is inside the specified rectangle
173     bool inside(const Rect_<_Tp>& r) const;
174 
175     _Tp x, y; //< the point coordinates
176 };
177 
178 typedef Point_<int> Point2i;
179 typedef Point_<float> Point2f;
180 typedef Point_<double> Point2d;
181 typedef Point2i Point;
182 
183 template<typename _Tp> class DataType< Point_<_Tp> >
184 {
185 public:
186     typedef Point_<_Tp>                               value_type;
187     typedef Point_<typename DataType<_Tp>::work_type> work_type;
188     typedef _Tp                                       channel_type;
189 
190     enum { generic_type = 0,
191            depth        = DataType<channel_type>::depth,
192            channels     = 2,
193            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
194            type         = CV_MAKETYPE(depth, channels)
195          };
196 
197     typedef Vec<channel_type, channels> vec_type;
198 };
199 
200 
201 
202 //////////////////////////////// Point3_ ////////////////////////////////
203 
204 /** @brief Template class for 3D points specified by its coordinates `x`, `y` and `z`.
205 
206 An instance of the class is interchangeable with the C structure CvPoint2D32f . Similarly to
207 Point_ , the coordinates of 3D points can be converted to another type. The vector arithmetic and
208 comparison operations are also supported.
209 
210 The following Point3_\<\> aliases are available:
211 @code
212     typedef Point3_<int> Point3i;
213     typedef Point3_<float> Point3f;
214     typedef Point3_<double> Point3d;
215 @endcode
216 @see cv::Point3i, cv::Point3f and cv::Point3d
217 */
218 template<typename _Tp> class Point3_
219 {
220 public:
221     typedef _Tp value_type;
222 
223     // various constructors
224     Point3_();
225     Point3_(_Tp _x, _Tp _y, _Tp _z);
226     Point3_(const Point3_& pt);
227     explicit Point3_(const Point_<_Tp>& pt);
228     Point3_(const Vec<_Tp, 3>& v);
229 
230     Point3_& operator = (const Point3_& pt);
231     //! conversion to another data type
232     template<typename _Tp2> operator Point3_<_Tp2>() const;
233     //! conversion to cv::Vec<>
234     operator Vec<_Tp, 3>() const;
235 
236     //! dot product
237     _Tp dot(const Point3_& pt) const;
238     //! dot product computed in double-precision arithmetics
239     double ddot(const Point3_& pt) const;
240     //! cross product of the 2 3D points
241     Point3_ cross(const Point3_& pt) const;
242 
243     _Tp x, y, z; //< the point coordinates
244 };
245 
246 typedef Point3_<int> Point3i;
247 typedef Point3_<float> Point3f;
248 typedef Point3_<double> Point3d;
249 
250 template<typename _Tp> class DataType< Point3_<_Tp> >
251 {
252 public:
253     typedef Point3_<_Tp>                               value_type;
254     typedef Point3_<typename DataType<_Tp>::work_type> work_type;
255     typedef _Tp                                        channel_type;
256 
257     enum { generic_type = 0,
258            depth        = DataType<channel_type>::depth,
259            channels     = 3,
260            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
261            type         = CV_MAKETYPE(depth, channels)
262          };
263 
264     typedef Vec<channel_type, channels> vec_type;
265 };
266 
267 
268 
269 //////////////////////////////// Size_ ////////////////////////////////
270 
271 /** @brief Template class for specifying the size of an image or rectangle.
272 
273 The class includes two members called width and height. The structure can be converted to and from
274 the old OpenCV structures CvSize and CvSize2D32f . The same set of arithmetic and comparison
275 operations as for Point_ is available.
276 
277 OpenCV defines the following Size_\<\> aliases:
278 @code
279     typedef Size_<int> Size2i;
280     typedef Size2i Size;
281     typedef Size_<float> Size2f;
282 @endcode
283 */
284 template<typename _Tp> class Size_
285 {
286 public:
287     typedef _Tp value_type;
288 
289     //! various constructors
290     Size_();
291     Size_(_Tp _width, _Tp _height);
292     Size_(const Size_& sz);
293     Size_(const Point_<_Tp>& pt);
294 
295     Size_& operator = (const Size_& sz);
296     //! the area (width*height)
297     _Tp area() const;
298 
299     //! conversion of another data type.
300     template<typename _Tp2> operator Size_<_Tp2>() const;
301 
302     _Tp width, height; // the width and the height
303 };
304 
305 typedef Size_<int> Size2i;
306 typedef Size_<float> Size2f;
307 typedef Size_<double> Size2d;
308 typedef Size2i Size;
309 
310 template<typename _Tp> class DataType< Size_<_Tp> >
311 {
312 public:
313     typedef Size_<_Tp>                               value_type;
314     typedef Size_<typename DataType<_Tp>::work_type> work_type;
315     typedef _Tp                                      channel_type;
316 
317     enum { generic_type = 0,
318            depth        = DataType<channel_type>::depth,
319            channels     = 2,
320            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
321            type         = CV_MAKETYPE(depth, channels)
322          };
323 
324     typedef Vec<channel_type, channels> vec_type;
325 };
326 
327 
328 
329 //////////////////////////////// Rect_ ////////////////////////////////
330 
331 /** @brief Template class for 2D rectangles
332 
333 described by the following parameters:
334 -   Coordinates of the top-left corner. This is a default interpretation of Rect_::x and Rect_::y
335     in OpenCV. Though, in your algorithms you may count x and y from the bottom-left corner.
336 -   Rectangle width and height.
337 
338 OpenCV typically assumes that the top and left boundary of the rectangle are inclusive, while the
339 right and bottom boundaries are not. For example, the method Rect_::contains returns true if
340 
341 \f[x  \leq pt.x < x+width,
342       y  \leq pt.y < y+height\f]
343 
344 Virtually every loop over an image ROI in OpenCV (where ROI is specified by Rect_\<int\> ) is
345 implemented as:
346 @code
347     for(int y = roi.y; y < roi.y + roi.height; y++)
348         for(int x = roi.x; x < roi.x + roi.width; x++)
349         {
350             // ...
351         }
352 @endcode
353 In addition to the class members, the following operations on rectangles are implemented:
354 -   \f$\texttt{rect} = \texttt{rect} \pm \texttt{point}\f$ (shifting a rectangle by a certain offset)
355 -   \f$\texttt{rect} = \texttt{rect} \pm \texttt{size}\f$ (expanding or shrinking a rectangle by a
356     certain amount)
357 -   rect += point, rect -= point, rect += size, rect -= size (augmenting operations)
358 -   rect = rect1 & rect2 (rectangle intersection)
359 -   rect = rect1 | rect2 (minimum area rectangle containing rect1 and rect2 )
360 -   rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
361 -   rect == rect1, rect != rect1 (rectangle comparison)
362 
363 This is an example how the partial ordering on rectangles can be established (rect1 \f$\subseteq\f$
364 rect2):
365 @code
366     template<typename _Tp> inline bool
367     operator <= (const Rect_<_Tp>& r1, const Rect_<_Tp>& r2)
368     {
369         return (r1 & r2) == r1;
370     }
371 @endcode
372 For your convenience, the Rect_\<\> alias is available: cv::Rect
373 */
374 template<typename _Tp> class Rect_
375 {
376 public:
377     typedef _Tp value_type;
378 
379     //! various constructors
380     Rect_();
381     Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
382     Rect_(const Rect_& r);
383     Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
384     Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);
385 
386     Rect_& operator = ( const Rect_& r );
387     //! the top-left corner
388     Point_<_Tp> tl() const;
389     //! the bottom-right corner
390     Point_<_Tp> br() const;
391 
392     //! size (width, height) of the rectangle
393     Size_<_Tp> size() const;
394     //! area (width*height) of the rectangle
395     _Tp area() const;
396 
397     //! conversion to another data type
398     template<typename _Tp2> operator Rect_<_Tp2>() const;
399 
400     //! checks whether the rectangle contains the point
401     bool contains(const Point_<_Tp>& pt) const;
402 
403     _Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle
404 };
405 
406 typedef Rect_<int> Rect2i;
407 typedef Rect_<float> Rect2f;
408 typedef Rect_<double> Rect2d;
409 typedef Rect2i Rect;
410 
411 template<typename _Tp> class DataType< Rect_<_Tp> >
412 {
413 public:
414     typedef Rect_<_Tp>                               value_type;
415     typedef Rect_<typename DataType<_Tp>::work_type> work_type;
416     typedef _Tp                                      channel_type;
417 
418     enum { generic_type = 0,
419            depth        = DataType<channel_type>::depth,
420            channels     = 4,
421            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
422            type         = CV_MAKETYPE(depth, channels)
423          };
424 
425     typedef Vec<channel_type, channels> vec_type;
426 };
427 
428 
429 
430 ///////////////////////////// RotatedRect /////////////////////////////
431 
432 /** @brief The class represents rotated (i.e. not up-right) rectangles on a plane.
433 
434 Each rectangle is specified by the center point (mass center), length of each side (represented by
435 cv::Size2f structure) and the rotation angle in degrees.
436 
437 The sample below demonstrates how to use RotatedRect:
438 @code
439     Mat image(200, 200, CV_8UC3, Scalar(0));
440     RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);
441 
442     Point2f vertices[4];
443     rRect.points(vertices);
444     for (int i = 0; i < 4; i++)
445         line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));
446 
447     Rect brect = rRect.boundingRect();
448     rectangle(image, brect, Scalar(255,0,0));
449 
450     imshow("rectangles", image);
451     waitKey(0);
452 @endcode
453 ![image](pics/rotatedrect.png)
454 
455 @sa CamShift, fitEllipse, minAreaRect, CvBox2D
456 */
457 class CV_EXPORTS RotatedRect
458 {
459 public:
460     //! various constructors
461     RotatedRect();
462     /**
463     @param center The rectangle mass center.
464     @param size Width and height of the rectangle.
465     @param angle The rotation angle in a clockwise direction. When the angle is 0, 90, 180, 270 etc.,
466     the rectangle becomes an up-right rectangle.
467     */
468     RotatedRect(const Point2f& center, const Size2f& size, float angle);
469     /**
470     Any 3 end points of the RotatedRect. They must be given in order (either clockwise or
471     anticlockwise).
472      */
473     RotatedRect(const Point2f& point1, const Point2f& point2, const Point2f& point3);
474 
475     /** returns 4 vertices of the rectangle
476     @param pts The points array for storing rectangle vertices.
477     */
478     void points(Point2f pts[]) const;
479     //! returns the minimal up-right rectangle containing the rotated rectangle
480     Rect boundingRect() const;
481 
482     Point2f center; //< the rectangle mass center
483     Size2f size;    //< width and height of the rectangle
484     float angle;    //< the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
485 };
486 
487 template<> class DataType< RotatedRect >
488 {
489 public:
490     typedef RotatedRect  value_type;
491     typedef value_type   work_type;
492     typedef float        channel_type;
493 
494     enum { generic_type = 0,
495            depth        = DataType<channel_type>::depth,
496            channels     = (int)sizeof(value_type)/sizeof(channel_type), // 5
497            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
498            type         = CV_MAKETYPE(depth, channels)
499          };
500 
501     typedef Vec<channel_type, channels> vec_type;
502 };
503 
504 
505 
506 //////////////////////////////// Range /////////////////////////////////
507 
508 /** @brief Template class specifying a continuous subsequence (slice) of a sequence.
509 
510 The class is used to specify a row or a column span in a matrix ( Mat ) and for many other purposes.
511 Range(a,b) is basically the same as a:b in Matlab or a..b in Python. As in Python, start is an
512 inclusive left boundary of the range and end is an exclusive right boundary of the range. Such a
513 half-opened interval is usually denoted as \f$[start,end)\f$ .
514 
515 The static method Range::all() returns a special variable that means "the whole sequence" or "the
516 whole range", just like " : " in Matlab or " ... " in Python. All the methods and functions in
517 OpenCV that take Range support this special Range::all() value. But, of course, in case of your own
518 custom processing, you will probably have to check and handle it explicitly:
519 @code
520     void my_function(..., const Range& r, ....)
521     {
522         if(r == Range::all()) {
523             // process all the data
524         }
525         else {
526             // process [r.start, r.end)
527         }
528     }
529 @endcode
530 */
531 class CV_EXPORTS Range
532 {
533 public:
534     Range();
535     Range(int _start, int _end);
536     int size() const;
537     bool empty() const;
538     static Range all();
539 
540     int start, end;
541 };
542 
543 template<> class DataType<Range>
544 {
545 public:
546     typedef Range      value_type;
547     typedef value_type work_type;
548     typedef int        channel_type;
549 
550     enum { generic_type = 0,
551            depth        = DataType<channel_type>::depth,
552            channels     = 2,
553            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
554            type         = CV_MAKETYPE(depth, channels)
555          };
556 
557     typedef Vec<channel_type, channels> vec_type;
558 };
559 
560 
561 
562 //////////////////////////////// Scalar_ ///////////////////////////////
563 
564 /** @brief Template class for a 4-element vector derived from Vec.
565 
566 Being derived from Vec\<_Tp, 4\> , Scalar_ and Scalar can be used just as typical 4-element
567 vectors. In addition, they can be converted to/from CvScalar . The type Scalar is widely used in
568 OpenCV to pass pixel values.
569 */
570 template<typename _Tp> class Scalar_ : public Vec<_Tp, 4>
571 {
572 public:
573     //! various constructors
574     Scalar_();
575     Scalar_(_Tp v0, _Tp v1, _Tp v2=0, _Tp v3=0);
576     Scalar_(_Tp v0);
577 
578     template<typename _Tp2, int cn>
579     Scalar_(const Vec<_Tp2, cn>& v);
580 
581     //! returns a scalar with all elements set to v0
582     static Scalar_<_Tp> all(_Tp v0);
583 
584     //! conversion to another data type
585     template<typename T2> operator Scalar_<T2>() const;
586 
587     //! per-element product
588     Scalar_<_Tp> mul(const Scalar_<_Tp>& a, double scale=1 ) const;
589 
590     // returns (v0, -v1, -v2, -v3)
591     Scalar_<_Tp> conj() const;
592 
593     // returns true iff v1 == v2 == v3 == 0
594     bool isReal() const;
595 };
596 
597 typedef Scalar_<double> Scalar;
598 
599 template<typename _Tp> class DataType< Scalar_<_Tp> >
600 {
601 public:
602     typedef Scalar_<_Tp>                               value_type;
603     typedef Scalar_<typename DataType<_Tp>::work_type> work_type;
604     typedef _Tp                                        channel_type;
605 
606     enum { generic_type = 0,
607            depth        = DataType<channel_type>::depth,
608            channels     = 4,
609            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
610            type         = CV_MAKETYPE(depth, channels)
611          };
612 
613     typedef Vec<channel_type, channels> vec_type;
614 };
615 
616 
617 
618 /////////////////////////////// KeyPoint ////////////////////////////////
619 
620 /** @brief Data structure for salient point detectors.
621 
622 The class instance stores a keypoint, i.e. a point feature found by one of many available keypoint
623 detectors, such as Harris corner detector, cv::FAST, cv::StarDetector, cv::SURF, cv::SIFT,
624 cv::LDetector etc.
625 
626 The keypoint is characterized by the 2D position, scale (proportional to the diameter of the
627 neighborhood that needs to be taken into account), orientation and some other parameters. The
628 keypoint neighborhood is then analyzed by another algorithm that builds a descriptor (usually
629 represented as a feature vector). The keypoints representing the same object in different images
630 can then be matched using cv::KDTree or another method.
631 */
632 class CV_EXPORTS_W_SIMPLE KeyPoint
633 {
634 public:
635     //! the default constructor
636     CV_WRAP KeyPoint();
637     /**
638     @param _pt x & y coordinates of the keypoint
639     @param _size keypoint diameter
640     @param _angle keypoint orientation
641     @param _response keypoint detector response on the keypoint (that is, strength of the keypoint)
642     @param _octave pyramid octave in which the keypoint has been detected
643     @param _class_id object id
644      */
645     KeyPoint(Point2f _pt, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1);
646     /**
647     @param x x-coordinate of the keypoint
648     @param y y-coordinate of the keypoint
649     @param _size keypoint diameter
650     @param _angle keypoint orientation
651     @param _response keypoint detector response on the keypoint (that is, strength of the keypoint)
652     @param _octave pyramid octave in which the keypoint has been detected
653     @param _class_id object id
654      */
655     CV_WRAP KeyPoint(float x, float y, float _size, float _angle=-1, float _response=0, int _octave=0, int _class_id=-1);
656 
657     size_t hash() const;
658 
659     /**
660     This method converts vector of keypoints to vector of points or the reverse, where each keypoint is
661     assigned the same size and the same orientation.
662 
663     @param keypoints Keypoints obtained from any feature detection algorithm like SIFT/SURF/ORB
664     @param points2f Array of (x,y) coordinates of each keypoint
665     @param keypointIndexes Array of indexes of keypoints to be converted to points. (Acts like a mask to
666     convert only specified keypoints)
667     */
668     CV_WRAP static void convert(const std::vector<KeyPoint>& keypoints,
669                                 CV_OUT std::vector<Point2f>& points2f,
670                                 const std::vector<int>& keypointIndexes=std::vector<int>());
671     /** @overload
672     @param points2f Array of (x,y) coordinates of each keypoint
673     @param keypoints Keypoints obtained from any feature detection algorithm like SIFT/SURF/ORB
674     @param size keypoint diameter
675     @param response keypoint detector response on the keypoint (that is, strength of the keypoint)
676     @param octave pyramid octave in which the keypoint has been detected
677     @param class_id object id
678     */
679     CV_WRAP static void convert(const std::vector<Point2f>& points2f,
680                                 CV_OUT std::vector<KeyPoint>& keypoints,
681                                 float size=1, float response=1, int octave=0, int class_id=-1);
682 
683     /**
684     This method computes overlap for pair of keypoints. Overlap is the ratio between area of keypoint
685     regions' intersection and area of keypoint regions' union (considering keypoint region as circle).
686     If they don't overlap, we get zero. If they coincide at same location with same size, we get 1.
687     @param kp1 First keypoint
688     @param kp2 Second keypoint
689     */
690     CV_WRAP static float overlap(const KeyPoint& kp1, const KeyPoint& kp2);
691 
692     CV_PROP_RW Point2f pt; //!< coordinates of the keypoints
693     CV_PROP_RW float size; //!< diameter of the meaningful keypoint neighborhood
694     CV_PROP_RW float angle; //!< computed orientation of the keypoint (-1 if not applicable);
695                             //!< it's in [0,360) degrees and measured relative to
696                             //!< image coordinate system, ie in clockwise.
697     CV_PROP_RW float response; //!< the response by which the most strong keypoints have been selected. Can be used for the further sorting or subsampling
698     CV_PROP_RW int octave; //!< octave (pyramid layer) from which the keypoint has been extracted
699     CV_PROP_RW int class_id; //!< object class (if the keypoints need to be clustered by an object they belong to)
700 };
701 
702 template<> class DataType<KeyPoint>
703 {
704 public:
705     typedef KeyPoint      value_type;
706     typedef float         work_type;
707     typedef float         channel_type;
708 
709     enum { generic_type = 0,
710            depth        = DataType<channel_type>::depth,
711            channels     = (int)(sizeof(value_type)/sizeof(channel_type)), // 7
712            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
713            type         = CV_MAKETYPE(depth, channels)
714          };
715 
716     typedef Vec<channel_type, channels> vec_type;
717 };
718 
719 
720 
721 //////////////////////////////// DMatch /////////////////////////////////
722 
723 /** @brief Class for matching keypoint descriptors
724 
725 query descriptor index, train descriptor index, train image index, and distance between
726 descriptors.
727 */
728 class CV_EXPORTS_W_SIMPLE DMatch
729 {
730 public:
731     CV_WRAP DMatch();
732     CV_WRAP DMatch(int _queryIdx, int _trainIdx, float _distance);
733     CV_WRAP DMatch(int _queryIdx, int _trainIdx, int _imgIdx, float _distance);
734 
735     CV_PROP_RW int queryIdx; // query descriptor index
736     CV_PROP_RW int trainIdx; // train descriptor index
737     CV_PROP_RW int imgIdx;   // train image index
738 
739     CV_PROP_RW float distance;
740 
741     // less is better
742     bool operator<(const DMatch &m) const;
743 };
744 
745 template<> class DataType<DMatch>
746 {
747 public:
748     typedef DMatch      value_type;
749     typedef int         work_type;
750     typedef int         channel_type;
751 
752     enum { generic_type = 0,
753            depth        = DataType<channel_type>::depth,
754            channels     = (int)(sizeof(value_type)/sizeof(channel_type)), // 4
755            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
756            type         = CV_MAKETYPE(depth, channels)
757          };
758 
759     typedef Vec<channel_type, channels> vec_type;
760 };
761 
762 
763 
764 ///////////////////////////// TermCriteria //////////////////////////////
765 
766 /** @brief The class defining termination criteria for iterative algorithms.
767 
768 You can initialize it by default constructor and then override any parameters, or the structure may
769 be fully initialized using the advanced variant of the constructor.
770 */
771 class CV_EXPORTS TermCriteria
772 {
773 public:
774     /**
775       Criteria type, can be one of: COUNT, EPS or COUNT + EPS
776     */
777     enum Type
778     {
779         COUNT=1, //!< the maximum number of iterations or elements to compute
780         MAX_ITER=COUNT, //!< ditto
781         EPS=2 //!< the desired accuracy or change in parameters at which the iterative algorithm stops
782     };
783 
784     //! default constructor
785     TermCriteria();
786     /**
787     @param type The type of termination criteria, one of TermCriteria::Type
788     @param maxCount The maximum number of iterations or elements to compute.
789     @param epsilon The desired accuracy or change in parameters at which the iterative algorithm stops.
790     */
791     TermCriteria(int type, int maxCount, double epsilon);
792 
793     int type; //!< the type of termination criteria: COUNT, EPS or COUNT + EPS
794     int maxCount; // the maximum number of iterations/elements
795     double epsilon; // the desired accuracy
796 };
797 
798 
799 //! @} core_basic
800 
801 ///////////////////////// raster image moments //////////////////////////
802 
803 //! @addtogroup imgproc_shape
804 //! @{
805 
806 /** @brief struct returned by cv::moments
807 
808 The spatial moments \f$\texttt{Moments::m}_{ji}\f$ are computed as:
809 
810 \f[\texttt{m} _{ji}= \sum _{x,y}  \left ( \texttt{array} (x,y)  \cdot x^j  \cdot y^i \right )\f]
811 
812 The central moments \f$\texttt{Moments::mu}_{ji}\f$ are computed as:
813 
814 \f[\texttt{mu} _{ji}= \sum _{x,y}  \left ( \texttt{array} (x,y)  \cdot (x -  \bar{x} )^j  \cdot (y -  \bar{y} )^i \right )\f]
815 
816 where \f$(\bar{x}, \bar{y})\f$ is the mass center:
817 
818 \f[\bar{x} = \frac{\texttt{m}_{10}}{\texttt{m}_{00}} , \; \bar{y} = \frac{\texttt{m}_{01}}{\texttt{m}_{00}}\f]
819 
820 The normalized central moments \f$\texttt{Moments::nu}_{ij}\f$ are computed as:
821 
822 \f[\texttt{nu} _{ji}= \frac{\texttt{mu}_{ji}}{\texttt{m}_{00}^{(i+j)/2+1}} .\f]
823 
824 @note
825 \f$\texttt{mu}_{00}=\texttt{m}_{00}\f$, \f$\texttt{nu}_{00}=1\f$
826 \f$\texttt{nu}_{10}=\texttt{mu}_{10}=\texttt{mu}_{01}=\texttt{mu}_{10}=0\f$ , hence the values are not
827 stored.
828 
829 The moments of a contour are defined in the same way but computed using the Green's formula (see
830 <http://en.wikipedia.org/wiki/Green_theorem>). So, due to a limited raster resolution, the moments
831 computed for a contour are slightly different from the moments computed for the same rasterized
832 contour.
833 
834 @note
835 Since the contour moments are computed using Green formula, you may get seemingly odd results for
836 contours with self-intersections, e.g. a zero area (m00) for butterfly-shaped contours.
837  */
838 class CV_EXPORTS_W_MAP Moments
839 {
840 public:
841     //! the default constructor
842     Moments();
843     //! the full constructor
844     Moments(double m00, double m10, double m01, double m20, double m11,
845             double m02, double m30, double m21, double m12, double m03 );
846     ////! the conversion from CvMoments
847     //Moments( const CvMoments& moments );
848     ////! the conversion to CvMoments
849     //operator CvMoments() const;
850 
851     //! @name spatial moments
852     //! @{
853     CV_PROP_RW double  m00, m10, m01, m20, m11, m02, m30, m21, m12, m03;
854     //! @}
855 
856     //! @name central moments
857     //! @{
858     CV_PROP_RW double  mu20, mu11, mu02, mu30, mu21, mu12, mu03;
859     //! @}
860 
861     //! @name central normalized moments
862     //! @{
863     CV_PROP_RW double  nu20, nu11, nu02, nu30, nu21, nu12, nu03;
864     //! @}
865 };
866 
867 template<> class DataType<Moments>
868 {
869 public:
870     typedef Moments     value_type;
871     typedef double      work_type;
872     typedef double      channel_type;
873 
874     enum { generic_type = 0,
875            depth        = DataType<channel_type>::depth,
876            channels     = (int)(sizeof(value_type)/sizeof(channel_type)), // 24
877            fmt          = DataType<channel_type>::fmt + ((channels - 1) << 8),
878            type         = CV_MAKETYPE(depth, channels)
879          };
880 
881     typedef Vec<channel_type, channels> vec_type;
882 };
883 
884 //! @} imgproc_shape
885 
886 //! @cond IGNORED
887 
888 /////////////////////////////////////////////////////////////////////////
889 ///////////////////////////// Implementation ////////////////////////////
890 /////////////////////////////////////////////////////////////////////////
891 
892 //////////////////////////////// Complex ////////////////////////////////
893 
894 template<typename _Tp> inline
Complex()895 Complex<_Tp>::Complex()
896     : re(0), im(0) {}
897 
898 template<typename _Tp> inline
Complex(_Tp _re,_Tp _im)899 Complex<_Tp>::Complex( _Tp _re, _Tp _im )
900     : re(_re), im(_im) {}
901 
902 template<typename _Tp> template<typename T2> inline
operator Complex<T2>() const903 Complex<_Tp>::operator Complex<T2>() const
904 {
905     return Complex<T2>(saturate_cast<T2>(re), saturate_cast<T2>(im));
906 }
907 
908 template<typename _Tp> inline
conj() const909 Complex<_Tp> Complex<_Tp>::conj() const
910 {
911     return Complex<_Tp>(re, -im);
912 }
913 
914 
915 template<typename _Tp> static inline
operator ==(const Complex<_Tp> & a,const Complex<_Tp> & b)916 bool operator == (const Complex<_Tp>& a, const Complex<_Tp>& b)
917 {
918     return a.re == b.re && a.im == b.im;
919 }
920 
921 template<typename _Tp> static inline
operator !=(const Complex<_Tp> & a,const Complex<_Tp> & b)922 bool operator != (const Complex<_Tp>& a, const Complex<_Tp>& b)
923 {
924     return a.re != b.re || a.im != b.im;
925 }
926 
927 template<typename _Tp> static inline
operator +(const Complex<_Tp> & a,const Complex<_Tp> & b)928 Complex<_Tp> operator + (const Complex<_Tp>& a, const Complex<_Tp>& b)
929 {
930     return Complex<_Tp>( a.re + b.re, a.im + b.im );
931 }
932 
933 template<typename _Tp> static inline
operator +=(Complex<_Tp> & a,const Complex<_Tp> & b)934 Complex<_Tp>& operator += (Complex<_Tp>& a, const Complex<_Tp>& b)
935 {
936     a.re += b.re; a.im += b.im;
937     return a;
938 }
939 
940 template<typename _Tp> static inline
operator -(const Complex<_Tp> & a,const Complex<_Tp> & b)941 Complex<_Tp> operator - (const Complex<_Tp>& a, const Complex<_Tp>& b)
942 {
943     return Complex<_Tp>( a.re - b.re, a.im - b.im );
944 }
945 
946 template<typename _Tp> static inline
operator -=(Complex<_Tp> & a,const Complex<_Tp> & b)947 Complex<_Tp>& operator -= (Complex<_Tp>& a, const Complex<_Tp>& b)
948 {
949     a.re -= b.re; a.im -= b.im;
950     return a;
951 }
952 
953 template<typename _Tp> static inline
operator -(const Complex<_Tp> & a)954 Complex<_Tp> operator - (const Complex<_Tp>& a)
955 {
956     return Complex<_Tp>(-a.re, -a.im);
957 }
958 
959 template<typename _Tp> static inline
operator *(const Complex<_Tp> & a,const Complex<_Tp> & b)960 Complex<_Tp> operator * (const Complex<_Tp>& a, const Complex<_Tp>& b)
961 {
962     return Complex<_Tp>( a.re*b.re - a.im*b.im, a.re*b.im + a.im*b.re );
963 }
964 
965 template<typename _Tp> static inline
operator *(const Complex<_Tp> & a,_Tp b)966 Complex<_Tp> operator * (const Complex<_Tp>& a, _Tp b)
967 {
968     return Complex<_Tp>( a.re*b, a.im*b );
969 }
970 
971 template<typename _Tp> static inline
operator *(_Tp b,const Complex<_Tp> & a)972 Complex<_Tp> operator * (_Tp b, const Complex<_Tp>& a)
973 {
974     return Complex<_Tp>( a.re*b, a.im*b );
975 }
976 
977 template<typename _Tp> static inline
operator +(const Complex<_Tp> & a,_Tp b)978 Complex<_Tp> operator + (const Complex<_Tp>& a, _Tp b)
979 {
980     return Complex<_Tp>( a.re + b, a.im );
981 }
982 
983 template<typename _Tp> static inline
operator -(const Complex<_Tp> & a,_Tp b)984 Complex<_Tp> operator - (const Complex<_Tp>& a, _Tp b)
985 { return Complex<_Tp>( a.re - b, a.im ); }
986 
987 template<typename _Tp> static inline
operator +(_Tp b,const Complex<_Tp> & a)988 Complex<_Tp> operator + (_Tp b, const Complex<_Tp>& a)
989 {
990     return Complex<_Tp>( a.re + b, a.im );
991 }
992 
993 template<typename _Tp> static inline
operator -(_Tp b,const Complex<_Tp> & a)994 Complex<_Tp> operator - (_Tp b, const Complex<_Tp>& a)
995 {
996     return Complex<_Tp>( b - a.re, -a.im );
997 }
998 
999 template<typename _Tp> static inline
operator +=(Complex<_Tp> & a,_Tp b)1000 Complex<_Tp>& operator += (Complex<_Tp>& a, _Tp b)
1001 {
1002     a.re += b; return a;
1003 }
1004 
1005 template<typename _Tp> static inline
operator -=(Complex<_Tp> & a,_Tp b)1006 Complex<_Tp>& operator -= (Complex<_Tp>& a, _Tp b)
1007 {
1008     a.re -= b; return a;
1009 }
1010 
1011 template<typename _Tp> static inline
operator *=(Complex<_Tp> & a,_Tp b)1012 Complex<_Tp>& operator *= (Complex<_Tp>& a, _Tp b)
1013 {
1014     a.re *= b; a.im *= b; return a;
1015 }
1016 
1017 template<typename _Tp> static inline
abs(const Complex<_Tp> & a)1018 double abs(const Complex<_Tp>& a)
1019 {
1020     return std::sqrt( (double)a.re*a.re + (double)a.im*a.im);
1021 }
1022 
1023 template<typename _Tp> static inline
operator /(const Complex<_Tp> & a,const Complex<_Tp> & b)1024 Complex<_Tp> operator / (const Complex<_Tp>& a, const Complex<_Tp>& b)
1025 {
1026     double t = 1./((double)b.re*b.re + (double)b.im*b.im);
1027     return Complex<_Tp>( (_Tp)((a.re*b.re + a.im*b.im)*t),
1028                         (_Tp)((-a.re*b.im + a.im*b.re)*t) );
1029 }
1030 
1031 template<typename _Tp> static inline
operator /=(Complex<_Tp> & a,const Complex<_Tp> & b)1032 Complex<_Tp>& operator /= (Complex<_Tp>& a, const Complex<_Tp>& b)
1033 {
1034     return (a = a / b);
1035 }
1036 
1037 template<typename _Tp> static inline
operator /(const Complex<_Tp> & a,_Tp b)1038 Complex<_Tp> operator / (const Complex<_Tp>& a, _Tp b)
1039 {
1040     _Tp t = (_Tp)1/b;
1041     return Complex<_Tp>( a.re*t, a.im*t );
1042 }
1043 
1044 template<typename _Tp> static inline
operator /(_Tp b,const Complex<_Tp> & a)1045 Complex<_Tp> operator / (_Tp b, const Complex<_Tp>& a)
1046 {
1047     return Complex<_Tp>(b)/a;
1048 }
1049 
1050 template<typename _Tp> static inline
operator /=(const Complex<_Tp> & a,_Tp b)1051 Complex<_Tp> operator /= (const Complex<_Tp>& a, _Tp b)
1052 {
1053     _Tp t = (_Tp)1/b;
1054     a.re *= t; a.im *= t; return a;
1055 }
1056 
1057 
1058 
1059 //////////////////////////////// 2D Point ///////////////////////////////
1060 
1061 template<typename _Tp> inline
Point_()1062 Point_<_Tp>::Point_()
1063     : x(0), y(0) {}
1064 
1065 template<typename _Tp> inline
Point_(_Tp _x,_Tp _y)1066 Point_<_Tp>::Point_(_Tp _x, _Tp _y)
1067     : x(_x), y(_y) {}
1068 
1069 template<typename _Tp> inline
Point_(const Point_ & pt)1070 Point_<_Tp>::Point_(const Point_& pt)
1071     : x(pt.x), y(pt.y) {}
1072 
1073 template<typename _Tp> inline
Point_(const Size_<_Tp> & sz)1074 Point_<_Tp>::Point_(const Size_<_Tp>& sz)
1075     : x(sz.width), y(sz.height) {}
1076 
1077 template<typename _Tp> inline
Point_(const Vec<_Tp,2> & v)1078 Point_<_Tp>::Point_(const Vec<_Tp,2>& v)
1079     : x(v[0]), y(v[1]) {}
1080 
1081 template<typename _Tp> inline
operator =(const Point_ & pt)1082 Point_<_Tp>& Point_<_Tp>::operator = (const Point_& pt)
1083 {
1084     x = pt.x; y = pt.y;
1085     return *this;
1086 }
1087 
1088 template<typename _Tp> template<typename _Tp2> inline
operator Point_<_Tp2>() const1089 Point_<_Tp>::operator Point_<_Tp2>() const
1090 {
1091     return Point_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y));
1092 }
1093 
1094 template<typename _Tp> inline
operator Vec<_Tp,2>() const1095 Point_<_Tp>::operator Vec<_Tp, 2>() const
1096 {
1097     return Vec<_Tp, 2>(x, y);
1098 }
1099 
1100 template<typename _Tp> inline
dot(const Point_ & pt) const1101 _Tp Point_<_Tp>::dot(const Point_& pt) const
1102 {
1103     return saturate_cast<_Tp>(x*pt.x + y*pt.y);
1104 }
1105 
1106 template<typename _Tp> inline
ddot(const Point_ & pt) const1107 double Point_<_Tp>::ddot(const Point_& pt) const
1108 {
1109     return (double)x*pt.x + (double)y*pt.y;
1110 }
1111 
1112 template<typename _Tp> inline
cross(const Point_ & pt) const1113 double Point_<_Tp>::cross(const Point_& pt) const
1114 {
1115     return (double)x*pt.y - (double)y*pt.x;
1116 }
1117 
1118 template<typename _Tp> inline bool
inside(const Rect_<_Tp> & r) const1119 Point_<_Tp>::inside( const Rect_<_Tp>& r ) const
1120 {
1121     return r.contains(*this);
1122 }
1123 
1124 
1125 template<typename _Tp> static inline
operator +=(Point_<_Tp> & a,const Point_<_Tp> & b)1126 Point_<_Tp>& operator += (Point_<_Tp>& a, const Point_<_Tp>& b)
1127 {
1128     a.x += b.x;
1129     a.y += b.y;
1130     return a;
1131 }
1132 
1133 template<typename _Tp> static inline
operator -=(Point_<_Tp> & a,const Point_<_Tp> & b)1134 Point_<_Tp>& operator -= (Point_<_Tp>& a, const Point_<_Tp>& b)
1135 {
1136     a.x -= b.x;
1137     a.y -= b.y;
1138     return a;
1139 }
1140 
1141 template<typename _Tp> static inline
operator *=(Point_<_Tp> & a,int b)1142 Point_<_Tp>& operator *= (Point_<_Tp>& a, int b)
1143 {
1144     a.x = saturate_cast<_Tp>(a.x * b);
1145     a.y = saturate_cast<_Tp>(a.y * b);
1146     return a;
1147 }
1148 
1149 template<typename _Tp> static inline
operator *=(Point_<_Tp> & a,float b)1150 Point_<_Tp>& operator *= (Point_<_Tp>& a, float b)
1151 {
1152     a.x = saturate_cast<_Tp>(a.x * b);
1153     a.y = saturate_cast<_Tp>(a.y * b);
1154     return a;
1155 }
1156 
1157 template<typename _Tp> static inline
operator *=(Point_<_Tp> & a,double b)1158 Point_<_Tp>& operator *= (Point_<_Tp>& a, double b)
1159 {
1160     a.x = saturate_cast<_Tp>(a.x * b);
1161     a.y = saturate_cast<_Tp>(a.y * b);
1162     return a;
1163 }
1164 
1165 template<typename _Tp> static inline
operator /=(Point_<_Tp> & a,int b)1166 Point_<_Tp>& operator /= (Point_<_Tp>& a, int b)
1167 {
1168     a.x = saturate_cast<_Tp>(a.x / b);
1169     a.y = saturate_cast<_Tp>(a.y / b);
1170     return a;
1171 }
1172 
1173 template<typename _Tp> static inline
operator /=(Point_<_Tp> & a,float b)1174 Point_<_Tp>& operator /= (Point_<_Tp>& a, float b)
1175 {
1176     a.x = saturate_cast<_Tp>(a.x / b);
1177     a.y = saturate_cast<_Tp>(a.y / b);
1178     return a;
1179 }
1180 
1181 template<typename _Tp> static inline
operator /=(Point_<_Tp> & a,double b)1182 Point_<_Tp>& operator /= (Point_<_Tp>& a, double b)
1183 {
1184     a.x = saturate_cast<_Tp>(a.x / b);
1185     a.y = saturate_cast<_Tp>(a.y / b);
1186     return a;
1187 }
1188 
1189 template<typename _Tp> static inline
norm(const Point_<_Tp> & pt)1190 double norm(const Point_<_Tp>& pt)
1191 {
1192     return std::sqrt((double)pt.x*pt.x + (double)pt.y*pt.y);
1193 }
1194 
1195 template<typename _Tp> static inline
operator ==(const Point_<_Tp> & a,const Point_<_Tp> & b)1196 bool operator == (const Point_<_Tp>& a, const Point_<_Tp>& b)
1197 {
1198     return a.x == b.x && a.y == b.y;
1199 }
1200 
1201 template<typename _Tp> static inline
operator !=(const Point_<_Tp> & a,const Point_<_Tp> & b)1202 bool operator != (const Point_<_Tp>& a, const Point_<_Tp>& b)
1203 {
1204     return a.x != b.x || a.y != b.y;
1205 }
1206 
1207 template<typename _Tp> static inline
operator +(const Point_<_Tp> & a,const Point_<_Tp> & b)1208 Point_<_Tp> operator + (const Point_<_Tp>& a, const Point_<_Tp>& b)
1209 {
1210     return Point_<_Tp>( saturate_cast<_Tp>(a.x + b.x), saturate_cast<_Tp>(a.y + b.y) );
1211 }
1212 
1213 template<typename _Tp> static inline
operator -(const Point_<_Tp> & a,const Point_<_Tp> & b)1214 Point_<_Tp> operator - (const Point_<_Tp>& a, const Point_<_Tp>& b)
1215 {
1216     return Point_<_Tp>( saturate_cast<_Tp>(a.x - b.x), saturate_cast<_Tp>(a.y - b.y) );
1217 }
1218 
1219 template<typename _Tp> static inline
operator -(const Point_<_Tp> & a)1220 Point_<_Tp> operator - (const Point_<_Tp>& a)
1221 {
1222     return Point_<_Tp>( saturate_cast<_Tp>(-a.x), saturate_cast<_Tp>(-a.y) );
1223 }
1224 
1225 template<typename _Tp> static inline
operator *(const Point_<_Tp> & a,int b)1226 Point_<_Tp> operator * (const Point_<_Tp>& a, int b)
1227 {
1228     return Point_<_Tp>( saturate_cast<_Tp>(a.x*b), saturate_cast<_Tp>(a.y*b) );
1229 }
1230 
1231 template<typename _Tp> static inline
operator *(int a,const Point_<_Tp> & b)1232 Point_<_Tp> operator * (int a, const Point_<_Tp>& b)
1233 {
1234     return Point_<_Tp>( saturate_cast<_Tp>(b.x*a), saturate_cast<_Tp>(b.y*a) );
1235 }
1236 
1237 template<typename _Tp> static inline
operator *(const Point_<_Tp> & a,float b)1238 Point_<_Tp> operator * (const Point_<_Tp>& a, float b)
1239 {
1240     return Point_<_Tp>( saturate_cast<_Tp>(a.x*b), saturate_cast<_Tp>(a.y*b) );
1241 }
1242 
1243 template<typename _Tp> static inline
operator *(float a,const Point_<_Tp> & b)1244 Point_<_Tp> operator * (float a, const Point_<_Tp>& b)
1245 {
1246     return Point_<_Tp>( saturate_cast<_Tp>(b.x*a), saturate_cast<_Tp>(b.y*a) );
1247 }
1248 
1249 template<typename _Tp> static inline
operator *(const Point_<_Tp> & a,double b)1250 Point_<_Tp> operator * (const Point_<_Tp>& a, double b)
1251 {
1252     return Point_<_Tp>( saturate_cast<_Tp>(a.x*b), saturate_cast<_Tp>(a.y*b) );
1253 }
1254 
1255 template<typename _Tp> static inline
operator *(double a,const Point_<_Tp> & b)1256 Point_<_Tp> operator * (double a, const Point_<_Tp>& b)
1257 {
1258     return Point_<_Tp>( saturate_cast<_Tp>(b.x*a), saturate_cast<_Tp>(b.y*a) );
1259 }
1260 
1261 template<typename _Tp> static inline
operator *(const Matx<_Tp,2,2> & a,const Point_<_Tp> & b)1262 Point_<_Tp> operator * (const Matx<_Tp, 2, 2>& a, const Point_<_Tp>& b)
1263 {
1264     Matx<_Tp, 2, 1> tmp = a * Vec<_Tp,2>(b.x, b.y);
1265     return Point_<_Tp>(tmp.val[0], tmp.val[1]);
1266 }
1267 
1268 template<typename _Tp> static inline
operator *(const Matx<_Tp,3,3> & a,const Point_<_Tp> & b)1269 Point3_<_Tp> operator * (const Matx<_Tp, 3, 3>& a, const Point_<_Tp>& b)
1270 {
1271     Matx<_Tp, 3, 1> tmp = a * Vec<_Tp,3>(b.x, b.y, 1);
1272     return Point3_<_Tp>(tmp.val[0], tmp.val[1], tmp.val[2]);
1273 }
1274 
1275 template<typename _Tp> static inline
operator /(const Point_<_Tp> & a,int b)1276 Point_<_Tp> operator / (const Point_<_Tp>& a, int b)
1277 {
1278     Point_<_Tp> tmp(a);
1279     tmp /= b;
1280     return tmp;
1281 }
1282 
1283 template<typename _Tp> static inline
operator /(const Point_<_Tp> & a,float b)1284 Point_<_Tp> operator / (const Point_<_Tp>& a, float b)
1285 {
1286     Point_<_Tp> tmp(a);
1287     tmp /= b;
1288     return tmp;
1289 }
1290 
1291 template<typename _Tp> static inline
operator /(const Point_<_Tp> & a,double b)1292 Point_<_Tp> operator / (const Point_<_Tp>& a, double b)
1293 {
1294     Point_<_Tp> tmp(a);
1295     tmp /= b;
1296     return tmp;
1297 }
1298 
1299 
1300 
1301 //////////////////////////////// 3D Point ///////////////////////////////
1302 
1303 template<typename _Tp> inline
Point3_()1304 Point3_<_Tp>::Point3_()
1305     : x(0), y(0), z(0) {}
1306 
1307 template<typename _Tp> inline
Point3_(_Tp _x,_Tp _y,_Tp _z)1308 Point3_<_Tp>::Point3_(_Tp _x, _Tp _y, _Tp _z)
1309     : x(_x), y(_y), z(_z) {}
1310 
1311 template<typename _Tp> inline
Point3_(const Point3_ & pt)1312 Point3_<_Tp>::Point3_(const Point3_& pt)
1313     : x(pt.x), y(pt.y), z(pt.z) {}
1314 
1315 template<typename _Tp> inline
Point3_(const Point_<_Tp> & pt)1316 Point3_<_Tp>::Point3_(const Point_<_Tp>& pt)
1317     : x(pt.x), y(pt.y), z(_Tp()) {}
1318 
1319 template<typename _Tp> inline
Point3_(const Vec<_Tp,3> & v)1320 Point3_<_Tp>::Point3_(const Vec<_Tp, 3>& v)
1321     : x(v[0]), y(v[1]), z(v[2]) {}
1322 
1323 template<typename _Tp> template<typename _Tp2> inline
operator Point3_<_Tp2>() const1324 Point3_<_Tp>::operator Point3_<_Tp2>() const
1325 {
1326     return Point3_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(z));
1327 }
1328 
1329 template<typename _Tp> inline
operator Vec<_Tp,3>() const1330 Point3_<_Tp>::operator Vec<_Tp, 3>() const
1331 {
1332     return Vec<_Tp, 3>(x, y, z);
1333 }
1334 
1335 template<typename _Tp> inline
operator =(const Point3_ & pt)1336 Point3_<_Tp>& Point3_<_Tp>::operator = (const Point3_& pt)
1337 {
1338     x = pt.x; y = pt.y; z = pt.z;
1339     return *this;
1340 }
1341 
1342 template<typename _Tp> inline
dot(const Point3_ & pt) const1343 _Tp Point3_<_Tp>::dot(const Point3_& pt) const
1344 {
1345     return saturate_cast<_Tp>(x*pt.x + y*pt.y + z*pt.z);
1346 }
1347 
1348 template<typename _Tp> inline
ddot(const Point3_ & pt) const1349 double Point3_<_Tp>::ddot(const Point3_& pt) const
1350 {
1351     return (double)x*pt.x + (double)y*pt.y + (double)z*pt.z;
1352 }
1353 
1354 template<typename _Tp> inline
cross(const Point3_<_Tp> & pt) const1355 Point3_<_Tp> Point3_<_Tp>::cross(const Point3_<_Tp>& pt) const
1356 {
1357     return Point3_<_Tp>(y*pt.z - z*pt.y, z*pt.x - x*pt.z, x*pt.y - y*pt.x);
1358 }
1359 
1360 
1361 template<typename _Tp> static inline
operator +=(Point3_<_Tp> & a,const Point3_<_Tp> & b)1362 Point3_<_Tp>& operator += (Point3_<_Tp>& a, const Point3_<_Tp>& b)
1363 {
1364     a.x += b.x;
1365     a.y += b.y;
1366     a.z += b.z;
1367     return a;
1368 }
1369 
1370 template<typename _Tp> static inline
operator -=(Point3_<_Tp> & a,const Point3_<_Tp> & b)1371 Point3_<_Tp>& operator -= (Point3_<_Tp>& a, const Point3_<_Tp>& b)
1372 {
1373     a.x -= b.x;
1374     a.y -= b.y;
1375     a.z -= b.z;
1376     return a;
1377 }
1378 
1379 template<typename _Tp> static inline
operator *=(Point3_<_Tp> & a,int b)1380 Point3_<_Tp>& operator *= (Point3_<_Tp>& a, int b)
1381 {
1382     a.x = saturate_cast<_Tp>(a.x * b);
1383     a.y = saturate_cast<_Tp>(a.y * b);
1384     a.z = saturate_cast<_Tp>(a.z * b);
1385     return a;
1386 }
1387 
1388 template<typename _Tp> static inline
operator *=(Point3_<_Tp> & a,float b)1389 Point3_<_Tp>& operator *= (Point3_<_Tp>& a, float b)
1390 {
1391     a.x = saturate_cast<_Tp>(a.x * b);
1392     a.y = saturate_cast<_Tp>(a.y * b);
1393     a.z = saturate_cast<_Tp>(a.z * b);
1394     return a;
1395 }
1396 
1397 template<typename _Tp> static inline
operator *=(Point3_<_Tp> & a,double b)1398 Point3_<_Tp>& operator *= (Point3_<_Tp>& a, double b)
1399 {
1400     a.x = saturate_cast<_Tp>(a.x * b);
1401     a.y = saturate_cast<_Tp>(a.y * b);
1402     a.z = saturate_cast<_Tp>(a.z * b);
1403     return a;
1404 }
1405 
1406 template<typename _Tp> static inline
operator /=(Point3_<_Tp> & a,int b)1407 Point3_<_Tp>& operator /= (Point3_<_Tp>& a, int b)
1408 {
1409     a.x = saturate_cast<_Tp>(a.x / b);
1410     a.y = saturate_cast<_Tp>(a.y / b);
1411     a.z = saturate_cast<_Tp>(a.z / b);
1412     return a;
1413 }
1414 
1415 template<typename _Tp> static inline
operator /=(Point3_<_Tp> & a,float b)1416 Point3_<_Tp>& operator /= (Point3_<_Tp>& a, float b)
1417 {
1418     a.x = saturate_cast<_Tp>(a.x / b);
1419     a.y = saturate_cast<_Tp>(a.y / b);
1420     a.z = saturate_cast<_Tp>(a.z / b);
1421     return a;
1422 }
1423 
1424 template<typename _Tp> static inline
operator /=(Point3_<_Tp> & a,double b)1425 Point3_<_Tp>& operator /= (Point3_<_Tp>& a, double b)
1426 {
1427     a.x = saturate_cast<_Tp>(a.x / b);
1428     a.y = saturate_cast<_Tp>(a.y / b);
1429     a.z = saturate_cast<_Tp>(a.z / b);
1430     return a;
1431 }
1432 
1433 template<typename _Tp> static inline
norm(const Point3_<_Tp> & pt)1434 double norm(const Point3_<_Tp>& pt)
1435 {
1436     return std::sqrt((double)pt.x*pt.x + (double)pt.y*pt.y + (double)pt.z*pt.z);
1437 }
1438 
1439 template<typename _Tp> static inline
operator ==(const Point3_<_Tp> & a,const Point3_<_Tp> & b)1440 bool operator == (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
1441 {
1442     return a.x == b.x && a.y == b.y && a.z == b.z;
1443 }
1444 
1445 template<typename _Tp> static inline
operator !=(const Point3_<_Tp> & a,const Point3_<_Tp> & b)1446 bool operator != (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
1447 {
1448     return a.x != b.x || a.y != b.y || a.z != b.z;
1449 }
1450 
1451 template<typename _Tp> static inline
operator +(const Point3_<_Tp> & a,const Point3_<_Tp> & b)1452 Point3_<_Tp> operator + (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
1453 {
1454     return Point3_<_Tp>( saturate_cast<_Tp>(a.x + b.x), saturate_cast<_Tp>(a.y + b.y), saturate_cast<_Tp>(a.z + b.z));
1455 }
1456 
1457 template<typename _Tp> static inline
operator -(const Point3_<_Tp> & a,const Point3_<_Tp> & b)1458 Point3_<_Tp> operator - (const Point3_<_Tp>& a, const Point3_<_Tp>& b)
1459 {
1460     return Point3_<_Tp>( saturate_cast<_Tp>(a.x - b.x), saturate_cast<_Tp>(a.y - b.y), saturate_cast<_Tp>(a.z - b.z));
1461 }
1462 
1463 template<typename _Tp> static inline
operator -(const Point3_<_Tp> & a)1464 Point3_<_Tp> operator - (const Point3_<_Tp>& a)
1465 {
1466     return Point3_<_Tp>( saturate_cast<_Tp>(-a.x), saturate_cast<_Tp>(-a.y), saturate_cast<_Tp>(-a.z) );
1467 }
1468 
1469 template<typename _Tp> static inline
operator *(const Point3_<_Tp> & a,int b)1470 Point3_<_Tp> operator * (const Point3_<_Tp>& a, int b)
1471 {
1472     return Point3_<_Tp>( saturate_cast<_Tp>(a.x*b), saturate_cast<_Tp>(a.y*b), saturate_cast<_Tp>(a.z*b) );
1473 }
1474 
1475 template<typename _Tp> static inline
operator *(int a,const Point3_<_Tp> & b)1476 Point3_<_Tp> operator * (int a, const Point3_<_Tp>& b)
1477 {
1478     return Point3_<_Tp>( saturate_cast<_Tp>(b.x * a), saturate_cast<_Tp>(b.y * a), saturate_cast<_Tp>(b.z * a) );
1479 }
1480 
1481 template<typename _Tp> static inline
operator *(const Point3_<_Tp> & a,float b)1482 Point3_<_Tp> operator * (const Point3_<_Tp>& a, float b)
1483 {
1484     return Point3_<_Tp>( saturate_cast<_Tp>(a.x * b), saturate_cast<_Tp>(a.y * b), saturate_cast<_Tp>(a.z * b) );
1485 }
1486 
1487 template<typename _Tp> static inline
operator *(float a,const Point3_<_Tp> & b)1488 Point3_<_Tp> operator * (float a, const Point3_<_Tp>& b)
1489 {
1490     return Point3_<_Tp>( saturate_cast<_Tp>(b.x * a), saturate_cast<_Tp>(b.y * a), saturate_cast<_Tp>(b.z * a) );
1491 }
1492 
1493 template<typename _Tp> static inline
operator *(const Point3_<_Tp> & a,double b)1494 Point3_<_Tp> operator * (const Point3_<_Tp>& a, double b)
1495 {
1496     return Point3_<_Tp>( saturate_cast<_Tp>(a.x * b), saturate_cast<_Tp>(a.y * b), saturate_cast<_Tp>(a.z * b) );
1497 }
1498 
1499 template<typename _Tp> static inline
operator *(double a,const Point3_<_Tp> & b)1500 Point3_<_Tp> operator * (double a, const Point3_<_Tp>& b)
1501 {
1502     return Point3_<_Tp>( saturate_cast<_Tp>(b.x * a), saturate_cast<_Tp>(b.y * a), saturate_cast<_Tp>(b.z * a) );
1503 }
1504 
1505 template<typename _Tp> static inline
operator *(const Matx<_Tp,3,3> & a,const Point3_<_Tp> & b)1506 Point3_<_Tp> operator * (const Matx<_Tp, 3, 3>& a, const Point3_<_Tp>& b)
1507 {
1508     Matx<_Tp, 3, 1> tmp = a * Vec<_Tp,3>(b.x, b.y, b.z);
1509     return Point3_<_Tp>(tmp.val[0], tmp.val[1], tmp.val[2]);
1510 }
1511 
1512 template<typename _Tp> static inline
operator *(const Matx<_Tp,4,4> & a,const Point3_<_Tp> & b)1513 Matx<_Tp, 4, 1> operator * (const Matx<_Tp, 4, 4>& a, const Point3_<_Tp>& b)
1514 {
1515     return a * Matx<_Tp, 4, 1>(b.x, b.y, b.z, 1);
1516 }
1517 
1518 template<typename _Tp> static inline
operator /(const Point3_<_Tp> & a,int b)1519 Point3_<_Tp> operator / (const Point3_<_Tp>& a, int b)
1520 {
1521     Point3_<_Tp> tmp(a);
1522     tmp /= b;
1523     return tmp;
1524 }
1525 
1526 template<typename _Tp> static inline
operator /(const Point3_<_Tp> & a,float b)1527 Point3_<_Tp> operator / (const Point3_<_Tp>& a, float b)
1528 {
1529     Point3_<_Tp> tmp(a);
1530     tmp /= b;
1531     return tmp;
1532 }
1533 
1534 template<typename _Tp> static inline
operator /(const Point3_<_Tp> & a,double b)1535 Point3_<_Tp> operator / (const Point3_<_Tp>& a, double b)
1536 {
1537     Point3_<_Tp> tmp(a);
1538     tmp /= b;
1539     return tmp;
1540 }
1541 
1542 
1543 
1544 ////////////////////////////////// Size /////////////////////////////////
1545 
1546 template<typename _Tp> inline
Size_()1547 Size_<_Tp>::Size_()
1548     : width(0), height(0) {}
1549 
1550 template<typename _Tp> inline
Size_(_Tp _width,_Tp _height)1551 Size_<_Tp>::Size_(_Tp _width, _Tp _height)
1552     : width(_width), height(_height) {}
1553 
1554 template<typename _Tp> inline
Size_(const Size_ & sz)1555 Size_<_Tp>::Size_(const Size_& sz)
1556     : width(sz.width), height(sz.height) {}
1557 
1558 template<typename _Tp> inline
Size_(const Point_<_Tp> & pt)1559 Size_<_Tp>::Size_(const Point_<_Tp>& pt)
1560     : width(pt.x), height(pt.y) {}
1561 
1562 template<typename _Tp> template<typename _Tp2> inline
operator Size_<_Tp2>() const1563 Size_<_Tp>::operator Size_<_Tp2>() const
1564 {
1565     return Size_<_Tp2>(saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height));
1566 }
1567 
1568 template<typename _Tp> inline
operator =(const Size_<_Tp> & sz)1569 Size_<_Tp>& Size_<_Tp>::operator = (const Size_<_Tp>& sz)
1570 {
1571     width = sz.width; height = sz.height;
1572     return *this;
1573 }
1574 
1575 template<typename _Tp> inline
area() const1576 _Tp Size_<_Tp>::area() const
1577 {
1578     return width * height;
1579 }
1580 
1581 template<typename _Tp> static inline
operator *=(Size_<_Tp> & a,_Tp b)1582 Size_<_Tp>& operator *= (Size_<_Tp>& a, _Tp b)
1583 {
1584     a.width *= b;
1585     a.height *= b;
1586     return a;
1587 }
1588 
1589 template<typename _Tp> static inline
operator *(const Size_<_Tp> & a,_Tp b)1590 Size_<_Tp> operator * (const Size_<_Tp>& a, _Tp b)
1591 {
1592     Size_<_Tp> tmp(a);
1593     tmp *= b;
1594     return tmp;
1595 }
1596 
1597 template<typename _Tp> static inline
operator /=(Size_<_Tp> & a,_Tp b)1598 Size_<_Tp>& operator /= (Size_<_Tp>& a, _Tp b)
1599 {
1600     a.width /= b;
1601     a.height /= b;
1602     return a;
1603 }
1604 
1605 template<typename _Tp> static inline
operator /(const Size_<_Tp> & a,_Tp b)1606 Size_<_Tp> operator / (const Size_<_Tp>& a, _Tp b)
1607 {
1608     Size_<_Tp> tmp(a);
1609     tmp /= b;
1610     return tmp;
1611 }
1612 
1613 template<typename _Tp> static inline
operator +=(Size_<_Tp> & a,const Size_<_Tp> & b)1614 Size_<_Tp>& operator += (Size_<_Tp>& a, const Size_<_Tp>& b)
1615 {
1616     a.width += b.width;
1617     a.height += b.height;
1618     return a;
1619 }
1620 
1621 template<typename _Tp> static inline
operator +(const Size_<_Tp> & a,const Size_<_Tp> & b)1622 Size_<_Tp> operator + (const Size_<_Tp>& a, const Size_<_Tp>& b)
1623 {
1624     Size_<_Tp> tmp(a);
1625     tmp += b;
1626     return tmp;
1627 }
1628 
1629 template<typename _Tp> static inline
operator -=(Size_<_Tp> & a,const Size_<_Tp> & b)1630 Size_<_Tp>& operator -= (Size_<_Tp>& a, const Size_<_Tp>& b)
1631 {
1632     a.width -= b.width;
1633     a.height -= b.height;
1634     return a;
1635 }
1636 
1637 template<typename _Tp> static inline
operator -(const Size_<_Tp> & a,const Size_<_Tp> & b)1638 Size_<_Tp> operator - (const Size_<_Tp>& a, const Size_<_Tp>& b)
1639 {
1640     Size_<_Tp> tmp(a);
1641     tmp -= b;
1642     return tmp;
1643 }
1644 
1645 template<typename _Tp> static inline
operator ==(const Size_<_Tp> & a,const Size_<_Tp> & b)1646 bool operator == (const Size_<_Tp>& a, const Size_<_Tp>& b)
1647 {
1648     return a.width == b.width && a.height == b.height;
1649 }
1650 
1651 template<typename _Tp> static inline
operator !=(const Size_<_Tp> & a,const Size_<_Tp> & b)1652 bool operator != (const Size_<_Tp>& a, const Size_<_Tp>& b)
1653 {
1654     return !(a == b);
1655 }
1656 
1657 
1658 
1659 ////////////////////////////////// Rect /////////////////////////////////
1660 
1661 template<typename _Tp> inline
Rect_()1662 Rect_<_Tp>::Rect_()
1663     : x(0), y(0), width(0), height(0) {}
1664 
1665 template<typename _Tp> inline
Rect_(_Tp _x,_Tp _y,_Tp _width,_Tp _height)1666 Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)
1667     : x(_x), y(_y), width(_width), height(_height) {}
1668 
1669 template<typename _Tp> inline
Rect_(const Rect_<_Tp> & r)1670 Rect_<_Tp>::Rect_(const Rect_<_Tp>& r)
1671     : x(r.x), y(r.y), width(r.width), height(r.height) {}
1672 
1673 template<typename _Tp> inline
Rect_(const Point_<_Tp> & org,const Size_<_Tp> & sz)1674 Rect_<_Tp>::Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz)
1675     : x(org.x), y(org.y), width(sz.width), height(sz.height) {}
1676 
1677 template<typename _Tp> inline
Rect_(const Point_<_Tp> & pt1,const Point_<_Tp> & pt2)1678 Rect_<_Tp>::Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2)
1679 {
1680     x = std::min(pt1.x, pt2.x);
1681     y = std::min(pt1.y, pt2.y);
1682     width = std::max(pt1.x, pt2.x) - x;
1683     height = std::max(pt1.y, pt2.y) - y;
1684 }
1685 
1686 template<typename _Tp> inline
operator =(const Rect_<_Tp> & r)1687 Rect_<_Tp>& Rect_<_Tp>::operator = ( const Rect_<_Tp>& r )
1688 {
1689     x = r.x;
1690     y = r.y;
1691     width = r.width;
1692     height = r.height;
1693     return *this;
1694 }
1695 
1696 template<typename _Tp> inline
tl() const1697 Point_<_Tp> Rect_<_Tp>::tl() const
1698 {
1699     return Point_<_Tp>(x,y);
1700 }
1701 
1702 template<typename _Tp> inline
br() const1703 Point_<_Tp> Rect_<_Tp>::br() const
1704 {
1705     return Point_<_Tp>(x + width, y + height);
1706 }
1707 
1708 template<typename _Tp> inline
size() const1709 Size_<_Tp> Rect_<_Tp>::size() const
1710 {
1711     return Size_<_Tp>(width, height);
1712 }
1713 
1714 template<typename _Tp> inline
area() const1715 _Tp Rect_<_Tp>::area() const
1716 {
1717     return width * height;
1718 }
1719 
1720 template<typename _Tp> template<typename _Tp2> inline
operator Rect_<_Tp2>() const1721 Rect_<_Tp>::operator Rect_<_Tp2>() const
1722 {
1723     return Rect_<_Tp2>(saturate_cast<_Tp2>(x), saturate_cast<_Tp2>(y), saturate_cast<_Tp2>(width), saturate_cast<_Tp2>(height));
1724 }
1725 
1726 template<typename _Tp> inline
contains(const Point_<_Tp> & pt) const1727 bool Rect_<_Tp>::contains(const Point_<_Tp>& pt) const
1728 {
1729     return x <= pt.x && pt.x < x + width && y <= pt.y && pt.y < y + height;
1730 }
1731 
1732 
1733 template<typename _Tp> static inline
operator +=(Rect_<_Tp> & a,const Point_<_Tp> & b)1734 Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Point_<_Tp>& b )
1735 {
1736     a.x += b.x;
1737     a.y += b.y;
1738     return a;
1739 }
1740 
1741 template<typename _Tp> static inline
operator -=(Rect_<_Tp> & a,const Point_<_Tp> & b)1742 Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Point_<_Tp>& b )
1743 {
1744     a.x -= b.x;
1745     a.y -= b.y;
1746     return a;
1747 }
1748 
1749 template<typename _Tp> static inline
operator +=(Rect_<_Tp> & a,const Size_<_Tp> & b)1750 Rect_<_Tp>& operator += ( Rect_<_Tp>& a, const Size_<_Tp>& b )
1751 {
1752     a.width += b.width;
1753     a.height += b.height;
1754     return a;
1755 }
1756 
1757 template<typename _Tp> static inline
operator -=(Rect_<_Tp> & a,const Size_<_Tp> & b)1758 Rect_<_Tp>& operator -= ( Rect_<_Tp>& a, const Size_<_Tp>& b )
1759 {
1760     a.width -= b.width;
1761     a.height -= b.height;
1762     return a;
1763 }
1764 
1765 template<typename _Tp> static inline
operator &=(Rect_<_Tp> & a,const Rect_<_Tp> & b)1766 Rect_<_Tp>& operator &= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
1767 {
1768     _Tp x1 = std::max(a.x, b.x);
1769     _Tp y1 = std::max(a.y, b.y);
1770     a.width = std::min(a.x + a.width, b.x + b.width) - x1;
1771     a.height = std::min(a.y + a.height, b.y + b.height) - y1;
1772     a.x = x1;
1773     a.y = y1;
1774     if( a.width <= 0 || a.height <= 0 )
1775         a = Rect();
1776     return a;
1777 }
1778 
1779 template<typename _Tp> static inline
operator |=(Rect_<_Tp> & a,const Rect_<_Tp> & b)1780 Rect_<_Tp>& operator |= ( Rect_<_Tp>& a, const Rect_<_Tp>& b )
1781 {
1782     _Tp x1 = std::min(a.x, b.x);
1783     _Tp y1 = std::min(a.y, b.y);
1784     a.width = std::max(a.x + a.width, b.x + b.width) - x1;
1785     a.height = std::max(a.y + a.height, b.y + b.height) - y1;
1786     a.x = x1;
1787     a.y = y1;
1788     return a;
1789 }
1790 
1791 template<typename _Tp> static inline
operator ==(const Rect_<_Tp> & a,const Rect_<_Tp> & b)1792 bool operator == (const Rect_<_Tp>& a, const Rect_<_Tp>& b)
1793 {
1794     return a.x == b.x && a.y == b.y && a.width == b.width && a.height == b.height;
1795 }
1796 
1797 template<typename _Tp> static inline
operator !=(const Rect_<_Tp> & a,const Rect_<_Tp> & b)1798 bool operator != (const Rect_<_Tp>& a, const Rect_<_Tp>& b)
1799 {
1800     return a.x != b.x || a.y != b.y || a.width != b.width || a.height != b.height;
1801 }
1802 
1803 template<typename _Tp> static inline
operator +(const Rect_<_Tp> & a,const Point_<_Tp> & b)1804 Rect_<_Tp> operator + (const Rect_<_Tp>& a, const Point_<_Tp>& b)
1805 {
1806     return Rect_<_Tp>( a.x + b.x, a.y + b.y, a.width, a.height );
1807 }
1808 
1809 template<typename _Tp> static inline
operator -(const Rect_<_Tp> & a,const Point_<_Tp> & b)1810 Rect_<_Tp> operator - (const Rect_<_Tp>& a, const Point_<_Tp>& b)
1811 {
1812     return Rect_<_Tp>( a.x - b.x, a.y - b.y, a.width, a.height );
1813 }
1814 
1815 template<typename _Tp> static inline
operator +(const Rect_<_Tp> & a,const Size_<_Tp> & b)1816 Rect_<_Tp> operator + (const Rect_<_Tp>& a, const Size_<_Tp>& b)
1817 {
1818     return Rect_<_Tp>( a.x, a.y, a.width + b.width, a.height + b.height );
1819 }
1820 
1821 template<typename _Tp> static inline
operator &(const Rect_<_Tp> & a,const Rect_<_Tp> & b)1822 Rect_<_Tp> operator & (const Rect_<_Tp>& a, const Rect_<_Tp>& b)
1823 {
1824     Rect_<_Tp> c = a;
1825     return c &= b;
1826 }
1827 
1828 template<typename _Tp> static inline
operator |(const Rect_<_Tp> & a,const Rect_<_Tp> & b)1829 Rect_<_Tp> operator | (const Rect_<_Tp>& a, const Rect_<_Tp>& b)
1830 {
1831     Rect_<_Tp> c = a;
1832     return c |= b;
1833 }
1834 
1835 
1836 
1837 ////////////////////////////// RotatedRect //////////////////////////////
1838 
1839 inline
RotatedRect()1840 RotatedRect::RotatedRect()
1841     : center(), size(), angle(0) {}
1842 
1843 inline
RotatedRect(const Point2f & _center,const Size2f & _size,float _angle)1844 RotatedRect::RotatedRect(const Point2f& _center, const Size2f& _size, float _angle)
1845     : center(_center), size(_size), angle(_angle) {}
1846 
1847 
1848 
1849 ///////////////////////////////// Range /////////////////////////////////
1850 
1851 inline
Range()1852 Range::Range()
1853     : start(0), end(0) {}
1854 
1855 inline
Range(int _start,int _end)1856 Range::Range(int _start, int _end)
1857     : start(_start), end(_end) {}
1858 
1859 inline
size() const1860 int Range::size() const
1861 {
1862     return end - start;
1863 }
1864 
1865 inline
empty() const1866 bool Range::empty() const
1867 {
1868     return start == end;
1869 }
1870 
1871 inline
all()1872 Range Range::all()
1873 {
1874     return Range(INT_MIN, INT_MAX);
1875 }
1876 
1877 
1878 static inline
operator ==(const Range & r1,const Range & r2)1879 bool operator == (const Range& r1, const Range& r2)
1880 {
1881     return r1.start == r2.start && r1.end == r2.end;
1882 }
1883 
1884 static inline
operator !=(const Range & r1,const Range & r2)1885 bool operator != (const Range& r1, const Range& r2)
1886 {
1887     return !(r1 == r2);
1888 }
1889 
1890 static inline
operator !(const Range & r)1891 bool operator !(const Range& r)
1892 {
1893     return r.start == r.end;
1894 }
1895 
1896 static inline
operator &(const Range & r1,const Range & r2)1897 Range operator & (const Range& r1, const Range& r2)
1898 {
1899     Range r(std::max(r1.start, r2.start), std::min(r1.end, r2.end));
1900     r.end = std::max(r.end, r.start);
1901     return r;
1902 }
1903 
1904 static inline
operator &=(Range & r1,const Range & r2)1905 Range& operator &= (Range& r1, const Range& r2)
1906 {
1907     r1 = r1 & r2;
1908     return r1;
1909 }
1910 
1911 static inline
operator +(const Range & r1,int delta)1912 Range operator + (const Range& r1, int delta)
1913 {
1914     return Range(r1.start + delta, r1.end + delta);
1915 }
1916 
1917 static inline
operator +(int delta,const Range & r1)1918 Range operator + (int delta, const Range& r1)
1919 {
1920     return Range(r1.start + delta, r1.end + delta);
1921 }
1922 
1923 static inline
operator -(const Range & r1,int delta)1924 Range operator - (const Range& r1, int delta)
1925 {
1926     return r1 + (-delta);
1927 }
1928 
1929 
1930 
1931 ///////////////////////////////// Scalar ////////////////////////////////
1932 
1933 template<typename _Tp> inline
Scalar_()1934 Scalar_<_Tp>::Scalar_()
1935 {
1936     this->val[0] = this->val[1] = this->val[2] = this->val[3] = 0;
1937 }
1938 
1939 template<typename _Tp> inline
Scalar_(_Tp v0,_Tp v1,_Tp v2,_Tp v3)1940 Scalar_<_Tp>::Scalar_(_Tp v0, _Tp v1, _Tp v2, _Tp v3)
1941 {
1942     this->val[0] = v0;
1943     this->val[1] = v1;
1944     this->val[2] = v2;
1945     this->val[3] = v3;
1946 }
1947 
1948 template<typename _Tp> template<typename _Tp2, int cn> inline
Scalar_(const Vec<_Tp2,cn> & v)1949 Scalar_<_Tp>::Scalar_(const Vec<_Tp2, cn>& v)
1950 {
1951     int i;
1952     for( i = 0; i < (cn < 4 ? cn : 4); i++ )
1953         this->val[i] = cv::saturate_cast<_Tp>(v.val[i]);
1954     for( ; i < 4; i++ )
1955         this->val[i] = 0;
1956 }
1957 
1958 template<typename _Tp> inline
Scalar_(_Tp v0)1959 Scalar_<_Tp>::Scalar_(_Tp v0)
1960 {
1961     this->val[0] = v0;
1962     this->val[1] = this->val[2] = this->val[3] = 0;
1963 }
1964 
1965 template<typename _Tp> inline
all(_Tp v0)1966 Scalar_<_Tp> Scalar_<_Tp>::all(_Tp v0)
1967 {
1968     return Scalar_<_Tp>(v0, v0, v0, v0);
1969 }
1970 
1971 
1972 template<typename _Tp> inline
mul(const Scalar_<_Tp> & a,double scale) const1973 Scalar_<_Tp> Scalar_<_Tp>::mul(const Scalar_<_Tp>& a, double scale ) const
1974 {
1975     return Scalar_<_Tp>(saturate_cast<_Tp>(this->val[0] * a.val[0] * scale),
1976                         saturate_cast<_Tp>(this->val[1] * a.val[1] * scale),
1977                         saturate_cast<_Tp>(this->val[2] * a.val[2] * scale),
1978                         saturate_cast<_Tp>(this->val[3] * a.val[3] * scale));
1979 }
1980 
1981 template<typename _Tp> inline
conj() const1982 Scalar_<_Tp> Scalar_<_Tp>::conj() const
1983 {
1984     return Scalar_<_Tp>(saturate_cast<_Tp>( this->val[0]),
1985                         saturate_cast<_Tp>(-this->val[1]),
1986                         saturate_cast<_Tp>(-this->val[2]),
1987                         saturate_cast<_Tp>(-this->val[3]));
1988 }
1989 
1990 template<typename _Tp> inline
isReal() const1991 bool Scalar_<_Tp>::isReal() const
1992 {
1993     return this->val[1] == 0 && this->val[2] == 0 && this->val[3] == 0;
1994 }
1995 
1996 
1997 template<typename _Tp> template<typename T2> inline
operator Scalar_<T2>() const1998 Scalar_<_Tp>::operator Scalar_<T2>() const
1999 {
2000     return Scalar_<T2>(saturate_cast<T2>(this->val[0]),
2001                        saturate_cast<T2>(this->val[1]),
2002                        saturate_cast<T2>(this->val[2]),
2003                        saturate_cast<T2>(this->val[3]));
2004 }
2005 
2006 
2007 template<typename _Tp> static inline
operator +=(Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2008 Scalar_<_Tp>& operator += (Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2009 {
2010     a.val[0] += b.val[0];
2011     a.val[1] += b.val[1];
2012     a.val[2] += b.val[2];
2013     a.val[3] += b.val[3];
2014     return a;
2015 }
2016 
2017 template<typename _Tp> static inline
operator -=(Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2018 Scalar_<_Tp>& operator -= (Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2019 {
2020     a.val[0] -= b.val[0];
2021     a.val[1] -= b.val[1];
2022     a.val[2] -= b.val[2];
2023     a.val[3] -= b.val[3];
2024     return a;
2025 }
2026 
2027 template<typename _Tp> static inline
operator *=(Scalar_<_Tp> & a,_Tp v)2028 Scalar_<_Tp>& operator *= ( Scalar_<_Tp>& a, _Tp v )
2029 {
2030     a.val[0] *= v;
2031     a.val[1] *= v;
2032     a.val[2] *= v;
2033     a.val[3] *= v;
2034     return a;
2035 }
2036 
2037 template<typename _Tp> static inline
operator ==(const Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2038 bool operator == ( const Scalar_<_Tp>& a, const Scalar_<_Tp>& b )
2039 {
2040     return a.val[0] == b.val[0] && a.val[1] == b.val[1] &&
2041            a.val[2] == b.val[2] && a.val[3] == b.val[3];
2042 }
2043 
2044 template<typename _Tp> static inline
operator !=(const Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2045 bool operator != ( const Scalar_<_Tp>& a, const Scalar_<_Tp>& b )
2046 {
2047     return a.val[0] != b.val[0] || a.val[1] != b.val[1] ||
2048            a.val[2] != b.val[2] || a.val[3] != b.val[3];
2049 }
2050 
2051 template<typename _Tp> static inline
operator +(const Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2052 Scalar_<_Tp> operator + (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2053 {
2054     return Scalar_<_Tp>(a.val[0] + b.val[0],
2055                         a.val[1] + b.val[1],
2056                         a.val[2] + b.val[2],
2057                         a.val[3] + b.val[3]);
2058 }
2059 
2060 template<typename _Tp> static inline
operator -(const Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2061 Scalar_<_Tp> operator - (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2062 {
2063     return Scalar_<_Tp>(saturate_cast<_Tp>(a.val[0] - b.val[0]),
2064                         saturate_cast<_Tp>(a.val[1] - b.val[1]),
2065                         saturate_cast<_Tp>(a.val[2] - b.val[2]),
2066                         saturate_cast<_Tp>(a.val[3] - b.val[3]));
2067 }
2068 
2069 template<typename _Tp> static inline
operator *(const Scalar_<_Tp> & a,_Tp alpha)2070 Scalar_<_Tp> operator * (const Scalar_<_Tp>& a, _Tp alpha)
2071 {
2072     return Scalar_<_Tp>(a.val[0] * alpha,
2073                         a.val[1] * alpha,
2074                         a.val[2] * alpha,
2075                         a.val[3] * alpha);
2076 }
2077 
2078 template<typename _Tp> static inline
operator *(_Tp alpha,const Scalar_<_Tp> & a)2079 Scalar_<_Tp> operator * (_Tp alpha, const Scalar_<_Tp>& a)
2080 {
2081     return a*alpha;
2082 }
2083 
2084 template<typename _Tp> static inline
operator -(const Scalar_<_Tp> & a)2085 Scalar_<_Tp> operator - (const Scalar_<_Tp>& a)
2086 {
2087     return Scalar_<_Tp>(saturate_cast<_Tp>(-a.val[0]),
2088                         saturate_cast<_Tp>(-a.val[1]),
2089                         saturate_cast<_Tp>(-a.val[2]),
2090                         saturate_cast<_Tp>(-a.val[3]));
2091 }
2092 
2093 
2094 template<typename _Tp> static inline
operator *(const Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2095 Scalar_<_Tp> operator * (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2096 {
2097     return Scalar_<_Tp>(saturate_cast<_Tp>(a[0]*b[0] - a[1]*b[1] - a[2]*b[2] - a[3]*b[3]),
2098                         saturate_cast<_Tp>(a[0]*b[1] + a[1]*b[0] + a[2]*b[3] - a[3]*b[2]),
2099                         saturate_cast<_Tp>(a[0]*b[2] - a[1]*b[3] + a[2]*b[0] + a[3]*b[1]),
2100                         saturate_cast<_Tp>(a[0]*b[3] + a[1]*b[2] - a[2]*b[1] + a[3]*b[0]));
2101 }
2102 
2103 template<typename _Tp> static inline
operator *=(Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2104 Scalar_<_Tp>& operator *= (Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2105 {
2106     a = a * b;
2107     return a;
2108 }
2109 
2110 template<typename _Tp> static inline
operator /(const Scalar_<_Tp> & a,_Tp alpha)2111 Scalar_<_Tp> operator / (const Scalar_<_Tp>& a, _Tp alpha)
2112 {
2113     return Scalar_<_Tp>(a.val[0] / alpha,
2114                         a.val[1] / alpha,
2115                         a.val[2] / alpha,
2116                         a.val[3] / alpha);
2117 }
2118 
2119 template<typename _Tp> static inline
operator /(const Scalar_<float> & a,float alpha)2120 Scalar_<float> operator / (const Scalar_<float>& a, float alpha)
2121 {
2122     float s = 1 / alpha;
2123     return Scalar_<float>(a.val[0] * s, a.val[1] * s, a.val[2] * s, a.val[3] * s);
2124 }
2125 
2126 template<typename _Tp> static inline
operator /(const Scalar_<double> & a,double alpha)2127 Scalar_<double> operator / (const Scalar_<double>& a, double alpha)
2128 {
2129     double s = 1 / alpha;
2130     return Scalar_<double>(a.val[0] * s, a.val[1] * s, a.val[2] * s, a.val[3] * s);
2131 }
2132 
2133 template<typename _Tp> static inline
operator /=(Scalar_<_Tp> & a,_Tp alpha)2134 Scalar_<_Tp>& operator /= (Scalar_<_Tp>& a, _Tp alpha)
2135 {
2136     a = a / alpha;
2137     return a;
2138 }
2139 
2140 template<typename _Tp> static inline
operator /(_Tp a,const Scalar_<_Tp> & b)2141 Scalar_<_Tp> operator / (_Tp a, const Scalar_<_Tp>& b)
2142 {
2143     _Tp s = a / (b[0]*b[0] + b[1]*b[1] + b[2]*b[2] + b[3]*b[3]);
2144     return b.conj() * s;
2145 }
2146 
2147 template<typename _Tp> static inline
operator /(const Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2148 Scalar_<_Tp> operator / (const Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2149 {
2150     return a * ((_Tp)1 / b);
2151 }
2152 
2153 template<typename _Tp> static inline
operator /=(Scalar_<_Tp> & a,const Scalar_<_Tp> & b)2154 Scalar_<_Tp>& operator /= (Scalar_<_Tp>& a, const Scalar_<_Tp>& b)
2155 {
2156     a = a / b;
2157     return a;
2158 }
2159 
2160 template<typename _Tp> static inline
operator *(const Matx<_Tp,4,4> & a,const Scalar & b)2161 Scalar operator * (const Matx<_Tp, 4, 4>& a, const Scalar& b)
2162 {
2163     Matx<double, 4, 1> c((Matx<double, 4, 4>)a, b, Matx_MatMulOp());
2164     return reinterpret_cast<const Scalar&>(c);
2165 }
2166 
2167 template<> inline
operator *(const Matx<double,4,4> & a,const Scalar & b)2168 Scalar operator * (const Matx<double, 4, 4>& a, const Scalar& b)
2169 {
2170     Matx<double, 4, 1> c(a, b, Matx_MatMulOp());
2171     return reinterpret_cast<const Scalar&>(c);
2172 }
2173 
2174 
2175 
2176 //////////////////////////////// KeyPoint ///////////////////////////////
2177 
2178 inline
KeyPoint()2179 KeyPoint::KeyPoint()
2180     : pt(0,0), size(0), angle(-1), response(0), octave(0), class_id(-1) {}
2181 
2182 inline
KeyPoint(Point2f _pt,float _size,float _angle,float _response,int _octave,int _class_id)2183 KeyPoint::KeyPoint(Point2f _pt, float _size, float _angle, float _response, int _octave, int _class_id)
2184     : pt(_pt), size(_size), angle(_angle), response(_response), octave(_octave), class_id(_class_id) {}
2185 
2186 inline
KeyPoint(float x,float y,float _size,float _angle,float _response,int _octave,int _class_id)2187 KeyPoint::KeyPoint(float x, float y, float _size, float _angle, float _response, int _octave, int _class_id)
2188     : pt(x, y), size(_size), angle(_angle), response(_response), octave(_octave), class_id(_class_id) {}
2189 
2190 
2191 
2192 ///////////////////////////////// DMatch ////////////////////////////////
2193 
2194 inline
DMatch()2195 DMatch::DMatch()
2196     : queryIdx(-1), trainIdx(-1), imgIdx(-1), distance(FLT_MAX) {}
2197 
2198 inline
DMatch(int _queryIdx,int _trainIdx,float _distance)2199 DMatch::DMatch(int _queryIdx, int _trainIdx, float _distance)
2200     : queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(-1), distance(_distance) {}
2201 
2202 inline
DMatch(int _queryIdx,int _trainIdx,int _imgIdx,float _distance)2203 DMatch::DMatch(int _queryIdx, int _trainIdx, int _imgIdx, float _distance)
2204     : queryIdx(_queryIdx), trainIdx(_trainIdx), imgIdx(_imgIdx), distance(_distance) {}
2205 
2206 inline
operator <(const DMatch & m) const2207 bool DMatch::operator < (const DMatch &m) const
2208 {
2209     return distance < m.distance;
2210 }
2211 
2212 
2213 
2214 ////////////////////////////// TermCriteria /////////////////////////////
2215 
2216 inline
TermCriteria()2217 TermCriteria::TermCriteria()
2218     : type(0), maxCount(0), epsilon(0) {}
2219 
2220 inline
TermCriteria(int _type,int _maxCount,double _epsilon)2221 TermCriteria::TermCriteria(int _type, int _maxCount, double _epsilon)
2222     : type(_type), maxCount(_maxCount), epsilon(_epsilon) {}
2223 
2224 //! @endcond
2225 
2226 } // cv
2227 
2228 #endif //__OPENCV_CORE_TYPES_HPP__
2229