PathOpsConicLineIntersectionTest.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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/core/SkGeometry.h"
  8. #include "src/pathops/SkIntersections.h"
  9. #include "src/pathops/SkPathOpsConic.h"
  10. #include "src/pathops/SkPathOpsLine.h"
  11. #include "src/pathops/SkReduceOrder.h"
  12. #include "tests/PathOpsExtendedTest.h"
  13. #include "tests/PathOpsTestCommon.h"
  14. #include "tests/Test.h"
  15. #include <utility>
  16. static struct lineConic {
  17. ConicPts conic;
  18. SkDLine line;
  19. int result;
  20. SkDPoint expected[2];
  21. } lineConicTests[] = {
  22. {
  23. {{{{30.6499996,25.6499996}, {30.6499996,20.6499996}, {25.6499996,20.6499996}}}, 0.707107008f},
  24. {{{25.6499996,20.6499996}, {45.6500015,20.6499996}}},
  25. 1,
  26. {{25.6499996,20.6499996}, {0,0}}
  27. },
  28. };
  29. static size_t lineConicTests_count = SK_ARRAY_COUNT(lineConicTests);
  30. static int doIntersect(SkIntersections& intersections, const SkDConic& conic, const SkDLine& line,
  31. bool& flipped) {
  32. int result;
  33. flipped = false;
  34. if (line[0].fX == line[1].fX) {
  35. double top = line[0].fY;
  36. double bottom = line[1].fY;
  37. flipped = top > bottom;
  38. if (flipped) {
  39. using std::swap;
  40. swap(top, bottom);
  41. }
  42. result = intersections.vertical(conic, top, bottom, line[0].fX, flipped);
  43. } else if (line[0].fY == line[1].fY) {
  44. double left = line[0].fX;
  45. double right = line[1].fX;
  46. flipped = left > right;
  47. if (flipped) {
  48. using std::swap;
  49. swap(left, right);
  50. }
  51. result = intersections.horizontal(conic, left, right, line[0].fY, flipped);
  52. } else {
  53. intersections.intersect(conic, line);
  54. result = intersections.used();
  55. }
  56. return result;
  57. }
  58. static struct oneLineConic {
  59. ConicPts conic;
  60. SkDLine line;
  61. } oneOffs[] = {
  62. {{{{{30.6499996,25.6499996}, {30.6499996,20.6499996}, {25.6499996,20.6499996}}}, 0.707107008f},
  63. {{{25.6499996,20.6499996}, {45.6500015,20.6499996}}}}
  64. };
  65. static size_t oneOffs_count = SK_ARRAY_COUNT(oneOffs);
  66. static void testOneOffs(skiatest::Reporter* reporter) {
  67. bool flipped = false;
  68. for (size_t index = 0; index < oneOffs_count; ++index) {
  69. const ConicPts& c = oneOffs[index].conic;
  70. SkDConic conic;
  71. conic.debugSet(c.fPts.fPts, c.fWeight);
  72. SkASSERT(ValidConic(conic));
  73. const SkDLine& line = oneOffs[index].line;
  74. SkASSERT(ValidLine(line));
  75. SkIntersections intersections;
  76. int result = doIntersect(intersections, conic, line, flipped);
  77. for (int inner = 0; inner < result; ++inner) {
  78. double conicT = intersections[0][inner];
  79. SkDPoint conicXY = conic.ptAtT(conicT);
  80. double lineT = intersections[1][inner];
  81. SkDPoint lineXY = line.ptAtT(lineT);
  82. if (!conicXY.approximatelyEqual(lineXY)) {
  83. conicXY.approximatelyEqual(lineXY);
  84. SkDebugf("");
  85. }
  86. REPORTER_ASSERT(reporter, conicXY.approximatelyEqual(lineXY));
  87. }
  88. }
  89. }
  90. DEF_TEST(PathOpsConicLineIntersectionOneOff, reporter) {
  91. testOneOffs(reporter);
  92. }
  93. DEF_TEST(PathOpsConicLineIntersection, reporter) {
  94. for (size_t index = 0; index < lineConicTests_count; ++index) {
  95. int iIndex = static_cast<int>(index);
  96. const ConicPts& c = lineConicTests[index].conic;
  97. SkDConic conic;
  98. conic.debugSet(c.fPts.fPts, c.fWeight);
  99. SkASSERT(ValidConic(conic));
  100. const SkDLine& line = lineConicTests[index].line;
  101. SkASSERT(ValidLine(line));
  102. SkReduceOrder reducer;
  103. SkPoint pts[3] = { conic.fPts.fPts[0].asSkPoint(), conic.fPts.fPts[1].asSkPoint(),
  104. conic.fPts.fPts[2].asSkPoint() };
  105. SkPoint reduced[3];
  106. SkConic floatConic;
  107. floatConic.set(pts, conic.fWeight);
  108. SkPath::Verb order1 = SkReduceOrder::Conic(floatConic, reduced);
  109. if (order1 != SkPath::kConic_Verb) {
  110. SkDebugf("%s [%d] conic verb=%d\n", __FUNCTION__, iIndex, order1);
  111. REPORTER_ASSERT(reporter, 0);
  112. }
  113. int order2 = reducer.reduce(line);
  114. if (order2 < 2) {
  115. SkDebugf("%s [%d] line order=%d\n", __FUNCTION__, iIndex, order2);
  116. REPORTER_ASSERT(reporter, 0);
  117. }
  118. SkIntersections intersections;
  119. bool flipped = false;
  120. int result = doIntersect(intersections, conic, line, flipped);
  121. REPORTER_ASSERT(reporter, result == lineConicTests[index].result);
  122. if (intersections.used() <= 0) {
  123. continue;
  124. }
  125. for (int pt = 0; pt < result; ++pt) {
  126. double tt1 = intersections[0][pt];
  127. REPORTER_ASSERT(reporter, tt1 >= 0 && tt1 <= 1);
  128. SkDPoint t1 = conic.ptAtT(tt1);
  129. double tt2 = intersections[1][pt];
  130. REPORTER_ASSERT(reporter, tt2 >= 0 && tt2 <= 1);
  131. SkDPoint t2 = line.ptAtT(tt2);
  132. if (!t1.approximatelyEqual(t2)) {
  133. SkDebugf("%s [%d,%d] x!= t1=%1.9g (%1.9g,%1.9g) t2=%1.9g (%1.9g,%1.9g)\n",
  134. __FUNCTION__, iIndex, pt, tt1, t1.fX, t1.fY, tt2, t2.fX, t2.fY);
  135. REPORTER_ASSERT(reporter, 0);
  136. }
  137. if (!t1.approximatelyEqual(lineConicTests[index].expected[0])
  138. && (lineConicTests[index].result == 1
  139. || !t1.approximatelyEqual(lineConicTests[index].expected[1]))) {
  140. SkDebugf("%s t1=(%1.9g,%1.9g)\n", __FUNCTION__, t1.fX, t1.fY);
  141. REPORTER_ASSERT(reporter, 0);
  142. }
  143. }
  144. }
  145. }