Collection of DPF-based plugins for packaging
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.

299 lines
8.4KB

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