More Math cleanup

This commit is contained in:
SirLynix
2022-12-18 16:33:05 +01:00
parent a1e62adb71
commit 5ca7b398c2
15 changed files with 104 additions and 454 deletions

View File

@@ -24,10 +24,8 @@ namespace Nz
public:
BoundingVolume();
BoundingVolume(Extend Extend);
BoundingVolume(T X, T Y, T Z, T Width, T Height, T Depth);
BoundingVolume(const Box<T>& box);
BoundingVolume(const OrientedBox<T>& orientedBox);
BoundingVolume(const Vector3<T>& vec1, const Vector3<T>& vec2);
template<typename U> explicit BoundingVolume(const BoundingVolume<U>& volume);
BoundingVolume(const BoundingVolume& volume) = default;
~BoundingVolume() = default;
@@ -40,17 +38,6 @@ namespace Nz
bool IsInfinite() const;
bool IsNull() const;
BoundingVolume& MakeInfinite();
BoundingVolume& MakeNull();
BoundingVolume& Set(Extend Extend);
BoundingVolume& Set(T X, T Y, T Z, T Width, T Height, T Depth);
BoundingVolume& Set(const BoundingVolume<T>& volume);
BoundingVolume& Set(const Box<T>& box);
BoundingVolume& Set(const OrientedBox<T>& orientedBox);
BoundingVolume& Set(const Vector3<T>& vec1, const Vector3<T>& vec2);
template<typename U> BoundingVolume& Set(const BoundingVolume<U>& volume);
std::string ToString() const;
void Update(const Matrix4<T>& transformMatrix);

View File

