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 typedef float SkScalar;
18 /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
20 #define SK_Scalar1 (1.0f)
21 /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
23 #define SK_ScalarHalf (0.5f)
24 /** SK_ScalarInfinity is defined to be infinity as an SkScalar
26 #define SK_ScalarInfinity SK_FloatInfinity
27 /** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar
29 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
30 /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
32 #define SK_ScalarMax (3.402823466e+38f)
33 /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
35 #define SK_ScalarMin (-SK_ScalarMax)
36 /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
38 #define SK_ScalarNaN SK_FloatNaN
39 /** SkScalarIsNaN(n) returns true if argument is not a number
41 static inline bool SkScalarIsNaN(float x) { return x != x; }
43 /** Returns true if x is not NaN and not infinite */
44 static inline bool SkScalarIsFinite(float x) {
45 // We rely on the following behavior of infinities and nans
47 // 0 * infinity --> NaN
50 // At this point, prod will either be NaN or 0
51 // Therefore we can return (prod == prod) or (0 == prod).
55 /** SkIntToScalar(n) returns its integer argument as an SkScalar
57 #define SkIntToScalar(n) ((float)(n))
58 /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
60 #define SkFixedToScalar(x) SkFixedToFloat(x)
61 /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
63 #define SkScalarToFixed(x) SkFloatToFixed(x)
65 #define SkScalarToFloat(n) (n)
66 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED
67 #define SkFloatToScalar(n) (n)
70 #define SkScalarToDouble(n) (double)(n)
71 #define SkDoubleToScalar(n) (float)(n)
73 /** SkScalarFraction(x) returns the signed fractional part of the argument
75 #define SkScalarFraction(x) sk_float_mod(x, 1.0f)
77 #define SkScalarFloorToScalar(x) sk_float_floor(x)
78 #define SkScalarCeilToScalar(x) sk_float_ceil(x)
79 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f)
81 #define SkScalarFloorToInt(x) sk_float_floor2int(x)
82 #define SkScalarCeilToInt(x) sk_float_ceil2int(x)
83 #define SkScalarRoundToInt(x) sk_float_round2int(x)
84 #define SkScalarTruncToInt(x) static_cast<int>(x)
86 /** Returns the absolute value of the specified SkScalar
88 #define SkScalarAbs(x) sk_float_abs(x)
89 /** Return x with the sign of y
91 #define SkScalarCopySign(x, y) sk_float_copysign(x, y)
92 /** Returns the value pinned between 0 and max inclusive
94 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
95 return x < 0 ? 0 : x > max ? max : x;
97 /** Returns the value pinned between min and max inclusive
99 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
100 return x < min ? min : x > max ? max : x;
102 /** Returns the specified SkScalar squared (x*x)
104 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
105 /** Returns the product of two SkScalars
107 #define SkScalarMul(a, b) ((float)(a) * (b))
108 /** Returns the product of two SkScalars plus a third SkScalar
110 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
111 /** Returns the quotient of two SkScalars (a/b)
113 #define SkScalarDiv(a, b) ((float)(a) / (b))
114 /** Returns the mod of two SkScalars (a mod b)
116 #define SkScalarMod(x,y) sk_float_mod(x,y)
117 /** Returns the product of the first two arguments, divided by the third argument
119 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
120 /** Returns the multiplicative inverse of the SkScalar (1/x)
122 #define SkScalarInvert(x) (SK_Scalar1 / (x))
123 #define SkScalarFastInvert(x) (SK_Scalar1 / (x))
124 /** Returns the square root of the SkScalar
126 #define SkScalarSqrt(x) sk_float_sqrt(x)
127 /** Returns b to the e
129 #define SkScalarPow(b, e) sk_float_pow(b, e)
130 /** Returns the average of two SkScalars (a+b)/2
132 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f)
133 /** Returns one half of the specified SkScalar
135 #define SkScalarHalf(a) ((a) * 0.5f)
137 #define SK_ScalarSqrt2 1.41421356f
138 #define SK_ScalarPI 3.14159265f
139 #define SK_ScalarTanPIOver8 0.414213562f
140 #define SK_ScalarRoot2Over2 0.707106781f
142 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
143 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
144 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
145 #define SkScalarSin(radians) (float)sk_float_sin(radians)
146 #define SkScalarCos(radians) (float)sk_float_cos(radians)
147 #define SkScalarTan(radians) (float)sk_float_tan(radians)
148 #define SkScalarASin(val) (float)sk_float_asin(val)
149 #define SkScalarACos(val) (float)sk_float_acos(val)
150 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
151 #define SkScalarExp(x) (float)sk_float_exp(x)
152 #define SkScalarLog(x) (float)sk_float_log(x)
154 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
155 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
157 static inline bool SkScalarIsInt(SkScalar x) {
158 return x == (float)(int)x;
161 // DEPRECATED : use ToInt or ToScalar variant
162 #ifdef SK_SUPPORT_DEPRECATED_SCALARROUND
163 # define SkScalarFloor(x) SkScalarFloorToInt(x)
164 # define SkScalarCeil(x) SkScalarCeilToInt(x)
165 # define SkScalarRound(x) SkScalarRoundToInt(x)
169 * Returns -1 || 0 || 1 depending on the sign of value:
174 static inline int SkScalarSignAsInt(SkScalar x) {
175 return x < 0 ? -1 : (x > 0);
178 // Scalar result version of above
179 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
180 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
183 #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
185 static inline bool SkScalarNearlyZero(SkScalar x,
186 SkScalar tolerance = SK_ScalarNearlyZero) {
187 SkASSERT(tolerance >= 0);
188 return SkScalarAbs(x) <= tolerance;
191 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
192 SkScalar tolerance = SK_ScalarNearlyZero) {
193 SkASSERT(tolerance >= 0);
194 return SkScalarAbs(x-y) <= tolerance;
197 /** Linearly interpolate between A and B, based on t.
201 t must be [0..SK_Scalar1]
203 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
204 SkASSERT(t >= 0 && t <= SK_Scalar1);
205 return A + (B - A) * t;
208 /** Interpolate along the function described by (keys[length], values[length])
209 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
210 clamp to the min or max value. This function was inspired by a desire
211 to change the multiplier for thickness in fakeBold; therefore it assumes
212 the number of pairs (length) will be small, and a linear search is used.
213 Repeated keys are allowed for discontinuous functions (so long as keys is
214 monotonically increasing), and if key is the value of a repeated scalar in
215 keys, the first one will be used. However, that may change if a binary
218 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
219 const SkScalar values[], int length);
222 * Helper to compare an array of scalars.
224 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
226 for (int i = 0; i < n; ++i) {