248 lines
11 KiB
C++
248 lines
11 KiB
C++
#pragma once
|
|
|
|
#include <J3ML/LinearAlgebra/Vector2.h>
|
|
#include <J3ML/LinearAlgebra/Vector3.h>
|
|
#include <cstddef>
|
|
#include <cstdlib>
|
|
#include <J3ML/LinearAlgebra/Angle2D.h>
|
|
|
|
namespace J3ML::LinearAlgebra {
|
|
|
|
// A 3D (x, y, z) ordered pair.
|
|
class Vector3 {
|
|
public:
|
|
float x = 0;
|
|
float y = 0;
|
|
float z = 0;
|
|
public:
|
|
enum {Dimensions = 3};
|
|
public:
|
|
static const Vector3 Zero;
|
|
static const Vector3 Up;
|
|
static const Vector3 Down;
|
|
static const Vector3 Left;
|
|
static const Vector3 Right;
|
|
static const Vector3 Forward;
|
|
static const Vector3 Backward;
|
|
static const Vector3 NaN;
|
|
static const Vector3 Infinity;
|
|
static const Vector3 NegativeInfinity;
|
|
public:
|
|
|
|
/// The default constructor does not initialize any members of this class.
|
|
/** This means that the values of the members x, y and z are all undefined after creating a new Vector3 using
|
|
this default constructor. Remember to assign to them before use.
|
|
@see x, y, z. */
|
|
Vector3();
|
|
// Constructs a new Vector3 with the value (X, Y, Z)
|
|
Vector3(float X, float Y, float Z);
|
|
Vector3(const Vector3& rhs); // Copy Constructor
|
|
Vector3(Vector3&&) = default; // Move Constructor
|
|
/// Constructs this float3 from a C array, to the value (data[0], data[1], data[2]).
|
|
/** @param data An array containing three elements for x, y and z. This pointer may not be null. */
|
|
explicit Vector3(const float* data);
|
|
/// Constructs a new Vector3 with the value (scalar, scalar, scalar).
|
|
explicit Vector3(float scalar);
|
|
|
|
|
|
/// 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,
|
|
ptr()[1] to access y, and ptr()[2] to access the z component of this Vector3.
|
|
@note Since the returned pointer points to this class, do not dereference the pointer after this
|
|
float3 has been deleted. You should never store a copy of the returned pointer.
|
|
@note This function is provided for compatibility with other APIs which require raw C pointer access
|
|
to vectors. Avoid using this function in general, and instead always use the operator [] or
|
|
the At() function to access the elements of this vector by index.
|
|
@return A pointer to the first float element of this class. The data is contiguous in memory.
|
|
@see operator [](), At(). */
|
|
float* ptr();
|
|
[[nodiscard]] const float *ptr() const { return &x;}
|
|
|
|
/// Accesses an element of this vector using array notation.
|
|
/** @param index The element to get. Pass in 0 for x, 1 for y and 2 for z.
|
|
@note If you have a non-const instance of this class, you can use this notation to set the elements of
|
|
this vector as well, e.g. vec[1] = 10.f; would set the y-component of this vector.
|
|
@see ptr(), At(). */
|
|
float operator[](std::size_t index) const;
|
|
float &operator[](std::size_t index);
|
|
|
|
/// Accesses an element of this vector.
|
|
/** @param index The element to get. Pass in 0 for x, 1 for y, and 2 for z.
|
|
@note If you have a non-const instance of this class, you can use this notation to set the elements of
|
|
this vector as well, e.g. vec.At(1) = 10.f; would set the y-component of this vector.
|
|
@see ptr(), operator [](). */
|
|
float At(int index) const;
|
|
float &At(int index);
|
|
|
|
static void Orthonormalize(Vector3& a, Vector3& b);
|
|
|
|
Vector3 Abs() const;
|
|
static Vector3 Abs(const Vector3& rhs);
|
|
|
|
/// Returns the DirectionVector for a given angle.
|
|
static Vector3 Direction(const Vector3 &rhs) ;
|
|
|
|
static void Orthonormalize(Vector3& a, Vector3& b, Vector3& c);
|
|
|
|
bool AreOrthonormal(const Vector3& a, const Vector3& b, float epsilon);
|
|
|
|
Vector3 ProjectToNorm(const Vector3& direction) const;
|
|
|
|
float GetX() const;
|
|
float GetY() const;
|
|
float GetZ() const;
|
|
void SetX(float newX);
|
|
void SetY(float newY);
|
|
void SetZ(float newZ);
|
|
|
|
bool IsWithinMarginOfError(const Vector3& rhs, float margin=0.001f) const;
|
|
bool IsNormalized(float epsilonSq = 1e-5f) const;
|
|
bool IsZero(float epsilonSq = 1e-6f) const;
|
|
bool IsPerpendicular(const Vector3& other, float epsilonSq=1e-5f) const;
|
|
|
|
|
|
bool operator == (const Vector3& rhs) const;
|
|
bool operator != (const Vector3& rhs) const;
|
|
|
|
bool IsFinite() const;
|
|
float MinElement() const;
|
|
static float MinElement(const Vector3& of);
|
|
|
|
// 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
|
|
an error code 0 is returned. This is different than the behavior of the Normalized() function, which prints an
|
|
error if normalization fails.
|
|
@note This function operates in-place.
|
|
@return The old length of this vector, or 0 if normalization failed.
|
|
@see Normalized(). */
|
|
float TryNormalize();
|
|
|
|
/// Computes a new normalized direction vector that is perpendicular to this vector and the specified hint vector.
|
|
/** If this vector points toward the hint vector, the vector hint2 is returned instead.
|
|
@see AnotherPerpendicular(), Cross(). */
|
|
Vector3 Perpendicular(const Vector3 &hint = Vector3(0,1,0), const Vector3 &hint2 = Vector3(0,0,1)) const;
|
|
|
|
Vector3 Min(const Vector3& min) const;
|
|
static Vector3 Min(const Vector3& a, const Vector3& b, const Vector3& c);
|
|
static Vector3 Min(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
Vector3 Max(const Vector3& max) const;
|
|
static Vector3 Max(const Vector3& a, const Vector3& b, const Vector3& c);
|
|
static Vector3 Max(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
Vector3 Clamp(const Vector3& min, const Vector3& max) const;
|
|
static Vector3 Clamp(const Vector3& min, const Vector3& input, const Vector3& max);
|
|
|
|
/// Returns the magnitude between the two vectors.
|
|
float Distance(const Vector3& to) const;
|
|
static float Distance(const Vector3& from, const Vector3& to);
|
|
//float Distance(const Ray&) const;
|
|
//float Distance(const LineSegment&) const;
|
|
//float Distance(const Plane&) const;
|
|
//float DIstance(const Triangle&) const;
|
|
|
|
float DistanceSquared(const Vector3& to) const;
|
|
static float DistanceSquared(const Vector3& from, const Vector3& to);
|
|
|
|
float Length() const;
|
|
static float Length(const Vector3& of);
|
|
|
|
float LengthSquared() const;
|
|
static float LengthSquared(const Vector3& of);
|
|
|
|
/// Returns the length of the vector, which is sqrt(x^2 + y^2 + z^2)
|
|
float Magnitude() const;
|
|
static float Magnitude(const Vector3& of);
|
|
|
|
/// Returns a float value equal to the magnitudes of the two vectors multiplied together and then multiplied by the cosine of the angle between them.
|
|
/// For normalized vectors, dot returns 1 if they point in exactly the same direction,
|
|
/// -1 if they point in completely opposite directions, and 0 if the vectors are perpendicular.
|
|
float Dot(const Vector3& rhs) const;
|
|
static float Dot(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
/// Projects one vector onto another and returns the result. (IDK)
|
|
Vector3 Project(const Vector3& rhs) const;
|
|
static Vector3 Project(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
/// The cross product of two vectors results in a third vector which is perpendicular to the two input vectors.
|
|
/// The result's magnitude is equal to the magnitudes of the two inputs multiplied together and then multiplied by the sine of the angle between the inputs.
|
|
Vector3 Cross(const Vector3& rhs) const;
|
|
static Vector3 Cross(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
/// Returns a copy of this vector, resized to have a magnitude of 1, while preserving "direction"
|
|
Vector3 Normalize() const;
|
|
static Vector3 Normalize(const Vector3& targ);
|
|
|
|
/// Linearly interpolates between two points.
|
|
/// Interpolates between the points and b by the interpolant t.
|
|
/// The parameter is (TODO: SHOULD BE!) clamped to the range[0, 1].
|
|
/// This is most commonly used to find a point some fraction of the wy along a line between two endpoints (eg. to move an object gradually between those points).
|
|
Vector3 Lerp(const Vector3& goal, float alpha) const;
|
|
static Vector3 Lerp(const Vector3& lhs, const Vector3& rhs, float alpha);
|
|
|
|
|
|
/// Returns the angle between this vector and the specified vector, in radians.
|
|
/** @note This function takes into account that this vector or the other vector can be unnormalized, and normalizes the computations.
|
|
If you are computing the angle between two normalized vectors, it is better to use AngleBetweenNorm().
|
|
@see AngleBetweenNorm(). */
|
|
Angle2D AngleBetween(const Vector3& rhs) const;
|
|
static Angle2D AngleBetween(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
/// Adds two vectors
|
|
Vector3 operator+(const Vector3& rhs) const;
|
|
Vector3 Add(const Vector3& rhs) const;
|
|
static Vector3 Add(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
/// Adds the vector(s, s, s) to this vector
|
|
Vector3 Add(float s) const;
|
|
|
|
/// Subtracts two vectors
|
|
Vector3 operator-(const Vector3& rhs) const;
|
|
Vector3 Sub(const Vector3& rhs) const;
|
|
static Vector3 Sub(const Vector3& lhs, const Vector3& rhs);
|
|
|
|
/// Multiplies this vector by a scalar value
|
|
Vector3 operator*(float rhs) const;
|
|
Vector3 Mul(float scalar) const;
|
|
static Vector3 Mul(const Vector3& lhs, float rhs);
|
|
|
|
/// Multiplies this vector by a vector, element-wise
|
|
/// @note Mathematically, the multiplication of two vectors is not defined in linear space structures,
|
|
/// but this function is provided here for syntactical convenience.
|
|
Vector3 Mul(const Vector3& rhs) const;
|
|
|
|
/// Divides this vector by a scalar
|
|
Vector3 operator/(float rhs) const;
|
|
Vector3 Div(float scalar) const;
|
|
static Vector3 Div(const Vector3& lhs, float rhs);
|
|
|
|
/// Divides this vector by a vector, element-wise
|
|
/// @note Mathematically, the multiplication of two vectors is not defined in linear space structures,
|
|
/// but this function is provided here for syntactical convenience
|
|
Vector3 Div(const Vector3& v) const;
|
|
|
|
/// Unary + operator
|
|
Vector3 operator+() const; // TODO: Implement
|
|
/// Unary - operator (Negation)
|
|
Vector3 operator-() const;
|
|
|
|
bool Equals(const Vector3& rhs, float epsilon = 1e-3f) const;
|
|
bool Equals(float _x, float _y, float _z, float epsilon = 1e-3f) const;
|
|
|
|
|
|
Vector3 &operator =(const Vector3& rhs);
|
|
Vector3& operator+=(const Vector3& rhs);
|
|
Vector3& operator-=(const Vector3& rhs);
|
|
Vector3& operator*=(float scalar);
|
|
Vector3& operator/=(float scalar);
|
|
|
|
};
|
|
|
|
static Vector3 operator*(float lhs, const Vector3& rhs)
|
|
{
|
|
return rhs * lhs;
|
|
}
|
|
} |