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 SimplifyFindNextTest { |
| 11 | |
| 12 | #include "Simplify.cpp" |
| 13 | |
| 14 | } // end of SimplifyFindNextTest namespace |
| 15 | |
| 16 | #include "Intersection_Tests.h" |
| 17 | |
| 18 | static const SimplifyFindNextTest::Segment* testCommon( |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame^] | 19 | int winding, int startIndex, int endIndex, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 20 | SkTArray<SimplifyFindNextTest::Contour>& contours, |
| 21 | SimplifyFindNextTest::EdgeBuilder& builder, const SkPath& path) { |
| 22 | SkTDArray<SimplifyFindNextTest::Contour*> contourList; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame^] | 23 | makeContourList(contours, contourList); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 24 | addIntersectTs(contourList[0], contourList[0], -1); |
| 25 | if (contours.count() > 1) { |
| 26 | SkASSERT(contours.count() == 2); |
| 27 | addIntersectTs(contourList[0], contourList[1], -1); |
| 28 | addIntersectTs(contourList[1], contourList[1], -1); |
| 29 | } |
| 30 | fixOtherTIndex(contourList); |
| 31 | SimplifyFindNextTest::Segment& segment = contours[0].fSegments[0]; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame^] | 32 | SkPoint pts[2]; |
| 33 | double startT = segment.t(endIndex); |
| 34 | segment.xyAtT(startT, &pts[0]); |
| 35 | SimplifyFindNextTest::Segment* next = segment.findNext(winding, |
| 36 | startIndex, endIndex); |
| 37 | double endT = next->t(startIndex); |
| 38 | next->xyAtT(endT, &pts[1]); |
| 39 | SkASSERT(pts[0] == pts[1]); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 40 | return next; |
| 41 | } |
| 42 | |
| 43 | static void test(const SkPath& path) { |
| 44 | SkTArray<SimplifyFindNextTest::Contour> contours; |
| 45 | SimplifyFindNextTest::EdgeBuilder builder(path, contours); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 46 | int winding = 0; |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame^] | 47 | int start = 0; |
| 48 | int end = 1; |
| 49 | testCommon(winding, start, end, contours, builder, path); |
| 50 | } |
| 51 | |
| 52 | static void test(const SkPath& path, int start, int end) { |
| 53 | SkTArray<SimplifyFindNextTest::Contour> contours; |
| 54 | SimplifyFindNextTest::EdgeBuilder builder(path, contours); |
| 55 | int winding = 0; |
| 56 | testCommon(winding, start, end, contours, builder, path); |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static void testLine1() { |
| 60 | SkPath path; |
| 61 | path.moveTo(2,0); |
| 62 | path.lineTo(1,1); |
| 63 | path.lineTo(0,0); |
| 64 | path.close(); |
| 65 | test(path); |
| 66 | } |
| 67 | |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame^] | 68 | static void addInnerCWTriangle(SkPath& path) { |
| 69 | path.moveTo(3,0); |
| 70 | path.lineTo(4,1); |
| 71 | path.lineTo(2,1); |
| 72 | path.close(); |
| 73 | } |
| 74 | |
| 75 | static void addInnerCCWTriangle(SkPath& path) { |
| 76 | path.moveTo(3,0); |
| 77 | path.lineTo(2,1); |
| 78 | path.lineTo(4,1); |
| 79 | path.close(); |
| 80 | } |
| 81 | |
| 82 | static void addOuterCWTriangle(SkPath& path) { |
| 83 | path.moveTo(3,0); |
| 84 | path.lineTo(6,2); |
| 85 | path.lineTo(0,2); |
| 86 | path.close(); |
| 87 | } |
| 88 | |
| 89 | static void addOuterCCWTriangle(SkPath& path) { |
| 90 | path.moveTo(3,0); |
| 91 | path.lineTo(0,2); |
| 92 | path.lineTo(6,2); |
| 93 | path.close(); |
| 94 | } |
| 95 | |
| 96 | static void testLine2() { |
| 97 | SkPath path; |
| 98 | addInnerCWTriangle(path); |
| 99 | addOuterCWTriangle(path); |
| 100 | test(path, 0, 3); |
| 101 | } |
| 102 | |
| 103 | static void testLine3() { |
| 104 | SkPath path; |
| 105 | addInnerCWTriangle(path); |
| 106 | addOuterCWTriangle(path); |
| 107 | test(path, 3, 0); |
| 108 | } |
| 109 | |
| 110 | static void testLine4() { |
| 111 | SkPath path; |
| 112 | addInnerCWTriangle(path); |
| 113 | addOuterCWTriangle(path); |
| 114 | test(path, 3, 2); |
| 115 | } |
| 116 | |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 117 | static void (*tests[])() = { |
| 118 | testLine1, |
caryclark@google.com | 1577e8f | 2012-05-22 17:01:14 +0000 | [diff] [blame^] | 119 | testLine2, |
| 120 | testLine3, |
| 121 | testLine4, |
caryclark@google.com | b45a1b4 | 2012-05-18 20:50:33 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | static const size_t testCount = sizeof(tests) / sizeof(tests[0]); |
| 125 | |
| 126 | static void (*firstTest)() = 0; |
| 127 | static bool skipAll = false; |
| 128 | |
| 129 | void SimplifyFindNext_Test() { |
| 130 | if (skipAll) { |
| 131 | return; |
| 132 | } |
| 133 | size_t index = 0; |
| 134 | if (firstTest) { |
| 135 | while (index < testCount && tests[index] != firstTest) { |
| 136 | ++index; |
| 137 | } |
| 138 | } |
| 139 | bool firstTestComplete = false; |
| 140 | for ( ; index < testCount; ++index) { |
| 141 | (*tests[index])(); |
| 142 | firstTestComplete = true; |
| 143 | } |
| 144 | } |