blob: 5f267503a9c03bf03d89b50ff2dd5b0fa4905fbc [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};
caryclark@google.comdb0b3e02012-12-21 21:34:36 +000036 bool done, unsortable = false;
caryclark@google.comf839c032012-10-26 21:03:50 +000037 const SimplifyFindTopTest::Segment* topSegment =
caryclark@google.comdb0b3e02012-12-21 21:34:36 +000038 findSortableTop(contourList, index, end, bestXY, unsortable, done, true);
caryclark@google.comfb51afb2012-10-19 15:54:16 +000039#endif
caryclark@google.comb45a1b42012-05-18 20:50:33 +000040 return topSegment;
41}
42
43static void test(const SkPath& path) {
44 SkTArray<SimplifyFindTopTest::Contour> contours;
45 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000046 int index, end;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000047 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000048 SkASSERT(index + 1 == end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000049}
50
51static void test(const SkPath& path, SkScalar x1, SkScalar y1,
52 SkScalar x2, SkScalar y2) {
53 SkTArray<SimplifyFindTopTest::Contour> contours;
54 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000055 int index, end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +000056 const SimplifyFindTopTest::Segment* topSegment =
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000057 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000058 SkPoint pts[2];
59 double firstT = topSegment->t(index);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000060 pts[0] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000061 int direction = index < end ? 1 : -1;
62 do {
63 index += direction;
64 double nextT = topSegment->t(index);
65 if (nextT == firstT) {
66 continue;
67 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000068 pts[1] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000069 if (pts[0] != pts[1]) {
70 break;
71 }
72 } while (true);
73 SkASSERT(pts[0].fX == x1);
74 SkASSERT(pts[0].fY == y1);
75 SkASSERT(pts[1].fX == x2);
76 SkASSERT(pts[1].fY == y2);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000077}
78
79static void testLine1() {
80 SkPath path;
81 path.moveTo(2,0);
82 path.lineTo(1,1);
83 path.lineTo(0,0);
84 path.close();
85 test(path);
86}
87
88static void addInnerCWTriangle(SkPath& path) {
89 path.moveTo(3,0);
90 path.lineTo(4,1);
91 path.lineTo(2,1);
92 path.close();
93}
94
95static void addInnerCCWTriangle(SkPath& path) {
96 path.moveTo(3,0);
97 path.lineTo(2,1);
98 path.lineTo(4,1);
99 path.close();
100}
101
102static void addOuterCWTriangle(SkPath& path) {
103 path.moveTo(3,0);
104 path.lineTo(6,2);
105 path.lineTo(0,2);
106 path.close();
107}
108
109static void addOuterCCWTriangle(SkPath& path) {
110 path.moveTo(3,0);
111 path.lineTo(0,2);
112 path.lineTo(6,2);
113 path.close();
114}
115
116static void testLine2() {
117 SkPath path;
118 addInnerCWTriangle(path);
119 addOuterCWTriangle(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 testLine3() {
124 SkPath path;
125 addOuterCWTriangle(path);
126 addInnerCWTriangle(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 testLine4() {
131 SkPath path;
132 addInnerCCWTriangle(path);
133 addOuterCWTriangle(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 testLine5() {
138 SkPath path;
139 addOuterCWTriangle(path);
140 addInnerCCWTriangle(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 testLine6() {
145 SkPath path;
146 addInnerCWTriangle(path);
147 addOuterCCWTriangle(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 testLine7() {
152 SkPath path;
153 addOuterCCWTriangle(path);
154 addInnerCWTriangle(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 testLine8() {
159 SkPath path;
160 addInnerCCWTriangle(path);
161 addOuterCCWTriangle(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 testLine9() {
166 SkPath path;
167 addOuterCCWTriangle(path);
168 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000169 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000170}
171
172static void testQuads() {
173 SkPath path;
174 path.moveTo(2,0);
175 path.quadTo(1,1, 0,0);
176 path.close();
177 test(path);
178}
179
180static void testCubics() {
181 SkPath path;
182 path.moveTo(2,0);
183 path.cubicTo(2,3, 1,1, 0,0);
184 path.close();
185 test(path);
186}
187
188static void (*tests[])() = {
189 testLine1,
190 testLine2,
191 testLine3,
192 testLine4,
193 testLine5,
194 testLine6,
195 testLine7,
196 testLine8,
197 testLine9,
198 testQuads,
199 testCubics
200};
201
202static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
203
204static void (*firstTest)() = 0;
205static bool skipAll = false;
206
207void SimplifyFindTop_Test() {
208 if (skipAll) {
209 return;
210 }
211 size_t index = 0;
212 if (firstTest) {
213 while (index < testCount && tests[index] != firstTest) {
214 ++index;
215 }
216 }
217 bool firstTestComplete = false;
218 for ( ; index < testCount; ++index) {
219 (*tests[index])();
220 firstTestComplete = true;
221 }
222}