blob: 3daf47b8c4a0b108f0edc3a01508121ca1f761b9 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +00007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/utils/SkRandom.h"
9#include "src/core/SkGeometry.h"
10#include "src/core/SkPointPriv.h"
11#include "tests/Test.h"
Hal Canary8a001442018-09-19 11:31:27 -040012
Chris Dalton91982ee2017-07-14 14:04:52 -060013#include <array>
Chris Daltonfc31be42017-11-08 17:04:47 -070014#include <numeric>
reed@android.comd8730ea2009-02-27 22:06:06 +000015
reed@google.com6fc321a2011-07-27 13:54:36 +000016static bool nearly_equal(const SkPoint& a, const SkPoint& b) {
17 return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY);
18}
19
reed@google.com087d5aa2012-02-29 20:59:24 +000020static void testChopCubic(skiatest::Reporter* reporter) {
21 /*
22 Inspired by this test, which used to assert that the tValues had dups
rmistry@google.comd6176b02012-08-23 18:14:13 +000023
reed@google.com087d5aa2012-02-29 20:59:24 +000024 <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];
reed@google.comc256cd12012-02-29 21:57:36 +000034 // make sure we don't assert internally
reed@google.com087d5aa2012-02-29 20:59:24 +000035 int count = SkChopCubicAtMaxCurvature(src, dst, tValues);
caryclark@google.com42639cd2012-06-06 12:03:39 +000036 if (false) { // avoid bit rot, suppress warning
37 REPORTER_ASSERT(reporter, count);
38 }
Chris Dalton1208e0f2018-08-13 00:20:33 -060039 // 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 }
Chris Dalton81b270a2020-10-16 15:12:10 -060049
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 }
reed@google.com087d5aa2012-02-29 20:59:24 +0000115}
116
reed40b7dd52015-03-20 06:01:08 -0700117static 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
reed65cb2cd2015-03-19 10:18:47 -0700127static 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 }
reed65cb2cd2015-03-19 10:18:47 -0700134 const SkScalar dt = SK_Scalar1 / 128;
reed40b7dd52015-03-20 06:01:08 -0700135 SkScalar t = dt;
136 for (int j = 1; j < 128; ++j) {
reed65cb2cd2015-03-19 10:18:47 -0700137 SkPoint r0;
138 SkEvalQuadAt(pts, t, &r0);
139 SkPoint r1 = SkEvalQuadAt(pts, t);
reed40b7dd52015-03-20 06:01:08 -0700140 check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY);
halcanary9d524f22016-03-29 09:03:52 -0700141
reed40b7dd52015-03-20 06:01:08 -0700142 SkVector v0;
halcanary96fcdcc2015-08-27 07:41:13 -0700143 SkEvalQuadAt(pts, t, nullptr, &v0);
reed40b7dd52015-03-20 06:01:08 -0700144 SkVector v1 = SkEvalQuadTangentAt(pts, t);
145 check_pairs(reporter, i, t, "quad-tan", v0.fX, v0.fY, v1.fX, v1.fY);
reed40b7dd52015-03-20 06:01:08 -0700146
reed65cb2cd2015-03-19 10:18:47 -0700147 t += dt;
148 }
149 }
150}
151
reedb6402032015-03-20 13:23:43 -0700152static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
153 SkPoint p0, p1;
halcanary96fcdcc2015-08-27 07:41:13 -0700154 conic.evalAt(t, &p0, nullptr);
reedb6402032015-03-20 13:23:43 -0700155 p1 = conic.evalAt(t);
156 check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY);
157}
158
159static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
160 SkVector v0, v1;
halcanary96fcdcc2015-08-27 07:41:13 -0700161 conic.evalAt(t, nullptr, &v0);
reedb6402032015-03-20 13:23:43 -0700162 v1 = conic.evalTangentAt(t);
163 check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY);
164}
165
reedb6402032015-03-20 13:23:43 -0700166static 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);
reedb6402032015-03-20 13:23:43 -0700176
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
caryclark45398df2015-08-25 13:19:06 -0700188static 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
208static 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
reedb1b12f82016-07-13 10:56:53 -0700228static 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
Cary Clarkdf429f32017-11-08 11:44:31 -0500234 REPORTER_ASSERT(r, SkPointPriv::AreFinite(qpts, pcount));
reedb1b12f82016-07-13 10:56:53 -0700235}
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 */
243static void test_conic_to_quads(skiatest::Reporter* reporter) {
244 const SkPoint triples[] = {
245 { 0, 0 }, { 1, 0 }, { 1, 1 },
msarett16ef4652016-07-13 13:08:44 -0700246 { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f },
reedb1b12f82016-07-13 10:56:53 -0700247 { 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
reedb1b12f82016-07-13 10:56:53 -0700255 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
caryclark45398df2015-08-25 13:19:06 -0700264static 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;
halcanary96fcdcc2015-08-27 07:41:13 -0700274 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);
caryclark45398df2015-08-25 13:19:06 -0700277 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
Chris Dalton91982ee2017-07-14 14:04:52 -0600285static void check_cubic_type(skiatest::Reporter* reporter,
Chris Daltonfc31be42017-11-08 17:04:47 -0700286 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.
Chris Dalton91982ee2017-07-14 14:04:52 -0600289 SkCubicType actualType = SkClassifyCubic(bezierPoints.data());
Chris Daltonfc31be42017-11-08 17:04:47 -0700290 if (!undefined) {
291 REPORTER_ASSERT(reporter, actualType == expectedType);
292 }
293}
294
295static 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 }
Chris Dalton91982ee2017-07-14 14:04:52 -0600361}
362
Chris Daltonc4ec2912020-08-21 11:27:15 -0600363static 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
370static 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
377static 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
Chris Daltonfd53e632020-09-17 19:21:44 -0600381 {{{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
Chris Daltonc4ec2912020-08-21 11:27:15 -0600384};
385
Chris Dalton91982ee2017-07-14 14:04:52 -0600386static void test_classify_cubic(skiatest::Reporter* reporter) {
Chris Daltonc4ec2912020-08-21 11:27:15 -0600387 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 }
Chris Daltonfc31be42017-11-08 17:04:47 -0700396 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);
Chris Dalton91982ee2017-07-14 14:04:52 -0600425}
426
Chris Daltonc4ec2912020-08-21 11:27:15 -0600427static 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
Cary Clarkdb160012018-08-31 15:07:51 -0400434static 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 }
Chris Daltonc4ec2912020-08-21 11:27:15 -0600444 for (auto cusp : kCusps) {
Mike Kleindc594f22020-08-20 01:46:05 +0000445 REPORTER_ASSERT(reporter, SkFindCubicCusp(cusp.data()) > 0);
Chris Dalton26766ad2020-08-19 08:31:37 -0600446 }
447}
448
Chris Daltonc4ec2912020-08-21 11:27:15 -0600449static 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
455static 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
470static 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);
Chris Daltonfd53e632020-09-17 19:21:44 -0600487 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 }
Chris Daltonc4ec2912020-08-21 11:27:15 -0600498 float expectedChoppedRotation = fullRotation/2;
499 if (cubicType == SkCubicType::kLocalCusp ||
500 (cubicType == SkCubicType::kLineOrPoint &&
501 SkScalarNearlyEqual(fullRotation, SK_ScalarPI, kTolerance))) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600502 // If we chop a cubic at a cusp, we lose 180 degrees of rotation.
Chris Daltonc4ec2912020-08-21 11:27:15 -0600503 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
512static 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
520SkPoint lerp(const SkPoint& a, const SkPoint& b, float t) {
521 return a * (1 - t) + b * t;
522}
523
524static 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
574static void test_chop_at_midtangent(skiatest::Reporter* reporter) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600575 SkPoint chops[10];
Chris Daltonc4ec2912020-08-21 11:27:15 -0600576 for (const auto& serp : kSerpentines) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600577 REPORTER_ASSERT(reporter, SkClassifyCubic(serp.data()) == SkCubicType::kSerpentine);
578 int n = SkChopCubicAtInflections(serp.data(), chops);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600579 for (int i = 0; i < n; ++i) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600580 test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kSerpentine);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600581 }
582 }
583 for (const auto& loop : kLoops) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600584 REPORTER_ASSERT(reporter, SkClassifyCubic(loop.data()) == SkCubicType::kLoop);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600585 test_chop_cubic_at_midtangent(reporter, loop.data(), SkCubicType::kLoop);
586 }
587 for (const auto& line : kLinearCubics) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600588 REPORTER_ASSERT(reporter, SkClassifyCubic(line.data()) == SkCubicType::kLineOrPoint);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600589 test_chop_cubic_at_midtangent(reporter, line.data(), SkCubicType::kLineOrPoint);
590 }
591 for (const auto& cusp : kCusps) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600592 REPORTER_ASSERT(reporter, SkClassifyCubic(cusp.data()) == SkCubicType::kLocalCusp);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600593 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());
Chris Daltonfd53e632020-09-17 19:21:44 -0600597 SkPoint asCubic[4] = {
Chris Daltonc4ec2912020-08-21 11:27:15 -0600598 quad[0], lerp(quad[0], quad[1], 2/3.f), lerp(quad[1], quad[2], 1/3.f), quad[2]};
Chris Daltonfd53e632020-09-17 19:21:44 -0600599 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);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600611 }
612}
613
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000614DEF_TEST(Geometry, reporter) {
Chris Dalton1208e0f2018-08-13 00:20:33 -0600615 SkPoint pts[5];
reed@android.comd8730ea2009-02-27 22:06:06 +0000616
617 pts[0].set(0, 0);
618 pts[1].set(100, 50);
619 pts[2].set(0, 100);
620
Chris Dalton1208e0f2018-08-13 00:20:33 -0600621 int count = SkChopQuadAtMaxCurvature(pts, pts); // Ensure src and dst can be the same pointer.
reed@android.comd8730ea2009-02-27 22:06:06 +0000622 REPORTER_ASSERT(reporter, count == 1 || count == 2);
reed@google.com6fc321a2011-07-27 13:54:36 +0000623
Brian Salomon1c42fcf2021-04-28 12:09:39 -0400624 // This previously crashed because the computed t of max curvature is NaN and SkChopQuadAt
625 // asserts that the passed t is in 0..1. Passes by not asserting.
626 pts[0].set(15.1213f, 7.77647f);
627 pts[1].set(6.2168e+19f, 1.51338e+20f);
628 pts[2].set(1.4579e+19f, 1.55558e+21f);
629 count = SkChopQuadAtMaxCurvature(pts, pts);
630
reed@google.com6fc321a2011-07-27 13:54:36 +0000631 pts[0].set(0, 0);
reeddaee7ea2015-03-26 20:22:33 -0700632 pts[1].set(3, 0);
633 pts[2].set(3, 3);
Chris Dalton1208e0f2018-08-13 00:20:33 -0600634 SkConvertQuadToCubic(pts, pts);
reed@google.com6fc321a2011-07-27 13:54:36 +0000635 const SkPoint cubic[] = {
reeddaee7ea2015-03-26 20:22:33 -0700636 { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 },
reed@google.com6fc321a2011-07-27 13:54:36 +0000637 };
638 for (int i = 0; i < 4; ++i) {
Chris Dalton1208e0f2018-08-13 00:20:33 -0600639 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], pts[i]));
reed@google.com6fc321a2011-07-27 13:54:36 +0000640 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000641
reed@google.com087d5aa2012-02-29 20:59:24 +0000642 testChopCubic(reporter);
reed65cb2cd2015-03-19 10:18:47 -0700643 test_evalquadat(reporter);
reedb6402032015-03-20 13:23:43 -0700644 test_conic(reporter);
caryclark45398df2015-08-25 13:19:06 -0700645 test_cubic_tangents(reporter);
646 test_quad_tangents(reporter);
647 test_conic_tangents(reporter);
reedb1b12f82016-07-13 10:56:53 -0700648 test_conic_to_quads(reporter);
Chris Dalton91982ee2017-07-14 14:04:52 -0600649 test_classify_cubic(reporter);
Cary Clarkdb160012018-08-31 15:07:51 -0400650 test_cubic_cusps(reporter);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600651 test_measure_rotation(reporter);
652 test_chop_at_midtangent(reporter);
reed@android.comd8730ea2009-02-27 22:06:06 +0000653}