blob: 33b5fe22425a744314cbfa91a277b308a0283236 [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 SimplifyAngleTest {
11
12#include "Simplify.cpp"
13
14} // end of SimplifyAngleTest namespace
15
16#include "Intersection_Tests.h"
17
18static const SkPoint lines[][2] = {
19 { { 10, 10}, { 10, 20} },
20 { { 10, 10}, { 20, 10} },
21 { { 10, 10}, {-20, 10} },
22 { { 10, 10}, { 10, -20} },
23 { { 10, 10}, { 20, 20} },
24 { { 10, 10}, {-20, -20} },
25 { { 10, 10}, {-20, 40} },
26 { { 10, 10}, { 40, -20} }
27};
28
29static const size_t lineCount = sizeof(lines) / sizeof(lines[0]);
30
31static const SkPoint quads[][3] = {
32 {{ 1, 1}, { 2, 2}, { 1, 3}}, // 0
33 {{ 1, 1}, { 3, 3}, { 1, 5}}, // 1
34 {{ 1, 1}, { 4, 4}, { 1, 7}}, // 2
35 {{ 1, 1}, { 5, 5}, { 9, 9}}, // 3
36 {{ 1, 1}, { 4, 4}, { 7, 1}}, // 4
37 {{ 1, 1}, { 3, 3}, { 5, 1}}, // 5
38 {{ 1, 1}, { 2, 2}, { 3, 1}}, // 6
39};
40
41static const size_t quadCount = sizeof(quads) / sizeof(quads[0]);
42
43static const SkPoint cubics[][4] = {
44 {{ 1, 1}, { 2, 2}, { 2, 3}, { 1, 4}},
45 {{ 1, 1}, { 3, 3}, { 3, 5}, { 1, 7}},
46 {{ 1, 1}, { 4, 4}, { 4, 7}, { 1, 10}},
47 {{ 1, 1}, { 5, 5}, { 8, 8}, { 9, 9}},
48 {{ 1, 1}, { 4, 4}, { 7, 4}, { 10, 1}},
49 {{ 1, 1}, { 3, 3}, { 5, 3}, { 7, 1}},
50 {{ 1, 1}, { 2, 2}, { 3, 2}, { 4, 1}},
51};
52
53static const size_t cubicCount = sizeof(cubics) / sizeof(cubics[0]);
54
caryclark@google.comc899ad92012-08-23 15:24:42 +000055struct segment {
56 SkPath::Verb verb;
57 SkPoint pts[4];
58};
59
60static const segment segmentTest1[] = {
61 {SkPath::kLine_Verb, {{2, 1}, {1, 0} }},
62 {SkPath::kQuad_Verb, {{2, 1}, {1, 0}, {0, 0}}},
63 {SkPath::kQuad_Verb, {{2, 1}, {3, 2}, {2, 3}}},
64 {SkPath::kLine_Verb, {{2, 1}, {3, 2} }},
65 {SkPath::kMove_Verb }
66};
67
68static const segment* segmentTests[] = {
69 segmentTest1
70};
71
72static const size_t segmentTestCount = sizeof(segmentTests) / sizeof(segmentTests[0]);
73
74static void testSegments(bool testFlat) {
75 for (size_t testIndex = 0; testIndex < segmentTestCount; ++testIndex) {
76 const segment* segPtr = segmentTests[testIndex];
77 SimplifyAngleTest::Angle lesser, greater;
78 int index = 0;
79 do {
80 int next = index + 1;
81 if (testFlat) {
82 lesser.setFlat(segPtr[index].pts, segPtr[index].verb, 0, index, next);
83 } else {
84 lesser.set(segPtr[index].pts, segPtr[index].verb, 0, index, next);
85 }
86 if (segPtr[next].verb == SkPath::kMove_Verb) {
87 break;
88 }
89 if (testFlat) {
90 greater.setFlat(segPtr[next].pts, segPtr[next].verb, 0, index, next);
91 } else {
92 greater.set(segPtr[next].pts, segPtr[next].verb, 0, index, next);
93 }
94 bool result = lesser < greater;
95 SkASSERT(result);
96 index = next;
97 } while (true);
98 }
99}
100
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000101static void testLines(bool testFlat) {
102 // create angles in a circle
103 SkTDArray<SimplifyAngleTest::Angle> angles;
104 SkTDArray<SimplifyAngleTest::Angle* > angleList;
105 SkTDArray<double> arcTans;
106 size_t x;
107 for (x = 0; x < lineCount; ++x) {
108 SimplifyAngleTest::Angle* angle = angles.append();
109 if (testFlat) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000110 angle->setFlat(lines[x], SkPath::kLine_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000111 } else {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000112 angle->set(lines[x], SkPath::kLine_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000113 }
114 double arcTan = atan2(lines[x][0].fX - lines[x][1].fX,
115 lines[x][0].fY - lines[x][1].fY);
116 arcTans.push(arcTan);
117 }
118 for (x = 0; x < lineCount; ++x) {
119 angleList.push(&angles[x]);
120 }
121 QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1);
122 bool first = true;
123 bool wrap = false;
124 double base, last;
125 for (size_t x = 0; x < lineCount; ++x) {
126 const SimplifyAngleTest::Angle* angle = angleList[x];
127 int span = angle->start();
128// SkDebugf("%s [%d] %1.9g (%1.9g,%1.9g %1.9g,%1.9g)\n", __FUNCTION__,
129// span, arcTans[span], lines[span][0].fX, lines[span][0].fY,
130// lines[span][1].fX, lines[span][1].fY);
131 if (first) {
132 base = last = arcTans[span];
133 first = false;
134 continue;
135 }
136 if (last < arcTans[span]) {
137 last = arcTans[span];
138 continue;
139 }
140 if (!wrap) {
141 if (base < arcTans[span]) {
142 SkDebugf("%s !wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]);
143 SkASSERT(0);
144 }
145 last = arcTans[span];
146 wrap = true;
147 continue;
148 }
149 SkDebugf("%s wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]);
150 SkASSERT(0);
151 }
152}
153
154static void testQuads(bool testFlat) {
155 SkTDArray<SimplifyAngleTest::Angle> angles;
156 SkTDArray<SimplifyAngleTest::Angle* > angleList;
157 size_t x;
158 for (x = 0; x < quadCount; ++x) {
159 SimplifyAngleTest::Angle* angle = angles.append();
160 if (testFlat) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000161 angle->setFlat(quads[x], SkPath::kQuad_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000162 } else {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000163 angle->set(quads[x], SkPath::kQuad_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000164 }
165 }
166 for (x = 0; x < quadCount; ++x) {
167 angleList.push(&angles[x]);
168 }
169 QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1);
170 for (size_t x = 0; x < quadCount; ++x) {
171 *angleList[x] < *angleList[x + 1];
172 SkASSERT(x == quadCount - 1 || *angleList[x] < *angleList[x + 1]);
173 const SimplifyAngleTest::Angle* angle = angleList[x];
caryclark@google.comf25edfe2012-06-01 18:20:10 +0000174 if ((int) x != angle->start()) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000175 SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start());
176 SkASSERT(0);
177 }
178 }
179}
180
181static void testCubics(bool testFlat) {
182 SkTDArray<SimplifyAngleTest::Angle> angles;
183 SkTDArray<SimplifyAngleTest::Angle* > angleList;
184 for (size_t x = 0; x < cubicCount; ++x) {
185 SimplifyAngleTest::Angle* angle = angles.append();
186 if (testFlat) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000187 angle->setFlat(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000188 } else {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000189 angle->set(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000190 }
191 angleList.push(angle);
192 }
193 QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1);
194 for (size_t x = 0; x < cubicCount; ++x) {
195 const SimplifyAngleTest::Angle* angle = angleList[x];
caryclark@google.comf25edfe2012-06-01 18:20:10 +0000196 if ((int) x != angle->start()) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000197 SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start());
198 SkASSERT(0);
199 }
200 }
201}
202
203static void (*tests[])(bool) = {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000204 testSegments,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000205 testLines,
206 testQuads,
207 testCubics
208};
209
210static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
211
212static void (*firstTest)(bool) = 0;
213static bool skipAll = false;
214
215void SimplifyAngle_Test() {
216 if (skipAll) {
217 return;
218 }
219 size_t index = 0;
220 if (firstTest) {
221 while (index < testCount && tests[index] != firstTest) {
222 ++index;
223 }
224 }
225 bool firstTestComplete = false;
226 for ( ; index < testCount; ++index) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000227 (*tests[index])(false); // set to true to exercise setFlat
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000228 firstTestComplete = true;
229 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000230}