blob: 2bd69788acfc9fa556f49528b4839007f81bec72 [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.comfa4a6e92012-07-11 17:52:32 +000035 int nextStart, nextEnd, flipped = 1;
36 SkTDArray<SimplifyFindNextTest::Span*> chaseArray;
37 SimplifyFindNextTest::Segment* next = segment.findNext(chaseArray, winding,
caryclark@google.come21cb182012-07-23 21:26:31 +000038 winding, 0, startIndex, endIndex, nextStart, nextEnd, flipped,
39 true, true);
caryclark@google.com88f7d0c2012-06-07 21:09:20 +000040 pts[1] = next->xyAtT(&next->span(nextStart));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000041 SkASSERT(pts[0] == pts[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000042 return next;
43}
44
45static void test(const SkPath& path) {
46 SkTArray<SimplifyFindNextTest::Contour> contours;
47 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000048 int winding = 0;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000049 int start = 0;
50 int end = 1;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000051 testCommon(winding, start, end, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000052}
53
54static void test(const SkPath& path, int start, int end) {
55 SkTArray<SimplifyFindNextTest::Contour> contours;
56 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
57 int winding = 0;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000058 testCommon(winding, start, end, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000059}
60
61static void testLine1() {
62 SkPath path;
63 path.moveTo(2,0);
64 path.lineTo(1,1);
65 path.lineTo(0,0);
66 path.close();
67 test(path);
68}
69
caryclark@google.com1577e8f2012-05-22 17:01:14 +000070static void addInnerCWTriangle(SkPath& path) {
71 path.moveTo(3,0);
72 path.lineTo(4,1);
73 path.lineTo(2,1);
74 path.close();
75}
76
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000077#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000078static void addInnerCCWTriangle(SkPath& path) {
79 path.moveTo(3,0);
80 path.lineTo(2,1);
81 path.lineTo(4,1);
82 path.close();
83}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000084#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000085
86static void addOuterCWTriangle(SkPath& path) {
87 path.moveTo(3,0);
88 path.lineTo(6,2);
89 path.lineTo(0,2);
90 path.close();
91}
92
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000093#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000094static void addOuterCCWTriangle(SkPath& path) {
95 path.moveTo(3,0);
96 path.lineTo(0,2);
97 path.lineTo(6,2);
98 path.close();
99}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000100#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000101
102static void testLine2() {
103 SkPath path;
104 addInnerCWTriangle(path);
105 addOuterCWTriangle(path);
106 test(path, 0, 3);
107}
108
109static void testLine3() {
110 SkPath path;
111 addInnerCWTriangle(path);
112 addOuterCWTriangle(path);
113 test(path, 3, 0);
114}
115
116static void testLine4() {
117 SkPath path;
118 addInnerCWTriangle(path);
119 addOuterCWTriangle(path);
120 test(path, 3, 2);
121}
122
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000123static void (*tests[])() = {
124 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000125 testLine2,
126 testLine3,
127 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000128};
129
130static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
131
132static void (*firstTest)() = 0;
133static bool skipAll = false;
134
135void SimplifyFindNext_Test() {
136 if (skipAll) {
137 return;
138 }
139 size_t index = 0;
140 if (firstTest) {
141 while (index < testCount && tests[index] != firstTest) {
142 ++index;
143 }
144 }
145 bool firstTestComplete = false;
146 for ( ; index < testCount; ++index) {
147 (*tests[index])();
148 firstTestComplete = true;
149 }
150}