blob: 3abcee15819fb4086c96ed9ab492de02e9c07a4b [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.com4eeda372012-12-06 21:47:48 +000022 makeContourList(contours, contourList, false, false);
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.comfb51afb2012-10-19 15:54:16 +000030#if SORTABLE_CONTOURS // old way
caryclark@google.com534aa5b2012-08-02 20:08:21 +000031 SimplifyFindTopTest::Segment* topStart = findTopContour(contourList);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000032 const SimplifyFindTopTest::Segment* topSegment = topStart->findTop(index,
caryclark@google.com1577e8f2012-05-22 17:01:14 +000033 end);
caryclark@google.comfb51afb2012-10-19 15:54:16 +000034#else
caryclark@google.comf839c032012-10-26 21:03:50 +000035 SkPoint bestXY = {SK_ScalarMin, SK_ScalarMin};
36 const SimplifyFindTopTest::Segment* topSegment =
37 findSortableTop(contourList, index, end, bestXY);
caryclark@google.comfb51afb2012-10-19 15:54:16 +000038#endif
caryclark@google.comb45a1b42012-05-18 20:50:33 +000039 return topSegment;
40}
41
42static void test(const SkPath& path) {
43 SkTArray<SimplifyFindTopTest::Contour> contours;
44 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000045 int index, end;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000046 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000047 SkASSERT(index + 1 == end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000048}
49
50static void test(const SkPath& path, SkScalar x1, SkScalar y1,
51 SkScalar x2, SkScalar y2) {
52 SkTArray<SimplifyFindTopTest::Contour> contours;
53 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000054 int index, end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +000055 const SimplifyFindTopTest::Segment* topSegment =
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000056 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000057 SkPoint pts[2];
58 double firstT = topSegment->t(index);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000059 pts[0] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000060 int direction = index < end ? 1 : -1;
61 do {
62 index += direction;
63 double nextT = topSegment->t(index);
64 if (nextT == firstT) {
65 continue;
66 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000067 pts[1] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000068 if (pts[0] != pts[1]) {
69 break;
70 }
71 } while (true);
72 SkASSERT(pts[0].fX == x1);
73 SkASSERT(pts[0].fY == y1);
74 SkASSERT(pts[1].fX == x2);
75 SkASSERT(pts[1].fY == y2);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000076}
77
78static void testLine1() {
79 SkPath path;
80 path.moveTo(2,0);
81 path.lineTo(1,1);
82 path.lineTo(0,0);
83 path.close();
84 test(path);
85}
86
87static void addInnerCWTriangle(SkPath& path) {
88 path.moveTo(3,0);
89 path.lineTo(4,1);
90 path.lineTo(2,1);
91 path.close();
92}
93
94static void addInnerCCWTriangle(SkPath& path) {
95 path.moveTo(3,0);
96 path.lineTo(2,1);
97 path.lineTo(4,1);
98 path.close();
99}
100
101static void addOuterCWTriangle(SkPath& path) {
102 path.moveTo(3,0);
103 path.lineTo(6,2);
104 path.lineTo(0,2);
105 path.close();
106}
107
108static void addOuterCCWTriangle(SkPath& path) {
109 path.moveTo(3,0);
110 path.lineTo(0,2);
111 path.lineTo(6,2);
112 path.close();
113}
114
115static void testLine2() {
116 SkPath path;
117 addInnerCWTriangle(path);
118 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000119 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000120}
121
122static void testLine3() {
123 SkPath path;
124 addOuterCWTriangle(path);
125 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000126 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000127}
128
129static void testLine4() {
130 SkPath path;
131 addInnerCCWTriangle(path);
132 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000133 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000134}
135
136static void testLine5() {
137 SkPath path;
138 addOuterCWTriangle(path);
139 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000140 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000141}
142
143static void testLine6() {
144 SkPath path;
145 addInnerCWTriangle(path);
146 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000147 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000148}
149
150static void testLine7() {
151 SkPath path;
152 addOuterCCWTriangle(path);
153 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000154 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000155}
156
157static void testLine8() {
158 SkPath path;
159 addInnerCCWTriangle(path);
160 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000161 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000162}
163
164static void testLine9() {
165 SkPath path;
166 addOuterCCWTriangle(path);
167 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000168 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000169}
170
171static void testQuads() {
172 SkPath path;
173 path.moveTo(2,0);
174 path.quadTo(1,1, 0,0);
175 path.close();
176 test(path);
177}
178
179static void testCubics() {
180 SkPath path;
181 path.moveTo(2,0);
182 path.cubicTo(2,3, 1,1, 0,0);
183 path.close();
184 test(path);
185}
186
187static void (*tests[])() = {
188 testLine1,
189 testLine2,
190 testLine3,
191 testLine4,
192 testLine5,
193 testLine6,
194 testLine7,
195 testLine8,
196 testLine9,
197 testQuads,
198 testCubics
199};
200
201static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
202
203static void (*firstTest)() = 0;
204static bool skipAll = false;
205
206void SimplifyFindTop_Test() {
207 if (skipAll) {
208 return;
209 }
210 size_t index = 0;
211 if (firstTest) {
212 while (index < testCount && tests[index] != firstTest) {
213 ++index;
214 }
215 }
216 bool firstTestComplete = false;
217 for ( ; index < testCount; ++index) {
218 (*tests[index])();
219 firstTestComplete = true;
220 }
221}