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