DarkflameServer/dCommon/NiPoint3.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

205 lines
5.6 KiB
C
Raw Permalink Normal View History

#ifndef NIPOINT3_H
#define NIPOINT3_H
/*!
\file NiPoint3.hpp
\brief Defines a point in space in XYZ coordinates
*/
class NiPoint3;
class NiQuaternion;
typedef NiPoint3 Vector3; //!< The Vector3 class is technically the NiPoint3 class, but typedef'd for clarity in some cases
//! A custom class the defines a point in space
class NiPoint3 {
public:
float x{ 0 }; //!< The x position
float y{ 0 }; //!< The y position
float z{ 0 }; //!< The z position
2022-07-28 13:39:57 +00:00
//! Initializer
constexpr NiPoint3() = default;
2022-07-28 13:39:57 +00:00
//! Initializer
/*!
\param x The x coordinate
\param y The y coordinate
\param z The z coordinate
*/
constexpr NiPoint3(const float x, const float y, const float z) noexcept
: x{ x }
, y{ y }
, z{ z } {
}
2022-07-28 13:39:57 +00:00
//! Copy Constructor
/*!
\param point The point to copy
*/
constexpr NiPoint3(const NiPoint3& point) noexcept
: x{ point.x }
, y{ point.y }
, z{ point.z } {
}
2022-07-28 13:39:57 +00:00
// MARK: Getters / Setters
2022-07-28 13:39:57 +00:00
//! Gets the X coordinate
/*!
\return The x coordinate
*/
[[nodiscard]] constexpr float GetX() const noexcept;
2022-07-28 13:39:57 +00:00
//! Sets the X coordinate
/*!
\param x The x coordinate
*/
constexpr void SetX(const float x) noexcept;
2022-07-28 13:39:57 +00:00
//! Gets the Y coordinate
/*!
\return The y coordinate
*/
[[nodiscard]] constexpr float GetY() const noexcept;
2022-07-28 13:39:57 +00:00
//! Sets the Y coordinate
/*!
\param y The y coordinate
*/
constexpr void SetY(const float y) noexcept;
2022-07-28 13:39:57 +00:00
//! Gets the Z coordinate
/*!
\return The z coordinate
*/
[[nodiscard]] constexpr float GetZ() const noexcept;
2022-07-28 13:39:57 +00:00
//! Sets the Z coordinate
/*!
\param z The z coordinate
*/
constexpr void SetZ(const float z) noexcept;
2022-07-28 13:39:57 +00:00
// MARK: Member Functions
2022-07-28 13:39:57 +00:00
//! Gets the length of the vector
/*!
\return The scalar length of the vector
2022-07-28 13:39:57 +00:00
*/
[[nodiscard]] float Length() const;
2022-07-28 13:39:57 +00:00
//! Gets the squared length of a vector
/*!
\return The squared length of a vector
*/
[[nodiscard]] constexpr float SquaredLength() const noexcept;
2022-07-28 13:39:57 +00:00
//! Returns the dot product of the vector dotted with another vector
/*!
\param vec The second vector
\return The dot product of the two vectors
*/
[[nodiscard]] constexpr float DotProduct(const Vector3& vec) const noexcept;
2022-07-28 13:39:57 +00:00
//! Returns the cross product of the vector crossed with another vector
/*!
\param vec The second vector
\return The cross product of the two vectors
*/
[[nodiscard]] constexpr Vector3 CrossProduct(const Vector3& vec) const noexcept;
2022-07-28 13:39:57 +00:00
//! Unitize the vector
2022-07-28 13:39:57 +00:00
/*!
\returns The current vector
2022-07-28 13:39:57 +00:00
*/
[[nodiscard]] NiPoint3 Unitize() const;
2022-07-28 13:39:57 +00:00
// MARK: Operators
2022-07-28 13:39:57 +00:00
//! Operator to check for equality
constexpr bool operator==(const NiPoint3& point) const noexcept;
2022-07-28 13:39:57 +00:00
//! Operator to check for inequality
constexpr bool operator!=(const NiPoint3& point) const noexcept;
2022-07-28 13:39:57 +00:00
//! Operator for subscripting
constexpr float& operator[](const int i) noexcept;
2022-07-28 13:39:57 +00:00
//! Operator for subscripting
constexpr const float& operator[](const int i) const noexcept;
//! Operator for addition of vectors
constexpr NiPoint3 operator+(const NiPoint3& point) const noexcept;
//! Operator for addition of vectors
constexpr NiPoint3& operator+=(const NiPoint3& point) noexcept;
constexpr NiPoint3& operator*=(const float scalar) noexcept;
2023-08-10 05:13:20 +00:00
//! Operator for subtraction of vectors
constexpr NiPoint3 operator-(const NiPoint3& point) const noexcept;
2022-07-28 13:39:57 +00:00
//! Operator for addition of a scalar on all vector components
constexpr NiPoint3 operator+(const float fScalar) const noexcept;
//! Operator for subtraction of a scalar on all vector components
constexpr NiPoint3 operator-(const float fScalar) const noexcept;
//! Operator for scalar multiplication of a vector
constexpr NiPoint3 operator*(const float fScalar) const noexcept;
//! Operator for scalar division of a vector
constexpr NiPoint3 operator/(const float fScalar) const noexcept;
// MARK: Helper Functions
//! Checks to see if the point (or vector) is with an Axis-Aligned Bounding Box
2022-07-28 13:39:57 +00:00
/*!
\param minPoint The minimum point of the bounding box
\param maxPoint The maximum point of the bounding box
\return Whether or not this point lies within the box
2022-07-28 13:39:57 +00:00
*/
[[nodiscard]] constexpr bool IsWithinAxisAlignedBox(const NiPoint3& minPoint, const NiPoint3& maxPoint) noexcept;
2022-07-28 13:39:57 +00:00
//! Checks to see if the point (or vector) is within a sphere
2022-07-28 13:39:57 +00:00
/*!
\param sphereCenter The sphere center
\param radius The radius
2022-07-28 13:39:57 +00:00
*/
[[nodiscard]] constexpr bool IsWithinSphere(const NiPoint3& sphereCenter, const float radius) noexcept;
2022-07-28 13:39:57 +00:00
/*!
\param a Start of line
\param b End of line
\param p Refrence point
\return The point of line AB which is closest to P
2022-07-28 13:39:57 +00:00
*/
[[nodiscard]] static constexpr NiPoint3 ClosestPointOnLine(const NiPoint3& a, const NiPoint3& b, const NiPoint3& p) noexcept;
2022-07-28 13:39:57 +00:00
[[nodiscard]] static float Angle(const NiPoint3& a, const NiPoint3& b);
2022-07-28 13:39:57 +00:00
[[nodiscard]] static float Distance(const NiPoint3& a, const NiPoint3& b);
2022-07-28 13:39:57 +00:00
[[nodiscard]] static constexpr float DistanceSquared(const NiPoint3& a, const NiPoint3& b) noexcept;
2022-07-28 13:39:57 +00:00
[[nodiscard]] static NiPoint3 MoveTowards(const NiPoint3& current, const NiPoint3& target, const float maxDistanceDelta);
2022-07-28 13:39:57 +00:00
//This code is yoinked from the MS XNA code, so it should be right, even if it's horrible.
[[nodiscard]] constexpr NiPoint3 RotateByQuaternion(const NiQuaternion& rotation) noexcept;
};
// Static Variables
namespace NiPoint3Constant {
constexpr NiPoint3 ZERO(0.0f, 0.0f, 0.0f);
constexpr NiPoint3 UNIT_X(1.0f, 0.0f, 0.0f);
constexpr NiPoint3 UNIT_Y(0.0f, 1.0f, 0.0f);
constexpr NiPoint3 UNIT_Z(0.0f, 0.0f, 1.0f);
constexpr NiPoint3 UNIT_ALL(1.0f, 1.0f, 1.0f);
}
// .inl file needed for code organization and to circumvent circular dependency issues
#include "NiPoint3.inl"
#endif // !NIPOINT3_H