blob: 3932f5abf8cebce1862da884852cf78572775e58 [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 */
7
8#include "Simplify.h"
9
10namespace SimplifyFindTopTest {
11
12#include "Simplify.cpp"
13
14} // end of SimplifyFindTopTest namespace
15
16#include "Intersection_Tests.h"
17
18static const SimplifyFindTopTest::Segment* testCommon(
19 SkTArray<SimplifyFindTopTest::Contour>& contours,
caryclark@google.com1577e8f2012-05-22 17:01:14 +000020 SimplifyFindTopTest::EdgeBuilder& builder, const SkPath& path,
21 int& index, int& end) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000022 SkTDArray<SimplifyFindTopTest::Contour*> contourList;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000023 makeContourList(contours, contourList);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000024 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 SimplifyFindTopTest::Segment* topStart = findTopContour(contourList,
32 contourList.count());
caryclark@google.comb45a1b42012-05-18 20:50:33 +000033 const SimplifyFindTopTest::Segment* topSegment = topStart->findTop(index,
caryclark@google.com1577e8f2012-05-22 17:01:14 +000034 end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000035 return topSegment;
36}
37
38static void test(const SkPath& path) {
39 SkTArray<SimplifyFindTopTest::Contour> contours;
40 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000041 int index, end;
42 testCommon(contours, builder, path, index, end);
43 SkASSERT(index + 1 == end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000044}
45
46static void test(const SkPath& path, SkScalar x1, SkScalar y1,
47 SkScalar x2, SkScalar y2) {
48 SkTArray<SimplifyFindTopTest::Contour> contours;
49 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000050 int index, end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +000051 const SimplifyFindTopTest::Segment* topSegment =
caryclark@google.com1577e8f2012-05-22 17:01:14 +000052 testCommon(contours, builder, path, index, end);
53 SkPoint pts[2];
54 double firstT = topSegment->t(index);
55 topSegment->xyAtT(firstT, &pts[0]);
56 int direction = index < end ? 1 : -1;
57 do {
58 index += direction;
59 double nextT = topSegment->t(index);
60 if (nextT == firstT) {
61 continue;
62 }
63 topSegment->xyAtT(nextT, &pts[1]);
64 if (pts[0] != pts[1]) {
65 break;
66 }
67 } while (true);
68 SkASSERT(pts[0].fX == x1);
69 SkASSERT(pts[0].fY == y1);
70 SkASSERT(pts[1].fX == x2);
71 SkASSERT(pts[1].fY == y2);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000072}
73
74static void testLine1() {
75 SkPath path;
76 path.moveTo(2,0);
77 path.lineTo(1,1);
78 path.lineTo(0,0);
79 path.close();
80 test(path);
81}
82
83static void addInnerCWTriangle(SkPath& path) {
84 path.moveTo(3,0);
85 path.lineTo(4,1);
86 path.lineTo(2,1);
87 path.close();
88}
89
90static void addInnerCCWTriangle(SkPath& path) {
91 path.moveTo(3,0);
92 path.lineTo(2,1);
93 path.lineTo(4,1);
94 path.close();
95}
96
97static void addOuterCWTriangle(SkPath& path) {
98 path.moveTo(3,0);
99 path.lineTo(6,2);
100 path.lineTo(0,2);
101 path.close();
102}
103
104static void addOuterCCWTriangle(SkPath& path) {
105 path.moveTo(3,0);
106 path.lineTo(0,2);
107 path.lineTo(6,2);
108 path.close();
109}
110
111static void testLine2() {
112 SkPath path;
113 addInnerCWTriangle(path);
114 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000115 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000116}
117
118static void testLine3() {
119 SkPath path;
120 addOuterCWTriangle(path);
121 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000122 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000123}
124
125static void testLine4() {
126 SkPath path;
127 addInnerCCWTriangle(path);
128 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000129 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000130}
131
132static void testLine5() {
133 SkPath path;
134 addOuterCWTriangle(path);
135 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000136 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000137}
138
139static void testLine6() {
140 SkPath path;
141 addInnerCWTriangle(path);
142 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000143 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000144}
145
146static void testLine7() {
147 SkPath path;
148 addOuterCCWTriangle(path);
149 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000150 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000151}
152
153static void testLine8() {
154 SkPath path;
155 addInnerCCWTriangle(path);
156 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000157 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000158}
159
160static void testLine9() {
161 SkPath path;
162 addOuterCCWTriangle(path);
163 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000164 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000165}
166
167static void testQuads() {
168 SkPath path;
169 path.moveTo(2,0);
170 path.quadTo(1,1, 0,0);
171 path.close();
172 test(path);
173}
174
175static void testCubics() {
176 SkPath path;
177 path.moveTo(2,0);
178 path.cubicTo(2,3, 1,1, 0,0);
179 path.close();
180 test(path);
181}
182
183static void (*tests[])() = {
184 testLine1,
185 testLine2,
186 testLine3,
187 testLine4,
188 testLine5,
189 testLine6,
190 testLine7,
191 testLine8,
192 testLine9,
193 testQuads,
194 testCubics
195};
196
197static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
198
199static void (*firstTest)() = 0;
200static bool skipAll = false;
201
202void SimplifyFindTop_Test() {
203 if (skipAll) {
204 return;
205 }
206 size_t index = 0;
207 if (firstTest) {
208 while (index < testCount && tests[index] != firstTest) {
209 ++index;
210 }
211 }
212 bool firstTestComplete = false;
213 for ( ; index < testCount; ++index) {
214 (*tests[index])();
215 firstTestComplete = true;
216 }
217}