blob: ffbe95eead66e077510f0df6e6e592ac41794aab [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];
32 double startT = segment.t(endIndex);
33 segment.xyAtT(startT, &pts[0]);
34 SimplifyFindNextTest::Segment* next = segment.findNext(winding,
35 startIndex, endIndex);
36 double endT = next->t(startIndex);
37 next->xyAtT(endT, &pts[1]);
38 SkASSERT(pts[0] == pts[1]);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000039 return next;
40}
41
42static void test(const SkPath& path) {
43 SkTArray<SimplifyFindNextTest::Contour> contours;
44 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000045 int winding = 0;
caryclark@google.com1577e8f2012-05-22 17:01:14 +000046 int start = 0;
47 int end = 1;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000048 testCommon(winding, start, end, contours);
caryclark@google.com1577e8f2012-05-22 17:01:14 +000049}
50
51static void test(const SkPath& path, int start, int end) {
52 SkTArray<SimplifyFindNextTest::Contour> contours;
53 SimplifyFindNextTest::EdgeBuilder builder(path, contours);
54 int winding = 0;
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000055 testCommon(winding, start, end, contours);
caryclark@google.comb45a1b42012-05-18 20:50:33 +000056}
57
58static void testLine1() {
59 SkPath path;
60 path.moveTo(2,0);
61 path.lineTo(1,1);
62 path.lineTo(0,0);
63 path.close();
64 test(path);
65}
66
caryclark@google.com1577e8f2012-05-22 17:01:14 +000067static void addInnerCWTriangle(SkPath& path) {
68 path.moveTo(3,0);
69 path.lineTo(4,1);
70 path.lineTo(2,1);
71 path.close();
72}
73
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000074#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000075static void addInnerCCWTriangle(SkPath& path) {
76 path.moveTo(3,0);
77 path.lineTo(2,1);
78 path.lineTo(4,1);
79 path.close();
80}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000081#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000082
83static void addOuterCWTriangle(SkPath& path) {
84 path.moveTo(3,0);
85 path.lineTo(6,2);
86 path.lineTo(0,2);
87 path.close();
88}
89
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000090#if DEBUG_UNUSED
caryclark@google.com1577e8f2012-05-22 17:01:14 +000091static void addOuterCCWTriangle(SkPath& path) {
92 path.moveTo(3,0);
93 path.lineTo(0,2);
94 path.lineTo(6,2);
95 path.close();
96}
caryclark@google.com65f9f0a2012-05-23 18:09:25 +000097#endif
caryclark@google.com1577e8f2012-05-22 17:01:14 +000098
99static void testLine2() {
100 SkPath path;
101 addInnerCWTriangle(path);
102 addOuterCWTriangle(path);
103 test(path, 0, 3);
104}
105
106static void testLine3() {
107 SkPath path;
108 addInnerCWTriangle(path);
109 addOuterCWTriangle(path);
110 test(path, 3, 0);
111}
112
113static void testLine4() {
114 SkPath path;
115 addInnerCWTriangle(path);
116 addOuterCWTriangle(path);
117 test(path, 3, 2);
118}
119
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000120static void (*tests[])() = {
121 testLine1,
caryclark@google.com1577e8f2012-05-22 17:01:14 +0000122 testLine2,
123 testLine3,
124 testLine4,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000125};
126
127static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
128
129static void (*firstTest)() = 0;
130static bool skipAll = false;
131
132void SimplifyFindNext_Test() {
133 if (skipAll) {
134 return;
135 }
136 size_t index = 0;
137 if (firstTest) {
138 while (index < testCount && tests[index] != firstTest) {
139 ++index;
140 }
141 }
142 bool firstTestComplete = false;
143 for ( ; index < testCount; ++index) {
144 (*tests[index])();
145 firstTestComplete = true;
146 }
147}