Reworked Bounding Boxes

This commit is contained in:
Diren D Bharwani 2022-10-12 21:59:34 +08:00
parent 2fa71f0fd9
commit d95dbd5ce6
9 changed files with 242 additions and 421 deletions

View File

@ -14,6 +14,9 @@
#include "SHBoundingBox.h" #include "SHBoundingBox.h"
// Project Headers // Project Headers
#include "Math/SHMathHelpers.h" #include "Math/SHMathHelpers.h"
#include "Math/SHRay.h"
using namespace DirectX;
namespace SHADE namespace SHADE
{ {
@ -21,89 +24,56 @@ namespace SHADE
/* Constructors & Destructor Definitions */ /* Constructors & Destructor Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHBoundingBox::SHBoundingBox() noexcept
{
type = Type::BOUNDING_BOX;
}
SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept SHBoundingBox::SHBoundingBox(const SHVec3& c, const SHVec3& hE) noexcept
: SHShape {}
, center { c }
, halfExtents { hE }
{
type = Type::BOUNDING_BOX;
}
SHBoundingBox::SHBoundingBox(const SHVec3* vertices, size_t numVertices) noexcept
: SHShape {}
{ {
type = Type::BOUNDING_BOX; type = Type::BOUNDING_BOX;
if (vertices == nullptr || numVertices < 2) Center = c;
{ Extents = hE;
SHLOG_ERROR("Insufficient number of vertices passed into bounding box constructor!")
return;
}
SHVec3 min { std::numeric_limits<float>::max() };
SHVec3 max { std::numeric_limits<float>::min() };
for (size_t i = 0; i < numVertices; ++i)
{
const SHVec3& v = vertices[i];
min.x = SHMath::Min(min.x, v.x);
min.y = SHMath::Min(min.y, v.y);
min.z = SHMath::Min(min.z, v.z);
max.x = SHMath::Max(max.x, v.x);
max.y = SHMath::Max(max.y, v.y);
max.z = SHMath::Max(max.z, v.z);
}
center = SHVec3::Lerp(min, max, 0.5f);
halfExtents = SHVec3::Abs((max - min) * 0.5f);
} }
SHBoundingBox::SHBoundingBox(const SHBoundingBox* boxes, size_t numBoxes) noexcept
: SHShape {}
{
type = Type::BOUNDING_BOX;
if (boxes == nullptr || numBoxes == 0)
{
SHLOG_ERROR("Insufficient number of boxes passed into bounding box constructor!")
return;
}
center = boxes->center;
halfExtents = boxes->halfExtents;
for (size_t i = 1; i < numBoxes; ++i)
*this = Combine(*this, boxes[i]);
}
SHBoundingBox::SHBoundingBox(const SHBoundingBox& rhs) noexcept SHBoundingBox::SHBoundingBox(const SHBoundingBox& rhs) noexcept
: SHShape {}
, center { rhs.center }
, halfExtents { rhs.halfExtents }
{ {
if (this == &rhs)
return;
type = Type::BOUNDING_BOX; type = Type::BOUNDING_BOX;
Center = rhs.Center;
Extents = rhs.Extents;
} }
SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept SHBoundingBox::SHBoundingBox(SHBoundingBox&& rhs) noexcept
: SHShape {}
, center { rhs.center }
, halfExtents { rhs.halfExtents }
{ {
type = Type::BOUNDING_BOX; type = Type::BOUNDING_BOX;
Center = rhs.Center;
Extents = rhs.Extents;
} }
/*-----------------------------------------------------------------------------------*/
/* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/
SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept SHBoundingBox& SHBoundingBox::operator=(const SHBoundingBox& rhs) noexcept
{ {
if (this == &rhs)
return *this;
if (rhs.type != Type::BOUNDING_BOX) if (rhs.type != Type::BOUNDING_BOX)
{ {
SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!") SHLOG_WARNING("Cannot assign a non-bounding box to a bounding box!")
} }
else else
{ {
center = rhs.center; Center = rhs.Center;
halfExtents = rhs.halfExtents; Extents = rhs.Extents;
} }
return *this; return *this;
@ -117,8 +87,8 @@ namespace SHADE
} }
else else
{ {
center = rhs.center; Center = rhs.Center;
halfExtents = rhs.halfExtents; Extents = rhs.Extents;
} }
return *this; return *this;
@ -130,22 +100,22 @@ namespace SHADE
const SHVec3& SHBoundingBox::GetCenter() const noexcept const SHVec3& SHBoundingBox::GetCenter() const noexcept
{ {
return center; return Center;
} }
const SHVec3& SHBoundingBox::GetHalfExtents() const noexcept const SHVec3& SHBoundingBox::GetHalfExtents() const noexcept
{ {
return halfExtents; return Extents;
} }
SHVec3 SHBoundingBox::GetMin() const noexcept SHVec3 SHBoundingBox::GetMin() const noexcept
{ {
return center - halfExtents; return SHVec3{ Center.x - Extents.x, Center.y - Extents.y, Center.z - Extents.z };
} }
SHVec3 SHBoundingBox::GetMax() const noexcept SHVec3 SHBoundingBox::GetMax() const noexcept
{ {
return center + halfExtents; return SHVec3{ Center.x + Extents.x, Center.y + Extents.y, Center.z + Extents.z };
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -154,36 +124,42 @@ namespace SHADE
void SHBoundingBox::SetCenter(const SHVec3& newCenter) noexcept void SHBoundingBox::SetCenter(const SHVec3& newCenter) noexcept
{ {
center = newCenter; Center = newCenter;
} }
void SHBoundingBox::SetHalfExtents(const SHVec3& newHalfExtents) noexcept void SHBoundingBox::SetHalfExtents(const SHVec3& newHalfExtents) noexcept
{ {
halfExtents = newHalfExtents; Extents = newHalfExtents;
} }
void SHBoundingBox::SetMin(const SHVec3& min) noexcept void SHBoundingBox::SetMin(const SHVec3& min) noexcept
{ {
const SHVec3 MAX = center + halfExtents; const SHVec3 MAX = GetMax();
center = SHVec3::Lerp(min, MAX, 0.5f); Center = SHVec3::Lerp(min, MAX, 0.5f);
halfExtents = SHVec3::Abs((MAX - min) * 0.5f); Extents = SHVec3::Abs((MAX - min) * 0.5f);
} }
void SHBoundingBox::SetMax(const SHVec3& max) noexcept void SHBoundingBox::SetMax(const SHVec3& max) noexcept
{ {
const SHVec3 MIN = center - halfExtents; const SHVec3 MIN = GetMin();
center = SHVec3::Lerp(MIN, max, 0.5f); Center = SHVec3::Lerp(MIN, max, 0.5f);
halfExtents = SHVec3::Abs((max - MIN) * 0.5f); Extents = SHVec3::Abs((max - MIN) * 0.5f);
} }
void SHBoundingBox::SetMinMax(const SHVec3& min, const SHVec3& max) noexcept void SHBoundingBox::SetMinMax(const SHVec3& min, const SHVec3& max) noexcept
{ {
center = SHVec3::Lerp(min, max, 0.5f); Center = SHVec3::Lerp(min, max, 0.5f);
halfExtents = SHVec3::Abs((max - min) * 0.5f); Extents = SHVec3::Abs((max - min) * 0.5f);
} }
std::vector<SHVec3> SHBoundingBox::GetVertices() const noexcept
{
std::vector<SHVec3> vertices{ 8 };
GetCorners(vertices.data());
return vertices;
}
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
/* Public Function Member Definitions */ /* Public Function Member Definitions */
@ -191,43 +167,29 @@ namespace SHADE
bool SHBoundingBox::TestPoint(const SHVec3& point) noexcept bool SHBoundingBox::TestPoint(const SHVec3& point) noexcept
{ {
const SHVec3 V = SHVec3::Abs(point - center); return BoundingBox::Contains(point);
for (size_t i = 0; i < SHVec3::SIZE; ++i)
{
if (V[i] > halfExtents[i])
return false;
}
return true;
} }
bool SHBoundingBox::Raycast(const SHVec3& SHRay, float& distance) noexcept bool SHBoundingBox::Raycast(const SHRay& ray, float& distance) noexcept
{ {
return false; return BoundingBox::Intersects(ray.position, ray.direction, distance);
} }
bool SHBoundingBox::Contains(const SHBoundingBox& rhs) const noexcept bool SHBoundingBox::Contains(const SHBoundingBox& rhs) const noexcept
{ {
const SHVec3 V = SHVec3::Abs(rhs.center - center); return BoundingBox::Contains(rhs);
for (size_t i = 0; i < SHVec3::SIZE; ++i)
{
if (V[i] > rhs.halfExtents[i])
return false;
}
return true;
} }
float SHBoundingBox::Volume() const noexcept float SHBoundingBox::Volume() const noexcept
{ {
return 8.0f * (halfExtents.x * halfExtents.y * halfExtents.z); return 8.0f * (Extents.x * Extents.y * Extents.z);
} }
float SHBoundingBox::SurfaceArea() const noexcept float SHBoundingBox::SurfaceArea() const noexcept
{ {
return 8.0f * ((halfExtents.x * halfExtents.y) return 8.0f * ((Extents.x * Extents.y)
+ (halfExtents.x * halfExtents.z) + (Extents.x * Extents.z)
+ (halfExtents.y * halfExtents.z)); + (Extents.y * Extents.z));
} }
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
@ -236,37 +198,31 @@ namespace SHADE
SHBoundingBox SHBoundingBox::Combine(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept SHBoundingBox SHBoundingBox::Combine(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept
{ {
if (lhs.Contains(rhs)) SHBoundingBox result;
return lhs; CreateMerged(result, lhs, rhs);
if (rhs.Contains(lhs))
return rhs;
const SHVec3 LHS_MIN = lhs.GetMin();
const SHVec3 LHS_MAX = lhs.GetMax();
const SHVec3 RHS_MIN = rhs.GetMin();
const SHVec3 RHS_MAX = rhs.GetMax();
SHVec3 min = SHVec3::Min({ LHS_MIN, RHS_MIN });
SHVec3 max = SHVec3::Max({ LHS_MAX, RHS_MAX });
SHBoundingBox result{ lhs };
result.SetMinMax(min, max);
return result; return result;
} }
bool SHBoundingBox::Intersect(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept bool SHBoundingBox::Intersect(const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept
{ {
const SHVec3 V = SHVec3::Abs(lhs.center - rhs.center); return lhs.Intersects(rhs);
const SHVec3 D = lhs.halfExtents + rhs.halfExtents; }
for (size_t i = 0; i < SHVec3::SIZE; ++i) SHBoundingBox SHBoundingBox::BuildFromBoxes(const SHBoundingBox* boxes, size_t numBoxes) noexcept
{ {
if (V[i] > D[i]) SHBoundingBox result;
return false;
}
return true; for (size_t i = 1; i < numBoxes; ++i)
CreateMerged(result, boxes[i - 1], boxes[i]);
return result;
}
SHBoundingBox SHBoundingBox::BuildFromVertices(const SHVec3* vertices, size_t numVertices, size_t stride) noexcept
{
SHBoundingBox result;
CreateFromPoints(result, numVertices, vertices, stride);
return result;
} }
} // namespace SHADE } // namespace SHADE

View File

@ -22,32 +22,43 @@ namespace SHADE
/* Type Definitions */ /* Type Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
// TODO(Diren): Use DirectX BoundingBox instead of custom class SH_API SHBoundingBox : public SHShape,
class SH_API SHBoundingBox : public SHShape private DirectX::BoundingBox
{ {
public: public:
/*---------------------------------------------------------------------------------*/
/* Static Data Members */
/*---------------------------------------------------------------------------------*/
static constexpr size_t NUM_VERTICES = 8;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Constructors & Destructor */ /* Constructors & Destructor */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept; ~SHBoundingBox () noexcept = default;
SHBoundingBox (const SHVec3* vertices, size_t numVertices) noexcept;
SHBoundingBox (const SHBoundingBox* boxes, size_t numBoxes) noexcept;
SHBoundingBox (const SHBoundingBox& rhs) noexcept; SHBoundingBox () noexcept;
SHBoundingBox (SHBoundingBox&& rhs) noexcept; SHBoundingBox (const SHVec3& center, const SHVec3& halfExtents) noexcept;
SHBoundingBox (const SHBoundingBox& rhs) noexcept;
SHBoundingBox (SHBoundingBox&& rhs) noexcept;
SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept; /*---------------------------------------------------------------------------------*/
SHBoundingBox& operator= (SHBoundingBox&& rhs) noexcept; /* Operator Overloads */
/*---------------------------------------------------------------------------------*/
SHBoundingBox& operator= (const SHBoundingBox& rhs) noexcept;
SHBoundingBox& operator= (SHBoundingBox&& rhs) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Getter Functions */ /* Getter Functions */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] const SHVec3& GetCenter () const noexcept; [[nodiscard]] const SHVec3& GetCenter () const noexcept;
[[nodiscard]] const SHVec3& GetHalfExtents () const noexcept; [[nodsicard]] const SHVec3& GetHalfExtents() const noexcept;
[[nodiscard]] SHVec3 GetMin () const noexcept; [[nodiscard]] SHVec3 GetMin () const noexcept;
[[nodiscard]] SHVec3 GetMax () const noexcept; [[nodiscard]] SHVec3 GetMax () const noexcept;
[[nodiscard]] std::vector<SHVec3> GetVertices () const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Setter Functions */ /* Setter Functions */
@ -59,32 +70,25 @@ namespace SHADE
void SetMax (const SHVec3& max) noexcept; void SetMax (const SHVec3& max) noexcept;
void SetMinMax (const SHVec3& min, const SHVec3& max) noexcept; void SetMinMax (const SHVec3& min, const SHVec3& max) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override; [[nodiscard]] bool TestPoint (const SHVec3& point) noexcept override;
[[nodiscard]] bool Raycast (const SHVec3& SHRay, float& distance) noexcept override; [[nodiscard]] bool Raycast (const SHRay& ray, float& distance) noexcept override;
[[nodiscard]] bool Contains (const SHBoundingBox& rhs) const noexcept; [[nodiscard]] bool Contains (const SHBoundingBox& rhs) const noexcept;
[[nodiscard]] float Volume () const noexcept; [[nodiscard]] float Volume () const noexcept;
[[nodiscard]] float SurfaceArea () const noexcept; [[nodiscard]] float SurfaceArea () const noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Static Function Members */ /* Static Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] static SHBoundingBox Combine (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; [[nodiscard]] static SHBoundingBox Combine (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept;
[[nodiscard]] static bool Intersect (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept; [[nodiscard]] static bool Intersect (const SHBoundingBox& lhs, const SHBoundingBox& rhs) noexcept;
[[nodiscard]] static SHBoundingBox BuildFromBoxes (const SHBoundingBox* boxes, size_t numBoxes) noexcept;
private: [[nodiscard]] static SHBoundingBox BuildFromVertices (const SHVec3* vertices, size_t numVertices, size_t stride = 0) noexcept;
/*---------------------------------------------------------------------------------*/
/* Data Members */
/*---------------------------------------------------------------------------------*/
SHVec3 center;
SHVec3 halfExtents;
}; };

View File

@ -71,8 +71,8 @@ namespace SHADE
/* Function Members */ /* Function Members */
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
[[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0; [[nodiscard]] virtual bool TestPoint (const SHVec3& point) noexcept = 0;
[[nodiscard]] virtual bool Raycast (const SHVec3& SHRay, float& distance) noexcept = 0; [[nodiscard]] virtual bool Raycast (const SHRay& ray, float& distance) noexcept = 0;
protected: protected:
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/

View File

@ -38,6 +38,10 @@ namespace SHADE
: XMFLOAT2( 0.0f, 0.0f ) : XMFLOAT2( 0.0f, 0.0f )
{} {}
SHVec2::SHVec2(const XMFLOAT2& xmfloat2) noexcept
: XMFLOAT2 ( xmfloat2.x, xmfloat2.y )
{}
SHVec2::SHVec2(float n) noexcept SHVec2::SHVec2(float n) noexcept
: XMFLOAT2( n, n ) : XMFLOAT2( n, n )
{} {}
@ -50,6 +54,11 @@ namespace SHADE
/* Operator Overload Definitions */ /* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHVec2::operator XMVECTOR() const noexcept
{
return XMLoadFloat2(this);
}
SHVec2& SHVec2::operator+=(const SHVec2& rhs) noexcept SHVec2& SHVec2::operator+=(const SHVec2& rhs) noexcept
{ {
return *this = *this + rhs; return *this = *this + rhs;
@ -83,22 +92,16 @@ namespace SHADE
SHVec2 SHVec2::operator+(const SHVec2& rhs) const noexcept SHVec2 SHVec2::operator+(const SHVec2& rhs) const noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V1 = XMLoadFloat2(this);
const XMVECTOR V2 = XMLoadFloat2(&rhs);
XMStoreFloat2(&result, XMVectorAdd(V1, V2)); XMStoreFloat2(&result, XMVectorAdd(*this, rhs));
return result; return result;
} }
SHVec2 SHVec2::operator-(const SHVec2& rhs) const noexcept SHVec2 SHVec2::operator-(const SHVec2& rhs) const noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V1 = XMLoadFloat2(this);
const XMVECTOR V2 = XMLoadFloat2(&rhs);
XMStoreFloat2(&result, XMVectorSubtract(V1, V2)); XMStoreFloat2(&result, XMVectorSubtract(*this, rhs));
return result; return result;
} }
@ -110,59 +113,43 @@ namespace SHADE
SHVec2 SHVec2::operator*(const SHVec2& rhs) const noexcept SHVec2 SHVec2::operator*(const SHVec2& rhs) const noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V1 = XMLoadFloat2(this);
const XMVECTOR V2 = XMLoadFloat2(&rhs);
XMStoreFloat2(&result, XMVectorMultiply(V1, V2)); XMStoreFloat2(&result, XMVectorMultiply(*this, rhs));
return result; return result;
} }
SHVec2 SHVec2::operator*(float rhs) const noexcept SHVec2 SHVec2::operator*(float rhs) const noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(this);
XMStoreFloat2(&result, XMVectorScale(V, rhs)); XMStoreFloat2(&result, XMVectorScale(*this, rhs));
return result; return result;
} }
SHVec2 SHVec2::operator/(const SHVec2& rhs) const noexcept SHVec2 SHVec2::operator/(const SHVec2& rhs) const noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V1 = XMLoadFloat2(this);
const XMVECTOR V2 = XMLoadFloat2(&rhs);
XMStoreFloat2(&result, XMVectorDivide(V1, V2)); XMStoreFloat2(&result, XMVectorDivide(*this, rhs));
return result; return result;
} }
SHVec2 SHVec2::operator/(float rhs) const noexcept SHVec2 SHVec2::operator/(float rhs) const noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(this);
XMStoreFloat2(&result, XMVectorScale(V, 1.0f / rhs)); XMStoreFloat2(&result, XMVectorScale(*this, 1.0f / rhs));
return result; return result;
} }
bool SHVec2::operator==(const SHVec2& rhs) const noexcept bool SHVec2::operator==(const SHVec2& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat2(this); return XMVector2Equal(*this, rhs);
const XMVECTOR V2 = XMLoadFloat2(&rhs);
return XMVector2Equal(V1, V2);
} }
bool SHVec2::operator!=(const SHVec2& rhs) const noexcept bool SHVec2::operator!=(const SHVec2& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat2(this); return XMVector2NotEqual(*this, rhs);
const XMVECTOR V2 = XMLoadFloat2(&rhs);
return XMVector2NotEqual(V1, V2);
} }
float& SHVec2::operator[](int index) float& SHVec2::operator[](int index)
@ -216,10 +203,8 @@ namespace SHADE
SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept SHVec2 operator* (float lhs, const SHVec2& rhs) noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(&rhs);
XMStoreFloat2(&result, XMVectorScale(V, lhs)); XMStoreFloat2(&result, XMVectorScale(rhs, lhs));
return result; return result;
} }
@ -229,16 +214,12 @@ namespace SHADE
float SHVec2::Length() const noexcept float SHVec2::Length() const noexcept
{ {
const XMVECTOR V = XMLoadFloat2(this); return XMVectorGetX(XMVector2Length(*this));
return XMVectorGetX(XMVector2Length(V));
} }
float SHVec2::LengthSquared() const noexcept float SHVec2::LengthSquared() const noexcept
{ {
const XMVECTOR V = XMLoadFloat2(this); return XMVectorGetX(XMVector2LengthSq(*this));
return XMVectorGetX(XMVector2LengthSq(V));
} }
std::string SHVec2::ToString() const noexcept std::string SHVec2::ToString() const noexcept
@ -251,20 +232,14 @@ namespace SHADE
float SHVec2::Dot(const SHVec2& rhs) const noexcept float SHVec2::Dot(const SHVec2& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat2(this); return XMVectorGetX(XMVector2Dot(*this, rhs));
const XMVECTOR V2 = XMLoadFloat2(&rhs);
return XMVectorGetX(XMVector2Dot(V1, V2));
} }
SHVec2 SHVec2::Cross(const SHVec2& rhs) const noexcept SHVec2 SHVec2::Cross(const SHVec2& rhs) const noexcept
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V1 = XMLoadFloat2(this); XMStoreFloat2(&result, XMVector2Cross(*this, rhs));
const XMVECTOR V2 = XMLoadFloat2(&rhs);
XMStoreFloat2(&result, XMVector2Cross(V1, V2));
return result; return result;
} }
@ -276,9 +251,7 @@ namespace SHADE
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(&vec2); XMStoreFloat2(&result, XMVector2Normalize(vec2));
XMStoreFloat2(&result, XMVector2Normalize(V));
return result; return result;
} }
@ -297,10 +270,10 @@ namespace SHADE
SHVec2 result; SHVec2 result;
XMVECTOR min = XMLoadFloat2(&(*vec2s.begin())); XMVECTOR min = *vec2s.begin();
for (auto it = vec2s.begin() + 1; it != vec2s.end(); ++it) for (auto it = vec2s.begin() + 1; it != vec2s.end(); ++it)
{ {
const XMVECTOR tmp = XMLoadFloat2(&(*it)); const XMVECTOR tmp = *it;
min = XMVectorMin(min, tmp); min = XMVectorMin(min, tmp);
} }
@ -318,10 +291,10 @@ namespace SHADE
SHVec2 result; SHVec2 result;
XMVECTOR max = XMLoadFloat2(&(*vec2s.begin())); XMVECTOR max = *vec2s.begin();
for (auto it = vec2s.begin() + 1; it != vec2s.end(); ++it) for (auto it = vec2s.begin() + 1; it != vec2s.end(); ++it)
{ {
const XMVECTOR tmp = XMLoadFloat2(&(*it)); const XMVECTOR tmp = *it;
max = XMVectorMax(max, tmp); max = XMVectorMax(max, tmp);
} }
@ -333,11 +306,7 @@ namespace SHADE
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(&v); XMStoreFloat2(&result, XMVectorClamp(v, vMin, vMax));
const XMVECTOR MIN = XMLoadFloat2(&vMin);
const XMVECTOR MAX = XMLoadFloat2(&vMax);
XMStoreFloat2(&result, XMVectorClamp(V, MIN, MAX));
return result; return result;
} }
@ -345,10 +314,7 @@ namespace SHADE
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V1 = XMLoadFloat2(&a); XMStoreFloat2(&result, XMVectorLerp(a, b, t));
const XMVECTOR V2 = XMLoadFloat2(&b);
XMStoreFloat2(&result, XMVectorLerp(V1, V2, t));
return result; return result;
} }
@ -369,10 +335,7 @@ namespace SHADE
float SHVec2::Angle(const SHVec2& lhs, const SHVec2& rhs) noexcept float SHVec2::Angle(const SHVec2& lhs, const SHVec2& rhs) noexcept
{ {
const XMVECTOR V1 = XMLoadFloat2(&lhs); return XMVectorGetX(XMVector2AngleBetweenVectors(lhs, rhs));
const XMVECTOR V2 = XMLoadFloat2(&rhs);
return XMVectorGetX(XMVector2AngleBetweenVectors(V1, V2));
} }
float SHVec2::Dot(const SHVec2& lhs, const SHVec2& rhs) noexcept float SHVec2::Dot(const SHVec2& lhs, const SHVec2& rhs) noexcept
@ -384,11 +347,10 @@ namespace SHADE
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR U = XMLoadFloat2(&u);
const float V_DOT_U = Dot(v, u); const float V_DOT_U = Dot(v, u);
const float U_LENSQ = u.LengthSquared(); const float U_LENSQ = u.LengthSquared();
XMStoreFloat2(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); XMStoreFloat2(&result, XMVectorScale(u, V_DOT_U / U_LENSQ));
return result; return result;
} }
@ -396,10 +358,8 @@ namespace SHADE
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(&v);
const XMVECTOR N = XMLoadFloat2(&normal);
XMStoreFloat2(&result, XMVector2Reflect(V, N)); XMStoreFloat2(&result, XMVector2Reflect(v, normal));
return result; return result;
} }
@ -407,10 +367,9 @@ namespace SHADE
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(&v);
const XMMATRIX R = XMMatrixRotationZ(angleInRad); const XMMATRIX R = XMMatrixRotationZ(angleInRad);
XMStoreFloat2(&result, XMVector2Transform(V, R)); XMStoreFloat2(&result, XMVector2Transform(v, R));
return result; return result;
} }
@ -418,10 +377,9 @@ namespace SHADE
{ {
SHVec2 result; SHVec2 result;
const XMVECTOR V = XMLoadFloat2(&v);
const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); const XMMATRIX TF = XMLoadFloat4x4(&transformMtx);
XMStoreFloat2(&result, XMVector2TransformCoord(V, TF)); XMStoreFloat2(&result, XMVector2TransformCoord(v, TF));
return result; return result;
} }

View File

@ -52,9 +52,10 @@ namespace SHADE
SHVec2 (SHVec2&& rhs) = default; SHVec2 (SHVec2&& rhs) = default;
~SHVec2 () = default; ~SHVec2 () = default;
SHVec2 () noexcept; SHVec2 () noexcept;
SHVec2 (float n) noexcept; SHVec2 (const XMFLOAT2& xmfloat2) noexcept;
SHVec2 (float x, float y) noexcept; SHVec2 (float n) noexcept;
SHVec2 (float x, float y) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
@ -63,6 +64,8 @@ namespace SHADE
SHVec2& operator= (const SHVec2& rhs) = default; SHVec2& operator= (const SHVec2& rhs) = default;
SHVec2& operator= (SHVec2&& rhs) = default; SHVec2& operator= (SHVec2&& rhs) = default;
operator DirectX::XMVECTOR () const noexcept;
SHVec2& operator+= (const SHVec2& rhs) noexcept; SHVec2& operator+= (const SHVec2& rhs) noexcept;
SHVec2& operator-= (const SHVec2& rhs) noexcept; SHVec2& operator-= (const SHVec2& rhs) noexcept;
SHVec2& operator*= (const SHVec2& rhs) noexcept; SHVec2& operator*= (const SHVec2& rhs) noexcept;

View File

@ -43,6 +43,10 @@ namespace SHADE
: XMFLOAT3( 0.0f, 0.0f, 0.0f ) : XMFLOAT3( 0.0f, 0.0f, 0.0f )
{} {}
SHVec3::SHVec3(const XMFLOAT3& xmfloat3) noexcept
: XMFLOAT3 ( xmfloat3.x, xmfloat3.y, xmfloat3.z )
{}
SHVec3::SHVec3(float n) noexcept SHVec3::SHVec3(float n) noexcept
: XMFLOAT3( n, n, n ) : XMFLOAT3( n, n, n )
{} {}
@ -55,6 +59,12 @@ namespace SHADE
/* Operator Overload Definitions */ /* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHVec3::operator XMVECTOR() const noexcept
{
return XMLoadFloat3(this);
}
SHVec3& SHVec3::operator+=(const SHVec3& rhs) noexcept SHVec3& SHVec3::operator+=(const SHVec3& rhs) noexcept
{ {
return *this = *this + rhs; return *this = *this + rhs;
@ -88,22 +98,16 @@ namespace SHADE
SHVec3 SHVec3::operator+(const SHVec3& rhs) const noexcept SHVec3 SHVec3::operator+(const SHVec3& rhs) const noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V1 = XMLoadFloat3(this);
const XMVECTOR V2 = XMLoadFloat3(&rhs);
XMStoreFloat3(&result, XMVectorAdd(V1, V2)); XMStoreFloat3(&result, XMVectorAdd(*this, rhs));
return result; return result;
} }
SHVec3 SHVec3::operator-(const SHVec3& rhs) const noexcept SHVec3 SHVec3::operator-(const SHVec3& rhs) const noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V1 = XMLoadFloat3(this);
const XMVECTOR V2 = XMLoadFloat3(&rhs);
XMStoreFloat3(&result, XMVectorSubtract(V1, V2)); XMStoreFloat3(&result, XMVectorSubtract(*this, rhs));
return result; return result;
} }
@ -116,59 +120,43 @@ namespace SHADE
SHVec3 SHVec3::operator*(const SHVec3& rhs) const noexcept SHVec3 SHVec3::operator*(const SHVec3& rhs) const noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V1 = XMLoadFloat3(this);
const XMVECTOR V2 = XMLoadFloat3(&rhs);
XMStoreFloat3(&result, XMVectorMultiply(V1, V2)); XMStoreFloat3(&result, XMVectorMultiply(*this, rhs));
return result; return result;
} }
SHVec3 SHVec3::operator*(float rhs) const noexcept SHVec3 SHVec3::operator*(float rhs) const noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(this);
XMStoreFloat3(&result, XMVectorScale(V, rhs)); XMStoreFloat3(&result, XMVectorScale(*this, rhs));
return result; return result;
} }
SHVec3 SHVec3::operator/(const SHVec3& rhs) const noexcept SHVec3 SHVec3::operator/(const SHVec3& rhs) const noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V1 = XMLoadFloat3(this);
const XMVECTOR V2 = XMLoadFloat3(&rhs);
XMStoreFloat3(&result, XMVectorDivide(V1, V2)); XMStoreFloat3(&result, XMVectorDivide(*this, rhs));
return result; return result;
} }
SHVec3 SHVec3::operator/(float rhs) const noexcept SHVec3 SHVec3::operator/(float rhs) const noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(this);
XMStoreFloat3(&result, XMVectorScale(V, 1.0f / rhs)); XMStoreFloat3(&result, XMVectorScale(*this, 1.0f / rhs));
return result; return result;
} }
bool SHVec3::operator==(const SHVec3& rhs) const noexcept bool SHVec3::operator==(const SHVec3& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat3(this); return XMVector3Equal(*this, rhs);
const XMVECTOR V2 = XMLoadFloat3(&rhs);
return XMVector3Equal(V1, V2);
} }
bool SHVec3::operator!=(const SHVec3& rhs) const noexcept bool SHVec3::operator!=(const SHVec3& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat3(this); return XMVector3NotEqual(*this, rhs);
const XMVECTOR V2 = XMLoadFloat3(&rhs);
return XMVector3NotEqual(V1, V2);
} }
float& SHVec3::operator[](int index) float& SHVec3::operator[](int index)
@ -226,10 +214,8 @@ namespace SHADE
SHVec3 operator* (float lhs, const SHVec3& rhs) noexcept SHVec3 operator* (float lhs, const SHVec3& rhs) noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&rhs);
XMStoreFloat3(&result, XMVectorScale(V, lhs)); XMStoreFloat3(&result, XMVectorScale(rhs, lhs));
return result; return result;
} }
@ -239,16 +225,12 @@ namespace SHADE
float SHVec3::Length() const noexcept float SHVec3::Length() const noexcept
{ {
const XMVECTOR V = XMLoadFloat3(this); return XMVectorGetX(XMVector3Length(*this));
return XMVectorGetX(XMVector3Length(V));
} }
float SHVec3::LengthSquared() const noexcept float SHVec3::LengthSquared() const noexcept
{ {
const XMVECTOR V = XMLoadFloat3(this); return XMVectorGetX(XMVector3LengthSq(*this));
return XMVectorGetX(XMVector3LengthSq(V));
} }
std::string SHVec3::ToString() const noexcept std::string SHVec3::ToString() const noexcept
@ -261,20 +243,14 @@ namespace SHADE
float SHVec3::Dot(const SHVec3& rhs) const noexcept float SHVec3::Dot(const SHVec3& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat3(this); return XMVectorGetX(XMVector3Dot(*this, rhs));
const XMVECTOR V2 = XMLoadFloat3(&rhs);
return XMVectorGetX(XMVector3Dot(V1, V2));
} }
SHVec3 SHVec3::Cross(const SHVec3& rhs) const noexcept SHVec3 SHVec3::Cross(const SHVec3& rhs) const noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V1 = XMLoadFloat3(this); XMStoreFloat3(&result, XMVector3Cross(*this, rhs));
const XMVECTOR V2 = XMLoadFloat3(&rhs);
XMStoreFloat3(&result, XMVector3Cross(V1, V2));
return result; return result;
} }
@ -286,9 +262,7 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v); XMStoreFloat3(&result, XMVector3Normalize(v));
XMStoreFloat3(&result, XMVector3Normalize(V));
return result; return result;
} }
@ -307,10 +281,10 @@ namespace SHADE
SHVec3 result; SHVec3 result;
XMVECTOR min = XMLoadFloat3(&(*vs.begin())); XMVECTOR min = *vs.begin();
for (auto it = vs.begin() + 1; it != vs.end(); ++it) for (auto it = vs.begin() + 1; it != vs.end(); ++it)
{ {
const XMVECTOR tmp = XMLoadFloat3(&(*it)); const XMVECTOR tmp = *it;
min = XMVectorMin(min, tmp); min = XMVectorMin(min, tmp);
} }
@ -328,10 +302,10 @@ namespace SHADE
SHVec3 result; SHVec3 result;
XMVECTOR max = XMLoadFloat3(&(*vs.begin())); XMVECTOR max = *vs.begin();
for (auto it = vs.begin() + 1; it != vs.end(); ++it) for (auto it = vs.begin() + 1; it != vs.end(); ++it)
{ {
const XMVECTOR tmp = XMLoadFloat3(&(*it)); const XMVECTOR tmp = *it;
max = XMVectorMax(max, tmp); max = XMVectorMax(max, tmp);
} }
@ -343,11 +317,7 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v); XMStoreFloat3(&result, XMVectorClamp(v, vMin, vMax));
const XMVECTOR MIN = XMLoadFloat3(&vMin);
const XMVECTOR MAX = XMLoadFloat3(&vMax);
XMStoreFloat3(&result, XMVectorClamp(V, MIN, MAX));
return result; return result;
} }
@ -355,10 +325,7 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V1 = XMLoadFloat3(&a); XMStoreFloat3(&result, XMVectorLerp(a, b, t));
const XMVECTOR V2 = XMLoadFloat3(&b);
XMStoreFloat3(&result, XMVectorLerp(V1, V2, t));
return result; return result;
} }
@ -382,7 +349,7 @@ namespace SHADE
const XMVECTOR V1 = XMLoadFloat3(&lhs); const XMVECTOR V1 = XMLoadFloat3(&lhs);
const XMVECTOR V2 = XMLoadFloat3(&rhs); const XMVECTOR V2 = XMLoadFloat3(&rhs);
return XMVectorGetX(XMVector3AngleBetweenVectors(V1, V2)); return XMVectorGetX(XMVector3AngleBetweenVectors(lhs, rhs));
} }
float SHVec3::Dot(const SHVec3& lhs, const SHVec3& rhs) noexcept float SHVec3::Dot(const SHVec3& lhs, const SHVec3& rhs) noexcept
@ -399,22 +366,18 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR U = XMLoadFloat3(&u);
const float V_DOT_U = Dot(v, u); const float V_DOT_U = Dot(v, u);
const float U_LENSQ = u.LengthSquared(); const float U_LENSQ = u.LengthSquared();
XMStoreFloat3(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); XMStoreFloat3(&result, XMVectorScale(u, V_DOT_U / U_LENSQ));
return result; return result;
} }
SHVec3 SHVec3::Reflect(const SHVec3& v, const SHVec3& normal) noexcept SHVec3 SHVec3::Reflect(const SHVec3& v, const SHVec3& normal) noexcept
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v);
const XMVECTOR N = XMLoadFloat3(&normal);
XMStoreFloat3(&result, XMVector3Reflect(V, N)); XMStoreFloat3(&result, XMVector3Reflect(v, normal));
return result; return result;
} }
@ -422,12 +385,9 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v); const XMVECTOR Q = XMQuaternionRotationAxis(axis, angleInRad);
const XMVECTOR AXIS = XMLoadFloat3(&axis); XMStoreFloat3(&result, XMVector3Rotate(v, Q));
const XMVECTOR Q = XMQuaternionRotationAxis(AXIS, angleInRad);
XMStoreFloat3(&result, XMVector3Rotate(V, Q));
return result; return result;
} }
@ -435,10 +395,9 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v); const XMMATRIX R = XMMatrixRotationX(angleInRad);
const XMMATRIX R = XMMatrixRotationX(angleInRad);
XMStoreFloat3(&result, XMVector3TransformCoord(V, R)); XMStoreFloat3(&result, XMVector3TransformCoord(v, R));
return result; return result;
} }
@ -446,10 +405,9 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v); const XMMATRIX R = XMMatrixRotationY(angleInRad);
const XMMATRIX R = XMMatrixRotationY(angleInRad);
XMStoreFloat3(&result, XMVector3TransformCoord(V, R)); XMStoreFloat3(&result, XMVector3TransformCoord(v, R));
return result; return result;
} }
@ -457,10 +415,9 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v); const XMMATRIX R = XMMatrixRotationZ(angleInRad);
const XMMATRIX R = XMMatrixRotationZ(angleInRad);
XMStoreFloat3(&result, XMVector3TransformCoord(V, R)); XMStoreFloat3(&result, XMVector3TransformCoord(v, R));
return result; return result;
} }
@ -468,10 +425,9 @@ namespace SHADE
{ {
SHVec3 result; SHVec3 result;
const XMVECTOR V = XMLoadFloat3(&v);
const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); const XMMATRIX TF = XMLoadFloat4x4(&transformMtx);
XMStoreFloat3(&result, XMVector3TransformCoord(V, TF)); XMStoreFloat3(&result, XMVector3TransformCoord(v, TF));
return result; return result;
} }
} }

View File

@ -58,6 +58,7 @@ namespace SHADE
~SHVec3 () = default; ~SHVec3 () = default;
SHVec3 () noexcept; SHVec3 () noexcept;
SHVec3 (const XMFLOAT3& xmfloat3) noexcept;
SHVec3 (float n) noexcept; SHVec3 (float n) noexcept;
SHVec3 (float x, float y, float z) noexcept; SHVec3 (float x, float y, float z) noexcept;
@ -68,6 +69,8 @@ namespace SHADE
SHVec3& operator= (const SHVec3& rhs) = default; SHVec3& operator= (const SHVec3& rhs) = default;
SHVec3& operator= (SHVec3&& rhs) = default; SHVec3& operator= (SHVec3&& rhs) = default;
operator DirectX::XMVECTOR () const noexcept;
SHVec3& operator+= (const SHVec3& rhs) noexcept; SHVec3& operator+= (const SHVec3& rhs) noexcept;
SHVec3& operator-= (const SHVec3& rhs) noexcept; SHVec3& operator-= (const SHVec3& rhs) noexcept;
SHVec3& operator*= (const SHVec3& rhs) noexcept; SHVec3& operator*= (const SHVec3& rhs) noexcept;

View File

@ -38,6 +38,10 @@ namespace SHADE
: XMFLOAT4( 0.0f, 0.0f, 0.0f, 0.0f ) : XMFLOAT4( 0.0f, 0.0f, 0.0f, 0.0f )
{} {}
SHVec4::SHVec4(const XMFLOAT4& xmfloat4) noexcept
: XMFLOAT4( xmfloat4.x, xmfloat4.y, xmfloat4.z, xmfloat4.w )
{}
SHVec4::SHVec4(float _x, float _y, float _z, float _w) noexcept SHVec4::SHVec4(float _x, float _y, float _z, float _w) noexcept
: XMFLOAT4( _x, _y, _z, _w ) : XMFLOAT4( _x, _y, _z, _w )
{} {}
@ -46,6 +50,11 @@ namespace SHADE
/* Operator Overload Definitions */ /* Operator Overload Definitions */
/*-----------------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------------------*/
SHVec4::operator XMVECTOR() const noexcept
{
return XMLoadFloat4(this);
}
SHVec4& SHVec4::operator+=(const SHVec4& rhs) noexcept SHVec4& SHVec4::operator+=(const SHVec4& rhs) noexcept
{ {
return *this = *this + rhs; return *this = *this + rhs;
@ -79,22 +88,16 @@ namespace SHADE
SHVec4 SHVec4::operator+(const SHVec4& rhs) const noexcept SHVec4 SHVec4::operator+(const SHVec4& rhs) const noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V1 = XMLoadFloat4(this);
const XMVECTOR V2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorAdd(V1, V2)); XMStoreFloat4(&result, XMVectorAdd(*this, rhs));
return result; return result;
} }
SHVec4 SHVec4::operator-(const SHVec4& rhs) const noexcept SHVec4 SHVec4::operator-(const SHVec4& rhs) const noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V1 = XMLoadFloat4(this);
const XMVECTOR V2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorSubtract(V1, V2)); XMStoreFloat4(&result, XMVectorSubtract(*this, rhs));
return result; return result;
} }
@ -106,59 +109,43 @@ namespace SHADE
SHVec4 SHVec4::operator*(const SHVec4& rhs) const noexcept SHVec4 SHVec4::operator*(const SHVec4& rhs) const noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V1 = XMLoadFloat4(this);
const XMVECTOR V2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorMultiply(V1, V2)); XMStoreFloat4(&result, XMVectorMultiply(*this, rhs));
return result; return result;
} }
SHVec4 SHVec4::operator*(float rhs) const noexcept SHVec4 SHVec4::operator*(float rhs) const noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(this);
XMStoreFloat4(&result, XMVectorScale(V, rhs)); XMStoreFloat4(&result, XMVectorScale(*this, rhs));
return result; return result;
} }
SHVec4 SHVec4::operator/(const SHVec4& rhs) const noexcept SHVec4 SHVec4::operator/(const SHVec4& rhs) const noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V1 = XMLoadFloat4(this);
const XMVECTOR V2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorDivide(V1, V2)); XMStoreFloat4(&result, XMVectorDivide(*this, rhs));
return result; return result;
} }
SHVec4 SHVec4::operator/(float rhs) const noexcept SHVec4 SHVec4::operator/(float rhs) const noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(this);
XMStoreFloat4(&result, XMVectorScale(V, 1.0f / rhs)); XMStoreFloat4(&result, XMVectorScale(*this, 1.0f / rhs));
return result; return result;
} }
bool SHVec4::operator==(const SHVec4& rhs) const noexcept bool SHVec4::operator==(const SHVec4& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat4(this); return XMVector4Equal(*this, rhs);
const XMVECTOR V2 = XMLoadFloat4(&rhs);
return XMVector4Equal(V1, V2);
} }
bool SHVec4::operator!=(const SHVec4& rhs) const noexcept bool SHVec4::operator!=(const SHVec4& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat4(this); return XMVector4NotEqual(*this, rhs);
const XMVECTOR V2 = XMLoadFloat4(&rhs);
return XMVector4NotEqual(V1, V2);
} }
float& SHVec4::operator[](int index) float& SHVec4::operator[](int index)
@ -220,10 +207,8 @@ namespace SHADE
SHVec4 operator* (float lhs, const SHVec4& rhs) noexcept SHVec4 operator* (float lhs, const SHVec4& rhs) noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVectorScale(V, lhs)); XMStoreFloat4(&result, XMVectorScale(rhs, lhs));
return result; return result;
} }
@ -233,30 +218,22 @@ namespace SHADE
float SHVec4::Length() const noexcept float SHVec4::Length() const noexcept
{ {
const XMVECTOR V = XMLoadFloat4(this); return XMVectorGetX(XMVector4Length(*this));
return XMVectorGetX(XMVector4Length(V));
} }
float SHVec4::Length3D() const noexcept float SHVec4::Length3D() const noexcept
{ {
const XMVECTOR V = XMLoadFloat4(this); return XMVectorGetX(XMVector3Length(*this));
return XMVectorGetX(XMVector3Length(V));
} }
float SHVec4::LengthSquared() const noexcept float SHVec4::LengthSquared() const noexcept
{ {
const XMVECTOR V = XMLoadFloat4(this); return XMVectorGetX(XMVector4LengthSq(*this));
return XMVectorGetX(XMVector4LengthSq(V));
} }
float SHVec4::LengthSquared3D() const noexcept float SHVec4::LengthSquared3D() const noexcept
{ {
const XMVECTOR V = XMLoadFloat4(this); return XMVectorGetX(XMVector3LengthSq(*this));
return XMVectorGetX(XMVector3LengthSq(V));
} }
std::string SHVec4::ToString() const noexcept std::string SHVec4::ToString() const noexcept
@ -269,28 +246,19 @@ namespace SHADE
float SHVec4::Dot(const SHVec4& rhs) const noexcept float SHVec4::Dot(const SHVec4& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat4(this); return XMVectorGetX(XMVector4Dot(*this, rhs));
const XMVECTOR V2 = XMLoadFloat4(&rhs);
return XMVectorGetX(XMVector4Dot(V1, V2));
} }
float SHVec4::Dot3D(const SHVec4& rhs) const noexcept float SHVec4::Dot3D(const SHVec4& rhs) const noexcept
{ {
const XMVECTOR V1 = XMLoadFloat4(this); return XMVectorGetX(XMVector3Dot(*this, rhs));
const XMVECTOR V2 = XMLoadFloat4(&rhs);
return XMVectorGetX(XMVector3Dot(V1, V2));
} }
SHVec4 SHVec4::Cross3D(const SHVec4& rhs) const noexcept SHVec4 SHVec4::Cross3D(const SHVec4& rhs) const noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V1 = XMLoadFloat4(this); XMStoreFloat4(&result, XMVector3Cross(*this, rhs));
const XMVECTOR V2 = XMLoadFloat4(&rhs);
XMStoreFloat4(&result, XMVector3Cross(V1, V2));
result.w = 1.0f; result.w = 1.0f;
return result; return result;
} }
@ -299,11 +267,7 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V3 = XMLoadFloat4(this); XMStoreFloat4(&result, XMVector4Cross(*this, v1, v2));
const XMVECTOR V1 = XMLoadFloat4(&v1);
const XMVECTOR V2 = XMLoadFloat4(&v2);
XMStoreFloat4(&result, XMVector4Cross(V3, V1, V2));
return result; return result;
} }
@ -315,9 +279,7 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(&v); XMStoreFloat4(&result, XMVector4Normalize(v));
XMStoreFloat4(&result, XMVector4Normalize(V));
return result; return result;
} }
@ -325,9 +287,7 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(&v); XMStoreFloat4(&result, XMVector3Normalize(v));
XMStoreFloat4(&result, XMVector3Normalize(V));
result.w = 1.0f; result.w = 1.0f;
return result; return result;
} }
@ -347,10 +307,10 @@ namespace SHADE
SHVec4 result; SHVec4 result;
XMVECTOR min = XMLoadFloat4(&(*vs.begin())); XMVECTOR min = *vs.begin();
for (auto it = vs.begin() + 1; it != vs.end(); ++it) for (auto it = vs.begin() + 1; it != vs.end(); ++it)
{ {
const XMVECTOR tmp = XMLoadFloat4(&(*it)); const XMVECTOR tmp = *it;
min = XMVectorMin(min, tmp); min = XMVectorMin(min, tmp);
} }
@ -368,10 +328,10 @@ namespace SHADE
SHVec4 result; SHVec4 result;
XMVECTOR max = XMLoadFloat4(&(*vs.begin())); XMVECTOR max = *vs.begin();
for (auto it = vs.begin() + 1; it != vs.end(); ++it) for (auto it = vs.begin() + 1; it != vs.end(); ++it)
{ {
const XMVECTOR tmp = XMLoadFloat4(&(*it)); const XMVECTOR tmp = *it;
max = XMVectorMax(max, tmp); max = XMVectorMax(max, tmp);
} }
@ -383,11 +343,7 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(&v); XMStoreFloat4(&result, XMVectorClamp(v, vMin, vMax));
const XMVECTOR MIN = XMLoadFloat4(&vMin);
const XMVECTOR MAX = XMLoadFloat4(&vMax);
XMStoreFloat4(&result, XMVectorClamp(V, MIN, MAX));
return result; return result;
} }
@ -395,10 +351,7 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V1 = XMLoadFloat4(&a); XMStoreFloat4(&result, XMVectorLerp(a, b, t));
const XMVECTOR V2 = XMLoadFloat4(&b);
XMStoreFloat4(&result, XMVectorLerp(V1, V2, t));
return result; return result;
} }
@ -429,18 +382,12 @@ namespace SHADE
float SHVec4::Angle(const SHVec4& lhs, const SHVec4& rhs) noexcept float SHVec4::Angle(const SHVec4& lhs, const SHVec4& rhs) noexcept
{ {
const XMVECTOR V1 = XMLoadFloat4(&lhs); return XMVectorGetX(XMVector4AngleBetweenVectors(lhs,rhs));
const XMVECTOR V2 = XMLoadFloat4(&rhs);
return XMVectorGetX(XMVector4AngleBetweenVectors(V1, V2));
} }
float SHVec4::Angle3D(const SHVec4& lhs, const SHVec4& rhs) noexcept float SHVec4::Angle3D(const SHVec4& lhs, const SHVec4& rhs) noexcept
{ {
const XMVECTOR V1 = XMLoadFloat4(&lhs); return XMVectorGetX(XMVector3AngleBetweenVectors(lhs,rhs));
const XMVECTOR V2 = XMLoadFloat4(&rhs);
return XMVectorGetX(XMVector3AngleBetweenVectors(V1, V2));
} }
float SHVec4::Dot(const SHVec4& lhs, const SHVec4& rhs) noexcept float SHVec4::Dot(const SHVec4& lhs, const SHVec4& rhs) noexcept
@ -467,11 +414,10 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR U = XMLoadFloat4(&u);
const float V_DOT_U = Dot(v, u); const float V_DOT_U = Dot(v, u);
const float U_LENSQ = u.LengthSquared(); const float U_LENSQ = u.LengthSquared();
XMStoreFloat4(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); XMStoreFloat4(&result, XMVectorScale(u, V_DOT_U / U_LENSQ));
return result; return result;
} }
@ -479,11 +425,10 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR U = XMLoadFloat4(&u);
const float V_DOT_U = Dot3D(v, u); const float V_DOT_U = Dot3D(v, u);
const float U_LENSQ = u.LengthSquared3D(); const float U_LENSQ = u.LengthSquared3D();
XMStoreFloat4(&result, XMVectorScale(U, V_DOT_U / U_LENSQ)); XMStoreFloat4(&result, XMVectorScale(u, V_DOT_U / U_LENSQ));
result.w = 1.0f; result.w = 1.0f;
return result; return result;
} }
@ -491,11 +436,8 @@ namespace SHADE
SHVec4 SHVec4::Reflect(const SHVec4& v, const SHVec4& normal) noexcept SHVec4 SHVec4::Reflect(const SHVec4& v, const SHVec4& normal) noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(&v);
const XMVECTOR N = XMLoadFloat4(&normal);
XMStoreFloat4(&result, XMVector4Reflect(V, N)); XMStoreFloat4(&result, XMVector4Reflect(v, normal));
result.w = 1.0f; result.w = 1.0f;
return result; return result;
} }
@ -503,11 +445,8 @@ namespace SHADE
SHVec4 SHVec4::Reflect3D(const SHVec4& v, const SHVec4& normal) noexcept SHVec4 SHVec4::Reflect3D(const SHVec4& v, const SHVec4& normal) noexcept
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(&v);
const XMVECTOR N = XMLoadFloat4(&normal);
XMStoreFloat4(&result, XMVector3Reflect(V, N)); XMStoreFloat4(&result, XMVector3Reflect(v, normal));
result.w = 1.0f; result.w = 1.0f;
return result; return result;
} }
@ -516,10 +455,9 @@ namespace SHADE
{ {
SHVec4 result; SHVec4 result;
const XMVECTOR V = XMLoadFloat4(&v);
const XMMATRIX TF = XMLoadFloat4x4(&transformMtx); const XMMATRIX TF = XMLoadFloat4x4(&transformMtx);
XMStoreFloat4(&result, XMVector3TransformCoord(V, TF)); XMStoreFloat4(&result, XMVector3TransformCoord(v, TF));
return result; return result;
} }
} }

