gtc_random.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2014 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2011-09-19
  5. // Updated : 2011-09-19
  6. // Licence : This source is under MIT licence
  7. // File : test/gtc/random.cpp
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. #include <glm/gtc/random.hpp>
  10. #include <glm/gtc/epsilon.hpp>
  11. #include <iostream>
  12. #if(GLM_LANG & GLM_LANG_CXX0X_FLAG)
  13. # include <array>
  14. #endif
  15. int test_linearRand()
  16. {
  17. int Error = 0;
  18. {
  19. float ResultFloat = 0.0f;
  20. double ResultDouble = 0.0f;
  21. for(std::size_t i = 0; i < 100000; ++i)
  22. {
  23. ResultFloat += glm::linearRand(-1.0f, 1.0f);
  24. ResultDouble += glm::linearRand(-1.0, 1.0);
  25. }
  26. Error += glm::epsilonEqual(ResultFloat, 0.0f, 0.0001f);
  27. Error += glm::epsilonEqual(ResultDouble, 0.0, 0.0001);
  28. assert(!Error);
  29. }
  30. return Error;
  31. }
  32. int test_circularRand()
  33. {
  34. int Error = 0;
  35. {
  36. std::size_t Max = 100000;
  37. float ResultFloat = 0.0f;
  38. double ResultDouble = 0.0f;
  39. double Radius = 2.0f;
  40. for(std::size_t i = 0; i < Max; ++i)
  41. {
  42. ResultFloat += glm::length(glm::circularRand(1.0f));
  43. ResultDouble += glm::length(glm::circularRand(Radius));
  44. }
  45. Error += glm::epsilonEqual(ResultFloat, float(Max), 0.01f) ? 0 : 1;
  46. Error += glm::epsilonEqual(ResultDouble, double(Max) * double(Radius), 0.01) ? 0 : 1;
  47. assert(!Error);
  48. }
  49. return Error;
  50. }
  51. int test_sphericalRand()
  52. {
  53. int Error = 0;
  54. {
  55. std::size_t Max = 100000;
  56. float ResultFloatA = 0.0f;
  57. float ResultFloatB = 0.0f;
  58. float ResultFloatC = 0.0f;
  59. double ResultDoubleA = 0.0f;
  60. double ResultDoubleB = 0.0f;
  61. double ResultDoubleC = 0.0f;
  62. for(std::size_t i = 0; i < Max; ++i)
  63. {
  64. ResultFloatA += glm::length(glm::sphericalRand(1.0f));
  65. ResultDoubleA += glm::length(glm::sphericalRand(1.0));
  66. ResultFloatB += glm::length(glm::sphericalRand(2.0f));
  67. ResultDoubleB += glm::length(glm::sphericalRand(2.0));
  68. ResultFloatC += glm::length(glm::sphericalRand(3.0f));
  69. ResultDoubleC += glm::length(glm::sphericalRand(3.0));
  70. }
  71. Error += glm::epsilonEqual(ResultFloatA, float(Max), 0.01f) ? 0 : 1;
  72. Error += glm::epsilonEqual(ResultDoubleA, double(Max), 0.0001) ? 0 : 1;
  73. Error += glm::epsilonEqual(ResultFloatB, float(Max * 2), 0.01f) ? 0 : 1;
  74. Error += glm::epsilonEqual(ResultDoubleB, double(Max * 2), 0.0001) ? 0 : 1;
  75. Error += glm::epsilonEqual(ResultFloatC, float(Max * 3), 0.01f) ? 0 : 1;
  76. Error += glm::epsilonEqual(ResultDoubleC, double(Max * 3), 0.01) ? 0 : 1;
  77. assert(!Error);
  78. }
  79. return Error;
  80. }
  81. int test_diskRand()
  82. {
  83. int Error = 0;
  84. {
  85. float ResultFloat = 0.0f;
  86. double ResultDouble = 0.0f;
  87. for(std::size_t i = 0; i < 100000; ++i)
  88. {
  89. ResultFloat += glm::length(glm::diskRand(2.0f));
  90. ResultDouble += glm::length(glm::diskRand(2.0));
  91. }
  92. Error += ResultFloat < 200000.f ? 0 : 1;
  93. Error += ResultDouble < 200000.0 ? 0 : 1;
  94. assert(!Error);
  95. }
  96. return Error;
  97. }
  98. int test_ballRand()
  99. {
  100. int Error = 0;
  101. {
  102. float ResultFloat = 0.0f;
  103. double ResultDouble = 0.0f;
  104. for(std::size_t i = 0; i < 100000; ++i)
  105. {
  106. ResultFloat += glm::length(glm::ballRand(2.0f));
  107. ResultDouble += glm::length(glm::ballRand(2.0));
  108. }
  109. Error += ResultFloat < 200000.f ? 0 : 1;
  110. Error += ResultDouble < 200000.0 ? 0 : 1;
  111. assert(!Error);
  112. }
  113. return Error;
  114. }
  115. /*
  116. #if(GLM_LANG & GLM_LANG_CXX0X_FLAG)
  117. int test_grid()
  118. {
  119. int Error = 0;
  120. typedef std::array<int, 8> colors;
  121. typedef std::array<int, 8 * 8> grid;
  122. grid Grid;
  123. colors Colors;
  124. grid GridBest;
  125. colors ColorsBest;
  126. while(true)
  127. {
  128. for(std::size_t i = 0; i < Grid.size(); ++i)
  129. Grid[i] = int(glm::linearRand(0.0, 8.0 * 8.0 * 8.0 - 1.0) / 64.0);
  130. for(std::size_t i = 0; i < Grid.size(); ++i)
  131. ++Colors[Grid[i]];
  132. bool Exit = true;
  133. for(std::size_t i = 0; i < Colors.size(); ++i)
  134. {
  135. if(Colors[i] == 8)
  136. continue;
  137. Exit = false;
  138. break;
  139. }
  140. if(Exit == true)
  141. break;
  142. }
  143. return Error;
  144. }
  145. #endif
  146. */
  147. int main()
  148. {
  149. int Error = 0;
  150. Error += test_linearRand();
  151. Error += test_circularRand();
  152. Error += test_sphericalRand();
  153. Error += test_diskRand();
  154. Error += test_ballRand();
  155. /*
  156. #if(GLM_LANG & GLM_LANG_CXX0X_FLAG)
  157. Error += test_grid();
  158. #endif
  159. */
  160. return Error;
  161. }