1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/include/core/SkScalar.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,234 @@ 1.4 +/* 1.5 + * Copyright 2006 The Android Open Source Project 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef SkScalar_DEFINED 1.12 +#define SkScalar_DEFINED 1.13 + 1.14 +#include "SkFixed.h" 1.15 +#include "SkFloatingPoint.h" 1.16 + 1.17 +//#define SK_SUPPORT_DEPRECATED_SCALARROUND 1.18 + 1.19 +typedef float SkScalar; 1.20 + 1.21 +/** SK_Scalar1 is defined to be 1.0 represented as an SkScalar 1.22 +*/ 1.23 +#define SK_Scalar1 (1.0f) 1.24 +/** SK_Scalar1 is defined to be 1/2 represented as an SkScalar 1.25 +*/ 1.26 +#define SK_ScalarHalf (0.5f) 1.27 +/** SK_ScalarInfinity is defined to be infinity as an SkScalar 1.28 +*/ 1.29 +#define SK_ScalarInfinity SK_FloatInfinity 1.30 +/** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar 1.31 +*/ 1.32 +#define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity 1.33 +/** SK_ScalarMax is defined to be the largest value representable as an SkScalar 1.34 +*/ 1.35 +#define SK_ScalarMax (3.402823466e+38f) 1.36 +/** SK_ScalarMin is defined to be the smallest value representable as an SkScalar 1.37 +*/ 1.38 +#define SK_ScalarMin (-SK_ScalarMax) 1.39 +/** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar 1.40 +*/ 1.41 +#define SK_ScalarNaN SK_FloatNaN 1.42 +/** SkScalarIsNaN(n) returns true if argument is not a number 1.43 +*/ 1.44 +static inline bool SkScalarIsNaN(float x) { return x != x; } 1.45 + 1.46 +/** Returns true if x is not NaN and not infinite */ 1.47 +static inline bool SkScalarIsFinite(float x) { 1.48 + // We rely on the following behavior of infinities and nans 1.49 + // 0 * finite --> 0 1.50 + // 0 * infinity --> NaN 1.51 + // 0 * NaN --> NaN 1.52 + float prod = x * 0; 1.53 + // At this point, prod will either be NaN or 0 1.54 + // Therefore we can return (prod == prod) or (0 == prod). 1.55 + return prod == prod; 1.56 +} 1.57 + 1.58 +/** SkIntToScalar(n) returns its integer argument as an SkScalar 1.59 +*/ 1.60 +#define SkIntToScalar(n) ((float)(n)) 1.61 +/** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar 1.62 +*/ 1.63 +#define SkFixedToScalar(x) SkFixedToFloat(x) 1.64 +/** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed 1.65 +*/ 1.66 +#define SkScalarToFixed(x) SkFloatToFixed(x) 1.67 + 1.68 +#define SkScalarToFloat(n) (n) 1.69 +#ifndef SK_SCALAR_TO_FLOAT_EXCLUDED 1.70 +#define SkFloatToScalar(n) (n) 1.71 +#endif 1.72 + 1.73 +#define SkScalarToDouble(n) (double)(n) 1.74 +#define SkDoubleToScalar(n) (float)(n) 1.75 + 1.76 +/** SkScalarFraction(x) returns the signed fractional part of the argument 1.77 +*/ 1.78 +#define SkScalarFraction(x) sk_float_mod(x, 1.0f) 1.79 + 1.80 +#define SkScalarFloorToScalar(x) sk_float_floor(x) 1.81 +#define SkScalarCeilToScalar(x) sk_float_ceil(x) 1.82 +#define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) 1.83 + 1.84 +#define SkScalarFloorToInt(x) sk_float_floor2int(x) 1.85 +#define SkScalarCeilToInt(x) sk_float_ceil2int(x) 1.86 +#define SkScalarRoundToInt(x) sk_float_round2int(x) 1.87 +#define SkScalarTruncToInt(x) static_cast<int>(x) 1.88 + 1.89 +/** Returns the absolute value of the specified SkScalar 1.90 +*/ 1.91 +#define SkScalarAbs(x) sk_float_abs(x) 1.92 +/** Return x with the sign of y 1.93 + */ 1.94 +#define SkScalarCopySign(x, y) sk_float_copysign(x, y) 1.95 +/** Returns the value pinned between 0 and max inclusive 1.96 +*/ 1.97 +inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { 1.98 + return x < 0 ? 0 : x > max ? max : x; 1.99 +} 1.100 +/** Returns the value pinned between min and max inclusive 1.101 +*/ 1.102 +inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { 1.103 + return x < min ? min : x > max ? max : x; 1.104 +} 1.105 +/** Returns the specified SkScalar squared (x*x) 1.106 +*/ 1.107 +inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } 1.108 +/** Returns the product of two SkScalars 1.109 +*/ 1.110 +#define SkScalarMul(a, b) ((float)(a) * (b)) 1.111 +/** Returns the product of two SkScalars plus a third SkScalar 1.112 +*/ 1.113 +#define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) 1.114 +/** Returns the quotient of two SkScalars (a/b) 1.115 +*/ 1.116 +#define SkScalarDiv(a, b) ((float)(a) / (b)) 1.117 +/** Returns the mod of two SkScalars (a mod b) 1.118 +*/ 1.119 +#define SkScalarMod(x,y) sk_float_mod(x,y) 1.120 +/** Returns the product of the first two arguments, divided by the third argument 1.121 +*/ 1.122 +#define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) 1.123 +/** Returns the multiplicative inverse of the SkScalar (1/x) 1.124 +*/ 1.125 +#define SkScalarInvert(x) (SK_Scalar1 / (x)) 1.126 +#define SkScalarFastInvert(x) (SK_Scalar1 / (x)) 1.127 +/** Returns the square root of the SkScalar 1.128 +*/ 1.129 +#define SkScalarSqrt(x) sk_float_sqrt(x) 1.130 +/** Returns b to the e 1.131 +*/ 1.132 +#define SkScalarPow(b, e) sk_float_pow(b, e) 1.133 +/** Returns the average of two SkScalars (a+b)/2 1.134 +*/ 1.135 +#define SkScalarAve(a, b) (((a) + (b)) * 0.5f) 1.136 +/** Returns one half of the specified SkScalar 1.137 +*/ 1.138 +#define SkScalarHalf(a) ((a) * 0.5f) 1.139 + 1.140 +#define SK_ScalarSqrt2 1.41421356f 1.141 +#define SK_ScalarPI 3.14159265f 1.142 +#define SK_ScalarTanPIOver8 0.414213562f 1.143 +#define SK_ScalarRoot2Over2 0.707106781f 1.144 + 1.145 +#define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) 1.146 +#define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI)) 1.147 +float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); 1.148 +#define SkScalarSin(radians) (float)sk_float_sin(radians) 1.149 +#define SkScalarCos(radians) (float)sk_float_cos(radians) 1.150 +#define SkScalarTan(radians) (float)sk_float_tan(radians) 1.151 +#define SkScalarASin(val) (float)sk_float_asin(val) 1.152 +#define SkScalarACos(val) (float)sk_float_acos(val) 1.153 +#define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) 1.154 +#define SkScalarExp(x) (float)sk_float_exp(x) 1.155 +#define SkScalarLog(x) (float)sk_float_log(x) 1.156 + 1.157 +inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } 1.158 +inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } 1.159 + 1.160 +static inline bool SkScalarIsInt(SkScalar x) { 1.161 + return x == (float)(int)x; 1.162 +} 1.163 + 1.164 +// DEPRECATED : use ToInt or ToScalar variant 1.165 +#ifdef SK_SUPPORT_DEPRECATED_SCALARROUND 1.166 +# define SkScalarFloor(x) SkScalarFloorToInt(x) 1.167 +# define SkScalarCeil(x) SkScalarCeilToInt(x) 1.168 +# define SkScalarRound(x) SkScalarRoundToInt(x) 1.169 +#endif 1.170 + 1.171 +/** 1.172 + * Returns -1 || 0 || 1 depending on the sign of value: 1.173 + * -1 if x < 0 1.174 + * 0 if x == 0 1.175 + * 1 if x > 0 1.176 + */ 1.177 +static inline int SkScalarSignAsInt(SkScalar x) { 1.178 + return x < 0 ? -1 : (x > 0); 1.179 +} 1.180 + 1.181 +// Scalar result version of above 1.182 +static inline SkScalar SkScalarSignAsScalar(SkScalar x) { 1.183 + return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0); 1.184 +} 1.185 + 1.186 +#define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) 1.187 + 1.188 +static inline bool SkScalarNearlyZero(SkScalar x, 1.189 + SkScalar tolerance = SK_ScalarNearlyZero) { 1.190 + SkASSERT(tolerance >= 0); 1.191 + return SkScalarAbs(x) <= tolerance; 1.192 +} 1.193 + 1.194 +static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y, 1.195 + SkScalar tolerance = SK_ScalarNearlyZero) { 1.196 + SkASSERT(tolerance >= 0); 1.197 + return SkScalarAbs(x-y) <= tolerance; 1.198 +} 1.199 + 1.200 +/** Linearly interpolate between A and B, based on t. 1.201 + If t is 0, return A 1.202 + If t is 1, return B 1.203 + else interpolate. 1.204 + t must be [0..SK_Scalar1] 1.205 +*/ 1.206 +static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { 1.207 + SkASSERT(t >= 0 && t <= SK_Scalar1); 1.208 + return A + (B - A) * t; 1.209 +} 1.210 + 1.211 +/** Interpolate along the function described by (keys[length], values[length]) 1.212 + for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] 1.213 + clamp to the min or max value. This function was inspired by a desire 1.214 + to change the multiplier for thickness in fakeBold; therefore it assumes 1.215 + the number of pairs (length) will be small, and a linear search is used. 1.216 + Repeated keys are allowed for discontinuous functions (so long as keys is 1.217 + monotonically increasing), and if key is the value of a repeated scalar in 1.218 + keys, the first one will be used. However, that may change if a binary 1.219 + search is used. 1.220 +*/ 1.221 +SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], 1.222 + const SkScalar values[], int length); 1.223 + 1.224 +/* 1.225 + * Helper to compare an array of scalars. 1.226 + */ 1.227 +static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { 1.228 + SkASSERT(n >= 0); 1.229 + for (int i = 0; i < n; ++i) { 1.230 + if (a[i] != b[i]) { 1.231 + return false; 1.232 + } 1.233 + } 1.234 + return true; 1.235 +} 1.236 + 1.237 +#endif