SkScalar.h revision 187d5595901d1120d9425851e5afdd773f574502
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.4028235e+38f)
54    /** SK_ScalarMin is defined to be the smallest value representable as an SkScalar
55    */
56    #define SK_ScalarMin            (1.1754944e-38f)
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    /** SkIntToScalar(n) returns its integer argument as an SkScalar
64    */
65    #define SkIntToScalar(n)        ((float)(n))
66    /** SkFixedToScalar(n) returns its SkFixed argument as an SkScalar
67    */
68    #define SkFixedToScalar(x)      SkFixedToFloat(x)
69    /** SkScalarToFixed(n) returns its SkScalar argument as an SkFixed
70    */
71    #define SkScalarToFixed(x)      SkFloatToFixed(x)
72
73    #define SkScalarToFloat(n)      (n)
74    #define SkFloatToScalar(n)      (n)
75
76    #define SkScalarToDouble(n)      (double)(n)
77    #define SkDoubleToScalar(n)      (float)(n)
78
79    /** SkScalarFraction(x) returns the signed fractional part of the argument
80    */
81    #define SkScalarFraction(x)     sk_float_mod(x, 1.0f)
82    /** Rounds the SkScalar to the nearest integer value
83    */
84    #define SkScalarRound(x)        sk_float_round2int(x)
85    /** Returns the smallest integer that is >= the specified SkScalar
86    */
87    #define SkScalarCeil(x)         sk_float_ceil2int(x)
88    /** Returns the largest integer that is <= the specified SkScalar
89    */
90    #define SkScalarFloor(x)        sk_float_floor2int(x)
91    /** Returns the absolute value of the specified SkScalar
92    */
93    #define SkScalarAbs(x)          sk_float_abs(x)
94    /** Returns the value pinned between 0 and max inclusive
95    */
96    inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
97        return x < 0 ? 0 : x > max ? max : x;
98    }
99    /** Returns the value pinned between min and max inclusive
100    */
101    inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
102        return x < min ? min : x > max ? max : x;
103    }
104    /** Returns the specified SkScalar squared (x*x)
105    */
106    inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
107    /** Returns the product of two SkScalars
108    */
109    #define SkScalarMul(a, b)       ((float)(a) * (b))
110    /** Returns the product of two SkScalars plus a third SkScalar
111    */
112    #define SkScalarMulAdd(a, b, c) ((float)(a) * (b) + (c))
113    /** Returns the product of a SkScalar and an int rounded to the nearest integer value
114    */
115    #define SkScalarMulRound(a, b) SkScalarRound((float)(a) * (b))
116    /** Returns the product of a SkScalar and an int promoted to the next larger int
117    */
118    #define SkScalarMulCeil(a, b) SkScalarCeil((float)(a) * (b))
119    /** Returns the product of a SkScalar and an int truncated to the next smaller int
120    */
121    #define SkScalarMulFloor(a, b) SkScalarFloor((float)(a) * (b))
122    /** Returns the quotient of two SkScalars (a/b)
123    */
124    #define SkScalarDiv(a, b)       ((float)(a) / (b))
125    /** Returns the mod of two SkScalars (a mod b)
126    */
127    #define SkScalarMod(x,y)        sk_float_mod(x,y)
128    /** Returns the product of the first two arguments, divided by the third argument
129    */
130    #define SkScalarMulDiv(a, b, c) ((float)(a) * (b) / (c))
131    /** Returns the multiplicative inverse of the SkScalar (1/x)
132    */
133    #define SkScalarInvert(x)       (SK_Scalar1 / (x))
134    #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
135    /** Returns the square root of the SkScalar
136    */
137    #define SkScalarSqrt(x)         sk_float_sqrt(x)
138    /** Returns the average of two SkScalars (a+b)/2
139    */
140    #define SkScalarAve(a, b)       (((a) + (b)) * 0.5f)
141    /** Returns the geometric mean of two SkScalars
142    */
143    #define SkScalarMean(a, b)      sk_float_sqrt((float)(a) * (b))
144    /** Returns one half of the specified SkScalar
145    */
146    #define SkScalarHalf(a)         ((a) * 0.5f)
147
148    #define SK_ScalarSqrt2          1.41421356f
149    #define SK_ScalarPI             3.14159265f
150    #define SK_ScalarTanPIOver8     0.414213562f
151    #define SK_ScalarRoot2Over2     0.707106781f
152
153    #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
154    float SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
155    #define SkScalarSin(radians)    (float)sk_float_sin(radians)
156    #define SkScalarCos(radians)    (float)sk_float_cos(radians)
157    #define SkScalarTan(radians)    (float)sk_float_tan(radians)
158    #define SkScalarASin(val)   (float)sk_float_asin(val)
159    #define SkScalarACos(val)   (float)sk_float_acos(val)
160    #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
161    #define SkScalarExp(x)  (float)sk_float_exp(x)
162    #define SkScalarLog(x)  (float)sk_float_log(x)
163
164    inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
165    inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
166
167#else
168    typedef SkFixed SkScalar;
169
170    #define SK_Scalar1              SK_Fixed1
171    #define SK_ScalarHalf           SK_FixedHalf
172    #define SK_ScalarInfinity   SK_FixedMax
173    #define SK_ScalarMax            SK_FixedMax
174    #define SK_ScalarMin            SK_FixedMin
175    #define SK_ScalarNaN            SK_FixedNaN
176    #define SkScalarIsNaN(x)        ((x) == SK_FixedNaN)
177    #define SkIntToScalar(n)        SkIntToFixed(n)
178    #define SkFixedToScalar(x)      (x)
179    #define SkScalarToFixed(x)      (x)
180    #ifdef SK_CAN_USE_FLOAT
181        #define SkScalarToFloat(n)  SkFixedToFloat(n)
182        #define SkFloatToScalar(n)  SkFloatToFixed(n)
183
184        #define SkScalarToDouble(n) SkFixedToDouble(n)
185        #define SkDoubleToScalar(n) SkDoubleToFixed(n)
186    #endif
187    #define SkScalarFraction(x)     SkFixedFraction(x)
188    #define SkScalarRound(x)        SkFixedRound(x)
189    #define SkScalarCeil(x)         SkFixedCeil(x)
190    #define SkScalarFloor(x)        SkFixedFloor(x)
191    #define SkScalarAbs(x)          SkFixedAbs(x)
192    #define SkScalarClampMax(x, max) SkClampMax(x, max)
193    #define SkScalarPin(x, min, max) SkPin32(x, min, max)
194    #define SkScalarSquare(x)       SkFixedSquare(x)
195    #define SkScalarMul(a, b)       SkFixedMul(a, b)
196    #define SkScalarMulAdd(a, b, c) SkFixedMulAdd(a, b, c)
197    #define SkScalarMulRound(a, b)  SkFixedMulCommon(a, b, SK_FixedHalf)
198    #define SkScalarMulCeil(a, b)   SkFixedMulCommon(a, b, SK_Fixed1 - 1)
199    #define SkScalarMulFloor(a, b)  SkFixedMulCommon(a, b, 0)
200    #define SkScalarDiv(a, b)       SkFixedDiv(a, b)
201    #define SkScalarMod(a, b)       SkFixedMod(a, b)
202    #define SkScalarMulDiv(a, b, c) SkMulDiv(a, b, c)
203    #define SkScalarInvert(x)       SkFixedInvert(x)
204    #define SkScalarFastInvert(x)   SkFixedFastInvert(x)
205    #define SkScalarSqrt(x)         SkFixedSqrt(x)
206    #define SkScalarAve(a, b)       SkFixedAve(a, b)
207    #define SkScalarMean(a, b)      SkFixedMean(a, b)
208    #define SkScalarHalf(a)         ((a) >> 1)
209
210    #define SK_ScalarSqrt2          SK_FixedSqrt2
211    #define SK_ScalarPI             SK_FixedPI
212    #define SK_ScalarTanPIOver8     SK_FixedTanPIOver8
213    #define SK_ScalarRoot2Over2     SK_FixedRoot2Over2
214
215    #define SkDegreesToRadians(degrees)     SkFractMul(degrees, SK_FractPIOver180)
216    #define SkScalarSinCos(radians, cosPtr) SkFixedSinCos(radians, cosPtr)
217    #define SkScalarSin(radians)    SkFixedSin(radians)
218    #define SkScalarCos(radians)    SkFixedCos(radians)
219    #define SkScalarTan(val)        SkFixedTan(val)
220    #define SkScalarASin(val)       SkFixedASin(val)
221    #define SkScalarACos(val)       SkFixedACos(val)
222    #define SkScalarATan2(y, x)     SkFixedATan2(y,x)
223    #define SkScalarExp(x)          SkFixedExp(x)
224    #define SkScalarLog(x)          SkFixedLog(x)
225
226    #define SkMaxScalar(a, b)       SkMax32(a, b)
227    #define SkMinScalar(a, b)       SkMin32(a, b)
228#endif
229
230#define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
231
232/*  <= is slower than < for floats, so we use < for our tolerance test
233*/
234
235static inline bool SkScalarNearlyZero(SkScalar x,
236                                  SkScalar tolerance = SK_ScalarNearlyZero) {
237    SkASSERT(tolerance > 0);
238    return SkScalarAbs(x) < tolerance;
239}
240
241/** Linearly interpolate between A and B, based on t.
242    If t is 0, return A
243    If t is 1, return B
244    else interpolate.
245    t must be [0..SK_Scalar1]
246*/
247static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
248    SkASSERT(t >= 0 && t <= SK_Scalar1);
249    return A + SkScalarMul(B - A, t);
250}
251
252#endif
253
254