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