From 9a0dffe625aa3ee63315b1653280089600bde05f Mon Sep 17 00:00:00 2001 From: sariug Date: Wed, 8 Apr 2020 22:50:05 +0200 Subject: [PATCH] Better quaternion, get rid of bool, put the links --- src/Magnum/Math/Random.h | 43 ++++++++++------------------------------ 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/src/Magnum/Math/Random.h b/src/Magnum/Math/Random.h index e0d335153..0de59af8a 100644 --- a/src/Magnum/Math/Random.h +++ b/src/Magnum/Math/Random.h @@ -80,48 +80,25 @@ Vector2 randomUnitVector2() template Vector3 randomUnitVector3() { + // Better to have it "theta" and "z" than three random numbers. + // https://mathworld.wolfram.com/SpherePointPicking.html auto a = Implementation::RandomGenerator::generate(0.0f, 2 * Math::Constants::pi()); auto z = randomSignedScalar(); auto r = sqrt(1 - z * z); return {r * std::cos(a), r * std::sin(a), z}; } -template -Vector2 randomPointInACircle() // always length < 1 -{ - while (true) - { - auto p = Vector2( - randomSignedScalar(), randomSignedScalar()); - if (p.length() >= 1) - continue; - return p; - } -} - -template -Vector3 randomPointInASphere() // always length < 1 -{ - - while (true) - { - auto p = Vector3(randomSignedScalar(), - randomSignedScalar(), - randomSignedScalar()); - if (p.length() >= 1) - continue; - return p; - } -} - -bool randomBool() -{ - return static_cast(randomUnsignedScalar()); -} template Quaternion randomRotation() { - return Quaternion({randomSignedScalar(), randomSignedScalar(), randomSignedScalar()}, randomSignedScalar()).normalized(); + //http://planning.cs.uiuc.edu/node198.html + auto u{randomUnsignedScalar()}; + auto v{2 * Math::Constants::pi() * randomUnsignedScalar()}; + auto w{2 * Math::Constants::pi() * randomUnsignedScalar()}; + return Quaternion({sqrt(1 - u) * std::sin(v), + sqrt(1 - u) * std::cos(v), + sqrt(u) * std::sin(w)}, + sqrt(u) * std::cos(w)); } } // namespace Random