|
|
|
@ -66,8 +66,27 @@ private: |
|
|
|
|
void test7(); |
|
|
|
|
void test8(); |
|
|
|
|
void test9(); |
|
|
|
|
void test10(); |
|
|
|
|
void test11(); |
|
|
|
|
void test12(); |
|
|
|
|
void test13(); |
|
|
|
|
void test14(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
static void compare(const std::vector<Point2f>& test, const std::vector<Point2f>& target) |
|
|
|
|
{ |
|
|
|
|
ASSERT_EQ(test.size(), target.size()); |
|
|
|
|
ASSERT_TRUE(test.size() < 4 || isContourConvex(test)); |
|
|
|
|
ASSERT_TRUE(target.size() < 4 || isContourConvex(target)); |
|
|
|
|
for( size_t i = 0; i < test.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = test[i].x - target[i].x; |
|
|
|
|
double dy = test[i].y - target[i].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
ASSERT_LT(r, ACCURACY); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::run(int) |
|
|
|
|
{ |
|
|
|
|
// See pics/intersection.png for the scenarios we are testing
|
|
|
|
@ -92,28 +111,20 @@ void CV_RotatedRectangleIntersectionTest::run(int) |
|
|
|
|
test7(); |
|
|
|
|
test8(); |
|
|
|
|
test9(); |
|
|
|
|
test10(); |
|
|
|
|
test11(); |
|
|
|
|
test12(); |
|
|
|
|
test13(); |
|
|
|
|
test14(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test1() |
|
|
|
|
{ |
|
|
|
|
// no intersection
|
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 12.0f; |
|
|
|
|
|
|
|
|
|
rect2.center.x = 10; |
|
|
|
|
rect2.center.y = 10; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 2; |
|
|
|
|
rect2.angle = 34.0f; |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 12.0f); |
|
|
|
|
RotatedRect rect2(Point2f(10, 10), Size2f(2, 2), 34.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_NONE); |
|
|
|
@ -123,375 +134,243 @@ void CV_RotatedRectangleIntersectionTest::test1() |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test2() |
|
|
|
|
{ |
|
|
|
|
// partial intersection, rectangles translated
|
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
|
|
|
|
|
rect2.center.x = 1; |
|
|
|
|
rect2.center.y = 1; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 2; |
|
|
|
|
rect2.angle = 0; |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(1, 1), Size2f(2, 2), 0.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
CV_Assert(vertices.size() == 4); |
|
|
|
|
|
|
|
|
|
vector<Point2f> possibleVertices(4); |
|
|
|
|
|
|
|
|
|
possibleVertices[0] = Point2f(0.0f, 0.0f); |
|
|
|
|
possibleVertices[1] = Point2f(1.0f, 1.0f); |
|
|
|
|
possibleVertices[2] = Point2f(0.0f, 1.0f); |
|
|
|
|
possibleVertices[3] = Point2f(1.0f, 0.0f); |
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double bestR = DBL_MAX; |
|
|
|
|
|
|
|
|
|
for( size_t j = 0; j < possibleVertices.size(); j++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = vertices[i].x - possibleVertices[j].x; |
|
|
|
|
double dy = vertices[i].y - possibleVertices[j].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
|
|
|
|
|
bestR = std::min(bestR, r); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CV_Assert(bestR < ACCURACY); |
|
|
|
|
} |
|
|
|
|
vector<Point2f> targetVertices(4); |
|
|
|
|
targetVertices[0] = Point2f(1.0f, 0.0f); |
|
|
|
|
targetVertices[1] = Point2f(1.0f, 1.0f); |
|
|
|
|
targetVertices[2] = Point2f(0.0f, 1.0f); |
|
|
|
|
targetVertices[3] = Point2f(0.0f, 0.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test3() |
|
|
|
|
{ |
|
|
|
|
// partial intersection, rectangles rotated 45 degree on the corner, forms a triangle intersection
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
|
|
|
|
|
rect2.center.x = 1; |
|
|
|
|
rect2.center.y = 1; |
|
|
|
|
rect2.size.width = sqrt(2.0f); |
|
|
|
|
rect2.size.height = 20; |
|
|
|
|
rect2.angle = 45.0f; |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(1, 1), Size2f(sqrt(2.0f), 20), 45.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
CV_Assert(vertices.size() == 3); |
|
|
|
|
|
|
|
|
|
vector<Point2f> possibleVertices(3); |
|
|
|
|
|
|
|
|
|
possibleVertices[0] = Point2f(1.0f, 1.0f); |
|
|
|
|
possibleVertices[1] = Point2f(0.0f, 1.0f); |
|
|
|
|
possibleVertices[2] = Point2f(1.0f, 0.0f); |
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double bestR = DBL_MAX; |
|
|
|
|
|
|
|
|
|
for( size_t j = 0; j < possibleVertices.size(); j++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = vertices[i].x - possibleVertices[j].x; |
|
|
|
|
double dy = vertices[i].y - possibleVertices[j].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
|
|
|
|
|
bestR = std::min(bestR, r); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CV_Assert(bestR < ACCURACY); |
|
|
|
|
} |
|
|
|
|
vector<Point2f> targetVertices(3); |
|
|
|
|
targetVertices[0] = Point2f(1.0f, 0.0f); |
|
|
|
|
targetVertices[1] = Point2f(1.0f, 1.0f); |
|
|
|
|
targetVertices[2] = Point2f(0.0f, 1.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test4() |
|
|
|
|
{ |
|
|
|
|
// full intersection, rectangles of same size directly on top of each other
|
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
|
|
|
|
|
rect2.center.x = 0; |
|
|
|
|
rect2.center.y = 0; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 2; |
|
|
|
|
rect2.angle = 0; |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_FULL); |
|
|
|
|
CV_Assert(vertices.size() == 4); |
|
|
|
|
|
|
|
|
|
vector<Point2f> possibleVertices(4); |
|
|
|
|
|
|
|
|
|
possibleVertices[0] = Point2f(-1.0f, 1.0f); |
|
|
|
|
possibleVertices[1] = Point2f(1.0f, -1.0f); |
|
|
|
|
possibleVertices[2] = Point2f(-1.0f, -1.0f); |
|
|
|
|
possibleVertices[3] = Point2f(1.0f, 1.0f); |
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double bestR = DBL_MAX; |
|
|
|
|
|
|
|
|
|
for( size_t j = 0; j < possibleVertices.size(); j++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = vertices[i].x - possibleVertices[j].x; |
|
|
|
|
double dy = vertices[i].y - possibleVertices[j].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
|
|
|
|
|
bestR = std::min(bestR, r); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CV_Assert(bestR < ACCURACY); |
|
|
|
|
} |
|
|
|
|
vector<Point2f> targetVertices(4); |
|
|
|
|
targetVertices[0] = Point2f(-1.0f, 1.0f); |
|
|
|
|
targetVertices[1] = Point2f(-1.0f, -1.0f); |
|
|
|
|
targetVertices[2] = Point2f(1.0f, -1.0f); |
|
|
|
|
targetVertices[3] = Point2f(1.0f, 1.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test5() |
|
|
|
|
{ |
|
|
|
|
// partial intersection, rectangle on top rotated 45 degrees
|
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
|
|
|
|
|
rect2.center.x = 0; |
|
|
|
|
rect2.center.y = 0; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 2; |
|
|
|
|
rect2.angle = 45.0f; |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 45.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
CV_Assert(vertices.size() == 8); |
|
|
|
|
|
|
|
|
|
vector<Point2f> possibleVertices(8); |
|
|
|
|
|
|
|
|
|
possibleVertices[0] = Point2f(-1.0f, -0.414214f); |
|
|
|
|
possibleVertices[1] = Point2f(-1.0f, 0.414214f); |
|
|
|
|
possibleVertices[2] = Point2f(-0.414214f, -1.0f); |
|
|
|
|
possibleVertices[3] = Point2f(0.414214f, -1.0f); |
|
|
|
|
possibleVertices[4] = Point2f(1.0f, -0.414214f); |
|
|
|
|
possibleVertices[5] = Point2f(1.0f, 0.414214f); |
|
|
|
|
possibleVertices[6] = Point2f(0.414214f, 1.0f); |
|
|
|
|
possibleVertices[7] = Point2f(-0.414214f, 1.0f); |
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double bestR = DBL_MAX; |
|
|
|
|
|
|
|
|
|
for( size_t j = 0; j < possibleVertices.size(); j++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = vertices[i].x - possibleVertices[j].x; |
|
|
|
|
double dy = vertices[i].y - possibleVertices[j].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
|
|
|
|
|
bestR = std::min(bestR, r); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CV_Assert(bestR < ACCURACY); |
|
|
|
|
} |
|
|
|
|
vector<Point2f> targetVertices(8); |
|
|
|
|
targetVertices[0] = Point2f(-1.0f, -0.414214f); |
|
|
|
|
targetVertices[1] = Point2f(-0.414214f, -1.0f); |
|
|
|
|
targetVertices[2] = Point2f(0.414214f, -1.0f); |
|
|
|
|
targetVertices[3] = Point2f(1.0f, -0.414214f); |
|
|
|
|
targetVertices[4] = Point2f(1.0f, 0.414214f); |
|
|
|
|
targetVertices[5] = Point2f(0.414214f, 1.0f); |
|
|
|
|
targetVertices[6] = Point2f(-0.414214f, 1.0f); |
|
|
|
|
targetVertices[7] = Point2f(-1.0f, 0.414214f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test6() |
|
|
|
|
{ |
|
|
|
|
// 6 - partial intersection, rectangle on top of different size
|
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
|
|
|
|
|
rect2.center.x = 0; |
|
|
|
|
rect2.center.y = 0; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 10; |
|
|
|
|
rect2.angle = 0; |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 10), 0.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
CV_Assert(vertices.size() == 4); |
|
|
|
|
|
|
|
|
|
vector<Point2f> possibleVertices(4); |
|
|
|
|
|
|
|
|
|
possibleVertices[0] = Point2f(1.0f, 1.0f); |
|
|
|
|
possibleVertices[1] = Point2f(1.0f, -1.0f); |
|
|
|
|
possibleVertices[2] = Point2f(-1.0f, -1.0f); |
|
|
|
|
possibleVertices[3] = Point2f(-1.0f, 1.0f); |
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double bestR = DBL_MAX; |
|
|
|
|
|
|
|
|
|
for( size_t j = 0; j < possibleVertices.size(); j++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = vertices[i].x - possibleVertices[j].x; |
|
|
|
|
double dy = vertices[i].y - possibleVertices[j].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
|
|
|
|
|
bestR = std::min(bestR, r); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CV_Assert(bestR < ACCURACY); |
|
|
|
|
} |
|
|
|
|
vector<Point2f> targetVertices(4); |
|
|
|
|
targetVertices[0] = Point2f(-1.0f, -1.0f); |
|
|
|
|
targetVertices[1] = Point2f(1.0f, -1.0f); |
|
|
|
|
targetVertices[2] = Point2f(1.0f, 1.0f); |
|
|
|
|
targetVertices[3] = Point2f(-1.0f, 1.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test7() |
|
|
|
|
{ |
|
|
|
|
// full intersection, rectangle fully enclosed in the other
|
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 12.34f; |
|
|
|
|
rect1.size.height = 56.78f; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
|
|
|
|
|
rect2.center.x = 0; |
|
|
|
|
rect2.center.y = 0; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 2; |
|
|
|
|
rect2.angle = 0; |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(12.34f, 56.78f), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_FULL); |
|
|
|
|
CV_Assert(vertices.size() == 4); |
|
|
|
|
|
|
|
|
|
vector<Point2f> possibleVertices(4); |
|
|
|
|
|
|
|
|
|
possibleVertices[0] = Point2f(1.0f, 1.0f); |
|
|
|
|
possibleVertices[1] = Point2f(1.0f, -1.0f); |
|
|
|
|
possibleVertices[2] = Point2f(-1.0f, -1.0f); |
|
|
|
|
possibleVertices[3] = Point2f(-1.0f, 1.0f); |
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double bestR = DBL_MAX; |
|
|
|
|
vector<Point2f> targetVertices(4); |
|
|
|
|
targetVertices[0] = Point2f(-1.0f, 1.0f); |
|
|
|
|
targetVertices[1] = Point2f(-1.0f, -1.0f); |
|
|
|
|
targetVertices[2] = Point2f(1.0f, -1.0f); |
|
|
|
|
targetVertices[3] = Point2f(1.0f, 1.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
for( size_t j = 0; j < possibleVertices.size(); j++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = vertices[i].x - possibleVertices[j].x; |
|
|
|
|
double dy = vertices[i].y - possibleVertices[j].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test8() |
|
|
|
|
{ |
|
|
|
|
// intersection by a single vertex
|
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(2, 2), Size2f(2, 2), 0.0f); |
|
|
|
|
|
|
|
|
|
bestR = std::min(bestR, r); |
|
|
|
|
} |
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(bestR < ACCURACY); |
|
|
|
|
} |
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
compare(vertices, vector<Point2f>(1, Point2f(1.0f, 1.0f))); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test8() |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test9() |
|
|
|
|
{ |
|
|
|
|
// full intersection, rectangle fully enclosed in the other
|
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(2, 0), Size2f(2, 123.45f), 0.0f); |
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
vector<Point2f> targetVertices(2); |
|
|
|
|
targetVertices[0] = Point2f(1.0f, -1.0f); |
|
|
|
|
targetVertices[1] = Point2f(1.0f, 1.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
rect2.center.x = 2; |
|
|
|
|
rect2.center.y = 2; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 2; |
|
|
|
|
rect2.angle = 0; |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test10() |
|
|
|
|
{ |
|
|
|
|
// three points of rect2 are inside rect1.
|
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 0.5), Size2f(1, 1), 45.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
|
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
CV_Assert(vertices.size() == 1); |
|
|
|
|
|
|
|
|
|
double dx = vertices[0].x - 1; |
|
|
|
|
double dy = vertices[0].y - 1; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
|
|
|
|
|
CV_Assert(r < ACCURACY); |
|
|
|
|
vector<Point2f> targetVertices(5); |
|
|
|
|
targetVertices[0] = Point2f(0.207107f, 1.0f); |
|
|
|
|
targetVertices[1] = Point2f(-0.207107f, 1.0f); |
|
|
|
|
targetVertices[2] = Point2f(-0.707107f, 0.5f); |
|
|
|
|
targetVertices[3] = Point2f(0.0f, -0.207107f); |
|
|
|
|
targetVertices[4] = Point2f(0.707107f, 0.5f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test9() |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test11() |
|
|
|
|
{ |
|
|
|
|
// full intersection, rectangle fully enclosed in the other
|
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(4, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), -45.0f); |
|
|
|
|
|
|
|
|
|
RotatedRect rect1, rect2; |
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
rect1.center.x = 0; |
|
|
|
|
rect1.center.y = 0; |
|
|
|
|
rect1.size.width = 2; |
|
|
|
|
rect1.size.height = 2; |
|
|
|
|
rect1.angle = 0; |
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
|
|
|
|
|
rect2.center.x = 2; |
|
|
|
|
rect2.center.y = 0; |
|
|
|
|
rect2.size.width = 2; |
|
|
|
|
rect2.size.height = 123.45f; |
|
|
|
|
rect2.angle = 0; |
|
|
|
|
vector<Point2f> targetVertices(6); |
|
|
|
|
targetVertices[0] = Point2f(-0.414214f, -1.0f); |
|
|
|
|
targetVertices[1] = Point2f(0.414213f, -1.0f); |
|
|
|
|
targetVertices[2] = Point2f(1.41421f, 0.0f); |
|
|
|
|
targetVertices[3] = Point2f(0.414214f, 1.0f); |
|
|
|
|
targetVertices[4] = Point2f(-0.414213f, 1.0f); |
|
|
|
|
targetVertices[5] = Point2f(-1.41421f, 0.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test12() |
|
|
|
|
{ |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 1), Size2f(1, 1), 0.0f); |
|
|
|
|
|
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
CV_Assert(vertices.size() == 2); |
|
|
|
|
|
|
|
|
|
vector<Point2f> possibleVertices(2); |
|
|
|
|
vector<Point2f> targetVertices(4); |
|
|
|
|
targetVertices[0] = Point2f(-0.5f, 1.0f); |
|
|
|
|
targetVertices[1] = Point2f(-0.5f, 0.5f); |
|
|
|
|
targetVertices[2] = Point2f(0.5f, 0.5f); |
|
|
|
|
targetVertices[3] = Point2f(0.5f, 1.0f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
possibleVertices[0] = Point2f(1.0f, 1.0f); |
|
|
|
|
possibleVertices[1] = Point2f(1.0f, -1.0f); |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test13() |
|
|
|
|
{ |
|
|
|
|
RotatedRect rect1(Point2f(0, 0), Size2f(1, 3), 0.0f); |
|
|
|
|
RotatedRect rect2(Point2f(0, 1), Size2f(3, 1), 0.0f); |
|
|
|
|
|
|
|
|
|
for( size_t i = 0; i < vertices.size(); i++ ) |
|
|
|
|
{ |
|
|
|
|
double bestR = DBL_MAX; |
|
|
|
|
vector<Point2f> vertices; |
|
|
|
|
int ret = rotatedRectangleIntersection(rect1, rect2, vertices); |
|
|
|
|
|
|
|
|
|
for( size_t j = 0; j < possibleVertices.size(); j++ ) |
|
|
|
|
{ |
|
|
|
|
double dx = vertices[i].x - possibleVertices[j].x; |
|
|
|
|
double dy = vertices[i].y - possibleVertices[j].y; |
|
|
|
|
double r = sqrt(dx*dx + dy*dy); |
|
|
|
|
CV_Assert(ret == INTERSECT_PARTIAL); |
|
|
|
|
|
|
|
|
|
bestR = std::min(bestR, r); |
|
|
|
|
} |
|
|
|
|
vector<Point2f> targetVertices(4); |
|
|
|
|
targetVertices[0] = Point2f(-0.5f, 0.5f); |
|
|
|
|
targetVertices[1] = Point2f(0.5f, 0.5f); |
|
|
|
|
targetVertices[2] = Point2f(0.5f, 1.5f); |
|
|
|
|
targetVertices[3] = Point2f(-0.5f, 1.5f); |
|
|
|
|
compare(vertices, targetVertices); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
CV_Assert(bestR < ACCURACY); |
|
|
|
|
void CV_RotatedRectangleIntersectionTest::test14() |
|
|
|
|
{ |
|
|
|
|
const int kNumTests = 100; |
|
|
|
|
const int kWidth = 5; |
|
|
|
|
const int kHeight = 5; |
|
|
|
|
RotatedRect rects[2]; |
|
|
|
|
std::vector<Point2f> inter; |
|
|
|
|
for (int i = 0; i < kNumTests; ++i) |
|
|
|
|
{ |
|
|
|
|
for (int j = 0; j < 2; ++j) |
|
|
|
|
{ |
|
|
|
|
rects[j].center = Point2f((float)(rand() % kWidth), (float)(rand() % kHeight)); |
|
|
|
|
rects[j].size = Size2f(rand() % kWidth + 1.0f, rand() % kHeight + 1.0f); |
|
|
|
|
rects[j].angle = (float)(rand() % 360); |
|
|
|
|
} |
|
|
|
|
rotatedRectangleIntersection(rects[0], rects[1], inter); |
|
|
|
|
ASSERT_TRUE(inter.size() < 4 || isContourConvex(inter)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|