Flutter Engine
The Flutter Engine
SkCubicMap.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 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
9
11#include "src/base/SkVx.h"
12
13#include <algorithm>
14#include <cmath>
15
16static float eval_poly(float t, float b) { return b; }
17
18template <typename... Rest>
19static float eval_poly(float t, float m, float b, Rest... rest) {
20 return eval_poly(t, std::fma(m, t, b), rest...);
21}
22
23static float cubic_solver(float A, float B, float C, float D) {
24#ifdef SK_DEBUG
25 auto valid = [](float t) { return t >= 0 && t <= 1; };
26#endif
27
28 auto guess_nice_cubic_root = [](float a, float b, float c, float d) { return -d; };
29 float t = guess_nice_cubic_root(A, B, C, D);
30
31 int iters = 0;
32 const int MAX_ITERS = 8;
33 for (; iters < MAX_ITERS; ++iters) {
34 SkASSERT(valid(t));
35 float f = eval_poly(t, A, B, C, D); // f = At^3 + Bt^2 + Ct + D
36 if (std::fabs(f) <= 0.00005f) {
37 break;
38 }
39 float fp = eval_poly(t, 3*A, 2*B, C); // f' = 3At^2 + 2Bt + C
40 float fpp = eval_poly(t, 3*A + 3*A, 2*B); // f'' = 6At + 2B
41
42 float numer = 2 * fp * f;
43 float denom = std::fma(2 * fp, fp, -(f * fpp));
44
45 t -= numer / denom;
46 }
47
48 SkASSERT(valid(t));
49 return t;
50}
51
52static inline bool nearly_zero(SkScalar x) {
53 SkASSERT(x >= 0);
54 return x <= 0.0000000001f;
55}
56
57static float compute_t_from_x(float A, float B, float C, float x) {
58 return cubic_solver(A, B, C, -x);
59}
60
61float SkCubicMap::computeYFromX(float x) const {
62 x = SkTPin(x, 0.0f, 1.0f);
63
64 if (nearly_zero(x) || nearly_zero(1 - x)) {
65 return x;
66 }
67 if (fType == kLine_Type) {
68 return x;
69 }
70 float t;
71 if (fType == kCubeRoot_Type) {
72 t = std::pow(x / fCoeff[0].fX, 1.0f / 3);
73 } else {
74 t = compute_t_from_x(fCoeff[0].fX, fCoeff[1].fX, fCoeff[2].fX, x);
75 }
76 float a = fCoeff[0].fY;
77 float b = fCoeff[1].fY;
78 float c = fCoeff[2].fY;
79 float y = ((a * t + b) * t + c) * t;
80
81 return y;
82}
83
84static inline bool coeff_nearly_zero(float delta) {
85 return std::fabs(delta) <= 0.0000001f;
86}
87
89 // Clamp X values only (we allow Ys outside [0..1]).
90 p1.fX = std::min(std::max(p1.fX, 0.0f), 1.0f);
91 p2.fX = std::min(std::max(p2.fX, 0.0f), 1.0f);
92
93 auto s1 = skvx::float2::Load(&p1) * 3;
94 auto s2 = skvx::float2::Load(&p2) * 3;
95
96 (1 + s1 - s2).store(&fCoeff[0]);
97 (s2 - s1 - s1).store(&fCoeff[1]);
98 s1.store(&fCoeff[2]);
99
100 fType = kSolver_Type;
101 if (SkScalarNearlyEqual(p1.fX, p1.fY) && SkScalarNearlyEqual(p2.fX, p2.fY)) {
102 fType = kLine_Type;
103 } else if (coeff_nearly_zero(fCoeff[1].fX) && coeff_nearly_zero(fCoeff[2].fX)) {
104 fType = kCubeRoot_Type;
105 }
106}
107
109 auto a = skvx::float2::Load(&fCoeff[0]);
110 auto b = skvx::float2::Load(&fCoeff[1]);
111 auto c = skvx::float2::Load(&fCoeff[2]);
112
114 (((a * t + b) * t + c) * t).store(&result);
115 return result;
116}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static float cubic_solver(float A, float B, float C, float D)
Definition: SkCubicMap.cpp:23
static float eval_poly(float t, float b)
Definition: SkCubicMap.cpp:16
static bool nearly_zero(SkScalar x)
Definition: SkCubicMap.cpp:52
static bool coeff_nearly_zero(float delta)
Definition: SkCubicMap.cpp:84
static float compute_t_from_x(float A, float B, float C, float x)
Definition: SkCubicMap.cpp:57
static skvx::float4 fma(const skvx::float4 &f, float m, const skvx::float4 &a)
Definition: SkGeometry.cpp:597
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:107
static constexpr const T & SkTPin(const T &x, const T &lo, const T &hi)
Definition: SkTPin.h:19
SI void store(P *ptr, const T &val)
float computeYFromX(float x) const
Definition: SkCubicMap.cpp:61
SkPoint computeFromT(float t) const
Definition: SkCubicMap.cpp:108
SkCubicMap(SkPoint p1, SkPoint p2)
Definition: SkCubicMap.cpp:88
VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE auto & d
Definition: main.cc:19
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
GAsyncResult * result
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
double y
double x
const uint32_t fp
float fX
x-axis value
Definition: SkPoint_impl.h:164
float fY
y-axis value
Definition: SkPoint_impl.h:165
static SKVX_ALWAYS_INLINE Vec Load(const void *ptr)
Definition: SkVx.h:109