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