blob: 0a04747bbba8020039b31b2930d07cf87c866734 [file] [log] [blame]
caryclark@google.comb45a1b42012-05-18 20:50:33 +00001/*
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.comd6176b02012-08-23 18:14:13 +00007
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#define DEBUG_TEST 1
rmistry@google.comd6176b02012-08-23 18:14:13 +00009
caryclark@google.comb45a1b42012-05-18 20:50:33 +000010#include "Simplify.h"
11
12namespace SimplifyFindNextTest {
13
14#include "Simplify.cpp"
15
16} // end of SimplifyFindNextTest namespace
17
18#include "Intersection_Tests.h"
19
20static const SimplifyFindNextTest::Segment* testCommon(
caryclark@google.com27c449a2012-07-27 18:26:38 +000021 int contourWinding, int spanWinding, int startIndex, int endIndex,
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000022 SkTArray<SimplifyFindNextTest::Contour>& contours) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000023 SkTDArray<SimplifyFindNextTest::Contour*> contourList;
caryclark@google.com4eeda372012-12-06 21:47:48 +000024 makeContourList(contours, contourList, false, false);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000025 addIntersectTs(contourList[0], contourList[0]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000026 if (contours.count() > 1) {
27 SkASSERT(contours.count() == 2);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000028 addIntersectTs(contourList[0], contourList[1]);
29 addIntersectTs(contourList[1], contourList[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000030 }
31 fixOtherTIndex(contourList);
caryclark@google.com8dcf1142012-07-02 20:27:02 +000032 SimplifyFindNextTest::Segment& segment = contours[0].debugSegments()[0];
caryclark@google.com1577e8f2012-05-22 17:01:14 +000033 SkPoint pts[2];
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000034 pts[0] = segment.xyAtT(&segment.span(endIndex));
caryclark@google.com24bec792012-08-20 12:43:57 +000035 int nextStart = startIndex;
36 int nextEnd = endIndex;
caryclark@google.comfa4a6e92012-07-11 17:52:32 +000037 SkTDArray<SimplifyFindNextTest::Span*> chaseArray;
caryclark@google.comc91dfe42012-10-16 12:06:27 +000038 bool unsortable = false;
caryclark@google.com24bec792012-08-20 12:43:57 +000039 SimplifyFindNextTest::Segment* next = segment.findNextWinding(chaseArray,
caryclark@google.com8f9f4682013-01-03 21:18:16 +000040 nextStart, nextEnd, unsortable);
caryclark@google.com88f7d0c2012-06-07 21:09:20 +000041 pts[1] = next->xyAtT(&next->span(nextStart));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000042 SkASSERT(pts[0] == pts[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000043 return next;
44}
45
46static void test(const SkPath& path) {
47 SkTArray<SimplifyFindNextTest::Contour> contours;
48 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.com27c449a2012-07-27 18:26:38 +000049 int contourWinding = 0;
50 int spanWinding = 1;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000051 int start = 0;
52 int end = 1;
caryclark@google.com27c449a2012-07-27 18:26:38 +000053 testCommon(contourWinding, spanWinding, start, end, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000054}
55
56static void test(const SkPath& path, int start, int end) {
57 SkTArray<SimplifyFindNextTest::Contour> contours;
58 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.com27c449a2012-07-27 18:26:38 +000059 int contourWinding = 0;
60 int spanWinding = 1;
61 testCommon(contourWinding, spanWinding, start, end, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000062}
63
64static void testLine1() {
65 SkPath path;
66 path.moveTo(2,0);
67 path.lineTo(1,1);
68 path.lineTo(0,0);
69 path.close();
70 test(path);
71}
72
caryclark@google.com1577e8f2012-05-22 17:01:14 +000073static void addInnerCWTriangle(SkPath& path) {
74 path.moveTo(3,0);
75 path.lineTo(4,1);
76 path.lineTo(2,1);
77 path.close();
78}
79
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000080#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000081static void addInnerCCWTriangle(SkPath& path) {
82 path.moveTo(3,0);
83 path.lineTo(2,1);
84 path.lineTo(4,1);
85 path.close();
86}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000087#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000088
89static void addOuterCWTriangle(SkPath& path) {
90 path.moveTo(3,0);
91 path.lineTo(6,2);
92 path.lineTo(0,2);
93 path.close();
94}
95
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000096#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000097static void addOuterCCWTriangle(SkPath& path) {
98 path.moveTo(3,0);
99 path.lineTo(0,2);
100 path.lineTo(6,2);
101 path.close();
102}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000103#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000104
105static void testLine2() {
106 SkPath path;
107 addInnerCWTriangle(path);
108 addOuterCWTriangle(path);
109 test(path, 0, 3);
110}
111
112static void testLine3() {
113 SkPath path;
114 addInnerCWTriangle(path);
115 addOuterCWTriangle(path);
116 test(path, 3, 0);
117}
118
119static void testLine4() {
120 SkPath path;
121 addInnerCWTriangle(path);
122 addOuterCWTriangle(path);
123 test(path, 3, 2);
124}
125
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000126static void (*tests[])() = {
127 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000128 testLine2,
129 testLine3,
130 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000131};
132
133static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
134
135static void (*firstTest)() = 0;
136static bool skipAll = false;
137
138void SimplifyFindNext_Test() {
139 if (skipAll) {
140 return;
141 }
142 size_t index = 0;
143 if (firstTest) {
144 while (index < testCount && tests[index] != firstTest) {
145 ++index;
146 }
147 }
148 bool firstTestComplete = false;
149 for ( ; index < testCount; ++index) {
150 (*tests[index])();
151 firstTestComplete = true;
152 }
153}