blob: 00e061123c94306fa0dd29faad64ad1def47cefd [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.
John Stiles97659732021-08-12 10:48:09 -040040 {
41 SkPoint pts[7];
42 for (int i = 0; i < 7; ++i) {
43 pts[i].set(i, i);
44 }
45 SkChopCubicAt(pts, pts, .5f);
46 for (int i = 0; i < 7; ++i) {
47 REPORTER_ASSERT(reporter, pts[i].fX == pts[i].fY);
48 REPORTER_ASSERT(reporter, pts[i].fX == i * .5f);
49 }
Chris Dalton1208e0f2018-08-13 00:20:33 -060050 }
Chris Dalton81b270a2020-10-16 15:12:10 -060051
52 static const float chopTs[] = {
53 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,
54 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,
55 };
56 float ones[] = {1,1,1,1,1};
57
58 // Ensure an odd number of T values so we exercise the single chop code at the end of
59 // SkChopCubicAt form multiple T.
60 static_assert(SK_ARRAY_COUNT(chopTs) % 2 == 1);
61 static_assert(SK_ARRAY_COUNT(ones) % 2 == 1);
62
63 SkRandom rand;
64 for (int iterIdx = 0; iterIdx < 5; ++iterIdx) {
65 SkPoint pts[4] = {{rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()},
66 {rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()}};
67
68 SkPoint allChops[4 + SK_ARRAY_COUNT(chopTs)*3];
69 SkChopCubicAt(pts, allChops, chopTs, SK_ARRAY_COUNT(chopTs));
70 int i = 3;
71 for (float chopT : chopTs) {
72 // Ensure we chop at approximately the correct points when we chop an entire list.
73 SkPoint expectedPt;
74 SkEvalCubicAt(pts, chopT, &expectedPt, nullptr, nullptr);
75 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].x(), expectedPt.x()));
76 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].y(), expectedPt.y()));
77 if (chopT == 0) {
78 REPORTER_ASSERT(reporter, allChops[i] == pts[0]);
79 }
80 if (chopT == 1) {
81 REPORTER_ASSERT(reporter, allChops[i] == pts[3]);
82 }
83 i += 3;
84
85 // Ensure the middle is exactly degenerate when we chop at two equal points.
86 SkPoint localChops[10];
87 SkChopCubicAt(pts, localChops, chopT, chopT);
88 REPORTER_ASSERT(reporter, localChops[3] == localChops[4]);
89 REPORTER_ASSERT(reporter, localChops[3] == localChops[5]);
90 REPORTER_ASSERT(reporter, localChops[3] == localChops[6]);
91 if (chopT == 0) {
92 // Also ensure the first curve is exactly p0 when we chop at T=0.
93 REPORTER_ASSERT(reporter, localChops[0] == pts[0]);
94 REPORTER_ASSERT(reporter, localChops[1] == pts[0]);
95 REPORTER_ASSERT(reporter, localChops[2] == pts[0]);
96 REPORTER_ASSERT(reporter, localChops[3] == pts[0]);
97 }
98 if (chopT == 1) {
99 // Also ensure the last curve is exactly p3 when we chop at T=1.
100 REPORTER_ASSERT(reporter, localChops[6] == pts[3]);
101 REPORTER_ASSERT(reporter, localChops[7] == pts[3]);
102 REPORTER_ASSERT(reporter, localChops[8] == pts[3]);
103 REPORTER_ASSERT(reporter, localChops[9] == pts[3]);
104 }
105 }
106
107 // Now test what happens when SkChopCubicAt does 0/0 and gets NaN values.
108 SkPoint oneChops[4 + SK_ARRAY_COUNT(ones)*3];
109 SkChopCubicAt(pts, oneChops, ones, SK_ARRAY_COUNT(ones));
110 REPORTER_ASSERT(reporter, oneChops[0] == pts[0]);
111 REPORTER_ASSERT(reporter, oneChops[1] == pts[1]);
112 REPORTER_ASSERT(reporter, oneChops[2] == pts[2]);
John Stilesfa088a62021-08-12 23:01:41 -0400113 for (size_t index = 3; index < SK_ARRAY_COUNT(oneChops); ++index) {
114 REPORTER_ASSERT(reporter, oneChops[index] == pts[3]);
Chris Dalton81b270a2020-10-16 15:12:10 -0600115 }
116 }
reed@google.com087d5aa2012-02-29 20:59:24 +0000117}
118
reed40b7dd52015-03-20 06:01:08 -0700119static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[],
120 SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) {
121 bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1);
122 if (!eq) {
123 SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n",
124 name, index, t, x0, y0, x1, y1);
125 REPORTER_ASSERT(reporter, eq);
126 }
127}
128
reed65cb2cd2015-03-19 10:18:47 -0700129static void test_evalquadat(skiatest::Reporter* reporter) {
130 SkRandom rand;
131 for (int i = 0; i < 1000; ++i) {
132 SkPoint pts[3];
133 for (int j = 0; j < 3; ++j) {
134 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
135 }
reed65cb2cd2015-03-19 10:18:47 -0700136 const SkScalar dt = SK_Scalar1 / 128;
reed40b7dd52015-03-20 06:01:08 -0700137 SkScalar t = dt;
138 for (int j = 1; j < 128; ++j) {
reed65cb2cd2015-03-19 10:18:47 -0700139 SkPoint r0;
140 SkEvalQuadAt(pts, t, &r0);
141 SkPoint r1 = SkEvalQuadAt(pts, t);
reed40b7dd52015-03-20 06:01:08 -0700142 check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY);
halcanary9d524f22016-03-29 09:03:52 -0700143
reed40b7dd52015-03-20 06:01:08 -0700144 SkVector v0;
halcanary96fcdcc2015-08-27 07:41:13 -0700145 SkEvalQuadAt(pts, t, nullptr, &v0);
reed40b7dd52015-03-20 06:01:08 -0700146 SkVector v1 = SkEvalQuadTangentAt(pts, t);
147 check_pairs(reporter, i, t, "quad-tan", v0.fX, v0.fY, v1.fX, v1.fY);
reed40b7dd52015-03-20 06:01:08 -0700148
reed65cb2cd2015-03-19 10:18:47 -0700149 t += dt;
150 }
151 }
152}
153
reedb6402032015-03-20 13:23:43 -0700154static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
155 SkPoint p0, p1;
halcanary96fcdcc2015-08-27 07:41:13 -0700156 conic.evalAt(t, &p0, nullptr);
reedb6402032015-03-20 13:23:43 -0700157 p1 = conic.evalAt(t);
158 check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY);
159}
160
161static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
162 SkVector v0, v1;
halcanary96fcdcc2015-08-27 07:41:13 -0700163 conic.evalAt(t, nullptr, &v0);
reedb6402032015-03-20 13:23:43 -0700164 v1 = conic.evalTangentAt(t);
165 check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY);
166}
167
reedb6402032015-03-20 13:23:43 -0700168static void test_conic(skiatest::Reporter* reporter) {
169 SkRandom rand;
170 for (int i = 0; i < 1000; ++i) {
171 SkPoint pts[3];
172 for (int j = 0; j < 3; ++j) {
173 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
174 }
175 for (int k = 0; k < 10; ++k) {
176 SkScalar w = rand.nextUScalar1() * 2;
177 SkConic conic(pts, w);
reedb6402032015-03-20 13:23:43 -0700178
179 const SkScalar dt = SK_Scalar1 / 128;
180 SkScalar t = dt;
181 for (int j = 1; j < 128; ++j) {
182 test_conic_eval_pos(reporter, conic, t);
183 test_conic_eval_tan(reporter, conic, t);
184 t += dt;
185 }
186 }
187 }
188}
189
caryclark45398df2015-08-25 13:19:06 -0700190static void test_quad_tangents(skiatest::Reporter* reporter) {
191 SkPoint pts[] = {
192 {10, 20}, {10, 20}, {20, 30},
193 {10, 20}, {15, 25}, {20, 30},
194 {10, 20}, {20, 30}, {20, 30},
195 };
196 int count = (int) SK_ARRAY_COUNT(pts) / 3;
197 for (int index = 0; index < count; ++index) {
198 SkConic conic(&pts[index * 3], 0.707f);
199 SkVector start = SkEvalQuadTangentAt(&pts[index * 3], 0);
200 SkVector mid = SkEvalQuadTangentAt(&pts[index * 3], .5f);
201 SkVector end = SkEvalQuadTangentAt(&pts[index * 3], 1);
202 REPORTER_ASSERT(reporter, start.fX && start.fY);
203 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
204 REPORTER_ASSERT(reporter, end.fX && end.fY);
205 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
206 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
207 }
208}
209
210static void test_conic_tangents(skiatest::Reporter* reporter) {
211 SkPoint pts[] = {
212 { 10, 20}, {10, 20}, {20, 30},
213 { 10, 20}, {15, 25}, {20, 30},
214 { 10, 20}, {20, 30}, {20, 30}
215 };
216 int count = (int) SK_ARRAY_COUNT(pts) / 3;
217 for (int index = 0; index < count; ++index) {
218 SkConic conic(&pts[index * 3], 0.707f);
219 SkVector start = conic.evalTangentAt(0);
220 SkVector mid = conic.evalTangentAt(.5f);
221 SkVector end = conic.evalTangentAt(1);
222 REPORTER_ASSERT(reporter, start.fX && start.fY);
223 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
224 REPORTER_ASSERT(reporter, end.fX && end.fY);
225 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
226 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
227 }
228}
229
reedb1b12f82016-07-13 10:56:53 -0700230static void test_this_conic_to_quad(skiatest::Reporter* r, const SkPoint pts[3], SkScalar w) {
231 SkAutoConicToQuads quadder;
232 const SkPoint* qpts = quadder.computeQuads(pts, w, 0.25);
233 const int qcount = quadder.countQuads();
234 const int pcount = qcount * 2 + 1;
235
Cary Clarkdf429f32017-11-08 11:44:31 -0500236 REPORTER_ASSERT(r, SkPointPriv::AreFinite(qpts, pcount));
reedb1b12f82016-07-13 10:56:53 -0700237}
238
239/**
240 * We need to ensure that when a conic is approximated by quads, that we always return finite
241 * values in the quads.
242 *
243 * Inspired by crbug_627414
244 */
245static void test_conic_to_quads(skiatest::Reporter* reporter) {
246 const SkPoint triples[] = {
247 { 0, 0 }, { 1, 0 }, { 1, 1 },
msarett16ef4652016-07-13 13:08:44 -0700248 { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f },
reedb1b12f82016-07-13 10:56:53 -0700249 { 0, 0 }, { 100000, 0 }, { 100000, 100000 },
250 { 0, 0 }, { 1e30f, 0 }, { 1e30f, 1e30f },
251 };
252 const int N = sizeof(triples) / sizeof(SkPoint);
253
254 for (int i = 0; i < N; i += 3) {
255 const SkPoint* pts = &triples[i];
256
reedb1b12f82016-07-13 10:56:53 -0700257 SkScalar w = 1e30f;
258 do {
259 w *= 2;
260 test_this_conic_to_quad(reporter, pts, w);
261 } while (SkScalarIsFinite(w));
262 test_this_conic_to_quad(reporter, pts, SK_ScalarNaN);
263 }
264}
265
caryclark45398df2015-08-25 13:19:06 -0700266static void test_cubic_tangents(skiatest::Reporter* reporter) {
267 SkPoint pts[] = {
268 { 10, 20}, {10, 20}, {20, 30}, {30, 40},
269 { 10, 20}, {15, 25}, {20, 30}, {30, 40},
270 { 10, 20}, {20, 30}, {30, 40}, {30, 40},
271 };
272 int count = (int) SK_ARRAY_COUNT(pts) / 4;
273 for (int index = 0; index < count; ++index) {
274 SkConic conic(&pts[index * 3], 0.707f);
275 SkVector start, mid, end;
halcanary96fcdcc2015-08-27 07:41:13 -0700276 SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr);
277 SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr);
278 SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr);
caryclark45398df2015-08-25 13:19:06 -0700279 REPORTER_ASSERT(reporter, start.fX && start.fY);
280 REPORTER_ASSERT(reporter, mid.fX && mid.fY);
281 REPORTER_ASSERT(reporter, end.fX && end.fY);
282 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
283 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
284 }
285}
286
Chris Dalton91982ee2017-07-14 14:04:52 -0600287static void check_cubic_type(skiatest::Reporter* reporter,
Chris Daltonfc31be42017-11-08 17:04:47 -0700288 const std::array<SkPoint, 4>& bezierPoints, SkCubicType expectedType,
289 bool undefined = false) {
290 // Classify the cubic even if the results will be undefined: check for crashes and asserts.
Chris Dalton91982ee2017-07-14 14:04:52 -0600291 SkCubicType actualType = SkClassifyCubic(bezierPoints.data());
Chris Daltonfc31be42017-11-08 17:04:47 -0700292 if (!undefined) {
293 REPORTER_ASSERT(reporter, actualType == expectedType);
294 }
295}
296
297static void check_cubic_around_rect(skiatest::Reporter* reporter,
298 float x1, float y1, float x2, float y2,
299 bool undefined = false) {
300 static constexpr SkCubicType expectations[24] = {
301 SkCubicType::kLoop,
302 SkCubicType::kCuspAtInfinity,
303 SkCubicType::kLocalCusp,
304 SkCubicType::kLocalCusp,
305 SkCubicType::kCuspAtInfinity,
306 SkCubicType::kLoop,
307 SkCubicType::kCuspAtInfinity,
308 SkCubicType::kLoop,
309 SkCubicType::kCuspAtInfinity,
310 SkCubicType::kLoop,
311 SkCubicType::kLocalCusp,
312 SkCubicType::kLocalCusp,
313 SkCubicType::kLocalCusp,
314 SkCubicType::kLocalCusp,
315 SkCubicType::kLoop,
316 SkCubicType::kCuspAtInfinity,
317 SkCubicType::kLoop,
318 SkCubicType::kCuspAtInfinity,
319 SkCubicType::kLoop,
320 SkCubicType::kCuspAtInfinity,
321 SkCubicType::kLocalCusp,
322 SkCubicType::kLocalCusp,
323 SkCubicType::kCuspAtInfinity,
324 SkCubicType::kLoop,
325 };
326 SkPoint points[] = {{x1, y1}, {x2, y1}, {x2, y2}, {x1, y2}};
327 std::array<SkPoint, 4> bezier;
328 for (int i=0; i < 4; ++i) {
329 bezier[0] = points[i];
330 for (int j=0; j < 3; ++j) {
331 int jidx = (j < i) ? j : j+1;
332 bezier[1] = points[jidx];
333 for (int k=0, kidx=0; k < 2; ++k, ++kidx) {
334 for (int n = 0; n < 2; ++n) {
335 kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx;
336 }
337 bezier[2] = points[kidx];
338 for (int l = 0; l < 4; ++l) {
339 if (l != i && l != jidx && l != kidx) {
340 bezier[3] = points[l];
341 break;
342 }
343 }
344 check_cubic_type(reporter, bezier, expectations[i*6 + j*2 + k], undefined);
345 }
346 }
347 }
348 for (int i=0; i < 4; ++i) {
349 bezier[0] = points[i];
350 for (int j=0; j < 3; ++j) {
351 int jidx = (j < i) ? j : j+1;
352 bezier[1] = points[jidx];
353 bezier[2] = points[jidx];
354 for (int k=0, kidx=0; k < 2; ++k, ++kidx) {
355 for (int n = 0; n < 2; ++n) {
356 kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx;
357 }
358 bezier[3] = points[kidx];
359 check_cubic_type(reporter, bezier, SkCubicType::kSerpentine, undefined);
360 }
361 }
362 }
Chris Dalton91982ee2017-07-14 14:04:52 -0600363}
364
Chris Daltonc4ec2912020-08-21 11:27:15 -0600365static std::array<SkPoint, 4> kSerpentines[] = {
366 {{{149.325f, 107.705f}, {149.325f, 103.783f}, {151.638f, 100.127f}, {156.263f, 96.736f}}},
367 {{{225.694f, 223.15f}, {209.831f, 224.837f}, {195.994f, 230.237f}, {184.181f, 239.35f}}},
368 {{{4.873f, 5.581f}, {5.083f, 5.2783f}, {5.182f, 4.8593f}, {5.177f, 4.3242f}}},
369 {{{285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}}}
370};
371
372static std::array<SkPoint, 4> kLoops[] = {
373 {{{635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}}},
374 {{{653.050f, 725.049f}, {663.000f, 176.000f}, {1189.000f, 508.000f}, {288.050f, 564.950f}}},
375 {{{631.050f, 478.049f}, {730.000f, 302.000f}, {870.000f, 350.000f}, {905.050f, 528.950f}}},
376 {{{631.050f, 478.0499f}, {221.000f, 230.000f}, {1265.000f, 451.000f}, {905.050f, 528.950f}}}
377};
378
379static std::array<SkPoint, 4> kLinearCubics[] = {
380 {{{0, 0}, {0, 1}, {0, 2}, {0, 3}}}, // 0-degree flat line.
381 {{{0, 0}, {1, 0}, {1, 0}, {0, 0}}}, // 180-degree flat line
382 {{{0, 1}, {0, 0}, {0, 2}, {0, 3}}}, // 180-degree flat line
Chris Daltonfd53e632020-09-17 19:21:44 -0600383 {{{0, 1}, {0, 0}, {0, 3}, {0, 2}}}, // 360-degree flat line
384 {{{0, 0}, {2, 0}, {1, 0}, {64, 0}}}, // 360-degree flat line
385 {{{1, 0}, {0, 0}, {3, 0}, {-64, 0}}} // 360-degree flat line
Chris Daltonc4ec2912020-08-21 11:27:15 -0600386};
387
Chris Dalton91982ee2017-07-14 14:04:52 -0600388static void test_classify_cubic(skiatest::Reporter* reporter) {
Chris Daltonc4ec2912020-08-21 11:27:15 -0600389 for (const auto& serp : kSerpentines) {
390 check_cubic_type(reporter, serp, SkCubicType::kSerpentine);
391 }
392 for (const auto& loop : kLoops) {
393 check_cubic_type(reporter, loop, SkCubicType::kLoop);
394 }
395 for (const auto& loop : kLinearCubics) {
396 check_cubic_type(reporter, loop, SkCubicType::kLineOrPoint);
397 }
Chris Daltonfc31be42017-11-08 17:04:47 -0700398 check_cubic_around_rect(reporter, 0, 0, 1, 1);
399 check_cubic_around_rect(reporter,
400 -std::numeric_limits<float>::max(),
401 -std::numeric_limits<float>::max(),
402 +std::numeric_limits<float>::max(),
403 +std::numeric_limits<float>::max());
404 check_cubic_around_rect(reporter, 1, 1,
405 +std::numeric_limits<float>::min(),
406 +std::numeric_limits<float>::max());
407 check_cubic_around_rect(reporter,
408 -std::numeric_limits<float>::min(),
409 -std::numeric_limits<float>::min(),
410 +std::numeric_limits<float>::min(),
411 +std::numeric_limits<float>::min());
412 check_cubic_around_rect(reporter, +1, -std::numeric_limits<float>::min(), -1, -1);
413 check_cubic_around_rect(reporter,
414 -std::numeric_limits<float>::infinity(),
415 -std::numeric_limits<float>::infinity(),
416 +std::numeric_limits<float>::infinity(),
417 +std::numeric_limits<float>::infinity(),
418 true);
419 check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::infinity(), true);
420 check_cubic_around_rect(reporter,
421 -std::numeric_limits<float>::quiet_NaN(),
422 -std::numeric_limits<float>::quiet_NaN(),
423 +std::numeric_limits<float>::quiet_NaN(),
424 +std::numeric_limits<float>::quiet_NaN(),
425 true);
426 check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::quiet_NaN(), true);
Chris Dalton91982ee2017-07-14 14:04:52 -0600427}
428
Chris Daltonc4ec2912020-08-21 11:27:15 -0600429static std::array<SkPoint, 4> kCusps[] = {
430 {{{0, 0}, {1, 1}, {1, 0}, {0, 1}}},
431 {{{0, 0}, {1, 1}, {0, 1}, {1, 0}}},
432 {{{0, 1}, {1, 0}, {0, 0}, {1, 1}}},
433 {{{0, 1}, {1, 0}, {1, 1}, {0, 0}}},
434};
435
Cary Clarkdb160012018-08-31 15:07:51 -0400436static void test_cubic_cusps(skiatest::Reporter* reporter) {
437 std::array<SkPoint, 4> noCusps[] = {
438 {{{0, 0}, {1, 1}, {2, 2}, {3, 3}}},
439 {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}},
440 {{{0, 0}, {1, 0}, {2, 1}, {2, 2}}},
441 {{{0, 0}, {1, 0}, {1, 1}, {2, 1}}},
442 };
443 for (auto noCusp : noCusps) {
444 REPORTER_ASSERT(reporter, SkFindCubicCusp(noCusp.data()) < 0);
445 }
Chris Daltonc4ec2912020-08-21 11:27:15 -0600446 for (auto cusp : kCusps) {
Mike Kleindc594f22020-08-20 01:46:05 +0000447 REPORTER_ASSERT(reporter, SkFindCubicCusp(cusp.data()) > 0);
Chris Dalton26766ad2020-08-19 08:31:37 -0600448 }
449}
450
Chris Daltonc4ec2912020-08-21 11:27:15 -0600451static SkMatrix kSkewMatrices[] = {
452 SkMatrix::MakeAll(1,0,0, 0,1,0, 0,0,1),
453 SkMatrix::MakeAll(1,-1,0, 1,1,0, 0,0,1),
454 SkMatrix::MakeAll(.889f,.553f,0, -.443f,.123f,0, 0,0,1),
455};
456
457static void test_chop_quad_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[3]) {
458 constexpr float kTolerance = 1e-3f;
459 for (const SkMatrix& m : kSkewMatrices) {
460 SkPoint mapped[3];
461 m.mapPoints(mapped, pts, 3);
462 float fullRotation = SkMeasureQuadRotation(pts);
463 SkPoint chopped[5];
464 SkChopQuadAtMidTangent(pts, chopped);
465 float leftRotation = SkMeasureQuadRotation(chopped);
466 float rightRotation = SkMeasureQuadRotation(chopped+2);
467 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, fullRotation/2, kTolerance));
468 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, fullRotation/2, kTolerance));
469 }
470}
471
472static void test_chop_cubic_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[4],
473 SkCubicType cubicType) {
474 constexpr float kTolerance = 1e-3f;
475 int n = SK_ARRAY_COUNT(kSkewMatrices);
476 if (cubicType == SkCubicType::kLocalCusp || cubicType == SkCubicType::kLineOrPoint) {
477 // FP precision isn't always enough to get the exact correct T value of the mid-tangent on
478 // cusps and lines. Only test the identity matrix and the matrix with all 1's.
479 n = 2;
480 }
481 for (int i = 0; i < n; ++i) {
482 SkPoint mapped[4];
483 kSkewMatrices[i].mapPoints(mapped, pts, 4);
484 float fullRotation = SkMeasureNonInflectCubicRotation(mapped);
485 SkPoint chopped[7];
486 SkChopCubicAtMidTangent(mapped, chopped);
487 float leftRotation = SkMeasureNonInflectCubicRotation(chopped);
488 float rightRotation = SkMeasureNonInflectCubicRotation(chopped+3);
Chris Daltonfd53e632020-09-17 19:21:44 -0600489 if (cubicType == SkCubicType::kLineOrPoint &&
490 (SkScalarNearlyEqual(fullRotation, 2*SK_ScalarPI, kTolerance) ||
491 SkScalarNearlyEqual(fullRotation, 0, kTolerance))) {
492 // 0- and 360-degree flat lines don't have single points of midtangent.
493 // (tangent == midtangent at every point on these curves except the cusp points.)
494 // Instead verify the promise from SkChopCubicAtMidTangent that neither side will rotate
495 // more than 180 degrees.
496 REPORTER_ASSERT(reporter, std::abs(leftRotation) - kTolerance <= SK_ScalarPI);
497 REPORTER_ASSERT(reporter, std::abs(rightRotation) - kTolerance <= SK_ScalarPI);
498 continue;
499 }
Chris Daltonc4ec2912020-08-21 11:27:15 -0600500 float expectedChoppedRotation = fullRotation/2;
501 if (cubicType == SkCubicType::kLocalCusp ||
502 (cubicType == SkCubicType::kLineOrPoint &&
503 SkScalarNearlyEqual(fullRotation, SK_ScalarPI, kTolerance))) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600504 // If we chop a cubic at a cusp, we lose 180 degrees of rotation.
Chris Daltonc4ec2912020-08-21 11:27:15 -0600505 expectedChoppedRotation = (fullRotation - SK_ScalarPI)/2;
506 }
507 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, expectedChoppedRotation,
508 kTolerance));
509 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, expectedChoppedRotation,
510 kTolerance));
511 }
512}
513
514static std::array<SkPoint, 3> kQuads[] = {
515 {{{10, 20}, {15, 35}, {30, 40}}},
516 {{{176.324f, 392.705f}, {719.325f, 205.782f}, {297.263f, 347.735f}}},
517 {{{652.050f, 602.049f}, {481.000f, 533.000f}, {288.050f, 564.950f}}},
518 {{{460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}}},
519 {{{359.050f, 578.049f}, {759.000f, 274.000f}, {288.050f, 564.950f}}}
520};
521
522SkPoint lerp(const SkPoint& a, const SkPoint& b, float t) {
523 return a * (1 - t) + b * t;
524}
525
526static void test_measure_rotation(skiatest::Reporter* reporter) {
527 static SkPoint kFlatCubic[4] = {{0, 0}, {0, 1}, {0, 2}, {0, 3}};
528 REPORTER_ASSERT(reporter, SkScalarNearlyZero(SkMeasureNonInflectCubicRotation(kFlatCubic)));
529
530 static SkPoint kFlatCubic180_1[4] = {{0, 0}, {1, 0}, {3, 0}, {2, 0}};
531 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_1),
532 SK_ScalarPI));
533
534 static SkPoint kFlatCubic180_2[4] = {{0, 1}, {0, 0}, {0, 2}, {0, 3}};
535 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_2),
536 SK_ScalarPI));
537
538 static SkPoint kFlatCubic360[4] = {{0, 1}, {0, 0}, {0, 3}, {0, 2}};
539 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic360),
540 2*SK_ScalarPI));
541
542 static SkPoint kSquare180[4] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
543 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kSquare180),
544 SK_ScalarPI));
545
546 auto checkQuadRotation = [=](const SkPoint pts[3], float expectedRotation) {
547 float r = SkMeasureQuadRotation(pts);
548 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(r, expectedRotation));
549
550 SkPoint cubic1[4] = {pts[0], pts[0], pts[1], pts[2]};
551 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic1),
552 expectedRotation));
553
554 SkPoint cubic2[4] = {pts[0], pts[1], pts[1], pts[2]};
555 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic2),
556 expectedRotation));
557
558 SkPoint cubic3[4] = {pts[0], pts[1], pts[2], pts[2]};
559 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic3),
560 expectedRotation));
561 };
562
563 static SkPoint kFlatQuad[4] = {{0, 0}, {0, 1}, {0, 2}};
564 checkQuadRotation(kFlatQuad, 0);
565
566 static SkPoint kFlatQuad180_1[4] = {{1, 0}, {0, 0}, {2, 0}};
567 checkQuadRotation(kFlatQuad180_1, SK_ScalarPI);
568
569 static SkPoint kFlatQuad180_2[4] = {{0, 0}, {0, 2}, {0, 1}};
570 checkQuadRotation(kFlatQuad180_2, SK_ScalarPI);
571
572 static SkPoint kTri120[3] = {{0, 0}, {.5f, std::sqrt(3.f)/2}, {1, 0}};
573 checkQuadRotation(kTri120, 2*SK_ScalarPI/3);
574}
575
576static void test_chop_at_midtangent(skiatest::Reporter* reporter) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600577 SkPoint chops[10];
Chris Daltonc4ec2912020-08-21 11:27:15 -0600578 for (const auto& serp : kSerpentines) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600579 REPORTER_ASSERT(reporter, SkClassifyCubic(serp.data()) == SkCubicType::kSerpentine);
580 int n = SkChopCubicAtInflections(serp.data(), chops);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600581 for (int i = 0; i < n; ++i) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600582 test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kSerpentine);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600583 }
584 }
585 for (const auto& loop : kLoops) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600586 REPORTER_ASSERT(reporter, SkClassifyCubic(loop.data()) == SkCubicType::kLoop);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600587 test_chop_cubic_at_midtangent(reporter, loop.data(), SkCubicType::kLoop);
588 }
589 for (const auto& line : kLinearCubics) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600590 REPORTER_ASSERT(reporter, SkClassifyCubic(line.data()) == SkCubicType::kLineOrPoint);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600591 test_chop_cubic_at_midtangent(reporter, line.data(), SkCubicType::kLineOrPoint);
592 }
593 for (const auto& cusp : kCusps) {
Chris Daltonfd53e632020-09-17 19:21:44 -0600594 REPORTER_ASSERT(reporter, SkClassifyCubic(cusp.data()) == SkCubicType::kLocalCusp);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600595 test_chop_cubic_at_midtangent(reporter, cusp.data(), SkCubicType::kLocalCusp);
596 }
597 for (const auto& quad : kQuads) {
598 test_chop_quad_at_midtangent(reporter, quad.data());
Chris Daltonfd53e632020-09-17 19:21:44 -0600599 SkPoint asCubic[4] = {
Chris Daltonc4ec2912020-08-21 11:27:15 -0600600 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 -0600601 test_chop_cubic_at_midtangent(reporter, asCubic, SkCubicType::kQuadratic);
602 }
603
604 static const SkPoint kExactQuad[4] = {{0,0}, {6,2}, {10,2}, {12,0}};
605 REPORTER_ASSERT(reporter, SkClassifyCubic(kExactQuad) == SkCubicType::kQuadratic);
606 test_chop_cubic_at_midtangent(reporter, kExactQuad, SkCubicType::kQuadratic);
607
608 static const SkPoint kExactCuspAtInf[4] = {{0,0}, {1,0}, {0,1}, {1,1}};
609 REPORTER_ASSERT(reporter, SkClassifyCubic(kExactCuspAtInf) == SkCubicType::kCuspAtInfinity);
610 int n = SkChopCubicAtInflections(kExactCuspAtInf, chops);
611 for (int i = 0; i < n; ++i) {
612 test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kCuspAtInfinity);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600613 }
614}
615
tfarina@chromium.orge4fafb12013-12-12 21:11:12 +0000616DEF_TEST(Geometry, reporter) {
Chris Dalton1208e0f2018-08-13 00:20:33 -0600617 SkPoint pts[5];
reed@android.comd8730ea2009-02-27 22:06:06 +0000618
619 pts[0].set(0, 0);
620 pts[1].set(100, 50);
621 pts[2].set(0, 100);
622
Chris Dalton1208e0f2018-08-13 00:20:33 -0600623 int count = SkChopQuadAtMaxCurvature(pts, pts); // Ensure src and dst can be the same pointer.
reed@android.comd8730ea2009-02-27 22:06:06 +0000624 REPORTER_ASSERT(reporter, count == 1 || count == 2);
reed@google.com6fc321a2011-07-27 13:54:36 +0000625
Brian Salomon1c42fcf2021-04-28 12:09:39 -0400626 // This previously crashed because the computed t of max curvature is NaN and SkChopQuadAt
627 // asserts that the passed t is in 0..1. Passes by not asserting.
628 pts[0].set(15.1213f, 7.77647f);
629 pts[1].set(6.2168e+19f, 1.51338e+20f);
630 pts[2].set(1.4579e+19f, 1.55558e+21f);
631 count = SkChopQuadAtMaxCurvature(pts, pts);
632
reed@google.com6fc321a2011-07-27 13:54:36 +0000633 pts[0].set(0, 0);
reeddaee7ea2015-03-26 20:22:33 -0700634 pts[1].set(3, 0);
635 pts[2].set(3, 3);
Chris Dalton1208e0f2018-08-13 00:20:33 -0600636 SkConvertQuadToCubic(pts, pts);
reed@google.com6fc321a2011-07-27 13:54:36 +0000637 const SkPoint cubic[] = {
reeddaee7ea2015-03-26 20:22:33 -0700638 { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 },
reed@google.com6fc321a2011-07-27 13:54:36 +0000639 };
640 for (int i = 0; i < 4; ++i) {
Chris Dalton1208e0f2018-08-13 00:20:33 -0600641 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], pts[i]));
reed@google.com6fc321a2011-07-27 13:54:36 +0000642 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000643
reed@google.com087d5aa2012-02-29 20:59:24 +0000644 testChopCubic(reporter);
reed65cb2cd2015-03-19 10:18:47 -0700645 test_evalquadat(reporter);
reedb6402032015-03-20 13:23:43 -0700646 test_conic(reporter);
caryclark45398df2015-08-25 13:19:06 -0700647 test_cubic_tangents(reporter);
648 test_quad_tangents(reporter);
649 test_conic_tangents(reporter);
reedb1b12f82016-07-13 10:56:53 -0700650 test_conic_to_quads(reporter);
Chris Dalton91982ee2017-07-14 14:04:52 -0600651 test_classify_cubic(reporter);
Cary Clarkdb160012018-08-31 15:07:51 -0400652 test_cubic_cusps(reporter);
Chris Daltonc4ec2912020-08-21 11:27:15 -0600653 test_measure_rotation(reporter);
654 test_chop_at_midtangent(reporter);
reed@android.comd8730ea2009-02-27 22:06:06 +0000655}