random.inl 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #include "../geometric.hpp"
  2. #include "../exponential.hpp"
  3. #include "../trigonometric.hpp"
  4. #include "../detail/type_vec1.hpp"
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <cassert>
  8. #include <cmath>
  9. namespace glm{
  10. namespace detail
  11. {
  12. template <length_t L, typename T, qualifier Q>
  13. struct compute_rand
  14. {
  15. GLM_FUNC_QUALIFIER static vec<L, T, Q> call();
  16. };
  17. template <qualifier P>
  18. struct compute_rand<1, uint8, P>
  19. {
  20. GLM_FUNC_QUALIFIER static vec<1, uint8, P> call()
  21. {
  22. return vec<1, uint8, P>(
  23. std::rand() % std::numeric_limits<uint8>::max());
  24. }
  25. };
  26. template <qualifier P>
  27. struct compute_rand<2, uint8, P>
  28. {
  29. GLM_FUNC_QUALIFIER static vec<2, uint8, P> call()
  30. {
  31. return vec<2, uint8, P>(
  32. std::rand() % std::numeric_limits<uint8>::max(),
  33. std::rand() % std::numeric_limits<uint8>::max());
  34. }
  35. };
  36. template <qualifier P>
  37. struct compute_rand<3, uint8, P>
  38. {
  39. GLM_FUNC_QUALIFIER static vec<3, uint8, P> call()
  40. {
  41. return vec<3, uint8, P>(
  42. std::rand() % std::numeric_limits<uint8>::max(),
  43. std::rand() % std::numeric_limits<uint8>::max(),
  44. std::rand() % std::numeric_limits<uint8>::max());
  45. }
  46. };
  47. template <qualifier P>
  48. struct compute_rand<4, uint8, P>
  49. {
  50. GLM_FUNC_QUALIFIER static vec<4, uint8, P> call()
  51. {
  52. return vec<4, uint8, P>(
  53. std::rand() % std::numeric_limits<uint8>::max(),
  54. std::rand() % std::numeric_limits<uint8>::max(),
  55. std::rand() % std::numeric_limits<uint8>::max(),
  56. std::rand() % std::numeric_limits<uint8>::max());
  57. }
  58. };
  59. template <length_t L, qualifier Q>
  60. struct compute_rand<L, uint16, Q>
  61. {
  62. GLM_FUNC_QUALIFIER static vec<L, uint16, Q> call()
  63. {
  64. return
  65. (vec<L, uint16, Q>(compute_rand<L, uint8, Q>::call()) << static_cast<uint16>(8)) |
  66. (vec<L, uint16, Q>(compute_rand<L, uint8, Q>::call()) << static_cast<uint16>(0));
  67. }
  68. };
  69. template <length_t L, qualifier Q>
  70. struct compute_rand<L, uint32, Q>
  71. {
  72. GLM_FUNC_QUALIFIER static vec<L, uint32, Q> call()
  73. {
  74. return
  75. (vec<L, uint32, Q>(compute_rand<L, uint16, Q>::call()) << static_cast<uint32>(16)) |
  76. (vec<L, uint32, Q>(compute_rand<L, uint16, Q>::call()) << static_cast<uint32>(0));
  77. }
  78. };
  79. template <length_t L, qualifier Q>
  80. struct compute_rand<L, uint64, Q>
  81. {
  82. GLM_FUNC_QUALIFIER static vec<L, uint64, Q> call()
  83. {
  84. return
  85. (vec<L, uint64, Q>(compute_rand<L, uint32, Q>::call()) << static_cast<uint64>(32)) |
  86. (vec<L, uint64, Q>(compute_rand<L, uint32, Q>::call()) << static_cast<uint64>(0));
  87. }
  88. };
  89. template <length_t L, typename T, qualifier Q>
  90. struct compute_linearRand
  91. {
  92. GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& Min, vec<L, T, Q> const& Max);
  93. };
  94. template<length_t L, qualifier Q>
  95. struct compute_linearRand<L, int8, Q>
  96. {
  97. GLM_FUNC_QUALIFIER static vec<L, int8, Q> call(vec<L, int8, Q> const& Min, vec<L, int8, Q> const& Max)
  98. {
  99. return (vec<L, int8, Q>(compute_rand<L, uint8, Q>::call() % vec<L, uint8, Q>(Max + static_cast<int8>(1) - Min))) + Min;
  100. }
  101. };
  102. template<length_t L, qualifier Q>
  103. struct compute_linearRand<L, uint8, Q>
  104. {
  105. GLM_FUNC_QUALIFIER static vec<L, uint8, Q> call(vec<L, uint8, Q> const& Min, vec<L, uint8, Q> const& Max)
  106. {
  107. return (compute_rand<L, uint8, Q>::call() % (Max + static_cast<uint8>(1) - Min)) + Min;
  108. }
  109. };
  110. template<length_t L, qualifier Q>
  111. struct compute_linearRand<L, int16, Q>
  112. {
  113. GLM_FUNC_QUALIFIER static vec<L, int16, Q> call(vec<L, int16, Q> const& Min, vec<L, int16, Q> const& Max)
  114. {
  115. return (vec<L, int16, Q>(compute_rand<L, uint16, Q>::call() % vec<L, uint16, Q>(Max + static_cast<int16>(1) - Min))) + Min;
  116. }
  117. };
  118. template<length_t L, qualifier Q>
  119. struct compute_linearRand<L, uint16, Q>
  120. {
  121. GLM_FUNC_QUALIFIER static vec<L, uint16, Q> call(vec<L, uint16, Q> const& Min, vec<L, uint16, Q> const& Max)
  122. {
  123. return (compute_rand<L, uint16, Q>::call() % (Max + static_cast<uint16>(1) - Min)) + Min;
  124. }
  125. };
  126. template<length_t L, qualifier Q>
  127. struct compute_linearRand<L, int32, Q>
  128. {
  129. GLM_FUNC_QUALIFIER static vec<L, int32, Q> call(vec<L, int32, Q> const& Min, vec<L, int32, Q> const& Max)
  130. {
  131. return (vec<L, int32, Q>(compute_rand<L, uint32, Q>::call() % vec<L, uint32, Q>(Max + static_cast<int32>(1) - Min))) + Min;
  132. }
  133. };
  134. template<length_t L, qualifier Q>
  135. struct compute_linearRand<L, uint32, Q>
  136. {
  137. GLM_FUNC_QUALIFIER static vec<L, uint32, Q> call(vec<L, uint32, Q> const& Min, vec<L, uint32, Q> const& Max)
  138. {
  139. return (compute_rand<L, uint32, Q>::call() % (Max + static_cast<uint32>(1) - Min)) + Min;
  140. }
  141. };
  142. template<length_t L, qualifier Q>
  143. struct compute_linearRand<L, int64, Q>
  144. {
  145. GLM_FUNC_QUALIFIER static vec<L, int64, Q> call(vec<L, int64, Q> const& Min, vec<L, int64, Q> const& Max)
  146. {
  147. return (vec<L, int64, Q>(compute_rand<L, uint64, Q>::call() % vec<L, uint64, Q>(Max + static_cast<int64>(1) - Min))) + Min;
  148. }
  149. };
  150. template<length_t L, qualifier Q>
  151. struct compute_linearRand<L, uint64, Q>
  152. {
  153. GLM_FUNC_QUALIFIER static vec<L, uint64, Q> call(vec<L, uint64, Q> const& Min, vec<L, uint64, Q> const& Max)
  154. {
  155. return (compute_rand<L, uint64, Q>::call() % (Max + static_cast<uint64>(1) - Min)) + Min;
  156. }
  157. };
  158. template<length_t L, qualifier Q>
  159. struct compute_linearRand<L, float, Q>
  160. {
  161. GLM_FUNC_QUALIFIER static vec<L, float, Q> call(vec<L, float, Q> const& Min, vec<L, float, Q> const& Max)
  162. {
  163. return vec<L, float, Q>(compute_rand<L, uint32, Q>::call()) / static_cast<float>(std::numeric_limits<uint32>::max()) * (Max - Min) + Min;
  164. }
  165. };
  166. template<length_t L, qualifier Q>
  167. struct compute_linearRand<L, double, Q>
  168. {
  169. GLM_FUNC_QUALIFIER static vec<L, double, Q> call(vec<L, double, Q> const& Min, vec<L, double, Q> const& Max)
  170. {
  171. return vec<L, double, Q>(compute_rand<L, uint64, Q>::call()) / static_cast<double>(std::numeric_limits<uint64>::max()) * (Max - Min) + Min;
  172. }
  173. };
  174. template<length_t L, qualifier Q>
  175. struct compute_linearRand<L, long double, Q>
  176. {
  177. GLM_FUNC_QUALIFIER static vec<L, long double, Q> call(vec<L, long double, Q> const& Min, vec<L, long double, Q> const& Max)
  178. {
  179. return vec<L, long double, Q>(compute_rand<L, uint64, Q>::call()) / static_cast<long double>(std::numeric_limits<uint64>::max()) * (Max - Min) + Min;
  180. }
  181. };
  182. }//namespace detail
  183. template<typename genType>
  184. GLM_FUNC_QUALIFIER genType linearRand(genType Min, genType Max)
  185. {
  186. return detail::compute_linearRand<1, genType, highp>::call(
  187. vec<1, genType, highp>(Min),
  188. vec<1, genType, highp>(Max)).x;
  189. }
  190. template<length_t L, typename T, qualifier Q>
  191. GLM_FUNC_QUALIFIER vec<L, T, Q> linearRand(vec<L, T, Q> const& Min, vec<L, T, Q> const& Max)
  192. {
  193. return detail::compute_linearRand<L, T, Q>::call(Min, Max);
  194. }
  195. template<typename genType>
  196. GLM_FUNC_QUALIFIER genType gaussRand(genType Mean, genType Deviation)
  197. {
  198. genType w, x1, x2;
  199. do
  200. {
  201. x1 = linearRand(genType(-1), genType(1));
  202. x2 = linearRand(genType(-1), genType(1));
  203. w = x1 * x1 + x2 * x2;
  204. } while(w > genType(1));
  205. return x2 * Deviation * Deviation * sqrt((genType(-2) * log(w)) / w) + Mean;
  206. }
  207. template<length_t L, typename T, qualifier Q>
  208. GLM_FUNC_QUALIFIER vec<L, T, Q> gaussRand(vec<L, T, Q> const& Mean, vec<L, T, Q> const& Deviation)
  209. {
  210. return detail::functor2<vec, L, T, Q>::call(gaussRand, Mean, Deviation);
  211. }
  212. template<typename T>
  213. GLM_FUNC_QUALIFIER vec<2, T, defaultp> diskRand(T Radius)
  214. {
  215. assert(Radius > static_cast<T>(0));
  216. vec<2, T, defaultp> Result(T(0));
  217. T LenRadius(T(0));
  218. do
  219. {
  220. Result = linearRand(
  221. vec<2, T, defaultp>(-Radius),
  222. vec<2, T, defaultp>(Radius));
  223. LenRadius = length(Result);
  224. }
  225. while(LenRadius > Radius);
  226. return Result;
  227. }
  228. template<typename T>
  229. GLM_FUNC_QUALIFIER vec<3, T, defaultp> ballRand(T Radius)
  230. {
  231. assert(Radius > static_cast<T>(0));
  232. vec<3, T, defaultp> Result(T(0));
  233. T LenRadius(T(0));
  234. do
  235. {
  236. Result = linearRand(
  237. vec<3, T, defaultp>(-Radius),
  238. vec<3, T, defaultp>(Radius));
  239. LenRadius = length(Result);
  240. }
  241. while(LenRadius > Radius);
  242. return Result;
  243. }
  244. template<typename T>
  245. GLM_FUNC_QUALIFIER vec<2, T, defaultp> circularRand(T Radius)
  246. {
  247. assert(Radius > static_cast<T>(0));
  248. T a = linearRand(T(0), static_cast<T>(6.283185307179586476925286766559));
  249. return vec<2, T, defaultp>(glm::cos(a), glm::sin(a)) * Radius;
  250. }
  251. template<typename T>
  252. GLM_FUNC_QUALIFIER vec<3, T, defaultp> sphericalRand(T Radius)
  253. {
  254. assert(Radius > static_cast<T>(0));
  255. T theta = linearRand(T(0), T(6.283185307179586476925286766559f));
  256. T phi = std::acos(linearRand(T(-1.0f), T(1.0f)));
  257. T x = std::sin(phi) * std::cos(theta);
  258. T y = std::sin(phi) * std::sin(theta);
  259. T z = std::cos(phi);
  260. return vec<3, T, defaultp>(x, y, z) * Radius;
  261. }
  262. }//namespace glm