core_func_geometric.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/core/func_geometric.cpp
  28. /// @date 2011-01-15 / 2011-09-13
  29. /// @author Christophe Riccio
  30. ///////////////////////////////////////////////////////////////////////////////////
  31. #include <glm/geometric.hpp>
  32. #include <glm/vector_relational.hpp>
  33. #include <glm/gtc/epsilon.hpp>
  34. #include <glm/gtc/vec1.hpp>
  35. #include <limits>
  36. namespace length
  37. {
  38. int test()
  39. {
  40. float Length1 = glm::length(glm::vec1(1));
  41. float Length2 = glm::length(glm::vec2(1, 0));
  42. float Length3 = glm::length(glm::vec3(1, 0, 0));
  43. float Length4 = glm::length(glm::vec4(1, 0, 0, 0));
  44. int Error = 0;
  45. Error += glm::abs(Length1 - 1.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  46. Error += glm::abs(Length2 - 1.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  47. Error += glm::abs(Length3 - 1.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  48. Error += glm::abs(Length4 - 1.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  49. return Error;
  50. }
  51. }//namespace length
  52. namespace distance
  53. {
  54. int test()
  55. {
  56. float Distance1 = glm::distance(glm::vec1(1), glm::vec1(1));
  57. float Distance2 = glm::distance(glm::vec2(1, 0), glm::vec2(1, 0));
  58. float Distance3 = glm::distance(glm::vec3(1, 0, 0), glm::vec3(1, 0, 0));
  59. float Distance4 = glm::distance(glm::vec4(1, 0, 0, 0), glm::vec4(1, 0, 0, 0));
  60. int Error = 0;
  61. Error += glm::abs(Distance1) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  62. Error += glm::abs(Distance2) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  63. Error += glm::abs(Distance3) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  64. Error += glm::abs(Distance4) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  65. return Error;
  66. }
  67. }//namespace distance
  68. namespace dot
  69. {
  70. int test()
  71. {
  72. float Dot1 = glm::dot(glm::vec1(1), glm::vec1(1));
  73. float Dot2 = glm::dot(glm::vec2(1), glm::vec2(1));
  74. float Dot3 = glm::dot(glm::vec3(1), glm::vec3(1));
  75. float Dot4 = glm::dot(glm::vec4(1), glm::vec4(1));
  76. int Error = 0;
  77. Error += glm::abs(Dot1 - 1.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  78. Error += glm::abs(Dot2 - 2.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  79. Error += glm::abs(Dot3 - 3.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  80. Error += glm::abs(Dot4 - 4.0f) < std::numeric_limits<float>::epsilon() ? 0 : 1;
  81. return Error;
  82. }
  83. }//namespace dot
  84. namespace cross
  85. {
  86. int test()
  87. {
  88. glm::vec3 Cross1 = glm::cross(glm::vec3(1, 0, 0), glm::vec3(0, 1, 0));
  89. glm::vec3 Cross2 = glm::cross(glm::vec3(0, 1, 0), glm::vec3(1, 0, 0));
  90. int Error = 0;
  91. Error += glm::all(glm::lessThan(glm::abs(Cross1 - glm::vec3(0, 0, 1)), glm::vec3(std::numeric_limits<float>::epsilon()))) ? 0 : 1;
  92. Error += glm::all(glm::lessThan(glm::abs(Cross2 - glm::vec3(0, 0,-1)), glm::vec3(std::numeric_limits<float>::epsilon()))) ? 0 : 1;
  93. return Error;
  94. }
  95. }//namespace cross
  96. namespace normalize
  97. {
  98. int test()
  99. {
  100. glm::vec3 Normalize1 = glm::normalize(glm::vec3(1, 0, 0));
  101. glm::vec3 Normalize2 = glm::normalize(glm::vec3(2, 0, 0));
  102. int Error = 0;
  103. Error += glm::all(glm::lessThan(glm::abs(Normalize1 - glm::vec3(1, 0, 0)), glm::vec3(std::numeric_limits<float>::epsilon()))) ? 0 : 1;
  104. Error += glm::all(glm::lessThan(glm::abs(Normalize2 - glm::vec3(1, 0, 0)), glm::vec3(std::numeric_limits<float>::epsilon()))) ? 0 : 1;
  105. return Error;
  106. }
  107. }//namespace normalize
  108. namespace faceforward
  109. {
  110. int test()
  111. {
  112. int Error = 0;
  113. {
  114. glm::vec3 N(0.0f, 0.0f, 1.0f);
  115. glm::vec3 I(1.0f, 0.0f, 1.0f);
  116. glm::vec3 Nref(0.0f, 0.0f, 1.0f);
  117. glm::vec3 F = glm::faceforward(N, I, Nref);
  118. }
  119. return Error;
  120. }
  121. }//namespace faceforward
  122. namespace reflect
  123. {
  124. int test()
  125. {
  126. int Error = 0;
  127. {
  128. glm::vec2 A(1.0f,-1.0f);
  129. glm::vec2 B(0.0f, 1.0f);
  130. glm::vec2 C = glm::reflect(A, B);
  131. Error += C == glm::vec2(1.0, 1.0) ? 0 : 1;
  132. }
  133. {
  134. glm::dvec2 A(1.0f,-1.0f);
  135. glm::dvec2 B(0.0f, 1.0f);
  136. glm::dvec2 C = glm::reflect(A, B);
  137. Error += C == glm::dvec2(1.0, 1.0) ? 0 : 1;
  138. }
  139. return Error;
  140. }
  141. }//namespace reflect
  142. namespace refract
  143. {
  144. int test()
  145. {
  146. int Error = 0;
  147. {
  148. float A(-1.0f);
  149. float B(1.0f);
  150. float C = glm::refract(A, B, 0.5f);
  151. Error += C == -1.0f ? 0 : 1;
  152. }
  153. {
  154. glm::vec2 A(0.0f,-1.0f);
  155. glm::vec2 B(0.0f, 1.0f);
  156. glm::vec2 C = glm::refract(A, B, 0.5f);
  157. Error += glm::all(glm::epsilonEqual(C, glm::vec2(0.0, -1.0), 0.0001f)) ? 0 : 1;
  158. }
  159. {
  160. glm::dvec2 A(0.0f,-1.0f);
  161. glm::dvec2 B(0.0f, 1.0f);
  162. glm::dvec2 C = glm::refract(A, B, 0.5);
  163. Error += C == glm::dvec2(0.0, -1.0) ? 0 : 1;
  164. }
  165. return Error;
  166. }
  167. }//namespace refract
  168. int main()
  169. {
  170. int Error(0);
  171. Error += length::test();
  172. Error += distance::test();
  173. Error += dot::test();
  174. Error += cross::test();
  175. Error += normalize::test();
  176. Error += faceforward::test();
  177. Error += reflect::test();
  178. Error += refract::test();
  179. return Error;
  180. }