OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2006 The Android Open Source Project | 2 * Copyright 2006 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkScalar_DEFINED | 8 #ifndef SkScalar_DEFINED |
9 #define SkScalar_DEFINED | 9 #define SkScalar_DEFINED |
10 | 10 |
11 #include "SkFixed.h" | 11 #include "SkFixed.h" |
12 #include "SkFloatingPoint.h" | 12 #include "SkFloatingPoint.h" |
13 | 13 |
14 //#define SK_SUPPORT_DEPRECATED_SCALARROUND | 14 //#define SK_SUPPORT_DEPRECATED_SCALARROUND |
15 | 15 |
16 typedef float SkScalar; | 16 // TODO: move this sort of check into SkPostConfig.h |
| 17 #define SK_SCALAR_IS_DOUBLE 0 |
| 18 #undef SK_SCALAR_IS_FLOAT |
| 19 #define SK_SCALAR_IS_FLOAT 1 |
17 | 20 |
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 SkScala
r | |
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 */ | |
41 static inline bool SkScalarIsNaN(float x) { return x != x; } | |
42 | 21 |
43 /** Returns true if x is not NaN and not infinite */ | 22 #if SK_SCALAR_IS_FLOAT |
44 static 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 | 23 |
55 /** SkIntToScalar(n) returns its integer argument as an SkScalar | 24 typedef float 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 | 25 |
65 #define SkScalarToFloat(n) (n) | 26 #define SK_Scalar1 1.0f |
66 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED | 27 #define SK_ScalarHalf 0.5f |
67 #define SkFloatToScalar(n) (n) | 28 #define SK_ScalarSqrt2 1.41421356f |
68 #endif | 29 #define SK_ScalarPI 3.14159265f |
| 30 #define SK_ScalarTanPIOver8 0.414213562f |
| 31 #define SK_ScalarRoot2Over2 0.707106781f |
| 32 #define SK_ScalarMax 3.402823466e+38f |
| 33 #define SK_ScalarInfinity SK_FloatInfinity |
| 34 #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity |
| 35 #define SK_ScalarNaN SK_FloatNaN |
69 | 36 |
70 #define SkScalarToDouble(n) (double)(n) | 37 #define SkFixedToScalar(x) SkFixedToFloat(x) |
71 #define SkDoubleToScalar(n) (float)(n) | 38 #define SkScalarToFixed(x) SkFloatToFixed(x) |
72 | |
73 /** SkScalarFraction(x) returns the signed fractional part of the argument | |
74 */ | |
75 #define SkScalarFraction(x) sk_float_mod(x, 1.0f) | |
76 | 39 |
77 #define SkScalarFloorToScalar(x) sk_float_floor(x) | 40 #define SkScalarFloorToScalar(x) sk_float_floor(x) |
78 #define SkScalarCeilToScalar(x) sk_float_ceil(x) | 41 #define SkScalarCeilToScalar(x) sk_float_ceil(x) |
79 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) | 42 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) |
80 | 43 |
81 #define SkScalarFloorToInt(x) sk_float_floor2int(x) | 44 #define SkScalarFloorToInt(x) sk_float_floor2int(x) |
82 #define SkScalarCeilToInt(x) sk_float_ceil2int(x) | 45 #define SkScalarCeilToInt(x) sk_float_ceil2int(x) |
83 #define SkScalarRoundToInt(x) sk_float_round2int(x) | 46 #define SkScalarRoundToInt(x) sk_float_round2int(x) |
84 #define SkScalarTruncToInt(x) static_cast<int>(x) | 47 |
| 48 #define SkScalarAbs(x) sk_float_abs(x) |
| 49 #define SkScalarCopySign(x, y) sk_float_copysign(x, y) |
| 50 #define SkScalarMod(x, y) sk_float_mod(x,y) |
| 51 #define SkScalarFraction(x) sk_float_mod(x, 1.0f) |
| 52 #define SkScalarSqrt(x) sk_float_sqrt(x) |
| 53 #define SkScalarPow(b, e) sk_float_pow(b, e) |
| 54 |
| 55 #define SkScalarSin(radians) (float)sk_float_sin(radians) |
| 56 #define SkScalarCos(radians) (float)sk_float_cos(radians) |
| 57 #define SkScalarTan(radians) (float)sk_float_tan(radians) |
| 58 #define SkScalarASin(val) (float)sk_float_asin(val) |
| 59 #define SkScalarACos(val) (float)sk_float_acos(val) |
| 60 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) |
| 61 #define SkScalarExp(x) (float)sk_float_exp(x) |
| 62 #define SkScalarLog(x) (float)sk_float_log(x) |
| 63 |
| 64 #else // SK_SCALAR_IS_DOUBLE |
| 65 |
| 66 typedef double SkScalar; |
| 67 |
| 68 #define SK_Scalar1 1.0 |
| 69 #define SK_ScalarHalf 0.5 |
| 70 #define SK_ScalarSqrt2 1.414213562373095 |
| 71 #define SK_ScalarPI 3.141592653589793 |
| 72 #define SK_ScalarTanPIOver8 0.4142135623731 |
| 73 #define SK_ScalarRoot2Over2 0.70710678118655 |
| 74 #define SK_ScalarMax 1.7976931348623157+308 |
| 75 #define SK_ScalarInfinity SK_DoubleInfinity |
| 76 #define SK_ScalarNegativeInfinity SK_DoubleNegativeInfinity |
| 77 #define SK_ScalarNaN SK_DoubleNaN |
| 78 |
| 79 #define SkFixedToScalar(x) SkFixedToDouble(x) |
| 80 #define SkScalarToFixed(x) SkDoubleToFixed(x) |
| 81 |
| 82 #define SkScalarFloorToScalar(x) floor(x) |
| 83 #define SkScalarCeilToScalar(x) ceil(x) |
| 84 #define SkScalarRoundToScalar(x) floor((x) + 0.5) |
| 85 |
| 86 #define SkScalarFloorToInt(x) (int)floor(x) |
| 87 #define SkScalarCeilToInt(x) (int)ceil(x) |
| 88 #define SkScalarRoundToInt(x) (int)floor((x) + 0.5) |
| 89 |
| 90 #define SkScalarAbs(x) abs(x) |
| 91 #define SkScalarCopySign(x, y) copysign(x, y) |
| 92 #define SkScalarMod(x, y) fmod(x,y) |
| 93 #define SkScalarFraction(x) fmod(x, 1.0) |
| 94 #define SkScalarSqrt(x) sqrt(x) |
| 95 #define SkScalarPow(b, e) pow(b, e) |
| 96 |
| 97 #define SkScalarSin(radians) sin(radians) |
| 98 #define SkScalarCos(radians) cos(radians) |
| 99 #define SkScalarTan(radians) tan(radians) |
| 100 #define SkScalarASin(val) asin(val) |
| 101 #define SkScalarACos(val) acos(val) |
| 102 #define SkScalarATan2(y, x) atan2(y,x) |
| 103 #define SkScalarExp(x) exp(x) |
| 104 #define SkScalarLog(x) log(x) |
| 105 |
| 106 #endif |
| 107 |
| 108 ////////////////////////////////////////////////////////////////////////////////
////////////////// |
| 109 |
| 110 #define SkIntToScalar(x) static_cast<SkScalar>(x) |
| 111 #define SkScalarTruncToInt(x) static_cast<int>(x) |
| 112 |
| 113 #define SkScalarToFloat(x) static_cast<float>(x) |
| 114 #define SkFloatToScalar(x) static_cast<SkScalar>(x) |
| 115 #define SkScalarToDouble(x) static_cast<double>(x) |
| 116 #define SkDoubleToScalar(x) static_cast<SkScalar>(x) |
| 117 |
| 118 #define SK_ScalarMin (-SK_ScalarMax) |
| 119 |
| 120 static inline bool SkScalarIsNaN(SkScalar x) { return x != x; } |
| 121 |
| 122 /** Returns true if x is not NaN and not infinite |
| 123 */ |
| 124 static inline bool SkScalarIsFinite(SkScalar x) { |
| 125 // We rely on the following behavior of infinities and nans |
| 126 // 0 * finite --> 0 |
| 127 // 0 * infinity --> NaN |
| 128 // 0 * NaN --> NaN |
| 129 SkScalar prod = x * 0; |
| 130 // At this point, prod will either be NaN or 0 |
| 131 // Therefore we can return (prod == prod) or (0 == prod). |
| 132 return prod == prod; |
| 133 } |
85 | 134 |
86 /** | 135 /** |
87 * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5)
explicitly using | 136 * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5)
explicitly using |
88 * double, to avoid possibly losing the low bit(s) of the answer before calling
floor(). | 137 * double, to avoid possibly losing the low bit(s) of the answer before calling
floor(). |
89 * | 138 * |
90 * This routine will likely be slower than SkScalarRoundToInt(), and should onl
y be used when the | 139 * This routine will likely be slower than SkScalarRoundToInt(), and should onl
y be used when the |
91 * extra precision is known to be valuable. | 140 * extra precision is known to be valuable. |
92 * | 141 * |
93 * In particular, this catches the following case: | 142 * In particular, this catches the following case: |
94 * SkScalar x = 0.49999997; | 143 * SkScalar x = 0.49999997; |
95 * int ix = SkScalarRoundToInt(x); | 144 * int ix = SkScalarRoundToInt(x); |
96 * SkASSERT(0 == ix); // <--- fails | 145 * SkASSERT(0 == ix); // <--- fails |
97 * ix = SkDScalarRoundToInt(x); | 146 * ix = SkDScalarRoundToInt(x); |
98 * SkASSERT(0 == ix); // <--- succeeds | 147 * SkASSERT(0 == ix); // <--- succeeds |
99 */ | 148 */ |
100 static inline int SkDScalarRoundToInt(SkScalar x) { | 149 static inline int SkDScalarRoundToInt(SkScalar x) { |
101 double xx = x; | 150 double xx = x; |
102 xx += 0.5; | 151 xx += 0.5; |
103 return (int)floor(xx); | 152 return (int)floor(xx); |
104 } | 153 } |
105 | 154 |
106 /** Returns the absolute value of the specified SkScalar | 155 static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { |
107 */ | |
108 #define SkScalarAbs(x) sk_float_abs(x) | |
109 /** Return x with the sign of y | |
110 */ | |
111 #define SkScalarCopySign(x, y) sk_float_copysign(x, y) | |
112 /** Returns the value pinned between 0 and max inclusive | |
113 */ | |
114 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { | |
115 return x < 0 ? 0 : x > max ? max : x; | 156 return x < 0 ? 0 : x > max ? max : x; |
116 } | 157 } |
117 /** Returns the value pinned between min and max inclusive | 158 |
118 */ | 159 static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { |
119 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { | |
120 return x < min ? min : x > max ? max : x; | 160 return x < min ? min : x > max ? max : x; |
121 } | 161 } |
122 /** Returns the specified SkScalar squared (x*x) | 162 |
123 */ | 163 SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue); |
124 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } | 164 |
125 /** Returns the product of two SkScalars | 165 static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } |
126 */ | 166 |
127 #define SkScalarMul(a, b) ((float)(a) * (b)) | 167 #define SkScalarMul(a, b) ((SkScalar)(a) * (b)) |
128 /** Returns the product of two SkScalars plus a third SkScalar | 168 #define SkScalarMulAdd(a, b, c) ((SkScalar)(a) * (b) + (c)) |
129 */ | 169 #define SkScalarDiv(a, b) ((SkScalar)(a) / (b)) |
130 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) | 170 #define SkScalarMulDiv(a, b, c) ((SkScalar)(a) * (b) / (c)) |
131 /** Returns the quotient of two SkScalars (a/b) | |
132 */ | |
133 #define SkScalarDiv(a, b) ((float)(a) / (b)) | |
134 /** Returns the mod of two SkScalars (a mod b) | |
135 */ | |
136 #define SkScalarMod(x,y) sk_float_mod(x,y) | |
137 /** Returns the product of the first two arguments, divided by the third argumen
t | |
138 */ | |
139 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) | |
140 /** Returns the multiplicative inverse of the SkScalar (1/x) | |
141 */ | |
142 #define SkScalarInvert(x) (SK_Scalar1 / (x)) | 171 #define SkScalarInvert(x) (SK_Scalar1 / (x)) |
143 #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) | 172 #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) |
144 /** Returns the square root of the SkScalar | 173 #define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf) |
145 */ | 174 #define SkScalarHalf(a) ((a) * SK_ScalarHalf) |
146 #define SkScalarSqrt(x) sk_float_sqrt(x) | |
147 /** Returns b to the e | |
148 */ | |
149 #define SkScalarPow(b, e) sk_float_pow(b, e) | |
150 /** Returns the average of two SkScalars (a+b)/2 | |
151 */ | |
152 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f) | |
153 /** Returns one half of the specified SkScalar | |
154 */ | |
155 #define SkScalarHalf(a) ((a) * 0.5f) | |
156 | |
157 #define SK_ScalarSqrt2 1.41421356f | |
158 #define SK_ScalarPI 3.14159265f | |
159 #define SK_ScalarTanPIOver8 0.414213562f | |
160 #define SK_ScalarRoot2Over2 0.707106781f | |
161 | 175 |
162 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) | 176 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) |
163 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI)) | 177 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI)) |
164 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); | |
165 #define SkScalarSin(radians) (float)sk_float_sin(radians) | |
166 #define SkScalarCos(radians) (float)sk_float_cos(radians) | |
167 #define SkScalarTan(radians) (float)sk_float_tan(radians) | |
168 #define SkScalarASin(val) (float)sk_float_asin(val) | |
169 #define SkScalarACos(val) (float)sk_float_acos(val) | |
170 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) | |
171 #define SkScalarExp(x) (float)sk_float_exp(x) | |
172 #define SkScalarLog(x) (float)sk_float_log(x) | |
173 | 178 |
174 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } | 179 static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a :
b; } |
175 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } | 180 static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a :
b; } |
176 | 181 |
177 static inline bool SkScalarIsInt(SkScalar x) { | 182 static inline bool SkScalarIsInt(SkScalar x) { |
178 return x == (float)(int)x; | 183 return x == (SkScalar)(int)x; |
179 } | 184 } |
180 | 185 |
181 // DEPRECATED : use ToInt or ToScalar variant | 186 // DEPRECATED : use ToInt or ToScalar variant |
182 #ifdef SK_SUPPORT_DEPRECATED_SCALARROUND | 187 #ifdef SK_SUPPORT_DEPRECATED_SCALARROUND |
183 # define SkScalarFloor(x) SkScalarFloorToInt(x) | 188 # define SkScalarFloor(x) SkScalarFloorToInt(x) |
184 # define SkScalarCeil(x) SkScalarCeilToInt(x) | 189 # define SkScalarCeil(x) SkScalarCeilToInt(x) |
185 # define SkScalarRound(x) SkScalarRoundToInt(x) | 190 # define SkScalarRound(x) SkScalarRoundToInt(x) |
186 #endif | 191 #endif |
187 | 192 |
188 /** | 193 /** |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 SkASSERT(n >= 0); | 250 SkASSERT(n >= 0); |
246 for (int i = 0; i < n; ++i) { | 251 for (int i = 0; i < n; ++i) { |
247 if (a[i] != b[i]) { | 252 if (a[i] != b[i]) { |
248 return false; | 253 return false; |
249 } | 254 } |
250 } | 255 } |
251 return true; | 256 return true; |
252 } | 257 } |
253 | 258 |
254 #endif | 259 #endif |
OLD | NEW |