@@ -42,28 +42,9 @@ namespace Nz
*/
template<typename T>
BoundingVolume<T>::BoundingVolume(Extend Extend)
BoundingVolume<T>::BoundingVolume(Extend Extend) :
extend(Extend)
{
Set(Extend);
}
/*!
* \brief Constructs a BoundingVolume object from its position and sizes
*
* \param X X component of position
* \param Y Y component of position
* \param Z Z component of position
* \param Width Width of the box (following X)
* \param Height Height of the box (following Y)
* \param Depth Depth of the box (following Z)
*
* \remark Aabb is uninitialized
*/
template<typename T>
BoundingVolume<T>::BoundingVolume(T X, T Y, T Z, T Width, T Height, T Depth)
{
Set(X, Y, Z, Width, Height, Depth);
}
/*!
@@ -75,9 +56,10 @@ namespace Nz
*/
template<typename T>
BoundingVolume<T>::BoundingVolume(const Box<T>& box)
BoundingVolume<T>::BoundingVolume(const Box<T>& box) :
extend(Extend::Finite),
obb(box)
{
Set(box);
}
/*!
@@ -89,25 +71,10 @@ namespace Nz
*/
template<typename T>
BoundingVolume<T>::BoundingVolume(const OrientedBox<T>& orientedBox)
BoundingVolume<T>::BoundingVolume(const OrientedBox<T>& orientedBox) :
extend(Extend::Finite),
obb(orientedBox)
{
Set(orientedBox);
}
/*!
* \brief Constructs a BoundingVolume object from two vectors representing point of the space
* (X, Y, Z) will be the components minimum of the two vectors and the (width, height, depth) will be the components maximum - minimum
*
* \param vec1 First point
* \param vec2 Second point
*
* \remark Aabb is uninitialized
*/
template<typename T>
BoundingVolume<T>::BoundingVolume(const Vector3<T>& vec1, const Vector3<T>& vec2)
{
Set(vec1, vec2);
}
/*!
@@ -118,12 +85,13 @@ namespace Nz
template<typename T>
template<typename U>
BoundingVolume<T>::BoundingVolume(const BoundingVolume<U>& volume)
BoundingVolume<T>::BoundingVolume(const BoundingVolume<U>& volume) :
extend(volume.extend),
aabb(volume.aabb),
obb(volume.obb)
{
Set(volume);
}
/*!
* \brief Extends the bounding volume to contain another bounding volume
* \return A reference to the the bounding volume
@@ -149,7 +117,7 @@ namespace Nz
}
case Extend::Infinite:
MakeInfinite();
extend = Extend::Infinite;
break;
case Extend::Null:
@@ -219,156 +187,6 @@ namespace Nz
return extend == Extend::Null;
}
/*!
* \brief Makes the bounding volume infinite
* \return A reference to this bounding volume with Extend::Infinite for extend
*
* \see Infinite
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::MakeInfinite()
{
extend = Extend::Infinite;
return *this;
}
/*!
* \brief Makes the bounding volume null
* \return A reference to this bounding volume with Extend::Null for extend
*
* \see Null
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::MakeNull()
{
extend = Extend::Null;
return *this;
}
/*!
* \brief Sets the extend of the bounding volume from Extend
* \return A reference to this bounding volume
*
* \param Extend New extend
*
* \remark This method is meant to be called with Extend::Infinite or Extend::Null
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::Set(Extend Extend)
{
extend = Extend;
return *this;
}
/*!
* \brief Sets the components of the bounding volume
* \return A reference to this bounding volume
*
* \param X X position
* \param Y Y position
* \param Z Z position
* \param Width Width of the oriented box (following X)
* \param Height Height of the oriented box (following Y)
* \param Depth Depth of the oriented box (following Z)
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::Set(T X, T Y, T Z, T Width, T Height, T Depth)
{
obb.Set(X, Y, Z, Width, Height, Depth);
extend = Extend::Finite;
return *this;
}
/*!
* \brief Sets the components of the bounding volume from another bounding volume
* \return A reference to this bounding volume
*
* \param volume The other bounding volume
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::Set(const BoundingVolume<T>& volume)
{
obb.Set(volume.obb); // Only OBB is important for the moment
extend = volume.extend;
return *this;
}
/*!
* \brief Sets the components of the bounding volume from a box
* \return A reference to this bounding volume
*
* \param box Box<T> object
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::Set(const Box<T>& box)
{
obb.Set(box);
extend = Extend::Finite;
return *this;
}
/*!
* \brief Sets the components of the bounding volume from an oriented box
* \return A reference to this bounding volume
*
* \param orientedBox OrientedBox<T> object
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::Set(const OrientedBox<T>& orientedBox)
{
obb.Set(orientedBox);
extend = Extend::Finite;
return *this;
}
/*!
* \brief Sets a BoundingVolume object from two vectors representing point of the space
* (X, Y, Z) will be the components minimum of the two vectors and the (width, height, depth) will be the components maximum - minimum
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
BoundingVolume<T>& BoundingVolume<T>::Set(const Vector3<T>& vec1, const Vector3<T>& vec2)
{
obb.Set(vec1, vec2);
extend = Extend::Finite;
return *this;
}
/*!
* \brief Sets the components of the bounding volume from another type of BoundingVolume
* \return A reference to this bounding volume
*
* \param volume BoundingVolume of type U to convert its components
*/
template<typename T>
template<typename U>
BoundingVolume<T>& BoundingVolume<T>::Set(const BoundingVolume<U>& volume)
{
obb.Set(volume.obb);
extend = volume.extend;
return *this;
}
/*!
* \brief Gives a string representation
* \return A string representation of the object: "BoundingVolume(localBox="")" if finite, or "BoundingVolume(Infinite)" or "BoundingVolume(Null)"
@@ -491,7 +309,7 @@ namespace Nz
BoundingVolume<T> BoundingVolume<T>::Infinite()
{
BoundingVolume volume;
volume.MakeInfinite();
volume.extend = Extend::Infinite;
return volume;
}
@@ -514,14 +332,6 @@ namespace Nz
template<typename T>
BoundingVolume<T> BoundingVolume<T>::Lerp(const BoundingVolume& from, const BoundingVolume& to, T interpolation)
{
#ifdef NAZARA_DEBUG
if (interpolation < T(0.0) || interpolation > T(1.0))
{
NazaraError("Interpolation must be in range [0..1] (Got " + NumberToString(interpolation) + ')');
return Null();
}
#endif
if (NumberEquals(interpolation, T(0.0)))
return from;
@@ -535,18 +345,18 @@ namespace Nz
switch (from.extend)
{
case Extend::Finite:
{
BoundingVolume volume;
volume.Set(OrientedBox<T>::Lerp(from.obb, to.obb, interpolation));
return volume;
}
return BoundingVolume{ OrientedBox<T>::Lerp(from.obb, to.obb, interpolation) };
case Extend::Infinite:
return Infinite();
case Extend::Null:
return to.obb * interpolation;
{
Box<T> destBox = to.obb.localBox;
destBox.Scale(interpolation);
return { destBox };
}
}
// If we arrive here, the extend is invalid
@@ -562,7 +372,12 @@ namespace Nz
switch (from.extend)
{
case Extend::Finite:
return from.obb * (T(1.0) - interpolation);
{
Box<T> fromBox = from.obb.localBox;
fromBox.Scale(T(1.0) - interpolation);
return { fromBox };
}
case Extend::Infinite:
return Infinite();
@@ -593,7 +408,7 @@ namespace Nz
BoundingVolume<T> BoundingVolume<T>::Null()
{
BoundingVolume volume;
volume.MakeNull();
volume.extend = Extend::Null;
return volume;
}

View File

@@ -26,9 +26,11 @@ namespace Nz
Box(T Width, T Height, T Depth);
Box(T X, T Y, T Z, T Width, T Height, T Depth);
Box(const Rect<T>& rect);
explicit Box(const Vector3<T>& lengths);
explicit Box(const Vector3<T>& pos, const Vector3<T>& lengths);
template<typename U> explicit Box(const Box<U>& box);
Box(const Box& box) = default;
Box(const Box&) = default;
Box(Box&&) noexcept = default;
~Box() = default;
bool Contains(T X, T Y, T Z) const;
@@ -69,7 +71,7 @@ namespace Nz
const T& operator[](std::size_t i) const;
Box& operator=(const Box&) = default;
Box& operator=(Box&&) = default;
Box& operator=(Box&&) noexcept = default;
bool operator==(const Box& box) const;
bool operator!=(const Box& box) const;

View File

@@ -78,6 +78,19 @@ namespace Nz
{
}
/*!
* \brief Constructs a Rect object from a vector representing its length
* The position will be set to zero
*
* \param pos (X, Y, Z) of the box
* \param lengths (Width, Height, Depth) of the box
*/
template<typename T>
Box<T>::Box(const Vector3<T>& lengths) :
Box(Vector3<T>::Zero(), lengths)
{
}
/*!
* \brief Constructs a Box object from two vector representing position and lengths
*

View File

@@ -22,11 +22,10 @@ namespace Nz
{
public:
OrientedBox() = default;
OrientedBox(T X, T Y, T Z, T Width, T Height, T Depth);
OrientedBox(const Box<T>& box);
OrientedBox(const Vector3<T>& vec1, const Vector3<T>& vec2);
template<typename U> explicit OrientedBox(const OrientedBox<U>& orientedBox);
OrientedBox(const OrientedBox& orientedBox) = default;
OrientedBox(const OrientedBox&) = default;
OrientedBox(OrientedBox&&) noexcept = default;
~OrientedBox() = default;
const Vector3<T>& GetCorner(BoxCorner corner) const;
@@ -34,25 +33,16 @@ namespace Nz
bool IsValid() const;
OrientedBox& MakeZero();
OrientedBox& Set(T X, T Y, T Z, T Width, T Height, T Depth);
OrientedBox& Set(const Box<T>& box);
OrientedBox& Set(const Vector3<T>& vec1, const Vector3<T>& vec2);
template<typename U> OrientedBox& Set(const OrientedBox<U>& orientedBox);
std::string ToString() const;
void Update(const Matrix4<T>& transformMatrix);
void Update(const Vector3<T>& transformMatrix);
Vector3<T>& operator()(unsigned int i);
Vector3<T> operator()(unsigned int i) const;
const Vector3<T>& operator()(unsigned int i) const;
OrientedBox operator*(T scalar) const;
OrientedBox& operator=(const OrientedBox& other) = default;
OrientedBox& operator*=(T scalar);
OrientedBox& operator=(const OrientedBox&) = default;
OrientedBox& operator=(OrientedBox&&) noexcept = default;
bool operator==(const OrientedBox& box) const;
bool operator!=(const OrientedBox& box) const;

View File

@@ -21,23 +21,6 @@ namespace Nz
* \remark You need to call Update not to have undefined behaviour
*/
/*!
* \brief Constructs a OrientedBox object from its position and sizes
*
* \param X X component of position
* \param Y Y component of position
* \param Z Z component of position
* \param Width Width of the box (following X)
* \param Height Height of the box (following Y)
* \param Depth Depth of the box (following Z)
*/
template<typename T>
OrientedBox<T>::OrientedBox(T X, T Y, T Z, T Width, T Height, T Depth)
{
Set(X, Y, Z, Width, Height, Depth);
}
/*!
* \brief Constructs a OrientedBox object from a box
*
@@ -45,23 +28,9 @@ namespace Nz
*/
template<typename T>
OrientedBox<T>::OrientedBox(const Box<T>& box)
OrientedBox<T>::OrientedBox(const Box<T>& box) :
localBox(box)
{
Set(box);
}
/*!
* \brief Constructs a OrientedBox object from two vectors representing point of the space
* (X, Y, Z) will be the components minimum of the two vectors and the (width, height, depth) will be the components maximum - minimum
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
OrientedBox<T>::OrientedBox(const Vector3<T>& vec1, const Vector3<T>& vec2)
{
Set(vec1, vec2);
}
/*!
@@ -72,9 +41,11 @@ namespace Nz
template<typename T>
template<typename U>
OrientedBox<T>::OrientedBox(const OrientedBox<U>& orientedBox)
OrientedBox<T>::OrientedBox(const OrientedBox<U>& orientedBox) :
localBox(orientedBox.localBox)
{
Set(orientedBox);
for (unsigned int i = 0; i < BoxCornerCount; ++i)
m_corners[i] = Vector3<T>(orientedBox(i));
}
/*!
@@ -119,91 +90,6 @@ namespace Nz
return localBox.IsValid();
}
/*!
* \brief Makes the oriented box position (0, 0, 0) and lengths (0, 0, 0)
* \return A reference to this oriented box with position (0, 0, 0) and lengths (0, 0, 0)
*
* \see Zero
*/
template<typename T>
OrientedBox<T>& OrientedBox<T>::MakeZero()
{
localBox.MakeZero();
return *this;
}
/*!
* \brief Sets the components of the oriented box
* \return A reference to this oriented box
*
* \param X X position
* \param Y Y position
* \param Z Z position
* \param Width Width of the oriented box (following X)
* \param Height Height of the oriented box (following Y)
* \param Depth Depth of the oriented box (following Z)
*/
template<typename T>
OrientedBox<T>& OrientedBox<T>::Set(T X, T Y, T Z, T Width, T Height, T Depth)
{
localBox.Set(X, Y, Z, Width, Height, Depth);
return *this;
}
/*!
* \brief Sets the components of the oriented box from a box
* \return A reference to this oriented box
*
* \param box Box<T> object
*/
template<typename T>
OrientedBox<T>& OrientedBox<T>::Set(const Box<T>& box)
{
localBox = box;
return *this;
}
/*!
* \brief Sets a OrientedBox object from two vectors representing point of the space
* (X, Y, Z) will be the components minimum of the two vectors and the (width, height, depth) will be the components maximum - minimum
*
* \param vec1 First point
* \param vec2 Second point
*/
template<typename T>
OrientedBox<T>& OrientedBox<T>::Set(const Vector3<T>& vec1, const Vector3<T>& vec2)
{
localBox.Set(vec1, vec2);
return *this;
}
/*!
* \brief Sets the components of the orientedBox from another type of OrientedBox
* \return A reference to this orientedBox
*
* \param orientedBox OrientedBox of type U to convert its components
*/
template<typename T>
template<typename U>
OrientedBox<T>& OrientedBox<T>::Set(const OrientedBox<U>& orientedBox)
{
for (unsigned int i = 0; i < BoxCornerCount; ++i)
m_corners[i].Set(orientedBox(i));
localBox.Set(orientedBox.localBox);
return *this;
}
/*!
* \brief Gives a string representation
* \return A string representation of the object: "OrientedBox(...)"
@@ -278,7 +164,7 @@ namespace Nz
*/
template<typename T>
Vector3<T> OrientedBox<T>::operator()(unsigned int i) const
const Vector3<T>& OrientedBox<T>::operator()(unsigned int i) const
{
#if NAZARA_MATH_SAFE
if (i >= BoxCornerCount)
@@ -294,37 +180,6 @@ namespace Nz
return m_corners[i];
}
/*!
* \brief Multiplies the lengths with the scalar
* \return A OrientedBox where the position is the same and width, height and depth are the product of the old width, height and depth and the scalar
*
* \param scalar The scalar to multiply width, height and depth with
*/
template<typename T>
OrientedBox<T> OrientedBox<T>::operator*(T scalar) const
{
OrientedBox box(*this);
box *= scalar;
return box;
}
/*!
* \brief Multiplies the lengths of this oriented box with the scalar
* \return A reference to this oriented box where lengths are the product of these lengths and the scalar
*
* \param scalar The scalar to multiply width, height and depth with
*/
template<typename T>
OrientedBox<T>& OrientedBox<T>::operator*=(T scalar)
{
localBox *= scalar;
return *this;
}
/*!
* \brief Compares the oriented box to other one
* \return true if the two oriented boxes are the same
@@ -368,10 +223,7 @@ namespace Nz
template<typename T>
OrientedBox<T> OrientedBox<T>::Lerp(const OrientedBox& from, const OrientedBox& to, T interpolation)
{
OrientedBox orientedBox;
orientedBox.Set(Box<T>::Lerp(from.localBox, to.localBox, interpolation));
return orientedBox;
return OrientedBox{ Box<T>::Lerp(from.localBox, to.localBox, interpolation) };
}
/*!
@@ -384,10 +236,7 @@ namespace Nz
template<typename T>
OrientedBox<T> OrientedBox<T>::Zero()
{
OrientedBox orientedBox;
orientedBox.MakeZero();
return orientedBox;
return OrientedBox{ Box<T>::Zero() };
}
/*!

View File

@@ -22,9 +22,11 @@ namespace Nz
Rect() = default;
Rect(T Width, T Height);
Rect(T X, T Y, T Width, T Height);
explicit Rect(const Vector2<T>& vec1, const Vector2<T>& vec2);
explicit Rect(const Vector2<T>& lengths);
explicit Rect(const Vector2<T>& pos, const Vector2<T>& lengths);
template<typename U> explicit Rect(const Rect<U>& rect);
Rect(const Rect& rect) = default;
Rect(const Rect&) = default;
Rect(Rect&&) noexcept = default;
~Rect() = default;
bool Contains(T X, T Y) const;
@@ -62,7 +64,7 @@ namespace Nz
const T& operator[](std::size_t i) const;
Rect& operator=(const Rect&) = default;
Rect& operator=(Rect&&) = default;
Rect& operator=(Rect&&) noexcept = default;
bool operator==(const Rect& rect) const;
bool operator!=(const Rect& rect) const;

View File

@@ -55,6 +55,18 @@ namespace Nz
{
}
/*!
* \brief Constructs a Rect object from a vector representing its length
* The position will be set to zero
*
* \param lengths (Width, Height) of the rect
*/
template<typename T>
Rect<T>::Rect(const Vector2<T>& lengths) :
Rect(Vector2<T>::Zero(), lengths)
{
}
/*!
* \brief Constructs a Rect object from two vector representing position and lengths
*