From 312d0390018a922f781ed81c7e5da3d451779745 Mon Sep 17 00:00:00 2001 From: josh Date: Fri, 31 May 2024 15:19:45 -0400 Subject: [PATCH] Implement Vector3::FromScalar() RandomDir() RandomSphere() RandomBox() --- include/J3ML/LinearAlgebra/Vector3.h | 22 +++++++++++++++++++++- src/J3ML/LinearAlgebra/Vector3.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/include/J3ML/LinearAlgebra/Vector3.h b/include/J3ML/LinearAlgebra/Vector3.h index 6123547..300ab8f 100644 --- a/include/J3ML/LinearAlgebra/Vector3.h +++ b/include/J3ML/LinearAlgebra/Vector3.h @@ -5,6 +5,10 @@ #include #include #include +#include + + +using namespace J3ML::Algorithm; namespace J3ML::LinearAlgebra { @@ -95,7 +99,23 @@ public: /// Constructs a new Vector3 with the value (scalar, scalar, 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. /** 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, @@ -172,7 +192,7 @@ public: float MinElement() const; 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 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 diff --git a/src/J3ML/LinearAlgebra/Vector3.cpp b/src/J3ML/LinearAlgebra/Vector3.cpp index 108eca9..3392eec 100644 --- a/src/J3ML/LinearAlgebra/Vector3.cpp +++ b/src/J3ML/LinearAlgebra/Vector3.cpp @@ -2,6 +2,7 @@ #include #include #include +#include namespace J3ML::LinearAlgebra { @@ -529,5 +530,29 @@ namespace J3ML::LinearAlgebra { 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 ¢er, 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}); + } + } \ No newline at end of file