| 1 |
/* |
| 2 |
* Copyright 2020 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/SkCanvas.h" |
| 9 |
#include "include/core/SkFont.h" |
| 10 |
#include "include/core/SkPaint.h" |
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPathBuilder.h" |
| 13 |
#include "include/private/base/SkTArray.h" |
| 14 |
#include "tools/fonts/FontToolUtils.h" |
| 15 |
#include "tools/viewer/ClickHandlerSlide.h" |
| 16 |
|
| 17 |
#include <tuple> |
| 18 |
|
| 19 |
using namespace skia_private; |
| 20 |
|
| 21 |
// Math constants are not always defined. |
| 22 |
#ifndef M_PI |
| 23 |
#define M_PI 3.14159265358979323846264338327950288 |
| 24 |
#endif |
| 25 |
|
| 26 |
#ifndef M_SQRT2 |
| 27 |
#define M_SQRT2 1.41421356237309504880168872420969808 |
| 28 |
#endif |
| 29 |
|
| 30 |
constexpr static int kCenterX = 300; |
| 31 |
constexpr static int kCenterY = 325; |
| 32 |
constexpr static int kRadius = 250; |
| 33 |
|
| 34 |
// This sample fits a cubic to the arc between two interactive points on a circle. It also finds the |
| 35 |
// T-coordinate of max error, and outputs it and its value in pixels. (It turns out that max error |
| 36 |
// always occurs at T=0.21132486540519.) |
| 37 |
// |
| 38 |
// Press 'E' to iteratively cut the arc in half and report the improvement in max error after each |
| 39 |
// halving. (It turns out that max error improves by exactly 64x on every halving.) |
| 40 |
class SampleFitCubicToCircle : public ClickHandlerSlide { |
| 41 |
public: |
| 42 |
SampleFitCubicToCircle() { fName = "FitCubicToCircle"; } |
| 43 |
void load(SkScalar w, SkScalar h) override { this->fitCubic(); } |
| 44 |
void draw(SkCanvas*) override; |
| 45 |
bool onChar(SkUnichar) override; |
| 46 |
|
| 47 |
protected: |
| 48 |
Click* onFindClickHandler(SkScalar x, SkScalar y, skui::ModifierKey) override; |
| 49 |
bool onClick(Click*) override; |
| 50 |
|
| 51 |
private: |
| 52 |
void fitCubic(); |
| 53 |
// Coordinates of two points on the unit circle. These are the two endpoints of the arc we fit. |
| 54 |
double fEndptsX[2] = {0, 1}; |
| 55 |
double fEndptsY[2] = {-1, 0}; |
| 56 |
|
| 57 |
// Fitted cubic and info, set by fitCubic(). |
| 58 |
double fControlLength; // Length of (p1 - p0) and/or (p3 - p2) in unit circle space. |
| 59 |
double fMaxErrorT; // T value where the cubic diverges most from the true arc. |
| 60 |
std::array<double, 4> fCubicX; // Screen space cubic control points. |
| 61 |
std::array<double, 4> fCubicY; |
| 62 |
double fMaxError; // Max error (in pixels) between the cubic and the screen-space arc. |
| 63 |
double fTheta; // Angle of the arc. This is only used for informational purposes. |
| 64 |
TArray<SkString> fInfoStrings; |
| 65 |
|
| 66 |
class Click; |
| 67 |
}; |
| 68 |
|
| 69 |
// Fits a cubic to an arc on the unit circle with endpoints (x0, y0) and (x1, y1). Using the |
| 70 |
// following 3 constraints, we arrive at the formula used in the method: |
| 71 |
// |
| 72 |
// 1) The endpoints and tangent directions at the endpoints must match the arc. |
| 73 |
// 2) The cubic must be symmetric (i.e., length(p1 - p0) == length(p3 - p2)). |
| 74 |
// 3) The height of the cubic must match the height of the arc. |
| 75 |
// |
| 76 |
// Returns the "control length", or length of (p1 - p0) and/or (p3 - p2). |
| 77 |
static float fit_cubic_to_unit_circle(double x0, double y0, double x1, double y1, |
| 78 |
std::array<double, 4>* X, std::array<double, 4>* Y) { |
| 79 |
constexpr static double kM = -4.0/3; |
| 80 |
constexpr static double kA = 4*M_SQRT2/3; |
| 81 |
double d = x0*x1 + y0*y1; |
| 82 |
double c = (std::sqrt(1 + d) * kM + kA) / std::sqrt(1 - d); |
| 83 |
*X = {x0, x0 - y0*c, x1 + y1*c, x1}; |
| 84 |
*Y = {y0, y0 + x0*c, y1 - x1*c, y1}; |
| 85 |
return c; |
| 86 |
} |
| 87 |
|
| 88 |
static double lerp(double x, double y, double T) { |
| 89 |
return x + T*(y - x); |
| 90 |
} |
| 91 |
|
| 92 |
// Evaluates the cubic and 1st and 2nd derivatives at T. |
| 93 |
static std::tuple<double, double, double> eval_cubic(double x[], double T) { |
| 94 |
// Use De Casteljau's algorithm for better accuracy and stability. |
| 95 |
double ab = lerp(x[0], x[1], T); |
| 96 |
double bc = lerp(x[1], x[2], T); |
| 97 |
double cd = lerp(x[2], x[3], T); |
| 98 |
double abc = lerp(ab, bc, T); |
| 99 |
double bcd = lerp(bc, cd, T); |
| 100 |
double abcd = lerp(abc, bcd, T); |
| 101 |
return {abcd, 3 * (bcd - abc) /*1st derivative.*/, 6 * (cd - 2*bc + ab) /*2nd derivative.*/}; |
| 102 |
} |
| 103 |
|
| 104 |
// Uses newton-raphson convergence to find the point where the provided cubic diverges most from the |
| 105 |
// unit circle. i.e., the point where the derivative of error == 0. For error we use: |
| 106 |
// |
| 107 |
// error = x^2 + y^2 - 1 |
| 108 |
// error' = 2xx' + 2yy' |
| 109 |
// error'' = 2xx'' + 2yy'' + 2x'^2 + 2y'^2 |
| 110 |
// |
| 111 |
double find_max_error_T(double cubicX[4], double cubicY[4]) { |
| 112 |
constexpr static double kInitialT = .25; |
| 113 |
double T = kInitialT; |
| 114 |
for (int i = 0; i < 64; ++i) { |
| 115 |
auto [x, dx, ddx] = eval_cubic(cubicX, T); |
| 116 |
auto [y, dy, ddy] = eval_cubic(cubicY, T); |
| 117 |
double dError = 2*(x*dx + y*dy); |
| 118 |
double ddError = 2*(x*ddx + y*ddy + dx*dx + dy*dy); |
| 119 |
T -= dError / ddError; |
| 120 |
} |
| 121 |
return T; |
| 122 |
} |
| 123 |
|
| 124 |
void SampleFitCubicToCircle::fitCubic() { |
| 125 |
fInfoStrings.clear(); |
| 126 |
|
| 127 |
std::array<double, 4> X, Y; |
| 128 |
// "Control length" is the length of (p1 - p0) and/or (p3 - p2) in unit circle space. |
| 129 |
fControlLength = fit_cubic_to_unit_circle(fEndptsX[0], fEndptsY[0], fEndptsX[1], fEndptsY[1], |
| 130 |
&X, &Y); |
| 131 |
fInfoStrings.push_back().printf("control length=%0.14f", fControlLength); |
| 132 |
|
| 133 |
fMaxErrorT = find_max_error_T(X.data(), Y.data()); |
| 134 |
fInfoStrings.push_back().printf("max error T=%0.14f", fMaxErrorT); |
| 135 |
|
| 136 |
for (int i = 0; i < 4; ++i) { |
| 137 |
fCubicX[i] = X[i] * kRadius + kCenterX; |
| 138 |
fCubicY[i] = Y[i] * kRadius + kCenterY; |
| 139 |
} |
| 140 |
double errX = std::get<0>(eval_cubic(fCubicX.data(), fMaxErrorT)) - kCenterX; |
| 141 |
double errY = std::get<0>(eval_cubic(fCubicY.data(), fMaxErrorT)) - kCenterY; |
| 142 |
fMaxError = std::sqrt(errX*errX + errY*errY) - kRadius; |
| 143 |
fInfoStrings.push_back().printf("max error=%.5gpx", fMaxError); |
| 144 |
|
| 145 |
fTheta = std::atan2(fEndptsY[1], fEndptsX[1]) - std::atan2(fEndptsY[0], fEndptsX[0]); |
| 146 |
fTheta = std::abs(fTheta * 180/M_PI); |
| 147 |
if (fTheta > 180) { |
| 148 |
fTheta = 360 - fTheta; |
| 149 |
} |
| 150 |
fInfoStrings.push_back().printf("(theta=%.2f)", fTheta); |
| 151 |
|
| 152 |
SkDebugf("\n"); |
| 153 |
for (const SkString& infoString : fInfoStrings) { |
| 154 |
SkDebugf("%s\n", infoString.c_str()); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
void SampleFitCubicToCircle::draw(SkCanvas* canvas) { |
| 159 |
canvas->clear(SK_ColorBLACK); |
| 160 |
|
| 161 |
SkPaint circlePaint; |
| 162 |
circlePaint.setColor(0x80ffffff); |
| 163 |
circlePaint.setStyle(SkPaint::kStroke_Style); |
| 164 |
circlePaint.setStrokeWidth(0); |
| 165 |
circlePaint.setAntiAlias(true); |
| 166 |
canvas->drawArc(SkRect::MakeXYWH(kCenterX - kRadius, kCenterY - kRadius, kRadius * 2, |
| 167 |
kRadius * 2), 0, 360, false, circlePaint); |
| 168 |
|
| 169 |
SkPaint cubicPaint; |
| 170 |
cubicPaint.setColor(SK_ColorGREEN); |
| 171 |
cubicPaint.setStyle(SkPaint::kStroke_Style); |
| 172 |
cubicPaint.setStrokeWidth(10); |
| 173 |
cubicPaint.setAntiAlias(true); |
| 174 |
SkPath cubicPath = SkPathBuilder() |
| 175 |
.moveTo(fCubicX[0], fCubicY[0]) |
| 176 |
.cubicTo(fCubicX[1], fCubicY[1], fCubicX[2], fCubicY[2], fCubicX[3], fCubicY[3]) |
| 177 |
.detach(); |
| 178 |
canvas->drawPath(cubicPath, cubicPaint); |
| 179 |
|
| 180 |
SkPaint endpointsPaint; |
| 181 |
endpointsPaint.setColor(SK_ColorBLUE); |
| 182 |
endpointsPaint.setStrokeWidth(8); |
| 183 |
endpointsPaint.setAntiAlias(true); |
| 184 |
SkPoint points[2] = {{(float)fCubicX[0], (float)fCubicY[0]}, |
| 185 |
{(float)fCubicX[3], (float)fCubicY[3]}}; |
| 186 |
canvas->drawPoints(SkCanvas::kPoints_PointMode, points, endpointsPaint); |
| 187 |
|
| 188 |
SkPaint textPaint; |
| 189 |
textPaint.setColor(SK_ColorWHITE); |
| 190 |
constexpr static float kInfoTextSize = 16; |
| 191 |
SkFont font(ToolUtils::DefaultTypeface(), kInfoTextSize); |
| 192 |
int infoY = 10 + kInfoTextSize; |
| 193 |
for (const SkString& infoString : fInfoStrings) { |
| 194 |
canvas->drawString(infoString.c_str(), 10, infoY, font, textPaint); |
| 195 |
infoY += kInfoTextSize * 3/2; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
class SampleFitCubicToCircle::Click : public ClickHandlerSlide::Click { |
| 200 |
public: |
| 201 |
Click(int ptIdx) : fPtIdx(ptIdx) {} |
| 202 |
|
| 203 |
void doClick(SampleFitCubicToCircle* that) { |
| 204 |
double dx = fCurr.fX - kCenterX; |
| 205 |
double dy = fCurr.fY - kCenterY; |
| 206 |
double l = std::sqrt(dx*dx + dy*dy); |
| 207 |
that->fEndptsX[fPtIdx] = dx/l; |
| 208 |
that->fEndptsY[fPtIdx] = dy/l; |
| 209 |
if (that->fEndptsX[0] * that->fEndptsY[1] - that->fEndptsY[0] * that->fEndptsX[1] < 0) { |
| 210 |
std::swap(that->fEndptsX[0], that->fEndptsX[1]); |
| 211 |
std::swap(that->fEndptsY[0], that->fEndptsY[1]); |
| 212 |
fPtIdx = 1 - fPtIdx; |
| 213 |
} |
| 214 |
that->fitCubic(); |
| 215 |
} |
| 216 |
|
| 217 |
private: |
| 218 |
int fPtIdx; |
| 219 |
}; |
| 220 |
|
| 221 |
ClickHandlerSlide::Click* SampleFitCubicToCircle::onFindClickHandler(SkScalar x, SkScalar y, |
| 222 |
skui::ModifierKey) { |
| 223 |
double dx0 = x - fCubicX[0]; |
| 224 |
double dy0 = y - fCubicY[0]; |
| 225 |
double dx3 = x - fCubicX[3]; |
| 226 |
double dy3 = y - fCubicY[3]; |
| 227 |
if (dx0*dx0 + dy0*dy0 < dx3*dx3 + dy3*dy3) { |
| 228 |
return new Click(0); |
| 229 |
} else { |
| 230 |
return new Click(1); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
bool SampleFitCubicToCircle::onClick(ClickHandlerSlide::Click* click) { |
| 235 |
Click* myClick = (Click*)click; |
| 236 |
myClick->doClick(this); |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
bool SampleFitCubicToCircle::onChar(SkUnichar unichar) { |
| 241 |
if (unichar == 'E') { |
| 242 |
constexpr static double kMaxErrorT = 0.21132486540519; // Always the same. |
| 243 |
// Split the arc in half until error =~0, and report the improvement after each halving. |
| 244 |
double lastError = -1; |
| 245 |
for (double theta = fTheta; lastError != 0; theta /= 2) { |
| 246 |
double rads = theta * M_PI/180; |
| 247 |
std::array<double, 4> X, Y; |
| 248 |
fit_cubic_to_unit_circle(1, 0, std::cos(rads), std::sin(rads), &X, &Y); |
| 249 |
auto [x, dx, ddx] = eval_cubic(X.data(), kMaxErrorT); |
| 250 |
auto [y, dy, ddy] = eval_cubic(Y.data(), kMaxErrorT); |
| 251 |
double error = std::sqrt(x*x + y*y) * kRadius - kRadius; |
| 252 |
if ((float)error <= 0) { |
| 253 |
error = 0; |
| 254 |
} |
| 255 |
SkDebugf("%6.2f degrees: error= %10.5gpx", theta, error); |
| 256 |
if (lastError > 0) { |
| 257 |
SkDebugf(" (%17.14fx improvement)", lastError / error); |
| 258 |
} |
| 259 |
SkDebugf("\n"); |
| 260 |
lastError = error; |
| 261 |
} |
| 262 |
return true; |
| 263 |
} |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
DEF_SLIDE(return new SampleFitCubicToCircle;) |