1 /*
2 * Copyright 2011 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 "include/utils/SkRandom.h"
9 #include "src/core/SkGeometry.h"
10 #include "src/core/SkPointPriv.h"
11 #include "tests/Test.h"
12
13 #include <array>
14 #include <numeric>
15
nearly_equal(const SkPoint & a,const SkPoint & b)16 static bool nearly_equal(const SkPoint& a, const SkPoint& b) {
17 return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY);
18 }
19
testChopCubic(skiatest::Reporter * reporter)20 static void testChopCubic(skiatest::Reporter* reporter) {
21 /*
22 Inspired by this test, which used to assert that the tValues had dups
23
24 <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2205,4980" />
25 */
26 const SkPoint src[] = {
27 { SkIntToScalar(2190), SkIntToScalar(5130) },
28 { SkIntToScalar(2190), SkIntToScalar(5070) },
29 { SkIntToScalar(2220), SkIntToScalar(5010) },
30 { SkIntToScalar(2205), SkIntToScalar(4980) },
31 };
32 SkPoint dst[13];
33 SkScalar tValues[3];
34 // make sure we don't assert internally
35 int count = SkChopCubicAtMaxCurvature(src, dst, tValues);
36 if (false) { // avoid bit rot, suppress warning
37 REPORTER_ASSERT(reporter, count);
38 }
39 // Make sure src and dst can be the same pointer.
40 SkPoint pts[7];
41 for (int i = 0; i < 7; ++i) {
42 pts[i].set(i, i);
43 }
44 SkChopCubicAt(pts, pts, .5f);
45 for (int i = 0; i < 7; ++i) {
46 REPORTER_ASSERT(reporter, pts[i].fX == pts[i].fY);
47 REPORTER_ASSERT(reporter, pts[i].fX == i * .5f);
48 }
49
50 static const float chopTs[] = {
51 0, 3/83.f, 3/79.f, 3/73.f, 3/71.f, 3/67.f, 3/61.f, 3/59.f, 3/53.f, 3/47.f, 3/43.f, 3/41.f,
52 3/37.f, 3/31.f, 3/29.f, 3/23.f, 3/19.f, 3/17.f, 3/13.f, 3/11.f, 3/7.f, 3/5.f, 1,
53 };
54 float ones[] = {1,1,1,1,1};
55
56 // Ensure an odd number of T values so we exercise the single chop code at the end of
57 // SkChopCubicAt form multiple T.
58 static_assert(SK_ARRAY_COUNT(chopTs) % 2 == 1);
59 static_assert(SK_ARRAY_COUNT(ones) % 2 == 1);
60
61 SkRandom rand;
62 for (int iterIdx = 0; iterIdx < 5; ++iterIdx) {
63 SkPoint pts[4] = {{rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()},
64 {rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()}};
65
66 SkPoint allChops[4 + SK_ARRAY_COUNT(chopTs)*3];
67 SkChopCubicAt(pts, allChops, chopTs, SK_ARRAY_COUNT(chopTs));
68 int i = 3;
69 for (float chopT : chopTs) {
70 // Ensure we chop at approximately the correct points when we chop an entire list.
71 SkPoint expectedPt;
72 SkEvalCubicAt(pts, chopT, &expectedPt, nullptr, nullptr);
73 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].x(), expectedPt.x()));
74 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].y(), expectedPt.y()));
75 if (chopT == 0) {
76 REPORTER_ASSERT(reporter, allChops[i] == pts[0]);
77 }
78 if (chopT == 1) {
79 REPORTER_ASSERT(reporter, allChops[i] == pts[3]);
80 }
81 i += 3;
82
83 // Ensure the middle is exactly degenerate when we chop at two equal points.
84 SkPoint localChops[10];
85 SkChopCubicAt(pts, localChops, chopT, chopT);
86 REPORTER_ASSERT(reporter, localChops[3] == localChops[4]);
87 REPORTER_ASSERT(reporter, localChops[3] == localChops[5]);
88 REPORTER_ASSERT(reporter, localChops[3] == localChops[6]);
89 if (chopT == 0) {
90 // Also ensure the first curve is exactly p0 when we chop at T=0.
91 REPORTER_ASSERT(reporter, localChops[0] == pts[0]);
92 REPORTER_ASSERT(reporter, localChops[1] == pts[0]);
93 REPORTER_ASSERT(reporter, localChops[2] == pts[0]);
94 REPORTER_ASSERT(reporter, localChops[3] == pts[0]);
95 }
96 if (chopT == 1) {
97 // Also ensure the last curve is exactly p3 when we chop at T=1.
98 REPORTER_ASSERT(reporter, localChops[6] == pts[3]);
99 REPORTER_ASSERT(reporter, localChops[7] == pts[3]);
100 REPORTER_ASSERT(reporter, localChops[8] == pts[3]);
101 REPORTER_ASSERT(reporter, localChops[9] == pts[3]);
102 }
103 }
104
105 // Now test what happens when SkChopCubicAt does 0/0 and gets NaN values.
106 SkPoint oneChops[4 + SK_ARRAY_COUNT(ones)*3];
107 SkChopCubicAt(pts, oneChops, ones, SK_ARRAY_COUNT(ones));
108 REPORTER_ASSERT(reporter, oneChops[0] == pts[0]);
109 REPORTER_ASSERT(reporter, oneChops[1] == pts[1]);
110 REPORTER_ASSERT(reporter, oneChops[2] == pts[2]);
111 for (size_t i = 3; i < SK_ARRAY_COUNT(oneChops); ++i) {
112 REPORTER_ASSERT(reporter, oneChops[i] == pts[3]);
113 }
114 }
115 }
116
check_pairs(skiatest::Reporter * reporter,int index,SkScalar t,const char name[],SkScalar x0,SkScalar y0,SkScalar x1,SkScalar y1)117 static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[],
118 SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) {
119 bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1);
120 if (!eq) {
121 SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n",
122 name, index, t, x0, y0, x1, y1);
123 REPORTER_ASSERT(reporter, eq);
124 }
125 }
126
test_evalquadat(skiatest::Reporter * reporter)127 static void test_evalquadat(skiatest::Reporter* reporter) {
128 SkRandom rand;
129 for (int i = 0; i < 1000; ++i) {
130 SkPoint pts[3];
131 for (int j = 0; j < 3; ++j) {
132 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
133 }
134 const SkScalar dt = SK_Scalar1 / 128;
135 SkScalar t = dt;
136 for (int j = 1; j < 128; ++j) {
137 SkPoint r0;
138 SkEvalQuadAt(pts, t, &r0);
139 SkPoint r1 = SkEvalQuadAt(pts, t);
140 check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY);
141
142 SkVector v0;
143 SkEvalQuadAt(pts, t, nullptr, &v0);
144 SkVector v1 = SkEvalQuadTangentAt(pts, t);
145 check_pairs(reporter, i, t, "quad-tan", v0.fX, v0.fY, v1.fX, v1.fY);
146
147 t += dt;
148 }
149 }
150 }
151
test_conic_eval_pos(skiatest::Reporter * reporter,const SkConic & conic,SkScalar t)152 static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
153 SkPoint p0, p1;
154 conic.evalAt(t, &p0, nullptr);
155 p1 = conic.evalAt(t);
156 check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY);
157 }
158
test_conic_eval_tan(skiatest::Reporter * reporter,const SkConic & conic,SkScalar t)159 static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
160 SkVector v0, v1;
161 conic.evalAt(t, nullptr, &v0);
162 v1 = conic.evalTangentAt(t);
163 check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY);
164 }
165
test_conic(skiatest::Reporter * reporter)166 static void test_conic(skiatest::Reporter* reporter) {
167 SkRandom rand;
168 for (int i = 0; i < 1000; ++i) {
169 SkPoint pts[3];
170 for (int j = 0; j < 3; ++j) {
171 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
172 }
173 for (int k = 0; k < 10; ++k) {
174 SkScalar w = rand.nextUScalar1() * 2;
175 SkConic conic(pts, w);
176
177 const SkScalar dt = SK_Scalar1 / 128;
178 SkScalar t = dt;
179 for (int j = 1; j < 128; ++j) {
180 test_conic_eval_pos(reporter, conic, t);
181 test_conic_eval_tan(reporter, conic, t);
182 t += dt;
183 }
184 }
185 }
186 }
187
test_quad_tangents(skiatest::Reporter * reporter)188 static void test_quad_tangents(skiatest::Reporter* reporter) {
189 SkPoint pts[] = {
190 {10, 20}, {10, 20}, {20, 30},
191 {10, 20}, {15, 25}, {20, 30},
192 {10, 20}, {20, 30}, {20, 30},
193 };
194 int count = (int) SK_ARRAY_COUNT(pts) / 3;
195 for (int index = 0; index < count; ++index) {
196 SkConic conic(&pts[index * 3], 0.707f);
197 SkVector start = SkEvalQuadTangentAt(&pts[index * 3], 0);
198 SkVector mid = SkEvalQuadTangentAt(&pts[index * 3], .5f);
199 SkVector end = SkEvalQuadTangentAt(&pts[index * 3], 1);
200 REPORTER_ASSERT(reporter, start.fX && start.fY);
201 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
202 REPORTER_ASSERT(reporter, end.fX && end.fY);
203 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
204 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
205 }
206 }
207
test_conic_tangents(skiatest::Reporter * reporter)208 static void test_conic_tangents(skiatest::Reporter* reporter) {
209 SkPoint pts[] = {
210 { 10, 20}, {10, 20}, {20, 30},
211 { 10, 20}, {15, 25}, {20, 30},
212 { 10, 20}, {20, 30}, {20, 30}
213 };
214 int count = (int) SK_ARRAY_COUNT(pts) / 3;
215 for (int index = 0; index < count; ++index) {
216 SkConic conic(&pts[index * 3], 0.707f);
217 SkVector start = conic.evalTangentAt(0);
218 SkVector mid = conic.evalTangentAt(.5f);
219 SkVector end = conic.evalTangentAt(1);
220 REPORTER_ASSERT(reporter, start.fX && start.fY);
221 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
222 REPORTER_ASSERT(reporter, end.fX && end.fY);
223 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
224 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
225 }
226 }
227
test_this_conic_to_quad(skiatest::Reporter * r,const SkPoint pts[3],SkScalar w)228 static void test_this_conic_to_quad(skiatest::Reporter* r, const SkPoint pts[3], SkScalar w) {
229 SkAutoConicToQuads quadder;
230 const SkPoint* qpts = quadder.computeQuads(pts, w, 0.25);
231 const int qcount = quadder.countQuads();
232 const int pcount = qcount * 2 + 1;
233
234 REPORTER_ASSERT(r, SkPointPriv::AreFinite(qpts, pcount));
235 }
236
237 /**
238 * We need to ensure that when a conic is approximated by quads, that we always return finite
239 * values in the quads.
240 *
241 * Inspired by crbug_627414
242 */
test_conic_to_quads(skiatest::Reporter * reporter)243 static void test_conic_to_quads(skiatest::Reporter* reporter) {
244 const SkPoint triples[] = {
245 { 0, 0 }, { 1, 0 }, { 1, 1 },
246 { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f },
247 { 0, 0 }, { 100000, 0 }, { 100000, 100000 },
248 { 0, 0 }, { 1e30f, 0 }, { 1e30f, 1e30f },
249 };
250 const int N = sizeof(triples) / sizeof(SkPoint);
251
252 for (int i = 0; i < N; i += 3) {
253 const SkPoint* pts = &triples[i];
254
255 SkScalar w = 1e30f;
256 do {
257 w *= 2;
258 test_this_conic_to_quad(reporter, pts, w);
259 } while (SkScalarIsFinite(w));
260 test_this_conic_to_quad(reporter, pts, SK_ScalarNaN);
261 }
262 }
263
test_cubic_tangents(skiatest::Reporter * reporter)264 static void test_cubic_tangents(skiatest::Reporter* reporter) {
265 SkPoint pts[] = {
266 { 10, 20}, {10, 20}, {20, 30}, {30, 40},
267 { 10, 20}, {15, 25}, {20, 30}, {30, 40},
268 { 10, 20}, {20, 30}, {30, 40}, {30, 40},
269 };
270 int count = (int) SK_ARRAY_COUNT(pts) / 4;
271 for (int index = 0; index < count; ++index) {
272 SkConic conic(&pts[index * 3], 0.707f);
273 SkVector start, mid, end;
274 SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr);
275 SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr);
276 SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr);
277 REPORTER_ASSERT(reporter, start.fX && start.fY);
278 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
279 REPORTER_ASSERT(reporter, end.fX && end.fY);
280 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
281 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
282 }
283 }
284
check_cubic_type(skiatest::Reporter * reporter,const std::array<SkPoint,4> & bezierPoints,SkCubicType expectedType,bool undefined=false)285 static void check_cubic_type(skiatest::Reporter* reporter,
286 const std::array<SkPoint, 4>& bezierPoints, SkCubicType expectedType,
287 bool undefined = false) {
288 // Classify the cubic even if the results will be undefined: check for crashes and asserts.
289 SkCubicType actualType = SkClassifyCubic(bezierPoints.data());
290 if (!undefined) {
291 REPORTER_ASSERT(reporter, actualType == expectedType);
292 }
293 }
294
check_cubic_around_rect(skiatest::Reporter * reporter,float x1,float y1,float x2,float y2,bool undefined=false)295 static void check_cubic_around_rect(skiatest::Reporter* reporter,
296 float x1, float y1, float x2, float y2,
297 bool undefined = false) {
298 static constexpr SkCubicType expectations[24] = {
299 SkCubicType::kLoop,
300 SkCubicType::kCuspAtInfinity,
301 SkCubicType::kLocalCusp,
302 SkCubicType::kLocalCusp,
303 SkCubicType::kCuspAtInfinity,
304 SkCubicType::kLoop,
305 SkCubicType::kCuspAtInfinity,
306 SkCubicType::kLoop,
307 SkCubicType::kCuspAtInfinity,
308 SkCubicType::kLoop,
309 SkCubicType::kLocalCusp,
310 SkCubicType::kLocalCusp,
311 SkCubicType::kLocalCusp,
312 SkCubicType::kLocalCusp,
313 SkCubicType::kLoop,
314 SkCubicType::kCuspAtInfinity,
315 SkCubicType::kLoop,
316 SkCubicType::kCuspAtInfinity,
317 SkCubicType::kLoop,
318 SkCubicType::kCuspAtInfinity,
319 SkCubicType::kLocalCusp,
320 SkCubicType::kLocalCusp,
321 SkCubicType::kCuspAtInfinity,
322 SkCubicType::kLoop,
323 };
324 SkPoint points[] = {{x1, y1}, {x2, y1}, {x2, y2}, {x1, y2}};
325 std::array<SkPoint, 4> bezier;
326 for (int i=0; i < 4; ++i) {
327 bezier[0] = points[i];
328 for (int j=0; j < 3; ++j) {
329 int jidx = (j < i) ? j : j+1;
330 bezier[1] = points[jidx];
331 for (int k=0, kidx=0; k < 2; ++k, ++kidx) {
332 for (int n = 0; n < 2; ++n) {
333 kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx;
334 }
335 bezier[2] = points[kidx];
336 for (int l = 0; l < 4; ++l) {
337 if (l != i && l != jidx && l != kidx) {
338 bezier[3] = points[l];
339 break;
340 }
341 }
342 check_cubic_type(reporter, bezier, expectations[i*6 + j*2 + k], undefined);
343 }
344 }
345 }
346 for (int i=0; i < 4; ++i) {
347 bezier[0] = points[i];
348 for (int j=0; j < 3; ++j) {
349 int jidx = (j < i) ? j : j+1;
350 bezier[1] = points[jidx];
351 bezier[2] = points[jidx];
352 for (int k=0, kidx=0; k < 2; ++k, ++kidx) {
353 for (int n = 0; n < 2; ++n) {
354 kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx;
355 }
356 bezier[3] = points[kidx];
357 check_cubic_type(reporter, bezier, SkCubicType::kSerpentine, undefined);
358 }
359 }
360 }
361 }
362
363 static std::array<SkPoint, 4> kSerpentines[] = {
364 {{{149.325f, 107.705f}, {149.325f, 103.783f}, {151.638f, 100.127f}, {156.263f, 96.736f}}},
365 {{{225.694f, 223.15f}, {209.831f, 224.837f}, {195.994f, 230.237f}, {184.181f, 239.35f}}},
366 {{{4.873f, 5.581f}, {5.083f, 5.2783f}, {5.182f, 4.8593f}, {5.177f, 4.3242f}}},
367 {{{285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}}}
368 };
369
370 static std::array<SkPoint, 4> kLoops[] = {
371 {{{635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}}},
372 {{{653.050f, 725.049f}, {663.000f, 176.000f}, {1189.000f, 508.000f}, {288.050f, 564.950f}}},
373 {{{631.050f, 478.049f}, {730.000f, 302.000f}, {870.000f, 350.000f}, {905.050f, 528.950f}}},
374 {{{631.050f, 478.0499f}, {221.000f, 230.000f}, {1265.000f, 451.000f}, {905.050f, 528.950f}}}
375 };
376
377 static std::array<SkPoint, 4> kLinearCubics[] = {
378 {{{0, 0}, {0, 1}, {0, 2}, {0, 3}}}, // 0-degree flat line.
379 {{{0, 0}, {1, 0}, {1, 0}, {0, 0}}}, // 180-degree flat line
380 {{{0, 1}, {0, 0}, {0, 2}, {0, 3}}}, // 180-degree flat line
381 {{{0, 1}, {0, 0}, {0, 3}, {0, 2}}}, // 360-degree flat line
382 {{{0, 0}, {2, 0}, {1, 0}, {64, 0}}}, // 360-degree flat line
383 {{{1, 0}, {0, 0}, {3, 0}, {-64, 0}}} // 360-degree flat line
384 };
385
test_classify_cubic(skiatest::Reporter * reporter)386 static void test_classify_cubic(skiatest::Reporter* reporter) {
387 for (const auto& serp : kSerpentines) {
388 check_cubic_type(reporter, serp, SkCubicType::kSerpentine);
389 }
390 for (const auto& loop : kLoops) {
391 check_cubic_type(reporter, loop, SkCubicType::kLoop);
392 }
393 for (const auto& loop : kLinearCubics) {
394 check_cubic_type(reporter, loop, SkCubicType::kLineOrPoint);
395 }
396 check_cubic_around_rect(reporter, 0, 0, 1, 1);
397 check_cubic_around_rect(reporter,
398 -std::numeric_limits<float>::max(),
399 -std::numeric_limits<float>::max(),
400 +std::numeric_limits<float>::max(),
401 +std::numeric_limits<float>::max());
402 check_cubic_around_rect(reporter, 1, 1,
403 +std::numeric_limits<float>::min(),
404 +std::numeric_limits<float>::max());
405 check_cubic_around_rect(reporter,
406 -std::numeric_limits<float>::min(),
407 -std::numeric_limits<float>::min(),
408 +std::numeric_limits<float>::min(),
409 +std::numeric_limits<float>::min());
410 check_cubic_around_rect(reporter, +1, -std::numeric_limits<float>::min(), -1, -1);
411 check_cubic_around_rect(reporter,
412 -std::numeric_limits<float>::infinity(),
413 -std::numeric_limits<float>::infinity(),
414 +std::numeric_limits<float>::infinity(),
415 +std::numeric_limits<float>::infinity(),
416 true);
417 check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::infinity(), true);
418 check_cubic_around_rect(reporter,
419 -std::numeric_limits<float>::quiet_NaN(),
420 -std::numeric_limits<float>::quiet_NaN(),
421 +std::numeric_limits<float>::quiet_NaN(),
422 +std::numeric_limits<float>::quiet_NaN(),
423 true);
424 check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::quiet_NaN(), true);
425 }
426
427 static std::array<SkPoint, 4> kCusps[] = {
428 {{{0, 0}, {1, 1}, {1, 0}, {0, 1}}},
429 {{{0, 0}, {1, 1}, {0, 1}, {1, 0}}},
430 {{{0, 1}, {1, 0}, {0, 0}, {1, 1}}},
431 {{{0, 1}, {1, 0}, {1, 1}, {0, 0}}},
432 };
433
test_cubic_cusps(skiatest::Reporter * reporter)434 static void test_cubic_cusps(skiatest::Reporter* reporter) {
435 std::array<SkPoint, 4> noCusps[] = {
436 {{{0, 0}, {1, 1}, {2, 2}, {3, 3}}},
437 {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}},
438 {{{0, 0}, {1, 0}, {2, 1}, {2, 2}}},
439 {{{0, 0}, {1, 0}, {1, 1}, {2, 1}}},
440 };
441 for (auto noCusp : noCusps) {
442 REPORTER_ASSERT(reporter, SkFindCubicCusp(noCusp.data()) < 0);
443 }
444 for (auto cusp : kCusps) {
445 REPORTER_ASSERT(reporter, SkFindCubicCusp(cusp.data()) > 0);
446 }
447 }
448
449 static SkMatrix kSkewMatrices[] = {
450 SkMatrix::MakeAll(1,0,0, 0,1,0, 0,0,1),
451 SkMatrix::MakeAll(1,-1,0, 1,1,0, 0,0,1),
452 SkMatrix::MakeAll(.889f,.553f,0, -.443f,.123f,0, 0,0,1),
453 };
454
test_chop_quad_at_midtangent(skiatest::Reporter * reporter,const SkPoint pts[3])455 static void test_chop_quad_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[3]) {
456 constexpr float kTolerance = 1e-3f;
457 for (const SkMatrix& m : kSkewMatrices) {
458 SkPoint mapped[3];
459 m.mapPoints(mapped, pts, 3);
460 float fullRotation = SkMeasureQuadRotation(pts);
461 SkPoint chopped[5];
462 SkChopQuadAtMidTangent(pts, chopped);
463 float leftRotation = SkMeasureQuadRotation(chopped);
464 float rightRotation = SkMeasureQuadRotation(chopped+2);
465 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, fullRotation/2, kTolerance));
466 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, fullRotation/2, kTolerance));
467 }
468 }
469
test_chop_cubic_at_midtangent(skiatest::Reporter * reporter,const SkPoint pts[4],SkCubicType cubicType)470 static void test_chop_cubic_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[4],
471 SkCubicType cubicType) {
472 constexpr float kTolerance = 1e-3f;
473 int n = SK_ARRAY_COUNT(kSkewMatrices);
474 if (cubicType == SkCubicType::kLocalCusp || cubicType == SkCubicType::kLineOrPoint) {
475 // FP precision isn't always enough to get the exact correct T value of the mid-tangent on
476 // cusps and lines. Only test the identity matrix and the matrix with all 1's.
477 n = 2;
478 }
479 for (int i = 0; i < n; ++i) {
480 SkPoint mapped[4];
481 kSkewMatrices[i].mapPoints(mapped, pts, 4);
482 float fullRotation = SkMeasureNonInflectCubicRotation(mapped);
483 SkPoint chopped[7];
484 SkChopCubicAtMidTangent(mapped, chopped);
485 float leftRotation = SkMeasureNonInflectCubicRotation(chopped);
486 float rightRotation = SkMeasureNonInflectCubicRotation(chopped+3);
487 if (cubicType == SkCubicType::kLineOrPoint &&
488 (SkScalarNearlyEqual(fullRotation, 2*SK_ScalarPI, kTolerance) ||
489 SkScalarNearlyEqual(fullRotation, 0, kTolerance))) {
490 // 0- and 360-degree flat lines don't have single points of midtangent.
491 // (tangent == midtangent at every point on these curves except the cusp points.)
492 // Instead verify the promise from SkChopCubicAtMidTangent that neither side will rotate
493 // more than 180 degrees.
494 REPORTER_ASSERT(reporter, std::abs(leftRotation) - kTolerance <= SK_ScalarPI);
495 REPORTER_ASSERT(reporter, std::abs(rightRotation) - kTolerance <= SK_ScalarPI);
496 continue;
497 }
498 float expectedChoppedRotation = fullRotation/2;
499 if (cubicType == SkCubicType::kLocalCusp ||
500 (cubicType == SkCubicType::kLineOrPoint &&
501 SkScalarNearlyEqual(fullRotation, SK_ScalarPI, kTolerance))) {
502 // If we chop a cubic at a cusp, we lose 180 degrees of rotation.
503 expectedChoppedRotation = (fullRotation - SK_ScalarPI)/2;
504 }
505 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, expectedChoppedRotation,
506 kTolerance));
507 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, expectedChoppedRotation,
508 kTolerance));
509 }
510 }
511
512 static std::array<SkPoint, 3> kQuads[] = {
513 {{{10, 20}, {15, 35}, {30, 40}}},
514 {{{176.324f, 392.705f}, {719.325f, 205.782f}, {297.263f, 347.735f}}},
515 {{{652.050f, 602.049f}, {481.000f, 533.000f}, {288.050f, 564.950f}}},
516 {{{460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}}},
517 {{{359.050f, 578.049f}, {759.000f, 274.000f}, {288.050f, 564.950f}}}
518 };
519
lerp(const SkPoint & a,const SkPoint & b,float t)520 SkPoint lerp(const SkPoint& a, const SkPoint& b, float t) {
521 return a * (1 - t) + b * t;
522 }
523
test_measure_rotation(skiatest::Reporter * reporter)524 static void test_measure_rotation(skiatest::Reporter* reporter) {
525 static SkPoint kFlatCubic[4] = {{0, 0}, {0, 1}, {0, 2}, {0, 3}};
526 REPORTER_ASSERT(reporter, SkScalarNearlyZero(SkMeasureNonInflectCubicRotation(kFlatCubic)));
527
528 static SkPoint kFlatCubic180_1[4] = {{0, 0}, {1, 0}, {3, 0}, {2, 0}};
529 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_1),
530 SK_ScalarPI));
531
532 static SkPoint kFlatCubic180_2[4] = {{0, 1}, {0, 0}, {0, 2}, {0, 3}};
533 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_2),
534 SK_ScalarPI));
535
536 static SkPoint kFlatCubic360[4] = {{0, 1}, {0, 0}, {0, 3}, {0, 2}};
537 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic360),
538 2*SK_ScalarPI));
539
540 static SkPoint kSquare180[4] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
541 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kSquare180),
542 SK_ScalarPI));
543
544 auto checkQuadRotation = [=](const SkPoint pts[3], float expectedRotation) {
545 float r = SkMeasureQuadRotation(pts);
546 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(r, expectedRotation));
547
548 SkPoint cubic1[4] = {pts[0], pts[0], pts[1], pts[2]};
549 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic1),
550 expectedRotation));
551
552 SkPoint cubic2[4] = {pts[0], pts[1], pts[1], pts[2]};
553 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic2),
554 expectedRotation));
555
556 SkPoint cubic3[4] = {pts[0], pts[1], pts[2], pts[2]};
557 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic3),
558 expectedRotation));
559 };
560
561 static SkPoint kFlatQuad[4] = {{0, 0}, {0, 1}, {0, 2}};
562 checkQuadRotation(kFlatQuad, 0);
563
564 static SkPoint kFlatQuad180_1[4] = {{1, 0}, {0, 0}, {2, 0}};
565 checkQuadRotation(kFlatQuad180_1, SK_ScalarPI);
566
567 static SkPoint kFlatQuad180_2[4] = {{0, 0}, {0, 2}, {0, 1}};
568 checkQuadRotation(kFlatQuad180_2, SK_ScalarPI);
569
570 static SkPoint kTri120[3] = {{0, 0}, {.5f, std::sqrt(3.f)/2}, {1, 0}};
571 checkQuadRotation(kTri120, 2*SK_ScalarPI/3);
572 }
573
test_chop_at_midtangent(skiatest::Reporter * reporter)574 static void test_chop_at_midtangent(skiatest::Reporter* reporter) {
575 SkPoint chops[10];
576 for (const auto& serp : kSerpentines) {
577 REPORTER_ASSERT(reporter, SkClassifyCubic(serp.data()) == SkCubicType::kSerpentine);
578 int n = SkChopCubicAtInflections(serp.data(), chops);
579 for (int i = 0; i < n; ++i) {
580 test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kSerpentine);
581 }
582 }
583 for (const auto& loop : kLoops) {
584 REPORTER_ASSERT(reporter, SkClassifyCubic(loop.data()) == SkCubicType::kLoop);
585 test_chop_cubic_at_midtangent(reporter, loop.data(), SkCubicType::kLoop);
586 }
587 for (const auto& line : kLinearCubics) {
588 REPORTER_ASSERT(reporter, SkClassifyCubic(line.data()) == SkCubicType::kLineOrPoint);
589 test_chop_cubic_at_midtangent(reporter, line.data(), SkCubicType::kLineOrPoint);
590 }
591 for (const auto& cusp : kCusps) {
592 REPORTER_ASSERT(reporter, SkClassifyCubic(cusp.data()) == SkCubicType::kLocalCusp);
593 test_chop_cubic_at_midtangent(reporter, cusp.data(), SkCubicType::kLocalCusp);
594 }
595 for (const auto& quad : kQuads) {
596 test_chop_quad_at_midtangent(reporter, quad.data());
597 SkPoint asCubic[4] = {
598 quad[0], lerp(quad[0], quad[1], 2/3.f), lerp(quad[1], quad[2], 1/3.f), quad[2]};
599 test_chop_cubic_at_midtangent(reporter, asCubic, SkCubicType::kQuadratic);
600 }
601
602 static const SkPoint kExactQuad[4] = {{0,0}, {6,2}, {10,2}, {12,0}};
603 REPORTER_ASSERT(reporter, SkClassifyCubic(kExactQuad) == SkCubicType::kQuadratic);
604 test_chop_cubic_at_midtangent(reporter, kExactQuad, SkCubicType::kQuadratic);
605
606 static const SkPoint kExactCuspAtInf[4] = {{0,0}, {1,0}, {0,1}, {1,1}};
607 REPORTER_ASSERT(reporter, SkClassifyCubic(kExactCuspAtInf) == SkCubicType::kCuspAtInfinity);
608 int n = SkChopCubicAtInflections(kExactCuspAtInf, chops);
609 for (int i = 0; i < n; ++i) {
610 test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kCuspAtInfinity);
611 }
612 }
613
DEF_TEST(Geometry,reporter)614 DEF_TEST(Geometry, reporter) {
615 SkPoint pts[5];
616
617 pts[0].set(0, 0);
618 pts[1].set(100, 50);
619 pts[2].set(0, 100);
620
621 int count = SkChopQuadAtMaxCurvature(pts, pts); // Ensure src and dst can be the same pointer.
622 REPORTER_ASSERT(reporter, count == 1 || count == 2);
623
624 pts[0].set(0, 0);
625 pts[1].set(3, 0);
626 pts[2].set(3, 3);
627 SkConvertQuadToCubic(pts, pts);
628 const SkPoint cubic[] = {
629 { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 },
630 };
631 for (int i = 0; i < 4; ++i) {
632 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], pts[i]));
633 }
634
635 testChopCubic(reporter);
636 test_evalquadat(reporter);
637 test_conic(reporter);
638 test_cubic_tangents(reporter);
639 test_quad_tangents(reporter);
640 test_conic_tangents(reporter);
641 test_conic_to_quads(reporter);
642 test_classify_cubic(reporter);
643 test_cubic_cusps(reporter);
644 test_measure_rotation(reporter);
645 test_chop_at_midtangent(reporter);
646 }
647