SkScalar.h revision 4ecd42e4bf41ef5ad92bfd67bdcf365c8c6d2864
1 2/* 3 * Copyright 2006 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10#ifndef SkScalar_DEFINED 11#define SkScalar_DEFINED 12 13#include "SkFixed.h" 14#include "SkFloatingPoint.h" 15 16/** \file SkScalar.h 17 18 Types and macros for the data type SkScalar. This is the fractional numeric type 19 that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemented 20 either as an IEEE float, or as a 16.16 SkFixed. The macros in this file are written 21 to allow the calling code to manipulate SkScalar values without knowing which representation 22 is in effect. 23*/ 24 25#ifdef SK_SCALAR_IS_FLOAT 26 27 /** SkScalar is our type for fractional values and coordinates. Depending on 28 compile configurations, it is either represented as an IEEE float, or 29 as a 16.16 fixed point integer. 30 */ 31 typedef float SkScalar; 32 33 /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar 34 */ 35 #define SK_Scalar1 (1.0f) 36 /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar 37 */ 38 #define SK_ScalarHalf (0.5f) 39 /** SK_ScalarInfinity is defined to be infinity as an SkScalar 40 */ 41 #define SK_ScalarInfinity SK_FloatInfinity 42 /** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar 43 */ 44 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity 45 /** SK_ScalarMax is defined to be the largest value representable as an SkScalar 46 */ 47 #define SK_ScalarMax (3.402823466e+38f) 48 /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar 49 */ 50 #define SK_ScalarMin (-SK_ScalarMax) 51 /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar 52 */ 53 #define SK_ScalarNaN SK_FloatNaN 54 /** SkScalarIsNaN(n) returns true if argument is not a number 55 */ 56 static inline bool SkScalarIsNaN(float x) { return x != x; } 57 58 /** Returns true if x is not NaN and not infinite */ 59 static inline bool SkScalarIsFinite(float x) { 60 // We rely on the following behavior of infinities and nans 61 // 0 * finite --> 0 62 // 0 * infinity --> NaN 63 // 0 * NaN --> NaN 64 float prod = x * 0; 65 // At this point, prod will either be NaN or 0 66 // Therefore we can return (prod == prod) or (0 == prod). 67 return prod == prod; 68 } 69 70#ifdef SK_DEBUG 71 /** SkIntToScalar(n) returns its integer argument as an SkScalar 72 * 73 * If we're compiling in DEBUG mode, and can thus afford some extra runtime 74 * cycles, check to make sure that the parameter passed in has not already 75 * been converted to SkScalar. (A double conversion like this is harmless 76 * for SK_SCALAR_IS_FLOAT, but for SK_SCALAR_IS_FIXED this causes trouble.) 77 * 78 * Note that we need all of these method signatures to properly handle the 79 * various types that we pass into SkIntToScalar() to date: 80 * int, size_t, U8CPU, etc., even though what we really mean is "anything 81 * but a float". 82 */ 83 static inline float SkIntToScalar(signed int param) { 84 return (float)param; 85 } 86 static inline float SkIntToScalar(unsigned int param) { 87 return (float)param; 88 } 89 static inline float SkIntToScalar(signed long param) { 90 return (float)param; 91 } 92 static inline float SkIntToScalar(unsigned long param) { 93 return (float)param; 94 } 95 static inline float SkIntToScalar(float /* param */) { 96 /* If the parameter passed into SkIntToScalar is a float, 97 * one of two things has happened: 98 * 1. the parameter was an SkScalar (which is typedef'd to float) 99 * 2. the parameter was a float instead of an int 100 * 101 * Either way, it's not good. 102 */ 103 SkDEBUGFAIL("looks like you passed an SkScalar into SkIntToScalar"); 104 return (float)0; 105 } 106#else // not SK_DEBUG 107 /** SkIntToScalar(n) returns its integer argument as an SkScalar 108 */ 109 #define SkIntToScalar(n) ((float)(n)) 110#endif // not SK_DEBUG 111 /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar 112 */ 113 #define SkFixedToScalar(x) SkFixedToFloat(x) 114 /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed 115 */ 116 #define SkScalarToFixed(x) SkFloatToFixed(x) 117 118 #define SkScalarToFloat(n) (n) 119 #define SkFloatToScalar(n) (n) 120 121 #define SkScalarToDouble(n) (double)(n) 122 #define SkDoubleToScalar(n) (float)(n) 123 124 /** SkScalarFraction(x) returns the signed fractional part of the argument 125 */ 126 #define SkScalarFraction(x) sk_float_mod(x, 1.0f) 127 128 #define SkScalarFloorToScalar(x) sk_float_floor(x) 129 #define SkScalarCeilToScalar(x) sk_float_ceil(x) 130 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) 131 132 #define SkScalarFloorToInt(x) sk_float_floor2int(x) 133 #define SkScalarCeilToInt(x) sk_float_ceil2int(x) 134 #define SkScalarRoundToInt(x) sk_float_round2int(x) 135 #define SkScalarTruncToInt(x) static_cast<int>(x) 136 137 /** Returns the absolute value of the specified SkScalar 138 */ 139 #define SkScalarAbs(x) sk_float_abs(x) 140 /** Return x with the sign of y 141 */ 142 #define SkScalarCopySign(x, y) sk_float_copysign(x, y) 143 /** Returns the value pinned between 0 and max inclusive 144 */ 145 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { 146 return x < 0 ? 0 : x > max ? max : x; 147 } 148 /** Returns the value pinned between min and max inclusive 149 */ 150 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { 151 return x < min ? min : x > max ? max : x; 152 } 153 /** Returns the specified SkScalar squared (x*x) 154 */ 155 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } 156 /** Returns the product of two SkScalars 157 */ 158 #define SkScalarMul(a, b) ((float)(a) * (b)) 159 /** Returns the product of two SkScalars plus a third SkScalar 160 */ 161 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) 162 /** Returns the product of a SkScalar and an int rounded to the nearest integer value 163 */ 164 #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b)) 165 /** Returns the product of a SkScalar and an int promoted to the next larger int 166 */ 167 #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b)) 168 /** Returns the product of a SkScalar and an int truncated to the next smaller int 169 */ 170 #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b)) 171 /** Returns the quotient of two SkScalars (a/b) 172 */ 173 #define SkScalarDiv(a, b) ((float)(a) / (b)) 174 /** Returns the mod of two SkScalars (a mod b) 175 */ 176 #define SkScalarMod(x,y) sk_float_mod(x,y) 177 /** Returns the product of the first two arguments, divided by the third argument 178 */ 179 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) 180 /** Returns the multiplicative inverse of the SkScalar (1/x) 181 */ 182 #define SkScalarInvert(x) (SK_Scalar1 / (x)) 183 #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) 184 /** Returns the square root of the SkScalar 185 */ 186 #define SkScalarSqrt(x) sk_float_sqrt(x) 187 /** Returns b to the e 188 */ 189 #define SkScalarPow(b, e) sk_float_pow(b, e) 190 /** Returns the average of two SkScalars (a+b)/2 191 */ 192 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f) 193 /** Returns the geometric mean of two SkScalars 194 */ 195 #define SkScalarMean(a, b) sk_float_sqrt((float)(a) * (b)) 196 /** Returns one half of the specified SkScalar 197 */ 198 #define SkScalarHalf(a) ((a) * 0.5f) 199 200 #define SK_ScalarSqrt2 1.41421356f 201 #define SK_ScalarPI 3.14159265f 202 #define SK_ScalarTanPIOver8 0.414213562f 203 #define SK_ScalarRoot2Over2 0.707106781f 204 205 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) 206 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); 207 #define SkScalarSin(radians) (float)sk_float_sin(radians) 208 #define SkScalarCos(radians) (float)sk_float_cos(radians) 209 #define SkScalarTan(radians) (float)sk_float_tan(radians) 210 #define SkScalarASin(val) (float)sk_float_asin(val) 211 #define SkScalarACos(val) (float)sk_float_acos(val) 212 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) 213 #define SkScalarExp(x) (float)sk_float_exp(x) 214 #define SkScalarLog(x) (float)sk_float_log(x) 215 216 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } 217 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } 218 219 static inline bool SkScalarIsInt(SkScalar x) { 220 return x == (float)(int)x; 221 } 222#else 223 typedef SkFixed SkScalar; 224 225 #define SK_Scalar1 SK_Fixed1 226 #define SK_ScalarHalf SK_FixedHalf 227 #define SK_ScalarInfinity SK_FixedMax 228 #define SK_ScalarNegativeInfinity SK_FixedMin 229 #define SK_ScalarMax SK_FixedMax 230 #define SK_ScalarMin SK_FixedMin 231 #define SK_ScalarNaN SK_FixedNaN 232 #define SkScalarIsNaN(x) ((x) == SK_FixedNaN) 233 #define SkScalarIsFinite(x) ((x) != SK_FixedNaN) 234 235 #define SkIntToScalar(n) SkIntToFixed(n) 236 #define SkFixedToScalar(x) (x) 237 #define SkScalarToFixed(x) (x) 238 #define SkScalarToFloat(n) SkFixedToFloat(n) 239 #define SkFloatToScalar(n) SkFloatToFixed(n) 240 241 #define SkScalarToDouble(n) SkFixedToDouble(n) 242 #define SkDoubleToScalar(n) SkDoubleToFixed(n) 243 #define SkScalarFraction(x) SkFixedFraction(x) 244 245 #define SkScalarFloorToScalar(x) SkFixedFloorToFixed(x) 246 #define SkScalarCeilToScalar(x) SkFixedCeilToFixed(x) 247 #define SkScalarRoundToScalar(x) SkFixedRoundToFixed(x) 248 249 #define SkScalarFloorToInt(x) SkFixedFloorToInt(x) 250 #define SkScalarCeilToInt(x) SkFixedCeilToInt(x) 251 #define SkScalarRoundToInt(x) SkFixedRoundToInt(x) 252 #define SkScalarTruncToInt(x) (((x) < 0) ? SkScalarCeilToInt(x) : SkScalarFloorToInt(x)) 253 254 #define SkScalarAbs(x) SkFixedAbs(x) 255 #define SkScalarCopySign(x, y) SkCopySign32(x, y) 256 #define SkScalarClampMax(x, max) SkClampMax(x, max) 257 #define SkScalarPin(x, min, max) SkPin32(x, min, max) 258 #define SkScalarSquare(x) SkFixedSquare(x) 259 #define SkScalarMul(a, b) SkFixedMul(a, b) 260 #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c) 261 #define SkScalarMulRound(a, b) SkFixedMulCommon(a, b, SK_FixedHalf) 262 #define SkScalarMulCeil(a, b) SkFixedMulCommon(a, b, SK_Fixed1 - 1) 263 #define SkScalarMulFloor(a, b) SkFixedMulCommon(a, b, 0) 264 #define SkScalarDiv(a, b) SkFixedDiv(a, b) 265 #define SkScalarMod(a, b) SkFixedMod(a, b) 266 #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c) 267 #define SkScalarInvert(x) SkFixedInvert(x) 268 #define SkScalarFastInvert(x) SkFixedFastInvert(x) 269 #define SkScalarSqrt(x) SkFixedSqrt(x) 270 #define SkScalarAve(a, b) SkFixedAve(a, b) 271 #define SkScalarMean(a, b) SkFixedMean(a, b) 272 #define SkScalarHalf(a) ((a) >> 1) 273 274 #define SK_ScalarSqrt2 SK_FixedSqrt2 275 #define SK_ScalarPI SK_FixedPI 276 #define SK_ScalarTanPIOver8 SK_FixedTanPIOver8 277 #define SK_ScalarRoot2Over2 SK_FixedRoot2Over2 278 279 #define SkDegreesToRadians(degrees) SkFractMul(degrees, SK_FractPIOver180) 280 #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr) 281 #define SkScalarSin(radians) SkFixedSin(radians) 282 #define SkScalarCos(radians) SkFixedCos(radians) 283 #define SkScalarTan(val) SkFixedTan(val) 284 #define SkScalarASin(val) SkFixedASin(val) 285 #define SkScalarACos(val) SkFixedACos(val) 286 #define SkScalarATan2(y, x) SkFixedATan2(y,x) 287 #define SkScalarExp(x) SkFixedExp(x) 288 #define SkScalarLog(x) SkFixedLog(x) 289 290 #define SkMaxScalar(a, b) SkMax32(a, b) 291 #define SkMinScalar(a, b) SkMin32(a, b) 292 293 static inline bool SkScalarIsInt(SkFixed x) { 294 return 0 == (x & 0xffff); 295 } 296#endif 297 298// DEPRECATED : use ToInt or ToScalar variant 299#define SkScalarFloor(x) SkScalarFloorToInt(x) 300#define SkScalarCeil(x) SkScalarCeilToInt(x) 301#define SkScalarRound(x) SkScalarRoundToInt(x) 302 303/** 304 * Returns -1 || 0 || 1 depending on the sign of value: 305 * -1 if x < 0 306 * 0 if x == 0 307 * 1 if x > 0 308 */ 309static inline int SkScalarSignAsInt(SkScalar x) { 310 return x < 0 ? -1 : (x > 0); 311} 312 313// Scalar result version of above 314static inline SkScalar SkScalarSignAsScalar(SkScalar x) { 315 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0); 316} 317 318#define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) 319 320static inline bool SkScalarNearlyZero(SkScalar x, 321 SkScalar tolerance = SK_ScalarNearlyZero) { 322 SkASSERT(tolerance >= 0); 323 return SkScalarAbs(x) <= tolerance; 324} 325 326static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y, 327 SkScalar tolerance = SK_ScalarNearlyZero) { 328 SkASSERT(tolerance >= 0); 329 return SkScalarAbs(x-y) <= tolerance; 330} 331 332/** Linearly interpolate between A and B, based on t. 333 If t is 0, return A 334 If t is 1, return B 335 else interpolate. 336 t must be [0..SK_Scalar1] 337*/ 338static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { 339 SkASSERT(t >= 0 && t <= SK_Scalar1); 340 return A + SkScalarMul(B - A, t); 341} 342 343static inline SkScalar SkScalarLog2(SkScalar x) { 344 static const SkScalar log2_conversion_factor = SkScalarDiv(1, SkScalarLog(2)); 345 346 return SkScalarMul(SkScalarLog(x), log2_conversion_factor); 347} 348 349/** Interpolate along the function described by (keys[length], values[length]) 350 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] 351 clamp to the min or max value. This function was inspired by a desire 352 to change the multiplier for thickness in fakeBold; therefore it assumes 353 the number of pairs (length) will be small, and a linear search is used. 354 Repeated keys are allowed for discontinuous functions (so long as keys is 355 monotonically increasing), and if key is the value of a repeated scalar in 356 keys, the first one will be used. However, that may change if a binary 357 search is used. 358*/ 359SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], 360 const SkScalar values[], int length); 361 362/* 363 * Helper to compare an array of scalars. 364 */ 365static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { 366#ifdef SK_SCALAR_IS_FLOAT 367 SkASSERT(n >= 0); 368 for (int i = 0; i < n; ++i) { 369 if (a[i] != b[i]) { 370 return false; 371 } 372 } 373 return true; 374#else 375 return 0 == memcmp(a, b, n * sizeof(SkScalar)); 376#endif 377} 378 379#endif 380