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 "../private/SkFloatingPoint.h"
12
13 #undef SK_SCALAR_IS_FLOAT
14 #define SK_SCALAR_IS_FLOAT 1
15
16 typedef float SkScalar;
17
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
28
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)
32 #define SkScalarTruncToScalar(x) sk_float_trunc(x)
33
34 #define SkScalarFloorToInt(x) sk_float_floor2int(x)
35 #define SkScalarCeilToInt(x) sk_float_ceil2int(x)
36 #define SkScalarRoundToInt(x) sk_float_round2int(x)
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)
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)
52 #define SkScalarLog2(x) (float)sk_float_log2(x)
53
54 //////////////////////////////////////////////////////////////////////////////////////////////////
55
56 #define SkIntToScalar(x) static_cast<SkScalar>(x)
57 #define SkIntToFloat(x) static_cast<float>(x)
58 #define SkScalarTruncToInt(x) sk_float_saturate2int(x)
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) sk_double_to_float(x)
64
65 #define SK_ScalarMin (-SK_ScalarMax)
66
SkScalarIsNaN(SkScalar x)67 static inline bool SkScalarIsNaN(SkScalar x) { return x != x; }
68
69 /** Returns true if x is not NaN and not infinite
70 */
SkScalarIsFinite(SkScalar x)71 static inline bool SkScalarIsFinite(SkScalar x) { return sk_float_isfinite(x); }
72
SkScalarsAreFinite(SkScalar a,SkScalar b)73 static inline bool SkScalarsAreFinite(SkScalar a, SkScalar b) {
74 return sk_float_isfinite(a) && sk_float_isfinite(b);
75 }
76
SkScalarsAreFinite(const SkScalar array[],int count)77 static inline bool SkScalarsAreFinite(const SkScalar array[], int count) {
78 SkScalar prod = 0;
79 for (int i = 0; i < count; ++i) {
80 prod *= array[i];
81 }
82 // At this point, prod will either be NaN or 0
83 return prod == 0; // if prod is NaN, this check will return false
84 }
85
86 /**
87 * 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().
89 *
90 * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the
91 * extra precision is known to be valuable.
92 *
93 * In particular, this catches the following case:
94 * SkScalar x = 0.49999997;
95 * int ix = SkScalarRoundToInt(x);
96 * SkASSERT(0 == ix); // <--- fails
97 * ix = SkDScalarRoundToInt(x);
98 * SkASSERT(0 == ix); // <--- succeeds
99 */
SkDScalarRoundToInt(SkScalar x)100 static inline int SkDScalarRoundToInt(SkScalar x) {
101 double xx = x;
102 xx += 0.5;
103 return (int)floor(xx);
104 }
105
106 /** Returns the fractional part of the scalar. */
SkScalarFraction(SkScalar x)107 static inline SkScalar SkScalarFraction(SkScalar x) {
108 return x - SkScalarTruncToScalar(x);
109 }
110
SkScalarClampMax(SkScalar x,SkScalar max)111 static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
112 x = SkTMin(x, max);
113 x = SkTMax<SkScalar>(x, 0);
114 return x;
115 }
116
SkScalarPin(SkScalar x,SkScalar min,SkScalar max)117 static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
118 return SkTPin(x, min, max);
119 }
120
121 SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
122
SkScalarSquare(SkScalar x)123 static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
124
125 #define SkScalarInvert(x) sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(SK_Scalar1, (x))
126 #define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
127 #define SkScalarHalf(a) ((a) * SK_ScalarHalf)
128
129 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
130 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
131
SkMaxScalar(SkScalar a,SkScalar b)132 static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
SkMinScalar(SkScalar a,SkScalar b)133 static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
134
SkScalarIsInt(SkScalar x)135 static inline bool SkScalarIsInt(SkScalar x) {
136 return x == SkScalarFloorToScalar(x);
137 }
138
139 /**
140 * Returns -1 || 0 || 1 depending on the sign of value:
141 * -1 if x < 0
142 * 0 if x == 0
143 * 1 if x > 0
144 */
SkScalarSignAsInt(SkScalar x)145 static inline int SkScalarSignAsInt(SkScalar x) {
146 return x < 0 ? -1 : (x > 0);
147 }
148
149 // Scalar result version of above
SkScalarSignAsScalar(SkScalar x)150 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
151 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
152 }
153
154 #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
155
156 static inline bool SkScalarNearlyZero(SkScalar x,
157 SkScalar tolerance = SK_ScalarNearlyZero) {
158 SkASSERT(tolerance >= 0);
159 return SkScalarAbs(x) <= tolerance;
160 }
161
162 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
163 SkScalar tolerance = SK_ScalarNearlyZero) {
164 SkASSERT(tolerance >= 0);
165 return SkScalarAbs(x-y) <= tolerance;
166 }
167
168 /** Linearly interpolate between A and B, based on t.
169 If t is 0, return A
170 If t is 1, return B
171 else interpolate.
172 t must be [0..SK_Scalar1]
173 */
SkScalarInterp(SkScalar A,SkScalar B,SkScalar t)174 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
175 SkASSERT(t >= 0 && t <= SK_Scalar1);
176 return A + (B - A) * t;
177 }
178
179 /** Interpolate along the function described by (keys[length], values[length])
180 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
181 clamp to the min or max value. This function was inspired by a desire
182 to change the multiplier for thickness in fakeBold; therefore it assumes
183 the number of pairs (length) will be small, and a linear search is used.
184 Repeated keys are allowed for discontinuous functions (so long as keys is
185 monotonically increasing), and if key is the value of a repeated scalar in
186 keys, the first one will be used. However, that may change if a binary
187 search is used.
188 */
189 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
190 const SkScalar values[], int length);
191
192 /*
193 * Helper to compare an array of scalars.
194 */
SkScalarsEqual(const SkScalar a[],const SkScalar b[],int n)195 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
196 SkASSERT(n >= 0);
197 for (int i = 0; i < n; ++i) {
198 if (a[i] != b[i]) {
199 return false;
200 }
201 }
202 return true;
203 }
204
205 #endif
206