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