PathOpsQuadLineIntersectionTest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "src/pathops/SkPathOpsQuad.h"
  10. #include "src/pathops/SkReduceOrder.h"
  11. #include "tests/PathOpsExtendedTest.h"
  12. #include "tests/PathOpsTestCommon.h"
  13. #include "tests/Test.h"
  14. #include <utility>
  15. static struct lineQuad {
  16. QuadPts quad;
  17. SkDLine line;
  18. int result;
  19. SkDPoint expected[2];
  20. } lineQuadTests[] = {
  21. // quad line results
  22. {{{{1, 1}, {2, 1}, {0, 2}}}, {{{0, 0}, {1, 1}}}, 1, {{1, 1}, {0, 0}} },
  23. {{{{0, 0}, {1, 1}, {3, 1}}}, {{{0, 0}, {3, 1}}}, 2, {{0, 0}, {3, 1}} },
  24. {{{{2, 0}, {1, 1}, {2, 2}}}, {{{0, 0}, {0, 2}}}, 0, {{0, 0}, {0, 0}} },
  25. {{{{4, 0}, {0, 1}, {4, 2}}}, {{{3, 1}, {4, 1}}}, 0, {{0, 0}, {0, 0}} },
  26. {{{{0, 0}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}, 1, {{.25, .75}, {0, 0}} },
  27. };
  28. static size_t lineQuadTests_count = SK_ARRAY_COUNT(lineQuadTests);
  29. static int doIntersect(SkIntersections& intersections, const SkDQuad& quad, const SkDLine& line,
  30. bool& flipped) {
  31. int result;
  32. flipped = false;
  33. if (line[0].fX == line[1].fX) {
  34. double top = line[0].fY;
  35. double bottom = line[1].fY;
  36. flipped = top > bottom;
  37. if (flipped) {
  38. using std::swap;
  39. swap(top, bottom);
  40. }
  41. result = intersections.vertical(quad, top, bottom, line[0].fX, flipped);
  42. } else if (line[0].fY == line[1].fY) {
  43. double left = line[0].fX;
  44. double right = line[1].fX;
  45. flipped = left > right;
  46. if (flipped) {
  47. using std::swap;
  48. swap(left, right);
  49. }
  50. result = intersections.horizontal(quad, left, right, line[0].fY, flipped);
  51. } else {
  52. intersections.intersect(quad, line);
  53. result = intersections.used();
  54. }
  55. return result;
  56. }
  57. static struct oneLineQuad {
  58. QuadPts quad;
  59. SkDLine line;
  60. } oneOffs[] = {
  61. {{{{97.9337616,100}, {88,112.94265}, {88,130}}},
  62. {{{88.919838,120}, {107.058823,120}}}},
  63. {{{{447.96701049804687, 894.4381103515625}, {448.007080078125, 894.4239501953125},
  64. {448.0140380859375, 894.4215087890625}}},
  65. {{{490.43548583984375, 879.40740966796875}, {405.59262084960937, 909.435546875}}}},
  66. {{{{142.589081, 102.283646}, {149.821579, 100}, {158, 100}}},
  67. {{{90, 230}, {160, 60}}}},
  68. {{{{1101, 10}, {1101, 8.3431453704833984}, {1099.828857421875, 7.1711997985839844}}},
  69. {{{1099.828857421875,7.1711711883544922}, {1099.121337890625,7.8786783218383789}}}},
  70. {{{{973, 507}, {973, 508.24264526367187}, {972.12158203125, 509.12161254882812}}},
  71. {{{930, 467}, {973, 510}}}},
  72. {{{{369.848602, 145.680267}, {382.360413, 121.298294}, {406.207703, 121.298294}}},
  73. {{{406.207703, 121.298294}, {348.781738, 123.864815}}}},
  74. };
  75. static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
  76. static void testOneOffs(skiatest::Reporter* reporter) {
  77. bool flipped = false;
  78. for (size_t index = 0; index < oneOffs_count; ++index) {
  79. const QuadPts& q = oneOffs[index].quad;
  80. SkDQuad quad;
  81. quad.debugSet(q.fPts);
  82. SkASSERT(ValidQuad(quad));
  83. const SkDLine& line = oneOffs[index].line;
  84. SkASSERT(ValidLine(line));
  85. SkIntersections intersections;
  86. int result = doIntersect(intersections, quad, line, flipped);
  87. for (int inner = 0; inner < result; ++inner) {
  88. double quadT = intersections[0][inner];
  89. SkDPoint quadXY = quad.ptAtT(quadT);
  90. double lineT = intersections[1][inner];
  91. SkDPoint lineXY = line.ptAtT(lineT);
  92. if (!quadXY.approximatelyEqual(lineXY)) {
  93. quadXY.approximatelyEqual(lineXY);
  94. SkDebugf("");
  95. }
  96. REPORTER_ASSERT(reporter, quadXY.approximatelyEqual(lineXY));
  97. }
  98. }
  99. }
  100. DEF_TEST(PathOpsQuadLineIntersectionOneOff, reporter) {
  101. testOneOffs(reporter);
  102. }
  103. DEF_TEST(PathOpsQuadLineIntersection, reporter) {
  104. for (size_t index = 0; index < lineQuadTests_count; ++index) {
  105. int iIndex = static_cast<int>(index);
  106. const QuadPts& q = lineQuadTests[index].quad;
  107. SkDQuad quad;
  108. quad.debugSet(q.fPts);
  109. SkASSERT(ValidQuad(quad));
  110. const SkDLine& line = lineQuadTests[index].line;
  111. SkASSERT(ValidLine(line));
  112. SkReduceOrder reducer1, reducer2;
  113. int order1 = reducer1.reduce(quad);
  114. int order2 = reducer2.reduce(line);
  115. if (order1 < 3) {
  116. SkDebugf("%s [%d] quad order=%d\n", __FUNCTION__, iIndex, order1);
  117. REPORTER_ASSERT(reporter, 0);
  118. }
  119. if (order2 < 2) {
  120. SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
  121. REPORTER_ASSERT(reporter, 0);
  122. }
  123. SkIntersections intersections;
  124. bool flipped = false;
  125. int result = doIntersect(intersections, quad, line, flipped);
  126. REPORTER_ASSERT(reporter, result == lineQuadTests[index].result);
  127. if (intersections.used() <= 0) {
  128. continue;
  129. }
  130. for (int pt = 0; pt < result; ++pt) {
  131. double tt1 = intersections[0][pt];
  132. REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
  133. SkDPoint t1 = quad.ptAtT(tt1);
  134. double tt2 = intersections[1][pt];
  135. REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
  136. SkDPoint t2 = line.ptAtT(tt2);
  137. if (!t1.approximatelyEqual(t2)) {
  138. SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
  139. __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
  140. REPORTER_ASSERT(reporter, 0);
  141. }
  142. if (!t1.approximatelyEqual(lineQuadTests[index].expected[0])
  143. && (lineQuadTests[index].result == 1
  144. || !t1.approximatelyEqual(lineQuadTests[index].expected[1]))) {
  145. SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
  146. REPORTER_ASSERT(reporter, 0);
  147. }
  148. }
  149. }
  150. }