SkScalar.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "../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 1.41421356f
  16. #define SK_ScalarPI 3.14159265f
  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_float_isfinite(a) && sk_float_isfinite(b);
  59. }
  60. static inline bool SkScalarsAreFinite(const SkScalar array[], int count) {
  61. SkScalar prod = 0;
  62. for (int i = 0; i < count; ++i) {
  63. prod *= array[i];
  64. }
  65. // At this point, prod will either be NaN or 0
  66. return prod == 0; // if prod is NaN, this check will return false
  67. }
  68. /**
  69. * Variant of SkScalarRoundToInt, that performs the rounding step (adding 0.5) explicitly using
  70. * double, to avoid possibly losing the low bit(s) of the answer before calling floor().
  71. *
  72. * This routine will likely be slower than SkScalarRoundToInt(), and should only be used when the
  73. * extra precision is known to be valuable.
  74. *
  75. * In particular, this catches the following case:
  76. * SkScalar x = 0.49999997;
  77. * int ix = SkScalarRoundToInt(x);
  78. * SkASSERT(0 == ix); // <--- fails
  79. * ix = SkDScalarRoundToInt(x);
  80. * SkASSERT(0 == ix); // <--- succeeds
  81. */
  82. static inline int SkDScalarRoundToInt(SkScalar x) {
  83. double xx = x;
  84. xx += 0.5;
  85. return (int)floor(xx);
  86. }
  87. /** Returns the fractional part of the scalar. */
  88. static inline SkScalar SkScalarFraction(SkScalar x) {
  89. return x - SkScalarTruncToScalar(x);
  90. }
  91. static inline SkScalar SkScalarClampMax(SkScalar x, SkScalar max) {
  92. x = SkTMin(x, max);
  93. x = SkTMax<SkScalar>(x, 0);
  94. return x;
  95. }
  96. static inline SkScalar SkScalarPin(SkScalar x, SkScalar min, SkScalar max) {
  97. return SkTPin(x, min, max);
  98. }
  99. SkScalar SkScalarSinCos(SkScalar radians, SkScalar* cosValue);
  100. static inline SkScalar SkScalarSquare(SkScalar x) { return x * x; }
  101. #define SkScalarInvert(x) sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(SK_Scalar1, (x))
  102. #define SkScalarAve(a, b) (((a) + (b)) * SK_ScalarHalf)
  103. #define SkScalarHalf(a) ((a) * SK_ScalarHalf)
  104. #define SkDegreesToRadians(degrees) ((degrees) * (SK_ScalarPI / 180))
  105. #define SkRadiansToDegrees(radians) ((radians) * (180 / SK_ScalarPI))
  106. static inline SkScalar SkMaxScalar(SkScalar a, SkScalar b) { return a > b ? a : b; }
  107. static inline SkScalar SkMinScalar(SkScalar a, SkScalar b) { return a < b ? a : b; }
  108. static inline bool SkScalarIsInt(SkScalar x) {
  109. return x == SkScalarFloorToScalar(x);
  110. }
  111. /**
  112. * Returns -1 || 0 || 1 depending on the sign of value:
  113. * -1 if x < 0
  114. * 0 if x == 0
  115. * 1 if x > 0
  116. */
  117. static inline int SkScalarSignAsInt(SkScalar x) {
  118. return x < 0 ? -1 : (x > 0);
  119. }
  120. // Scalar result version of above
  121. static inline SkScalar SkScalarSignAsScalar(SkScalar x) {
  122. return x < 0 ? -SK_Scalar1 : ((x > 0) ? SK_Scalar1 : 0);
  123. }
  124. #define SK_ScalarNearlyZero (SK_Scalar1 / (1 << 12))
  125. static inline bool SkScalarNearlyZero(SkScalar x,
  126. SkScalar tolerance = SK_ScalarNearlyZero) {
  127. SkASSERT(tolerance >= 0);
  128. return SkScalarAbs(x) <= tolerance;
  129. }
  130. static inline bool SkScalarNearlyEqual(SkScalar x, SkScalar y,
  131. SkScalar tolerance = SK_ScalarNearlyZero) {
  132. SkASSERT(tolerance >= 0);
  133. return SkScalarAbs(x-y) <= tolerance;
  134. }
  135. /** Linearly interpolate between A and B, based on t.
  136. If t is 0, return A
  137. If t is 1, return B
  138. else interpolate.
  139. t must be [0..SK_Scalar1]
  140. */
  141. static inline SkScalar SkScalarInterp(SkScalar A, SkScalar B, SkScalar t) {
  142. SkASSERT(t >= 0 && t <= SK_Scalar1);
  143. return A + (B - A) * t;
  144. }
  145. /** Interpolate along the function described by (keys[length], values[length])
  146. for the passed searchKey. SearchKeys outside the range keys[0]-keys[Length]
  147. clamp to the min or max value. This function was inspired by a desire
  148. to change the multiplier for thickness in fakeBold; therefore it assumes
  149. the number of pairs (length) will be small, and a linear search is used.
  150. Repeated keys are allowed for discontinuous functions (so long as keys is
  151. monotonically increasing), and if key is the value of a repeated scalar in
  152. keys, the first one will be used. However, that may change if a binary
  153. search is used.
  154. */
  155. SkScalar SkScalarInterpFunc(SkScalar searchKey, const SkScalar keys[],
  156. const SkScalar values[], int length);
  157. /*
  158. * Helper to compare an array of scalars.
  159. */
  160. static inline bool SkScalarsEqual(const SkScalar a[], const SkScalar b[], int n) {
  161. SkASSERT(n >= 0);
  162. for (int i = 0; i < n; ++i) {
  163. if (a[i] != b[i]) {
  164. return false;
  165. }
  166. }
  167. return true;
  168. }
  169. #endif