1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*M///////////////////////////////////////////////////////////////////////////////////////
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  By downloading, copying, installing or using the software you agree to this license.
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  If you do not agree to this license, do not download, install,
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  copy or use the software.
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//                           License Agreement
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//                For Open Source Computer Vision Library
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved.
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Third party copyrights are property of their respective owners.
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// @Authors
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//      Nghia Ho, nghiaho12@yahoo.com
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Redistribution and use in source and binary forms, with or without modification,
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// are permitted provided that the following conditions are met:
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * Redistribution's of source code must retain the above copyright notice,
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     this list of conditions and the following disclaimer.
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * Redistribution's in binary form must reproduce the above copyright notice,
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     this list of conditions and the following disclaimer in the documentation
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     and/or other materials provided with the distribution.
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * The name of OpenCV Foundation may not be used to endorse or promote products
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     derived from this software without specific prior written permission.
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// This software is provided by the copyright holders and contributors "as is" and
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// any express or implied warranties, including, but not limited to, the implied
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// warranties of merchantability and fitness for a particular purpose are disclaimed.
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// In no event shall the OpenCV Foundation or contributors be liable for any direct,
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// indirect, incidental, special, exemplary, or consequential damages
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// (including, but not limited to, procurement of substitute goods or services;
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// loss of use, data, or profits; or business interruption) however caused
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// and on any theory of liability, whether in contract, strict liability,
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// or tort (including negligence or otherwise) arising in any way out of
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// the use of this software, even if advised of the possibility of such damage.
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//M*/
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "precomp.hpp"
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslernamespace cv
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerint rotatedRectangleIntersection( const RotatedRect& rect1, const RotatedRect& rect2, OutputArray intersectingRegion )
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const float samePointEps = 0.00001f; // used to test if two points are the same
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Point2f vec1[4], vec2[4];
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Point2f pts1[4], pts2[4];
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    std::vector <Point2f> intersection;
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    rect1.points(pts1);
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    rect2.points(pts2);
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int ret = INTERSECT_FULL;
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // Specical case of rect1 == rect2
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        bool same = true;
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        for( int i = 0; i < 4; i++ )
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            if( fabs(pts1[i].x - pts2[i].x) > samePointEps || (fabs(pts1[i].y - pts2[i].y) > samePointEps) )
71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                same = false;
73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                break;
74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        if(same)
78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            intersection.resize(4);
80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            for( int i = 0; i < 4; i++ )
82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                intersection[i] = pts1[i];
84793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
85793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
86793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            Mat(intersection).copyTo(intersectingRegion);
87793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
88793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            return INTERSECT_FULL;
89793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
90793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
91793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
92793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // Line vector
93793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // A line from p1 to p2 is: p1 + (p2-p1)*t, t=[0,1]
94793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for( int i = 0; i < 4; i++ )
95793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
96793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        vec1[i].x = pts1[(i+1)%4].x - pts1[i].x;
97793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        vec1[i].y = pts1[(i+1)%4].y - pts1[i].y;
98793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
99793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        vec2[i].x = pts2[(i+1)%4].x - pts2[i].x;
100793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        vec2[i].y = pts2[(i+1)%4].y - pts2[i].y;
101793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
102793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
103793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // Line test - test all line combos for intersection
104793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for( int i = 0; i < 4; i++ )
105793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
106793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        for( int j = 0; j < 4; j++ )
107793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
108793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            // Solve for 2x2 Ax=b
109793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float x21 = pts2[j].x - pts1[i].x;
110793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float y21 = pts2[j].y - pts1[i].y;
111793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
112793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float vx1 = vec1[i].x;
113793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float vy1 = vec1[i].y;
114793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
115793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float vx2 = vec2[j].x;
116793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float vy2 = vec2[j].y;
117793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
118793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float det = vx2*vy1 - vx1*vy2;
119793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
120793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float t1 = (vx2*y21 - vy2*x21) / det;
121793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float t2 = (vx1*y21 - vy1*x21) / det;
122793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
123793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            // This takes care of parallel lines
124793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            if( cvIsInf(t1) || cvIsInf(t2) || cvIsNaN(t1) || cvIsNaN(t2) )
125793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
126793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                continue;
127793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
128793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
129793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            if( t1 >= 0.0f && t1 <= 1.0f && t2 >= 0.0f && t2 <= 1.0f )
130793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
131793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                float xi = pts1[i].x + vec1[i].x*t1;
132793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                float yi = pts1[i].y + vec1[i].y*t1;
133793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
134793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                intersection.push_back(Point2f(xi,yi));
135793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
136793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
137793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
138793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
139793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if( !intersection.empty() )
140793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
141793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        ret = INTERSECT_PARTIAL;
142793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
143793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
144793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // Check for vertices from rect1 inside recct2
145793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for( int i = 0; i < 4; i++ )
146793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
147793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        // We do a sign test to see which side the point lies.
148793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        // If the point all lie on the same sign for all 4 sides of the rect,
149793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        // then there's an intersection
150793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        int posSign = 0;
151793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        int negSign = 0;
152793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
153793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        float x = pts1[i].x;
154793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        float y = pts1[i].y;
155793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
156793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        for( int j = 0; j < 4; j++ )
157793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
158793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            // line equation: Ax + By + C = 0
159793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            // see which side of the line this point is at
160793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float A = -vec2[j].y;
161793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float B = vec2[j].x;
162793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float C = -(A*pts2[j].x + B*pts2[j].y);
163793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
164793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float s = A*x+ B*y+ C;
165793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
166793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            if( s >= 0 )
167793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
168793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                posSign++;
169793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
170793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            else
171793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
172793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                negSign++;
173793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
174793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
175793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
176793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        if( posSign == 4 || negSign == 4 )
177793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
178793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            intersection.push_back(pts1[i]);
179793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
180793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
181793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
182793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // Reverse the check - check for vertices from rect2 inside recct1
183793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for( int i = 0; i < 4; i++ )
184793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
185793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        // We do a sign test to see which side the point lies.
186793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        // If the point all lie on the same sign for all 4 sides of the rect,
187793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        // then there's an intersection
188793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        int posSign = 0;
189793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        int negSign = 0;
190793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
191793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        float x = pts2[i].x;
192793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        float y = pts2[i].y;
193793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
194793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        for( int j = 0; j < 4; j++ )
195793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
196793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            // line equation: Ax + By + C = 0
197793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            // see which side of the line this point is at
198793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float A = -vec1[j].y;
199793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float B = vec1[j].x;
200793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float C = -(A*pts1[j].x + B*pts1[j].y);
201793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
202793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float s = A*x + B*y + C;
203793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
204793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            if( s >= 0 )
205793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
206793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                posSign++;
207793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
208793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            else
209793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
210793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                negSign++;
211793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
212793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
213793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
214793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        if( posSign == 4 || negSign == 4 )
215793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
216793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            intersection.push_back(pts2[i]);
217793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
218793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
219793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
220793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // Get rid of dupes
221793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    for( int i = 0; i < (int)intersection.size()-1; i++ )
222793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
223793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        for( size_t j = i+1; j < intersection.size(); j++ )
224793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        {
225793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float dx = intersection[i].x - intersection[j].x;
226793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            float dy = intersection[i].y - intersection[j].y;
227793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            double d2 = dx*dx + dy*dy; // can be a really small number, need double here
228793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
229793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            if( d2 < samePointEps*samePointEps )
230793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            {
231793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                // Found a dupe, remove it
232793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                std::swap(intersection[j], intersection.back());
233793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                intersection.pop_back();
234793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler                j--; // restart check
235793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler            }
236793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        }
237793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
238793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
239793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    if( intersection.empty() )
240793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    {
241793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        return INTERSECT_NONE ;
242793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    }
243793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
244793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // If this check fails then it means we're getting dupes, increase samePointEps
245793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    CV_Assert( intersection.size() <= 8 );
246793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
247793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Mat(intersection).copyTo(intersectingRegion);
248793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
249793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    return ret;
250793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
251793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
252793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} // end namespace
253