reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkScalar_DEFINED |
| 9 | #define SkScalar_DEFINED |
| 10 | |
reed | 95dd177 | 2015-08-17 18:29:47 -0700 | [diff] [blame] | 11 | #include "../private/SkFloatingPoint.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
Leon Scroggins | 8233fc8 | 2017-02-02 13:12:23 +0000 | [diff] [blame] | 13 | #undef SK_SCALAR_IS_FLOAT |
| 14 | #define SK_SCALAR_IS_FLOAT 1 |
| 15 | |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 16 | typedef float SkScalar; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 18 | #define SK_Scalar1 1.0f |
| 19 | #define SK_ScalarHalf 0.5f |
| 20 | #define SK_ScalarSqrt2 1.41421356f |
| 21 | #define SK_ScalarPI 3.14159265f |
| 22 | #define SK_ScalarTanPIOver8 0.414213562f |
| 23 | #define SK_ScalarRoot2Over2 0.707106781f |
| 24 | #define SK_ScalarMax 3.402823466e+38f |
| 25 | #define SK_ScalarInfinity SK_FloatInfinity |
| 26 | #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity |
| 27 | #define SK_ScalarNaN SK_FloatNaN |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 28 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 29 | #define SkScalarFloorToScalar(x) sk_float_floor(x) |
| 30 | #define SkScalarCeilToScalar(x) sk_float_ceil(x) |
| 31 | #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) |
bungeman | dd67e3d | 2016-03-10 13:39:30 -0800 | [diff] [blame] | 32 | #define SkScalarTruncToScalar(x) sk_float_trunc(x) |
reed@google.com | 1b20280 | 2011-08-01 20:49:45 +0000 | [diff] [blame] | 33 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 34 | #define SkScalarFloorToInt(x) sk_float_floor2int(x) |
| 35 | #define SkScalarCeilToInt(x) sk_float_ceil2int(x) |
| 36 | #define SkScalarRoundToInt(x) sk_float_round2int(x) |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 37 | |
| 38 | #define SkScalarAbs(x) sk_float_abs(x) |
| 39 | #define SkScalarCopySign(x, y) sk_float_copysign(x, y) |
| 40 | #define SkScalarMod(x, y) sk_float_mod(x,y) |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 41 | #define SkScalarSqrt(x) sk_float_sqrt(x) |
| 42 | #define SkScalarPow(b, e) sk_float_pow(b, e) |
| 43 | |
| 44 | #define SkScalarSin(radians) (float)sk_float_sin(radians) |
| 45 | #define SkScalarCos(radians) (float)sk_float_cos(radians) |
| 46 | #define SkScalarTan(radians) (float)sk_float_tan(radians) |
| 47 | #define SkScalarASin(val) (float)sk_float_asin(val) |
| 48 | #define SkScalarACos(val) (float)sk_float_acos(val) |
| 49 | #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) |
| 50 | #define SkScalarExp(x) (float)sk_float_exp(x) |
| 51 | #define SkScalarLog(x) (float)sk_float_log(x) |
reed | 7729e56 | 2015-01-16 08:35:09 -0800 | [diff] [blame] | 52 | #define SkScalarLog2(x) (float)sk_float_log2(x) |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 53 | |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 54 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
| 55 | |
| 56 | #define SkIntToScalar(x) static_cast<SkScalar>(x) |
robertphillips | f054b17 | 2016-05-13 05:06:19 -0700 | [diff] [blame] | 57 | #define SkIntToFloat(x) static_cast<float>(x) |
Ben Wagner | 47a540f | 2017-09-18 13:20:32 -0400 | [diff] [blame] | 58 | #define SkScalarTruncToInt(x) sk_float_saturate2int(x) |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 59 | |
| 60 | #define SkScalarToFloat(x) static_cast<float>(x) |
| 61 | #define SkFloatToScalar(x) static_cast<SkScalar>(x) |
| 62 | #define SkScalarToDouble(x) static_cast<double>(x) |
| 63 | #define SkDoubleToScalar(x) static_cast<SkScalar>(x) |
| 64 | |
| 65 | #define SK_ScalarMin (-SK_ScalarMax) |
| 66 | |
| 67 | static inline bool SkScalarIsNaN(SkScalar x) { return x != x; } |
| 68 | |
| 69 | /** Returns true if x is not NaN and not infinite |
| 70 | */ |
| 71 | static inline bool SkScalarIsFinite(SkScalar x) { |
| 72 | // We rely on the following behavior of infinities and nans |
| 73 | // 0 * finite --> 0 |
| 74 | // 0 * infinity --> NaN |
| 75 | // 0 * NaN --> NaN |
| 76 | SkScalar prod = x * 0; |
| 77 | // At this point, prod will either be NaN or 0 |
reed | 454fa71 | 2015-02-10 08:46:22 -0800 | [diff] [blame] | 78 | return !SkScalarIsNaN(prod); |
| 79 | } |
| 80 | |
| 81 | static inline bool SkScalarsAreFinite(SkScalar a, SkScalar b) { |
| 82 | SkScalar prod = 0; |
| 83 | prod *= a; |
| 84 | prod *= b; |
| 85 | // At this point, prod will either be NaN or 0 |
| 86 | return !SkScalarIsNaN(prod); |
| 87 | } |
| 88 | |
| 89 | static inline bool SkScalarsAreFinite(const SkScalar array[], int count) { |
| 90 | SkScalar prod = 0; |
| 91 | for (int i = 0; i < count; ++i) { |
| 92 | prod *= array[i]; |
| 93 | } |
| 94 | // At this point, prod will either be NaN or 0 |
ehsan.akhgari | 6f90475 | 2014-12-15 12:08:47 -0800 | [diff] [blame] | 95 | return !SkScalarIsNaN(prod); |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 96 | } |
reed@google.com | 1b20280 | 2011-08-01 20:49:45 +0000 | [diff] [blame] | 97 | |
commit-bot@chromium.org | 4e332f8 | 2014-05-05 16:04:42 +0000 | [diff] [blame] | 98 | /** |
| 99 | * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using |
| 100 | * double, to avoid possibly losing the low bit(s) of the answer before calling floor(). |
| 101 | * |
| 102 | * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the |
| 103 | * extra precision is known to be valuable. |
| 104 | * |
| 105 | * In particular, this catches the following case: |
| 106 | * SkScalar x = 0.49999997; |
| 107 | * int ix = SkScalarRoundToInt(x); |
| 108 | * SkASSERT(0 == ix); // <--- fails |
| 109 | * ix = SkDScalarRoundToInt(x); |
| 110 | * SkASSERT(0 == ix); // <--- succeeds |
| 111 | */ |
| 112 | static inline int SkDScalarRoundToInt(SkScalar x) { |
| 113 | double xx = x; |
| 114 | xx += 0.5; |
| 115 | return (int)floor(xx); |
| 116 | } |
| 117 | |
bungeman | dd67e3d | 2016-03-10 13:39:30 -0800 | [diff] [blame] | 118 | /** Returns the fractional part of the scalar. */ |
| 119 | static inline SkScalar SkScalarFraction(SkScalar x) { |
| 120 | return x - SkScalarTruncToScalar(x); |
| 121 | } |
| 122 | |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 123 | static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { |
bungeman | 167eb17 | 2015-03-09 13:40:15 -0700 | [diff] [blame] | 124 | x = SkTMin(x, max); |
| 125 | x = SkTMax<SkScalar>(x, 0); |
| 126 | return x; |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 127 | } |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 128 | |
| 129 | static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { |
bungeman | fd0ecf4 | 2015-04-16 12:18:28 -0700 | [diff] [blame] | 130 | return SkTPin(x, min, max); |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 131 | } |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 132 | |
| 133 | SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue); |
| 134 | |
| 135 | static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } |
| 136 | |
Mike Reed | faffa86 | 2018-02-12 11:46:19 -0500 | [diff] [blame] | 137 | #define SkScalarDiv(numer, denom) sk_ieee_float_divide(numer, denom) |
| 138 | #define SkScalarInvert(x) sk_ieee_float_divide(SK_Scalar1, (x)) |
| 139 | #define SkScalarFastInvert(x) sk_ieee_float_divide(SK_Scalar1, (x)) |
| 140 | #define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf) |
| 141 | #define SkScalarHalf(a) ((a) * SK_ScalarHalf) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 142 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 143 | #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) |
commit-bot@chromium.org | c8d7328 | 2014-01-06 18:17:24 +0000 | [diff] [blame] | 144 | #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 145 | |
reed | bd16059 | 2014-10-31 06:55:41 -0700 | [diff] [blame] | 146 | static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } |
| 147 | static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 149 | static inline bool SkScalarIsInt(SkScalar x) { |
Brian Salomon | 3416969 | 2017-08-28 15:32:01 -0400 | [diff] [blame] | 150 | return x == SkScalarFloorToScalar(x); |
reed@google.com | 8f4d230 | 2013-12-17 16:44:46 +0000 | [diff] [blame] | 151 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 152 | |
bsalomon@google.com | 647a804 | 2011-08-23 14:39:01 +0000 | [diff] [blame] | 153 | /** |
| 154 | * Returns -1 || 0 || 1 depending on the sign of value: |
| 155 | * -1 if x < 0 |
| 156 | * 0 if x == 0 |
| 157 | * 1 if x > 0 |
| 158 | */ |
| 159 | static inline int SkScalarSignAsInt(SkScalar x) { |
| 160 | return x < 0 ? -1 : (x > 0); |
| 161 | } |
| 162 | |
| 163 | // Scalar result version of above |
| 164 | static inline SkScalar SkScalarSignAsScalar(SkScalar x) { |
| 165 | return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0); |
| 166 | } |
reed@google.com | 1b20280 | 2011-08-01 20:49:45 +0000 | [diff] [blame] | 167 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 168 | #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) |
| 169 | |
reed@android.com | 187d559 | 2009-07-08 14:03:56 +0000 | [diff] [blame] | 170 | static inline bool SkScalarNearlyZero(SkScalar x, |
robertphillips | 64b0f5f | 2016-01-25 14:19:56 -0800 | [diff] [blame] | 171 | SkScalar tolerance = SK_ScalarNearlyZero) { |
reed@google.com | 9b5ca29 | 2012-04-12 12:51:32 +0000 | [diff] [blame] | 172 | SkASSERT(tolerance >= 0); |
| 173 | return SkScalarAbs(x) <= tolerance; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 174 | } |
| 175 | |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 176 | static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y, |
robertphillips | 30c4cae | 2015-09-15 10:20:55 -0700 | [diff] [blame] | 177 | SkScalar tolerance = SK_ScalarNearlyZero) { |
reed@google.com | 9b5ca29 | 2012-04-12 12:51:32 +0000 | [diff] [blame] | 178 | SkASSERT(tolerance >= 0); |
| 179 | return SkScalarAbs(x-y) <= tolerance; |
epoger@google.com | 1fd56dc | 2011-06-15 18:04:58 +0000 | [diff] [blame] | 180 | } |
| 181 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 182 | /** Linearly interpolate between A and B, based on t. |
| 183 | If t is 0, return A |
| 184 | If t is 1, return B |
| 185 | else interpolate. |
| 186 | t must be [0..SK_Scalar1] |
| 187 | */ |
reed@android.com | 187d559 | 2009-07-08 14:03:56 +0000 | [diff] [blame] | 188 | static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | SkASSERT(t >= 0 && t <= SK_Scalar1); |
reed@google.com | 1a5e51f | 2014-01-27 13:41:02 +0000 | [diff] [blame] | 190 | return A + (B - A) * t; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 191 | } |
| 192 | |
vandebo@chromium.org | 28be72b | 2010-11-11 21:37:00 +0000 | [diff] [blame] | 193 | /** Interpolate along the function described by (keys[length], values[length]) |
| 194 | for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] |
| 195 | clamp to the min or max value. This function was inspired by a desire |
| 196 | to change the multiplier for thickness in fakeBold; therefore it assumes |
| 197 | the number of pairs (length) will be small, and a linear search is used. |
| 198 | Repeated keys are allowed for discontinuous functions (so long as keys is |
| 199 | monotonically increasing), and if key is the value of a repeated scalar in |
| 200 | keys, the first one will be used. However, that may change if a binary |
| 201 | search is used. |
| 202 | */ |
| 203 | SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], |
| 204 | const SkScalar values[], int length); |
| 205 | |
reed@google.com | e1e7d7a | 2012-10-31 19:59:23 +0000 | [diff] [blame] | 206 | /* |
| 207 | * Helper to compare an array of scalars. |
| 208 | */ |
| 209 | static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { |
reed@google.com | e1e7d7a | 2012-10-31 19:59:23 +0000 | [diff] [blame] | 210 | SkASSERT(n >= 0); |
| 211 | for (int i = 0; i < n; ++i) { |
| 212 | if (a[i] != b[i]) { |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | return true; |
reed@google.com | e1e7d7a | 2012-10-31 19:59:23 +0000 | [diff] [blame] | 217 | } |
| 218 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 219 | #endif |