blob: 4bf22fd44f612b7299ad6ac923c39cbc24168b4d [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
caryclark@google.com3350c3c2012-08-24 15:24:36 +000068static const segment segmentTest2[] = {
69 {SkPath::kLine_Verb, {{1, 0}, {0, 0} }},
70 {SkPath::kQuad_Verb, {{1, 0}, {1.89897954f, 0.898979485f}, {2.39387703f, 1.59591794f}}},
71 {SkPath::kLine_Verb, {{1, 0}, {3, 2} }},
72 {SkPath::kMove_Verb }
73};
74
caryclark@google.com32546db2012-08-31 20:55:07 +000075static const segment segmentTest3[] = {
76 {SkPath::kQuad_Verb, {{0, 0}, {2, 0}, {0, 1}}},
77 {SkPath::kQuad_Verb, {{0, 0}, {1, 0}, {0, 1}}},
78 {SkPath::kMove_Verb }
79};
80
caryclark@google.comc899ad92012-08-23 15:24:42 +000081static const segment* segmentTests[] = {
caryclark@google.com32546db2012-08-31 20:55:07 +000082 segmentTest3,
caryclark@google.com3350c3c2012-08-24 15:24:36 +000083 segmentTest2,
84 segmentTest1,
caryclark@google.comc899ad92012-08-23 15:24:42 +000085};
86
87static const size_t segmentTestCount = sizeof(segmentTests) / sizeof(segmentTests[0]);
88
89static void testSegments(bool testFlat) {
90 for (size_t testIndex = 0; testIndex < segmentTestCount; ++testIndex) {
91 const segment* segPtr = segmentTests[testIndex];
92 SimplifyAngleTest::Angle lesser, greater;
93 int index = 0;
94 do {
95 int next = index + 1;
caryclark@google.com3350c3c2012-08-24 15:24:36 +000096 #if HIGH_DEF_ANGLES==0
caryclark@google.comc899ad92012-08-23 15:24:42 +000097 if (testFlat) {
98 lesser.setFlat(segPtr[index].pts, segPtr[index].verb, 0, index, next);
99 } else {
100 lesser.set(segPtr[index].pts, segPtr[index].verb, 0, index, next);
101 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000102 #else
103 lesser.set(segPtr[index].pts, segPtr[index].verb, 0, index, next, 0, 1);
104 #endif
caryclark@google.comc899ad92012-08-23 15:24:42 +0000105 if (segPtr[next].verb == SkPath::kMove_Verb) {
106 break;
107 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000108 #if HIGH_DEF_ANGLES==0
caryclark@google.comc899ad92012-08-23 15:24:42 +0000109 if (testFlat) {
110 greater.setFlat(segPtr[next].pts, segPtr[next].verb, 0, index, next);
111 } else {
112 greater.set(segPtr[next].pts, segPtr[next].verb, 0, index, next);
113 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000114 #else
115 greater.set(segPtr[next].pts, segPtr[next].verb, 0, index, next, 0, 1);
116 #endif
caryclark@google.comc899ad92012-08-23 15:24:42 +0000117 bool result = lesser < greater;
118 SkASSERT(result);
119 index = next;
120 } while (true);
121 }
122}
123
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000124static void testLines(bool testFlat) {
125 // create angles in a circle
126 SkTDArray<SimplifyAngleTest::Angle> angles;
127 SkTDArray<SimplifyAngleTest::Angle* > angleList;
128 SkTDArray<double> arcTans;
129 size_t x;
130 for (x = 0; x < lineCount; ++x) {
131 SimplifyAngleTest::Angle* angle = angles.append();
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000132 #if HIGH_DEF_ANGLES==0
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000133 if (testFlat) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000134 angle->setFlat(lines[x], SkPath::kLine_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000135 } else {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000136 angle->set(lines[x], SkPath::kLine_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000137 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000138 #else
139 angle->set(lines[x], SkPath::kLine_Verb, 0, x, x + 1, 0, 1);
140 #endif
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000141 double arcTan = atan2(lines[x][0].fX - lines[x][1].fX,
142 lines[x][0].fY - lines[x][1].fY);
143 arcTans.push(arcTan);
144 }
145 for (x = 0; x < lineCount; ++x) {
146 angleList.push(&angles[x]);
147 }
148 QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1);
149 bool first = true;
150 bool wrap = false;
151 double base, last;
152 for (size_t x = 0; x < lineCount; ++x) {
153 const SimplifyAngleTest::Angle* angle = angleList[x];
154 int span = angle->start();
155// SkDebugf("%s [%d] %1.9g (%1.9g,%1.9g %1.9g,%1.9g)\n", __FUNCTION__,
156// span, arcTans[span], lines[span][0].fX, lines[span][0].fY,
157// lines[span][1].fX, lines[span][1].fY);
158 if (first) {
159 base = last = arcTans[span];
160 first = false;
161 continue;
162 }
163 if (last < arcTans[span]) {
164 last = arcTans[span];
165 continue;
166 }
167 if (!wrap) {
168 if (base < arcTans[span]) {
169 SkDebugf("%s !wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]);
170 SkASSERT(0);
171 }
172 last = arcTans[span];
173 wrap = true;
174 continue;
175 }
176 SkDebugf("%s wrap [%d] %g\n", __FUNCTION__, span, arcTans[span]);
177 SkASSERT(0);
178 }
179}
180
181static void testQuads(bool testFlat) {
182 SkTDArray<SimplifyAngleTest::Angle> angles;
183 SkTDArray<SimplifyAngleTest::Angle* > angleList;
184 size_t x;
185 for (x = 0; x < quadCount; ++x) {
186 SimplifyAngleTest::Angle* angle = angles.append();
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000187 #if HIGH_DEF_ANGLES==0
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000188 if (testFlat) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000189 angle->setFlat(quads[x], SkPath::kQuad_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000190 } else {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000191 angle->set(quads[x], SkPath::kQuad_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000192 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000193 #else
194 angle->set(quads[x], SkPath::kQuad_Verb, 0, x, x + 1, 0, 1);
195 #endif
196 }
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000197 for (x = 0; x < quadCount; ++x) {
198 angleList.push(&angles[x]);
199 }
200 QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1);
201 for (size_t x = 0; x < quadCount; ++x) {
202 *angleList[x] < *angleList[x + 1];
203 SkASSERT(x == quadCount - 1 || *angleList[x] < *angleList[x + 1]);
204 const SimplifyAngleTest::Angle* angle = angleList[x];
caryclark@google.comf25edfe2012-06-01 18:20:10 +0000205 if ((int) x != angle->start()) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000206 SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start());
207 SkASSERT(0);
208 }
209 }
210}
211
212static void testCubics(bool testFlat) {
213 SkTDArray<SimplifyAngleTest::Angle> angles;
214 SkTDArray<SimplifyAngleTest::Angle* > angleList;
215 for (size_t x = 0; x < cubicCount; ++x) {
216 SimplifyAngleTest::Angle* angle = angles.append();
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000217 #if HIGH_DEF_ANGLES==0
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000218 if (testFlat) {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000219 angle->setFlat(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000220 } else {
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000221 angle->set(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1);
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000222 }
caryclark@google.com3350c3c2012-08-24 15:24:36 +0000223 #else
224 angle->set(cubics[x], SkPath::kCubic_Verb, 0, x, x + 1, 0, 1);
225 #endif
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000226 angleList.push(angle);
227 }
228 QSort<SimplifyAngleTest::Angle>(angleList.begin(), angleList.end() - 1);
229 for (size_t x = 0; x < cubicCount; ++x) {
230 const SimplifyAngleTest::Angle* angle = angleList[x];
caryclark@google.comf25edfe2012-06-01 18:20:10 +0000231 if ((int) x != angle->start()) {
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000232 SkDebugf("%s [%d] [%d]\n", __FUNCTION__, x, angle->start());
233 SkASSERT(0);
234 }
235 }
236}
237
238static void (*tests[])(bool) = {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000239 testSegments,
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000240 testLines,
241 testQuads,
242 testCubics
243};
244
245static const size_t testCount = sizeof(tests) / sizeof(tests[0]);
246
247static void (*firstTest)(bool) = 0;
248static bool skipAll = false;
249
250void SimplifyAngle_Test() {
251 if (skipAll) {
252 return;
253 }
254 size_t index = 0;
255 if (firstTest) {
256 while (index < testCount && tests[index] != firstTest) {
257 ++index;
258 }
259 }
260 bool firstTestComplete = false;
261 for ( ; index < testCount; ++index) {
caryclark@google.comc899ad92012-08-23 15:24:42 +0000262 (*tests[index])(false); // set to true to exercise setFlat
caryclark@google.comb45a1b42012-05-18 20:50:33 +0000263 firstTestComplete = true;
264 }
caryclark@google.coma3f05fa2012-06-01 17:44:28 +0000265}