Flutter Engine
The Flutter Engine
Macros | Typedefs | Functions
SkScalar.h File Reference
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkFloatingPoint.h"
#include <cmath>

Go to the source code of this file.

Macros

#define SK_Scalar1   1.0f
 
#define SK_ScalarHalf   0.5f
 
#define SK_ScalarSqrt2   SK_FloatSqrt2
 
#define SK_ScalarPI   SK_FloatPI
 
#define SK_ScalarTanPIOver8   0.414213562f
 
#define SK_ScalarRoot2Over2   0.707106781f
 
#define SK_ScalarMax   3.402823466e+38f
 
#define SK_ScalarMin   (-SK_ScalarMax)
 
#define SK_ScalarInfinity   SK_FloatInfinity
 
#define SK_ScalarNegativeInfinity   SK_FloatNegativeInfinity
 
#define SK_ScalarNaN   SK_FloatNaN
 
#define SkScalarFloorToScalar(x)   std::floor(x)
 
#define SkScalarCeilToScalar(x)   std::ceil(x)
 
#define SkScalarRoundToScalar(x)   sk_float_round(x)
 
#define SkScalarTruncToScalar(x)   std::trunc(x)
 
#define SkScalarFloorToInt(x)   sk_float_floor2int(x)
 
#define SkScalarCeilToInt(x)   sk_float_ceil2int(x)
 
#define SkScalarRoundToInt(x)   sk_float_round2int(x)
 
#define SkScalarAbs(x)   std::fabs(x)
 
#define SkScalarCopySign(x, y)   std::copysign(x, y)
 
#define SkScalarMod(x, y)   std::fmod(x,y)
 
#define SkScalarSqrt(x)   std::sqrt(x)
 
#define SkScalarPow(b, e)   std::pow(b, e)
 
#define SkScalarSin(radians)   ((float)std::sin(radians))
 
#define SkScalarCos(radians)   ((float)std::cos(radians))
 
#define SkScalarTan(radians)   ((float)std::tan(radians))
 
#define SkScalarASin(val)   ((float)std::asin(val))
 
#define SkScalarACos(val)   ((float)std::acos(val))
 
#define SkScalarATan2(y, x)   ((float)std::atan2(y,x))
 
#define SkScalarExp(x)   ((float)std::exp(x))
 
#define SkScalarLog(x)   ((float)std::log(x))
 
#define SkScalarLog2(x)   ((float)std::log2(x))
 
#define SkIntToScalar(x)   static_cast<SkScalar>(x)
 
#define SkIntToFloat(x)   static_cast<float>(x)
 
#define SkScalarTruncToInt(x)   sk_float_saturate2int(x)
 
#define SkScalarToFloat(x)   static_cast<float>(x)
 
#define SkFloatToScalar(x)   static_cast<SkScalar>(x)
 
#define SkScalarToDouble(x)   static_cast<double>(x)
 
#define SkDoubleToScalar(x)   sk_double_to_float(x)
 
#define SkScalarInvert(x)   (SK_Scalar1 / (x))
 
#define SkScalarAve(a, b)   (((a) + (b)) * SK_ScalarHalf)
 
#define SkScalarHalf(a)   ((a) * SK_ScalarHalf)
 
#define SkDegreesToRadians(degrees)   ((degrees) * (SK_ScalarPI / 180))
 
#define SkRadiansToDegrees(radians)   ((radians) * (180 / SK_ScalarPI))
 
#define SK_ScalarNearlyZero   (SK_Scalar1 / (1 << 12))
 
#define SK_ScalarSinCosNearlyZero   (SK_Scalar1 / (1 << 16))
 

Typedefs

typedef float SkScalar
 

Functions

static SkScalar SkScalarFraction (SkScalar x)
 
static SkScalar SkScalarSquare (SkScalar x)
 
static bool SkScalarIsInt (SkScalar x)
 
static int SkScalarSignAsInt (SkScalar x)
 
static SkScalar SkScalarSignAsScalar (SkScalar x)
 
