|
1 /* |
|
2 * Copyright 2006 The Android Open Source Project |
|
3 * |
|
4 * Use of this source code is governed by a BSD-style license that can be |
|
5 * found in the LICENSE file. |
|
6 */ |
|
7 |
|
8 #ifndef SkScalar_DEFINED |
|
9 #define SkScalar_DEFINED |
|
10 |
|
11 #include "SkFixed.h" |
|
12 #include "SkFloatingPoint.h" |
|
13 |
|
14 //#define SK_SUPPORT_DEPRECATED_SCALARROUND |
|
15 |
|
16 typedef float SkScalar; |
|
17 |
|
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 SkScalar |
|
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 |
|
43 /** Returns true if x is not NaN and not infinite */ |
|
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 |
|
55 /** SkIntToScalar(n) returns its integer argument as an 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 |
|
65 #define SkScalarToFloat(n) (n) |
|
66 #ifndef SK_SCALAR_TO_FLOAT_EXCLUDED |
|
67 #define SkFloatToScalar(n) (n) |
|
68 #endif |
|
69 |
|
70 #define SkScalarToDouble(n) (double)(n) |
|
71 #define SkDoubleToScalar(n) (float)(n) |
|
72 |
|
73 /** SkScalarFraction(x) returns the signed fractional part of the argument |
|
74 */ |
|
75 #define SkScalarFraction(x) sk_float_mod(x, 1.0f) |
|
76 |
|
77 #define SkScalarFloorToScalar(x) sk_float_floor(x) |
|
78 #define SkScalarCeilToScalar(x) sk_float_ceil(x) |
|
79 #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f) |
|
80 |
|
81 #define SkScalarFloorToInt(x) sk_float_floor2int(x) |
|
82 #define SkScalarCeilToInt(x) sk_float_ceil2int(x) |
|
83 #define SkScalarRoundToInt(x) sk_float_round2int(x) |
|
84 #define SkScalarTruncToInt(x) static_cast<int>(x) |
|
85 |
|
86 /** Returns the absolute value of the specified SkScalar |
|
87 */ |
|
88 #define SkScalarAbs(x) sk_float_abs(x) |
|
89 /** Return x with the sign of y |
|
90 */ |
|
91 #define SkScalarCopySign(x, y) sk_float_copysign(x, y) |
|
92 /** Returns the value pinned between 0 and max inclusive |
|
93 */ |
|
94 inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) { |
|
95 return x < 0 ? 0 : x > max ? max : x; |
|
96 } |
|
97 /** Returns the value pinned between min and max inclusive |
|
98 */ |
|
99 inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) { |
|
100 return x < min ? min : x > max ? max : x; |
|
101 } |
|
102 /** Returns the specified SkScalar squared (x*x) |
|
103 */ |
|
104 inline SkScalar SkScalarSquare(SkScalar x) { return x * x; } |
|
105 /** Returns the product of two SkScalars |
|
106 */ |
|
107 #define SkScalarMul(a, b) ((float)(a) * (b)) |
|
108 /** Returns the product of two SkScalars plus a third SkScalar |
|
109 */ |
|
110 #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c)) |
|
111 /** Returns the quotient of two SkScalars (a/b) |
|
112 */ |
|
113 #define SkScalarDiv(a, b) ((float)(a) / (b)) |
|
114 /** Returns the mod of two SkScalars (a mod b) |
|
115 */ |
|
116 #define SkScalarMod(x,y) sk_float_mod(x,y) |
|
117 /** Returns the product of the first two arguments, divided by the third argument |
|
118 */ |
|
119 #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c)) |
|
120 /** Returns the multiplicative inverse of the SkScalar (1/x) |
|
121 */ |
|
122 #define SkScalarInvert(x) (SK_Scalar1 / (x)) |
|
123 #define SkScalarFastInvert(x) (SK_Scalar1 / (x)) |
|
124 /** Returns the square root of the SkScalar |
|
125 */ |
|
126 #define SkScalarSqrt(x) sk_float_sqrt(x) |
|
127 /** Returns b to the e |
|
128 */ |
|
129 #define SkScalarPow(b, e) sk_float_pow(b, e) |
|
130 /** Returns the average of two SkScalars (a+b)/2 |
|
131 */ |
|
132 #define SkScalarAve(a, b) (((a) + (b)) * 0.5f) |
|
133 /** Returns one half of the specified SkScalar |
|
134 */ |
|
135 #define SkScalarHalf(a) ((a) * 0.5f) |
|
136 |
|
137 #define SK_ScalarSqrt2 1.41421356f |
|
138 #define SK_ScalarPI 3.14159265f |
|
139 #define SK_ScalarTanPIOver8 0.414213562f |
|
140 #define SK_ScalarRoot2Over2 0.707106781f |
|
141 |
|
142 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180)) |
|
143 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI)) |
|
144 float SkScalarSinCos(SkScalar radians, SkScalar* cosValue); |
|
145 #define SkScalarSin(radians) (float)sk_float_sin(radians) |
|
146 #define SkScalarCos(radians) (float)sk_float_cos(radians) |
|
147 #define SkScalarTan(radians) (float)sk_float_tan(radians) |
|
148 #define SkScalarASin(val) (float)sk_float_asin(val) |
|
149 #define SkScalarACos(val) (float)sk_float_acos(val) |
|
150 #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x) |
|
151 #define SkScalarExp(x) (float)sk_float_exp(x) |
|
152 #define SkScalarLog(x) (float)sk_float_log(x) |
|
153 |
|
154 inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; } |
|
155 inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; } |
|
156 |
|
157 static inline bool SkScalarIsInt(SkScalar x) { |
|
158 return x == (float)(int)x; |
|
159 } |
|
160 |
|
161 // DEPRECATED : use ToInt or ToScalar variant |
|
162 #ifdef SK_SUPPORT_DEPRECATED_SCALARROUND |
|
163 # define SkScalarFloor(x) SkScalarFloorToInt(x) |
|
164 # define SkScalarCeil(x) SkScalarCeilToInt(x) |
|
165 # define SkScalarRound(x) SkScalarRoundToInt(x) |
|
166 #endif |
|
167 |
|
168 /** |
|
169 * Returns -1 || 0 || 1 depending on the sign of value: |
|
170 * -1 if x < 0 |
|
171 * 0 if x == 0 |
|
172 * 1 if x > 0 |
|
173 */ |
|
174 static inline int SkScalarSignAsInt(SkScalar x) { |
|
175 return x < 0 ? -1 : (x > 0); |
|
176 } |
|
177 |
|
178 // Scalar result version of above |
|
179 static inline SkScalar SkScalarSignAsScalar(SkScalar x) { |
|
180 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0); |
|
181 } |
|
182 |
|
183 #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12)) |
|
184 |
|
185 static inline bool SkScalarNearlyZero(SkScalar x, |
|
186 SkScalar tolerance = SK_ScalarNearlyZero) { |
|
187 SkASSERT(tolerance >= 0); |
|
188 return SkScalarAbs(x) <= tolerance; |
|
189 } |
|
190 |
|
191 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y, |
|
192 SkScalar tolerance = SK_ScalarNearlyZero) { |
|
193 SkASSERT(tolerance >= 0); |
|
194 return SkScalarAbs(x-y) <= tolerance; |
|
195 } |
|
196 |
|
197 /** Linearly interpolate between A and B, based on t. |
|
198 If t is 0, return A |
|
199 If t is 1, return B |
|
200 else interpolate. |
|
201 t must be [0..SK_Scalar1] |
|
202 */ |
|
203 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) { |
|
204 SkASSERT(t >= 0 && t <= SK_Scalar1); |
|
205 return A + (B - A) * t; |
|
206 } |
|
207 |
|
208 /** Interpolate along the function described by (keys[length], values[length]) |
|
209 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length] |
|
210 clamp to the min or max value. This function was inspired by a desire |
|
211 to change the multiplier for thickness in fakeBold; therefore it assumes |
|
212 the number of pairs (length) will be small, and a linear search is used. |
|
213 Repeated keys are allowed for discontinuous functions (so long as keys is |
|
214 monotonically increasing), and if key is the value of a repeated scalar in |
|
215 keys, the first one will be used. However, that may change if a binary |
|
216 search is used. |
|
217 */ |
|
218 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[], |
|
219 const SkScalar values[], int length); |
|
220 |
|
221 /* |
|
222 * Helper to compare an array of scalars. |
|
223 */ |
|
224 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) { |
|
225 SkASSERT(n >= 0); |
|
226 for (int i = 0; i < n; ++i) { |
|
227 if (a[i] != b[i]) { |
|
228 return false; |
|
229 } |
|
230 } |
|
231 return true; |
|
232 } |
|
233 |
|
234 #endif |