Flutter Engine
The Flutter Engine
SkScalar.h
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
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#ifndef SkScalar_DEFINED
9#define SkScalar_DEFINED
10
13
14#include <cmath>
15
16typedef float SkScalar;
17
18#define SK_Scalar1 1.0f
19#define SK_ScalarHalf 0.5f
20#define SK_ScalarSqrt2 SK_FloatSqrt2
21#define SK_ScalarPI SK_FloatPI
22#define SK_ScalarTanPIOver8 0.414213562f
23#define SK_ScalarRoot2Over2 0.707106781f
24#define SK_ScalarMax 3.402823466e+38f
25#define SK_ScalarMin (-SK_ScalarMax)
26#define SK_ScalarInfinity SK_FloatInfinity
27#define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
28#define SK_ScalarNaN SK_FloatNaN
29
30#define SkScalarFloorToScalar(x) std::floor(x)
31#define SkScalarCeilToScalar(x) std::ceil(x)
32#define SkScalarRoundToScalar(x) sk_float_round(x)
33#define SkScalarTruncToScalar(x) std::trunc(x)
34
35#define SkScalarFloorToInt(x) sk_float_floor2int(x)
36#define SkScalarCeilToInt(x) sk_float_ceil2int(x)
37#define SkScalarRoundToInt(x) sk_float_round2int(x)
38
39#define SkScalarAbs(x) std::fabs(x)
40#define SkScalarCopySign(x, y) std::copysign(x, y)
41#define SkScalarMod(x, y) std::fmod(x,y)
42#define SkScalarSqrt(x) std::sqrt(x)
43#define SkScalarPow(b, e) std::pow(b, e)
44
45#define SkScalarSin(radians) ((float)std::sin(radians))
46#define SkScalarCos(radians) ((float)std::cos(radians))
47#define SkScalarTan(radians) ((float)std::tan(radians))
48#define SkScalarASin(val) ((float)std::asin(val))
49#define SkScalarACos(val) ((float)std::acos(val))
50#define SkScalarATan2(y, x) ((float)std::atan2(y,x))
51#define SkScalarExp(x) ((float)std::exp(x))
52#define SkScalarLog(x) ((float)std::log(x))
53#define SkScalarLog2(x) ((float)std::log2(x))
54
55//////////////////////////////////////////////////////////////////////////////////////////////////
56
57#define SkIntToScalar(x) static_cast<SkScalar>(x)
58#define SkIntToFloat(x) static_cast<float>(x)
59#define SkScalarTruncToInt(x) sk_float_saturate2int(x)
60
61#define SkScalarToFloat(x) static_cast<float>(x)
62#define SkFloatToScalar(x) static_cast<SkScalar>(x)
63#define SkScalarToDouble(x) static_cast<double>(x)
64#define SkDoubleToScalar(x) sk_double_to_float(x)
65
66/** Returns the fractional part of the scalar. */
68 return x - SkScalarTruncToScalar(x);
69}
70
71static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
72
73#define SkScalarInvert(x) (SK_Scalar1 / (x))
74#define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
75#define SkScalarHalf(a) ((a) * SK_ScalarHalf)
76
77#define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
78#define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
79
80static inline bool SkScalarIsInt(SkScalar x) {
81 return x == SkScalarFloorToScalar(x);
82}
83
84/**
85 * Returns -1 || 0 || 1 depending on the sign of value:
86 * -1 if x < 0
87 * 0 if x == 0
88 * 1 if x > 0
89 */
90static inline int SkScalarSignAsInt(SkScalar x) {
91 return x < 0 ? -1 : (x > 0);
92}
93
94// Scalar result version of above
96 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
97}
98
99#define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
100
101static inline bool SkScalarNearlyZero(SkScalar x,
102 SkScalar tolerance = SK_ScalarNearlyZero) {
103 SkASSERT(tolerance >= 0);
104 return SkScalarAbs(x) <= tolerance;
105}
106
108 SkScalar tolerance = SK_ScalarNearlyZero) {
109 SkASSERT(tolerance >= 0);
110 return SkScalarAbs(x-y) <= tolerance;
111}
112
113#define SK_ScalarSinCosNearlyZero (SK_Scalar1 / (1 << 16))
114
115static inline float SkScalarSinSnapToZero(SkScalar radians) {
116 float v = SkScalarSin(radians);
117 return SkScalarNearlyZero(v, SK_ScalarSinCosNearlyZero) ? 0.0f : v;
118}
119
120static inline float SkScalarCosSnapToZero(SkScalar radians) {
121 float v = SkScalarCos(radians);
122 return SkScalarNearlyZero(v, SK_ScalarSinCosNearlyZero) ? 0.0f : v;
123}
124
125/** Linearly interpolate between A and B, based on t.
126 If t is 0, return A
127 If t is 1, return B
128 else interpolate.
129 t must be [0..SK_Scalar1]
130*/
132 SkASSERT(t >= 0 && t <= SK_Scalar1);
133 return A + (B - A) * t;
134}
135
136/** Interpolate along the function described by (keys[length], values[length])
137 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
138 clamp to the min or max value. This function assumes the number of pairs
139 (length) will be small and a linear search is used.
140
141 Repeated keys are allowed for discontinuous functions (so long as keys is
142 monotonically increasing). If key is the value of a repeated scalar in
143 keys the first one will be used.
144*/
145SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
146 const SkScalar values[], int length);
147
148/*
149 * Helper to compare an array of scalars.
150 */
151static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
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}
160
161#endif
#define SkASSERT(cond)
Definition: SkAssert.h:116
static SkScalar SkScalarSignAsScalar(SkScalar x)
Definition: SkScalar.h:95
#define SkScalarTruncToScalar(x)
Definition: SkScalar.h:33
#define SK_ScalarSinCosNearlyZero
Definition: SkScalar.h:113
#define SkScalarFloorToScalar(x)
Definition: SkScalar.h:30
static float SkScalarSinSnapToZero(SkScalar radians)
Definition: SkScalar.h:115
static bool SkScalarNearlyZero(SkScalar x, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:101
static int SkScalarSignAsInt(SkScalar x)
Definition: SkScalar.h:90
#define SkScalarSin(radians)
Definition: SkScalar.h:45
static bool SkScalarNearlyEqual(SkScalar x, SkScalar y, SkScalar tolerance=SK_ScalarNearlyZero)
Definition: SkScalar.h:107
#define SK_Scalar1
Definition: SkScalar.h:18
#define SK_ScalarNearlyZero
Definition: SkScalar.h:99
#define SkScalarCos(radians)
Definition: SkScalar.h:46
static bool SkScalarIsInt(SkScalar x)
Definition: SkScalar.h:80
static SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t)
Definition: SkScalar.h:131
SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], const SkScalar values[], int length)
Definition: SkScalar.cpp:11
float SkScalar
Definition: SkScalar.h:16
static SkScalar SkScalarFraction(SkScalar x)
Definition: SkScalar.h:67
static SkScalar SkScalarSquare(SkScalar x)
Definition: SkScalar.h:71
#define SkScalarAbs(x)
Definition: SkScalar.h:39
static float SkScalarCosSnapToZero(SkScalar radians)
Definition: SkScalar.h:120
static bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n)
Definition: SkScalar.h:151
float SkScalar
Definition: extension.cpp:12
static bool b
struct MyStruct a[10]
size_t length
double y
double x