2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkScalar_DEFINED
9 #define SkScalar_DEFINED
12 #include "SkFloatingPoint.h"
14 //#define SK_SUPPORT_DEPRECATED_SCALARROUND
16 // TODO: move this sort of check into SkPostConfig.h
17 #define SK_SCALAR_IS_DOUBLE 0
18 #undef SK_SCALAR_IS_FLOAT
19 #define SK_SCALAR_IS_FLOAT 1
22 #if SK_SCALAR_IS_FLOAT
24 typedef float SkScalar;
26 #define SK_Scalar1 1.0f
27 #define SK_ScalarHalf 0.5f
28 #define SK_ScalarSqrt2 1.41421356f
29 #define SK_ScalarPI 3.14159265f
30 #define SK_ScalarTanPIOver8 0.414213562f
31 #define SK_ScalarRoot2Over2 0.707106781f
32 #define SK_ScalarMax 3.402823466e+38f
33 #define SK_ScalarInfinity SK_FloatInfinity
34 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
35 #define SK_ScalarNaN SK_FloatNaN
37 #define SkFixedToScalar(x) SkFixedToFloat(x)
38 #define SkScalarToFixed(x) SkFloatToFixed(x)
40 #define SkScalarFloorToScalar(x) sk_float_floor(x)
41 #define SkScalarCeilToScalar(x) sk_float_ceil(x)
42 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f)
44 #define SkScalarFloorToInt(x) sk_float_floor2int(x)
45 #define SkScalarCeilToInt(x) sk_float_ceil2int(x)
46 #define SkScalarRoundToInt(x) sk_float_round2int(x)
48 #define SkScalarAbs(x) sk_float_abs(x)
49 #define SkScalarCopySign(x, y) sk_float_copysign(x, y)
50 #define SkScalarMod(x, y) sk_float_mod(x,y)
51 #define SkScalarFraction(x) sk_float_mod(x, 1.0f)
52 #define SkScalarSqrt(x) sk_float_sqrt(x)
53 #define SkScalarPow(b, e) sk_float_pow(b, e)
55 #define SkScalarSin(radians) (float)sk_float_sin(radians)
56 #define SkScalarCos(radians) (float)sk_float_cos(radians)
57 #define SkScalarTan(radians) (float)sk_float_tan(radians)
58 #define SkScalarASin(val) (float)sk_float_asin(val)
59 #define SkScalarACos(val) (float)sk_float_acos(val)
60 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
61 #define SkScalarExp(x) (float)sk_float_exp(x)
62 #define SkScalarLog(x) (float)sk_float_log(x)
64 #else // SK_SCALAR_IS_DOUBLE
66 typedef double SkScalar;
68 #define SK_Scalar1 1.0
69 #define SK_ScalarHalf 0.5
70 #define SK_ScalarSqrt2 1.414213562373095
71 #define SK_ScalarPI 3.141592653589793
72 #define SK_ScalarTanPIOver8 0.4142135623731
73 #define SK_ScalarRoot2Over2 0.70710678118655
74 #define SK_ScalarMax 1.7976931348623157+308
75 #define SK_ScalarInfinity SK_DoubleInfinity
76 #define SK_ScalarNegativeInfinity SK_DoubleNegativeInfinity
77 #define SK_ScalarNaN SK_DoubleNaN
79 #define SkFixedToScalar(x) SkFixedToDouble(x)
80 #define SkScalarToFixed(x) SkDoubleToFixed(x)
82 #define SkScalarFloorToScalar(x) floor(x)
83 #define SkScalarCeilToScalar(x) ceil(x)
84 #define SkScalarRoundToScalar(x) floor((x) + 0.5)
86 #define SkScalarFloorToInt(x) (int)floor(x)
87 #define SkScalarCeilToInt(x) (int)ceil(x)
88 #define SkScalarRoundToInt(x) (int)floor((x) + 0.5)
90 #define SkScalarAbs(x) abs(x)
91 #define SkScalarCopySign(x, y) copysign(x, y)
92 #define SkScalarMod(x, y) fmod(x,y)
93 #define SkScalarFraction(x) fmod(x, 1.0)
94 #define SkScalarSqrt(x) sqrt(x)
95 #define SkScalarPow(b, e) pow(b, e)
97 #define SkScalarSin(radians) sin(radians)
98 #define SkScalarCos(radians) cos(radians)
99 #define SkScalarTan(radians) tan(radians)
100 #define SkScalarASin(val) asin(val)
101 #define SkScalarACos(val) acos(val)
102 #define SkScalarATan2(y, x) atan2(y,x)
103 #define SkScalarExp(x) exp(x)
104 #define SkScalarLog(x) log(x)
108 //////////////////////////////////////////////////////////////////////////////////////////////////
110 #define SkIntToScalar(x) static_cast<SkScalar>(x)
111 #define SkScalarTruncToInt(x) static_cast<int>(x)
113 #define SkScalarToFloat(x) static_cast<float>(x)
114 #define SkFloatToScalar(x) static_cast<SkScalar>(x)
115 #define SkScalarToDouble(x) static_cast<double>(x)
116 #define SkDoubleToScalar(x) static_cast<SkScalar>(x)
118 #define SK_ScalarMin (-SK_ScalarMax)
120 static inline bool SkScalarIsNaN(SkScalar x) { return x != x; }
122 /** Returns true if x is not NaN and not infinite
124 static inline bool SkScalarIsFinite(SkScalar x) {
125 // We rely on the following behavior of infinities and nans
127 // 0 * infinity --> NaN
129 SkScalar prod = x * 0;
130 // At this point, prod will either be NaN or 0
131 // Therefore we can return (prod == prod) or (0 == prod).
136 * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using
137 * double, to avoid possibly losing the low bit(s) of the answer before calling floor().
139 * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the
140 * extra precision is known to be valuable.
142 * In particular, this catches the following case:
143 * SkScalar x = 0.49999997;
144 * int ix = SkScalarRoundToInt(x);
145 * SkASSERT(0 == ix); // <--- fails
146 * ix = SkDScalarRoundToInt(x);
147 * SkASSERT(0 == ix); // <--- succeeds
149 static inline int SkDScalarRoundToInt(SkScalar x) {
152 return (int)floor(xx);
155 static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
156 return x < 0 ? 0 : x > max ? max : x;
159 static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
160 return x < min ? min : x > max ? max : x;
163 SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
165 static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
167 #define SkScalarMul(a, b) ((SkScalar)(a) * (b))
168 #define SkScalarMulAdd(a, b, c) ((SkScalar)(a) * (b) + (c))
169 #define SkScalarDiv(a, b) ((SkScalar)(a) / (b))
170 #define SkScalarMulDiv(a, b, c) ((SkScalar)(a) * (b) / (c))
171 #define SkScalarInvert(x) (SK_Scalar1 / (x))
172 #define SkScalarFastInvert(x) (SK_Scalar1 / (x))
173 #define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
174 #define SkScalarHalf(a) ((a) * SK_ScalarHalf)
176 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
177 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
179 static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
180 static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
182 static inline bool SkScalarIsInt(SkScalar x) {
183 return x == (SkScalar)(int)x;
186 // DEPRECATED : use ToInt or ToScalar variant
187 #ifdef SK_SUPPORT_DEPRECATED_SCALARROUND
188 # define SkScalarFloor(x) SkScalarFloorToInt(x)
189 # define SkScalarCeil(x) SkScalarCeilToInt(x)
190 # define SkScalarRound(x) SkScalarRoundToInt(x)
194 * Returns -1 || 0 || 1 depending on the sign of value:
199 static inline int SkScalarSignAsInt(SkScalar x) {
200 return x < 0 ? -1 : (x > 0);
203 // Scalar result version of above
204 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
205 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
208 #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
210 static inline bool SkScalarNearlyZero(SkScalar x,
211 SkScalar tolerance = SK_ScalarNearlyZero) {
212 SkASSERT(tolerance >= 0);
213 return SkScalarAbs(x) <= tolerance;
216 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
217 SkScalar tolerance = SK_ScalarNearlyZero) {
218 SkASSERT(tolerance >= 0);
219 return SkScalarAbs(x-y) <= tolerance;
222 /** Linearly interpolate between A and B, based on t.
226 t must be [0..SK_Scalar1]
228 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
229 SkASSERT(t >= 0 && t <= SK_Scalar1);
230 return A + (B - A) * t;
233 /** Interpolate along the function described by (keys[length], values[length])
234 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
235 clamp to the min or max value. This function was inspired by a desire
236 to change the multiplier for thickness in fakeBold; therefore it assumes
237 the number of pairs (length) will be small, and a linear search is used.
238 Repeated keys are allowed for discontinuous functions (so long as keys is
239 monotonically increasing), and if key is the value of a repeated scalar in
240 keys, the first one will be used. However, that may change if a binary
243 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
244 const SkScalar values[], int length);
247 * Helper to compare an array of scalars.
249 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
251 for (int i = 0; i < n; ++i) {