gtc_random.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2011 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/glm.hpp>
  10. #include <glm/gtc/random.hpp>
  11. #include <glm/gtx/epsilon.hpp>
  12. #include <iostream>
  13. int test_linearRand()
  14. {
  15. int Error = 0;
  16. {
  17. float ResultFloat = 0.0f;
  18. double ResultDouble = 0.0f;
  19. for(std::size_t i = 0; i < 100000; ++i)
  20. {
  21. ResultFloat += glm::linearRand(-1.0f, 1.0f);
  22. ResultDouble += glm::linearRand(-1.0, 1.0);
  23. }
  24. Error += glm::equalEpsilon(ResultFloat, 0.0f, 0.0001f);
  25. Error += glm::equalEpsilon(ResultDouble, 0.0, 0.0001);
  26. assert(!Error);
  27. }
  28. return Error;
  29. }
  30. int test_circularRand()
  31. {
  32. int Error = 0;
  33. {
  34. std::size_t Max = 100000;
  35. float ResultFloat = 0.0f;
  36. double ResultDouble = 0.0f;
  37. double Radius = 2.0f;
  38. for(std::size_t i = 0; i < Max; ++i)
  39. {
  40. ResultFloat += glm::length(glm::circularRand(1.0f));
  41. ResultDouble += glm::length(glm::circularRand(Radius));
  42. }
  43. Error += glm::equalEpsilon(ResultFloat, float(Max), 0.01f) ? 0 : 1;
  44. Error += glm::equalEpsilon(ResultDouble, double(Max) * double(Radius), 0.01) ? 0 : 1;
  45. assert(!Error);
  46. }
  47. return Error;
  48. }
  49. int test_sphericalRand()
  50. {
  51. int Error = 0;
  52. {
  53. std::size_t Max = 100000;
  54. float ResultFloatA = 0.0f;
  55. float ResultFloatB = 0.0f;
  56. float ResultFloatC = 0.0f;
  57. double ResultDoubleA = 0.0f;
  58. double ResultDoubleB = 0.0f;
  59. double ResultDoubleC = 0.0f;
  60. for(std::size_t i = 0; i < Max; ++i)
  61. {
  62. ResultFloatA += glm::length(glm::sphericalRand(1.0f));
  63. ResultDoubleA += glm::length(glm::sphericalRand(1.0));
  64. ResultFloatB += glm::length(glm::sphericalRand(2.0f));
  65. ResultDoubleB += glm::length(glm::sphericalRand(2.0));
  66. ResultFloatC += glm::length(glm::sphericalRand(3.0f));
  67. ResultDoubleC += glm::length(glm::sphericalRand(3.0));
  68. }
  69. Error += glm::equalEpsilon(ResultFloatA, float(Max), 0.01f) ? 0 : 1;
  70. Error += glm::equalEpsilon(ResultDoubleA, double(Max), 0.0001) ? 0 : 1;
  71. Error += glm::equalEpsilon(ResultFloatB, float(Max * 2), 0.01f) ? 0 : 1;
  72. Error += glm::equalEpsilon(ResultDoubleB, double(Max * 2), 0.0001) ? 0 : 1;
  73. Error += glm::equalEpsilon(ResultFloatC, float(Max * 3), 0.01f) ? 0 : 1;
  74. Error += glm::equalEpsilon(ResultDoubleC, double(Max * 3), 0.01) ? 0 : 1;
  75. assert(!Error);
  76. }
  77. return Error;
  78. }
  79. int test_diskRand()
  80. {
  81. int Error = 0;
  82. {
  83. float ResultFloat = 0.0f;
  84. double ResultDouble = 0.0f;
  85. for(std::size_t i = 0; i < 100000; ++i)
  86. {
  87. ResultFloat += glm::length(glm::diskRand(2.0f));
  88. ResultDouble += glm::length(glm::diskRand(2.0));
  89. }
  90. Error += ResultFloat < 200000.f ? 0 : 1;
  91. Error += ResultDouble < 200000.0 ? 0 : 1;
  92. assert(!Error);
  93. }
  94. return Error;
  95. }
  96. int test_ballRand()
  97. {
  98. int Error = 0;
  99. {
  100. float ResultFloat = 0.0f;
  101. double ResultDouble = 0.0f;
  102. for(std::size_t i = 0; i < 100000; ++i)
  103. {
  104. ResultFloat += glm::length(glm::ballRand(2.0f));
  105. ResultDouble += glm::length(glm::ballRand(2.0));
  106. }
  107. Error += ResultFloat < 200000.f ? 0 : 1;
  108. Error += ResultDouble < 200000.0 ? 0 : 1;
  109. assert(!Error);
  110. }
  111. return Error;
  112. }
  113. int main()
  114. {
  115. int Error = 0;
  116. Error += test_linearRand();
  117. Error += test_circularRand();
  118. Error += test_sphericalRand();
  119. Error += test_diskRand();
  120. Error += test_ballRand();
  121. return Error;
  122. }