blob: c83de89531351878a63db1a1dbf0b88206e6093f [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];
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000032 pts[0] = segment.xyAtT(&segment.span(endIndex));
caryclark@google.com495f8e42012-05-31 13:13:11 +000033 int nextStart, nextEnd;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000034 SimplifyFindNextTest::Segment* next = segment.findNext(winding,
caryclark@google.com495f8e42012-05-31 13:13:11 +000035 startIndex, endIndex, nextStart, nextEnd);
caryclark@google.coma3f05fa2012-06-01 17:44:28 +000036 pts[1] = next->xyAtT(&segment.span(nextStart));
caryclark@google.com1577e8f2012-05-22 17:01:14 +000037 SkASSERT(pts[0] == pts[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000038 return next;
39}
40
41static void test(const SkPath& path) {
42 SkTArray<SimplifyFindNextTest::Contour> contours;
43 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000044 int winding = 0;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000045 int start = 0;
46 int end = 1;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000047 testCommon(winding, start, end, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000048}
49
50static void test(const SkPath& path, int start, int end) {
51 SkTArray<SimplifyFindNextTest::Contour> contours;
52 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
53 int winding = 0;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000054 testCommon(winding, start, end, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000055}
56
57static void testLine1() {
58 SkPath path;
59 path.moveTo(2,0);
60 path.lineTo(1,1);
61 path.lineTo(0,0);
62 path.close();
63 test(path);
64}
65
caryclark@google.com1577e8f2012-05-22 17:01:14 +000066static void addInnerCWTriangle(SkPath& path) {
67 path.moveTo(3,0);
68 path.lineTo(4,1);
69 path.lineTo(2,1);
70 path.close();
71}
72
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000073#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000074static void addInnerCCWTriangle(SkPath& path) {
75 path.moveTo(3,0);
76 path.lineTo(2,1);
77 path.lineTo(4,1);
78 path.close();
79}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000080#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000081
82static void addOuterCWTriangle(SkPath& path) {
83 path.moveTo(3,0);
84 path.lineTo(6,2);
85 path.lineTo(0,2);
86 path.close();
87}
88
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000089#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000090static void addOuterCCWTriangle(SkPath& path) {
91 path.moveTo(3,0);
92 path.lineTo(0,2);
93 path.lineTo(6,2);
94 path.close();
95}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000096#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000097
98static void testLine2() {
99 SkPath path;
100 addInnerCWTriangle(path);
101 addOuterCWTriangle(path);
102 test(path, 0, 3);
103}
104
105static void testLine3() {
106 SkPath path;
107 addInnerCWTriangle(path);
108 addOuterCWTriangle(path);
109 test(path, 3, 0);
110}
111
112static void testLine4() {
113 SkPath path;
114 addInnerCWTriangle(path);
115 addOuterCWTriangle(path);
116 test(path, 3, 2);
117}
118
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000119static void (*tests[])() = {
120 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000121 testLine2,
122 testLine3,
123 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000124};
125
126static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
127
128static void (*firstTest)() = 0;
129static bool skipAll = false;
130
131void SimplifyFindNext_Test() {
132 if (skipAll) {
133 return;
134 }
135 size_t index = 0;
136 if (firstTest) {
137 while (index < testCount && tests[index] != firstTest) {
138 ++index;
139 }
140 }
141 bool firstTestComplete = false;
142 for ( ; index < testCount; ++index) {
143 (*tests[index])();
144 firstTestComplete = true;
145 }
146}