blob: 9c9548e321fb794c8c98678f20f490f1b515689d [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);
30 SimplifyFindTopTest::Segment* topStart = findTopContour(contourList,
31 contourList.count());
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.comb45a1b42012-05-18 20:50:33 +000034 return topSegment;
35}
36
37static void test(const SkPath& path) {
38 SkTArray<SimplifyFindTopTest::Contour> contours;
39 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000040 int index, end;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000041 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000042 SkASSERT(index + 1 == end);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000043}
44
45static void test(const SkPath& path, SkScalar x1, SkScalar y1,
46 SkScalar x2, SkScalar y2) {
47 SkTArray<SimplifyFindTopTest::Contour> contours;
48 SimplifyFindTopTest::EdgeBuilder builder(path, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000049 int index, end;
caryclark@google.comb45a1b42012-05-18 20:50:33 +000050 const SimplifyFindTopTest::Segment* topSegment =
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000051 testCommon(contours, index, end);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000052 SkPoint pts[2];
53 double firstT = topSegment->t(index);
54 topSegment->xyAtT(firstT, &pts[0]);
55 int direction = index < end ? 1 : -1;
56 do {
57 index += direction;
58 double nextT = topSegment->t(index);
59 if (nextT == firstT) {
60 continue;
61 }
62 topSegment->xyAtT(nextT, &pts[1]);
63 if (pts[0] != pts[1]) {
64 break;
65 }
66 } while (true);
67 SkASSERT(pts[0].fX == x1);
68 SkASSERT(pts[0].fY == y1);
69 SkASSERT(pts[1].fX == x2);
70 SkASSERT(pts[1].fY == y2);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000071}
72
73static void testLine1() {
74 SkPath path;
75 path.moveTo(2,0);
76 path.lineTo(1,1);
77 path.lineTo(0,0);
78 path.close();
79 test(path);
80}
81
82static void addInnerCWTriangle(SkPath& path) {
83 path.moveTo(3,0);
84 path.lineTo(4,1);
85 path.lineTo(2,1);
86 path.close();
87}
88
89static void addInnerCCWTriangle(SkPath& path) {
90 path.moveTo(3,0);
91 path.lineTo(2,1);
92 path.lineTo(4,1);
93 path.close();
94}
95
96static void addOuterCWTriangle(SkPath& path) {
97 path.moveTo(3,0);
98 path.lineTo(6,2);
99 path.lineTo(0,2);
100 path.close();
101}
102
103static void addOuterCCWTriangle(SkPath& path) {
104 path.moveTo(3,0);
105 path.lineTo(0,2);
106 path.lineTo(6,2);
107 path.close();
108}
109
110static void testLine2() {
111 SkPath path;
112 addInnerCWTriangle(path);
113 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000114 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000115}
116
117static void testLine3() {
118 SkPath path;
119 addOuterCWTriangle(path);
120 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000121 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000122}
123
124static void testLine4() {
125 SkPath path;
126 addInnerCCWTriangle(path);
127 addOuterCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000128 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000129}
130
131static void testLine5() {
132 SkPath path;
133 addOuterCWTriangle(path);
134 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000135 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000136}
137
138static void testLine6() {
139 SkPath path;
140 addInnerCWTriangle(path);
141 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000142 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000143}
144
145static void testLine7() {
146 SkPath path;
147 addOuterCCWTriangle(path);
148 addInnerCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000149 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000150}
151
152static void testLine8() {
153 SkPath path;
154 addInnerCCWTriangle(path);
155 addOuterCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000156 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000157}
158
159static void testLine9() {
160 SkPath path;
161 addOuterCCWTriangle(path);
162 addInnerCCWTriangle(path);
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000163 test(path, 0, 2, 3, 0);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000164}
165
166static void testQuads() {
167 SkPath path;
168 path.moveTo(2,0);
169 path.quadTo(1,1, 0,0);
170 path.close();
171 test(path);
172}
173
174static void testCubics() {
175 SkPath path;
176 path.moveTo(2,0);
177 path.cubicTo(2,3, 1,1, 0,0);
178 path.close();
179 test(path);
180}
181
182static void (*tests[])() = {
183 testLine1,
184 testLine2,
185 testLine3,
186 testLine4,
187 testLine5,
188 testLine6,
189 testLine7,
190 testLine8,
191 testLine9,
192 testQuads,
193 testCubics
194};
195
196static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
197
198static void (*firstTest)() = 0;
199static bool skipAll = false;
200
201void SimplifyFindTop_Test() {
202 if (skipAll) {
203 return;
204 }
205 size_t index = 0;
206 if (firstTest) {
207 while (index < testCount && tests[index] != firstTest) {
208 ++index;
209 }
210 }
211 bool firstTestComplete = false;
212 for ( ; index < testCount; ++index) {
213 (*tests[index])();
214 firstTestComplete = true;
215 }
216}