1 /*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/utils/SkRandom.h"
9 #include "src/core/SkGeometry.h"
10 #include "src/gpu/tessellate/Tessellation.h"
11 #include "tests/Test.h"
12
13 namespace skgpu {
14
is_linear(SkPoint p0,SkPoint p1,SkPoint p2)15 static bool is_linear(SkPoint p0, SkPoint p1, SkPoint p2) {
16 return SkScalarNearlyZero((p0 - p1).cross(p2 - p1));
17 }
18
is_linear(const SkPoint p[4])19 static bool is_linear(const SkPoint p[4]) {
20 return is_linear(p[0],p[1],p[2]) && is_linear(p[0],p[2],p[3]) && is_linear(p[1],p[2],p[3]);
21 }
22
check_cubic_convex_180(skiatest::Reporter * r,const SkPoint p[4])23 static void check_cubic_convex_180(skiatest::Reporter* r, const SkPoint p[4]) {
24 bool areCusps = false;
25 float inflectT[2], convex180T[2];
26 if (int inflectN = SkFindCubicInflections(p, inflectT)) {
27 // The curve has inflections. FindCubicConvex180Chops should return the inflection
28 // points.
29 int convex180N = FindCubicConvex180Chops(p, convex180T, &areCusps);
30 REPORTER_ASSERT(r, inflectN == convex180N);
31 if (!areCusps) {
32 REPORTER_ASSERT(r, inflectN == 1 ||
33 fabsf(inflectT[0] - inflectT[1]) >= SK_ScalarNearlyZero);
34 }
35 for (int i = 0; i < convex180N; ++i) {
36 REPORTER_ASSERT(r, SkScalarNearlyEqual(inflectT[i], convex180T[i]));
37 }
38 } else {
39 float totalRotation = SkMeasureNonInflectCubicRotation(p);
40 int convex180N = FindCubicConvex180Chops(p, convex180T, &areCusps);
41 SkPoint chops[10];
42 SkChopCubicAt(p, chops, convex180T, convex180N);
43 float radsSum = 0;
44 for (int i = 0; i <= convex180N; ++i) {
45 float rads = SkMeasureNonInflectCubicRotation(chops + i*3);
46 SkASSERT(rads < SK_ScalarPI + SK_ScalarNearlyZero);
47 radsSum += rads;
48 }
49 if (totalRotation < SK_ScalarPI - SK_ScalarNearlyZero) {
50 // The curve should never chop if rotation is <180 degrees.
51 REPORTER_ASSERT(r, convex180N == 0);
52 } else if (!is_linear(p)) {
53 REPORTER_ASSERT(r, SkScalarNearlyEqual(radsSum, totalRotation));
54 if (totalRotation > SK_ScalarPI + SK_ScalarNearlyZero) {
55 REPORTER_ASSERT(r, convex180N == 1);
56 // This works because cusps take the "inflection" path above, so we don't get
57 // non-lilnear curves that lose rotation when chopped.
58 REPORTER_ASSERT(r, SkScalarNearlyEqual(
59 SkMeasureNonInflectCubicRotation(chops), SK_ScalarPI));
60 REPORTER_ASSERT(r, SkScalarNearlyEqual(
61 SkMeasureNonInflectCubicRotation(chops + 3), totalRotation - SK_ScalarPI));
62 }
63 REPORTER_ASSERT(r, !areCusps);
64 } else {
65 REPORTER_ASSERT(r, areCusps);
66 }
67 }
68 }
69
DEF_TEST(FindCubicConvex180Chops,r)70 DEF_TEST(FindCubicConvex180Chops, r) {
71 // Test all combinations of corners from the square [0,0,1,1]. This covers every cubic type as
72 // well as a wide variety of special cases for cusps, lines, loops, and inflections.
73 for (int i = 0; i < (1 << 8); ++i) {
74 SkPoint p[4] = {SkPoint::Make((i>>0)&1, (i>>1)&1),
75 SkPoint::Make((i>>2)&1, (i>>3)&1),
76 SkPoint::Make((i>>4)&1, (i>>5)&1),
77 SkPoint::Make((i>>6)&1, (i>>7)&1)};
78 check_cubic_convex_180(r, p);
79 }
80
81 {
82 // This cubic has a convex-180 chop at T=1-"epsilon"
83 static const uint32_t hexPts[] = {0x3ee0ac74, 0x3f1e061a, 0x3e0fc408, 0x3f457230,
84 0x3f42ac7c, 0x3f70d76c, 0x3f4e6520, 0x3f6acafa};
85 SkPoint p[4];
86 memcpy(p, hexPts, sizeof(p));
87 check_cubic_convex_180(r, p);
88 }
89
90 // Now test an exact quadratic.
91 SkPoint quad[4] = {{0,0}, {2,2}, {4,2}, {6,0}};
92 float T[2];
93 bool areCusps;
94 REPORTER_ASSERT(r, FindCubicConvex180Chops(quad, T, &areCusps) == 0);
95
96 // Now test that cusps and near-cusps get flagged as cusps.
97 SkPoint cusp[4] = {{0,0}, {1,1}, {1,0}, {0,1}};
98 REPORTER_ASSERT(r, FindCubicConvex180Chops(cusp, T, &areCusps) == 1);
99 REPORTER_ASSERT(r, areCusps == true);
100
101 // Find the height of the right side of "cusp" at which the distance between its inflection
102 // points is kEpsilon (in parametric space).
103 constexpr static double kEpsilon = 1.0 / (1 << 11);
104 constexpr static double kEpsilonSquared = kEpsilon * kEpsilon;
105 double h = (1 - kEpsilonSquared) / (3 * kEpsilonSquared + 1);
106 double dy = (1 - h) / 2;
107 cusp[1].fY = (float)(1 - dy);
108 cusp[2].fY = (float)(0 + dy);
109 REPORTER_ASSERT(r, SkFindCubicInflections(cusp, T) == 2);
110 REPORTER_ASSERT(r, SkScalarNearlyEqual(T[1] - T[0], (float)kEpsilon, (float)kEpsilonSquared));
111
112 // Ensure two inflection points barely more than kEpsilon apart do not get flagged as cusps.
113 cusp[1].fY = (float)(1 - 1.1 * dy);
114 cusp[2].fY = (float)(0 + 1.1 * dy);
115 REPORTER_ASSERT(r, FindCubicConvex180Chops(cusp, T, &areCusps) == 2);
116 REPORTER_ASSERT(r, areCusps == false);
117
118 // Ensure two inflection points barely less than kEpsilon apart do get flagged as cusps.
119 cusp[1].fY = (float)(1 - .9 * dy);
120 cusp[2].fY = (float)(0 + .9 * dy);
121 REPORTER_ASSERT(r, FindCubicConvex180Chops(cusp, T, &areCusps) == 1);
122 REPORTER_ASSERT(r, areCusps == true);
123 }
124
125 } // namespace skgpu
126