You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.5 KiB

5 years ago
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. /// OpenGL Mathematics (glm.g-truc.net)
  3. ///
  4. /// Copyright (c) 2005 - 2013 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. /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. /// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. /// THE SOFTWARE.
  22. ///
  23. /// @ref gtc_random
  24. /// @file glm/gtc/random.inl
  25. /// @date 2011-09-19 / 2012-04-07
  26. /// @author Christophe Riccio
  27. ///////////////////////////////////////////////////////////////////////////////////
  28. #include <ctime>
  29. #include <cassert>
  30. namespace glm{
  31. namespace detail
  32. {
  33. struct compute_linearRand
  34. {
  35. template <typename T>
  36. GLM_FUNC_QUALIFIER T operator() (T const & Min, T const & Max) const;
  37. /*
  38. {
  39. GLM_STATIC_ASSERT(0, "'linearRand' invalid template parameter type. GLM_GTC_random only supports floating-point template types.");
  40. return Min;
  41. }
  42. */
  43. };
  44. template <>
  45. GLM_FUNC_QUALIFIER half compute_linearRand::operator()<half> (half const & Min, half const & Max) const
  46. {
  47. return half(float(std::rand()) / float(RAND_MAX) * (float(Max) - float(Min)) + float(Min));
  48. }
  49. template <>
  50. GLM_FUNC_QUALIFIER float compute_linearRand::operator()<float> (float const & Min, float const & Max) const
  51. {
  52. return float(std::rand()) / float(RAND_MAX) * (Max - Min) + Min;
  53. }
  54. template <>
  55. GLM_FUNC_QUALIFIER double compute_linearRand::operator()<double> (double const & Min, double const & Max) const
  56. {
  57. return double(std::rand()) / double(RAND_MAX) * (Max - Min) + Min;
  58. }
  59. template <>
  60. GLM_FUNC_QUALIFIER long double compute_linearRand::operator()<long double> (long double const & Min, long double const & Max) const
  61. {
  62. return (long double)(std::rand()) / (long double)(RAND_MAX) * (Max - Min) + Min;
  63. }
  64. }//namespace detail
  65. template <typename genType>
  66. GLM_FUNC_QUALIFIER genType linearRand
  67. (
  68. genType const & Min,
  69. genType const & Max
  70. )
  71. {
  72. return detail::compute_linearRand()(Min, Max);
  73. }
  74. VECTORIZE_VEC_VEC(linearRand)
  75. template <typename genType>
  76. GLM_FUNC_QUALIFIER genType gaussRand
  77. (
  78. genType const & Mean,
  79. genType const & Deviation
  80. )
  81. {
  82. genType w, x1, x2;
  83. do
  84. {
  85. x1 = linearRand(genType(-1), genType(1));
  86. x2 = linearRand(genType(-1), genType(1));
  87. w = x1 * x1 + x2 * x2;
  88. } while(w > genType(1));
  89. return x2 * Deviation * Deviation * sqrt((genType(-2) * log(w)) / w) + Mean;
  90. }
  91. VECTORIZE_VEC_VEC(gaussRand)
  92. template <typename T>
  93. GLM_FUNC_QUALIFIER detail::tvec2<T> diskRand
  94. (
  95. T const & Radius
  96. )
  97. {
  98. detail::tvec2<T> Result(T(0));
  99. T LenRadius(T(0));
  100. do
  101. {
  102. Result = linearRand(detail::tvec2<T>(-Radius), detail::tvec2<T>(Radius));
  103. LenRadius = length(Result);
  104. }
  105. while(LenRadius > Radius);
  106. return Result;
  107. }
  108. template <typename T>
  109. GLM_FUNC_QUALIFIER detail::tvec3<T> ballRand
  110. (
  111. T const & Radius
  112. )
  113. {
  114. detail::tvec3<T> Result(T(0));
  115. T LenRadius(T(0));
  116. do
  117. {
  118. Result = linearRand(detail::tvec3<T>(-Radius), detail::tvec3<T>(Radius));
  119. LenRadius = length(Result);
  120. }
  121. while(LenRadius > Radius);
  122. return Result;
  123. }
  124. template <typename T>
  125. GLM_FUNC_QUALIFIER detail::tvec2<T> circularRand
  126. (
  127. T const & Radius
  128. )
  129. {
  130. T a = linearRand(T(0), T(6.283185307179586476925286766559f));
  131. return detail::tvec2<T>(cos(a), sin(a)) * Radius;
  132. }
  133. template <typename T>
  134. GLM_FUNC_QUALIFIER detail::tvec3<T> sphericalRand
  135. (
  136. T const & Radius
  137. )
  138. {
  139. T z = linearRand(T(-1), T(1));
  140. T a = linearRand(T(0), T(6.283185307179586476925286766559f));
  141. T r = sqrt(T(1) - z * z);
  142. T x = r * cos(a);
  143. T y = r * sin(a);
  144. return detail::tvec3<T>(x, y, z) * Radius;
  145. }
  146. }//namespace glm