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