PathOpsLineIntersectionTest.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2012 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "src/pathops/SkIntersections.h"
  8. #include "src/pathops/SkPathOpsLine.h"
  9. #include "tests/PathOpsTestCommon.h"
  10. #include "tests/Test.h"
  11. // FIXME: add tests for intersecting, non-intersecting, degenerate, coincident
  12. static const SkDLine tests[][2] = {
  13. {{{{0.00010360032320022583, 1.0172703415155411}, {0.00014114845544099808, 1.0200891587883234}}},
  14. {{{0.00010259449481964111, 1.017270140349865}, {0.00018215179443359375, 1.022890567779541}}}},
  15. #if 0
  16. // these do intersect at a pair of points, but not close enough for check results liking
  17. {{{{365.848175,5081.15186}, {368,5103}}}, {{{367.967712,5102.61084}, {368.278717,5105.71045}}}},
  18. #endif
  19. {{{{30,20}, {30,50}}}, {{{24,30}, {36,30}}}},
  20. {{{{323,193}, {-317,193}}}, {{{0,994}, {0,0}}}},
  21. {{{{90,230}, {160,60}}}, {{{60,120}, {260,120}}}},
  22. {{{{90,230}, {160,60}}}, {{{181.176468,120}, {135.294128,120}}}},
  23. {{{{181.1764678955078125f, 120}, {186.3661956787109375f, 134.7042236328125f}}},
  24. {{{175.8309783935546875f, 141.5211334228515625f}, {187.8782806396484375f, 133.7258148193359375f}}}},
  25. #if 0 // FIXME: these fail because one line is too short and appears quasi-coincident
  26. {{{{158.000000, 926.000000}, {1108.00000, 926.000000}}},
  27. {{{1108.00000, 926.000000}, {1108.00000, 925.999634}}}},
  28. {{{{1108,926}, {1108,925.9996337890625}}}, {{{158,926}, {1108,926}}}},
  29. #endif
  30. {{{{192, 4}, {243, 4}}}, {{{246, 4}, {189, 4}}}},
  31. {{{{246, 4}, {189, 4}}}, {{{192, 4}, {243, 4}}}},
  32. {{{{5, 0}, {0, 5}}}, {{{5, 4}, {1, 4}}}},
  33. {{{{0, 0}, {1, 0}}}, {{{1, 0}, {0, 0}}}},
  34. {{{{0, 0}, {0, 0}}}, {{{0, 0}, {1, 0}}}},
  35. {{{{0, 1}, {0, 1}}}, {{{0, 0}, {0, 2}}}},
  36. {{{{0, 0}, {1, 0}}}, {{{0, 0}, {2, 0}}}},
  37. {{{{1, 1}, {2, 2}}}, {{{0, 0}, {3, 3}}}},
  38. {{{{166.86950047022856, 112.69654129527828}, {166.86948801592692, 112.69655741235339}}},
  39. {{{166.86960700313026, 112.6965477747386}, {166.86925794355412, 112.69656471103423}}}}
  40. };
  41. static const size_t tests_count = SK_ARRAY_COUNT(tests);
  42. static const SkDLine noIntersect[][2] = {
  43. {{{{(double) (2 - 1e-6f),2}, {(double) (2 - 1e-6f),4}}},
  44. {{{2,1}, {2,3}}}},
  45. {{{{0, 0}, {1, 0}}}, {{{3, 0}, {2, 0}}}},
  46. {{{{0, 0}, {0, 0}}}, {{{1, 0}, {2, 0}}}},
  47. {{{{0, 1}, {0, 1}}}, {{{0, 3}, {0, 2}}}},
  48. {{{{0, 0}, {1, 0}}}, {{{2, 0}, {3, 0}}}},
  49. {{{{1, 1}, {2, 2}}}, {{{4, 4}, {3, 3}}}},
  50. };
  51. static const size_t noIntersect_count = SK_ARRAY_COUNT(noIntersect);
  52. static const SkDLine coincidentTests[][2] = {
  53. {{{ {-1.48383003e-006,-83}, {4.2268899e-014,-60} }},
  54. {{ {9.5359502e-007,-60}, {5.08227985e-015,-83} }}},
  55. {{{ { 10105, 2510 }, { 10123, 2509.98999f } }},
  56. {{{10105, 2509.98999f}, { 10123, 2510 } }}},
  57. {{ { { 0, 482.5 }, { -4.4408921e-016, 682.5 } } },
  58. {{{0,683}, {0,482}}}},
  59. {{{{1.77635684e-015,312}, {-1.24344979e-014,348}}},
  60. {{{0,348}, {0,312}}}},
  61. {{{{979.304871, 561}, {1036.69507, 291}}},
  62. {{{985.681519, 531}, {982.159790, 547.568542}}}},
  63. {{{{232.159805, 547.568542}, {235.681549, 531}}},
  64. {{{286.695129,291}, {229.304855,561}}}},
  65. {{{{186.3661956787109375f, 134.7042236328125f}, {187.8782806396484375f, 133.7258148193359375f}}},
  66. {{{175.8309783935546875f, 141.5211334228515625f}, {187.8782806396484375f, 133.7258148193359375f}}}},
  67. {{{{235.681549, 531.000000}, {280.318420, 321.000000}}},
  68. {{{286.695129, 291.000000}, {229.304855, 561.000000}}}},
  69. };
  70. static const size_t coincidentTests_count = SK_ARRAY_COUNT(coincidentTests);
  71. static void check_results(skiatest::Reporter* reporter, const SkDLine& line1, const SkDLine& line2,
  72. const SkIntersections& ts, bool nearAllowed) {
  73. for (int i = 0; i < ts.used(); ++i) {
  74. SkDPoint result1 = line1.ptAtT(ts[0][i]);
  75. SkDPoint result2 = line2.ptAtT(ts[1][i]);
  76. if (nearAllowed && result1.roughlyEqual(result2)) {
  77. continue;
  78. }
  79. if (!result1.approximatelyEqual(result2) && !ts.nearlySame(i)) {
  80. REPORTER_ASSERT(reporter, ts.used() != 1);
  81. result2 = line2.ptAtT(ts[1][i ^ 1]);
  82. if (!result1.approximatelyEqual(result2)) {
  83. SkDebugf(".");
  84. }
  85. REPORTER_ASSERT(reporter, result1.approximatelyEqual(result2));
  86. REPORTER_ASSERT(reporter, result1.approximatelyEqual(ts.pt(i).asSkPoint()));
  87. }
  88. }
  89. }
  90. static void testOne(skiatest::Reporter* reporter, const SkDLine& line1, const SkDLine& line2,
  91. bool nearAllowed) {
  92. SkASSERT(ValidLine(line1));
  93. SkASSERT(ValidLine(line2));
  94. SkIntersections i;
  95. i.allowNear(nearAllowed);
  96. int pts = i.intersect(line1, line2);
  97. REPORTER_ASSERT(reporter, pts);
  98. REPORTER_ASSERT(reporter, pts == i.used());
  99. check_results(reporter, line1, line2, i, nearAllowed);
  100. if (line1[0] == line1[1] || line2[0] == line2[1]) {
  101. return;
  102. }
  103. if (line1[0].fY == line1[1].fY) {
  104. double left = SkTMin(line1[0].fX, line1[1].fX);
  105. double right = SkTMax(line1[0].fX, line1[1].fX);
  106. SkIntersections ts;
  107. ts.horizontal(line2, left, right, line1[0].fY, line1[0].fX != left);
  108. check_results(reporter, line2, line1, ts, nearAllowed);
  109. }
  110. if (line2[0].fY == line2[1].fY) {
  111. double left = SkTMin(line2[0].fX, line2[1].fX);
  112. double right = SkTMax(line2[0].fX, line2[1].fX);
  113. SkIntersections ts;
  114. ts.horizontal(line1, left, right, line2[0].fY, line2[0].fX != left);
  115. check_results(reporter, line1, line2, ts, nearAllowed);
  116. }
  117. if (line1[0].fX == line1[1].fX) {
  118. double top = SkTMin(line1[0].fY, line1[1].fY);
  119. double bottom = SkTMax(line1[0].fY, line1[1].fY);
  120. SkIntersections ts;
  121. ts.vertical(line2, top, bottom, line1[0].fX, line1[0].fY != top);
  122. check_results(reporter, line2, line1, ts, nearAllowed);
  123. }
  124. if (line2[0].fX == line2[1].fX) {
  125. double top = SkTMin(line2[0].fY, line2[1].fY);
  126. double bottom = SkTMax(line2[0].fY, line2[1].fY);
  127. SkIntersections ts;
  128. ts.vertical(line1, top, bottom, line2[0].fX, line2[0].fY != top);
  129. check_results(reporter, line1, line2, ts, nearAllowed);
  130. }
  131. reporter->bumpTestCount();
  132. }
  133. static void testOneCoincident(skiatest::Reporter* reporter, const SkDLine& line1,
  134. const SkDLine& line2) {
  135. SkASSERT(ValidLine(line1));
  136. SkASSERT(ValidLine(line2));
  137. SkIntersections ts;
  138. int pts = ts.intersect(line1, line2);
  139. REPORTER_ASSERT(reporter, pts == 2);
  140. REPORTER_ASSERT(reporter, pts == ts.used());
  141. check_results(reporter, line1, line2, ts, false);
  142. if (line1[0] == line1[1] || line2[0] == line2[1]) {
  143. return;
  144. }
  145. if (line1[0].fY == line1[1].fY) {
  146. double left = SkTMin(line1[0].fX, line1[1].fX);
  147. double right = SkTMax(line1[0].fX, line1[1].fX);
  148. SkIntersections ts;
  149. ts.horizontal(line2, left, right, line1[0].fY, line1[0].fX != left);
  150. REPORTER_ASSERT(reporter, pts == 2);
  151. REPORTER_ASSERT(reporter, pts == ts.used());
  152. check_results(reporter, line2, line1, ts, false);
  153. }
  154. if (line2[0].fY == line2[1].fY) {
  155. double left = SkTMin(line2[0].fX, line2[1].fX);
  156. double right = SkTMax(line2[0].fX, line2[1].fX);
  157. SkIntersections ts;
  158. ts.horizontal(line1, left, right, line2[0].fY, line2[0].fX != left);
  159. REPORTER_ASSERT(reporter, pts == 2);
  160. REPORTER_ASSERT(reporter, pts == ts.used());
  161. check_results(reporter, line1, line2, ts, false);
  162. }
  163. if (line1[0].fX == line1[1].fX) {
  164. double top = SkTMin(line1[0].fY, line1[1].fY);
  165. double bottom = SkTMax(line1[0].fY, line1[1].fY);
  166. SkIntersections ts;
  167. ts.vertical(line2, top, bottom, line1[0].fX, line1[0].fY != top);
  168. REPORTER_ASSERT(reporter, pts == 2);
  169. REPORTER_ASSERT(reporter, pts == ts.used());
  170. check_results(reporter, line2, line1, ts, false);
  171. }
  172. if (line2[0].fX == line2[1].fX) {
  173. double top = SkTMin(line2[0].fY, line2[1].fY);
  174. double bottom = SkTMax(line2[0].fY, line2[1].fY);
  175. SkIntersections ts;
  176. ts.vertical(line1, top, bottom, line2[0].fX, line2[0].fY != top);
  177. REPORTER_ASSERT(reporter, pts == 2);
  178. REPORTER_ASSERT(reporter, pts == ts.used());
  179. check_results(reporter, line1, line2, ts, false);
  180. }
  181. reporter->bumpTestCount();
  182. }
  183. DEF_TEST(PathOpsLineIntersection, reporter) {
  184. size_t index;
  185. for (index = 0; index < coincidentTests_count; ++index) {
  186. const SkDLine& line1 = coincidentTests[index][0];
  187. const SkDLine& line2 = coincidentTests[index][1];
  188. testOneCoincident(reporter, line1, line2);
  189. }
  190. for (index = 0; index < tests_count; ++index) {
  191. const SkDLine& line1 = tests[index][0];
  192. const SkDLine& line2 = tests[index][1];
  193. testOne(reporter, line1, line2, true);
  194. }
  195. for (index = 0; index < noIntersect_count; ++index) {
  196. const SkDLine& line1 = noIntersect[index][0];
  197. const SkDLine& line2 = noIntersect[index][1];
  198. SkIntersections ts;
  199. int pts = ts.intersect(line1, line2);
  200. REPORTER_ASSERT(reporter, !pts);
  201. REPORTER_ASSERT(reporter, pts == ts.used());
  202. reporter->bumpTestCount();
  203. }
  204. }
  205. DEF_TEST(PathOpsLineIntersectionOneOff, reporter) {
  206. int index = 0;
  207. SkASSERT(index < (int) tests_count);
  208. testOne(reporter, tests[index][0], tests[index][1], true);
  209. }
  210. DEF_TEST(PathOpsLineIntersectionExactOneOff, reporter) {
  211. int index = 0;
  212. SkASSERT(index < (int) tests_count);
  213. testOne(reporter, tests[index][0], tests[index][1], false);
  214. }
  215. DEF_TEST(PathOpsLineIntersectionOneCoincident, reporter) {
  216. int index = 0;
  217. SkASSERT(index < (int) coincidentTests_count);
  218. const SkDLine& line1 = coincidentTests[index][0];
  219. const SkDLine& line2 = coincidentTests[index][1];
  220. testOneCoincident(reporter, line1, line2);
  221. }