Home | History | Annotate | Download | only in core
      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 "../private/SkFloatingPoint.h"
     12 
     13 #undef SK_SCALAR_IS_FLOAT
     14 #define SK_SCALAR_IS_FLOAT  1
     15 
     16 typedef float SkScalar;
     17 
     18 #define SK_Scalar1                  1.0f
     19 #define SK_ScalarHalf               0.5f
     20 #define SK_ScalarSqrt2              1.41421356f
     21 #define SK_ScalarPI                 3.14159265f
     22 #define SK_ScalarTanPIOver8         0.414213562f
     23 #define SK_ScalarRoot2Over2         0.707106781f
     24 #define SK_ScalarMax                3.402823466e+38f
     25 #define SK_ScalarInfinity           SK_FloatInfinity
     26 #define SK_ScalarNegativeInfinity   SK_FloatNegativeInfinity
     27 #define SK_ScalarNaN                SK_FloatNaN
     28 
     29 #define SkScalarFloorToScalar(x)    sk_float_floor(x)
     30 #define SkScalarCeilToScalar(x)     sk_float_ceil(x)
     31 #define SkScalarRoundToScalar(x)    sk_float_floor((x) + 0.5f)
     32 #define SkScalarTruncToScalar(x)    sk_float_trunc(x)
     33 
     34 #define SkScalarFloorToInt(x)       sk_float_floor2int(x)
     35 #define SkScalarCeilToInt(x)        sk_float_ceil2int(x)
     36 #define SkScalarRoundToInt(x)       sk_float_round2int(x)
     37 
     38 #define SkScalarAbs(x)              sk_float_abs(x)
     39 #define SkScalarCopySign(x, y)      sk_float_copysign(x, y)
     40 #define SkScalarMod(x, y)           sk_float_mod(x,y)
     41 #define SkScalarSqrt(x)             sk_float_sqrt(x)
     42 #define SkScalarPow(b, e)           sk_float_pow(b, e)
     43 
     44 #define SkScalarSin(radians)        (float)sk_float_sin(radians)
     45 #define SkScalarCos(radians)        (float)sk_float_cos(radians)
     46 #define SkScalarTan(radians)        (float)sk_float_tan(radians)
     47 #define SkScalarASin(val)           (float)sk_float_asin(val)
     48 #define SkScalarACos(val)           (float)sk_float_acos(val)
     49 #define SkScalarATan2(y, x)         (float)sk_float_atan2(y,x)
     50 #define SkScalarExp(x)              (float)sk_float_exp(x)
     51 #define SkScalarLog(x)              (float)sk_float_log(x)
     52 #define SkScalarLog2(x)             (float)sk_float_log2(x)
     53 
     54 //////////////////////////////////////////////////////////////////////////////////////////////////
     55 
     56 #define SkIntToScalar(x)        static_cast<SkScalar>(x)
     57 #define SkIntToFloat(x)         static_cast<float>(x)
     58 #define SkScalarTruncToInt(x)   sk_float_saturate2int(x)
     59 
     60 #define SkScalarToFloat(x)      static_cast<float>(x)
     61 #define SkFloatToScalar(x)      static_cast<SkScalar>(x)
     62 #define SkScalarToDouble(x)     static_cast<double>(x)
     63 #define SkDoubleToScalar(x)     static_cast<SkScalar>(x)
     64 
     65 #define SK_ScalarMin            (-SK_ScalarMax)
     66 
     67 static inline bool SkScalarIsNaN(SkScalar x) { return x != x; }
     68 
     69 /** Returns true if x is not NaN and not infinite
     70  */
     71 static inline bool SkScalarIsFinite(SkScalar x) {
     72     // We rely on the following behavior of infinities and nans
     73     // 0 * finite --> 0
     74     // 0 * infinity --> NaN
     75     // 0 * NaN --> NaN
     76     SkScalar prod = x * 0;
     77     // At this point, prod will either be NaN or 0
     78     return !SkScalarIsNaN(prod);
     79 }
     80 
     81 static inline bool SkScalarsAreFinite(SkScalar a, SkScalar b) {
     82     SkScalar prod = 0;
     83     prod *= a;
     84     prod *= b;
     85     // At this point, prod will either be NaN or 0
     86     return !SkScalarIsNaN(prod);
     87 }
     88 
     89 static inline bool SkScalarsAreFinite(const SkScalar array[], int count) {
     90     SkScalar prod = 0;
     91     for (int i = 0; i < count; ++i) {
     92         prod *= array[i];
     93     }
     94     // At this point, prod will either be NaN or 0
     95     return !SkScalarIsNaN(prod);
     96 }
     97 
     98 /**
     99  *  Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using
    100  *  double, to avoid possibly losing the low bit(s) of the answer before calling floor().
    101  *
    102  *  This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the
    103  *  extra precision is known to be valuable.
    104  *
    105  *  In particular, this catches the following case:
    106  *      SkScalar x = 0.49999997;
    107  *      int ix = SkScalarRoundToInt(x);
    108  *      SkASSERT(0 == ix);    // <--- fails
    109  *      ix = SkDScalarRoundToInt(x);
    110  *      SkASSERT(0 == ix);    // <--- succeeds
    111  */
    112 static inline int SkDScalarRoundToInt(SkScalar x) {
    113     double xx = x;
    114     xx += 0.5;
    115     return (int)floor(xx);
    116 }
    117 
    118 /** Returns the fractional part of the scalar. */
    119 static inline SkScalar SkScalarFraction(SkScalar x) {
    120     return x - SkScalarTruncToScalar(x);
    121 }
    122 
    123 static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
    124     x = SkTMin(x, max);
    125     x = SkTMax<SkScalar>(x, 0);
    126     return x;
    127 }
    128 
    129 static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
    130     return SkTPin(x, min, max);
    131 }
    132 
    133 SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
    134 
    135 static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
    136 
    137 #define SkScalarInvert(x)       (SK_Scalar1 / (x))
    138 #define SkScalarFastInvert(x)   (SK_Scalar1 / (x))
    139 #define SkScalarAve(a, b)       (((a) + (b)) * SK_ScalarHalf)
    140 #define SkScalarHalf(a)         ((a) * SK_ScalarHalf)
    141 
    142 #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
    143 #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
    144 
    145 static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
    146 static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
    147 
    148 static inline bool SkScalarIsInt(SkScalar x) {
    149     return x == SkScalarFloorToScalar(x);
    150 }
    151 
    152 /**
    153  *  Returns -1 || 0 || 1 depending on the sign of value:
    154  *  -1 if x < 0
    155  *   0 if x == 0
    156  *   1 if x > 0
    157  */
    158 static inline int SkScalarSignAsInt(SkScalar x) {
    159     return x < 0 ? -1 : (x > 0);
    160 }
    161 
    162 // Scalar result version of above
    163 static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
    164     return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
    165 }
    166 
    167 #define SK_ScalarNearlyZero         (SK_Scalar1 / (1 << 12))
    168 
    169 static inline bool SkScalarNearlyZero(SkScalar x,
    170                                       SkScalar tolerance = SK_ScalarNearlyZero) {
    171     SkASSERT(tolerance >= 0);
    172     return SkScalarAbs(x) <= tolerance;
    173 }
    174 
    175 static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
    176                                        SkScalar tolerance = SK_ScalarNearlyZero) {
    177     SkASSERT(tolerance >= 0);
    178     return SkScalarAbs(x-y) <= tolerance;
    179 }
    180 
    181 /** Linearly interpolate between A and B, based on t.
    182     If t is 0, return A
    183     If t is 1, return B
    184     else interpolate.
    185     t must be [0..SK_Scalar1]
    186 */
    187 static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
    188     SkASSERT(t >= 0 && t <= SK_Scalar1);
    189     return A + (B - A) * t;
    190 }
    191 
    192 /** Interpolate along the function described by (keys[length], values[length])
    193     for the passed searchKey.  SearchKeys outside the range keys[0]-keys[Length]
    194     clamp to the min or max value.  This function was inspired by a desire
    195     to change the multiplier for thickness in fakeBold; therefore it assumes
    196     the number of pairs (length) will be small, and a linear search is used.
    197     Repeated keys are allowed for discontinuous functions (so long as keys is
    198     monotonically increasing), and if key is the value of a repeated scalar in
    199     keys, the first one will be used.  However, that may change if a binary
    200     search is used.
    201 */
    202 SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
    203                             const SkScalar values[], int length);
    204 
    205 /*
    206  *  Helper to compare an array of scalars.
    207  */
    208 static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
    209     SkASSERT(n >= 0);
    210     for (int i = 0; i < n; ++i) {
    211         if (a[i] != b[i]) {
    212             return false;
    213         }
    214     }
    215     return true;
    216 }
    217 
    218 #endif
    219