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