plumageRender/3rdparty/glm/gtc/random.inl

304 lines
8.5 KiB
Plaintext
Raw Normal View History

2023-05-17 14:49:05 +08:00
#include "../geometric.hpp"
#include "../exponential.hpp"
2023-06-07 10:52:04 +08:00
#include "../trigonometric.hpp"
2025-02-15 20:55:58 +08:00
#include "../detail/type_vec1.hpp"
2023-05-17 14:49:05 +08:00
#include <cstdlib>
#include <ctime>
#include <cassert>
2023-06-07 10:52:04 +08:00
#include <cmath>
2023-05-17 14:49:05 +08:00
namespace glm{
namespace detail
{
2023-06-07 10:52:04 +08:00
template <length_t L, typename T, qualifier Q>
2023-05-17 14:49:05 +08:00
struct compute_rand
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, T, Q> call();
2023-05-17 14:49:05 +08:00
};
2023-06-07 10:52:04 +08:00
template <qualifier P>
struct compute_rand<1, uint8, P>
2023-05-17 14:49:05 +08:00
{
GLM_FUNC_QUALIFIER static vec<1, uint8, P> call()
{
return vec<1, uint8, P>(
2025-02-15 20:55:58 +08:00
static_cast<uint8>(std::rand() % std::numeric_limits<uint8>::max()));
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template <qualifier P>
struct compute_rand<2, uint8, P>
2023-05-17 14:49:05 +08:00
{
GLM_FUNC_QUALIFIER static vec<2, uint8, P> call()
{
return vec<2, uint8, P>(
std::rand() % std::numeric_limits<uint8>::max(),
std::rand() % std::numeric_limits<uint8>::max());
}
};
2023-06-07 10:52:04 +08:00
template <qualifier P>
struct compute_rand<3, uint8, P>
2023-05-17 14:49:05 +08:00
{
GLM_FUNC_QUALIFIER static vec<3, uint8, P> call()
{
return vec<3, uint8, P>(
std::rand() % std::numeric_limits<uint8>::max(),
std::rand() % std::numeric_limits<uint8>::max(),
std::rand() % std::numeric_limits<uint8>::max());
}
};
2023-06-07 10:52:04 +08:00
template <qualifier P>
struct compute_rand<4, uint8, P>
2023-05-17 14:49:05 +08:00
{
GLM_FUNC_QUALIFIER static vec<4, uint8, P> call()
{
return vec<4, uint8, P>(
std::rand() % std::numeric_limits<uint8>::max(),
std::rand() % std::numeric_limits<uint8>::max(),
std::rand() % std::numeric_limits<uint8>::max(),
std::rand() % std::numeric_limits<uint8>::max());
}
};
2023-06-07 10:52:04 +08:00
template <length_t L, qualifier Q>
struct compute_rand<L, uint16, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, uint16, Q> call()
2023-05-17 14:49:05 +08:00
{
return
2023-06-07 10:52:04 +08:00
(vec<L, uint16, Q>(compute_rand<L, uint8, Q>::call()) << static_cast<uint16>(8)) |
(vec<L, uint16, Q>(compute_rand<L, uint8, Q>::call()) << static_cast<uint16>(0));
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template <length_t L, qualifier Q>
struct compute_rand<L, uint32, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, uint32, Q> call()
2023-05-17 14:49:05 +08:00
{
return
2023-06-07 10:52:04 +08:00
(vec<L, uint32, Q>(compute_rand<L, uint16, Q>::call()) << static_cast<uint32>(16)) |
(vec<L, uint32, Q>(compute_rand<L, uint16, Q>::call()) << static_cast<uint32>(0));
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template <length_t L, qualifier Q>
struct compute_rand<L, uint64, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, uint64, Q> call()
2023-05-17 14:49:05 +08:00
{
return
2023-06-07 10:52:04 +08:00
(vec<L, uint64, Q>(compute_rand<L, uint32, Q>::call()) << static_cast<uint64>(32)) |
(vec<L, uint64, Q>(compute_rand<L, uint32, Q>::call()) << static_cast<uint64>(0));
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template <length_t L, typename T, qualifier Q>
2023-05-17 14:49:05 +08:00
struct compute_linearRand
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, T, Q> call(vec<L, T, Q> const& Min, vec<L, T, Q> const& Max);
2023-05-17 14:49:05 +08:00
};
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, int8, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, int8, Q> call(vec<L, int8, Q> const& Min, vec<L, int8, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (vec<L, int8, Q>(compute_rand<L, uint8, Q>::call() % vec<L, uint8, Q>(Max + static_cast<int8>(1) - Min))) + Min;
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, uint8, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, uint8, Q> call(vec<L, uint8, Q> const& Min, vec<L, uint8, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (compute_rand<L, uint8, Q>::call() % (Max + static_cast<uint8>(1) - Min)) + Min;
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, int16, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, int16, Q> call(vec<L, int16, Q> const& Min, vec<L, int16, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (vec<L, int16, Q>(compute_rand<L, uint16, Q>::call() % vec<L, uint16, Q>(Max + static_cast<int16>(1) - Min))) + Min;
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, uint16, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, uint16, Q> call(vec<L, uint16, Q> const& Min, vec<L, uint16, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (compute_rand<L, uint16, Q>::call() % (Max + static_cast<uint16>(1) - Min)) + Min;
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, int32, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, int32, Q> call(vec<L, int32, Q> const& Min, vec<L, int32, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (vec<L, int32, Q>(compute_rand<L, uint32, Q>::call() % vec<L, uint32, Q>(Max + static_cast<int32>(1) - Min))) + Min;
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, uint32, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, uint32, Q> call(vec<L, uint32, Q> const& Min, vec<L, uint32, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (compute_rand<L, uint32, Q>::call() % (Max + static_cast<uint32>(1) - Min)) + Min;
2023-05-17 14:49:05 +08:00
}
};
2025-02-15 20:55:58 +08:00
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, int64, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, int64, Q> call(vec<L, int64, Q> const& Min, vec<L, int64, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (vec<L, int64, Q>(compute_rand<L, uint64, Q>::call() % vec<L, uint64, Q>(Max + static_cast<int64>(1) - Min))) + Min;
2023-05-17 14:49:05 +08:00
}
};
2023-06-07 10:52:04 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, uint64, Q>
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
GLM_FUNC_QUALIFIER static vec<L, uint64, Q> call(vec<L, uint64, Q> const& Min, vec<L, uint64, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return (compute_rand<L, uint64, Q>::call() % (Max + static_cast<uint64>(1) - Min)) + Min;
2023-05-17 14:49:05 +08:00
}
};
2025-02-15 20:55:58 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, float, Q>
2023-05-17 14:49:05 +08:00
{
2025-02-15 20:55:58 +08:00
GLM_FUNC_QUALIFIER static vec<L, float, Q> call(vec<L, float, Q> const& Min, vec<L, float, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2025-02-15 20:55:58 +08:00
return vec<L, float, Q>(compute_rand<L, uint32, Q>::call()) / static_cast<float>(std::numeric_limits<uint32>::max()) * (Max - Min) + Min;
2023-05-17 14:49:05 +08:00
}
};
2025-02-15 20:55:58 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, double, Q>
2023-05-17 14:49:05 +08:00
{
2025-02-15 20:55:58 +08:00
GLM_FUNC_QUALIFIER static vec<L, double, Q> call(vec<L, double, Q> const& Min, vec<L, double, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2025-02-15 20:55:58 +08:00
return vec<L, double, Q>(compute_rand<L, uint64, Q>::call()) / static_cast<double>(std::numeric_limits<uint64>::max()) * (Max - Min) + Min;
2023-05-17 14:49:05 +08:00
}
};
2025-02-15 20:55:58 +08:00
template<length_t L, qualifier Q>
struct compute_linearRand<L, long double, Q>
2023-05-17 14:49:05 +08:00
{
2025-02-15 20:55:58 +08:00
GLM_FUNC_QUALIFIER static vec<L, long double, Q> call(vec<L, long double, Q> const& Min, vec<L, long double, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2025-02-15 20:55:58 +08:00
return vec<L, long double, Q>(compute_rand<L, uint64, Q>::call()) / static_cast<long double>(std::numeric_limits<uint64>::max()) * (Max - Min) + Min;
2023-05-17 14:49:05 +08:00
}
};
}//namespace detail
template<typename genType>
GLM_FUNC_QUALIFIER genType linearRand(genType Min, genType Max)
{
2023-06-07 10:52:04 +08:00
return detail::compute_linearRand<1, genType, highp>::call(
2023-05-17 14:49:05 +08:00
vec<1, genType, highp>(Min),
vec<1, genType, highp>(Max)).x;
}
2023-06-07 10:52:04 +08:00
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> linearRand(vec<L, T, Q> const& Min, vec<L, T, Q> const& Max)
2023-05-17 14:49:05 +08:00
{
2023-06-07 10:52:04 +08:00
return detail::compute_linearRand<L, T, Q>::call(Min, Max);
2023-05-17 14:49:05 +08:00
}
template<typename genType>
GLM_FUNC_QUALIFIER genType gaussRand(genType Mean, genType Deviation)
{
genType w, x1, x2;
2023-06-07 10:52:04 +08:00
2023-05-17 14:49:05 +08:00
do
{
x1 = linearRand(genType(-1), genType(1));
x2 = linearRand(genType(-1), genType(1));
2025-02-15 20:55:58 +08:00
2023-05-17 14:49:05 +08:00
w = x1 * x1 + x2 * x2;
} while(w > genType(1));
2023-06-07 10:52:04 +08:00
2025-02-15 20:55:58 +08:00
return static_cast<genType>(x2 * Deviation * Deviation * sqrt((genType(-2) * log(w)) / w) + Mean);
2023-05-17 14:49:05 +08:00
}
2023-06-07 10:52:04 +08:00
template<length_t L, typename T, qualifier Q>
GLM_FUNC_QUALIFIER vec<L, T, Q> gaussRand(vec<L, T, Q> const& Mean, vec<L, T, Q> const& Deviation)
2023-05-17 14:49:05 +08:00
{
2025-02-15 20:55:58 +08:00
return detail::functor2<vec, L, T, Q>::call(gaussRand, Mean, Deviation);
2023-05-17 14:49:05 +08:00
}
template<typename T>
GLM_FUNC_QUALIFIER vec<2, T, defaultp> diskRand(T Radius)
2025-02-15 20:55:58 +08:00
{
assert(Radius > static_cast<T>(0));
2023-05-17 14:49:05 +08:00
vec<2, T, defaultp> Result(T(0));
T LenRadius(T(0));
2023-06-07 10:52:04 +08:00
2023-05-17 14:49:05 +08:00
do
{
Result = linearRand(
vec<2, T, defaultp>(-Radius),
vec<2, T, defaultp>(Radius));
LenRadius = length(Result);
}
while(LenRadius > Radius);
2023-06-07 10:52:04 +08:00
2023-05-17 14:49:05 +08:00
return Result;
}
2023-06-07 10:52:04 +08:00
2023-05-17 14:49:05 +08:00
template<typename T>
GLM_FUNC_QUALIFIER vec<3, T, defaultp> ballRand(T Radius)
2023-06-07 10:52:04 +08:00
{
2025-02-15 20:55:58 +08:00
assert(Radius > static_cast<T>(0));
2023-05-17 14:49:05 +08:00
vec<3, T, defaultp> Result(T(0));
T LenRadius(T(0));
2025-02-15 20:55:58 +08:00
2023-05-17 14:49:05 +08:00
do
{
Result = linearRand(
vec<3, T, defaultp>(-Radius),
vec<3, T, defaultp>(Radius));
LenRadius = length(Result);
}
while(LenRadius > Radius);
2025-02-15 20:55:58 +08:00
2023-05-17 14:49:05 +08:00
return Result;
}
2023-06-07 10:52:04 +08:00
2023-05-17 14:49:05 +08:00
template<typename T>
GLM_FUNC_QUALIFIER vec<2, T, defaultp> circularRand(T Radius)
{
2025-02-15 20:55:58 +08:00
assert(Radius > static_cast<T>(0));
2023-06-07 10:52:04 +08:00
T a = linearRand(T(0), static_cast<T>(6.283185307179586476925286766559));
return vec<2, T, defaultp>(glm::cos(a), glm::sin(a)) * Radius;
2023-05-17 14:49:05 +08:00
}
2023-06-07 10:52:04 +08:00
2023-05-17 14:49:05 +08:00
template<typename T>
GLM_FUNC_QUALIFIER vec<3, T, defaultp> sphericalRand(T Radius)
{
2025-02-15 20:55:58 +08:00
assert(Radius > static_cast<T>(0));
2023-06-07 10:52:04 +08:00
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;
2023-05-17 14:49:05 +08:00
}
}//namespace glm