| 1 |
/* |
| 2 |
* Copyright 2023 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 |
#ifndef SkCubics_DEFINED |
| 8 |
#define SkCubics_DEFINED |
| 9 |
|
| 10 |
#include <cmath> |
| 11 |
|
| 12 |
/** |
| 13 |
* Utilities for dealing with cubic formulas with one variable: |
| 14 |
* f(t) = A*t^3 + B*t^2 + C*t + d |
| 15 |
*/ |
| 16 |
class SkCubics { |
| 17 |
public: |
| 18 |
/** |
| 19 |
* Puts up to 3 real solutions to the equation |
| 20 |
* A*t^3 + B*t^2 + C*t + d = 0 |
| 21 |
* in the provided array and returns how many roots that was. |
| 22 |
*/ |
| 23 |
static int RootsReal(double A, double B, double C, double D, |
| 24 |
double solution[3]); |
| 25 |
|
| 26 |
/** |
| 27 |
* Puts up to 3 real solutions to the equation |
| 28 |
* A*t^3 + B*t^2 + C*t + D = 0 |
| 29 |
* in the provided array, with the constraint that t is in the range [0.0, 1.0], |
| 30 |
* and returns how many roots that was. |
| 31 |
*/ |
| 32 |
static int RootsValidT(double A, double B, double C, double D, |
| 33 |
double solution[3]); |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* Puts up to 3 real solutions to the equation |
| 38 |
* A*t^3 + B*t^2 + C*t + D = 0 |
| 39 |
* in the provided array, with the constraint that t is in the range [0.0, 1.0], |
| 40 |
* and returns how many roots that was. |
| 41 |
* This is a slower method than RootsValidT, but more accurate in circumstances |
| 42 |
* where floating point error gets too big. |
| 43 |
*/ |
| 44 |
static int BinarySearchRootsValidT(double A, double B, double C, double D, |
| 45 |
double solution[3]); |
| 46 |
|
| 47 |
/** |
| 48 |
* Evaluates the cubic function with the 4 provided coefficients and the |
| 49 |
* provided variable. |
| 50 |
*/ |
| 51 |
static double EvalAt(double A, double B, double C, double D, double t) { |
| 52 |
return std::fma(t, std::fma(t, std::fma(t, A, B), C), D); |
| 53 |
} |
| 54 |
|
| 55 |
static double EvalAt(double coefficients[4], double t) { |
| 56 |
return EvalAt(coefficients[0], coefficients[1], coefficients[2], coefficients[3], t); |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
#endif |