Debian Codegraph @

src/base/SkBezierCurves.cpp

raw
1 /*
2 * Copyright 2012 Google LLC
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 "src/base/SkBezierCurves.h"
9
10 #include "include/private/base/SkAssert.h"
11 #include "include/private/base/SkFloatingPoint.h"
12 #include "include/private/base/SkPoint_impl.h"
13 #include "include/private/base/SkSpan_impl.h"
14 #include "src/base/SkCubics.h"
15 #include "src/base/SkQuads.h"
16
17 #include <cstddef>
18
19 static inline double interpolate(double A, double B, double t) {
20 return A + (B - A) * t;
21 }
22
23 std::array<double, 2> SkBezierCubic::EvalAt(const double curve[8], double t) {
24 const auto in_X = [&curve](size_t n) { return curve[2*n]; };
25 const auto in_Y = [&curve](size_t n) { return curve[2*n + 1]; };
26
27 // Two semi-common fast paths
28 if (t == 0) {
29 return {in_X(0), in_Y(0)};
30 }
31 if (t == 1) {
32 return {in_X(3), in_Y(3)};
33 }
34 // X(t) = X_0*(1-t)^3 + 3*X_1*t(1-t)^2 + 3*X_2*t^2(1-t) + X_3*t^3
35 // Y(t) = Y_0*(1-t)^3 + 3*Y_1*t(1-t)^2 + 3*Y_2*t^2(1-t) + Y_3*t^3
36 // Some compilers are smart enough and have sufficient registers/intrinsics to write optimal
37 // code from
38 // double one_minus_t = 1 - t;
39 // double a = one_minus_t * one_minus_t * one_minus_t;
40 // double b = 3 * one_minus_t * one_minus_t * t;
41 // double c = 3 * one_minus_t * t * t;
42 // double d = t * t * t;
43 // However, some (e.g. when compiling for ARM) fail to do so, so we use this form
44 // to help more compilers generate smaller/faster ASM. https://godbolt.org/z/M6jG9x45c
45 double one_minus_t = 1 - t;
46 double one_minus_t_squared = one_minus_t * one_minus_t;
47 double a = (one_minus_t_squared * one_minus_t);
48 double b = 3 * one_minus_t_squared * t;
49 double t_squared = t * t;
50 double c = 3 * one_minus_t * t_squared;
51 double d = t_squared * t;
52
53 return {a * in_X(0) + b * in_X(1) + c * in_X(2) + d * in_X(3),
54 a * in_Y(0) + b * in_Y(1) + c * in_Y(2) + d * in_Y(3)};
55 }
56
57 // Perform subdivision using De Casteljau's algorithm, that is, repeated linear
58 // interpolation between adjacent points.
59 void SkBezierCubic::Subdivide(const double curve[8], double t,
60 double twoCurves[14]) {
61 SkASSERT(0.0 <= t && t <= 1.0);
62 // We split the curve "in" into two curves "alpha" and "beta"
63 const auto in_X = [&curve](size_t n) { return curve[2*n]; };
64 const auto in_Y = [&curve](size_t n) { return curve[2*n + 1]; };
65 const auto alpha_X = [&twoCurves](size_t n) -> double& { return twoCurves[2*n]; };
66 const auto alpha_Y = [&twoCurves](size_t n) -> double& { return twoCurves[2*n + 1]; };
67 const auto beta_X = [&twoCurves](size_t n) -> double& { return twoCurves[2*n + 6]; };
68 const auto beta_Y = [&twoCurves](size_t n) -> double& { return twoCurves[2*n + 7]; };
69
70 alpha_X(0) = in_X(0);
71 alpha_Y(0) = in_Y(0);
72
73 beta_X(3) = in_X(3);
74 beta_Y(3) = in_Y(3);
75
76 double x01 = interpolate(in_X(0), in_X(1), t);
77 double y01 = interpolate(in_Y(0), in_Y(1), t);
78 double x12 = interpolate(in_X(1), in_X(2), t);
79 double y12 = interpolate(in_Y(1), in_Y(2), t);
80 double x23 = interpolate(in_X(2), in_X(3), t);
81 double y23 = interpolate(in_Y(2), in_Y(3), t);
82
83 alpha_X(1) = x01;
84 alpha_Y(1) = y01;
85
86 beta_X(2) = x23;
87 beta_Y(2) = y23;
88
89 alpha_X(2) = interpolate(x01, x12, t);
90 alpha_Y(2) = interpolate(y01, y12, t);
91
92 beta_X(1) = interpolate(x12, x23, t);
93 beta_Y(1) = interpolate(y12, y23, t);
94
95 alpha_X(3) /*= beta_X(0) */ = interpolate(alpha_X(2), beta_X(1), t);
96 alpha_Y(3) /*= beta_Y(0) */ = interpolate(alpha_Y(2), beta_Y(1), t);
97 }
98
99 std::array<double, 4> SkBezierCubic::ConvertToPolynomial(const double curve[8], bool yValues) {
100 const double* offset_curve = yValues ? curve + 1 : curve;
101 const auto P = [&offset_curve](size_t n) { return offset_curve[2*n]; };
102 // A cubic Bézier curve is interpolated as follows:
103 // c(t) = (1 - t)^3 P_0 + 3t(1 - t)^2 P_1 + 3t^2 (1 - t) P_2 + t^3 P_3
104 // = (-P_0 + 3P_1 + -3P_2 + P_3) t^3 + (3P_0 - 6P_1 + 3P_2) t^2 +
105 // (-3P_0 + 3P_1) t + P_0
106 // Where P_N is the Nth point. The second step expands the polynomial and groups
107 // by powers of t. The desired output is a cubic formula, so we just need to
108 // combine the appropriate points to make the coefficients.
109 std::array<double, 4> results;
110 results[0] = -P(0) + 3*P(1) - 3*P(2) + P(3);
111 results[1] = 3*P(0) - 6*P(1) + 3*P(2);
112 results[2] = -3*P(0) + 3*P(1);
113 results[3] = P(0);
114 return results;
115 }
116
117 namespace {
118 struct DPoint {
119 DPoint(double x_, double y_) : x{x_}, y{y_} {}
120 DPoint(SkPoint p) : x{p.fX}, y{p.fY} {}
121 double x, y;
122 };
123
124 DPoint operator- (DPoint a) {
125 return {-a.x, -a.y};
126 }
127
128 DPoint operator+ (DPoint a, DPoint b) {
129 return {a.x + b.x, a.y + b.y};
130 }
131
132 DPoint operator- (DPoint a, DPoint b) {
133 return {a.x - b.x, a.y - b.y};
134 }
135
136 DPoint operator* (double s, DPoint a) {
137 return {s * a.x, s * a.y};
138 }
139
140 // Pin to 0 or 1 if within half a float ulp of 0 or 1.
141 double pinTRange(double t) {
142 // The ULPs around 0 are tiny compared to the ULPs around 1. Shift to 1 to use the same
143 // size ULPs.
144 if (sk_double_to_float(t + 1.0) == 1.0f) {
145 return 0.0;
146 } else if (sk_double_to_float(t) == 1.0f) {
147 return 1.0;
148 }
149 return t;
150 }
151 } // namespace
152
153 SkSpan<const float>
154 SkBezierCubic::IntersectWithHorizontalLine(
155 SkSpan<const SkPoint> controlPoints, float yIntercept, float* intersectionStorage) {
156 SkASSERT(controlPoints.size() >= 4);
157 const DPoint P0 = controlPoints[0],
158 P1 = controlPoints[1],
159 P2 = controlPoints[2],
160 P3 = controlPoints[3];
161
162 const DPoint A = -P0 + 3*P1 - 3*P2 + P3,
163 B = 3*P0 - 6*P1 + 3*P2,
164 C = -3*P0 + 3*P1,
165 D = P0;
166
167 return Intersect(A.x, B.x, C.x, D.x, A.y, B.y, C.y, D.y, yIntercept, intersectionStorage);
168 }
169
170 SkSpan<const float>
171 SkBezierCubic::Intersect(double AX, double BX, double CX, double DX,
172 double AY, double BY, double CY, double DY,
173 float toIntersect, float intersectionsStorage[3]) {
174 double roots[3];
175 SkSpan<double> ts = SkSpan(roots,
176 SkCubics::RootsReal(AY, BY, CY, DY - toIntersect, roots));
177
178 size_t intersectionCount = 0;
179 for (double t : ts) {
180 const double pinnedT = pinTRange(t);
181 if (0 <= pinnedT && pinnedT <= 1) {
182 intersectionsStorage[intersectionCount++] = SkCubics::EvalAt(AX, BX, CX, DX, pinnedT);
183 }
184 }
185
186 return {intersectionsStorage, intersectionCount};
187 }
188
189 SkSpan<const float>
190 SkBezierQuad::IntersectWithHorizontalLine(SkSpan<const SkPoint> controlPoints, float yIntercept,
191 float intersectionStorage[2]) {
192 SkASSERT(controlPoints.size() >= 3);
193 const DPoint p0 = controlPoints[0],
194 p1 = controlPoints[1],
195 p2 = controlPoints[2];
196
197 // Calculate A, B, C using doubles to reduce round-off error.
198 const DPoint A = p0 - 2 * p1 + p2,
199 // Remember we are generating the polynomial in the form A*t^2 -2*B*t + C, so the factor
200 // of 2 is not needed and the term is negated. This term for a Bézier curve is usually
201 // 2(p1-p0).
202 B = p0 - p1,
203 C = p0;
204
205 return Intersect(A.x, B.x, C.x, A.y, B.y, C.y, yIntercept, intersectionStorage);
206 }
207
208 SkSpan<const float> SkBezierQuad::Intersect(
209 double AX, double BX, double CX, double AY, double BY, double CY,
210 double yIntercept, float intersectionStorage[2]) {
211 auto [discriminant, r0, r1] = SkQuads::Roots(AY, BY, CY - yIntercept);
212
213 size_t intersectionCount = 0;
214 // Round the roots to the nearest float to generate the values t. Valid t's are on the
215 // domain [0, 1].
216 const double t0 = pinTRange(r0);
217 if (0 <= t0 && t0 <= 1) {
218 intersectionStorage[intersectionCount++] = SkQuads::EvalAt(AX, -2 * BX, CX, t0);
219 }
220
221 const double t1 = pinTRange(r1);
222 if (0 <= t1 && t1 <= 1 && t1 != t0) {
223 intersectionStorage[intersectionCount++] = SkQuads::EvalAt(AX, -2 * BX, CX, t1);
224 }
225
226 return {intersectionStorage, intersectionCount};
227 }
228