blob: b01b951d230b5d28a33bb954a9380f08d3890987 [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 */
rmistry@google.comd6176b02012-08-23 18:14:13 +00007
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#define DEBUG_TEST 1
rmistry@google.comd6176b02012-08-23 18:14:13 +00009
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.com27c449a2012-07-27 18:26:38 +000021 int contourWinding, int spanWinding, 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.com4eeda372012-12-06 21:47:48 +000024 makeContourList(contours, contourList, false, false);
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.com24bec792012-08-20 12:43:57 +000035 int nextStart = startIndex;
36 int nextEnd = endIndex;
caryclark@google.comfa4a6e92012-07-11 17:52:32 +000037 SkTDArray<SimplifyFindNextTest::Span*> chaseArray;
caryclark@google.comc91dfe42012-10-16 12:06:27 +000038 bool unsortable = false;
caryclark@google.com24bec792012-08-20 12:43:57 +000039 SimplifyFindNextTest::Segment* next = segment.findNextWinding(chaseArray,
caryclark@google.comc91dfe42012-10-16 12:06:27 +000040 true, nextStart, nextEnd, contourWinding, spanWinding,
41 unsortable);
caryclark@google.com88f7d0c2012-06-07 21:09:20 +000042 pts[1] = next->xyAtT(&next->span(nextStart));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000043 SkASSERT(pts[0] == pts[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000044 return next;
45}
46
47static void test(const SkPath& path) {
48 SkTArray<SimplifyFindNextTest::Contour> contours;
49 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.com27c449a2012-07-27 18:26:38 +000050 int contourWinding = 0;
51 int spanWinding = 1;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000052 int start = 0;
53 int end = 1;
caryclark@google.com27c449a2012-07-27 18:26:38 +000054 testCommon(contourWinding, spanWinding, start, end, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000055}
56
57static void test(const SkPath& path, int start, int end) {
58 SkTArray<SimplifyFindNextTest::Contour> contours;
59 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.com27c449a2012-07-27 18:26:38 +000060 int contourWinding = 0;
61 int spanWinding = 1;
62 testCommon(contourWinding, spanWinding, start, end, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000063}
64
65static void testLine1() {
66 SkPath path;
67 path.moveTo(2,0);
68 path.lineTo(1,1);
69 path.lineTo(0,0);
70 path.close();
71 test(path);
72}
73
caryclark@google.com1577e8f2012-05-22 17:01:14 +000074static void addInnerCWTriangle(SkPath& path) {
75 path.moveTo(3,0);
76 path.lineTo(4,1);
77 path.lineTo(2,1);
78 path.close();
79}
80
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000081#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000082static void addInnerCCWTriangle(SkPath& path) {
83 path.moveTo(3,0);
84 path.lineTo(2,1);
85 path.lineTo(4,1);
86 path.close();
87}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000088#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000089
90static void addOuterCWTriangle(SkPath& path) {
91 path.moveTo(3,0);
92 path.lineTo(6,2);
93 path.lineTo(0,2);
94 path.close();
95}
96
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000097#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000098static void addOuterCCWTriangle(SkPath& path) {
99 path.moveTo(3,0);
100 path.lineTo(0,2);
101 path.lineTo(6,2);
102 path.close();
103}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +0000104#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000105
106static void testLine2() {
107 SkPath path;
108 addInnerCWTriangle(path);
109 addOuterCWTriangle(path);
110 test(path, 0, 3);
111}
112
113static void testLine3() {
114 SkPath path;
115 addInnerCWTriangle(path);
116 addOuterCWTriangle(path);
117 test(path, 3, 0);
118}
119
120static void testLine4() {
121 SkPath path;
122 addInnerCWTriangle(path);
123 addOuterCWTriangle(path);
124 test(path, 3, 2);
125}
126
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000127static void (*tests[])() = {
128 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000129 testLine2,
130 testLine3,
131 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000132};
133
134static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
135
136static void (*firstTest)() = 0;
137static bool skipAll = false;
138
139void SimplifyFindNext_Test() {
140 if (skipAll) {
141 return;
142 }
143 size_t index = 0;
144 if (firstTest) {
145 while (index < testCount && tests[index] != firstTest) {
146 ++index;
147 }
148 }
149 bool firstTestComplete = false;
150 for ( ; index < testCount; ++index) {
151 (*tests[index])();
152 firstTestComplete = true;
153 }
154}