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