gtc_random.cpp 4.3 KB

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