test_intersectconvexconvex.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. TEST(Imgproc_IntersectConvexConvex, no_intersection)
  7. {
  8. std::vector<cv::Point> convex1;
  9. convex1.push_back(cv::Point(290, 126));
  10. convex1.push_back(cv::Point(284, 132));
  11. convex1.push_back(cv::Point(281, 133));
  12. convex1.push_back(cv::Point(256, 124));
  13. convex1.push_back(cv::Point(249, 116));
  14. convex1.push_back(cv::Point(234, 91));
  15. convex1.push_back(cv::Point(232, 86));
  16. convex1.push_back(cv::Point(232, 79));
  17. convex1.push_back(cv::Point(251, 69));
  18. convex1.push_back(cv::Point(257, 68));
  19. convex1.push_back(cv::Point(297, 85));
  20. convex1.push_back(cv::Point(299, 87));
  21. std::vector<cv::Point> convex2;
  22. convex2.push_back(cv::Point(192, 236));
  23. convex2.push_back(cv::Point(190, 245));
  24. convex2.push_back(cv::Point(177, 260));
  25. convex2.push_back(cv::Point(154, 271));
  26. convex2.push_back(cv::Point(142, 270));
  27. convex2.push_back(cv::Point(135, 263));
  28. convex2.push_back(cv::Point(131, 254));
  29. convex2.push_back(cv::Point(132, 240));
  30. convex2.push_back(cv::Point(172, 213));
  31. convex2.push_back(cv::Point(176, 216));
  32. std::vector<cv::Point> intersection;
  33. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  34. EXPECT_TRUE(intersection.empty());
  35. EXPECT_NEAR(area, 0, std::numeric_limits<float>::epsilon());
  36. }
  37. TEST(Imgproc_IntersectConvexConvex, no_intersection_with_1_vertex_on_edge_1)
  38. {
  39. std::vector<cv::Point> convex1;
  40. convex1.push_back(cv::Point(0,0));
  41. convex1.push_back(cv::Point(740, 0));
  42. convex1.push_back(cv::Point(740, 540));
  43. convex1.push_back(cv::Point(0, 540));
  44. std::vector<cv::Point> convex2;
  45. convex2.push_back(cv::Point(0, 210));
  46. convex2.push_back(cv::Point(-30, 210));
  47. convex2.push_back(cv::Point(-37, 170));
  48. convex2.push_back(cv::Point(-7, 172));
  49. std::vector<cv::Point> intersection;
  50. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  51. EXPECT_TRUE(intersection.empty());
  52. EXPECT_NEAR(area, 0, std::numeric_limits<float>::epsilon());
  53. }
  54. TEST(Imgproc_IntersectConvexConvex, no_intersection_with_1_vertex_on_edge_2)
  55. {
  56. std::vector<cv::Point> convex1;
  57. convex1.push_back(cv::Point(0,0));
  58. convex1.push_back(cv::Point(740, 0));
  59. convex1.push_back(cv::Point(740, 540));
  60. convex1.push_back(cv::Point(0, 540));
  61. std::vector<cv::Point> convex2;
  62. convex2.push_back(cv::Point(740, 210));
  63. convex2.push_back(cv::Point(750, 100));
  64. convex2.push_back(cv::Point(790, 250));
  65. convex2.push_back(cv::Point(800, 500));
  66. std::vector<cv::Point> intersection;
  67. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  68. EXPECT_TRUE(intersection.empty());
  69. EXPECT_NEAR(area, 0, std::numeric_limits<float>::epsilon());
  70. }
  71. TEST(Imgproc_IntersectConvexConvex, intersection_with_1_vertex_on_edge)
  72. {
  73. std::vector<cv::Point> convex1;
  74. convex1.push_back(cv::Point(0,0));
  75. convex1.push_back(cv::Point(740, 0));
  76. convex1.push_back(cv::Point(740, 540));
  77. convex1.push_back(cv::Point(0, 540));
  78. std::vector<cv::Point> convex2;
  79. convex2.push_back(cv::Point(30, 210));
  80. convex2.push_back(cv::Point(0,210));
  81. convex2.push_back(cv::Point(7, 172));
  82. convex2.push_back(cv::Point(37, 170));
  83. std::vector<cv::Point> intersection;
  84. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  85. std::vector<cv::Point> expected_intersection;
  86. expected_intersection.push_back(cv::Point(0, 210));
  87. expected_intersection.push_back(cv::Point(7, 172));
  88. expected_intersection.push_back(cv::Point(37, 170));
  89. expected_intersection.push_back(cv::Point(30, 210));
  90. EXPECT_EQ(intersection, expected_intersection);
  91. EXPECT_NEAR(area, 1163, std::numeric_limits<float>::epsilon());
  92. }
  93. TEST(Imgproc_IntersectConvexConvex, intersection_with_2_vertices_on_edge)
  94. {
  95. std::vector<cv::Point> convex1;
  96. convex1.push_back(cv::Point(0,0));
  97. convex1.push_back(cv::Point(740, 0));
  98. convex1.push_back(cv::Point(740, 540));
  99. convex1.push_back(cv::Point(0, 540));
  100. std::vector<cv::Point> convex2;
  101. convex2.push_back(cv::Point(30, 210));
  102. convex2.push_back(cv::Point(37, 170));
  103. convex2.push_back(cv::Point(0,210));
  104. convex2.push_back(cv::Point(0, 300));
  105. std::vector<cv::Point> intersection;
  106. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  107. std::vector<cv::Point> expected_intersection;
  108. expected_intersection.push_back(cv::Point(0, 300));
  109. expected_intersection.push_back(cv::Point(0, 210));
  110. expected_intersection.push_back(cv::Point(37, 170));
  111. expected_intersection.push_back(cv::Point(30, 210));
  112. EXPECT_EQ(intersection, expected_intersection);
  113. EXPECT_NEAR(area, 1950, std::numeric_limits<float>::epsilon());
  114. }
  115. TEST(Imgproc_IntersectConvexConvex, intersection_1)
  116. {
  117. std::vector<cv::Point> convex1;
  118. convex1.push_back(cv::Point(0,0));
  119. convex1.push_back(cv::Point(740, 0));
  120. convex1.push_back(cv::Point(740, 540));
  121. convex1.push_back(cv::Point(0, 540));
  122. std::vector<cv::Point> convex2;
  123. convex2.push_back(cv::Point(20,210));
  124. convex2.push_back(cv::Point(30, 210));
  125. convex2.push_back(cv::Point(37, 170));
  126. convex2.push_back(cv::Point(7, 172));
  127. std::vector<cv::Point> intersection;
  128. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  129. std::vector<cv::Point> expected_intersection;
  130. expected_intersection.push_back(cv::Point(7, 172));
  131. expected_intersection.push_back(cv::Point(37, 170));
  132. expected_intersection.push_back(cv::Point(30, 210));
  133. expected_intersection.push_back(cv::Point(20, 210));
  134. EXPECT_EQ(intersection, expected_intersection);
  135. EXPECT_NEAR(area, 783, std::numeric_limits<float>::epsilon());
  136. }
  137. TEST(Imgproc_IntersectConvexConvex, intersection_2)
  138. {
  139. std::vector<cv::Point> convex1;
  140. convex1.push_back(cv::Point(0,0));
  141. convex1.push_back(cv::Point(740, 0));
  142. convex1.push_back(cv::Point(740, 540));
  143. convex1.push_back(cv::Point(0, 540));
  144. std::vector<cv::Point> convex2;
  145. convex2.push_back(cv::Point(-2,210));
  146. convex2.push_back(cv::Point(-5, 300));
  147. convex2.push_back(cv::Point(37, 150));
  148. convex2.push_back(cv::Point(7, 172));
  149. std::vector<cv::Point> intersection;
  150. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  151. std::vector<cv::Point> expected_intersection;
  152. expected_intersection.push_back(cv::Point(0, 202));
  153. expected_intersection.push_back(cv::Point(7, 172));
  154. expected_intersection.push_back(cv::Point(37, 150));
  155. expected_intersection.push_back(cv::Point(0, 282));
  156. EXPECT_EQ(intersection, expected_intersection);
  157. EXPECT_NEAR(area, 1857.19836425781, std::numeric_limits<float>::epsilon());
  158. }
  159. TEST(Imgproc_IntersectConvexConvex, intersection_3)
  160. {
  161. std::vector<cv::Point> convex1;
  162. convex1.push_back(cv::Point(15, 0));
  163. convex1.push_back(cv::Point(740, 0));
  164. convex1.push_back(cv::Point(740, 540));
  165. convex1.push_back(cv::Point(15, 540));
  166. std::vector<cv::Point> convex2;
  167. convex2.push_back(cv::Point(0,210));
  168. convex2.push_back(cv::Point(30, 210));
  169. convex2.push_back(cv::Point(37, 170));
  170. convex2.push_back(cv::Point(7, 172));
  171. std::vector<cv::Point> intersection;
  172. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  173. std::vector<cv::Point> expected_intersection;
  174. expected_intersection.push_back(cv::Point(15, 171));
  175. expected_intersection.push_back(cv::Point(37, 170));
  176. expected_intersection.push_back(cv::Point(30, 210));
  177. expected_intersection.push_back(cv::Point(15, 210));
  178. EXPECT_EQ(intersection, expected_intersection);
  179. EXPECT_NEAR(area, 723.866760253906, std::numeric_limits<float>::epsilon());
  180. }
  181. TEST(Imgproc_IntersectConvexConvex, intersection_4)
  182. {
  183. std::vector<cv::Point> convex1;
  184. convex1.push_back(cv::Point(15, 0));
  185. convex1.push_back(cv::Point(740, 0));
  186. convex1.push_back(cv::Point(740, 540));
  187. convex1.push_back(cv::Point(15, 540));
  188. std::vector<cv::Point> convex2;
  189. convex2.push_back(cv::Point(15, 0));
  190. convex2.push_back(cv::Point(740, 0));
  191. convex2.push_back(cv::Point(740, 540));
  192. convex2.push_back(cv::Point(15, 540));
  193. std::vector<cv::Point> intersection;
  194. float area = cv::intersectConvexConvex(convex1, convex2, intersection);
  195. std::vector<cv::Point> expected_intersection;
  196. expected_intersection.push_back(cv::Point(15, 0));
  197. expected_intersection.push_back(cv::Point(740, 0));
  198. expected_intersection.push_back(cv::Point(740, 540));
  199. expected_intersection.push_back(cv::Point(15, 540));
  200. EXPECT_EQ(intersection, expected_intersection);
  201. EXPECT_NEAR(area, 391500, std::numeric_limits<float>::epsilon());
  202. }
  203. } // namespace
  204. } // opencv_test