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 "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 SK_FloatSqrt2
21 #define SK_ScalarPI SK_FloatPI
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_floats_are_finite(a, b);
75 }
76
SkScalarsAreFinite(const SkScalar array[],int count)77 static inline bool SkScalarsAreFinite(const SkScalar array[], int count) {
78 return sk_floats_are_finite(array, count);
79 }
80
81 /**
82 * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using
83 * double, to avoid possibly losing the low bit(s) of the answer before calling floor().
84 *
85 * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the
86 * extra precision is known to be valuable.
87 *
88 * In particular, this catches the following case:
89 * SkScalar x = 0.49999997;
90 * int ix = SkScalarRoundToInt(x);
91 * SkASSERT(0 == ix); // <--- fails
92 * ix = SkDScalarRoundToInt(x);
93 * SkASSERT(0 == ix); // <--- succeeds
94 */
SkDScalarRoundToInt(SkScalar x)95 static inline int SkDScalarRoundToInt(SkScalar x) {
96 double xx = x;
97 xx += 0.5;
98 return (int)floor(xx);
99 }
100
101 /** Returns the fractional part of the scalar. */
SkScalarFraction(SkScalar x)102 static inline SkScalar SkScalarFraction(SkScalar x) {
103 return x - SkScalarTruncToScalar(x);
104 }
105
SkScalarClampMax(SkScalar x,SkScalar max)106 static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
107 x = SkTMin(x, max);
108 x = SkTMax<SkScalar>(x, 0);
109 return x;
110 }
111
SkScalarPin(SkScalar x,SkScalar min,SkScalar max)112 static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
113 return SkTPin(x, min, max);
114 }
115
SkScalarSquare(SkScalar x)116 static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
117
118 #define SkScalarInvert(x) sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(SK_Scalar1, (x))
119 #define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
120 #define SkScalarHalf(a) ((a) * SK_ScalarHalf)
121
122 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
123 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
124
SkMaxScalar(SkScalar a,SkScalar b)125 static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
SkMinScalar(SkScalar a,SkScalar b)126 static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
127
SkScalarIsInt(SkScalar x)128 static inline bool SkScalarIsInt(SkScalar x) {
129 return x == SkScalarFloorToScalar(x);
130 }
131
132 /**
133 * Returns -1 || 0 || 1 depending on the sign of value:
134 * -1 if x < 0
135 * 0 if x == 0
136 * 1 if x > 0
137 */
SkScalarSignAsInt(SkScalar x)138 static inline int SkScalarSignAsInt(SkScalar x) {
139 return x < 0 ? -1 : (x > 0);
140 }
141
142 // Scalar result version of above
SkScalarSignAsScalar(SkScalar x)143 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
144 return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
145 }
146
147 #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
148
149 static inline bool SkScalarNearlyZero(SkScalar x,
150 SkScalar tolerance = SK_ScalarNearlyZero) {
151 SkASSERT(tolerance >= 0);
152 return SkScalarAbs(x) <= tolerance;
153 }
154
155 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
156 SkScalar tolerance = SK_ScalarNearlyZero) {
157 SkASSERT(tolerance >= 0);
158 return SkScalarAbs(x-y) <= tolerance;
159 }
160
SkScalarSinSnapToZero(SkScalar radians)161 static inline float SkScalarSinSnapToZero(SkScalar radians) {
162 float v = SkScalarSin(radians);
163 return SkScalarNearlyZero(v) ? 0.0f : v;
164 }
165
SkScalarCosSnapToZero(SkScalar radians)166 static inline float SkScalarCosSnapToZero(SkScalar radians) {
167 float v = SkScalarCos(radians);
168 return SkScalarNearlyZero(v) ? 0.0f : v;
169 }
170
171 /** Linearly interpolate between A and B, based on t.
172 If t is 0, return A
173 If t is 1, return B
174 else interpolate.
175 t must be [0..SK_Scalar1]
176 */
SkScalarInterp(SkScalar A,SkScalar B,SkScalar t)177 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
178 SkASSERT(t >= 0 && t <= SK_Scalar1);
179 return A + (B - A) * t;
180 }
181
182 /** Interpolate along the function described by (keys[length], values[length])
183 for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
184 clamp to the min or max value. This function was inspired by a desire
185 to change the multiplier for thickness in fakeBold; therefore it assumes
186 the number of pairs (length) will be small, and a linear search is used.
187 Repeated keys are allowed for discontinuous functions (so long as keys is
188 monotonically increasing), and if key is the value of a repeated scalar in
189 keys, the first one will be used. However, that may change if a binary
190 search is used.
191 */
192 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
193 const SkScalar values[], int length);
194
195 /*
196 * Helper to compare an array of scalars.
197 */
SkScalarsEqual(const SkScalar a[],const SkScalar b[],int n)198 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
199 SkASSERT(n >= 0);
200 for (int i = 0; i < n; ++i) {
201 if (a[i] != b[i]) {
202 return false;
203 }
204 }
205 return true;
206 }
207
208 #endif
209