3#include "egg/math/Math.hh"
5#include "egg/util/Stream.hh"
15 : x(std::numeric_limits<f32>::signaling_NaN()),
16 y(std::numeric_limits<f32>::signaling_NaN()) {}
20 constexpr Vector2f(f32 x_, f32 y_) : x(x_), y(y_) {}
23 inline void set(f32 val) {
27 [[nodiscard]]
Vector2f operator-()
const {
32 return Vector2f(x - rhs.x, y - rhs.y);
36 return Vector2f(x + rhs.x, y + rhs.y);
40 return *
this = *
this + rhs;
43 [[nodiscard]]
Vector2f operator*(
const f32 scalar)
const {
44 return Vector2f(x * scalar, y * scalar);
47 Vector2f &operator*=(
const f32 scalar) {
48 return *
this = *
this * scalar;
52 return Vector2f(scalar * rhs.x, scalar * rhs.y);
55 [[nodiscard]] f32 cross(
const Vector2f &rhs)
const {
56 return x * rhs.y - y * rhs.x;
59 [[nodiscard]] f32 dot(
const Vector2f &rhs)
const {
60 return x * rhs.x + y * rhs.y;
63 [[nodiscard]] f32 dot()
const {
67 [[nodiscard]] f32 length()
const {
68 return dot() > std::numeric_limits<f32>::epsilon() ? Mathf::sqrt(dot()) : 0.0f;
90 : x(std::numeric_limits<f32>::signaling_NaN()),
91 y(std::numeric_limits<f32>::signaling_NaN()),
92 z(std::numeric_limits<f32>::signaling_NaN()) {}
96 constexpr Vector3f(f32 x_, f32 y_, f32 z_) : x(x_), y(y_), z(z_) {}
101 inline void setZero() {
105 inline void set(f32 val) {
109 [[nodiscard]]
Vector3f operator-()
const {
114 return Vector3f(x - rhs.x, y - rhs.y, z - rhs.z);
118 return *
this = *
this - rhs;
122 return Vector3f(x + rhs.x, y + rhs.y, z + rhs.z);
126 return *
this = *
this + rhs;
129 [[nodiscard]]
Vector3f operator+(f32 val)
const {
130 return Vector3f(x + val, y + val, z + val);
134 return *
this = *
this + val;
138 return Vector3f(x * rhs.x, y * rhs.y, z * rhs.z);
141 [[nodiscard]]
Vector3f operator*(f32 scalar)
const {
142 return Vector3f(x * scalar, y * scalar, z * scalar);
150 return *
this = *
this * scalar;
153 [[nodiscard]]
Vector3f operator/(f32 scalar)
const {
154 return Vector3f(x / scalar, y / scalar, z / scalar);
158 return *
this = *
this / scalar;
161 bool operator==(
const Vector3f &rhs)
const {
162 return x == rhs.x && y == rhs.y && z == rhs.z;
165 bool operator!=(
const Vector3f &rhs)
const {
166 return !(*
this == rhs);
170 explicit operator std::string()
const {
171 return std::format(
"[0x{:08X}, 0x{:08X}, 0x{:08X}] | [{}, {}, {}]", f2u(x), f2u(y), f2u(z),
177 return Vector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
182 return x * x + y * y + z * z;
187 return x * rhs.x + y * rhs.y + z * rhs.z;
198 return rhs * rhs.
dot(*
this);
204 return *
this -
proj(rhs);
208 [[nodiscard]] std::pair<Vector3f, Vector3f> projAndRej(
const Vector3f &rhs)
const {
209 return std::pair(
proj(rhs),
rej(rhs));
214 return Vector3f(Mathf::abs(x), Mathf::abs(y), Mathf::abs(z));
226 return *
this * (1.0f / val);
229 [[nodiscard]] f32
ps_dot()
const;
251inline constexpr Vector3f Vector3f::zero = Vector3f(0.0f, 0.0f, 0.0f);
252inline constexpr Vector3f Vector3f::unit = Vector3f(1.0f, 1.0f, 1.0f);
253inline constexpr Vector3f Vector3f::ex = Vector3f(1.0f, 0.0f, 0.0f);
254inline constexpr Vector3f Vector3f::ey = Vector3f(0.0f, 1.0f, 0.0f);
255inline constexpr Vector3f Vector3f::ez = Vector3f(0.0f, 0.0f, 1.0f);
258inline constexpr Vector3f Vector3f::inf = Vector3f(std::numeric_limits<f32>::infinity(),
259 std::numeric_limits<f32>::infinity(), std::numeric_limits<f32>::infinity());
A stream of data, abstracted to allow for continuous seeking.
void read(Stream &stream)
Initializes a Vector2f by reading 8 bytes from the stream.
f32 normalise()
Normalizes the vector and returns the original length.
Vector3f abs() const
Returns the absolute value of each element of the vector.
f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
f32 length() const
The square root of the vector's dot product.
f32 squaredLength() const
The dot product between the vector and itself.
Vector3f proj(const Vector3f &rhs) const
The projection of this vector onto rhs.
f32 ps_squareMag() const
Differs from ps_dot due to variation in which operands are fused.
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const
Calculates the orthogonal vector, based on the plane defined by this vector and rhs.
EGG::Vector3f multInv(f32 val) const
Multiplies a vector by the inverse of val.
Vector3f rej(const Vector3f &rhs) const
The rejection of this vector onto rhs.
Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
f32 sqDistance(const Vector3f &rhs) const
The square of the distance between two vectors.
f32 ps_sqDistance(const Vector3f &rhs) const
Paired-singles impl. of sqDistance.
f32 ps_dot() const
Paired-singles dot product implementation.
Vector3f minimize(const Vector3f &rhs) const
Returns a vector whose elements are the min of the elements of both vectors.