• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "include/core/SkCubicMap.h"
9 
10 #include "include/private/base/SkFloatingPoint.h"
11 #include "include/private/base/SkTPin.h"
12 #include "src/base/SkVx.h"
13 #include "src/core/SkOpts.h"
14 
15 #include <algorithm>
16 
nearly_zero(SkScalar x)17 static inline bool nearly_zero(SkScalar x) {
18     SkASSERT(x >= 0);
19     return x <= 0.0000000001f;
20 }
21 
compute_t_from_x(float A,float B,float C,float x)22 static float compute_t_from_x(float A, float B, float C, float x) {
23     return SkOpts::cubic_solver(A, B, C, -x);
24 }
25 
computeYFromX(float x) const26 float SkCubicMap::computeYFromX(float x) const {
27     x = SkTPin(x, 0.0f, 1.0f);
28 
29     if (nearly_zero(x) || nearly_zero(1 - x)) {
30         return x;
31     }
32     if (fType == kLine_Type) {
33         return x;
34     }
35     float t;
36     if (fType == kCubeRoot_Type) {
37         t = sk_float_pow(x / fCoeff[0].fX, 1.0f / 3);
38     } else {
39         t = compute_t_from_x(fCoeff[0].fX, fCoeff[1].fX, fCoeff[2].fX, x);
40     }
41     float a = fCoeff[0].fY;
42     float b = fCoeff[1].fY;
43     float c = fCoeff[2].fY;
44     float y = ((a * t + b) * t + c) * t;
45 
46     return y;
47 }
48 
coeff_nearly_zero(float delta)49 static inline bool coeff_nearly_zero(float delta) {
50     return sk_float_abs(delta) <= 0.0000001f;
51 }
52 
SkCubicMap(SkPoint p1,SkPoint p2)53 SkCubicMap::SkCubicMap(SkPoint p1, SkPoint p2) {
54     // Clamp X values only (we allow Ys outside [0..1]).
55     p1.fX = std::min(std::max(p1.fX, 0.0f), 1.0f);
56     p2.fX = std::min(std::max(p2.fX, 0.0f), 1.0f);
57 
58     auto s1 = skvx::float2::Load(&p1) * 3;
59     auto s2 = skvx::float2::Load(&p2) * 3;
60 
61     (1 + s1 - s2).store(&fCoeff[0]);
62     (s2 - s1 - s1).store(&fCoeff[1]);
63     s1.store(&fCoeff[2]);
64 
65     fType = kSolver_Type;
66     if (SkScalarNearlyEqual(p1.fX, p1.fY) && SkScalarNearlyEqual(p2.fX, p2.fY)) {
67         fType = kLine_Type;
68     } else if (coeff_nearly_zero(fCoeff[1].fX) && coeff_nearly_zero(fCoeff[2].fX)) {
69         fType = kCubeRoot_Type;
70     }
71 }
72 
computeFromT(float t) const73 SkPoint SkCubicMap::computeFromT(float t) const {
74     auto a = skvx::float2::Load(&fCoeff[0]);
75     auto b = skvx::float2::Load(&fCoeff[1]);
76     auto c = skvx::float2::Load(&fCoeff[2]);
77 
78     SkPoint result;
79     (((a * t + b) * t + c) * t).store(&result);
80     return result;
81 }
82