caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +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 | |
| 8 | #include "Simplify.h" |
| 9 | |
| 10 | namespace SimplifyFindTopTest { |
| 11 | |
| 12 | #include "Simplify.cpp" |
| 13 | |
| 14 | } // end of SimplifyFindTopTest namespace |
| 15 | |
| 16 | #include "Intersection_Tests.h" |
| 17 | |
| 18 | static const SimplifyFindTopTest::Segment* testCommon( |
| 19 | SkTArray<SimplifyFindTopTest::Contour>& contours, |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 20 | int& index, int& end) { |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 21 | SkTDArray<SimplifyFindTopTest::Contour*> contourList; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 22 | makeContourList(contours, contourList); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 23 | addIntersectTs(contourList[0], contourList[0]); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 24 | if (contours.count() > 1) { |
| 25 | SkASSERT(contours.count() == 2); |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 26 | addIntersectTs(contourList[0], contourList[1]); |
| 27 | addIntersectTs(contourList[1], contourList[1]); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 28 | } |
| 29 | fixOtherTIndex(contourList); |
caryclark@google.com | 534aa5b | 2012-08-02 20:08:21 +0000 | [diff] [blame] | 30 | SimplifyFindTopTest::Segment* topStart = findTopContour(contourList); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 31 | const SimplifyFindTopTest::Segment* topSegment = topStart->findTop(index, |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 32 | end); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 33 | return topSegment; |
| 34 | } |
| 35 | |
| 36 | static void test(const SkPath& path) { |
| 37 | SkTArray<SimplifyFindTopTest::Contour> contours; |
| 38 | SimplifyFindTopTest::EdgeBuilder builder(path, contours); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 39 | int index, end; |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 40 | testCommon(contours, index, end); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 41 | SkASSERT(index + 1 == end); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static void test(const SkPath& path, SkScalar x1, SkScalar y1, |
| 45 | SkScalar x2, SkScalar y2) { |
| 46 | SkTArray<SimplifyFindTopTest::Contour> contours; |
| 47 | SimplifyFindTopTest::EdgeBuilder builder(path, contours); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 48 | int index, end; |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 49 | const SimplifyFindTopTest::Segment* topSegment = |
caryclark@google.com | 65f9f0a | 2012-05-23 18:09:25 +0000 | [diff] [blame] | 50 | testCommon(contours, index, end); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 51 | SkPoint pts[2]; |
| 52 | double firstT = topSegment->t(index); |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 53 | pts[0] = topSegment->xyAtT(&topSegment->span(index)); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 54 | int direction = index < end ? 1 : -1; |
| 55 | do { |
| 56 | index += direction; |
| 57 | double nextT = topSegment->t(index); |
| 58 | if (nextT == firstT) { |
| 59 | continue; |
| 60 | } |
caryclark@google.com | a3f05fa | 2012-06-01 17:44:28 +0000 | [diff] [blame] | 61 | pts[1] = topSegment->xyAtT(&topSegment->span(index)); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 62 | if (pts[0] != pts[1]) { |
| 63 | break; |
| 64 | } |
| 65 | } while (true); |
| 66 | SkASSERT(pts[0].fX == x1); |
| 67 | SkASSERT(pts[0].fY == y1); |
| 68 | SkASSERT(pts[1].fX == x2); |
| 69 | SkASSERT(pts[1].fY == y2); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static void testLine1() { |
| 73 | SkPath path; |
| 74 | path.moveTo(2,0); |
| 75 | path.lineTo(1,1); |
| 76 | path.lineTo(0,0); |
| 77 | path.close(); |
| 78 | test(path); |
| 79 | } |
| 80 | |
| 81 | static void addInnerCWTriangle(SkPath& path) { |
| 82 | path.moveTo(3,0); |
| 83 | path.lineTo(4,1); |
| 84 | path.lineTo(2,1); |
| 85 | path.close(); |
| 86 | } |
| 87 | |
| 88 | static void addInnerCCWTriangle(SkPath& path) { |
| 89 | path.moveTo(3,0); |
| 90 | path.lineTo(2,1); |
| 91 | path.lineTo(4,1); |
| 92 | path.close(); |
| 93 | } |
| 94 | |
| 95 | static void addOuterCWTriangle(SkPath& path) { |
| 96 | path.moveTo(3,0); |
| 97 | path.lineTo(6,2); |
| 98 | path.lineTo(0,2); |
| 99 | path.close(); |
| 100 | } |
| 101 | |
| 102 | static void addOuterCCWTriangle(SkPath& path) { |
| 103 | path.moveTo(3,0); |
| 104 | path.lineTo(0,2); |
| 105 | path.lineTo(6,2); |
| 106 | path.close(); |
| 107 | } |
| 108 | |
| 109 | static void testLine2() { |
| 110 | SkPath path; |
| 111 | addInnerCWTriangle(path); |
| 112 | addOuterCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 113 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void testLine3() { |
| 117 | SkPath path; |
| 118 | addOuterCWTriangle(path); |
| 119 | addInnerCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 120 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static void testLine4() { |
| 124 | SkPath path; |
| 125 | addInnerCCWTriangle(path); |
| 126 | addOuterCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 127 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | static void testLine5() { |
| 131 | SkPath path; |
| 132 | addOuterCWTriangle(path); |
| 133 | addInnerCCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 134 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | static void testLine6() { |
| 138 | SkPath path; |
| 139 | addInnerCWTriangle(path); |
| 140 | addOuterCCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 141 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | static void testLine7() { |
| 145 | SkPath path; |
| 146 | addOuterCCWTriangle(path); |
| 147 | addInnerCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 148 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void testLine8() { |
| 152 | SkPath path; |
| 153 | addInnerCCWTriangle(path); |
| 154 | addOuterCCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 155 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void testLine9() { |
| 159 | SkPath path; |
| 160 | addOuterCCWTriangle(path); |
| 161 | addInnerCCWTriangle(path); |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame] | 162 | test(path, 0, 2, 3, 0); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static void testQuads() { |
| 166 | SkPath path; |
| 167 | path.moveTo(2,0); |
| 168 | path.quadTo(1,1, 0,0); |
| 169 | path.close(); |
| 170 | test(path); |
| 171 | } |
| 172 | |
| 173 | static void testCubics() { |
| 174 | SkPath path; |
| 175 | path.moveTo(2,0); |
| 176 | path.cubicTo(2,3, 1,1, 0,0); |
| 177 | path.close(); |
| 178 | test(path); |
| 179 | } |
| 180 | |
| 181 | static void (*tests[])() = { |
| 182 | testLine1, |
| 183 | testLine2, |
| 184 | testLine3, |
| 185 | testLine4, |
| 186 | testLine5, |
| 187 | testLine6, |
| 188 | testLine7, |
| 189 | testLine8, |
| 190 | testLine9, |
| 191 | testQuads, |
| 192 | testCubics |
| 193 | }; |
| 194 | |
| 195 | static const size_t testCount = sizeof(tests) / sizeof(tests[0]); |
| 196 | |
| 197 | static void (*firstTest)() = 0; |
| 198 | static bool skipAll = false; |
| 199 | |
| 200 | void SimplifyFindTop_Test() { |
| 201 | if (skipAll) { |
| 202 | return; |
| 203 | } |
| 204 | size_t index = 0; |
| 205 | if (firstTest) { |
| 206 | while (index < testCount && tests[index] != firstTest) { |
| 207 | ++index; |
| 208 | } |
| 209 | } |
| 210 | bool firstTestComplete = false; |
| 211 | for ( ; index < testCount; ++index) { |
| 212 | (*tests[index])(); |
| 213 | firstTestComplete = true; |
| 214 | } |
| 215 | } |