SkScalar.h revision e15b2f5296a65c92be477a71ddf9eae9d95eddce
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 16typedef 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*/ 41static inline bool SkScalarIsNaN(float x) { return x != x; } 42 43/** Returns true if x is not NaN and not infinite */ 44static 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/** Returns the absolute value of the specified SkScalar 87*/ 88#define SkScalarAbs(x) sk_float_abs(x) 89/** Return x with the sign of y 90 */ 91#define SkScalarCopySign(x, y) sk_float_copysign(x, y) 92/** Returns the value pinned between 0 and max inclusive 93*/ 94inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { 95 return x < 0 ? 0 : x > max ? max : x; 96} 97/** Returns the value pinned between min and max inclusive 98*/ 99inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { 100 return x < min ? min : x > max ? max : x; 101} 102/** Returns the specified SkScalar squared (x*x) 103*/ 104inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } 105/** Returns the product of two SkScalars 106*/ 107#define SkScalarMul(a, b) ((float)(a) * (b)) 108/** Returns the product of two SkScalars plus a third SkScalar 109*/ 110#define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) 111/** Returns the product of a SkScalar and an int rounded to the nearest integer value 112*/ 113#define SkScalarMulRound(a, b) SkScalarRoundToInt((float)(a) * (b)) 114/** Returns the product of a SkScalar and an int promoted to the next larger int 115*/ 116#define SkScalarMulCeil(a, b) SkScalarCeilToInt((float)(a) * (b)) 117/** Returns the product of a SkScalar and an int truncated to the next smaller int 118*/ 119#define SkScalarMulFloor(a, b) SkScalarFloorToInt((float)(a) * (b)) 120/** Returns the quotient of two SkScalars (a/b) 121*/ 122#define SkScalarDiv(a, b) ((float)(a) / (b)) 123/** Returns the mod of two SkScalars (a mod b) 124*/ 125#define SkScalarMod(x,y) sk_float_mod(x,y) 126/** Returns the product of the first two arguments, divided by the third argument 127*/ 128#define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) 129/** Returns the multiplicative inverse of the SkScalar (1/x) 130*/ 131#define SkScalarInvert(x) (SK_Scalar1 / (x)) 132#define SkScalarFastInvert(x) (SK_Scalar1 / (x)) 133/** Returns the square root of the SkScalar 134*/ 135#define SkScalarSqrt(x) sk_float_sqrt(x) 136/** Returns b to the e 137*/ 138#define SkScalarPow(b, e) sk_float_pow(b, e) 139/** Returns the average of two SkScalars (a+b)/2 140*/ 141#define SkScalarAve(a, b) (((a) + (b)) * 0.5f) 142/** Returns the geometric mean of two SkScalars 143*/ 144#define SkScalarMean(a, b) sk_float_sqrt((float)(a) * (b)) 145/** Returns one half of the specified SkScalar 146*/ 147#define SkScalarHalf(a) ((a) * 0.5f) 148 149#define SK_ScalarSqrt2 1.41421356f 150#define SK_ScalarPI 3.14159265f 151#define SK_ScalarTanPIOver8 0.414213562f 152#define SK_ScalarRoot2Over2 0.707106781f 153 154#define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) 155float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); 156#define SkScalarSin(radians) (float)sk_float_sin(radians) 157#define SkScalarCos(radians) (float)sk_float_cos(radians) 158#define SkScalarTan(radians) (float)sk_float_tan(radians) 159#define SkScalarASin(val) (float)sk_float_asin(val) 160#define SkScalarACos(val) (float)sk_float_acos(val) 161#define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) 162#define SkScalarExp(x) (float)sk_float_exp(x) 163#define SkScalarLog(x) (float)sk_float_log(x) 164 165inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } 166inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } 167 168static inline bool SkScalarIsInt(SkScalar x) { 169 return x == (float)(int)x; 170} 171 172// DEPRECATED : use ToInt or ToScalar variant 173#ifdef SK_SUPPORT_DEPRECATED_SCALARROUND 174# define SkScalarFloor(x) SkScalarFloorToInt(x) 175# define SkScalarCeil(x) SkScalarCeilToInt(x) 176# define SkScalarRound(x) SkScalarRoundToInt(x) 177#endif 178 179/** 180 * Returns -1 || 0 || 1 depending on the sign of value: 181 * -1 if x < 0 182 * 0 if x == 0 183 * 1 if x > 0 184 */ 185static inline int SkScalarSignAsInt(SkScalar x) { 186 return x < 0 ? -1 : (x > 0); 187} 188 189// Scalar result version of above 190static inline SkScalar SkScalarSignAsScalar(SkScalar x) { 191 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0); 192} 193 194#define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) 195 196static inline bool SkScalarNearlyZero(SkScalar x, 197 SkScalar tolerance = SK_ScalarNearlyZero) { 198 SkASSERT(tolerance >= 0); 199 return SkScalarAbs(x) <= tolerance; 200} 201 202static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y, 203 SkScalar tolerance = SK_ScalarNearlyZero) { 204 SkASSERT(tolerance >= 0); 205 return SkScalarAbs(x-y) <= tolerance; 206} 207 208/** Linearly interpolate between A and B, based on t. 209 If t is 0, return A 210 If t is 1, return B 211 else interpolate. 212 t must be [0..SK_Scalar1] 213*/ 214static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { 215 SkASSERT(t >= 0 && t <= SK_Scalar1); 216 return A + SkScalarMul(B - A, t); 217} 218 219/** Interpolate along the function described by (keys[length], values[length]) 220 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] 221 clamp to the min or max value. This function was inspired by a desire 222 to change the multiplier for thickness in fakeBold; therefore it assumes 223 the number of pairs (length) will be small, and a linear search is used. 224 Repeated keys are allowed for discontinuous functions (so long as keys is 225 monotonically increasing), and if key is the value of a repeated scalar in 226 keys, the first one will be used. However, that may change if a binary 227 search is used. 228*/ 229SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], 230 const SkScalar values[], int length); 231 232/* 233 * Helper to compare an array of scalars. 234 */ 235static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { 236 SkASSERT(n >= 0); 237 for (int i = 0; i < n; ++i) { 238 if (a[i] != b[i]) { 239 return false; 240 } 241 } 242 return true; 243} 244 245#endif 246