blob: e30908d2534da296f70665cd43f76bba88978ff8 [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 SimplifyFindNextTest {
11
12#include "Simplify.cpp"
13
14} // end of SimplifyFindNextTest namespace
15
16#include "Intersection_Tests.h"
17
18static const SimplifyFindNextTest::Segment* testCommon(
caryclark@google.com1577e8f2012-05-22 17:01:14 +000019 int winding, int startIndex, int endIndex,
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000020 SkTArray<SimplifyFindNextTest::Contour>& contours) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000021 SkTDArray<SimplifyFindNextTest::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 SimplifyFindNextTest::Segment& segment = contours[0].fSegments[0];
caryclark@google.com1577e8f2012-05-22 17:01:14 +000031 SkPoint pts[2];
32 double startT = segment.t(endIndex);
33 segment.xyAtT(startT, &pts[0]);
caryclark@google.com495f8e42012-05-31 13:13:11 +000034 int nextStart, nextEnd;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000035 SimplifyFindNextTest::Segment* next = segment.findNext(winding,
caryclark@google.com495f8e42012-05-31 13:13:11 +000036 startIndex, endIndex, nextStart, nextEnd);
37 double endT = next->t(nextStart);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000038 next->xyAtT(endT, &pts[1]);
39 SkASSERT(pts[0] == pts[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000040 return next;
41}
42
43static void test(const SkPath& path) {
44 SkTArray<SimplifyFindNextTest::Contour> contours;
45 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000046 int winding = 0;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000047 int start = 0;
48 int end = 1;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000049 testCommon(winding, start, end, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000050}
51
52static void test(const SkPath& path, int start, int end) {
53 SkTArray<SimplifyFindNextTest::Contour> contours;
54 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
55 int winding = 0;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000056 testCommon(winding, start, end, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000057}
58
59static void testLine1() {
60 SkPath path;
61 path.moveTo(2,0);
62 path.lineTo(1,1);
63 path.lineTo(0,0);
64 path.close();
65 test(path);
66}
67
caryclark@google.com1577e8f2012-05-22 17:01:14 +000068static void addInnerCWTriangle(SkPath& path) {
69 path.moveTo(3,0);
70 path.lineTo(4,1);
71 path.lineTo(2,1);
72 path.close();
73}
74
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000075#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000076static void addInnerCCWTriangle(SkPath& path) {
77 path.moveTo(3,0);
78 path.lineTo(2,1);
79 path.lineTo(4,1);
80 path.close();
81}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000082#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000083
84static void addOuterCWTriangle(SkPath& path) {
85 path.moveTo(3,0);
86 path.lineTo(6,2);
87 path.lineTo(0,2);
88 path.close();
89}
90
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000091#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000092static void addOuterCCWTriangle(SkPath& path) {
93 path.moveTo(3,0);
94 path.lineTo(0,2);
95 path.lineTo(6,2);
96 path.close();
97}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000098#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000099
100static void testLine2() {
101 SkPath path;
102 addInnerCWTriangle(path);
103 addOuterCWTriangle(path);
104 test(path, 0, 3);
105}
106
107static void testLine3() {
108 SkPath path;
109 addInnerCWTriangle(path);
110 addOuterCWTriangle(path);
111 test(path, 3, 0);
112}
113
114static void testLine4() {
115 SkPath path;
116 addInnerCWTriangle(path);
117 addOuterCWTriangle(path);
118 test(path, 3, 2);
119}
120
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000121static void (*tests[])() = {
122 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000123 testLine2,
124 testLine3,
125 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000126};
127
128static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
129
130static void (*firstTest)() = 0;
131static bool skipAll = false;
132
133void SimplifyFindNext_Test() {
134 if (skipAll) {
135 return;
136 }
137 size_t index = 0;
138 if (firstTest) {
139 while (index < testCount && tests[index] != firstTest) {
140 ++index;
141 }
142 }
143 bool firstTestComplete = false;
144 for ( ; index < testCount; ++index) {
145 (*tests[index])();
146 firstTestComplete = true;
147 }
148}