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