Plasma Engine  2.0
Loading...
Searching...
No Matches
AllClassesRandom_inl.h
1#pragma once
2
3#include <Foundation/Math/Angle.h>
4#include <Foundation/Math/Random.h>
5#include <Foundation/Math/Vec3.h>
6
7template <typename Type>
9{
10 double px, py, pz;
11 double len = 0.0;
12
13 do
14 {
15 px = inout_rng.DoubleMinMax(-1, 1);
16 py = inout_rng.DoubleMinMax(-1, 1);
17 pz = inout_rng.DoubleMinMax(-1, 1);
18
19 len = (px * px) + (py * py) + (pz * pz);
20 } while (len > 1.0 || len <= 0.000001); // prevent the exact center
21
22 return plVec3Template<Type>((Type)px, (Type)py, (Type)pz);
23}
24
25template <typename Type>
27{
28 plVec3Template<Type> vec = MakeRandomPointInSphere(inout_rng);
29 vec.Normalize();
30 return vec;
31}
32
33template <typename Type>
34PL_IMPLEMENT_IF_FLOAT_TYPE plVec3Template<Type> plVec3Template<Type>::MakeRandomDeviationX(plRandom& inout_rng, const plAngle& maxDeviation)
35{
36 const double twoPi = 2.0 * plMath::Pi<double>();
37
38 const double cosAngle = plMath::Cos(maxDeviation);
39
40 const double x = inout_rng.DoubleZeroToOneInclusive() * (1 - cosAngle) + cosAngle;
41 const plAngle phi = plAngle::MakeFromRadian((float)(inout_rng.DoubleZeroToOneInclusive() * twoPi));
42 const double invSqrt = plMath::Sqrt(1 - (x * x));
43 const double y = invSqrt * plMath::Cos(phi);
44 const double z = invSqrt * plMath::Sin(phi);
45
46 return plVec3Template<Type>((Type)x, (Type)y, (Type)z);
47}
48
49template <typename Type>
50PL_IMPLEMENT_IF_FLOAT_TYPE plVec3Template<Type> plVec3Template<Type>::MakeRandomDeviationY(plRandom& inout_rng, const plAngle& maxDeviation)
51{
52 plVec3Template<Type> vec = MakeRandomDeviationX(inout_rng, maxDeviation);
53 plMath::Swap(vec.x, vec.y);
54 return vec;
55}
56
57template <typename Type>
58PL_IMPLEMENT_IF_FLOAT_TYPE plVec3Template<Type> plVec3Template<Type>::MakeRandomDeviationZ(plRandom& inout_rng, const plAngle& maxDeviation)
59{
60 plVec3Template<Type> vec = MakeRandomDeviationX(inout_rng, maxDeviation);
61 plMath::Swap(vec.x, vec.z);
62 return vec;
63}
64
65template <typename Type>
66PL_IMPLEMENT_IF_FLOAT_TYPE plVec3Template<Type> plVec3Template<Type>::MakeRandomDeviation(plRandom& inout_rng, const plAngle& maxDeviation, const plVec3Template<Type>& vNormal)
67{
68 // If you need to do this very often:
69 // *** Pre-compute this once: ***
70
71 // how to get from the X axis to our desired basis
73
74 // *** Then call this with the precomputed value as often as needed: ***
75
76 // create a random vector along X
77 plVec3Template<Type> vec = MakeRandomDeviationX(inout_rng, maxDeviation);
78 // rotate from X to our basis
79 return qRotXtoDir * vec;
80}
Float wrapper struct for a safe usage and conversions of angles.
Definition Angle.h:10
static constexpr plAngle MakeFromRadian(float fRadian)
Creates an instance of plAngle that was initialized from radian. (No need for any conversion)
Definition Angle_inl.h:38
Quaternions can be used to represent rotations in 3D space.
Definition Quat.h:19
static plQuatTemplate< float > MakeShortestRotation(const plVec3Template< float > &vDirFrom, const plVec3Template< float > &vDirTo)
Definition Quat_inl.h:363
A random number generator. Currently uses the WELL512 algorithm.
Definition Random.h:9
double DoubleMinMax(double fMinValue, double fMaxValue)
Returns a double value in range [fMinValue ; fMaxValue].
Definition Random.cpp:115
PL_ALWAYS_INLINE double DoubleZeroToOneInclusive()
Returns a value in range [0.0 ; 1.0], ie. including zero and one.
Definition Random.h:51
A 3-component vector class.
Definition Vec3.h:9
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationZ(plRandom &inout_rng, const plAngle &maxDeviation)
Creates a random vector around the z axis with a maximum deviation angle of maxDeviation....
Definition AllClassesRandom_inl.h:58
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationX(plRandom &inout_rng, const plAngle &maxDeviation)
Creates a random vector around the x axis with a maximum deviation angle of maxDeviation....
Definition AllClassesRandom_inl.h:34
PL_DECLARE_IF_FLOAT_TYPE void Normalize()
Normalizes this vector.
Definition Vec3_inl.h:95
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomPointInSphere(plRandom &inout_rng)
Returns a random point inside a unit sphere (radius 1).
Definition AllClassesRandom_inl.h:8
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDirection(plRandom &inout_rng)
Creates a random direction vector. The vector is normalized.
Definition AllClassesRandom_inl.h:26
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviationY(plRandom &inout_rng, const plAngle &maxDeviation)
Creates a random vector around the y axis with a maximum deviation angle of maxDeviation....
Definition AllClassesRandom_inl.h:50
static PL_DECLARE_IF_FLOAT_TYPE plVec3Template< Type > MakeRandomDeviation(plRandom &inout_rng, const plAngle &maxDeviation, const plVec3Template< Type > &vNormal)
Creates a random vector around the given normal with a maximum deviation.
Definition AllClassesRandom_inl.h:66
constexpr TYPE Pi()
Returns the natural constant Pi.
PL_ALWAYS_INLINE float Sin(plAngle a)
***** Trigonometric Functions *****
Definition MathFloat_inl.h:62
PL_ALWAYS_INLINE double Sqrt(double f)
Returns the square root of f.
Definition MathDouble_inl.h:99
PL_ALWAYS_INLINE void Swap(T &ref_f1, T &ref_f2)
Swaps the values in the two variables f1 and f2.
Definition Math_inl.h:224
PL_ALWAYS_INLINE float Cos(plAngle a)
Takes an angle, returns its cosine.
Definition MathFloat_inl.h:67