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