blob: 4e55038b4639848510bb2f1c33f4851d8b108dcf [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.comb45a1b42012-05-18 20:50:33 +000020 SkTArray<SimplifyFindNextTest::Contour>& contours,
21 SimplifyFindNextTest::EdgeBuilder& builder, const SkPath& path) {
22 SkTDArray<SimplifyFindNextTest::Contour*> contourList;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000023 makeContourList(contours, contourList);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000024 addIntersectTs(contourList[0], contourList[0], -1);
25 if (contours.count() > 1) {
26 SkASSERT(contours.count() == 2);
27 addIntersectTs(contourList[0], contourList[1], -1);
28 addIntersectTs(contourList[1], contourList[1], -1);
29 }
30 fixOtherTIndex(contourList);
31 SimplifyFindNextTest::Segment& segment = contours[0].fSegments[0];
caryclark@google.com1577e8f2012-05-22 17:01:14 +000032 SkPoint pts[2];
33 double startT = segment.t(endIndex);
34 segment.xyAtT(startT, &pts[0]);
35 SimplifyFindNextTest::Segment* next = segment.findNext(winding,
36 startIndex, endIndex);
37 double endT = next->t(startIndex);
38 next->xyAtT(endT, &pts[1]);
39 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;
49 testCommon(winding, start, end, contours, builder, path);
50}
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;
56 testCommon(winding, start, end, contours, builder, path);
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
75static void addInnerCCWTriangle(SkPath& path) {
76 path.moveTo(3,0);
77 path.lineTo(2,1);
78 path.lineTo(4,1);
79 path.close();
80}
81
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
89static void addOuterCCWTriangle(SkPath& path) {
90 path.moveTo(3,0);
91 path.lineTo(0,2);
92 path.lineTo(6,2);
93 path.close();
94}
95
96static void testLine2() {
97 SkPath path;
98 addInnerCWTriangle(path);
99 addOuterCWTriangle(path);
100 test(path, 0, 3);
101}
102
103static void testLine3() {
104 SkPath path;
105 addInnerCWTriangle(path);
106 addOuterCWTriangle(path);
107 test(path, 3, 0);
108}
109
110static void testLine4() {
111 SkPath path;
112 addInnerCWTriangle(path);
113 addOuterCWTriangle(path);
114 test(path, 3, 2);
115}
116
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000117static void (*tests[])() = {
118 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000119 testLine2,
120 testLine3,
121 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000122};
123
124static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
125
126static void (*firstTest)() = 0;
127static bool skipAll = false;
128
129void SimplifyFindNext_Test() {
130 if (skipAll) {
131 return;
132 }
133 size_t index = 0;
134 if (firstTest) {
135 while (index < testCount && tests[index] != firstTest) {
136 ++index;
137 }
138 }
139 bool firstTestComplete = false;
140 for ( ; index < testCount; ++index) {
141 (*tests[index])();
142 firstTestComplete = true;
143 }
144}