blob: 11b083f947db8e360430cd486f168d404be19a79 [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.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
35 const SimplifyFindTopTest::Segment* topSegment = findSortableTop(contourList, index, end);
36#endif
caryclark@google.comb45a1b42012-05-18 20:50:33 +000037 return topSegment;
38}
39
40static void test(const SkPath& path) {
41 SkTArray<SimplifyFindTopTest::Contour> contours;
42 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000043 int index, end;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000044 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000045 SkASSERT(index + 1 == end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000046}
47
48static void test(const SkPath& path, SkScalar x1, SkScalar y1,
49 SkScalar x2, SkScalar y2) {
50 SkTArray<SimplifyFindTopTest::Contour> contours;
51 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000052 int index, end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +000053 const SimplifyFindTopTest::Segment* topSegment =
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000054 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000055 SkPoint pts[2];
56 double firstT = topSegment->t(index);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000057 pts[0] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000058 int direction = index < end ? 1 : -1;
59 do {
60 index += direction;
61 double nextT = topSegment->t(index);
62 if (nextT == firstT) {
63 continue;
64 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000065 pts[1] = topSegment->xyAtT(&topSegment->span(index));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000066 if (pts[0] != pts[1]) {
67 break;
68 }
69 } while (true);
70 SkASSERT(pts[0].fX == x1);
71 SkASSERT(pts[0].fY == y1);
72 SkASSERT(pts[1].fX == x2);
73 SkASSERT(pts[1].fY == y2);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000074}
75
76static void testLine1() {
77 SkPath path;
78 path.moveTo(2,0);
79 path.lineTo(1,1);
80 path.lineTo(0,0);
81 path.close();
82 test(path);
83}
84
85static void addInnerCWTriangle(SkPath& path) {
86 path.moveTo(3,0);
87 path.lineTo(4,1);
88 path.lineTo(2,1);
89 path.close();
90}
91
92static void addInnerCCWTriangle(SkPath& path) {
93 path.moveTo(3,0);
94 path.lineTo(2,1);
95 path.lineTo(4,1);
96 path.close();
97}
98
99static void addOuterCWTriangle(SkPath& path) {
100 path.moveTo(3,0);
101 path.lineTo(6,2);
102 path.lineTo(0,2);
103 path.close();
104}
105
106static void addOuterCCWTriangle(SkPath& path) {
107 path.moveTo(3,0);
108 path.lineTo(0,2);
109 path.lineTo(6,2);
110 path.close();
111}
112
113static void testLine2() {
114 SkPath path;
115 addInnerCWTriangle(path);
116 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000117 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000118}
119
120static void testLine3() {
121 SkPath path;
122 addOuterCWTriangle(path);
123 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000124 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000125}
126
127static void testLine4() {
128 SkPath path;
129 addInnerCCWTriangle(path);
130 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000131 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000132}
133
134static void testLine5() {
135 SkPath path;
136 addOuterCWTriangle(path);
137 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000138 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000139}
140
141static void testLine6() {
142 SkPath path;
143 addInnerCWTriangle(path);
144 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000145 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000146}
147
148static void testLine7() {
149 SkPath path;
150 addOuterCCWTriangle(path);
151 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000152 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000153}
154
155static void testLine8() {
156 SkPath path;
157 addInnerCCWTriangle(path);
158 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000159 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000160}
161
162static void testLine9() {
163 SkPath path;
164 addOuterCCWTriangle(path);
165 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000166 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000167}
168
169static void testQuads() {
170 SkPath path;
171 path.moveTo(2,0);
172 path.quadTo(1,1, 0,0);
173 path.close();
174 test(path);
175}
176
177static void testCubics() {
178 SkPath path;
179 path.moveTo(2,0);
180 path.cubicTo(2,3, 1,1, 0,0);
181 path.close();
182 test(path);
183}
184
185static void (*tests[])() = {
186 testLine1,
187 testLine2,
188 testLine3,
189 testLine4,
190 testLine5,
191 testLine6,
192 testLine7,
193 testLine8,
194 testLine9,
195 testQuads,
196 testCubics
197};
198
199static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
200
201static void (*firstTest)() = 0;
202static bool skipAll = false;
203
204void SimplifyFindTop_Test() {
205 if (skipAll) {
206 return;
207 }
208 size_t index = 0;
209 if (firstTest) {
210 while (index < testCount && tests[index] != firstTest) {
211 ++index;
212 }
213 }
214 bool firstTestComplete = false;
215 for ( ; index < testCount; ++index) {
216 (*tests[index])();
217 firstTestComplete = true;
218 }
219}