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