Flutter Engine
The Flutter Engine
FindCubicConvex180ChopsTest.cpp
Go to the documentation of this file.
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
11#include "src/core/SkGeometry.h"
13#include "tests/Test.h"
14
15#include <cmath>
16#include <cstdint>
17#include <cstring>
18
19namespace skgpu::tess {
20
21static bool is_linear(SkPoint p0, SkPoint p1, SkPoint p2) {
22 return SkScalarNearlyZero((p0 - p1).cross(p2 - p1));
23}
24
25static 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
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);
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.
67 SkMeasureNonInflectCubicRotation(chops + 3), totalRotation - SK_ScalarPI));
68 }
69 REPORTER_ASSERT(r, !areCusps);
70 } else {
71 REPORTER_ASSERT(r, areCusps);
72 }
73 }
74}
75
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)};
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));
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);
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
#define SkASSERT(cond)
Definition: SkAssert.h:116
static constexpr double kEpsilon
float SkMeasureNonInflectCubicRotation(const SkPoint pts[4])
Definition: SkGeometry.cpp:579
void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t)
Definition: SkGeometry.cpp:473
int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[2])
Definition: SkGeometry.cpp:741
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:101
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:107
#define SK_ScalarNearlyZero
Definition: SkScalar.h:99
#define SK_ScalarPI
Definition: SkScalar.h:21
#define REPORTER_ASSERT(r, cond,...)
Definition: Test.h:286
int64_t cross(Point d0, Point d1)
Definition: Myers.cpp:55
DEF_TEST(CullTestTest, reporter)
static bool is_linear(SkPoint p0, SkPoint p1, SkPoint p2)
int FindCubicConvex180Chops(const SkPoint pts[], float T[2], bool *areCusps)
static void check_cubic_convex_180(skiatest::Reporter *r, const SkPoint p[4])
SkScalar h
#define T
Definition: precompiler.cc:65
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
float fY
y-axis value
Definition: SkPoint_impl.h:165