SkScalar.h revision ee9aa304579b3d5314519372728187879456d49d
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkScalar_DEFINED
18#define SkScalar_DEFINED
19
20#include "SkFixed.h"
21
22/** \file SkScalar.h
23
24    Types and macros for the data type SkScalar. This is the fractional numeric type
25    that, depending on the compile-time flag SK_SCALAR_IS_FLOAT, may be implemented
26    either as an IEEE float, or as a 16.16 SkFixed. The macros in this file are written
27    to allow the calling code to manipulate SkScalar values without knowing which representation
28    is in effect.
29*/
30
31#ifdef SK_SCALAR_IS_FLOAT
32    #include "SkFloatingPoint.h"
33
34    /** SkScalar is our type for fractional values and coordinates. Depending on
35        compile configurations, it is either represented as an IEEE float, or
36        as a 16.16 fixed point integer.
37    */
38    typedef float   SkScalar;
39    extern const uint32_t gIEEENotANumber;
40    extern const uint32_t gIEEEInfinity;
41
42    /** SK_Scalar1 is defined to be 1.0 represented as an SkScalar
43    */
44    #define SK_Scalar1              (1.0f)
45    /** SK_Scalar1 is defined to be 1/2 represented as an SkScalar
46    */
47    #define SK_ScalarHalf           (0.5f)
48    /** SK_ScalarInfinity is defined to be infinity as an SkScalar
49    */
50    #define SK_ScalarInfinity           (*(const float*)&gIEEEInfinity)
51    /** SK_ScalarMax is defined to be the largest value representable as an SkScalar
52    */
53    #define SK_ScalarMax            (3.402823466e+38f)
54    /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
55    */
56    #define SK_ScalarMin            (-SK_ScalarMax)
57    /** SK_ScalarNaN is defined to be 'Not a Number' as an SkScalar
58    */
59    #define SK_ScalarNaN      (*(const float*)(const void*)&gIEEENotANumber)
60    /** SkScalarIsNaN(n) returns true if argument is not a number
61    */
62    static inline bool SkScalarIsNaN(float x) { return x != x; }
63    /** Returns true if x is not NaN and not infinite */
64    static inline bool SkScalarIsFinite(float x) {
65        uint32_t bits = SkFloat2Bits(x);    // need unsigned for our shifts
66        int exponent = bits << 1 >> 24;
67        return exponent != 0xFF;
68    }
69    /** SkIntToScalar(n) returns its integer argument as an SkScalar
70    */
71    #define SkIntToScalar(n)        ((float)(n))
72    /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
73    */
74    #define SkFixedToScalar(x)      SkFixedToFloat(x)
75    /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
76    */
77    #define SkScalarToFixed(x)      SkFloatToFixed(x)
78
79    #define SkScalarToFloat(n)      (n)
80    #define SkFloatToScalar(n)      (n)
81
82    #define SkScalarToDouble(n)      (double)(n)
83    #define SkDoubleToScalar(n)      (float)(n)
84
85    /** SkScalarFraction(x) returns the signed fractional part of the argument
86    */
87    #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
88    /** Rounds the SkScalar to the nearest integer value
89    */
90    #define SkScalarRound(x)        sk_float_round2int(x)
91    /** Returns the smallest integer that is >= the specified SkScalar
92    */
93    #define SkScalarCeil(x)         sk_float_ceil2int(x)
94    /** Returns the largest integer that is <= the specified SkScalar
95    */
96    #define SkScalarFloor(x)        sk_float_floor2int(x)
97    /** Returns the absolute value of the specified SkScalar
98    */
99    #define SkScalarAbs(x)          sk_float_abs(x)
100    /** Return x with the sign of y
101     */
102    #define SkScalarCopySign(x, y)  sk_float_copysign(x, y)
103    /** Returns the value pinned between 0 and max inclusive
104    */
105    inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
106        return x < 0 ? 0 : x > max ? max : x;
107    }
108    /** Returns the value pinned between min and max inclusive
109    */
110    inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
111        return x < min ? min : x > max ? max : x;
112    }
113    /** Returns the specified SkScalar squared (x*x)
114    */
115    inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
116    /** Returns the product of two SkScalars
117    */
118    #define SkScalarMul(a, b)       ((float)(a) * (b))
119    /** Returns the product of two SkScalars plus a third SkScalar
120    */
121    #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
122    /** Returns the product of a SkScalar and an int rounded to the nearest integer value
123    */
124    #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
125    /** Returns the product of a SkScalar and an int promoted to the next larger int
126    */
127    #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
128    /** Returns the product of a SkScalar and an int truncated to the next smaller int
129    */
130    #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
131    /** Returns the quotient of two SkScalars (a/b)
132    */
133    #define SkScalarDiv(a, b)       ((float)(a) / (b))
134    /** Returns the mod of two SkScalars (a mod b)
135    */
136    #define SkScalarMod(x,y)        sk_float_mod(x,y)
137    /** Returns the product of the first two arguments, divided by the third argument
138    */
139    #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
140    /** Returns the multiplicative inverse of the SkScalar (1/x)
141    */
142    #define SkScalarInvert(x)       (SK_Scalar1 / (x))
143    #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
144    /** Returns the square root of the SkScalar
145    */
146    #define SkScalarSqrt(x)         sk_float_sqrt(x)
147    /** Returns the average of two SkScalars (a+b)/2
148    */
149    #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
150    /** Returns the geometric mean of two SkScalars
151    */
152    #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
153    /** Returns one half of the specified SkScalar
154    */
155    #define SkScalarHalf(a)         ((a) * 0.5f)
156
157    #define SK_ScalarSqrt2          1.41421356f
158    #define SK_ScalarPI             3.14159265f
159    #define SK_ScalarTanPIOver8     0.414213562f
160    #define SK_ScalarRoot2Over2     0.707106781f
161
162    #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
163    float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
164    #define SkScalarSin(radians)    (float)sk_float_sin(radians)
165    #define SkScalarCos(radians)    (float)sk_float_cos(radians)
166    #define SkScalarTan(radians)    (float)sk_float_tan(radians)
167    #define SkScalarASin(val)   (float)sk_float_asin(val)
168    #define SkScalarACos(val)   (float)sk_float_acos(val)
169    #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
170    #define SkScalarExp(x)  (float)sk_float_exp(x)
171    #define SkScalarLog(x)  (float)sk_float_log(x)
172
173    inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
174    inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
175
176    static inline bool SkScalarIsInt(SkScalar x) {
177        return x == (float)(int)x;
178    }
179#else
180    typedef SkFixed SkScalar;
181
182    #define SK_Scalar1              SK_Fixed1
183    #define SK_ScalarHalf           SK_FixedHalf
184    #define SK_ScalarInfinity   SK_FixedMax
185    #define SK_ScalarMax            SK_FixedMax
186    #define SK_ScalarMin            SK_FixedMin
187    #define SK_ScalarNaN            SK_FixedNaN
188    #define SkScalarIsNaN(x)        ((x) == SK_FixedNaN)
189    #define SkScalarIsFinite(x)     ((x) != SK_FixedNaN)
190
191    #define SkIntToScalar(n)        SkIntToFixed(n)
192    #define SkFixedToScalar(x)      (x)
193    #define SkScalarToFixed(x)      (x)
194    #ifdef SK_CAN_USE_FLOAT
195        #define SkScalarToFloat(n)  SkFixedToFloat(n)
196        #define SkFloatToScalar(n)  SkFloatToFixed(n)
197
198        #define SkScalarToDouble(n) SkFixedToDouble(n)
199        #define SkDoubleToScalar(n) SkDoubleToFixed(n)
200    #endif
201    #define SkScalarFraction(x)     SkFixedFraction(x)
202    #define SkScalarRound(x)        SkFixedRound(x)
203    #define SkScalarCeil(x)         SkFixedCeil(x)
204    #define SkScalarFloor(x)        SkFixedFloor(x)
205    #define SkScalarAbs(x)          SkFixedAbs(x)
206    #define SkScalarCopySign(x, y)  SkCopySign32(x, y)
207    #define SkScalarClampMax(x, max) SkClampMax(x, max)
208    #define SkScalarPin(x, min, max) SkPin32(x, min, max)
209    #define SkScalarSquare(x)       SkFixedSquare(x)
210    #define SkScalarMul(a, b)       SkFixedMul(a, b)
211    #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
212    #define SkScalarMulRound(a, b)  SkFixedMulCommon(a, b, SK_FixedHalf)
213    #define SkScalarMulCeil(a, b)   SkFixedMulCommon(a, b, SK_Fixed1 - 1)
214    #define SkScalarMulFloor(a, b)  SkFixedMulCommon(a, b, 0)
215    #define SkScalarDiv(a, b)       SkFixedDiv(a, b)
216    #define SkScalarMod(a, b)       SkFixedMod(a, b)
217    #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
218    #define SkScalarInvert(x)       SkFixedInvert(x)
219    #define SkScalarFastInvert(x)   SkFixedFastInvert(x)
220    #define SkScalarSqrt(x)         SkFixedSqrt(x)
221    #define SkScalarAve(a, b)       SkFixedAve(a, b)
222    #define SkScalarMean(a, b)      SkFixedMean(a, b)
223    #define SkScalarHalf(a)         ((a) >> 1)
224
225    #define SK_ScalarSqrt2          SK_FixedSqrt2
226    #define SK_ScalarPI             SK_FixedPI
227    #define SK_ScalarTanPIOver8     SK_FixedTanPIOver8
228    #define SK_ScalarRoot2Over2     SK_FixedRoot2Over2
229
230    #define SkDegreesToRadians(degrees)     SkFractMul(degrees, SK_FractPIOver180)
231    #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
232    #define SkScalarSin(radians)    SkFixedSin(radians)
233    #define SkScalarCos(radians)    SkFixedCos(radians)
234    #define SkScalarTan(val)        SkFixedTan(val)
235    #define SkScalarASin(val)       SkFixedASin(val)
236    #define SkScalarACos(val)       SkFixedACos(val)
237    #define SkScalarATan2(y, x)     SkFixedATan2(y,x)
238    #define SkScalarExp(x)          SkFixedExp(x)
239    #define SkScalarLog(x)          SkFixedLog(x)
240
241    #define SkMaxScalar(a, b)       SkMax32(a, b)
242    #define SkMinScalar(a, b)       SkMin32(a, b)
243
244    static inline bool SkScalarIsInt(SkFixed x) {
245        return 0 == (x & 0xffff);
246    }
247#endif
248
249#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
250
251/*  <= is slower than < for floats, so we use < for our tolerance test
252*/
253
254static inline bool SkScalarNearlyZero(SkScalar x,
255                                  SkScalar tolerance = SK_ScalarNearlyZero) {
256    SkASSERT(tolerance > 0);
257    return SkScalarAbs(x) < tolerance;
258}
259
260/** Linearly interpolate between A and B, based on t.
261    If t is 0, return A
262    If t is 1, return B
263    else interpolate.
264    t must be [0..SK_Scalar1]
265*/
266static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
267    SkASSERT(t >= 0 && t <= SK_Scalar1);
268    return A + SkScalarMul(B - A, t);
269}
270
271/** Interpolate along the function described by (keys[length], values[length])
272    for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
273    clamp to the min or max value.  This function was inspired by a desire
274    to change the multiplier for thickness in fakeBold; therefore it assumes
275    the number of pairs (length) will be small, and a linear search is used.
276    Repeated keys are allowed for discontinuous functions (so long as keys is
277    monotonically increasing), and if key is the value of a repeated scalar in
278    keys, the first one will be used.  However, that may change if a binary
279    search is used.
280*/
281SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
282                            const SkScalar values[], int length);
283
284#endif
285
286