SkScalar.h revision 8015cdd8fa5694e52b70e728bcdc6b35d739b819
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
16typedef 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*/
41static inline bool SkScalarIsNaN(float x) { return x != x; }
42
43/** Returns true if x is not NaN and not infinite */
44static 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*/
94inline 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*/
99inline 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*/
104inline 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))
143float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
144#define SkScalarSin(radians)    (float)sk_float_sin(radians)
145#define SkScalarCos(radians)    (float)sk_float_cos(radians)
146#define SkScalarTan(radians)    (float)sk_float_tan(radians)
147#define SkScalarASin(val)   (float)sk_float_asin(val)
148#define SkScalarACos(val)   (float)sk_float_acos(val)
149#define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
150#define SkScalarExp(x)  (float)sk_float_exp(x)
151#define SkScalarLog(x)  (float)sk_float_log(x)
152
153inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
154inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
155
156static inline bool SkScalarIsInt(SkScalar x) {
157    return x == (float)(int)x;
158}
159
160// DEPRECATED : use ToInt or ToScalar variant
161#ifdef SK_SUPPORT_DEPRECATED_SCALARROUND
162#   define SkScalarFloor(x)    SkScalarFloorToInt(x)
163#   define SkScalarCeil(x)     SkScalarCeilToInt(x)
164#   define SkScalarRound(x)    SkScalarRoundToInt(x)
165#endif
166
167/**
168 *  Returns -1 || 0 || 1 depending on the sign of value:
169 *  -1 if x < 0
170 *   0 if x == 0
171 *   1 if x > 0
172 */
173static inline int SkScalarSignAsInt(SkScalar x) {
174    return x < 0 ? -1 : (x > 0);
175}
176
177// Scalar result version of above
178static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
179    return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
180}
181
182#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
183
184static inline bool SkScalarNearlyZero(SkScalar x,
185                                    SkScalar tolerance = SK_ScalarNearlyZero) {
186    SkASSERT(tolerance >= 0);
187    return SkScalarAbs(x) <= tolerance;
188}
189
190static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
191                                     SkScalar tolerance = SK_ScalarNearlyZero) {
192    SkASSERT(tolerance >= 0);
193    return SkScalarAbs(x-y) <= tolerance;
194}
195
196/** Linearly interpolate between A and B, based on t.
197    If t is 0, return A
198    If t is 1, return B
199    else interpolate.
200    t must be [0..SK_Scalar1]
201*/
202static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
203    SkASSERT(t >= 0 && t <= SK_Scalar1);
204    return A + SkScalarMul(B - A, t);
205}
206
207/** Interpolate along the function described by (keys[length], values[length])
208    for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
209    clamp to the min or max value.  This function was inspired by a desire
210    to change the multiplier for thickness in fakeBold; therefore it assumes
211    the number of pairs (length) will be small, and a linear search is used.
212    Repeated keys are allowed for discontinuous functions (so long as keys is
213    monotonically increasing), and if key is the value of a repeated scalar in
214    keys, the first one will be used.  However, that may change if a binary
215    search is used.
216*/
217SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
218                            const SkScalar values[], int length);
219
220/*
221 *  Helper to compare an array of scalars.
222 */
223static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
224    SkASSERT(n >= 0);
225    for (int i = 0; i < n; ++i) {
226        if (a[i] != b[i]) {
227            return false;
228        }
229    }
230    return true;
231}
232
233#endif
234