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/core/SkMatrix.h"
9 #include "include/core/SkPoint.h"
10 #include "include/core/SkScalar.h"
11 #include "include/core/SkSpan.h"
12 #include "include/core/SkTypes.h"
13 #include "include/private/base/SkDebug.h"
14 #include "src/base/SkRandom.h"
15 #include "src/core/SkGeometry.h"
16 #include "src/core/SkPointPriv.h"
17 #include "tests/Test.h"
18 
19 #include <array>
20 #include <cmath>
21 #include <cstdlib>
22 #include <limits>
23 #include <string>
24 
nearly_equal(const SkPoint & a,const SkPoint & b)25 static bool nearly_equal(const SkPoint& a, const SkPoint& b) {
26     return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY);
27 }
28 
testChopCubic(skiatest::Reporter * reporter)29 static void testChopCubic(skiatest::Reporter* reporter) {
30     /*
31         Inspired by this test, which used to assert that the tValues had dups
32 
33         <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2205,4980" />
34      */
35     const SkPoint src[] = {
36         { SkIntToScalar(2190), SkIntToScalar(5130) },
37         { SkIntToScalar(2190), SkIntToScalar(5070) },
38         { SkIntToScalar(2220), SkIntToScalar(5010) },
39         { SkIntToScalar(2205), SkIntToScalar(4980) },
40     };
41     SkPoint dst[13];
42     SkScalar tValues[3];
43     // make sure we don't assert internally
44     int count = SkChopCubicAtMaxCurvature(src, dst, tValues);
45     if ((false)) { // avoid bit rot, suppress warning
46         REPORTER_ASSERT(reporter, count);
47     }
48     // Make sure src and dst can be the same pointer.
49     {
50         SkPoint pts[7];
51         for (int i = 0; i < 7; ++i) {
52             pts[i].set(i, i);
53         }
54         SkChopCubicAt(pts, pts, .5f);
55         for (int i = 0; i < 7; ++i) {
56             REPORTER_ASSERT(reporter, pts[i].fX == pts[i].fY);
57             REPORTER_ASSERT(reporter, pts[i].fX == i * .5f);
58         }
59     }
60 
61     static const float chopTs[] = {
62         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,
63         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,
64     };
65     float ones[] = {1,1,1,1,1};
66 
67     // Ensure an odd number of T values so we exercise the single chop code at the end of
68     // SkChopCubicAt form multiple T.
69     static_assert(std::size(chopTs) % 2 == 1);
70     static_assert(std::size(ones) % 2 == 1);
71 
72     SkRandom rand;
73     for (int iterIdx = 0; iterIdx < 5; ++iterIdx) {
74         SkPoint pts[4] = {{rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()},
75                           {rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()}};
76 
77         SkPoint allChops[4 + std::size(chopTs)*3];
78         SkChopCubicAt(pts, allChops, chopTs, std::size(chopTs));
79         int i = 3;
80         for (float chopT : chopTs) {
81             // Ensure we chop at approximately the correct points when we chop an entire list.
82             SkPoint expectedPt;
83             SkEvalCubicAt(pts, chopT, &expectedPt, nullptr, nullptr);
84             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].x(), expectedPt.x()));
85             REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].y(), expectedPt.y()));
86             if (chopT == 0) {
87                 REPORTER_ASSERT(reporter, allChops[i] == pts[0]);
88             }
89             if (chopT == 1) {
90                 REPORTER_ASSERT(reporter, allChops[i] == pts[3]);
91             }
92             i += 3;
93 
94             // Ensure the middle is exactly degenerate when we chop at two equal points.
95             SkPoint localChops[10];
96             SkChopCubicAt(pts, localChops, chopT, chopT);
97             REPORTER_ASSERT(reporter, localChops[3] == localChops[4]);
98             REPORTER_ASSERT(reporter, localChops[3] == localChops[5]);
99             REPORTER_ASSERT(reporter, localChops[3] == localChops[6]);
100             if (chopT == 0) {
101                 // Also ensure the first curve is exactly p0 when we chop at T=0.
102                 REPORTER_ASSERT(reporter, localChops[0] == pts[0]);
103                 REPORTER_ASSERT(reporter, localChops[1] == pts[0]);
104                 REPORTER_ASSERT(reporter, localChops[2] == pts[0]);
105                 REPORTER_ASSERT(reporter, localChops[3] == pts[0]);
106             }
107             if (chopT == 1) {
108                 // Also ensure the last curve is exactly p3 when we chop at T=1.
109                 REPORTER_ASSERT(reporter, localChops[6] == pts[3]);
110                 REPORTER_ASSERT(reporter, localChops[7] == pts[3]);
111                 REPORTER_ASSERT(reporter, localChops[8] == pts[3]);
112                 REPORTER_ASSERT(reporter, localChops[9] == pts[3]);
113             }
114         }
115 
116         // Now test what happens when SkChopCubicAt does 0/0 and gets NaN values.
117         SkPoint oneChops[4 + std::size(ones)*3];
118         SkChopCubicAt(pts, oneChops, ones, std::size(ones));
119         REPORTER_ASSERT(reporter, oneChops[0] == pts[0]);
120         REPORTER_ASSERT(reporter, oneChops[1] == pts[1]);
121         REPORTER_ASSERT(reporter, oneChops[2] == pts[2]);
122         for (size_t index = 3; index < std::size(oneChops); ++index) {
123             REPORTER_ASSERT(reporter, oneChops[index] == pts[3]);
124         }
125     }
126 }
127 
check_pairs(skiatest::Reporter * reporter,int index,SkScalar t,const char name[],SkScalar x0,SkScalar y0,SkScalar x1,SkScalar y1)128 static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[],
129                         SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) {
130     bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1);
131     if (!eq) {
132         SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n",
133                  name, index, t, x0, y0, x1, y1);
134         REPORTER_ASSERT(reporter, eq);
135     }
136 }
137 
test_evalquadat(skiatest::Reporter * reporter)138 static void test_evalquadat(skiatest::Reporter* reporter) {
139     SkRandom rand;
140     for (int i = 0; i < 1000; ++i) {
141         SkPoint pts[3];
142         for (int j = 0; j < 3; ++j) {
143             pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
144         }
145         const SkScalar dt = SK_Scalar1 / 128;
146         SkScalar t = dt;
147         for (int j = 1; j < 128; ++j) {
148             SkPoint r0;
149             SkEvalQuadAt(pts, t, &r0);
150             SkPoint r1 = SkEvalQuadAt(pts, t);
151             check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY);
152 
153             SkVector v0;
154             SkEvalQuadAt(pts, t, nullptr, &v0);
155             SkVector v1 = SkEvalQuadTangentAt(pts, t);
156             check_pairs(reporter, i, t, "quad-tan", v0.fX, v0.fY, v1.fX, v1.fY);
157 
158             t += dt;
159         }
160     }
161 }
162 
test_conic_eval_pos(skiatest::Reporter * reporter,const SkConic & conic,SkScalar t)163 static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
164     SkPoint p0, p1;
165     conic.evalAt(t, &p0, nullptr);
166     p1 = conic.evalAt(t);
167     check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY);
168 }
169 
test_conic_eval_tan(skiatest::Reporter * reporter,const SkConic & conic,SkScalar t)170 static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) {
171     SkVector v0, v1;
172     conic.evalAt(t, nullptr, &v0);
173     v1 = conic.evalTangentAt(t);
174     check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY);
175 }
176 
test_conic(skiatest::Reporter * reporter)177 static void test_conic(skiatest::Reporter* reporter) {
178     SkRandom rand;
179     for (int i = 0; i < 1000; ++i) {
180         SkPoint pts[3];
181         for (int j = 0; j < 3; ++j) {
182             pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100);
183         }
184         for (int k = 0; k < 10; ++k) {
185             SkScalar w = rand.nextUScalar1() * 2;
186             SkConic conic(pts, w);
187 
188             const SkScalar dt = SK_Scalar1 / 128;
189             SkScalar t = dt;
190             for (int j = 1; j < 128; ++j) {
191                 test_conic_eval_pos(reporter, conic, t);
192                 test_conic_eval_tan(reporter, conic, t);
193                 t += dt;
194             }
195         }
196     }
197 }
198 
test_quad_tangents(skiatest::Reporter * reporter)199 static void test_quad_tangents(skiatest::Reporter* reporter) {
200     SkPoint pts[] = {
201         {10, 20}, {10, 20}, {20, 30},
202         {10, 20}, {15, 25}, {20, 30},
203         {10, 20}, {20, 30}, {20, 30},
204     };
205     int count = (int) std::size(pts) / 3;
206     for (int index = 0; index < count; ++index) {
207         SkConic conic(&pts[index * 3], 0.707f);
208         SkVector start = SkEvalQuadTangentAt(&pts[index * 3], 0);
209         SkVector mid = SkEvalQuadTangentAt(&pts[index * 3], .5f);
210         SkVector end = SkEvalQuadTangentAt(&pts[index * 3], 1);
211         REPORTER_ASSERT(reporter, start.fX && start.fY);
212         REPORTER_ASSERT(reporter, mid.fX && mid.fY);
213         REPORTER_ASSERT(reporter, end.fX && end.fY);
214         REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
215         REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
216     }
217 }
218 
test_conic_tangents(skiatest::Reporter * reporter)219 static void test_conic_tangents(skiatest::Reporter* reporter) {
220     SkPoint pts[] = {
221         { 10, 20}, {10, 20}, {20, 30},
222         { 10, 20}, {15, 25}, {20, 30},
223         { 10, 20}, {20, 30}, {20, 30}
224     };
225     int count = (int) std::size(pts) / 3;
226     for (int index = 0; index < count; ++index) {
227         SkConic conic(&pts[index * 3], 0.707f);
228         SkVector start = conic.evalTangentAt(0);
229         SkVector mid = conic.evalTangentAt(.5f);
230         SkVector end = conic.evalTangentAt(1);
231         REPORTER_ASSERT(reporter, start.fX && start.fY);
232         REPORTER_ASSERT(reporter, mid.fX && mid.fY);
233         REPORTER_ASSERT(reporter, end.fX && end.fY);
234         REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
235         REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
236     }
237 }
238 
test_this_conic_to_quad(skiatest::Reporter * r,const SkPoint pts[3],SkScalar w)239 static void test_this_conic_to_quad(skiatest::Reporter* r, const SkPoint pts[3], SkScalar w) {
240     SkAutoConicToQuads quadder;
241     const SkPoint* qpts = quadder.computeQuads(pts, w, 0.25);
242     const int qcount = quadder.countQuads();
243     const int pcount = qcount * 2 + 1;
244 
245     REPORTER_ASSERT(r, SkPointPriv::AreFinite(qpts, pcount));
246 }
247 
248 /**
249  *  We need to ensure that when a conic is approximated by quads, that we always return finite
250  *  values in the quads.
251  *
252  *  Inspired by crbug_627414
253  */
test_conic_to_quads(skiatest::Reporter * reporter)254 static void test_conic_to_quads(skiatest::Reporter* reporter) {
255     const SkPoint triples[] = {
256         { 0, 0 }, { 1, 0 }, { 1, 1 },
257         { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f },
258         { 0, 0 }, { 100000, 0 }, { 100000, 100000 },
259         { 0, 0 }, { 1e30f, 0 }, { 1e30f, 1e30f },
260     };
261     const int N = sizeof(triples) / sizeof(SkPoint);
262 
263     for (int i = 0; i < N; i += 3) {
264         const SkPoint* pts = &triples[i];
265 
266         SkScalar w = 1e30f;
267         do {
268             w *= 2;
269             test_this_conic_to_quad(reporter, pts, w);
270         } while (SkScalarIsFinite(w));
271         test_this_conic_to_quad(reporter, pts, SK_ScalarNaN);
272     }
273 }
274 
test_cubic_tangents(skiatest::Reporter * reporter)275 static void test_cubic_tangents(skiatest::Reporter* reporter) {
276     SkPoint pts[] = {
277         { 10, 20}, {10, 20}, {20, 30}, {30, 40},
278         { 10, 20}, {15, 25}, {20, 30}, {30, 40},
279         { 10, 20}, {20, 30}, {30, 40}, {30, 40},
280     };
281     int count = (int) std::size(pts) / 4;
282     for (int index = 0; index < count; ++index) {
283         SkConic conic(&pts[index * 3], 0.707f);
284         SkVector start, mid, end;
285         SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr);
286         SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr);
287         SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr);
288         REPORTER_ASSERT(reporter, start.fX && start.fY);
289         REPORTER_ASSERT(reporter, mid.fX && mid.fY);
290         REPORTER_ASSERT(reporter, end.fX && end.fY);
291         REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid)));
292         REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end)));
293     }
294 }
295 
check_cubic_type(skiatest::Reporter * reporter,const std::array<SkPoint,4> & bezierPoints,SkCubicType expectedType,bool undefined=false)296 static void check_cubic_type(skiatest::Reporter* reporter,
297                              const std::array<SkPoint, 4>& bezierPoints, SkCubicType expectedType,
298                              bool undefined = false) {
299     // Classify the cubic even if the results will be undefined: check for crashes and asserts.
300     SkCubicType actualType = SkClassifyCubic(bezierPoints.data());
301     if (!undefined) {
302         REPORTER_ASSERT(reporter, actualType == expectedType);
303     }
304 }
305 
check_cubic_around_rect(skiatest::Reporter * reporter,float x1,float y1,float x2,float y2,bool undefined=false)306 static void check_cubic_around_rect(skiatest::Reporter* reporter,
307                                     float x1, float y1, float x2, float y2,
308                                     bool undefined = false) {
309     static constexpr SkCubicType expectations[24] = {
310         SkCubicType::kLoop,
311         SkCubicType::kCuspAtInfinity,
312         SkCubicType::kLocalCusp,
313         SkCubicType::kLocalCusp,
314         SkCubicType::kCuspAtInfinity,
315         SkCubicType::kLoop,
316         SkCubicType::kCuspAtInfinity,
317         SkCubicType::kLoop,
318         SkCubicType::kCuspAtInfinity,
319         SkCubicType::kLoop,
320         SkCubicType::kLocalCusp,
321         SkCubicType::kLocalCusp,
322         SkCubicType::kLocalCusp,
323         SkCubicType::kLocalCusp,
324         SkCubicType::kLoop,
325         SkCubicType::kCuspAtInfinity,
326         SkCubicType::kLoop,
327         SkCubicType::kCuspAtInfinity,
328         SkCubicType::kLoop,
329         SkCubicType::kCuspAtInfinity,
330         SkCubicType::kLocalCusp,
331         SkCubicType::kLocalCusp,
332         SkCubicType::kCuspAtInfinity,
333         SkCubicType::kLoop,
334     };
335     SkPoint points[] = {{x1, y1}, {x2, y1}, {x2, y2}, {x1, y2}};
336     std::array<SkPoint, 4> bezier;
337     for (int i=0; i < 4; ++i) {
338         bezier[0] = points[i];
339         for (int j=0; j < 3; ++j) {
340             int jidx = (j < i) ? j : j+1;
341             bezier[1] = points[jidx];
342             for (int k=0, kidx=0; k < 2; ++k, ++kidx) {
343                 for (int n = 0; n < 2; ++n) {
344                     kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx;
345                 }
346                 bezier[2] = points[kidx];
347                 for (int l = 0; l < 4; ++l) {
348                     if (l != i && l != jidx && l != kidx) {
349                         bezier[3] = points[l];
350                         break;
351                     }
352                 }
353                 check_cubic_type(reporter, bezier, expectations[i*6 + j*2 + k], undefined);
354             }
355         }
356     }
357     for (int i=0; i < 4; ++i) {
358         bezier[0] = points[i];
359         for (int j=0; j < 3; ++j) {
360             int jidx = (j < i) ? j : j+1;
361             bezier[1] = points[jidx];
362             bezier[2] = points[jidx];
363             for (int k=0, kidx=0; k < 2; ++k, ++kidx) {
364                 for (int n = 0; n < 2; ++n) {
365                     kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx;
366                 }
367                 bezier[3] = points[kidx];
368                 check_cubic_type(reporter, bezier, SkCubicType::kSerpentine, undefined);
369             }
370         }
371     }
372 }
373 
374 static std::array<SkPoint, 4> kSerpentines[] = {
375     {{{149.325f, 107.705f}, {149.325f, 103.783f}, {151.638f, 100.127f}, {156.263f, 96.736f}}},
376     {{{225.694f, 223.15f}, {209.831f, 224.837f}, {195.994f, 230.237f}, {184.181f, 239.35f}}},
377     {{{4.873f, 5.581f}, {5.083f, 5.2783f}, {5.182f, 4.8593f}, {5.177f, 4.3242f}}},
378     {{{285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}}}
379 };
380 
381 static std::array<SkPoint, 4> kLoops[] = {
382     {{{635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}}},
383     {{{653.050f, 725.049f}, {663.000f, 176.000f}, {1189.000f, 508.000f}, {288.050f, 564.950f}}},
384     {{{631.050f, 478.049f}, {730.000f, 302.000f}, {870.000f, 350.000f}, {905.050f, 528.950f}}},
385     {{{631.050f, 478.0499f}, {221.000f, 230.000f}, {1265.000f, 451.000f}, {905.050f, 528.950f}}}
386 };
387 
388 static std::array<SkPoint, 4> kLinearCubics[] = {
389     {{{0, 0}, {0, 1}, {0, 2}, {0, 3}}},  // 0-degree flat line.
390     {{{0, 0}, {1, 0}, {1, 0}, {0, 0}}},  // 180-degree flat line
391     {{{0, 1}, {0, 0}, {0, 2}, {0, 3}}},  // 180-degree flat line
392     {{{0, 1}, {0, 0}, {0, 3}, {0, 2}}},  // 360-degree flat line
393     {{{0, 0}, {2, 0}, {1, 0}, {64, 0}}},  // 360-degree flat line
394     {{{1, 0}, {0, 0}, {3, 0}, {-64, 0}}}  // 360-degree flat line
395 };
396 
test_classify_cubic(skiatest::Reporter * reporter)397 static void test_classify_cubic(skiatest::Reporter* reporter) {
398     for (const auto& serp : kSerpentines) {
399         check_cubic_type(reporter, serp, SkCubicType::kSerpentine);
400     }
401     for (const auto& loop : kLoops) {
402         check_cubic_type(reporter, loop, SkCubicType::kLoop);
403     }
404     for (const auto& loop : kLinearCubics) {
405         check_cubic_type(reporter, loop, SkCubicType::kLineOrPoint);
406     }
407     check_cubic_around_rect(reporter, 0, 0, 1, 1);
408     check_cubic_around_rect(reporter,
409                             -std::numeric_limits<float>::max(),
410                             -std::numeric_limits<float>::max(),
411                             +std::numeric_limits<float>::max(),
412                             +std::numeric_limits<float>::max());
413     check_cubic_around_rect(reporter, 1, 1,
414                             +std::numeric_limits<float>::min(),
415                             +std::numeric_limits<float>::max());
416     check_cubic_around_rect(reporter,
417                             -std::numeric_limits<float>::min(),
418                             -std::numeric_limits<float>::min(),
419                             +std::numeric_limits<float>::min(),
420                             +std::numeric_limits<float>::min());
421     check_cubic_around_rect(reporter, +1, -std::numeric_limits<float>::min(), -1, -1);
422     check_cubic_around_rect(reporter,
423                             -std::numeric_limits<float>::infinity(),
424                             -std::numeric_limits<float>::infinity(),
425                             +std::numeric_limits<float>::infinity(),
426                             +std::numeric_limits<float>::infinity(),
427                             true);
428     check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::infinity(), true);
429     check_cubic_around_rect(reporter,
430                             -std::numeric_limits<float>::quiet_NaN(),
431                             -std::numeric_limits<float>::quiet_NaN(),
432                             +std::numeric_limits<float>::quiet_NaN(),
433                             +std::numeric_limits<float>::quiet_NaN(),
434                             true);
435     check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::quiet_NaN(), true);
436 }
437 
438 static std::array<SkPoint, 4> kCusps[] = {
439     {{{0, 0}, {1, 1}, {1, 0}, {0, 1}}},
440     {{{0, 0}, {1, 1}, {0, 1}, {1, 0}}},
441     {{{0, 1}, {1, 0}, {0, 0}, {1, 1}}},
442     {{{0, 1}, {1, 0}, {1, 1}, {0, 0}}},
443 };
444 
test_cubic_cusps(skiatest::Reporter * reporter)445 static void test_cubic_cusps(skiatest::Reporter* reporter) {
446     std::array<SkPoint, 4> noCusps[] = {
447         {{{0, 0}, {1, 1}, {2, 2}, {3, 3}}},
448         {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}},
449         {{{0, 0}, {1, 0}, {2, 1}, {2, 2}}},
450         {{{0, 0}, {1, 0}, {1, 1}, {2, 1}}},
451     };
452     for (auto noCusp : noCusps) {
453         REPORTER_ASSERT(reporter, SkFindCubicCusp(noCusp.data()) < 0);
454     }
455     for (auto cusp : kCusps) {
456         REPORTER_ASSERT(reporter, SkFindCubicCusp(cusp.data()) > 0);
457     }
458 }
459 
460 static SkMatrix kSkewMatrices[] = {
461     SkMatrix::MakeAll(1,0,0, 0,1,0, 0,0,1),
462     SkMatrix::MakeAll(1,-1,0, 1,1,0, 0,0,1),
463     SkMatrix::MakeAll(.889f,.553f,0, -.443f,.123f,0, 0,0,1),
464 };
465 
test_chop_quad_at_midtangent(skiatest::Reporter * reporter,const SkPoint pts[3])466 static void test_chop_quad_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[3]) {
467     constexpr float kTolerance = 1e-3f;
468     for (const SkMatrix& m : kSkewMatrices) {
469         SkPoint mapped[3];
470         m.mapPoints(mapped, pts, 3);
471         float fullRotation = SkMeasureQuadRotation(pts);
472         SkPoint chopped[5];
473         SkChopQuadAtMidTangent(pts, chopped);
474         float leftRotation = SkMeasureQuadRotation(chopped);
475         float rightRotation = SkMeasureQuadRotation(chopped+2);
476         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, fullRotation/2, kTolerance));
477         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, fullRotation/2, kTolerance));
478     }
479 }
480 
test_chop_cubic_at_midtangent(skiatest::Reporter * reporter,const SkPoint pts[4],SkCubicType cubicType)481 static void test_chop_cubic_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[4],
482                                           SkCubicType cubicType) {
483     constexpr float kTolerance = 1e-3f;
484     int n = std::size(kSkewMatrices);
485     if (cubicType == SkCubicType::kLocalCusp || cubicType == SkCubicType::kLineOrPoint) {
486         // FP precision isn't always enough to get the exact correct T value of the mid-tangent on
487         // cusps and lines. Only test the identity matrix and the matrix with all 1's.
488         n = 2;
489     }
490     for (int i = 0; i < n; ++i) {
491         SkPoint mapped[4];
492         kSkewMatrices[i].mapPoints(mapped, pts, 4);
493         float fullRotation = SkMeasureNonInflectCubicRotation(mapped);
494         SkPoint chopped[7];
495         SkChopCubicAtMidTangent(mapped, chopped);
496         float leftRotation = SkMeasureNonInflectCubicRotation(chopped);
497         float rightRotation = SkMeasureNonInflectCubicRotation(chopped+3);
498         if (cubicType == SkCubicType::kLineOrPoint &&
499             (SkScalarNearlyEqual(fullRotation, 2*SK_ScalarPI, kTolerance) ||
500              SkScalarNearlyEqual(fullRotation, 0, kTolerance))) {
501             // 0- and 360-degree flat lines don't have single points of midtangent.
502             // (tangent == midtangent at every point on these curves except the cusp points.)
503             // Instead verify the promise from SkChopCubicAtMidTangent that neither side will rotate
504             // more than 180 degrees.
505             REPORTER_ASSERT(reporter, std::abs(leftRotation) - kTolerance <= SK_ScalarPI);
506             REPORTER_ASSERT(reporter, std::abs(rightRotation) - kTolerance <= SK_ScalarPI);
507             continue;
508         }
509         float expectedChoppedRotation = fullRotation/2;
510         if (cubicType == SkCubicType::kLocalCusp ||
511             (cubicType == SkCubicType::kLineOrPoint &&
512              SkScalarNearlyEqual(fullRotation, SK_ScalarPI, kTolerance))) {
513             // If we chop a cubic at a cusp, we lose 180 degrees of rotation.
514             expectedChoppedRotation = (fullRotation - SK_ScalarPI)/2;
515         }
516         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, expectedChoppedRotation,
517                                                       kTolerance));
518         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, expectedChoppedRotation,
519                                                       kTolerance));
520     }
521 }
522 
523 static std::array<SkPoint, 3> kQuads[] = {
524     {{{10, 20}, {15, 35}, {30, 40}}},
525     {{{176.324f, 392.705f}, {719.325f, 205.782f}, {297.263f, 347.735f}}},
526     {{{652.050f, 602.049f}, {481.000f, 533.000f}, {288.050f, 564.950f}}},
527     {{{460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}}},
528     {{{359.050f, 578.049f}, {759.000f, 274.000f}, {288.050f, 564.950f}}}
529 };
530 
lerp(const SkPoint & a,const SkPoint & b,float t)531 SkPoint lerp(const SkPoint& a, const SkPoint& b, float t) {
532     return a * (1 - t) + b * t;
533 }
534 
test_measure_rotation(skiatest::Reporter * reporter)535 static void test_measure_rotation(skiatest::Reporter* reporter) {
536     static SkPoint kFlatCubic[4] = {{0, 0}, {0, 1}, {0, 2}, {0, 3}};
537     REPORTER_ASSERT(reporter, SkScalarNearlyZero(SkMeasureNonInflectCubicRotation(kFlatCubic)));
538 
539     static SkPoint kFlatCubic180_1[4] = {{0, 0}, {1, 0}, {3, 0}, {2, 0}};
540     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_1),
541                                                   SK_ScalarPI));
542 
543     static SkPoint kFlatCubic180_2[4] = {{0, 1}, {0, 0}, {0, 2}, {0, 3}};
544     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_2),
545                                                   SK_ScalarPI));
546 
547     static SkPoint kFlatCubic360[4] = {{0, 1}, {0, 0}, {0, 3}, {0, 2}};
548     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic360),
549                                                   2*SK_ScalarPI));
550 
551     static SkPoint kSquare180[4] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}};
552     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kSquare180),
553                                                   SK_ScalarPI));
554 
555     auto checkQuadRotation = [=](const SkPoint pts[3], float expectedRotation) {
556         float r = SkMeasureQuadRotation(pts);
557         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(r, expectedRotation));
558 
559         SkPoint cubic1[4] = {pts[0], pts[0], pts[1], pts[2]};
560         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic1),
561                                                       expectedRotation));
562 
563         SkPoint cubic2[4] = {pts[0], pts[1], pts[1], pts[2]};
564         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic2),
565                                                       expectedRotation));
566 
567         SkPoint cubic3[4] = {pts[0], pts[1], pts[2], pts[2]};
568         REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic3),
569                                                       expectedRotation));
570     };
571 
572     static SkPoint kFlatQuad[4] = {{0, 0}, {0, 1}, {0, 2}};
573     checkQuadRotation(kFlatQuad, 0);
574 
575     static SkPoint kFlatQuad180_1[4] = {{1, 0}, {0, 0}, {2, 0}};
576     checkQuadRotation(kFlatQuad180_1, SK_ScalarPI);
577 
578     static SkPoint kFlatQuad180_2[4] = {{0, 0}, {0, 2}, {0, 1}};
579     checkQuadRotation(kFlatQuad180_2, SK_ScalarPI);
580 
581     static SkPoint kTri120[3] = {{0, 0}, {.5f, std::sqrt(3.f)/2}, {1, 0}};
582     checkQuadRotation(kTri120, 2*SK_ScalarPI/3);
583 }
584 
test_chop_at_midtangent(skiatest::Reporter * reporter)585 static void test_chop_at_midtangent(skiatest::Reporter* reporter) {
586     SkPoint chops[10];
587     for (const auto& serp : kSerpentines) {
588         REPORTER_ASSERT(reporter, SkClassifyCubic(serp.data()) == SkCubicType::kSerpentine);
589         int n = SkChopCubicAtInflections(serp.data(), chops);
590         for (int i = 0; i < n; ++i) {
591             test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kSerpentine);
592         }
593     }
594     for (const auto& loop : kLoops) {
595         REPORTER_ASSERT(reporter, SkClassifyCubic(loop.data()) == SkCubicType::kLoop);
596         test_chop_cubic_at_midtangent(reporter, loop.data(), SkCubicType::kLoop);
597     }
598     for (const auto& line : kLinearCubics) {
599         REPORTER_ASSERT(reporter, SkClassifyCubic(line.data()) == SkCubicType::kLineOrPoint);
600         test_chop_cubic_at_midtangent(reporter, line.data(), SkCubicType::kLineOrPoint);
601     }
602     for (const auto& cusp : kCusps) {
603         REPORTER_ASSERT(reporter, SkClassifyCubic(cusp.data()) == SkCubicType::kLocalCusp);
604         test_chop_cubic_at_midtangent(reporter, cusp.data(), SkCubicType::kLocalCusp);
605     }
606     for (const auto& quad : kQuads) {
607         test_chop_quad_at_midtangent(reporter, quad.data());
608         SkPoint asCubic[4] = {
609                 quad[0], lerp(quad[0], quad[1], 2/3.f), lerp(quad[1], quad[2], 1/3.f), quad[2]};
610         test_chop_cubic_at_midtangent(reporter, asCubic, SkCubicType::kQuadratic);
611     }
612 
613     static const SkPoint kExactQuad[4] = {{0,0}, {6,2}, {10,2}, {12,0}};
614     REPORTER_ASSERT(reporter, SkClassifyCubic(kExactQuad) == SkCubicType::kQuadratic);
615     test_chop_cubic_at_midtangent(reporter, kExactQuad, SkCubicType::kQuadratic);
616 
617     static const SkPoint kExactCuspAtInf[4] = {{0,0}, {1,0}, {0,1}, {1,1}};
618     REPORTER_ASSERT(reporter, SkClassifyCubic(kExactCuspAtInf) == SkCubicType::kCuspAtInfinity);
619     int n = SkChopCubicAtInflections(kExactCuspAtInf, chops);
620     for (int i = 0; i < n; ++i) {
621         test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kCuspAtInfinity);
622     }
623 }
624 
DEF_TEST(Geometry,reporter)625 DEF_TEST(Geometry, reporter) {
626     SkPoint pts[5];
627 
628     pts[0].set(0, 0);
629     pts[1].set(100, 50);
630     pts[2].set(0, 100);
631 
632     int count = SkChopQuadAtMaxCurvature(pts, pts);  // Ensure src and dst can be the same pointer.
633     REPORTER_ASSERT(reporter, count == 1 || count == 2);
634 
635     // This previously crashed because the computed t of max curvature is NaN and SkChopQuadAt
636     // asserts that the passed t is in 0..1. Passes by not asserting.
637     pts[0].set(15.1213f, 7.77647f);
638     pts[1].set(6.2168e+19f, 1.51338e+20f);
639     pts[2].set(1.4579e+19f, 1.55558e+21f);
640     count = SkChopQuadAtMaxCurvature(pts, pts);
641 
642     pts[0].set(0, 0);
643     pts[1].set(3, 0);
644     pts[2].set(3, 3);
645     SkConvertQuadToCubic(pts, pts);
646     const SkPoint cubic[] = {
647         { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 },
648     };
649     for (int i = 0; i < 4; ++i) {
650         REPORTER_ASSERT(reporter, nearly_equal(cubic[i], pts[i]));
651     }
652 
653     testChopCubic(reporter);
654     test_evalquadat(reporter);
655     test_conic(reporter);
656     test_cubic_tangents(reporter);
657     test_quad_tangents(reporter);
658     test_conic_tangents(reporter);
659     test_conic_to_quads(reporter);
660     test_classify_cubic(reporter);
661     test_cubic_cusps(reporter);
662     test_measure_rotation(reporter);
663     test_chop_at_midtangent(reporter);
664 }
665 
testChopMonoCubicAtY(skiatest::Reporter * reporter,std::string name,SkSpan<const SkPoint> curveInputs,SkScalar yToChopAt,SkSpan<const SkPoint> expectedOutputs)666 static void testChopMonoCubicAtY(skiatest::Reporter* reporter, std::string name,
667                                  SkSpan<const SkPoint> curveInputs, SkScalar yToChopAt,
668                                  SkSpan<const SkPoint> expectedOutputs) {
669     skiatest::ReporterContext subtest(reporter, name);
670     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(expectedOutputs[3].y(), yToChopAt),
671                     "Invalid test case. 4th point's Y should be %f", yToChopAt);
672 
673     SkPoint outputs[7];
674     // Make sure it actually chopped
675     REPORTER_ASSERT(reporter, SkChopMonoCubicAtY(curveInputs.begin(), yToChopAt, outputs));
676 
677     for (int i = 0; i < 7; ++i) {
678         REPORTER_ASSERT(reporter, nearly_equal(expectedOutputs[i], outputs[i]),
679                         "(%f, %f) != (%f, %f) at index %d",
680                         expectedOutputs[i].x(), expectedOutputs[i].y(),
681                         outputs[i].x(), outputs[i].y(), i);
682     }
683 }
684 
DEF_TEST(GeometryChopMonoCubicAtY_Successful,reporter)685 DEF_TEST(GeometryChopMonoCubicAtY_Successful, reporter) {
686     // These cubics are all arbitrary, picked using Desmos for something that looked "nice".
687 
688     testChopMonoCubicAtY(reporter, "straight, positive slope @ 2.5",
689         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
690         2.5f,
691         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, {  1.065055f,  1.065055f },
692          {  2.500000f,  2.500000f },
693          {  5.461981f,  5.461981f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
694     );
695     testChopMonoCubicAtY(reporter, "straight, positive slope @ 5.0",
696         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
697         5.0f,
698         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, {  2.500000f,  2.500000f },
699          {  5.000000f,  5.000000f },
700          {  7.500000f,  7.500000f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
701     );
702     testChopMonoCubicAtY(reporter, "straight, positive slope @ 9.0",
703         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
704         9.0f,
705         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, {  6.467375f,  6.467375f },
706          {  9.000000f,  9.000000f },
707          {  9.616623f,  9.616623f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
708     );
709     testChopMonoCubicAtY(reporter, "straight, positive slope @ 10.0",
710         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
711         10.0f,
712         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, { 10.000000f, 10.000000f },
713          { 10.000000f, 10.000000f },
714          { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
715     );
716 
717     testChopMonoCubicAtY(reporter, "curve, positive slope @ 2.0",
718         {{ 1, 1 }, { 5, 2 }, { 7, 4 }, { 8, 7 }},
719         2.0f,
720         {{  1.000000f,  1.000000f }, {  2.055050f,  1.263763f }, {  2.970959f,  1.597096f },
721          {  3.766077f,  2.000000f },
722          {  5.985480f,  3.124621f }, {  7.263762f,  4.791288f }, {  8.000000f,  7.000000f }}
723     );
724     testChopMonoCubicAtY(reporter, "curve, positive slope @ 5.0",
725         {{ 1, 1 }, { 5, 2 }, { 7, 4 }, { 8, 7 }},
726         5.0f,
727         {{  1.000000f,  1.000000f }, {  4.033223f,  1.758306f }, {  5.916391f,  3.091639f },
728          {  7.085550f,  5.000000f },
729          {  7.458195f,  5.608251f }, {  7.758306f,  6.274917f }, {  8.000000f,  7.000000f }}
730     );
731 
732     testChopMonoCubicAtY(reporter, "curve, negative slope @ 5.0",
733         {{ 2, 7 }, { 3, 2 }, { 6, 3 }, { 11, 2 }},
734         5.0f,
735         {{  2.000000f,  7.000000f }, {  2.162856f,  6.185719f }, {  2.378757f,  5.530570f },
736          {  2.647702f,  5.000000f },
737          {  4.030182f,  2.272668f }, {  6.814281f,  2.837144f }, { 11.000000f,  2.000000f }}
738     );
739     testChopMonoCubicAtY(reporter, "curve, negative slope @ 3.0",
740         {{ 2, 7 }, { 3, 2 }, { 6, 3 }, { 11, 2 }},
741         3.0f,
742         {{  2.000000f,  7.000000f }, {  2.500000f,  4.500000f }, {  3.500000f,  3.500000f },
743          {  5.000000f,  3.000000f },
744          {  6.500000f,  2.500000f }, {  8.500000f,  2.500000f }, { 11.000000f,  2.000000f }}
745     );
746     testChopMonoCubicAtY(reporter, "curve, negative slope @ 2.5",
747         {{ 2, 7 }, { 3, 2 }, { 6, 3 }, { 11, 2 }},
748         2.5f,
749         {{  2.000000f,  7.000000f }, {  2.750000f,  3.250000f }, {  4.625000f,  2.875000f },
750          {  7.625000f,  2.500000f },
751          {  8.625000f,  2.375000f }, {  9.750000f,  2.250000f }, { 11.000000f,  2.000000f }}
752     );
753 
754     // This is the same curve as above, just the 4 points given in the opposite order.
755     // We would expect the math to result in the same chop points, with the outputs
756     // in the opposite order too.
757     testChopMonoCubicAtY(reporter, "inverted curve, negative slope @ 5.0",
758         {{ 11, 2 }, { 6, 3 }, { 3, 2 }, { 2, 7 }},
759         5.0f,
760         {{ 11.000000f,  2.000000f }, {  6.814281f,  2.837144f }, {  4.030182f,  2.272668f },
761          {  2.647702f,  5.000000f },
762          {  2.378757f,  5.530570f }, {  2.162856f,  6.185719f }, {  2.000000f,  7.000000f }}
763     );
764     testChopMonoCubicAtY(reporter, "inverted curve, negative slope @ 3.0",
765         {{ 11, 2 }, { 6, 3 }, { 3, 2 }, { 2, 7 }},
766         3.0f,
767         {{ 11.000000f,  2.000000f }, {  8.500000f,  2.500000f }, {  6.500000f,  2.500000f },
768          {  5.000000f,  3.000000f },
769          {  3.500000f,  3.500000f }, {  2.500000f,  4.500000f }, {  2.000000f,  7.000000f }}
770     );
771     testChopMonoCubicAtY(reporter, "inverted curve, negative slope @ 2.5",
772         {{ 11, 2 }, { 6, 3 }, { 3, 2 }, { 2, 7 }},
773         2.5f,
774         {{ 11.000000f,  2.000000f }, {  9.750000f,  2.250000f }, {  8.625000f,  2.375000f },
775          {  7.625000f,  2.500000f },
776          {  4.625000f,  2.875000f }, {  2.750000f,  3.250000f }, {  2.000000f,  7.000000f }}
777     );
778 
779     testChopMonoCubicAtY(reporter, "big curve, negative slope @ 90",
780         {{ -2, 100 }, { 0, 0 }, { 0, 0 }, { 100, -2 }},
781         90.f,
782         {{ -2.000000f,100.000000f }, { -1.930979f, 96.548965f }, { -1.864341f, 93.217033f },
783          { -1.795892f, 90.000000f },
784          {  0.119096f, -0.002382f }, {  3.451032f, -0.069021f }, {100.000000f, -2.000000f }}
785     );
786     testChopMonoCubicAtY(reporter, "big curve, negative slope @ 10",
787         {{ -2, 100 }, { 0, 0 }, { 0, 0 }, { 100, -2 }},
788         10.f,
789         {{ -2.000000f,100.000000f }, { -0.937505f, 46.875271f }, { -0.439458f, 21.972910f },
790          { 14.787060f, 10.000000f },
791          { 28.222368f, -0.564447f }, { 53.124729f, -1.062495f }, {100.000000f, -2.000000f }}
792     );
793     testChopMonoCubicAtY(reporter, "big curve, negative slope @ 0",
794         {{ -2, 100 }, { 0, 0 }, { 0, 0 }, { 100, -2 }},
795         0.f,
796         {{ -2.000000f,100.000000f }, { -0.426983f, 21.349131f }, { -0.091157f,  4.557854f },
797          { 48.633648f, 0.000000f },
798          { 61.859592f, -1.237192f }, { 78.650871f, -1.573017f }, {100.000000f, -2.000000f }}
799     );
800 
801     testChopMonoCubicAtY(reporter, "ossfuzz:55680 curve barely crosses Y axis",
802         {{-250.121582f, -1180.09509f}, {10.007843f, -1180.09509f},
803          {20.015685f, -786.041259f}, {40.0313721f, 2.0664072f}},
804         0.f,
805         {{-250.121582f, -1180.095093f}, {9.780392f, -1180.095093f}, {19.997992f, -786.730042f},
806          {39.978889f, 0.000000f},
807          {39.996376f, 0.688501f}, {40.013870f, 1.377304f}, {40.031372f, 2.066407f}}
808     );
809 }
810 
DEF_TEST(GeometryChopMonoCubicAtY_OutOfRangeReturnFalse,reporter)811 DEF_TEST(GeometryChopMonoCubicAtY_OutOfRangeReturnFalse, reporter) {
812     SkPoint inputs[] = {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }};
813     SkPoint outputs[7];
814 
815     // Too low
816     REPORTER_ASSERT(reporter, !SkChopMonoCubicAtY(inputs, -10, outputs));
817     // Too high
818     REPORTER_ASSERT(reporter, !SkChopMonoCubicAtY(inputs, 20, outputs));
819 }
820 
testChopMonoCubicAtX(skiatest::Reporter * reporter,std::string name,SkSpan<const SkPoint> curveInputs,SkScalar xToChopAt,SkSpan<const SkPoint> expectedOutputs)821 static void testChopMonoCubicAtX(skiatest::Reporter* reporter, std::string name,
822                                  SkSpan<const SkPoint> curveInputs, SkScalar xToChopAt,
823                                  SkSpan<const SkPoint> expectedOutputs) {
824     skiatest::ReporterContext subtest(reporter, name);
825     REPORTER_ASSERT(reporter, curveInputs.size() == 4,
826                     "Invalid test case. Input curve should have 4 points");
827     REPORTER_ASSERT(reporter, expectedOutputs.size() == 7,
828                     "Invalid test case. Outputs should have 7 points");
829     REPORTER_ASSERT(reporter, SkScalarNearlyEqual(expectedOutputs[3].x(), xToChopAt),
830                     "Invalid test case. 4th point's X should be %f", xToChopAt);
831 
832     SkPoint outputs[7];
833     // Make sure it actually chopped
834     REPORTER_ASSERT(reporter, SkChopMonoCubicAtX(curveInputs.begin(), xToChopAt, outputs));
835 
836     for (int i = 0; i < 7; ++i) {
837         REPORTER_ASSERT(reporter, nearly_equal(expectedOutputs[i], outputs[i]),
838                         "(%f, %f) != (%f, %f) at index %d",
839                         expectedOutputs[i].x(), expectedOutputs[i].y(),
840                         outputs[i].x(), outputs[i].y(), i);
841     }
842 }
843 
DEF_TEST(GeometryChopMonoCubicAtX_Successful,reporter)844 DEF_TEST(GeometryChopMonoCubicAtX_Successful, reporter) {
845     // These cubics are all arbitrary, picked using Desmos for something that looked "nice".
846 
847     testChopMonoCubicAtX(reporter, "straight, positive slope @ 2.5",
848         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
849         2.5f,
850         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, {  1.065055f,  1.065055f },
851          {  2.500000f,  2.500000f },
852          {  5.461981f,  5.461981f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
853     );
854     testChopMonoCubicAtX(reporter, "straight, positive slope @ 5.0",
855         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
856         5.0f,
857         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, {  2.500000f,  2.500000f },
858          {  5.000000f,  5.000000f },
859          {  7.500000f,  7.500000f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
860     );
861     testChopMonoCubicAtX(reporter, "straight, positive slope @ 9.0",
862         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
863         9.0f,
864         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, {  6.467375f,  6.467375f },
865          {  9.000000f,  9.000000f },
866          {  9.616623f,  9.616623f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
867     );
868     testChopMonoCubicAtX(reporter, "straight, positive slope @ 10.0",
869         {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }},
870         10.0f,
871         {{  0.000000f,  0.000000f }, {  0.000000f,  0.000000f }, { 10.000000f, 10.000000f },
872          { 10.000000f, 10.000000f },
873          { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }, { 10.000000f, 10.000000f }}
874     );
875 
876     testChopMonoCubicAtX(reporter, "curve, positive slope @ 2.0",
877         {{ 1, 1 }, { 5, 2 }, { 7, 4 }, { 8, 7 }},
878         2.0f,
879         {{  1.000000f,  1.000000f }, {  1.348275f,  1.087069f }, {  1.681389f,  1.181719f },
880          {  2.000000f,  1.283949f },
881          {  5.340694f,  2.355856f }, {  7.087069f,  4.261207f }, {  8.000000f,  7.000000f }}
882     );
883     testChopMonoCubicAtX(reporter, "curve, positive slope @ 5.0",
884         {{ 1, 1 }, { 5, 2 }, { 7, 4 }, { 8, 7 }},
885         5.0f,
886         {{  1.000000f,  1.000000f }, {  2.650396f,  1.412599f }, {  3.960316f,  1.995436f },
887          {  5.000000f,  2.748511f },
888          {  6.480158f,  3.820634f }, {  7.412599f,  5.237797f }, {  8.000000f,  7.000000f }}
889     );
890 
891     testChopMonoCubicAtX(reporter, "curve, negative slope @ 5.0",
892         {{ 2, 7 }, { 3, 2 }, { 6, 3 }, { 11, 2 }},
893         5.0f,
894         {{  2.000000f,  7.000000f }, {  2.500000f,  4.500000f }, {  3.500000f,  3.500000f },
895          {  5.000000f,  3.000000f },
896          {  6.500000f,  2.500000f }, {  8.500000f,  2.500000f }, { 11.000000f,  2.000000f }}
897     );
898     testChopMonoCubicAtX(reporter, "curve, negative slope @ 3.0",
899         {{ 2, 7 }, { 3, 2 }, { 6, 3 }, { 11, 2 }},
900         3.0f,
901         {{  2.000000f,  7.000000f }, {  2.228714f,  5.856432f }, {  2.562047f,  5.026724f },
902          {  3.000000f,  4.415163f },
903          {  4.476901f,  2.352807f }, {  7.143568f,  2.771286f }, { 11.000000f,  2.000000f }}
904     );
905     testChopMonoCubicAtX(reporter, "curve, negative slope @ 2.5",
906         {{ 2, 7 }, { 3, 2 }, { 6, 3 }, { 11, 2 }},
907         2.5f,
908         {{  2.000000f,  7.000000f }, {  2.131881f,  6.340593f }, {  2.298548f,  5.785543f },
909          {  2.500000f,  5.316498f },
910          {  3.826073f,  2.228977f }, {  6.659407f,  2.868119f }, { 11.000000f,  2.000000f }}
911     );
912 
913     // This is the same curve as above, just the 4 points given in the opposite order.
914     // We would expect the math to result in the same chop points, with the outputs
915     // in the opposite order too.
916     testChopMonoCubicAtX(reporter, "inverted curve, negative slope @ 5.0",
917         {{ 11, 2 }, { 6, 3 }, { 3, 2 }, { 2, 7 }},
918         5.0f,
919         {{ 11.000000f,  2.000000f }, {  8.500000f,  2.500000f }, {  6.500000f,  2.500000f },
920          {  5.000000f,  3.000000f },
921          {  3.500000f,  3.500000f }, {  2.500000f,  4.500000f }, {  2.000000f,  7.000000f }}
922     );
923     testChopMonoCubicAtX(reporter, "inverted curve, negative slope @ 3.0",
924         {{ 11, 2 }, { 6, 3 }, { 3, 2 }, { 2, 7 }},
925         3.0f,
926         {{ 11.000000f,  2.000000f }, {  7.143568f,  2.771286f }, {  4.476901f,  2.352807f },
927          {  3.000000f,  4.415163f },
928          {  2.562047f,  5.026724f }, {  2.228714f,  5.856432f }, {  2.000000f,  7.000000f }}
929     );
930     testChopMonoCubicAtX(reporter, "inverted curve, negative slope @ 2.5",
931         {{ 11, 2 }, { 6, 3 }, { 3, 2 }, { 2, 7 }},
932         2.5f,
933         {{ 11.000000f,  2.000000f }, {  6.659407f,  2.868119f }, {  3.826073f,  2.228977f },
934          {  2.500000f,  5.316498f },
935          {  2.298548f,  5.785543f }, {  2.131881f,  6.340593f }, {  2.000000f,  7.000000f }}
936     );
937 
938     testChopMonoCubicAtX(reporter, "big curve, negative slope @ 90",
939         {{ -2, 100 }, { 0, 0 }, { 0, 0 }, { 100, -2 }},
940         90.f,
941         {{ -2.000000f,100.000000f }, { -0.069021f,  3.451032f }, { -0.002382f,  0.119096f },
942          { 90.000000f, -1.795892f },
943          { 93.217033f, -1.864341f }, { 96.548965f, -1.930979f }, {100.000000f, -2.000000f }}
944     );
945     testChopMonoCubicAtX(reporter, "big curve, negative slope @ 10",
946         {{ -2, 100 }, { 0, 0 }, { 0, 0 }, { 100, -2 }},
947         10.f,
948         {{ -2.000000f,100.000000f }, { -1.062495f, 53.124729f }, { -0.564447f, 28.222368f },
949          { 10.000000f, 14.787060f },
950          { 21.972910f, -0.439458f }, { 46.875271f, -0.937505f }, {100.000000f, -2.000000f }}
951     );
952     testChopMonoCubicAtX(reporter, "big curve, negative slope @ 0",
953         {{ -2, 100 }, { 0, 0 }, { 0, 0 }, { 100, -2 }},
954         0.f,
955         {{ -2.000000f,100.000000f }, { -1.573017f, 78.650871f }, { -1.237192f, 61.859592f },
956          {  0.000000f, 48.633648f },
957          {  4.557854f, -0.091157f }, { 21.349131f, -0.426983f }, {100.000000f, -2.000000f }}
958     );
959 }
960 
DEF_TEST(GeometryChopMonoCubicAtX_OutOfRangeReturnFalse,reporter)961 DEF_TEST(GeometryChopMonoCubicAtX_OutOfRangeReturnFalse, reporter) {
962     SkPoint inputs[] = {{ 0, 0 }, { 0, 0 }, { 10, 10 }, { 10, 10 }};
963     SkPoint outputs[7];
964 
965     // Too low
966     REPORTER_ASSERT(reporter, !SkChopMonoCubicAtX(inputs, -10, outputs));
967     // Too high
968     REPORTER_ASSERT(reporter, !SkChopMonoCubicAtX(inputs, 20, outputs));
969 }
970