caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 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 | */ |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 7 | #include "src/pathops/SkIntersections.h" |
| 8 | #include "src/pathops/SkPathOpsRect.h" |
| 9 | #include "src/pathops/SkReduceOrder.h" |
| 10 | #include "tests/PathOpsCubicIntersectionTestData.h" |
| 11 | #include "tests/PathOpsQuadIntersectionTestData.h" |
| 12 | #include "tests/PathOpsTestCommon.h" |
| 13 | #include "tests/Test.h" |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 14 | |
John Stiles | 9765973 | 2021-08-12 10:48:09 -0400 | [diff] [blame] | 15 | using namespace PathOpsCubicIntersectionTestData; |
| 16 | |
caryclark@google.com | 02352d1 | 2013-11-25 15:05:05 +0000 | [diff] [blame] | 17 | #if 0 // disable test until stroke reduction is supported |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 18 | static bool controls_inside(const SkDCubic& cubic) { |
| 19 | return between(cubic[0].fX, cubic[1].fX, cubic[3].fX) |
| 20 | && between(cubic[0].fX, cubic[2].fX, cubic[3].fX) |
| 21 | && between(cubic[0].fY, cubic[1].fY, cubic[3].fY) |
| 22 | && between(cubic[0].fY, cubic[2].fY, cubic[3].fY); |
| 23 | } |
| 24 | |
| 25 | static bool tiny(const SkDCubic& cubic) { |
| 26 | int index, minX, maxX, minY, maxY; |
| 27 | minX = maxX = minY = maxY = 0; |
| 28 | for (index = 1; index < 4; ++index) { |
| 29 | if (cubic[minX].fX > cubic[index].fX) { |
| 30 | minX = index; |
| 31 | } |
| 32 | if (cubic[minY].fY > cubic[index].fY) { |
| 33 | minY = index; |
| 34 | } |
| 35 | if (cubic[maxX].fX < cubic[index].fX) { |
| 36 | maxX = index; |
| 37 | } |
| 38 | if (cubic[maxY].fY < cubic[index].fY) { |
| 39 | maxY = index; |
| 40 | } |
| 41 | } |
| 42 | return approximately_equal(cubic[maxX].fX, cubic[minX].fX) |
| 43 | && approximately_equal(cubic[maxY].fY, cubic[minY].fY); |
| 44 | } |
| 45 | |
| 46 | static void find_tight_bounds(const SkDCubic& cubic, SkDRect& bounds) { |
| 47 | SkDCubicPair cubicPair = cubic.chopAt(0.5); |
| 48 | if (!tiny(cubicPair.first()) && !controls_inside(cubicPair.first())) { |
| 49 | find_tight_bounds(cubicPair.first(), bounds); |
| 50 | } else { |
| 51 | bounds.add(cubicPair.first()[0]); |
| 52 | bounds.add(cubicPair.first()[3]); |
| 53 | } |
| 54 | if (!tiny(cubicPair.second()) && !controls_inside(cubicPair.second())) { |
| 55 | find_tight_bounds(cubicPair.second(), bounds); |
| 56 | } else { |
| 57 | bounds.add(cubicPair.second()[0]); |
| 58 | bounds.add(cubicPair.second()[3]); |
| 59 | } |
| 60 | } |
caryclark@google.com | 111417a | 2013-11-25 14:36:58 +0000 | [diff] [blame] | 61 | #endif |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 62 | |
tfarina@chromium.org | 78e7b4e | 2014-01-02 21:45:03 +0000 | [diff] [blame] | 63 | DEF_TEST(PathOpsReduceOrderCubic, reporter) { |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 64 | size_t index; |
| 65 | SkReduceOrder reducer; |
| 66 | int order; |
| 67 | enum { |
| 68 | RunAll, |
| 69 | RunPointDegenerates, |
| 70 | RunNotPointDegenerates, |
| 71 | RunLines, |
| 72 | RunNotLines, |
| 73 | RunModEpsilonLines, |
| 74 | RunLessEpsilonLines, |
| 75 | RunNegEpsilonLines, |
| 76 | RunQuadraticLines, |
| 77 | RunQuadraticPoints, |
| 78 | RunQuadraticModLines, |
| 79 | RunComputedLines, |
| 80 | RunNone |
| 81 | } run = RunAll; |
| 82 | int firstTestIndex = 0; |
| 83 | #if 0 |
| 84 | run = RunComputedLines; |
| 85 | firstTestIndex = 18; |
| 86 | #endif |
| 87 | int firstPointDegeneratesTest = run == RunAll ? 0 : run == RunPointDegenerates |
| 88 | ? firstTestIndex : SK_MaxS32; |
| 89 | int firstNotPointDegeneratesTest = run == RunAll ? 0 : run == RunNotPointDegenerates |
| 90 | ? firstTestIndex : SK_MaxS32; |
| 91 | int firstLinesTest = run == RunAll ? 0 : run == RunLines ? firstTestIndex : SK_MaxS32; |
| 92 | int firstNotLinesTest = run == RunAll ? 0 : run == RunNotLines ? firstTestIndex : SK_MaxS32; |
| 93 | int firstModEpsilonTest = run == RunAll ? 0 : run == RunModEpsilonLines |
| 94 | ? firstTestIndex : SK_MaxS32; |
| 95 | int firstLessEpsilonTest = run == RunAll ? 0 : run == RunLessEpsilonLines |
| 96 | ? firstTestIndex : SK_MaxS32; |
| 97 | int firstNegEpsilonTest = run == RunAll ? 0 : run == RunNegEpsilonLines |
| 98 | ? firstTestIndex : SK_MaxS32; |
| 99 | int firstQuadraticPointTest = run == RunAll ? 0 : run == RunQuadraticPoints |
| 100 | ? firstTestIndex : SK_MaxS32; |
| 101 | int firstQuadraticLineTest = run == RunAll ? 0 : run == RunQuadraticLines |
| 102 | ? firstTestIndex : SK_MaxS32; |
| 103 | int firstQuadraticModLineTest = run == RunAll ? 0 : run == RunQuadraticModLines |
| 104 | ? firstTestIndex : SK_MaxS32; |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 105 | #if 0 |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 106 | int firstComputedLinesTest = run == RunAll ? 0 : run == RunComputedLines |
| 107 | ? firstTestIndex : SK_MaxS32; |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 108 | #endif |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 109 | for (index = firstPointDegeneratesTest; index < pointDegenerates_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 110 | const CubicPts& c = pointDegenerates[index]; |
| 111 | SkDCubic cubic; |
| 112 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 113 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 114 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 115 | if (order != 1) { |
| 116 | SkDebugf("[%d] pointDegenerates order=%d\n", static_cast<int>(index), order); |
| 117 | REPORTER_ASSERT(reporter, 0); |
| 118 | } |
| 119 | } |
| 120 | for (index = firstNotPointDegeneratesTest; index < notPointDegenerates_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 121 | const CubicPts& c = notPointDegenerates[index]; |
| 122 | SkDCubic cubic; |
| 123 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 124 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 125 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 126 | if (order == 1) { |
| 127 | SkDebugf("[%d] notPointDegenerates order=%d\n", static_cast<int>(index), order); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 128 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 129 | REPORTER_ASSERT(reporter, 0); |
| 130 | } |
| 131 | } |
| 132 | for (index = firstLinesTest; index < lines_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 133 | const CubicPts& c = lines[index]; |
| 134 | SkDCubic cubic; |
| 135 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 136 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 137 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 138 | if (order != 2) { |
| 139 | SkDebugf("[%d] lines order=%d\n", static_cast<int>(index), order); |
| 140 | REPORTER_ASSERT(reporter, 0); |
| 141 | } |
| 142 | } |
| 143 | for (index = firstNotLinesTest; index < notLines_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 144 | const CubicPts& c = notLines[index]; |
| 145 | SkDCubic cubic; |
| 146 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 147 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 148 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 149 | if (order == 2) { |
| 150 | SkDebugf("[%d] notLines order=%d\n", static_cast<int>(index), order); |
| 151 | REPORTER_ASSERT(reporter, 0); |
| 152 | } |
| 153 | } |
| 154 | for (index = firstModEpsilonTest; index < modEpsilonLines_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 155 | const CubicPts& c = modEpsilonLines[index]; |
| 156 | SkDCubic cubic; |
| 157 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 158 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 159 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 160 | if (order == 2) { |
| 161 | SkDebugf("[%d] line mod by epsilon order=%d\n", static_cast<int>(index), order); |
| 162 | REPORTER_ASSERT(reporter, 0); |
| 163 | } |
| 164 | } |
| 165 | for (index = firstLessEpsilonTest; index < lessEpsilonLines_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 166 | const CubicPts& c = lessEpsilonLines[index]; |
| 167 | SkDCubic cubic; |
| 168 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 169 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 170 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 171 | if (order != 2) { |
| 172 | SkDebugf("[%d] line less by epsilon/2 order=%d\n", static_cast<int>(index), order); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 173 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 174 | REPORTER_ASSERT(reporter, 0); |
| 175 | } |
| 176 | } |
| 177 | for (index = firstNegEpsilonTest; index < negEpsilonLines_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 178 | const CubicPts& c = negEpsilonLines[index]; |
| 179 | SkDCubic cubic; |
| 180 | cubic.debugSet(c.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 181 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 182 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 183 | if (order != 2) { |
| 184 | SkDebugf("[%d] line neg by epsilon/2 order=%d\n", static_cast<int>(index), order); |
| 185 | REPORTER_ASSERT(reporter, 0); |
| 186 | } |
| 187 | } |
| 188 | for (index = firstQuadraticPointTest; index < quadraticPoints_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 189 | const QuadPts& q = quadraticPoints[index]; |
| 190 | SkDQuad quad; |
| 191 | quad.debugSet(q.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 192 | SkASSERT(ValidQuad(quad)); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 193 | SkDCubic cubic = quad.debugToCubic(); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 194 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 195 | if (order != 1) { |
| 196 | SkDebugf("[%d] point quad order=%d\n", static_cast<int>(index), order); |
| 197 | REPORTER_ASSERT(reporter, 0); |
| 198 | } |
| 199 | } |
| 200 | for (index = firstQuadraticLineTest; index < quadraticLines_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 201 | const QuadPts& q = quadraticLines[index]; |
| 202 | SkDQuad quad; |
| 203 | quad.debugSet(q.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 204 | SkASSERT(ValidQuad(quad)); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 205 | SkDCubic cubic = quad.debugToCubic(); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 206 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 207 | if (order != 2) { |
| 208 | SkDebugf("[%d] line quad order=%d\n", static_cast<int>(index), order); |
| 209 | REPORTER_ASSERT(reporter, 0); |
| 210 | } |
| 211 | } |
| 212 | for (index = firstQuadraticModLineTest; index < quadraticModEpsilonLines_count; ++index) { |
caryclark | a35ab3e | 2016-10-20 08:32:18 -0700 | [diff] [blame] | 213 | const QuadPts& q = quadraticModEpsilonLines[index]; |
| 214 | SkDQuad quad; |
| 215 | quad.debugSet(q.fPts); |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 216 | SkASSERT(ValidQuad(quad)); |
caryclark | 624637c | 2015-05-11 07:21:27 -0700 | [diff] [blame] | 217 | SkDCubic cubic = quad.debugToCubic(); |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 218 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 219 | if (order != 3) { |
| 220 | SkDebugf("[%d] line mod quad order=%d\n", static_cast<int>(index), order); |
| 221 | REPORTER_ASSERT(reporter, 0); |
| 222 | } |
| 223 | } |
| 224 | |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 225 | #if 0 // disable test until stroke reduction is supported |
| 226 | // test if computed line end points are valid |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 227 | for (index = firstComputedLinesTest; index < lines_count; ++index) { |
| 228 | const SkDCubic& cubic = lines[index]; |
caryclark@google.com | 8d0a524 | 2013-07-16 16:11:16 +0000 | [diff] [blame] | 229 | SkASSERT(ValidCubic(cubic)); |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 230 | bool controlsInside = controls_inside(cubic); |
| 231 | order = reducer.reduce(cubic, SkReduceOrder::kAllow_Quadratics, |
| 232 | SkReduceOrder::kStroke_Style); |
| 233 | if (order == 2 && reducer.fLine[0] == reducer.fLine[1]) { |
| 234 | SkDebugf("[%d] line computed ends match order=%d\n", static_cast<int>(index), order); |
| 235 | REPORTER_ASSERT(reporter, 0); |
| 236 | } |
| 237 | if (controlsInside) { |
| 238 | if ( (reducer.fLine[0].fX != cubic[0].fX && reducer.fLine[0].fX != cubic[3].fX) |
| 239 | || (reducer.fLine[0].fY != cubic[0].fY && reducer.fLine[0].fY != cubic[3].fY) |
| 240 | || (reducer.fLine[1].fX != cubic[0].fX && reducer.fLine[1].fX != cubic[3].fX) |
| 241 | || (reducer.fLine[1].fY != cubic[0].fY && reducer.fLine[1].fY != cubic[3].fY)) { |
| 242 | SkDebugf("[%d] line computed ends order=%d\n", static_cast<int>(index), order); |
| 243 | REPORTER_ASSERT(reporter, 0); |
| 244 | } |
| 245 | } else { |
| 246 | // binary search for extrema, compare against actual results |
| 247 | // while a control point is outside of bounding box formed by end points, split |
| 248 | SkDRect bounds = {DBL_MAX, DBL_MAX, -DBL_MAX, -DBL_MAX}; |
| 249 | find_tight_bounds(cubic, bounds); |
| 250 | if ( (!AlmostEqualUlps(reducer.fLine[0].fX, bounds.fLeft) |
| 251 | && !AlmostEqualUlps(reducer.fLine[0].fX, bounds.fRight)) |
| 252 | || (!AlmostEqualUlps(reducer.fLine[0].fY, bounds.fTop) |
| 253 | && !AlmostEqualUlps(reducer.fLine[0].fY, bounds.fBottom)) |
| 254 | || (!AlmostEqualUlps(reducer.fLine[1].fX, bounds.fLeft) |
| 255 | && !AlmostEqualUlps(reducer.fLine[1].fX, bounds.fRight)) |
| 256 | || (!AlmostEqualUlps(reducer.fLine[1].fY, bounds.fTop) |
| 257 | && !AlmostEqualUlps(reducer.fLine[1].fY, bounds.fBottom))) { |
| 258 | SkDebugf("[%d] line computed tight bounds order=%d\n", static_cast<int>(index), order); |
| 259 | REPORTER_ASSERT(reporter, 0); |
| 260 | } |
| 261 | } |
| 262 | } |
caryclark@google.com | 927b702 | 2013-11-25 14:18:21 +0000 | [diff] [blame] | 263 | #endif |
caryclark@google.com | 9166dcb | 2013-04-08 11:50:00 +0000 | [diff] [blame] | 264 | } |