gtx_common.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2015 G-Truc Creation (www.g-truc.net)
  5. /// Permission is hereby granted, free of charge, to any person obtaining a copy
  6. /// of this software and associated documentation files (the "Software"), to deal
  7. /// in the Software without restriction, including without limitation the rights
  8. /// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. /// copies of the Software, and to permit persons to whom the Software is
  10. /// furnished to do so, subject to the following conditions:
  11. ///
  12. /// The above copyright notice and this permission notice shall be included in
  13. /// all copies or substantial portions of the Software.
  14. ///
  15. /// Restrictions:
  16. /// By making use of the Software for military purposes, you choose to make
  17. /// a Bunny unhappy.
  18. ///
  19. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. /// THE SOFTWARE.
  26. ///
  27. /// @file test/gtx/gtx_common.cpp
  28. /// @date 2014-09-08 / 2014-11-25
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/gtx/common.hpp>
  32. #include <glm/gtc/integer.hpp>
  33. #include <glm/gtc/epsilon.hpp>
  34. #include <glm/vector_relational.hpp>
  35. #include <glm/common.hpp>
  36. namespace fmod_
  37. {
  38. template <typename genType>
  39. GLM_FUNC_QUALIFIER genType modTrunc(genType a, genType b)
  40. {
  41. return a - b * trunc(a / b);
  42. }
  43. int test()
  44. {
  45. int Error(0);
  46. {
  47. float A0(3.0);
  48. float B0(2.0f);
  49. float C0 = glm::fmod(A0, B0);
  50. Error += glm::abs(C0 - 1.0f) < 0.00001f ? 0 : 1;
  51. glm::vec4 A1(3.0);
  52. float B1(2.0f);
  53. glm::vec4 C1 = glm::fmod(A1, B1);
  54. Error += glm::all(glm::epsilonEqual(C1, glm::vec4(1.0f), 0.00001f)) ? 0 : 1;
  55. glm::vec4 A2(3.0);
  56. glm::vec4 B2(2.0f);
  57. glm::vec4 C2 = glm::fmod(A2, B2);
  58. Error += glm::all(glm::epsilonEqual(C2, glm::vec4(1.0f), 0.00001f)) ? 0 : 1;
  59. glm::ivec4 A3(3);
  60. int B3(2);
  61. glm::ivec4 C3 = glm::fmod(A3, B3);
  62. Error += glm::all(glm::equal(C3, glm::ivec4(1))) ? 0 : 1;
  63. glm::ivec4 A4(3);
  64. glm::ivec4 B4(2);
  65. glm::ivec4 C4 = glm::fmod(A4, B4);
  66. Error += glm::all(glm::equal(C4, glm::ivec4(1))) ? 0 : 1;
  67. }
  68. {
  69. float A0(22.0);
  70. float B0(-10.0f);
  71. float C0 = glm::fmod(A0, B0);
  72. Error += glm::abs(C0 - 2.0f) < 0.00001f ? 0 : 1;
  73. glm::vec4 A1(22.0);
  74. float B1(-10.0f);
  75. glm::vec4 C1 = glm::fmod(A1, B1);
  76. Error += glm::all(glm::epsilonEqual(C1, glm::vec4(2.0f), 0.00001f)) ? 0 : 1;
  77. glm::vec4 A2(22.0);
  78. glm::vec4 B2(-10.0f);
  79. glm::vec4 C2 = glm::fmod(A2, B2);
  80. Error += glm::all(glm::epsilonEqual(C2, glm::vec4(2.0f), 0.00001f)) ? 0 : 1;
  81. glm::ivec4 A3(22);
  82. int B3(-10);
  83. glm::ivec4 C3 = glm::fmod(A3, B3);
  84. Error += glm::all(glm::equal(C3, glm::ivec4(2))) ? 0 : 1;
  85. glm::ivec4 A4(22);
  86. glm::ivec4 B4(-10);
  87. glm::ivec4 C4 = glm::fmod(A4, B4);
  88. Error += glm::all(glm::equal(C4, glm::ivec4(2))) ? 0 : 1;
  89. }
  90. // http://stackoverflow.com/questions/7610631/glsl-mod-vs-hlsl-fmod
  91. {
  92. for (float y = -10.0f; y < 10.0f; y += 0.1f)
  93. for (float x = -10.0f; x < 10.0f; x += 0.1f)
  94. {
  95. float const A(std::fmod(x, y));
  96. //float const B(std::remainder(x, y));
  97. float const C(glm::fmod(x, y));
  98. float const D(modTrunc(x, y));
  99. //Error += glm::epsilonEqual(A, B, 0.0001f) ? 0 : 1;
  100. //assert(!Error);
  101. Error += glm::epsilonEqual(A, C, 0.0001f) ? 0 : 1;
  102. assert(!Error);
  103. Error += glm::epsilonEqual(A, D, 0.00001f) ? 0 : 1;
  104. assert(!Error);
  105. }
  106. }
  107. return Error;
  108. }
  109. }//namespace fmod_
  110. int test_isdenormal()
  111. {
  112. int Error(0);
  113. bool A = glm::isdenormal(1.0f);
  114. glm::bvec1 B = glm::isdenormal(glm::vec1(1.0f));
  115. glm::bvec2 C = glm::isdenormal(glm::vec2(1.0f));
  116. glm::bvec3 D = glm::isdenormal(glm::vec3(1.0f));
  117. glm::bvec4 E = glm::isdenormal(glm::vec4(1.0f));
  118. return Error;
  119. }
  120. int main()
  121. {
  122. int Error(0);
  123. Error += test_isdenormal();
  124. Error += ::fmod_::test();
  125. return Error;
  126. }