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;
91 : x(std::numeric_limits<f32>::signaling_NaN()),
92 y(std::numeric_limits<f32>::signaling_NaN()),
93 z(std::numeric_limits<f32>::signaling_NaN()) {}
97 constexpr Vector3f(f32 x_, f32 y_, f32 z_) : x(x_), y(y_), z(z_) {}
102 inline void setZero() {
106 inline void set(f32 val) {
110 [[nodiscard]]
Vector3f operator-()
const {
115 return Vector3f(x - rhs.x, y - rhs.y, z - rhs.z);
119 return *
this = *
this - rhs;
123 return Vector3f(x + rhs.x, y + rhs.y, z + rhs.z);
127 return *
this = *
this + rhs;
130 [[nodiscard]]
Vector3f operator+(f32 val)
const {
131 return Vector3f(x + val, y + val, z + val);
135 return *
this = *
this + val;
139 return Vector3f(x * rhs.x, y * rhs.y, z * rhs.z);
142 [[nodiscard]]
constexpr Vector3f operator*(f32 scalar)
const {
143 return Vector3f(x * scalar, y * scalar, z * scalar);
151 return *
this = *
this * scalar;
154 [[nodiscard]]
Vector3f operator/(f32 scalar)
const {
155 return Vector3f(x / scalar, y / scalar, z / scalar);
159 return *
this = *
this / scalar;
162 bool operator==(
const Vector3f &rhs)
const {
163 return x == rhs.x && y == rhs.y && z == rhs.z;
166 bool operator!=(
const Vector3f &rhs)
const {
167 return !(*
this == rhs);
171 explicit operator std::string()
const {
172 return std::format(
"[0x{:08X}, 0x{:08X}, 0x{:08X}] | [{}, {}, {}]", f2u(x), f2u(y), f2u(z),
178 return Vector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
183 return x * x + y * y + z * z;
188 return x * rhs.x + y * rhs.y + z * rhs.z;
199 return rhs * rhs.
dot(*
this);
205 return *
this -
proj(rhs);
209 [[nodiscard]] std::pair<Vector3f, Vector3f> projAndRej(
const Vector3f &rhs)
const {
210 return std::pair(
proj(rhs),
rej(rhs));
215 return Vector3f(Mathf::abs(x), Mathf::abs(y), Mathf::abs(z));
227 return *
this * (1.0f / val);
230 [[nodiscard]] f32
ps_dot()
const;
252inline constexpr Vector3f Vector3f::zero = Vector3f(0.0f, 0.0f, 0.0f);
253inline constexpr Vector3f Vector3f::unit = Vector3f(1.0f, 1.0f, 1.0f);
254inline constexpr Vector3f Vector3f::ex = Vector3f(1.0f, 0.0f, 0.0f);
255inline constexpr Vector3f Vector3f::ey = Vector3f(0.0f, 1.0f, 0.0f);
256inline constexpr Vector3f Vector3f::ez = Vector3f(0.0f, 0.0f, 1.0f);
259inline constexpr Vector3f Vector3f::inf = Vector3f(std::numeric_limits<f32>::infinity(),
260 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.