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