/src/core/MathUtils.h

http://github.com/imageworks/OpenColorIO · C Header · 189 lines · 82 code · 36 blank · 71 comment · 0 complexity · 1fcc6b9cd86cba2697c47a18090773ff MD5 · raw file

  1. /*
  2. Copyright (c) 2003-2010 Sony Pictures Imageworks Inc., et al.
  3. All Rights Reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Sony Pictures Imageworks nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef INCLUDED_OCIO_MATHUTILS_H
  28. #define INCLUDED_OCIO_MATHUTILS_H
  29. #include <OpenColorIO/OpenColorIO.h>
  30. #include <cmath>
  31. #include <vector>
  32. #include "Op.h"
  33. #include "Platform.h"
  34. #ifdef WINDOWS
  35. #include <float.h>
  36. #endif
  37. OCIO_NAMESPACE_ENTER
  38. {
  39. // From Imath
  40. //--------------------------------------------------------------------------
  41. // Compare two numbers and test if they are "approximately equal":
  42. //
  43. // equalWithAbsError (x1, x2, e)
  44. //
  45. // Returns true if x1 is the same as x2 with an absolute error of
  46. // no more than e,
  47. //
  48. // abs (x1 - x2) <= e
  49. //
  50. // equalWithRelError (x1, x2, e)
  51. //
  52. // Returns true if x1 is the same as x2 with an relative error of
  53. // no more than e,
  54. //
  55. // abs (x1 - x2) <= e * x1
  56. //
  57. //--------------------------------------------------------------------------
  58. inline bool equalWithAbsError (float x1, float x2, float e)
  59. {
  60. return ((x1 > x2)? x1 - x2: x2 - x1) <= e;
  61. }
  62. inline bool equalWithRelError (float x1, float x2, float e)
  63. {
  64. return ((x1 > x2)? x1 - x2: x2 - x1) <= e * ((x1 > 0)? x1: -x1);
  65. }
  66. inline float lerpf(float a, float b, float z)
  67. {
  68. return (b - a) * z + a;
  69. }
  70. #ifdef WINDOWS
  71. inline double
  72. round (float val) {
  73. return floor (val + 0.5);
  74. }
  75. inline float
  76. roundf (float val) {
  77. return static_cast<float>(round (val));
  78. }
  79. inline int
  80. isnan (float val) {
  81. // Windows uses a non-standard version of 'isnan'
  82. return _isnan (val);
  83. }
  84. #else
  85. #ifdef ANDROID
  86. // support std::isnan - needs to be tested as it might not be part of the NDK
  87. #define _GLIBCXX_USE_C99_MATH 1
  88. #endif
  89. // This lets all platforms just use isnan, within the OCIO namespace,
  90. // across all platforms. (Windows defines the function above).
  91. using std::isnan;
  92. #endif
  93. // Checks within fltmin tolerance
  94. bool IsScalarEqualToZero(float v);
  95. bool IsScalarEqualToOne(float v);
  96. bool IsScalarEqualToZeroFlt(double v);
  97. bool IsScalarEqualToOneFlt(double v);
  98. // Are all the vector components the specified value?
  99. bool IsVecEqualToZero(const float* v, int size);
  100. bool IsVecEqualToOne(const float* v, int size);
  101. bool IsVecEqualToOneFlt(const double* v, int size);
  102. // Is at least one of the specified components equal to 0?
  103. bool VecContainsZero(const float* v, int size);
  104. bool VecContainsOne(const float* v, int size);
  105. // Are two vectors equal? (Same size, same values?)
  106. bool VecsEqualWithRelError(const float* v1, int size1,
  107. const float* v2, int size2,
  108. float e);
  109. inline double GetHalfMax()
  110. {
  111. return 65504.0; // Largest positive half
  112. }
  113. inline double GetHalfMin()
  114. {
  115. return 5.96046448e-08; // Smallest positive half;
  116. }
  117. inline double GetHalfNormMin()
  118. {
  119. return 6.10351562e-05; // Smallest positive normalized half
  120. }
  121. //! Clamp the specified value to the valid range of normalized half.
  122. // (can be either positive or negative though
  123. double ClampToNormHalf(double val);
  124. float GetSafeScalarInverse(float v, float defaultValue = 1.0);
  125. // All matrix / vector operations use the following sizing...
  126. //
  127. // m : 4x4 matrix
  128. // v : 4 column vector
  129. // Return the 4x4 inverse, and whether the inverse has succeeded.
  130. // Supports in-place operations
  131. bool GetM44Inverse(float* mout, const float* m);
  132. // Is an identity matrix? (with fltmin tolerance)
  133. bool IsM44Identity(const float* m);
  134. // Is this a purely diagonal matrix?
  135. bool IsM44Diagonal(const float* m);
  136. // Extract the diagonal
  137. void GetM44Diagonal(float* vout, const float* m);
  138. // Get the product, out = m1*m2
  139. // Supports in-place operations
  140. void GetM44Product(float* mout, const float* m1, const float* m2);
  141. // Combine two transforms in the mx+b form, into a single transform
  142. // mout*x+vout == m2*(m1*x+v1)+v2
  143. // Supports in-place operations
  144. void GetMxbCombine(float* mout, float* vout,
  145. const float* m1, const float* v1,
  146. const float* m2, const float* v2);
  147. // Supports in-place operations
  148. bool GetMxbInverse(float* mout, float* vout,
  149. const float* m, const float* v);
  150. }
  151. OCIO_NAMESPACE_EXIT
  152. #endif