test_intersection.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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, nghiaho12@yahoo.com
  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 "test_precomp.hpp"
  46. namespace opencv_test { namespace {
  47. #define ACCURACY 0.00001
  48. // See pics/intersection.png for the scenarios we are testing
  49. // Test the following scenarios:
  50. // 1 - no intersection
  51. // 2 - partial intersection, rectangle translated
  52. // 3 - partial intersection, rectangle rotated 45 degree on the corner, forms a triangle intersection
  53. // 4 - full intersection, rectangles of same size directly on top of each other
  54. // 5 - partial intersection, rectangle on top rotated 45 degrees
  55. // 6 - partial intersection, rectangle on top of different size
  56. // 7 - full intersection, rectangle fully enclosed in the other
  57. // 8 - partial intersection, rectangle corner just touching. point contact
  58. // 9 - partial intersection. rectangle side by side, line contact
  59. static void compare(const std::vector<Point2f>& test, const std::vector<Point2f>& target)
  60. {
  61. ASSERT_EQ(test.size(), target.size());
  62. ASSERT_TRUE(test.size() < 4 || isContourConvex(test));
  63. ASSERT_TRUE(target.size() < 4 || isContourConvex(target));
  64. for( size_t i = 0; i < test.size(); i++ )
  65. {
  66. double r = sqrt(normL2Sqr<double>(test[i] - target[i]));
  67. ASSERT_LT(r, ACCURACY);
  68. }
  69. }
  70. TEST(Imgproc_RotatedRectangleIntersection, accuracy_1)
  71. {
  72. // no intersection
  73. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 12.0f);
  74. RotatedRect rect2(Point2f(10, 10), Size2f(2, 2), 34.0f);
  75. vector<Point2f> vertices;
  76. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  77. CV_Assert(ret == INTERSECT_NONE);
  78. CV_Assert(vertices.empty());
  79. }
  80. TEST(Imgproc_RotatedRectangleIntersection, accuracy_2)
  81. {
  82. // partial intersection, rectangles translated
  83. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  84. RotatedRect rect2(Point2f(1, 1), Size2f(2, 2), 0.0f);
  85. vector<Point2f> vertices;
  86. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  87. CV_Assert(ret == INTERSECT_PARTIAL);
  88. vector<Point2f> targetVertices(4);
  89. targetVertices[0] = Point2f(1.0f, 0.0f);
  90. targetVertices[1] = Point2f(1.0f, 1.0f);
  91. targetVertices[2] = Point2f(0.0f, 1.0f);
  92. targetVertices[3] = Point2f(0.0f, 0.0f);
  93. compare(vertices, targetVertices);
  94. }
  95. TEST(Imgproc_RotatedRectangleIntersection, accuracy_3)
  96. {
  97. // partial intersection, rectangles rotated 45 degree on the corner, forms a triangle intersection
  98. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  99. RotatedRect rect2(Point2f(1, 1), Size2f(sqrt(2.0f), 20), 45.0f);
  100. vector<Point2f> vertices;
  101. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  102. CV_Assert(ret == INTERSECT_PARTIAL);
  103. vector<Point2f> targetVertices(3);
  104. targetVertices[0] = Point2f(1.0f, 0.0f);
  105. targetVertices[1] = Point2f(1.0f, 1.0f);
  106. targetVertices[2] = Point2f(0.0f, 1.0f);
  107. compare(vertices, targetVertices);
  108. }
  109. TEST(Imgproc_RotatedRectangleIntersection, accuracy_4)
  110. {
  111. // full intersection, rectangles of same size directly on top of each other
  112. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  113. RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
  114. vector<Point2f> vertices;
  115. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  116. CV_Assert(ret == INTERSECT_FULL);
  117. vector<Point2f> targetVertices(4);
  118. targetVertices[0] = Point2f(-1.0f, 1.0f);
  119. targetVertices[1] = Point2f(-1.0f, -1.0f);
  120. targetVertices[2] = Point2f(1.0f, -1.0f);
  121. targetVertices[3] = Point2f(1.0f, 1.0f);
  122. compare(vertices, targetVertices);
  123. }
  124. TEST(Imgproc_RotatedRectangleIntersection, accuracy_5)
  125. {
  126. // partial intersection, rectangle on top rotated 45 degrees
  127. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  128. RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 45.0f);
  129. vector<Point2f> vertices;
  130. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  131. CV_Assert(ret == INTERSECT_PARTIAL);
  132. vector<Point2f> targetVertices(8);
  133. targetVertices[0] = Point2f(-1.0f, -0.414214f);
  134. targetVertices[1] = Point2f(-0.414214f, -1.0f);
  135. targetVertices[2] = Point2f(0.414214f, -1.0f);
  136. targetVertices[3] = Point2f(1.0f, -0.414214f);
  137. targetVertices[4] = Point2f(1.0f, 0.414214f);
  138. targetVertices[5] = Point2f(0.414214f, 1.0f);
  139. targetVertices[6] = Point2f(-0.414214f, 1.0f);
  140. targetVertices[7] = Point2f(-1.0f, 0.414214f);
  141. compare(vertices, targetVertices);
  142. }
  143. TEST(Imgproc_RotatedRectangleIntersection, accuracy_6)
  144. {
  145. // 6 - partial intersection, rectangle on top of different size
  146. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  147. RotatedRect rect2(Point2f(0, 0), Size2f(2, 10), 0.0f);
  148. vector<Point2f> vertices;
  149. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  150. CV_Assert(ret == INTERSECT_PARTIAL);
  151. vector<Point2f> targetVertices(4);
  152. targetVertices[0] = Point2f(-1.0f, -1.0f);
  153. targetVertices[1] = Point2f(1.0f, -1.0f);
  154. targetVertices[2] = Point2f(1.0f, 1.0f);
  155. targetVertices[3] = Point2f(-1.0f, 1.0f);
  156. compare(vertices, targetVertices);
  157. }
  158. TEST(Imgproc_RotatedRectangleIntersection, accuracy_7)
  159. {
  160. // full intersection, rectangle fully enclosed in the other
  161. RotatedRect rect1(Point2f(0, 0), Size2f(12.34f, 56.78f), 0.0f);
  162. RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), 0.0f);
  163. vector<Point2f> vertices;
  164. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  165. CV_Assert(ret == INTERSECT_FULL);
  166. vector<Point2f> targetVertices(4);
  167. targetVertices[0] = Point2f(-1.0f, 1.0f);
  168. targetVertices[1] = Point2f(-1.0f, -1.0f);
  169. targetVertices[2] = Point2f(1.0f, -1.0f);
  170. targetVertices[3] = Point2f(1.0f, 1.0f);
  171. compare(vertices, targetVertices);
  172. }
  173. TEST(Imgproc_RotatedRectangleIntersection, accuracy_8)
  174. {
  175. // intersection by a single vertex
  176. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  177. RotatedRect rect2(Point2f(2, 2), Size2f(2, 2), 0.0f);
  178. vector<Point2f> vertices;
  179. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  180. CV_Assert(ret == INTERSECT_PARTIAL);
  181. compare(vertices, vector<Point2f>(1, Point2f(1.0f, 1.0f)));
  182. }
  183. TEST(Imgproc_RotatedRectangleIntersection, accuracy_9)
  184. {
  185. // full intersection, rectangle fully enclosed in the other
  186. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  187. RotatedRect rect2(Point2f(2, 0), Size2f(2, 123.45f), 0.0f);
  188. vector<Point2f> vertices;
  189. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  190. CV_Assert(ret == INTERSECT_PARTIAL);
  191. vector<Point2f> targetVertices(2);
  192. targetVertices[0] = Point2f(1.0f, -1.0f);
  193. targetVertices[1] = Point2f(1.0f, 1.0f);
  194. compare(vertices, targetVertices);
  195. }
  196. TEST(Imgproc_RotatedRectangleIntersection, accuracy_10)
  197. {
  198. // three points of rect2 are inside rect1.
  199. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  200. RotatedRect rect2(Point2f(0, 0.5), Size2f(1, 1), 45.0f);
  201. vector<Point2f> vertices;
  202. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  203. CV_Assert(ret == INTERSECT_PARTIAL);
  204. vector<Point2f> targetVertices(5);
  205. targetVertices[0] = Point2f(0.207107f, 1.0f);
  206. targetVertices[1] = Point2f(-0.207107f, 1.0f);
  207. targetVertices[2] = Point2f(-0.707107f, 0.5f);
  208. targetVertices[3] = Point2f(0.0f, -0.207107f);
  209. targetVertices[4] = Point2f(0.707107f, 0.5f);
  210. compare(vertices, targetVertices);
  211. }
  212. TEST(Imgproc_RotatedRectangleIntersection, accuracy_11)
  213. {
  214. RotatedRect rect1(Point2f(0, 0), Size2f(4, 2), 0.0f);
  215. RotatedRect rect2(Point2f(0, 0), Size2f(2, 2), -45.0f);
  216. vector<Point2f> vertices;
  217. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  218. CV_Assert(ret == INTERSECT_PARTIAL);
  219. vector<Point2f> targetVertices(6);
  220. targetVertices[0] = Point2f(-0.414214f, -1.0f);
  221. targetVertices[1] = Point2f(0.414213f, -1.0f);
  222. targetVertices[2] = Point2f(1.41421f, 0.0f);
  223. targetVertices[3] = Point2f(0.414214f, 1.0f);
  224. targetVertices[4] = Point2f(-0.414213f, 1.0f);
  225. targetVertices[5] = Point2f(-1.41421f, 0.0f);
  226. compare(vertices, targetVertices);
  227. }
  228. TEST(Imgproc_RotatedRectangleIntersection, accuracy_12)
  229. {
  230. RotatedRect rect1(Point2f(0, 0), Size2f(2, 2), 0.0f);
  231. RotatedRect rect2(Point2f(0, 1), Size2f(1, 1), 0.0f);
  232. vector<Point2f> vertices;
  233. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  234. CV_Assert(ret == INTERSECT_PARTIAL);
  235. vector<Point2f> targetVertices(4);
  236. targetVertices[0] = Point2f(-0.5f, 1.0f);
  237. targetVertices[1] = Point2f(-0.5f, 0.5f);
  238. targetVertices[2] = Point2f(0.5f, 0.5f);
  239. targetVertices[3] = Point2f(0.5f, 1.0f);
  240. compare(vertices, targetVertices);
  241. }
  242. TEST(Imgproc_RotatedRectangleIntersection, accuracy_13)
  243. {
  244. RotatedRect rect1(Point2f(0, 0), Size2f(1, 3), 0.0f);
  245. RotatedRect rect2(Point2f(0, 1), Size2f(3, 1), 0.0f);
  246. vector<Point2f> vertices;
  247. int ret = rotatedRectangleIntersection(rect1, rect2, vertices);
  248. CV_Assert(ret == INTERSECT_PARTIAL);
  249. vector<Point2f> targetVertices(4);
  250. targetVertices[0] = Point2f(-0.5f, 0.5f);
  251. targetVertices[1] = Point2f(0.5f, 0.5f);
  252. targetVertices[2] = Point2f(0.5f, 1.5f);
  253. targetVertices[3] = Point2f(-0.5f, 1.5f);
  254. compare(vertices, targetVertices);
  255. }
  256. TEST(Imgproc_RotatedRectangleIntersection, accuracy_14)
  257. {
  258. const int kNumTests = 100;
  259. const float kWidth = 5;
  260. const float kHeight = 5;
  261. RotatedRect rects[2];
  262. std::vector<Point2f> inter;
  263. cv::RNG& rng = cv::theRNG();
  264. for (int i = 0; i < kNumTests; ++i)
  265. {
  266. for (int j = 0; j < 2; ++j)
  267. {
  268. rects[j].center = Point2f(rng.uniform(0.0f, kWidth), rng.uniform(0.0f, kHeight));
  269. rects[j].size = Size2f(rng.uniform(1.0f, kWidth), rng.uniform(1.0f, kHeight));
  270. rects[j].angle = rng.uniform(0.0f, 360.0f);
  271. }
  272. int res = rotatedRectangleIntersection(rects[0], rects[1], inter);
  273. EXPECT_TRUE(res == INTERSECT_NONE || res == INTERSECT_PARTIAL || res == INTERSECT_FULL) << res;
  274. ASSERT_TRUE(inter.size() < 4 || isContourConvex(inter)) << inter;
  275. }
  276. }
  277. TEST(Imgproc_RotatedRectangleIntersection, regression_12221_1)
  278. {
  279. RotatedRect r1(
  280. Point2f(259.65081787109375, 51.58895492553711),
  281. Size2f(5487.8779296875, 233.8921661376953),
  282. -29.488616943359375);
  283. RotatedRect r2(
  284. Point2f(293.70465087890625, 112.10154724121094),
  285. Size2f(5487.8896484375, 234.87368774414062),
  286. -31.27001953125);
  287. std::vector<Point2f> intersections;
  288. int interType = cv::rotatedRectangleIntersection(r1, r2, intersections);
  289. EXPECT_EQ(INTERSECT_PARTIAL, interType);
  290. EXPECT_LE(intersections.size(), (size_t)8);
  291. }
  292. TEST(Imgproc_RotatedRectangleIntersection, regression_12221_2)
  293. {
  294. RotatedRect r1(
  295. Point2f(239.78500366210938, 515.72021484375),
  296. Size2f(70.23420715332031, 39.74684524536133),
  297. -42.86162567138672);
  298. RotatedRect r2(
  299. Point2f(242.4205322265625, 510.1195373535156),
  300. Size2f(66.85948944091797, 61.46455383300781),
  301. -9.840961456298828);
  302. std::vector<Point2f> intersections;
  303. int interType = cv::rotatedRectangleIntersection(r1, r2, intersections);
  304. EXPECT_EQ(INTERSECT_PARTIAL, interType);
  305. EXPECT_LE(intersections.size(), (size_t)8);
  306. }
  307. TEST(Imgproc_RotatedRectangleIntersection, regression_18520)
  308. {
  309. RotatedRect rr_empty(
  310. Point2f(2, 2),
  311. Size2f(0, 0), // empty
  312. 0);
  313. RotatedRect rr(
  314. Point2f(50, 50),
  315. Size2f(4, 4),
  316. 0);
  317. {
  318. std::vector<Point2f> intersections;
  319. int interType = cv::rotatedRectangleIntersection(rr_empty, rr, intersections);
  320. EXPECT_EQ(INTERSECT_NONE, interType) << "rr_empty, rr";
  321. EXPECT_EQ((size_t)0, intersections.size()) << "rr_empty, rr";
  322. }
  323. {
  324. std::vector<Point2f> intersections;
  325. int interType = cv::rotatedRectangleIntersection(rr, rr_empty, intersections);
  326. EXPECT_EQ(INTERSECT_NONE, interType) << "rr, rr_empty";
  327. EXPECT_EQ((size_t)0, intersections.size()) << "rr, rr_empty";
  328. }
  329. }
  330. TEST(Imgproc_RotatedRectangleIntersection, regression_19824)
  331. {
  332. RotatedRect r1(
  333. Point2f(246805.033f, 4002326.94f),
  334. Size2f(26.40587f, 6.20026f),
  335. -62.10156f);
  336. RotatedRect r2(
  337. Point2f(246805.122f, 4002326.59f),
  338. Size2f(27.4821f, 8.5361f),
  339. -56.33761f);
  340. std::vector<Point2f> intersections;
  341. int interType = cv::rotatedRectangleIntersection(r1, r2, intersections);
  342. EXPECT_EQ(INTERSECT_PARTIAL, interType);
  343. EXPECT_LE(intersections.size(), (size_t)7);
  344. }
  345. }} // namespace