blob: c8ca984c8d00bfd43bdf1385ffe357eb62bc927a [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
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#define DEBUG_TEST 1
9
caryclark@google.comb45a1b42012-05-18 20:50:33 +000010#include "Simplify.h"
11
12namespace SimplifyFindNextTest {
13
14#include "Simplify.cpp"
15
16} // end of SimplifyFindNextTest namespace
17
18#include "Intersection_Tests.h"
19
20static const SimplifyFindNextTest::Segment* testCommon(
caryclark@google.com1577e8f2012-05-22 17:01:14 +000021 int winding, int startIndex, int endIndex,
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000022 SkTArray<SimplifyFindNextTest::Contour>& contours) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +000023 SkTDArray<SimplifyFindNextTest::Contour*> contourList;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000024 makeContourList(contours, contourList);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000025 addIntersectTs(contourList[0], contourList[0]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000026 if (contours.count() > 1) {
27 SkASSERT(contours.count() == 2);
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000028 addIntersectTs(contourList[0], contourList[1]);
29 addIntersectTs(contourList[1], contourList[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000030 }
31 fixOtherTIndex(contourList);
caryclark@google.com8dcf1142012-07-02 20:27:02 +000032 SimplifyFindNextTest::Segment& segment = contours[0].debugSegments()[0];
caryclark@google.com1577e8f2012-05-22 17:01:14 +000033 SkPoint pts[2];
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000034 pts[0] = segment.xyAtT(&segment.span(endIndex));
caryclark@google.com495f8e42012-05-31 13:13:11 +000035 int nextStart, nextEnd;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000036 SimplifyFindNextTest::Segment* next = segment.findNext(winding,
caryclark@google.com8dcf1142012-07-02 20:27:02 +000037 startIndex, endIndex, nextStart, nextEnd, true);
caryclark@google.com88f7d0c2012-06-07 21:09:20 +000038 pts[1] = next->xyAtT(&next->span(nextStart));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000039 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}