2
0

MathFunctions.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. #pragma once
  35. #ifdef __GNUC__
  36. # pragma GCC system_header
  37. #endif
  38. /** @file MathFunctions.h
  39. * @brief Implementation of math utility functions.
  40. *
  41. */
  42. #include <limits>
  43. namespace Assimp {
  44. namespace Math {
  45. /// @brief Will return the greatest common divisor.
  46. /// @param a [in] Value a.
  47. /// @param b [in] Value b.
  48. /// @return The greatest common divisor.
  49. template <typename IntegerType>
  50. inline IntegerType gcd( IntegerType a, IntegerType b ) {
  51. const IntegerType zero = (IntegerType)0;
  52. while ( true ) {
  53. if ( a == zero ) {
  54. return b;
  55. }
  56. b %= a;
  57. if ( b == zero ) {
  58. return a;
  59. }
  60. a %= b;
  61. }
  62. }
  63. /// @brief Will return the greatest common divisor.
  64. /// @param a [in] Value a.
  65. /// @param b [in] Value b.
  66. /// @return The greatest common divisor.
  67. template < typename IntegerType >
  68. inline IntegerType lcm( IntegerType a, IntegerType b ) {
  69. const IntegerType t = gcd (a,b);
  70. if (!t) {
  71. return t;
  72. }
  73. return a / t * b;
  74. }
  75. /// @brief Will return the smallest epsilon-value for the requested type.
  76. /// @return The numercical limit epsilon depending on its type.
  77. template<class T>
  78. inline T getEpsilon() {
  79. return std::numeric_limits<T>::epsilon();
  80. }
  81. /// @brief Will return the constant PI for the requested type.
  82. /// @return Pi
  83. template<class T>
  84. inline T aiPi() {
  85. return static_cast<T>(3.14159265358979323846);
  86. }
  87. } // namespace Math
  88. } // namespace Assimp