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