Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions | Variables
SkCubics.cpp File Reference
#include "src/base/SkCubics.h"
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkFloatingPoint.h"
#include "include/private/base/SkTPin.h"
#include "src/base/SkQuads.h"
#include <algorithm>
#include <cmath>

Go to the source code of this file.

Functions

static bool nearly_equal (double x, double y)
 
static bool close_to_a_quadratic (double A, double B)
 
static bool approximately_zero (double x)
 
static int find_extrema_valid_t (double A, double B, double C, double t[2])
 
static double binary_search (double A, double B, double C, double D, double start, double stop)
 

Variables

static constexpr double PI = 3.141592653589793
 

Function Documentation

◆ approximately_zero()

static bool approximately_zero ( double  x)
static

Definition at line 153 of file SkCubics.cpp.

153 {
154 // This cutoff for our binary search hopefully strikes a good balance between
155 // performance and accuracy.
156 return std::abs(x) < 0.00000001;
157}
double x

◆ binary_search()

static double binary_search ( double  A,
double  B,
double  C,
double  D,
double  start,
double  stop 
)
static

Definition at line 176 of file SkCubics.cpp.

176 {
177 SkASSERT(start <= stop);
178 double left = SkCubics::EvalAt(A, B, C, D, start);
180 return start;
181 }
182 double right = SkCubics::EvalAt(A, B, C, D, stop);
183 if (!SkIsFinite(left, right)) {
184 return -1; // Not going to deal with one or more endpoints being non-finite.
185 }
186 if ((left > 0 && right > 0) || (left < 0 && right < 0)) {
187 return -1; // We can only have a root if one is above 0 and the other is below 0.
188 }
189
190 constexpr int maxIterations = 1000; // prevent infinite loop
191 for (int i = 0; i < maxIterations; i++) {
192 double step = (start + stop) / 2;
193 double curr = SkCubics::EvalAt(A, B, C, D, step);
194 if (approximately_zero(curr)) {
195 return step;
196 }
197 if ((curr < 0 && left < 0) || (curr > 0 && left > 0)) {
198 // go right
199 start = step;
200 } else {
201 // go left
202 stop = step;
203 }
204 }
205 return -1;
206}
static int step(int x, SkScalar min, SkScalar max)
Definition BlurTest.cpp:215
#define SkASSERT(cond)
Definition SkAssert.h:116
static bool approximately_zero(double x)
Definition SkCubics.cpp:153
static bool SkIsFinite(T x, Pack... values)
static bool left(const SkPoint &p0, const SkPoint &p1)
static bool right(const SkPoint &p0, const SkPoint &p1)
static double EvalAt(double A, double B, double C, double D, double t)
Definition SkCubics.h:51

◆ close_to_a_quadratic()

static bool close_to_a_quadratic ( double  A,
double  B 
)
static

Definition at line 31 of file SkCubics.cpp.

31 {
34 }
35 return std::abs(A / B) < 1.0e-7;
36}
bool sk_double_nearly_zero(double a)

◆ find_extrema_valid_t()

static int find_extrema_valid_t ( double  A,
double  B,
double  C,
double  t[2] 
)
static

Definition at line 159 of file SkCubics.cpp.

160 {
161 // To find the local min and max of a cubic, we take the derivative and
162 // solve when that is equal to 0.
163 // d/dt (A*t^3 + B*t^2 + C*t + D) = 3A*t^2 + 2B*t + C
164 double roots[2] = {0, 0};
165 int numRoots = SkQuads::RootsReal(3*A, 2*B, C, roots);
166 int validRoots = 0;
167 for (int i = 0; i < numRoots; i++) {
168 double tValue = roots[i];
169 if (tValue >= 0 && tValue <= 1.0) {
170 t[validRoots++] = tValue;
171 }
172 }
173 return validRoots;
174}
static int RootsReal(double A, double B, double C, double solution[2])
Definition SkQuads.cpp:135

◆ nearly_equal()

static bool nearly_equal ( double  x,
double  y 
)
static

Definition at line 20 of file SkCubics.cpp.

20 {
23 }
25}
bool sk_doubles_nearly_equal_ulps(double a, double b, uint8_t maxUlpsDiff=16)
double y

Variable Documentation

◆ PI

constexpr double PI = 3.141592653589793
staticconstexpr

Definition at line 18 of file SkCubics.cpp.