blob: 4edc53254f8253710e245d63a76a6c8d81b833f3 [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,
38 startIndex, endIndex, nextStart, nextEnd, flipped, true);
caryclark@google.com88f7d0c2012-06-07 21:09:20 +000039 pts[1] = next->xyAtT(&next->span(nextStart));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000040 SkASSERT(pts[0] == pts[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000041 return next;
42}
43
44static void test(const SkPath& path) {
45 SkTArray<SimplifyFindNextTest::Contour> contours;
46 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000047 int winding = 0;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000048 int start = 0;
49 int end = 1;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000050 testCommon(winding, start, end, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000051}
52
53static void test(const SkPath& path, int start, int end) {
54 SkTArray<SimplifyFindNextTest::Contour> contours;
55 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
56 int winding = 0;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000057 testCommon(winding, start, end, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000058}
59
60static void testLine1() {
61 SkPath path;
62 path.moveTo(2,0);
63 path.lineTo(1,1);
64 path.lineTo(0,0);
65 path.close();
66 test(path);
67}
68
caryclark@google.com1577e8f2012-05-22 17:01:14 +000069static void addInnerCWTriangle(SkPath& path) {
70 path.moveTo(3,0);
71 path.lineTo(4,1);
72 path.lineTo(2,1);
73 path.close();
74}
75
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000076#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000077static void addInnerCCWTriangle(SkPath& path) {
78 path.moveTo(3,0);
79 path.lineTo(2,1);
80 path.lineTo(4,1);
81 path.close();
82}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000083#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000084
85static void addOuterCWTriangle(SkPath& path) {
86 path.moveTo(3,0);
87 path.lineTo(6,2);
88 path.lineTo(0,2);
89 path.close();
90}
91
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000092#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000093static void addOuterCCWTriangle(SkPath& path) {
94 path.moveTo(3,0);
95 path.lineTo(0,2);
96 path.lineTo(6,2);
97 path.close();
98}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000099#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000100
101static void testLine2() {
102 SkPath path;
103 addInnerCWTriangle(path);
104 addOuterCWTriangle(path);
105 test(path, 0, 3);
106}
107
108static void testLine3() {
109 SkPath path;
110 addInnerCWTriangle(path);
111 addOuterCWTriangle(path);
112 test(path, 3, 0);
113}
114
115static void testLine4() {
116 SkPath path;
117 addInnerCWTriangle(path);
118 addOuterCWTriangle(path);
119 test(path, 3, 2);
120}
121
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000122static void (*tests[])() = {
123 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000124 testLine2,
125 testLine3,
126 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000127};
128
129static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
130
131static void (*firstTest)() = 0;
132static bool skipAll = false;
133
134void SimplifyFindNext_Test() {
135 if (skipAll) {
136 return;
137 }
138 size_t index = 0;
139 if (firstTest) {
140 while (index < testCount && tests[index] != firstTest) {
141 ++index;
142 }
143 }
144 bool firstTestComplete = false;
145 for ( ; index < testCount; ++index) {
146 (*tests[index])();
147 firstTestComplete = true;
148 }
149}