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 11 #include "SkFixed.h" 12 #include "SkFloatingPoint.h" 13 14 //#define SK_SUPPORT_DEPRECATED_SCALARROUND 15 16 typedef float SkScalar; 17 18 /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar 19 */ 20 #define SK_Scalar1 (1.0f) 21 /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar 22 */ 23 #define SK_ScalarHalf (0.5f) 24 /** SK_ScalarInfinity is defined to be infinity as an SkScalar 25 */ 26 #define SK_ScalarInfinity SK_FloatInfinity 27 /** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar 28 */ 29 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity 30 /** SK_ScalarMax is defined to be the largest value representable as an SkScalar 31 */ 32 #define SK_ScalarMax (3.402823466e+38f) 33 /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar 34 */ 35 #define SK_ScalarMin (-SK_ScalarMax) 36 /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar 37 */ 38 #define SK_ScalarNaN SK_FloatNaN 39 /** SkScalarIsNaN(n) returns true if argument is not a number 40 */ 41 static inline bool SkScalarIsNaN(float x) { return x != x; } 42 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 46 // 0 * finite --> 0 47 // 0 * infinity --> NaN 48 // 0 * NaN --> NaN 49 float prod = x * 0; 50 // At this point, prod will either be NaN or 0 51 // Therefore we can return (prod == prod) or (0 == prod). 52 return prod == prod; 53 } 54 55 /** SkIntToScalar(n) returns its integer argument as an SkScalar 56 */ 57 #define SkIntToScalar(n) ((float)(n)) 58 /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar 59 */ 60 #define SkFixedToScalar(x) SkFixedToFloat(x) 61 /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed 62 */ 63 #define SkScalarToFixed(x) SkFloatToFixed(x) 64 65 #define SkScalarToFloat(n) (n) 66 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED 67 #define SkFloatToScalar(n) (n) 68 #endif 69 70 #define SkScalarToDouble(n) (double)(n) 71 #define SkDoubleToScalar(n) (float)(n) 72 73 /** SkScalarFraction(x) returns the signed fractional part of the argument 74 */ 75 #define SkScalarFraction(x) sk_float_mod(x, 1.0f) 76 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) 80 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) 85 86 /** 87 * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using 88 * double, to avoid possibly losing the low bit(s) of the answer before calling floor(). 89 * 90 * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the 91 * extra precision is known to be valuable. 92 * 93 * In particular, this catches the following case: 94 * SkScalar x = 0.49999997; 95 * int ix = SkScalarRoundToInt(x); 96 * SkASSERT(0 == ix); // <--- fails 97 * ix = SkDScalarRoundToInt(x); 98 * SkASSERT(0 == ix); // <--- succeeds 99 */ 100 static inline int SkDScalarRoundToInt(SkScalar x) { 101 double xx = x; 102 xx += 0.5; 103 return (int)floor(xx); 104 } 105 106 /** Returns the absolute value of the specified SkScalar 107 */ 108 #define SkScalarAbs(x) sk_float_abs(x) 109 /** Return x with the sign of y 110 */ 111 #define SkScalarCopySign(x, y) sk_float_copysign(x, y) 112 /** Returns the value pinned between 0 and max inclusive 113 */ 114 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { 115 return x < 0 ? 0 : x > max ? max : x; 116 } 117 /** Returns the value pinned between min and max inclusive 118 */ 119 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { 120 return x < min ? min : x > max ? max : x; 121 } 122 /** Returns the specified SkScalar squared (x*x) 123 */ 124 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } 125 /** Returns the product of two SkScalars 126 */ 127 #define SkScalarMul(a, b) ((float)(a) * (b)) 128 /** Returns the product of two SkScalars plus a third SkScalar 129 */ 130 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) 131 /** Returns the quotient of two SkScalars (a/b) 132 */ 133 #define SkScalarDiv(a, b) ((float)(a) / (b)) 134 /** Returns the mod of two SkScalars (a mod b) 135 */ 136 #define SkScalarMod(x,y) sk_float_mod(x,y) 137 /** Returns the product of the first two arguments, divided by the third argument 138 */ 139 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) 140 /** Returns the multiplicative inverse of the SkScalar (1/x) 141 */ 142 #define SkScalarInvert(x) (SK_Scalar1 / (x)) 143 #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) 144 /** Returns the square root of the SkScalar 145 */ 146 #define SkScalarSqrt(x) sk_float_sqrt(x) 147 /** Returns b to the e 148 */ 149 #define SkScalarPow(b, e) sk_float_pow(b, e) 150 /** Returns the average of two SkScalars (a+b)/2 151 */ 152 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f) 153 /** Returns one half of the specified SkScalar 154 */ 155 #define SkScalarHalf(a) ((a) * 0.5f) 156 157 #define SK_ScalarSqrt2 1.41421356f 158 #define SK_ScalarPI 3.14159265f 159 #define SK_ScalarTanPIOver8 0.414213562f 160 #define SK_ScalarRoot2Over2 0.707106781f 161 162 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) 163 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI)) 164 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); 165 #define SkScalarSin(radians) (float)sk_float_sin(radians) 166 #define SkScalarCos(radians) (float)sk_float_cos(radians) 167 #define SkScalarTan(radians) (float)sk_float_tan(radians) 168 #define SkScalarASin(val) (float)sk_float_asin(val) 169 #define SkScalarACos(val) (float)sk_float_acos(val) 170 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) 171 #define SkScalarExp(x) (float)sk_float_exp(x) 172 #define SkScalarLog(x) (float)sk_float_log(x) 173 174 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } 175 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } 176 177 static inline bool SkScalarIsInt(SkScalar x) { 178 return x == (float)(int)x; 179 } 180 181 // DEPRECATED : use ToInt or ToScalar variant 182 #ifdef SK_SUPPORT_DEPRECATED_SCALARROUND 183 # define SkScalarFloor(x) SkScalarFloorToInt(x) 184 # define SkScalarCeil(x) SkScalarCeilToInt(x) 185 # define SkScalarRound(x) SkScalarRoundToInt(x) 186 #endif 187 188 /** 189 * Returns -1 || 0 || 1 depending on the sign of value: 190 * -1 if x < 0 191 * 0 if x == 0 192 * 1 if x > 0 193 */ 194 static inline int SkScalarSignAsInt(SkScalar x) { 195 return x < 0 ? -1 : (x > 0); 196 } 197 198 // Scalar result version of above 199 static inline SkScalar SkScalarSignAsScalar(SkScalar x) { 200 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0); 201 } 202 203 #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) 204 205 static inline bool SkScalarNearlyZero(SkScalar x, 206 SkScalar tolerance = SK_ScalarNearlyZero) { 207 SkASSERT(tolerance >= 0); 208 return SkScalarAbs(x) <= tolerance; 209 } 210 211 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y, 212 SkScalar tolerance = SK_ScalarNearlyZero) { 213 SkASSERT(tolerance >= 0); 214 return SkScalarAbs(x-y) <= tolerance; 215 } 216 217 /** Linearly interpolate between A and B, based on t. 218 If t is 0, return A 219 If t is 1, return B 220 else interpolate. 221 t must be [0..SK_Scalar1] 222 */ 223 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { 224 SkASSERT(t >= 0 && t <= SK_Scalar1); 225 return A + (B - A) * t; 226 } 227 228 /** Interpolate along the function described by (keys[length], values[length]) 229 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] 230 clamp to the min or max value. This function was inspired by a desire 231 to change the multiplier for thickness in fakeBold; therefore it assumes 232 the number of pairs (length) will be small, and a linear search is used. 233 Repeated keys are allowed for discontinuous functions (so long as keys is 234 monotonically increasing), and if key is the value of a repeated scalar in 235 keys, the first one will be used. However, that may change if a binary 236 search is used. 237 */ 238 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], 239 const SkScalar values[], int length); 240 241 /* 242 * Helper to compare an array of scalars. 243 */ 244 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { 245 SkASSERT(n >= 0); 246 for (int i = 0; i < n; ++i) { 247 if (a[i] != b[i]) { 248 return false; 249 } 250 } 251 return true; 252 } 253 254 #endif 255