blob: 0c9c2e080345b8374a81fca45416e33a53581220 [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 int& index, int& end) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000021 SkTDArray<SimplifyFindTopTest::Contour*> contourList;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000022 makeContourList(contours, contourList);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000023 addIntersectTs(contourList[0], contourList[0]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000024 if (contours.count() > 1) {
25 SkASSERT(contours.count() == 2);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000026 addIntersectTs(contourList[0], contourList[1]);
27 addIntersectTs(contourList[1], contourList[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000028 }
29 fixOtherTIndex(contourList);
caryclark@google.com534aa5b2012-08-02 20:08:21 +000030 SimplifyFindTopTest::Segment* topStart = findTopContour(contourList);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000031 const SimplifyFindTopTest::Segment* topSegment = topStart->findTop(index,
caryclark@google.com1577e8f2012-05-22 17:01:14 +000032 end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000033 return topSegment;
34}
35
36static void test(const SkPath& path) {
37 SkTArray<SimplifyFindTopTest::Contour> contours;
38 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000039 int index, end;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000040 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000041 SkASSERT(index + 1 == end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000042}
43
44static void test(const SkPath& path, SkScalar x1, SkScalar y1,
45 SkScalar x2, SkScalar y2) {
46 SkTArray<SimplifyFindTopTest::Contour> contours;
47 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000048 int index, end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +000049 const SimplifyFindTopTest::Segment* topSegment =
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000050 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000051 SkPoint pts[2];
52 double firstT = topSegment->t(index);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000053 pts[0] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000054 int direction = index < end ? 1 : -1;
55 do {
56 index += direction;
57 double nextT = topSegment->t(index);
58 if (nextT == firstT) {
59 continue;
60 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000061 pts[1] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000062 if (pts[0] != pts[1]) {
63 break;
64 }
65 } while (true);
66 SkASSERT(pts[0].fX == x1);
67 SkASSERT(pts[0].fY == y1);
68 SkASSERT(pts[1].fX == x2);
69 SkASSERT(pts[1].fY == y2);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000070}
71
72static void testLine1() {
73 SkPath path;
74 path.moveTo(2,0);
75 path.lineTo(1,1);
76 path.lineTo(0,0);
77 path.close();
78 test(path);
79}
80
81static void addInnerCWTriangle(SkPath& path) {
82 path.moveTo(3,0);
83 path.lineTo(4,1);
84 path.lineTo(2,1);
85 path.close();
86}
87
88static void addInnerCCWTriangle(SkPath& path) {
89 path.moveTo(3,0);
90 path.lineTo(2,1);
91 path.lineTo(4,1);
92 path.close();
93}
94
95static void addOuterCWTriangle(SkPath& path) {
96 path.moveTo(3,0);
97 path.lineTo(6,2);
98 path.lineTo(0,2);
99 path.close();
100}
101
102static void addOuterCCWTriangle(SkPath& path) {
103 path.moveTo(3,0);
104 path.lineTo(0,2);
105 path.lineTo(6,2);
106 path.close();
107}
108
109static void testLine2() {
110 SkPath path;
111 addInnerCWTriangle(path);
112 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000113 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000114}
115
116static void testLine3() {
117 SkPath path;
118 addOuterCWTriangle(path);
119 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000120 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000121}
122
123static void testLine4() {
124 SkPath path;
125 addInnerCCWTriangle(path);
126 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000127 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000128}
129
130static void testLine5() {
131 SkPath path;
132 addOuterCWTriangle(path);
133 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000134 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000135}
136
137static void testLine6() {
138 SkPath path;
139 addInnerCWTriangle(path);
140 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000141 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000142}
143
144static void testLine7() {
145 SkPath path;
146 addOuterCCWTriangle(path);
147 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000148 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000149}
150
151static void testLine8() {
152 SkPath path;
153 addInnerCCWTriangle(path);
154 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000155 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000156}
157
158static void testLine9() {
159 SkPath path;
160 addOuterCCWTriangle(path);
161 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000162 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000163}
164
165static void testQuads() {
166 SkPath path;
167 path.moveTo(2,0);
168 path.quadTo(1,1, 0,0);
169 path.close();
170 test(path);
171}
172
173static void testCubics() {
174 SkPath path;
175 path.moveTo(2,0);
176 path.cubicTo(2,3, 1,1, 0,0);
177 path.close();
178 test(path);
179}
180
181static void (*tests[])() = {
182 testLine1,
183 testLine2,
184 testLine3,
185 testLine4,
186 testLine5,
187 testLine6,
188 testLine7,
189 testLine8,
190 testLine9,
191 testQuads,
192 testCubics
193};
194
195static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
196
197static void (*firstTest)() = 0;
198static bool skipAll = false;
199
200void SimplifyFindTop_Test() {
201 if (skipAll) {
202 return;
203 }
204 size_t index = 0;
205 if (firstTest) {
206 while (index < testCount && tests[index] != firstTest) {
207 ++index;
208 }
209 }
210 bool firstTestComplete = false;
211 for ( ; index < testCount; ++index) {
212 (*tests[index])();
213 firstTestComplete = true;
214 }
215}