Path: blob/master/modules/imgproc/test/test_intersection.cpp
16354 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2008-2011, Willow Garage Inc., all rights reserved.14// Third party copyrights are property of their respective owners.15//16// @Authors17// Nghia Ho, [email protected]18//19// Redistribution and use in source and binary forms, with or without modification,20// are permitted provided that the following conditions are met:21//22// * Redistribution's of source code must retain the above copyright notice,23// this list of conditions and the following disclaimer.24//25// * Redistribution's in binary form must reproduce the above copyright notice,26// this list of conditions and the following disclaimer in the documentation27// and/or other materials provided with the distribution.28//29// * The name of OpenCV Foundation may not be used to endorse or promote products30// derived from this software without specific prior written permission.31//32// This software is provided by the copyright holders and contributors "as is" and33// any express or implied warranties, including, but not limited to, the implied34// warranties of merchantability and fitness for a particular purpose are disclaimed.35// In no event shall the OpenCV Foundation or contributors be liable for any direct,36// indirect, incidental, special, exemplary, or consequential damages37// (including, but not limited to, procurement of substitute goods or services;38// loss of use, data, or profits; or business interruption) however caused39// and on any theory of liability, whether in contract, strict liability,40// or tort (including negligence or otherwise) arising in any way out of41// the use of this software, even if advised of the possibility of such damage.42//43//M*/4445#include "test_precomp.hpp"4647namespace opencv_test { namespace {4849#define ACCURACY 0.000015051// See pics/intersection.png for the scenarios we are testing5253// Test the following scenarios:54// 1 - no intersection55// 2 - partial intersection, rectangle translated56// 3 - partial intersection, rectangle rotated 45 degree on the corner, forms a triangle intersection57// 4 - full intersection, rectangles of same size directly on top of each other58// 5 - partial intersection, rectangle on top rotated 45 degrees59// 6 - partial intersection, rectangle on top of different size60// 7 - full intersection, rectangle fully enclosed in the other61// 8 - partial intersection, rectangle corner just touching. point contact62// 9 - partial intersetion. rectangle side by side, line contact6364static void compare(const std::vector<Point2f>& test, const std::vector<Point2f>& target)65{66ASSERT_EQ(test.size(), target.size());67ASSERT_TRUE(test.size() < 4 || isContourConvex(test));68ASSERT_TRUE(target.size() < 4 || isContourConvex(target));69for( size_t i = 0; i < test.size(); i++ )70{71double r = sqrt(normL2Sqr<double>(test[i] - target[i]));72ASSERT_LT(r, ACCURACY);73}74}7576TEST(Imgproc_RotatedRectangleIntersection, accuracy_1)77{78// no intersection79RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 12.0f);80RotatedRect rect2(Point2f(10, 10), Size2f(2, 2), 34.0f);8182vector<Point2f> vertices;83int ret = rotatedRectangleIntersection(rect1, rect2, vertices);8485CV_Assert(ret == INTERSECT_NONE);86CV_Assert(vertices.empty());87}8889TEST(Imgproc_RotatedRectangleIntersection, accuracy_2)90{91// partial intersection, rectangles translated92RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);93RotatedRect rect2(Point2f(1, 1), Size2f(2, 2), 0.0f);9495vector<Point2f> vertices;96int ret = rotatedRectangleIntersection(rect1, rect2, vertices);9798CV_Assert(ret == INTERSECT_PARTIAL);99100vector<Point2f> targetVertices(4);101targetVertices[0] = Point2f(1.0f, 0.0f);102targetVertices[1] = Point2f(1.0f, 1.0f);103targetVertices[2] = Point2f(0.0f, 1.0f);104targetVertices[3] = Point2f(0.0f, 0.0f);105compare(vertices, targetVertices);106}107108TEST(Imgproc_RotatedRectangleIntersection, accuracy_3)109{110// partial intersection, rectangles rotated 45 degree on the corner, forms a triangle intersection111RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);112RotatedRect rect2(Point2f(1, 1), Size2f(sqrt(2.0f), 20), 45.0f);113114vector<Point2f> vertices;115int ret = rotatedRectangleIntersection(rect1, rect2, vertices);116117CV_Assert(ret == INTERSECT_PARTIAL);118119vector<Point2f> targetVertices(3);120targetVertices[0] = Point2f(1.0f, 0.0f);121targetVertices[1] = Point2f(1.0f, 1.0f);122targetVertices[2] = Point2f(0.0f, 1.0f);123compare(vertices, targetVertices);124}125126TEST(Imgproc_RotatedRectangleIntersection, accuracy_4)127{128// full intersection, rectangles of same size directly on top of each other129RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);130RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);131132vector<Point2f> vertices;133int ret = rotatedRectangleIntersection(rect1, rect2, vertices);134135CV_Assert(ret == INTERSECT_FULL);136137vector<Point2f> targetVertices(4);138targetVertices[0] = Point2f(-1.0f, 1.0f);139targetVertices[1] = Point2f(-1.0f, -1.0f);140targetVertices[2] = Point2f(1.0f, -1.0f);141targetVertices[3] = Point2f(1.0f, 1.0f);142compare(vertices, targetVertices);143}144145TEST(Imgproc_RotatedRectangleIntersection, accuracy_5)146{147// partial intersection, rectangle on top rotated 45 degrees148RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);149RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 45.0f);150151vector<Point2f> vertices;152int ret = rotatedRectangleIntersection(rect1, rect2, vertices);153154CV_Assert(ret == INTERSECT_PARTIAL);155156vector<Point2f> targetVertices(8);157targetVertices[0] = Point2f(-1.0f, -0.414214f);158targetVertices[1] = Point2f(-0.414214f, -1.0f);159targetVertices[2] = Point2f(0.414214f, -1.0f);160targetVertices[3] = Point2f(1.0f, -0.414214f);161targetVertices[4] = Point2f(1.0f, 0.414214f);162targetVertices[5] = Point2f(0.414214f, 1.0f);163targetVertices[6] = Point2f(-0.414214f, 1.0f);164targetVertices[7] = Point2f(-1.0f, 0.414214f);165compare(vertices, targetVertices);166}167168TEST(Imgproc_RotatedRectangleIntersection, accuracy_6)169{170// 6 - partial intersection, rectangle on top of different size171RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);172RotatedRect rect2(Point2f(0, 0), Size2f(2, 10), 0.0f);173174vector<Point2f> vertices;175int ret = rotatedRectangleIntersection(rect1, rect2, vertices);176177CV_Assert(ret == INTERSECT_PARTIAL);178179vector<Point2f> targetVertices(4);180targetVertices[0] = Point2f(-1.0f, -1.0f);181targetVertices[1] = Point2f(1.0f, -1.0f);182targetVertices[2] = Point2f(1.0f, 1.0f);183targetVertices[3] = Point2f(-1.0f, 1.0f);184compare(vertices, targetVertices);185}186187TEST(Imgproc_RotatedRectangleIntersection, accuracy_7)188{189// full intersection, rectangle fully enclosed in the other190RotatedRect rect1(Point2f(0, 0), Size2f(12.34f, 56.78f), 0.0f);191RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);192193vector<Point2f> vertices;194int ret = rotatedRectangleIntersection(rect1, rect2, vertices);195196CV_Assert(ret == INTERSECT_FULL);197198vector<Point2f> targetVertices(4);199targetVertices[0] = Point2f(-1.0f, 1.0f);200targetVertices[1] = Point2f(-1.0f, -1.0f);201targetVertices[2] = Point2f(1.0f, -1.0f);202targetVertices[3] = Point2f(1.0f, 1.0f);203compare(vertices, targetVertices);204}205206TEST(Imgproc_RotatedRectangleIntersection, accuracy_8)207{208// intersection by a single vertex209RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);210RotatedRect rect2(Point2f(2, 2), Size2f(2, 2), 0.0f);211212vector<Point2f> vertices;213int ret = rotatedRectangleIntersection(rect1, rect2, vertices);214215CV_Assert(ret == INTERSECT_PARTIAL);216compare(vertices, vector<Point2f>(1, Point2f(1.0f, 1.0f)));217}218219TEST(Imgproc_RotatedRectangleIntersection, accuracy_9)220{221// full intersection, rectangle fully enclosed in the other222RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);223RotatedRect rect2(Point2f(2, 0), Size2f(2, 123.45f), 0.0f);224225vector<Point2f> vertices;226int ret = rotatedRectangleIntersection(rect1, rect2, vertices);227228CV_Assert(ret == INTERSECT_PARTIAL);229230vector<Point2f> targetVertices(2);231targetVertices[0] = Point2f(1.0f, -1.0f);232targetVertices[1] = Point2f(1.0f, 1.0f);233compare(vertices, targetVertices);234}235236TEST(Imgproc_RotatedRectangleIntersection, accuracy_10)237{238// three points of rect2 are inside rect1.239RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);240RotatedRect rect2(Point2f(0, 0.5), Size2f(1, 1), 45.0f);241242vector<Point2f> vertices;243int ret = rotatedRectangleIntersection(rect1, rect2, vertices);244245CV_Assert(ret == INTERSECT_PARTIAL);246247vector<Point2f> targetVertices(5);248targetVertices[0] = Point2f(0.207107f, 1.0f);249targetVertices[1] = Point2f(-0.207107f, 1.0f);250targetVertices[2] = Point2f(-0.707107f, 0.5f);251targetVertices[3] = Point2f(0.0f, -0.207107f);252targetVertices[4] = Point2f(0.707107f, 0.5f);253compare(vertices, targetVertices);254}255256TEST(Imgproc_RotatedRectangleIntersection, accuracy_11)257{258RotatedRect rect1(Point2f(0, 0), Size2f(4, 2), 0.0f);259RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), -45.0f);260261vector<Point2f> vertices;262int ret = rotatedRectangleIntersection(rect1, rect2, vertices);263264CV_Assert(ret == INTERSECT_PARTIAL);265266vector<Point2f> targetVertices(6);267targetVertices[0] = Point2f(-0.414214f, -1.0f);268targetVertices[1] = Point2f(0.414213f, -1.0f);269targetVertices[2] = Point2f(1.41421f, 0.0f);270targetVertices[3] = Point2f(0.414214f, 1.0f);271targetVertices[4] = Point2f(-0.414213f, 1.0f);272targetVertices[5] = Point2f(-1.41421f, 0.0f);273compare(vertices, targetVertices);274}275276TEST(Imgproc_RotatedRectangleIntersection, accuracy_12)277{278RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);279RotatedRect rect2(Point2f(0, 1), Size2f(1, 1), 0.0f);280281vector<Point2f> vertices;282int ret = rotatedRectangleIntersection(rect1, rect2, vertices);283284CV_Assert(ret == INTERSECT_PARTIAL);285286vector<Point2f> targetVertices(4);287targetVertices[0] = Point2f(-0.5f, 1.0f);288targetVertices[1] = Point2f(-0.5f, 0.5f);289targetVertices[2] = Point2f(0.5f, 0.5f);290targetVertices[3] = Point2f(0.5f, 1.0f);291compare(vertices, targetVertices);292}293294TEST(Imgproc_RotatedRectangleIntersection, accuracy_13)295{296RotatedRect rect1(Point2f(0, 0), Size2f(1, 3), 0.0f);297RotatedRect rect2(Point2f(0, 1), Size2f(3, 1), 0.0f);298299vector<Point2f> vertices;300int ret = rotatedRectangleIntersection(rect1, rect2, vertices);301302CV_Assert(ret == INTERSECT_PARTIAL);303304vector<Point2f> targetVertices(4);305targetVertices[0] = Point2f(-0.5f, 0.5f);306targetVertices[1] = Point2f(0.5f, 0.5f);307targetVertices[2] = Point2f(0.5f, 1.5f);308targetVertices[3] = Point2f(-0.5f, 1.5f);309compare(vertices, targetVertices);310}311312TEST(Imgproc_RotatedRectangleIntersection, accuracy_14)313{314const int kNumTests = 100;315const float kWidth = 5;316const float kHeight = 5;317RotatedRect rects[2];318std::vector<Point2f> inter;319cv::RNG& rng = cv::theRNG();320for (int i = 0; i < kNumTests; ++i)321{322for (int j = 0; j < 2; ++j)323{324rects[j].center = Point2f(rng.uniform(0.0f, kWidth), rng.uniform(0.0f, kHeight));325rects[j].size = Size2f(rng.uniform(1.0f, kWidth), rng.uniform(1.0f, kHeight));326rects[j].angle = rng.uniform(0.0f, 360.0f);327}328int res = rotatedRectangleIntersection(rects[0], rects[1], inter);329EXPECT_TRUE(res == INTERSECT_NONE || res == INTERSECT_PARTIAL || res == INTERSECT_FULL) << res;330ASSERT_TRUE(inter.size() < 4 || isContourConvex(inter)) << inter;331}332}333334TEST(Imgproc_RotatedRectangleIntersection, regression_12221_1)335{336RotatedRect r1(337Point2f(259.65081787109375, 51.58895492553711),338Size2f(5487.8779296875, 233.8921661376953),339-29.488616943359375);340RotatedRect r2(341Point2f(293.70465087890625, 112.10154724121094),342Size2f(5487.8896484375, 234.87368774414062),343-31.27001953125);344345std::vector<Point2f> intersections;346int interType = cv::rotatedRectangleIntersection(r1, r2, intersections);347EXPECT_EQ(INTERSECT_PARTIAL, interType);348EXPECT_LE(intersections.size(), (size_t)8);349}350351TEST(Imgproc_RotatedRectangleIntersection, regression_12221_2)352{353RotatedRect r1(354Point2f(239.78500366210938, 515.72021484375),355Size2f(70.23420715332031, 39.74684524536133),356-42.86162567138672);357RotatedRect r2(358Point2f(242.4205322265625, 510.1195373535156),359Size2f(66.85948944091797, 61.46455383300781),360-9.840961456298828);361362std::vector<Point2f> intersections;363int interType = cv::rotatedRectangleIntersection(r1, r2, intersections);364EXPECT_EQ(INTERSECT_PARTIAL, interType);365EXPECT_LE(intersections.size(), (size_t)8);366}367368}} // namespace369370371