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