static bool SkScalarNearlyZero (SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
 
static bool SkScalarNearlyEqual (SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
 
static float SkScalarSinSnapToZero (SkScalar radians)
 
static float SkScalarCosSnapToZero (SkScalar radians)
 
static SkScalar SkScalarInterp (SkScalar A, SkScalar B, SkScalar t)
 
SkScalar SkScalarInterpFunc (SkScalar searchKey, const SkScalar keys[], const SkScalar values[], int length)
 
static bool SkScalarsEqual (const SkScalar a[], const SkScalar b[], int n)
 

Macro Definition Documentation

◆ SK_Scalar1

#define SK_Scalar1   1.0f

Definition at line 18 of file SkScalar.h.

◆ SK_ScalarHalf

#define SK_ScalarHalf   0.5f

Definition at line 19 of file SkScalar.h.

◆ SK_ScalarInfinity

#define SK_ScalarInfinity   SK_FloatInfinity

Definition at line 26 of file SkScalar.h.

◆ SK_ScalarMax

#define SK_ScalarMax   3.402823466e+38f

Definition at line 24 of file SkScalar.h.

◆ SK_ScalarMin

#define SK_ScalarMin   (-SK_ScalarMax)

Definition at line 25 of file SkScalar.h.

◆ SK_ScalarNaN

#define SK_ScalarNaN   SK_FloatNaN

Definition at line 28 of file SkScalar.h.

◆ SK_ScalarNearlyZero

#define SK_ScalarNearlyZero   (SK_Scalar1 / (1 << 12))

Definition at line 99 of file SkScalar.h.

◆ SK_ScalarNegativeInfinity

#define SK_ScalarNegativeInfinity   SK_FloatNegativeInfinity

Definition at line 27 of file SkScalar.h.

◆ SK_ScalarPI

#define SK_ScalarPI   SK_FloatPI

Definition at line 21 of file SkScalar.h.

◆ SK_ScalarRoot2Over2

#define SK_ScalarRoot2Over2   0.707106781f

Definition at line 23 of file SkScalar.h.

◆ SK_ScalarSinCosNearlyZero

#define SK_ScalarSinCosNearlyZero   (SK_Scalar1 / (1 << 16))

Definition at line 113 of file SkScalar.h.

◆ SK_ScalarSqrt2

#define SK_ScalarSqrt2   SK_FloatSqrt2

Definition at line 20 of file SkScalar.h.

◆ SK_ScalarTanPIOver8

#define SK_ScalarTanPIOver8   0.414213562f

Definition at line 22 of file SkScalar.h.

◆ SkDegreesToRadians

#define SkDegreesToRadians (   degrees)    ((degrees) * (SK_ScalarPI / 180))

Definition at line 77 of file SkScalar.h.

◆ SkDoubleToScalar

#define SkDoubleToScalar (   x)    sk_double_to_float(x)

Definition at line 64 of file SkScalar.h.

◆ SkFloatToScalar

#define SkFloatToScalar (   x)    static_cast<SkScalar>(x)

Definition at line 62 of file SkScalar.h.

◆ SkIntToFloat

#define SkIntToFloat (   x)    static_cast<float>(x)

Definition at line 58 of file SkScalar.h.

◆ SkIntToScalar

#define SkIntToScalar (   x)    static_cast<SkScalar>(x)

Definition at line 57 of file SkScalar.h.

◆ SkRadiansToDegrees

#define SkRadiansToDegrees (   radians)    ((radians) * (180 / SK_ScalarPI))

Definition at line 78 of file SkScalar.h.

◆ SkScalarAbs

#define SkScalarAbs (   x)    std::fabs(x)

Definition at line 39 of file SkScalar.h.

◆ SkScalarACos

#define SkScalarACos (   val)    ((float)std::acos(val))

Definition at line 49 of file SkScalar.h.

◆ SkScalarASin

#define SkScalarASin (   val)    ((float)std::asin(val))

Definition at line 48 of file SkScalar.h.

◆ SkScalarATan2

#define SkScalarATan2 (   y,
  x 
)    ((float)std::atan2(y,x))

Definition at line 50 of file SkScalar.h.

◆ SkScalarAve

#define SkScalarAve (   a,
  b 
)    (((a) + (b)) * SK_ScalarHalf)

Definition at line 74 of file SkScalar.h.

◆ SkScalarCeilToInt

#define SkScalarCeilToInt (   x)    sk_float_ceil2int(x)

Definition at line 36 of file SkScalar.h.

◆ SkScalarCeilToScalar

#define SkScalarCeilToScalar (   x)    std::ceil(x)

Definition at line 31 of file SkScalar.h.

◆ SkScalarCopySign

#define SkScalarCopySign (   x,
  y 
)    std::copysign(x, y)

Definition at line 40 of file SkScalar.h.

◆ SkScalarCos

#define SkScalarCos (   radians)    ((float)std::cos(radians))

