SkScalar.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. #ifndef SkScalar_DEFINED
  8. #define SkScalar_DEFINED
  9. #include "include/private/SkFloatingPoint.h"
  10. #undef SK_SCALAR_IS_FLOAT
  11. #define SK_SCALAR_IS_FLOAT 1
  12. typedef float SkScalar;
  13. #define SK_Scalar1 1.0f
  14. #define SK_ScalarHalf 0.5f
  15. #define SK_ScalarSqrt2 SK_FloatSqrt2
  16. #define SK_ScalarPI SK_FloatPI
  17. #define SK_ScalarTanPIOver8 0.414213562f
  18. #define SK_ScalarRoot2Over2 0.707106781f
  19. #define SK_ScalarMax 3.402823466e+38f
  20. #define SK_ScalarInfinity SK_FloatInfinity
  21. #define SK_ScalarNegativeInfinity SK_FloatNegativeInfinity
  22. #define SK_ScalarNaN SK_FloatNaN
  23. #define SkScalarFloorToScalar(x) sk_float_floor(x)
  24. #define SkScalarCeilToScalar(x) sk_float_ceil(x)
  25. #define SkScalarRoundToScalar(x) sk_float_floor((x) + 0.5f)
  26. #define SkScalarTruncToScalar(x) sk_float_trunc(x)
  27. #define SkScalarFloorToInt(x) sk_float_floor2int(x)
  28. #define SkScalarCeilToInt(x) sk_float_ceil2int(x)
  29. #define SkScalarRoundToInt(x) sk_float_round2int(x)
  30. #define SkScalarAbs(x) sk_float_abs(x)
  31. #define SkScalarCopySign(x, y) sk_float_copysign(x, y)
  32. #define SkScalarMod(x, y) sk_float_mod(x,y)
  33. #define SkScalarSqrt(x) sk_float_sqrt(x)
  34. #define SkScalarPow(b, e) sk_float_pow(b, e)
  35. #define SkScalarSin(radians) (float)sk_float_sin(radians)
  36. #define SkScalarCos(radians) (float)sk_float_cos(radians)
  37. #define SkScalarTan(radians) (float)sk_float_tan(radians)
  38. #define SkScalarASin(val) (float)sk_float_asin(val)
  39. #define SkScalarACos(val) (float)sk_float_acos(val)
  40. #define SkScalarATan2(y, x) (float)sk_float_atan2(y,x)
  41. #define SkScalarExp(x) (float)sk_float_exp(x)
  42. #define SkScalarLog(x) (float)sk_float_log(x)
  43. #define SkScalarLog2(x) (float)sk_float_log2(x)
  44. //////////////////////////////////////////////////////////////////////////////////////////////////
  45. #define SkIntToScalar(x) static_cast<SkScalar>(x)
  46. #define SkIntToFloat(x) static_cast<float>(x)
  47. #define SkScalarTruncToInt(x) sk_float_saturate2int(x)
  48. #define SkScalarToFloat(x) static_cast<float>(x)
  49. #define SkFloatToScalar(x) static_cast<SkScalar>(x)
  50. #define SkScalarToDouble(x) static_cast<double>(x)
  51. #define SkDoubleToScalar(x) sk_double_to_float(x)
  52. #define SK_ScalarMin (-SK_ScalarMax)
  53. static inline bool SkScalarIsNaN(SkScalar x) { return x != x; }
  54. /** Returns true if x is not NaN and not infinite
  55. */
  56. static inline bool SkScalarIsFinite(SkScalar x) { return sk_float_isfinite(x); }
  57. static inline bool SkScalarsAreFinite(SkScalar a, SkScalar b) {
  58. return sk_floats_are_finite(a, b);
  59. }
  60. static inline bool SkScalarsAreFinite(const SkScalar array[], int count) {
  61. return sk_floats_are_finite(array, count);
  62. }
  63. /**
  64. * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using
  65. * double, to avoid possibly losing the low bit(s) of the answer before calling floor().
  66. *
  67. * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the
  68. * extra precision is known to be valuable.
  69. *
  70. * In particular, this catches the following case:
  71. * SkScalar x = 0.49999997;
  72. * int ix = SkScalarRoundToInt(x);
  73. * SkASSERT(0 == ix); // <--- fails
  74. * ix = SkDScalarRoundToInt(x);
  75. * SkASSERT(0 == ix); // <--- succeeds
  76. */
  77. static inline int SkDScalarRoundToInt(SkScalar x) {
  78. double xx = x;
  79. xx += 0.5;
  80. return (int)floor(xx);
  81. }
  82. /** Returns the fractional part of the scalar. */
  83. static inline SkScalar SkScalarFraction(SkScalar x) {
  84. return x - SkScalarTruncToScalar(x);
  85. }
  86. static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
  87. x = SkTMin(x, max);
  88. x = SkTMax<SkScalar>(x, 0);
  89. return x;
  90. }
  91. static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
  92. return SkTPin(x, min, max);
  93. }
  94. static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
  95. #define SkScalarInvert(x) sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(SK_Scalar1, (x))
  96. #define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
  97. #define SkScalarHalf(a) ((a) * SK_ScalarHalf)
  98. #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
  99. #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
  100. static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
  101. static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
  102. static inline bool SkScalarIsInt(SkScalar x) {
  103. return x == SkScalarFloorToScalar(x);
  104. }
  105. /**
  106. * Returns -1 || 0 || 1 depending on the sign of value:
  107. * -1 if x < 0
  108. * 0 if x == 0
  109. * 1 if x > 0
  110. */
  111. static inline int SkScalarSignAsInt(SkScalar x) {
  112. return x < 0 ? -1 : (x > 0);
  113. }
  114. // Scalar result version of above
  115. static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
  116. return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
  117. }
  118. #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
  119. static inline bool SkScalarNearlyZero(SkScalar x,
  120. SkScalar tolerance = SK_ScalarNearlyZero) {
  121. SkASSERT(tolerance >= 0);
  122. return SkScalarAbs(x) <= tolerance;
  123. }
  124. static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
  125. SkScalar tolerance = SK_ScalarNearlyZero) {
  126. SkASSERT(tolerance >= 0);
  127. return SkScalarAbs(x-y) <= tolerance;
  128. }
  129. static inline float SkScalarSinSnapToZero(SkScalar radians) {
  130. float v = SkScalarSin(radians);
  131. return SkScalarNearlyZero(v) ? 0.0f : v;
  132. }
  133. static inline float SkScalarCosSnapToZero(SkScalar radians) {
  134. float v = SkScalarCos(radians);
  135. return SkScalarNearlyZero(v) ? 0.0f : v;
  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. */
  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. /** Interpolate along the function described by (keys[length], values[length])
  148. for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
  149. clamp to the min or max value. This function was inspired by a desire
  150. to change the multiplier for thickness in fakeBold; therefore it assumes
  151. the number of pairs (length) will be small, and a linear search is used.
  152. Repeated keys are allowed for discontinuous functions (so long as keys is
  153. monotonically increasing), and if key is the value of a repeated scalar in
  154. keys, the first one will be used. However, that may change if a binary
  155. search is used.
  156. */
  157. SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
  158. const SkScalar values[], int length);
  159. /*
  160. * Helper to compare an array of scalars.
  161. */
  162. static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
  163. SkASSERT(n >= 0);
  164. for (int i = 0; i < n; ++i) {
  165. if (a[i] != b[i]) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. #endif