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 #include "include/private/SkNx.h"
10 #include "src/core/SkOpts.h"
11
12 //#define CUBICMAP_TRACK_MAX_ERROR
13
14 #ifdef CUBICMAP_TRACK_MAX_ERROR
15 #include "src/pathops/SkPathOpsCubic.h"
16 #endif
17
nearly_zero(SkScalar x)18 static inline bool nearly_zero(SkScalar x) {
19 SkASSERT(x >= 0);
20 return x <= 0.0000000001f;
21 }
22
23 #ifdef CUBICMAP_TRACK_MAX_ERROR
24 static int max_iters;
25 #endif
26
27 #ifdef CUBICMAP_TRACK_MAX_ERROR
compute_slow(float A,float B,float C,float x)28 static float compute_slow(float A, float B, float C, float x) {
29 double roots[3];
30 SkDEBUGCODE(int count =) SkDCubic::RootsValidT(A, B, C, -x, roots);
31 SkASSERT(count == 1);
32 return (float)roots[0];
33 }
34
35 static float max_err;
36 #endif
37
compute_t_from_x(float A,float B,float C,float x)38 static float compute_t_from_x(float A, float B, float C, float x) {
39 #ifdef CUBICMAP_TRACK_MAX_ERROR
40 float answer = compute_slow(A, B, C, x);
41 #endif
42 float answer2 = SkOpts::cubic_solver(A, B, C, -x);
43
44 #ifdef CUBICMAP_TRACK_MAX_ERROR
45 float err = sk_float_abs(answer - answer2);
46 if (err > max_err) {
47 max_err = err;
48 SkDebugf("max error %g\n", max_err);
49 }
50 #endif
51 return answer2;
52 }
53
computeYFromX(float x) const54 float SkCubicMap::computeYFromX(float x) const {
55 x = SkTPin(x, 0.0f, 1.0f);
56
57 if (nearly_zero(x) || nearly_zero(1 - x)) {
58 return x;
59 }
60 if (fType == kLine_Type) {
61 return x;
62 }
63 float t;
64 if (fType == kCubeRoot_Type) {
65 t = sk_float_pow(x / fCoeff[0].fX, 1.0f / 3);
66 } else {
67 t = compute_t_from_x(fCoeff[0].fX, fCoeff[1].fX, fCoeff[2].fX, x);
68 }
69 float a = fCoeff[0].fY;
70 float b = fCoeff[1].fY;
71 float c = fCoeff[2].fY;
72 float y = ((a * t + b) * t + c) * t;
73
74 return y;
75 }
76
coeff_nearly_zero(float delta)77 static inline bool coeff_nearly_zero(float delta) {
78 return sk_float_abs(delta) <= 0.0000001f;
79 }
80
SkCubicMap(SkPoint p1,SkPoint p2)81 SkCubicMap::SkCubicMap(SkPoint p1, SkPoint p2) {
82 // Clamp X values only (we allow Ys outside [0..1]).
83 p1.fX = std::min(std::max(p1.fX, 0.0f), 1.0f);
84 p2.fX = std::min(std::max(p2.fX, 0.0f), 1.0f);
85
86 Sk2s s1 = Sk2s::Load(&p1) * 3;
87 Sk2s s2 = Sk2s::Load(&p2) * 3;
88
89 (Sk2s(1) + s1 - s2).store(&fCoeff[0]);
90 (s2 - s1 - s1).store(&fCoeff[1]);
91 s1.store(&fCoeff[2]);
92
93 fType = kSolver_Type;
94 if (SkScalarNearlyEqual(p1.fX, p1.fY) && SkScalarNearlyEqual(p2.fX, p2.fY)) {
95 fType = kLine_Type;
96 } else if (coeff_nearly_zero(fCoeff[1].fX) && coeff_nearly_zero(fCoeff[2].fX)) {
97 fType = kCubeRoot_Type;
98 }
99 }
100
computeFromT(float t) const101 SkPoint SkCubicMap::computeFromT(float t) const {
102 Sk2s a = Sk2s::Load(&fCoeff[0]);
103 Sk2s b = Sk2s::Load(&fCoeff[1]);
104 Sk2s c = Sk2s::Load(&fCoeff[2]);
105
106 SkPoint result;
107 (((a * t + b) * t + c) * t).store(&result);
108 return result;
109 }
110