Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgproc/src/intersection.cpp
16354 views
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) 2008-2011, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// @Authors
18
// Nghia Ho, [email protected]
19
//
20
// Redistribution and use in source and binary forms, with or without modification,
21
// are permitted provided that the following conditions are met:
22
//
23
// * Redistribution's of source code must retain the above copyright notice,
24
// this list of conditions and the following disclaimer.
25
//
26
// * Redistribution's in binary form must reproduce the above copyright notice,
27
// this list of conditions and the following disclaimer in the documentation
28
// and/or other materials provided with the distribution.
29
//
30
// * The name of OpenCV Foundation may not be used to endorse or promote products
31
// derived from this software without specific prior written permission.
32
//
33
// This software is provided by the copyright holders and contributors "as is" and
34
// any express or implied warranties, including, but not limited to, the implied
35
// warranties of merchantability and fitness for a particular purpose are disclaimed.
36
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
37
// indirect, incidental, special, exemplary, or consequential damages
38
// (including, but not limited to, procurement of substitute goods or services;
39
// loss of use, data, or profits; or business interruption) however caused
40
// and on any theory of liability, whether in contract, strict liability,
41
// or tort (including negligence or otherwise) arising in any way out of
42
// the use of this software, even if advised of the possibility of such damage.
43
//
44
//M*/
45
#include "precomp.hpp"
46
47
namespace cv
48
{
49
50
int rotatedRectangleIntersection( const RotatedRect& rect1, const RotatedRect& rect2, OutputArray intersectingRegion )
51
{
52
CV_INSTRUMENT_REGION();
53
54
// L2 metric
55
const float samePointEps = std::max(1e-16f, 1e-6f * (float)std::max(rect1.size.area(), rect2.size.area()));
56
57
Point2f vec1[4], vec2[4];
58
Point2f pts1[4], pts2[4];
59
60
std::vector <Point2f> intersection; intersection.reserve(24);
61
62
rect1.points(pts1);
63
rect2.points(pts2);
64
65
int ret = INTERSECT_FULL;
66
67
// Specical case of rect1 == rect2
68
{
69
bool same = true;
70
71
for( int i = 0; i < 4; i++ )
72
{
73
if( fabs(pts1[i].x - pts2[i].x) > samePointEps || (fabs(pts1[i].y - pts2[i].y) > samePointEps) )
74
{
75
same = false;
76
break;
77
}
78
}
79
80
if(same)
81
{
82
intersection.resize(4);
83
84
for( int i = 0; i < 4; i++ )
85
{
86
intersection[i] = pts1[i];
87
}
88
89
Mat(intersection).copyTo(intersectingRegion);
90
91
return INTERSECT_FULL;
92
}
93
}
94
95
// Line vector
96
// A line from p1 to p2 is: p1 + (p2-p1)*t, t=[0,1]
97
for( int i = 0; i < 4; i++ )
98
{
99
vec1[i].x = pts1[(i+1)%4].x - pts1[i].x;
100
vec1[i].y = pts1[(i+1)%4].y - pts1[i].y;
101
102
vec2[i].x = pts2[(i+1)%4].x - pts2[i].x;
103
vec2[i].y = pts2[(i+1)%4].y - pts2[i].y;
104
}
105
106
// Line test - test all line combos for intersection
107
for( int i = 0; i < 4; i++ )
108
{
109
for( int j = 0; j < 4; j++ )
110
{
111
// Solve for 2x2 Ax=b
112
float x21 = pts2[j].x - pts1[i].x;
113
float y21 = pts2[j].y - pts1[i].y;
114
115
float vx1 = vec1[i].x;
116
float vy1 = vec1[i].y;
117
118
float vx2 = vec2[j].x;
119
float vy2 = vec2[j].y;
120
121
float det = vx2*vy1 - vx1*vy2;
122
123
float t1 = (vx2*y21 - vy2*x21) / det;
124
float t2 = (vx1*y21 - vy1*x21) / det;
125
126
// This takes care of parallel lines
127
if( cvIsInf(t1) || cvIsInf(t2) || cvIsNaN(t1) || cvIsNaN(t2) )
128
{
129
continue;
130
}
131
132
if( t1 >= 0.0f && t1 <= 1.0f && t2 >= 0.0f && t2 <= 1.0f )
133
{
134
float xi = pts1[i].x + vec1[i].x*t1;
135
float yi = pts1[i].y + vec1[i].y*t1;
136
137
intersection.push_back(Point2f(xi,yi));
138
}
139
}
140
}
141
142
if( !intersection.empty() )
143
{
144
ret = INTERSECT_PARTIAL;
145
}
146
147
// Check for vertices from rect1 inside recct2
148
for( int i = 0; i < 4; i++ )
149
{
150
// We do a sign test to see which side the point lies.
151
// If the point all lie on the same sign for all 4 sides of the rect,
152
// then there's an intersection
153
int posSign = 0;
154
int negSign = 0;
155
156
float x = pts1[i].x;
157
float y = pts1[i].y;
158
159
for( int j = 0; j < 4; j++ )
160
{
161
// line equation: Ax + By + C = 0
162
// see which side of the line this point is at
163
float A = -vec2[j].y;
164
float B = vec2[j].x;
165
float C = -(A*pts2[j].x + B*pts2[j].y);
166
167
float s = A*x+ B*y+ C;
168
169
if( s >= 0 )
170
{
171
posSign++;
172
}
173
else
174
{
175
negSign++;
176
}
177
}
178
179
if( posSign == 4 || negSign == 4 )
180
{
181
intersection.push_back(pts1[i]);
182
}
183
}
184
185
// Reverse the check - check for vertices from rect2 inside recct1
186
for( int i = 0; i < 4; i++ )
187
{
188
// We do a sign test to see which side the point lies.
189
// If the point all lie on the same sign for all 4 sides of the rect,
190
// then there's an intersection
191
int posSign = 0;
192
int negSign = 0;
193
194
float x = pts2[i].x;
195
float y = pts2[i].y;
196
197
for( int j = 0; j < 4; j++ )
198
{
199
// line equation: Ax + By + C = 0
200
// see which side of the line this point is at
201
float A = -vec1[j].y;
202
float B = vec1[j].x;
203
float C = -(A*pts1[j].x + B*pts1[j].y);
204
205
float s = A*x + B*y + C;
206
207
if( s >= 0 )
208
{
209
posSign++;
210
}
211
else
212
{
213
negSign++;
214
}
215
}
216
217
if( posSign == 4 || negSign == 4 )
218
{
219
intersection.push_back(pts2[i]);
220
}
221
}
222
223
int N = (int)intersection.size();
224
if (N == 0)
225
{
226
return INTERSECT_NONE;
227
}
228
229
// Get rid of duplicated points
230
int Nstride = N;
231
cv::AutoBuffer<float, 100> distPt(N * N);
232
cv::AutoBuffer<int> ptDistRemap(N);
233
for (int i = 0; i < N; ++i)
234
{
235
const Point2f pt0 = intersection[i];
236
ptDistRemap[i] = i;
237
for (int j = i + 1; j < N; )
238
{
239
const Point2f pt1 = intersection[j];
240
float d2 = normL2Sqr<float>(pt1 - pt0);
241
if(d2 <= samePointEps)
242
{
243
if (j < N - 1)
244
intersection[j] = intersection[N - 1];
245
N--;
246
continue;
247
}
248
distPt[i*Nstride + j] = d2;
249
++j;
250
}
251
}
252
while (N > 8) // we still have duplicate points after samePointEps threshold (eliminate closest points)
253
{
254
int minI = 0;
255
int minJ = 1;
256
float minD = distPt[1];
257
for (int i = 0; i < N - 1; ++i)
258
{
259
float* pDist = distPt.data() + Nstride * ptDistRemap[i];
260
for (int j = i + 1; j < N; ++j)
261
{
262
float d = pDist[ptDistRemap[j]];
263
if (d < minD)
264
{
265
minD = d;
266
minI = i;
267
minJ = j;
268
}
269
}
270
}
271
CV_Assert(fabs(normL2Sqr<float>(intersection[minI] - intersection[minJ]) - minD) < 1e-6); // ptDistRemap is not corrupted
272
// drop minJ point
273
if (minJ < N - 1)
274
{
275
intersection[minJ] = intersection[N - 1];
276
ptDistRemap[minJ] = ptDistRemap[N - 1];
277
}
278
N--;
279
}
280
281
// order points
282
for (int i = 0; i < N - 1; ++i)
283
{
284
Point2f diffI = intersection[i + 1] - intersection[i];
285
for (int j = i + 2; j < N; ++j)
286
{
287
Point2f diffJ = intersection[j] - intersection[i];
288
if (diffI.cross(diffJ) < 0)
289
{
290
std::swap(intersection[i + 1], intersection[j]);
291
diffI = diffJ;
292
}
293
}
294
}
295
296
intersection.resize(N);
297
Mat(intersection).copyTo(intersectingRegion);
298
299
return ret;
300
}
301
302
} // end namespace
303
304