/// @ref gtc_random /// @file glm/gtc/random.inl #include "../geometric.hpp" #include "../exponential.hpp" #include "../trigonometric.hpp" #include "../ext/vec1.hpp" #include #include #include #include namespace glm{ namespace detail { template struct compute_rand { GLM_FUNC_QUALIFIER static vec call(); }; template struct compute_rand<1, uint8, P> { GLM_FUNC_QUALIFIER static vec<1, uint8, P> call() { return vec<1, uint8, P>( std::rand() % std::numeric_limits::max()); } }; template struct compute_rand<2, uint8, P> { GLM_FUNC_QUALIFIER static vec<2, uint8, P> call() { return vec<2, uint8, P>( std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max()); } }; template struct compute_rand<3, uint8, P> { GLM_FUNC_QUALIFIER static vec<3, uint8, P> call() { return vec<3, uint8, P>( std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max()); } }; template struct compute_rand<4, uint8, P> { GLM_FUNC_QUALIFIER static vec<4, uint8, P> call() { return vec<4, uint8, P>( std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max()); } }; template struct compute_rand { GLM_FUNC_QUALIFIER static vec call() { return (vec(compute_rand::call()) << static_cast(8)) | (vec(compute_rand::call()) << static_cast(0)); } }; template struct compute_rand { GLM_FUNC_QUALIFIER static vec call() { return (vec(compute_rand::call()) << static_cast(16)) | (vec(compute_rand::call()) << static_cast(0)); } }; template struct compute_rand { GLM_FUNC_QUALIFIER static vec call() { return (vec(compute_rand::call()) << static_cast(32)) | (vec(compute_rand::call()) << static_cast(0)); } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max); }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (vec(compute_rand::call() % vec(Max + static_cast(1) - Min))) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (compute_rand::call() % (Max + static_cast(1) - Min)) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (vec(compute_rand::call() % vec(Max + static_cast(1) - Min))) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (compute_rand::call() % (Max + static_cast(1) - Min)) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (vec(compute_rand::call() % vec(Max + static_cast(1) - Min))) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (compute_rand::call() % (Max + static_cast(1) - Min)) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (vec(compute_rand::call() % vec(Max + static_cast(1) - Min))) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return (compute_rand::call() % (Max + static_cast(1) - Min)) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return vec(compute_rand::call()) / static_cast(std::numeric_limits::max()) * (Max - Min) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return vec(compute_rand::call()) / static_cast(std::numeric_limits::max()) * (Max - Min) + Min; } }; template struct compute_linearRand { GLM_FUNC_QUALIFIER static vec call(vec const& Min, vec const& Max) { return vec(compute_rand::call()) / static_cast(std::numeric_limits::max()) * (Max - Min) + Min; } }; }//namespace detail template GLM_FUNC_QUALIFIER genType linearRand(genType Min, genType Max) { return detail::compute_linearRand<1, genType, highp>::call( vec<1, genType, highp>(Min), vec<1, genType, highp>(Max)).x; } template GLM_FUNC_QUALIFIER vec linearRand(vec const& Min, vec const& Max) { return detail::compute_linearRand::call(Min, Max); } template GLM_FUNC_QUALIFIER genType gaussRand(genType Mean, genType Deviation) { genType w, x1, x2; do { x1 = linearRand(genType(-1), genType(1)); x2 = linearRand(genType(-1), genType(1)); w = x1 * x1 + x2 * x2; } while(w > genType(1)); return x2 * Deviation * Deviation * sqrt((genType(-2) * log(w)) / w) + Mean; } template GLM_FUNC_QUALIFIER vec gaussRand(vec const& Mean, vec const& Deviation) { return detail::functor2::call(gaussRand, Mean, Deviation); } template GLM_FUNC_QUALIFIER vec<2, T, defaultp> diskRand(T Radius) { vec<2, T, defaultp> Result(T(0)); T LenRadius(T(0)); do { Result = linearRand( vec<2, T, defaultp>(-Radius), vec<2, T, defaultp>(Radius)); LenRadius = length(Result); } while(LenRadius > Radius); return Result; } template GLM_FUNC_QUALIFIER vec<3, T, defaultp> ballRand(T Radius) { vec<3, T, defaultp> Result(T(0)); T LenRadius(T(0)); do { Result = linearRand( vec<3, T, defaultp>(-Radius), vec<3, T, defaultp>(Radius)); LenRadius = length(Result); } while(LenRadius > Radius); return Result; } template GLM_FUNC_QUALIFIER vec<2, T, defaultp> circularRand(T Radius) { T a = linearRand(T(0), static_cast(6.283185307179586476925286766559)); return vec<2, T, defaultp>(glm::cos(a), glm::sin(a)) * Radius; } template GLM_FUNC_QUALIFIER vec<3, T, defaultp> sphericalRand(T Radius) { T theta = linearRand(T(0), T(6.283185307179586476925286766559f)); T phi = std::acos(linearRand(T(-1.0f), T(1.0f))); T x = std::sin(phi) * std::cos(theta); T y = std::sin(phi) * std::sin(theta); T z = std::cos(phi); return vec<3, T, defaultp>(x, y, z) * Radius; } }//namespace glm