Definition at line 46 of file SkScalar.h.

◆ SkScalarExp

#define SkScalarExp (   x)    ((float)std::exp(x))

Definition at line 51 of file SkScalar.h.

◆ SkScalarFloorToInt

#define SkScalarFloorToInt (   x)    sk_float_floor2int(x)

Definition at line 35 of file SkScalar.h.

◆ SkScalarFloorToScalar

#define SkScalarFloorToScalar (   x)    std::floor(x)

Definition at line 30 of file SkScalar.h.

◆ SkScalarHalf

#define SkScalarHalf (   a)    ((a) * SK_ScalarHalf)

Definition at line 75 of file SkScalar.h.

◆ SkScalarInvert

#define SkScalarInvert (   x)    (SK_Scalar1 / (x))

Definition at line 73 of file SkScalar.h.

◆ SkScalarLog

#define SkScalarLog (   x)    ((float)std::log(x))

Definition at line 52 of file SkScalar.h.

◆ SkScalarLog2

#define SkScalarLog2 (   x)    ((float)std::log2(x))

Definition at line 53 of file SkScalar.h.

◆ SkScalarMod

#define SkScalarMod (   x,
  y 
)    std::fmod(x,y)

Definition at line 41 of file SkScalar.h.

◆ SkScalarPow

#define SkScalarPow (   b,
 
)    std::pow(b, e)

Definition at line 43 of file SkScalar.h.

◆ SkScalarRoundToInt

#define SkScalarRoundToInt (   x)    sk_float_round2int(x)

Definition at line 37 of file SkScalar.h.

◆ SkScalarRoundToScalar

#define SkScalarRoundToScalar (   x)    sk_float_round(x)

Definition at line 32 of file SkScalar.h.

◆ SkScalarSin

#define SkScalarSin (   radians)    ((float)std::sin(radians))

Definition at line 45 of file SkScalar.h.

◆ SkScalarSqrt

#define SkScalarSqrt (   x)    std::sqrt(x)

Definition at line 42 of file SkScalar.h.

◆ SkScalarTan

#define SkScalarTan (   radians)    ((float)std::tan(radians))

Definition at line 47 of file SkScalar.h.

◆ SkScalarToDouble

#define SkScalarToDouble (   x)    static_cast<double>(x)

Definition at line 63 of file SkScalar.h.

◆ SkScalarToFloat

#define SkScalarToFloat (   x)    static_cast<float>(x)

Definition at line 61 of file SkScalar.h.

◆ SkScalarTruncToInt

#define SkScalarTruncToInt (   x)    sk_float_saturate2int(x)

Definition at line 59 of file SkScalar.h.

◆ SkScalarTruncToScalar

#define SkScalarTruncToScalar (   x)    std::trunc(x)

Definition at line 33 of file SkScalar.h.

Typedef Documentation

◆ SkScalar

typedef float SkScalar

Definition at line 16 of file SkScalar.h.

Function Documentation

◆ SkScalarCosSnapToZero()

static float SkScalarCosSnapToZero ( SkScalar  radians)
inlinestatic

Definition at line 120 of file SkScalar.h.

120 {
121 float v = SkScalarCos(radians);
122 return SkScalarNearlyZero(v, SK_ScalarSinCosNearlyZero) ? 0.0f : v;
123}
#define SK_ScalarSinCosNearlyZero
Definition: SkScalar.h:113
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:101
#define SkScalarCos(radians)
Definition: SkScalar.h:46

◆ SkScalarFraction()

static SkScalar SkScalarFraction ( SkScalar  x)
inlinestatic

Returns the fractional part of the scalar.

Definition at line 67 of file SkScalar.h.

67 {
68 return x - SkScalarTruncToScalar(x);
69}
#define SkScalarTruncToScalar(x)
Definition: SkScalar.h:33
double x

◆ SkScalarInterp()

static SkScalar SkScalarInterp ( SkScalar  A,
SkScalar  B,
SkScalar  t 
)
inlinestatic

Linearly interpolate between A and B, based on t. If t is 0, return A If t is 1, return B else interpolate. t must be [0..SK_Scalar1]

Definition at line 131 of file SkScalar.h.

131 {
132 SkASSERT(t >= 0 && t <= SK_Scalar1);
133 return A + (B - A) * t;
134}
#define SkASSERT(cond)
Definition: SkAssert.h:116
#define SK_Scalar1
Definition: SkScalar.h:18

◆ SkScalarInterpFunc()

