Implement Vector3::FromScalar() RandomDir() RandomSphere() RandomBox()
Some checks are pending
Run tests / Explore-Gitea-Actions (push) Waiting to run
Build Docs With Doxygen / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
2024-05-31 15:19:45 -04:00
parent e0ba266444
commit 312d039001
2 changed files with 46 additions and 1 deletions

View File

@@ -5,6 +5,10 @@
#include <cstddef> #include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <J3ML/LinearAlgebra/Angle2D.h> #include <J3ML/LinearAlgebra/Angle2D.h>
#include <J3ML/Algorithm/RNG.h>
using namespace J3ML::Algorithm;
namespace J3ML::LinearAlgebra { namespace J3ML::LinearAlgebra {
@@ -95,7 +99,23 @@ public:
/// Constructs a new Vector3 with the value (scalar, scalar, scalar). /// Constructs a new Vector3 with the value (scalar, scalar, scalar).
explicit Vector3(float scalar); explicit Vector3(float scalar);
/// Generates a new Vector3 by fillings its entries by the given scalar.
/// @see Vector3::Vector3(float scalar), SetFromScalar()
static Vector3 FromScalar(float scalar);
/// Generates a direction vector of the given length.
/** The returned vector points at a uniformly random direction.
@see RandomSphere(), RandomBox() */
static Vector3 RandomDir(RNG& rng, float length = 1.f);
static Vector3 RandomSphere(RNG& rng, const Vector3& center, float radius);
static Vector3 RandomBox(RNG& rng, float xmin, float xmax, float ymin, float ymax, float zmin, float zmax);
static Vector3 RandomBox(RNG& rng, const Vector3& min, const Vector3 &max);
static Vector3 RandomBox(RNG& rng, float min, float max);
static inline Vector3 RandomGeneral(RNG& rng, float minElem, float maxElem) { return RandomBox(rng, minElem, maxElem); }
public:
/// Casts this float3 to a C array. /// Casts this float3 to a C array.
/** This function does not allocate new memory or make a copy of this Vector3. This function simply /** This function does not allocate new memory or make a copy of this Vector3. This function simply
returns a C pointer view to this data structure. Use ptr()[0] to access the x component of this float3, returns a C pointer view to this data structure. Use ptr()[0] to access the x component of this float3,
@@ -172,7 +192,7 @@ public:
float MinElement() const; float MinElement() const;
static float MinElement(const Vector3& of); static float MinElement(const Vector3& of);
// Normalizes this Vector3. /// Normalizes this Vector3.
/** In the case of failure, this vector is set to (1, 0, 0), so calling this function will never result in an /** In the case of failure, this vector is set to (1, 0, 0), so calling this function will never result in an
unnormalized vector. unnormalized vector.
@note If this function fails to normalize the vector, no error message is printed, the vector is set to (1,0,0) and @note If this function fails to normalize the vector, no error message is printed, the vector is set to (1,0,0) and

View File

@@ -2,6 +2,7 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <cmath> #include <cmath>
#include <J3ML/Geometry/Sphere.h>
namespace J3ML::LinearAlgebra { namespace J3ML::LinearAlgebra {
@@ -529,5 +530,29 @@ namespace J3ML::LinearAlgebra {
c.IsNormalized(epsilon*epsilon); c.IsNormalized(epsilon*epsilon);
} }
Vector3 Vector3::FromScalar(float scalar) {
return {scalar, scalar, scalar};
}
Vector3 Vector3::RandomDir(RNG &rng, float length) {
return Sphere(Vector3::FromScalar(0.f), length).RandomPointOnSurface(rng);
}
Vector3 Vector3::RandomSphere(RNG &rng, const Vector3 &center, float radius) {
return Sphere(center, radius).RandomPointInside(rng);
}
Vector3 Vector3::RandomBox(RNG &rng, const Vector3& min, const Vector3& max) {
return AABB(min, max).RandomPointInside(rng);
}
Vector3 Vector3::RandomBox(RNG &rng, float min, float max) {
return RandomBox(rng, {min,min,min}, {max,max,max});
}
Vector3 Vector3::RandomBox(RNG &rng, float xmin, float xmax, float ymin, float ymax, float zmin, float zmax) {
return RandomBox(rng, {xmin,ymin,zmin}, {xmax,ymax,zmax});
}
} }