SkScalar.h revision 8f4d2306fa866a26f9448048ff63f692b2ba43aa
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
14typedef float   SkScalar;
15
16/** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
17*/
18#define SK_Scalar1              (1.0f)
19/** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
20*/
21#define SK_ScalarHalf           (0.5f)
22/** SK_ScalarInfinity is defined to be infinity as an SkScalar
23*/
24#define SK_ScalarInfinity       SK_FloatInfinity
25/** SK_ScalarNegativeInfinity is defined to be negative infinity as an SkScalar
26*/
27#define SK_ScalarNegativeInfinity       SK_FloatNegativeInfinity
28/** SK_ScalarMax is defined to be the largest value representable as an SkScalar
29*/
30#define SK_ScalarMax            (3.402823466e+38f)
31/** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
32*/
33#define SK_ScalarMin            (-SK_ScalarMax)
34/** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
35*/
36#define SK_ScalarNaN            SK_FloatNaN
37/** SkScalarIsNaN(n) returns true if argument is not a number
38*/
39static inline bool SkScalarIsNaN(float x) { return x != x; }
40
41/** Returns true if x is not NaN and not infinite */
42static inline bool SkScalarIsFinite(float x) {
43    // We rely on the following behavior of infinities and nans
44    // 0 * finite --> 0
45    // 0 * infinity --> NaN
46    // 0 * NaN --> NaN
47    float prod = x * 0;
48    // At this point, prod will either be NaN or 0
49    // Therefore we can return (prod == prod) or (0 == prod).
50    return prod == prod;
51}
52
53/** SkIntToScalar(n) returns its integer argument as an SkScalar
54*/
55#define SkIntToScalar(n)        ((float)(n))
56/** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
57*/
58#define SkFixedToScalar(x)      SkFixedToFloat(x)
59/** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
60*/
61#define SkScalarToFixed(x)      SkFloatToFixed(x)
62
63#define SkScalarToFloat(n)      (n)
64#ifndef SK_SCALAR_TO_FLOAT_EXCLUDED
65#define SkFloatToScalar(n)      (n)
66#endif
67
68#define SkScalarToDouble(n)      (double)(n)
69#define SkDoubleToScalar(n)      (float)(n)
70
71/** SkScalarFraction(x) returns the signed fractional part of the argument
72*/
73#define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
74
75#define SkScalarFloorToScalar(x)    sk_float_floor(x)
76#define SkScalarCeilToScalar(x)     sk_float_ceil(x)
77#define SkScalarRoundToScalar(x)    sk_float_floor((x) + 0.5f)
78
79#define SkScalarFloorToInt(x)       sk_float_floor2int(x)
80#define SkScalarCeilToInt(x)        sk_float_ceil2int(x)
81#define SkScalarRoundToInt(x)       sk_float_round2int(x)
82#define SkScalarTruncToInt(x)       static_cast<int>(x)
83
84/** Returns the absolute value of the specified SkScalar
85*/
86#define SkScalarAbs(x)          sk_float_abs(x)
87/** Return x with the sign of y
88 */
89#define SkScalarCopySign(x, y)  sk_float_copysign(x, y)
90/** Returns the value pinned between 0 and max inclusive
91*/
92inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
93    return x < 0 ? 0 : x > max ? max : x;
94}
95/** Returns the value pinned between min and max inclusive
96*/
97inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
98    return x < min ? min : x > max ? max : x;
99}
100/** Returns the specified SkScalar squared (x*x)
101*/
102inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
103/** Returns the product of two SkScalars
104*/
105#define SkScalarMul(a, b)       ((float)(a) * (b))
106/** Returns the product of two SkScalars plus a third SkScalar
107*/
108#define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
109/** Returns the product of a SkScalar and an int rounded to the nearest integer value
110*/
111#define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
112/** Returns the product of a SkScalar and an int promoted to the next larger int
113*/
114#define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
115/** Returns the product of a SkScalar and an int truncated to the next smaller int
116*/
117#define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
118/** Returns the quotient of two SkScalars (a/b)
119*/
120#define SkScalarDiv(a, b)       ((float)(a) / (b))
121/** Returns the mod of two SkScalars (a mod b)
122*/
123#define SkScalarMod(x,y)        sk_float_mod(x,y)
124/** Returns the product of the first two arguments, divided by the third argument
125*/
126#define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
127/** Returns the multiplicative inverse of the SkScalar (1/x)
128*/
129#define SkScalarInvert(x)       (SK_Scalar1 / (x))
130#define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
131/** Returns the square root of the SkScalar
132*/
133#define SkScalarSqrt(x)         sk_float_sqrt(x)
134/** Returns b to the e
135*/
136#define SkScalarPow(b, e)       sk_float_pow(b, e)
137/** Returns the average of two SkScalars (a+b)/2
138*/
139#define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
140/** Returns the geometric mean of two SkScalars
141*/
142#define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
143/** Returns one half of the specified SkScalar
144*/
145#define SkScalarHalf(a)         ((a) * 0.5f)
146
147#define SK_ScalarSqrt2          1.41421356f
148#define SK_ScalarPI             3.14159265f
149#define SK_ScalarTanPIOver8     0.414213562f
150#define SK_ScalarRoot2Over2     0.707106781f
151
152#define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
153float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
154#define SkScalarSin(radians)    (float)sk_float_sin(radians)
155#define SkScalarCos(radians)    (float)sk_float_cos(radians)
156#define SkScalarTan(radians)    (float)sk_float_tan(radians)
157#define SkScalarASin(val)   (float)sk_float_asin(val)
158#define SkScalarACos(val)   (float)sk_float_acos(val)
159#define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
160#define SkScalarExp(x)  (float)sk_float_exp(x)
161#define SkScalarLog(x)  (float)sk_float_log(x)
162
163inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
164inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
165
166static inline bool SkScalarIsInt(SkScalar x) {
167    return x == (float)(int)x;
168}
169
170// DEPRECATED : use ToInt or ToScalar variant
171#define SkScalarFloor(x)    SkScalarFloorToInt(x)
172#define SkScalarCeil(x)     SkScalarCeilToInt(x)
173#define SkScalarRound(x)    SkScalarRoundToInt(x)
174
175/**
176 *  Returns -1 || 0 || 1 depending on the sign of value:
177 *  -1 if x < 0
178 *   0 if x == 0
179 *   1 if x > 0
180 */
181static inline int SkScalarSignAsInt(SkScalar x) {
182    return x < 0 ? -1 : (x > 0);
183}
184
185// Scalar result version of above
186static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
187    return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
188}
189
190#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
191
192static inline bool SkScalarNearlyZero(SkScalar x,
193                                    SkScalar tolerance = SK_ScalarNearlyZero) {
194    SkASSERT(tolerance >= 0);
195    return SkScalarAbs(x) <= tolerance;
196}
197
198static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
199                                     SkScalar tolerance = SK_ScalarNearlyZero) {
200    SkASSERT(tolerance >= 0);
201    return SkScalarAbs(x-y) <= tolerance;
202}
203
204/** Linearly interpolate between A and B, based on t.
205    If t is 0, return A
206    If t is 1, return B
207    else interpolate.
208    t must be [0..SK_Scalar1]
209*/
210static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
211    SkASSERT(t >= 0 && t <= SK_Scalar1);
212    return A + SkScalarMul(B - A, t);
213}
214
215static inline SkScalar SkScalarLog2(SkScalar x) {
216    static const SkScalar log2_conversion_factor = SkScalarDiv(1, SkScalarLog(2));
217
218    return SkScalarMul(SkScalarLog(x), log2_conversion_factor);
219}
220
221/** Interpolate along the function described by (keys[length], values[length])
222    for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
223    clamp to the min or max value.  This function was inspired by a desire
224    to change the multiplier for thickness in fakeBold; therefore it assumes
225    the number of pairs (length) will be small, and a linear search is used.
226    Repeated keys are allowed for discontinuous functions (so long as keys is
227    monotonically increasing), and if key is the value of a repeated scalar in
228    keys, the first one will be used.  However, that may change if a binary
229    search is used.
230*/
231SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
232                            const SkScalar values[], int length);
233
234/*
235 *  Helper to compare an array of scalars.
236 */
237static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
238    SkASSERT(n >= 0);
239    for (int i = 0; i < n; ++i) {
240        if (a[i] != b[i]) {
241            return false;
242        }
243    }
244    return true;
245}
246
247#endif
248