SkScalar SkScalarInterpFunc ( SkScalar  searchKey,
const SkScalar  keys[],
const SkScalar  values[],
int  length 
)

Interpolate along the function described by (keys[length], values[length]) for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] clamp to the min or max value. This function assumes the number of pairs (length) will be small and a linear search is used.

Repeated keys are allowed for discontinuous functions (so long as keys is monotonically increasing). If key is the value of a repeated scalar in keys the first one will be used.

Definition at line 11 of file SkScalar.cpp.

12 {
13 SkASSERT(length > 0);
14 SkASSERT(keys != nullptr);
15 SkASSERT(values != nullptr);
16#ifdef SK_DEBUG
17 for (int i = 1; i < length; i++) {
18 SkASSERT(keys[i-1] <= keys[i]);
19 }
20#endif
21 int right = 0;
22 while (right < length && keys[right] < searchKey) {
23 ++right;
24 }
25 // Could use sentinel values to eliminate conditionals, but since the
26 // tables are taken as input, a simpler format is better.
27 if (right == length) {
28 return values[length-1];
29 }
30 if (right == 0) {
31 return values[0];
32 }
33 // Otherwise, interpolate between right - 1 and right.
34 SkScalar leftKey = keys[right-1];
35 SkScalar rightKey = keys[right];
36 SkScalar fract = (searchKey - leftKey) / (rightKey - leftKey);
38}
static bool right(const SkPoint &p0, const SkPoint &p1)
static SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t)
Definition: SkScalar.h:131
float SkScalar
Definition: extension.cpp:12
size_t length
SIN Vec< N, float > fract(const Vec< N, float > &x)
Definition: SkVx.h:744

◆ SkScalarIsInt()

static bool SkScalarIsInt ( SkScalar  x)
inlinestatic

Definition at line 80 of file SkScalar.h.

80 {
81 return x == SkScalarFloorToScalar(x);
82}
#define SkScalarFloorToScalar(x)
Definition: SkScalar.h:30

◆ SkScalarNearlyEqual()

static bool SkScalarNearlyEqual ( SkScalar  x,
SkScalar  y,
SkScalar  tolerance = SK_ScalarNearlyZero 
)
inlinestatic

Definition at line 107 of file SkScalar.h.

108 {
109 SkASSERT(tolerance >= 0);
110 return SkScalarAbs(x-y) <= tolerance;
111}
#define SkScalarAbs(x)
Definition: SkScalar.h:39
double y

◆ SkScalarNearlyZero()

static bool SkScalarNearlyZero ( SkScalar  x,
SkScalar  tolerance = SK_ScalarNearlyZero 
)
inlinestatic

Definition at line 101 of file SkScalar.h.

102 {
103 SkASSERT(tolerance >= 0);
104 return SkScalarAbs(x) <= tolerance;
105}

◆ SkScalarsEqual()

static bool SkScalarsEqual ( const SkScalar  a[],
const SkScalar  b[],
int  n 
)
inlinestatic

Definition at line 151 of file SkScalar.h.

151 {
152 SkASSERT(n >= 0);
153 for (int i = 0; i < n; ++i) {
154 if (a[i] != b[i]) {
155 return false;
156 }
157 }
158 return true;
159}
static bool b
struct MyStruct a[10]

◆ SkScalarSignAsInt()

static int SkScalarSignAsInt ( SkScalar  x)
inlinestatic

Returns -1 || 0 || 1 depending on the sign of value: -1 if x < 0 0 if x == 0 1 if x > 0

Definition at line 90 of file SkScalar.h.

90 {
91 return x < 0 ? -1 : (x > 0);
92}

◆ SkScalarSignAsScalar()

static SkScalar SkScalarSignAsScalar ( SkScalar  x)
inlinestatic

Definition at line 95 of file SkScalar.h.

95 {
96 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
97}

◆ SkScalarSinSnapToZero()

static float SkScalarSinSnapToZero ( SkScalar  radians)
inlinestatic

Definition at line 115 of file SkScalar.h.

115 {
116 float v = SkScalarSin(radians);
117 return SkScalarNearlyZero(v, SK_ScalarSinCosNearlyZero) ? 0.0f : v;
118}
#define SkScalarSin(radians)
Definition: SkScalar.h:45

◆ SkScalarSquare()

static SkScalar SkScalarSquare ( SkScalar  x)
inlinestatic

Definition at line 71 of file SkScalar.h.

71{ return x * x; }