PathOpsCubicLineIntersectionTest.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/SkPathOpsCubic.h"
  9. #include "src/pathops/SkPathOpsLine.h"
  10. #include "src/pathops/SkReduceOrder.h"
  11. #include "tests/PathOpsTestCommon.h"
  12. #include "tests/Test.h"
  13. #include <utility>
  14. struct lineCubic {
  15. CubicPts cubic;
  16. SkDLine line;
  17. };
  18. static lineCubic failLineCubicTests[] = {
  19. {{{{37.5273438,-1.44140625}, {37.8736992,-1.69921875}, {38.1640625,-2.140625},
  20. {38.3984375,-2.765625}}},
  21. {{{40.625,-5.7890625}, {37.7109375,1.3515625}}}},
  22. };
  23. static const size_t failLineCubicTests_count = SK_ARRAY_COUNT(failLineCubicTests);
  24. static void testFail(skiatest::Reporter* reporter, int iIndex) {
  25. const CubicPts& cuPts = failLineCubicTests[iIndex].cubic;
  26. SkDCubic cubic;
  27. cubic.debugSet(cuPts.fPts);
  28. SkASSERT(ValidCubic(cubic));
  29. const SkDLine& line = failLineCubicTests[iIndex].line;
  30. SkASSERT(ValidLine(line));
  31. SkReduceOrder reduce1;
  32. SkReduceOrder reduce2;
  33. int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
  34. int order2 = reduce2.reduce(line);
  35. if (order1 < 4) {
  36. SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
  37. REPORTER_ASSERT(reporter, 0);
  38. }
  39. if (order2 < 2) {
  40. SkDebugf("[%d] line order=%d\n", iIndex, order2);
  41. REPORTER_ASSERT(reporter, 0);
  42. }
  43. if (order1 == 4 && order2 == 2) {
  44. SkIntersections i;
  45. int roots = i.intersect(cubic, line);
  46. REPORTER_ASSERT(reporter, roots == 0);
  47. }
  48. }
  49. static lineCubic lineCubicTests[] = {
  50. {{{{0, 6}, {1.0851458311080933, 4.3722810745239258}, {1.5815209150314331, 3.038947582244873}, {1.9683018922805786, 1.9999997615814209}}},
  51. {{{3,2}, {1,2}}}},
  52. {{{{0.468027353,4}, {1.06734705,1.33333337}, {1.36700678,0}, {3,0}}},
  53. {{{2,1}, {0,1}}}},
  54. {{{{-634.60540771484375, -481.262939453125}, {266.2696533203125, -752.70867919921875},
  55. {-751.8370361328125, -317.37921142578125}, {-969.7427978515625, 824.7255859375}}},
  56. {{{-287.9506133720805678, -557.1376476615772617},
  57. {-285.9506133720805678, -557.1376476615772617}}}},
  58. {{{{36.7184372,0.888650894}, {36.7184372,0.888650894}, {35.1233864,0.554015458},
  59. {34.5114098,-0.115255356}}}, {{{35.4531212,0}, {31.9375,0}}}},
  60. {{{{421, 378}, {421, 380.209137f}, {418.761414f, 382}, {416, 382}}},
  61. {{{320, 378}, {421, 378.000031f}}}},
  62. {{{{416, 383}, {418.761414f, 383}, {421, 380.761414f}, {421, 378}}},
  63. {{{320, 378}, {421, 378.000031f}}}},
  64. {{{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}},
  65. {{{149,675}, {149,710.001465}}}},
  66. {{{{0,1}, {1,6}, {4,1}, {4,3}}},
  67. {{{6,1}, {1,4}}}},
  68. {{{{0,1}, {2,6}, {4,1}, {5,4}}},
  69. {{{6,2}, {1,4}}}},
  70. {{{{0,4}, {3,4}, {6,2}, {5,2}}},
  71. {{{4,3}, {2,6}}}},
  72. #if 0
  73. {{{{258, 122}, {260.761414, 122}, { 263, 124.238579}, {263, 127}}},
  74. {{{259.82843, 125.17157}, {261.535522, 123.46447}}}},
  75. #endif
  76. {{{{1006.6951293945312,291}, {1023.263671875,291}, {1033.8402099609375,304.43145751953125},
  77. {1030.318359375,321}}},
  78. {{{979.30487060546875,561}, {1036.695068359375,291}}}},
  79. {{{{259.30487060546875,561}, {242.73631286621094,561}, {232.15980529785156,547.56854248046875},
  80. {235.68154907226562,531}}},
  81. {{{286.69512939453125,291}, {229.30485534667969,561}}}},
  82. {{{{1, 2}, {2, 6}, {2, 0}, {1, 0}}}, {{{1, 0}, {1, 2}}}},
  83. {{{{0, 0}, {0, 1}, {0, 1}, {1, 1}}}, {{{0, 1}, {1, 0}}}},
  84. };
  85. static const size_t lineCubicTests_count = SK_ARRAY_COUNT(lineCubicTests);
  86. static int doIntersect(SkIntersections& intersections, const SkDCubic& cubic, const SkDLine& line) {
  87. int result;
  88. bool flipped = false;
  89. if (line[0].fX == line[1].fX) {
  90. double top = line[0].fY;
  91. double bottom = line[1].fY;
  92. flipped = top > bottom;
  93. if (flipped) {
  94. using std::swap;
  95. swap(top, bottom);
  96. }
  97. result = intersections.vertical(cubic, top, bottom, line[0].fX, flipped);
  98. } else if (line[0].fY == line[1].fY) {
  99. double left = line[0].fX;
  100. double right = line[1].fX;
  101. flipped = left > right;
  102. if (flipped) {
  103. using std::swap;
  104. swap(left, right);
  105. }
  106. result = intersections.horizontal(cubic, left, right, line[0].fY, flipped);
  107. } else {
  108. intersections.intersect(cubic, line);
  109. result = intersections.used();
  110. }
  111. return result;
  112. }
  113. static void testOne(skiatest::Reporter* reporter, int iIndex) {
  114. const CubicPts& cuPts = lineCubicTests[iIndex].cubic;
  115. SkDCubic cubic;
  116. cubic.debugSet(cuPts.fPts);
  117. SkASSERT(ValidCubic(cubic));
  118. const SkDLine& line = lineCubicTests[iIndex].line;
  119. SkASSERT(ValidLine(line));
  120. SkReduceOrder reduce1;
  121. SkReduceOrder reduce2;
  122. int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
  123. int order2 = reduce2.reduce(line);
  124. if (order1 < 4) {
  125. SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
  126. REPORTER_ASSERT(reporter, 0);
  127. }
  128. if (order2 < 2) {
  129. SkDebugf("[%d] line order=%d\n", iIndex, order2);
  130. REPORTER_ASSERT(reporter, 0);
  131. }
  132. if (order1 == 4 && order2 == 2) {
  133. SkIntersections i;
  134. int roots = doIntersect(i, cubic, line);
  135. for (int pt = 0; pt < roots; ++pt) {
  136. double tt1 = i[0][pt];
  137. SkDPoint xy1 = cubic.ptAtT(tt1);
  138. double tt2 = i[1][pt];
  139. SkDPoint xy2 = line.ptAtT(tt2);
  140. if (!xy1.approximatelyEqual(xy2)) {
  141. SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
  142. __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
  143. }
  144. REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
  145. }
  146. #if ONE_OFF_DEBUG
  147. double cubicT = i[0][0];
  148. SkDPoint prev = cubic.ptAtT(cubicT * 2 - 1);
  149. SkDPoint sect = cubic.ptAtT(cubicT);
  150. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prev.fX, prev.fY, sect.fX, sect.fY);
  151. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", sect.fX, sect.fY, cubic[3].fX, cubic[3].fY);
  152. SkDPoint prevL = line.ptAtT(i[1][0] - 0.0000007);
  153. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", prevL.fX, prevL.fY, i.pt(0).fX, i.pt(0).fY);
  154. SkDPoint nextL = line.ptAtT(i[1][0] + 0.0000007);
  155. SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", i.pt(0).fX, i.pt(0).fY, nextL.fX, nextL.fY);
  156. SkDebugf("prevD=%1.9g dist=%1.9g nextD=%1.9g\n", prev.distance(nextL),
  157. sect.distance(i.pt(0)), cubic[3].distance(prevL));
  158. #endif
  159. }
  160. }
  161. DEF_TEST(PathOpsFailCubicLineIntersection, reporter) {
  162. for (size_t index = 0; index < failLineCubicTests_count; ++index) {
  163. int iIndex = static_cast<int>(index);
  164. testFail(reporter, iIndex);
  165. reporter->bumpTestCount();
  166. }
  167. }
  168. DEF_TEST(PathOpsCubicLineIntersection, reporter) {
  169. for (size_t index = 0; index < lineCubicTests_count; ++index) {
  170. int iIndex = static_cast<int>(index);
  171. testOne(reporter, iIndex);
  172. reporter->bumpTestCount();
  173. }
  174. }
  175. DEF_TEST(PathOpsCubicLineIntersectionOneOff, reporter) {
  176. int iIndex = 0;
  177. testOne(reporter, iIndex);
  178. const CubicPts& cuPts = lineCubicTests[iIndex].cubic;
  179. SkDCubic cubic;
  180. cubic.debugSet(cuPts.fPts);
  181. const SkDLine& line = lineCubicTests[iIndex].line;
  182. SkIntersections i;
  183. i.intersect(cubic, line);
  184. SkASSERT(i.used() == 1);
  185. }