• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/SkAssert.h"
12 #include "include/private/base/SkFloatingPoint.h"
13 
14 typedef float SkScalar;
15 
16 #define SK_Scalar1                  1.0f
17 #define SK_ScalarHalf               0.5f
18 #define SK_ScalarSqrt2              SK_FloatSqrt2
19 #define SK_ScalarPI                 SK_FloatPI
20 #define SK_ScalarTanPIOver8         0.414213562f
21 #define SK_ScalarRoot2Over2         0.707106781f
22 #define SK_ScalarMax                3.402823466e+38f
23 #define SK_ScalarMin                (-SK_ScalarMax)
24 #define SK_ScalarInfinity           SK_FloatInfinity
25 #define SK_ScalarNegativeInfinity   SK_FloatNegativeInfinity
26 #define SK_ScalarNaN                SK_FloatNaN
27 
28 #define SkScalarFloorToScalar(x)    sk_float_floor(x)
29 #define SkScalarCeilToScalar(x)     sk_float_ceil(x)
30 #define SkScalarRoundToScalar(x)    sk_float_round(x)
31 #define SkScalarTruncToScalar(x)    sk_float_trunc(x)
32 
33 #define SkScalarFloorToInt(x)       sk_float_floor2int(x)
34 #define SkScalarCeilToInt(x)        sk_float_ceil2int(x)
35 #define SkScalarRoundToInt(x)       sk_float_round2int(x)
36 
37 #define SkScalarAbs(x)              sk_float_abs(x)
38 #define SkScalarCopySign(x, y)      sk_float_copysign(x, y)
39 #define SkScalarMod(x, y)           sk_float_mod(x,y)
40 #define SkScalarSqrt(x)             sk_float_sqrt(x)
41 #define SkScalarPow(b, e)           sk_float_pow(b, e)
42 
43 #define SkScalarSin(radians)        (float)sk_float_sin(radians)
44 #define SkScalarCos(radians)        (float)sk_float_cos(radians)
45 #define SkScalarTan(radians)        (float)sk_float_tan(radians)
46 #define SkScalarASin(val)           (float)sk_float_asin(val)
47 #define SkScalarACos(val)           (float)sk_float_acos(val)
48 #define SkScalarATan2(y, x)         (float)sk_float_atan2(y,x)
49 #define SkScalarExp(x)              (float)sk_float_exp(x)
50 #define SkScalarLog(x)              (float)sk_float_log(x)
51 #define SkScalarLog2(x)             (float)sk_float_log2(x)
52 
53 //////////////////////////////////////////////////////////////////////////////////////////////////
54 
55 #define SkIntToScalar(x)        static_cast<SkScalar>(x)
56 #define SkIntToFloat(x)         static_cast<float>(x)
57 #define SkScalarTruncToInt(x)   sk_float_saturate2int(x)
58 
59 #define SkScalarToFloat(x)      static_cast<float>(x)
60 #define SkFloatToScalar(x)      static_cast<SkScalar>(x)
61 #define SkScalarToDouble(x)     static_cast<double>(x)
62 #define SkDoubleToScalar(x)     sk_double_to_float(x)
63 
SkScalarIsNaN(SkScalar x)64 static inline bool SkScalarIsNaN(SkScalar x) { return x != x; }
65 
66 /** Returns true if x is not NaN and not infinite
67  */
SkScalarIsFinite(SkScalar x)68 static inline bool SkScalarIsFinite(SkScalar x) { return sk_float_isfinite(x); }
69 
SkScalarsAreFinite(SkScalar a,SkScalar b)70 static inline bool SkScalarsAreFinite(SkScalar a, SkScalar b) {
71     return sk_floats_are_finite(a, b);
72 }
73 
SkScalarsAreFinite(const SkScalar array[],int count)74 static inline bool SkScalarsAreFinite(const SkScalar array[], int count) {
75     return sk_floats_are_finite(array, count);
76 }
77 
78 /** Returns the fractional part of the scalar. */
SkScalarFraction(SkScalar x)79 static inline SkScalar SkScalarFraction(SkScalar x) {
80     return x - SkScalarTruncToScalar(x);
81 }
82 
SkScalarSquare(SkScalar x)83 static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
84 
85 #define SkScalarInvert(x)           sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(SK_Scalar1, (x))
86 #define SkScalarAve(a, b)           (((a) + (b)) * SK_ScalarHalf)
87 #define SkScalarHalf(a)             ((a) * SK_ScalarHalf)
88 
89 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
90 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
91 
SkScalarIsInt(SkScalar x)92 static inline bool SkScalarIsInt(SkScalar x) {
93     return x == SkScalarFloorToScalar(x);
94 }
95 
96 /**
97  *  Returns -1 || 0 || 1 depending on the sign of value:
98  *  -1 if x < 0
99  *   0 if x == 0
100  *   1 if x > 0
101  */
SkScalarSignAsInt(SkScalar x)102 static inline int SkScalarSignAsInt(SkScalar x) {
103     return x < 0 ? -1 : (x > 0);
104 }
105 
106 // Scalar result version of above
SkScalarSignAsScalar(SkScalar x)107 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
108     return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
109 }
110 
111 #define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
112 
113 static inline bool SkScalarNearlyZero(SkScalar x,
114                                       SkScalar tolerance = SK_ScalarNearlyZero) {
115     SkASSERT(tolerance >= 0);
116     return SkScalarAbs(x) <= tolerance;
117 }
118 
119 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
120                                        SkScalar tolerance = SK_ScalarNearlyZero) {
121     SkASSERT(tolerance >= 0);
122     return SkScalarAbs(x-y) <= tolerance;
123 }
124 
125 #define SK_ScalarSinCosNearlyZero   (SK_Scalar1 / (1 << 16))
126 
SkScalarSinSnapToZero(SkScalar radians)127 static inline float SkScalarSinSnapToZero(SkScalar radians) {
128     float v = SkScalarSin(radians);
129     return SkScalarNearlyZero(v, SK_ScalarSinCosNearlyZero) ? 0.0f : v;
130 }
131 
SkScalarCosSnapToZero(SkScalar radians)132 static inline float SkScalarCosSnapToZero(SkScalar radians) {
133     float v = SkScalarCos(radians);
134     return SkScalarNearlyZero(v, SK_ScalarSinCosNearlyZero) ? 0.0f : v;
135 }
136 
137 /** Linearly interpolate between A and B, based on t.
138     If t is 0, return A
139     If t is 1, return B
140     else interpolate.
141     t must be [0..SK_Scalar1]
142 */
SkScalarInterp(SkScalar A,SkScalar B,SkScalar t)143 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
144     SkASSERT(t >= 0 && t <= SK_Scalar1);
145     return A + (B - A) * t;
146 }
147 
148 /** Interpolate along the function described by (keys[length], values[length])
149     for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
150     clamp to the min or max value. This function assumes the number of pairs
151     (length) will be small and a linear search is used.
152 
153     Repeated keys are allowed for discontinuous functions (so long as keys is
154     monotonically increasing). If key is the value of a repeated scalar in
155     keys the first one will be used.
156 */
157 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
158                             const SkScalar values[], int length);
159 
160 /*
161  *  Helper to compare an array of scalars.
162  */
SkScalarsEqual(const SkScalar a[],const SkScalar b[],int n)163 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
164     SkASSERT(n >= 0);
165     for (int i = 0; i < n; ++i) {
166         if (a[i] != b[i]) {
167             return false;
168         }
169     }
170     return true;
171 }
172 
173 #endif
174