View File

@ -52,8 +52,9 @@ namespace SHADE
SHVec4 (SHVec4&& rhs) = default; SHVec4 (SHVec4&& rhs) = default;
~SHVec4 () = default; ~SHVec4 () = default;
SHVec4 () noexcept; SHVec4 () noexcept;
SHVec4 (float x, float y, float z, float w) noexcept; SHVec4 (const XMFLOAT4& xmfloat4) noexcept;
SHVec4 (float x, float y, float z, float w) noexcept;
/*---------------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------------*/
/* Operator Overloads */ /* Operator Overloads */
@ -62,6 +63,8 @@ namespace SHADE
SHVec4& operator= (const SHVec4& rhs) = default; SHVec4& operator= (const SHVec4& rhs) = default;
SHVec4& operator= (SHVec4&& rhs) = default; SHVec4& operator= (SHVec4&& rhs) = default;
operator DirectX::XMVECTOR () const noexcept;
SHVec4& operator+= (const SHVec4& rhs) noexcept; SHVec4& operator+= (const SHVec4& rhs) noexcept;
SHVec4& operator-= (const SHVec4& rhs) noexcept; SHVec4& operator-= (const SHVec4& rhs) noexcept;
SHVec4& operator*= (const SHVec4& rhs) noexcept; SHVec4& operator*= (const SHVec4& rhs) noexcept;