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 constexpr inline void set(f32 val) {
27 [[nodiscard]]
constexpr 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]]
constexpr Vector2f operator*(
const f32 scalar)
const {
44 return Vector2f(x * scalar, y * scalar);
47 constexpr Vector2f &operator*=(
const f32 scalar) {
48 return *
this = *
this * scalar;
52 return Vector2f(scalar * rhs.x, scalar * rhs.y);
55 [[nodiscard]]
constexpr f32 cross(
const Vector2f &rhs)
const {
56 return x * rhs.y - y * rhs.x;
59 [[nodiscard]]
constexpr f32 dot(
const Vector2f &rhs)
const {
60 return x * rhs.x + y * rhs.y;
63 [[nodiscard]]
constexpr f32 dot()
const {
67 [[nodiscard]]
constexpr f32 length()
const {
68 return dot() > std::numeric_limits<f32>::epsilon() ? Mathf::sqrt(dot()) : 0.0f;
72 constexpr f32 normalise() {
75 *
this = *
this * (1.0f / len);
82 constexpr void normalise2() {
84 if (sqLen > std::numeric_limits<f32>::epsilon()) {
91 x = stream.read_f32();
92 y = stream.read_f32();
102inline constexpr Vector2f Vector2f::zero = Vector2f(0.0f, 0.0f);
103inline constexpr Vector2f Vector2f::ex = Vector2f(1.0f, 0.0f);
104inline constexpr Vector2f Vector2f::ey = Vector2f(0.0f, 1.0f);
110 : x(std::numeric_limits<f32>::signaling_NaN()),
111 y(std::numeric_limits<f32>::signaling_NaN()),
112 z(std::numeric_limits<f32>::signaling_NaN()) {}
116 constexpr Vector3f(f32 x_, f32 y_, f32 z_) : x(x_), y(y_), z(z_) {}
121 constexpr inline void setZero() {
125 constexpr inline void set(f32 val) {
129 [[nodiscard]]
constexpr Vector3f operator-()
const {
134 return Vector3f(x - rhs.x, y - rhs.y, z - rhs.z);
138 return *
this = *
this - rhs;
142 return Vector3f(x + rhs.x, y + rhs.y, z + rhs.z);
146 return *
this = *
this + rhs;
149 [[nodiscard]]
constexpr Vector3f operator+(f32 val)
const {
150 return Vector3f(x + val, y + val, z + val);
153 constexpr Vector3f &operator+=(f32 val) {
154 return *
this = *
this + val;
158 return Vector3f(x * rhs.x, y * rhs.y, z * rhs.z);
161 [[nodiscard]]
constexpr Vector3f operator*(f32 scalar)
const {
162 return Vector3f(x * scalar, y * scalar, z * scalar);
165 [[nodiscard]]
friend constexpr Vector3f operator*(f32 scalar,
const Vector3f &rhs) {
169 constexpr Vector3f &operator*=(f32 scalar) {
170 return *
this = *
this * scalar;
173 [[nodiscard]]
constexpr Vector3f operator/(f32 scalar)
const {
174 return Vector3f(x / scalar, y / scalar, z / scalar);
177 constexpr Vector3f &operator/=(f32 scalar) {
178 return *
this = *
this / scalar;
181 [[nodiscard]]
constexpr bool operator==(
const Vector3f &rhs)
const {
182 return x == rhs.x && y == rhs.y && z == rhs.z;
185 [[nodiscard]]
constexpr bool operator!=(
const Vector3f &rhs)
const {
186 return !(*
this == rhs);
190 [[nodiscard]]
explicit operator std::string()
const {
191 return std::format(
"[0x{:08X}, 0x{:08X}, 0x{:08X}] | [{}, {}, {}]", f2u(x), f2u(y), f2u(z),
197 return Vector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
202 return x * x + y * y + z * z;
207 return x * rhs.x + y * rhs.y + z * rhs.z;
211 [[nodiscard]]
constexpr f32
length()
const {
216 [[nodiscard]]
constexpr f32 ps_length()
const {
218 return dot == 0.0f ? 0.0f : Mathf::sqrt(
dot);
224 return rhs * rhs.
dot(*
this);
230 return *
this -
proj(rhs);
234 [[nodiscard]]
constexpr std::pair<Vector3f, Vector3f> projAndRej(
const Vector3f &rhs)
const {
235 return std::pair(
proj(rhs),
rej(rhs));
240 return Vector3f(Mathf::abs(x), Mathf::abs(y), Mathf::abs(z));
252 return *
this * (1.0f / val);
256 [[nodiscard]]
constexpr f32
ps_dot()
const {
265 return xy + z * rhs.z;
283 *
this *= (1.0f / len);
296 [[nodiscard]]
constexpr std::pair<f32, EGG::Vector3f> ps_normalized() {
297 f32 mag = ps_length();
299 return std::make_pair(mag, EGG::Vector3f::zero);
302 return std::make_pair(mag, *
this * (1.0f / mag));
306 constexpr void normalise2() {
308 if (sqLen > std::numeric_limits<f32>::epsilon()) {
318 out.x = x > rhs.x ? x : rhs.x;
319 out.y = y > rhs.y ? y : rhs.y;
320 out.z = z > rhs.z ? z : rhs.z;
330 out.x = x < rhs.x ? x : rhs.x;
331 out.y = y < rhs.y ? y : rhs.y;
332 out.z = z < rhs.z ? z : rhs.z;
347 if (Mathf::abs(
dot(rhs)) == 1.0f) {
348 return EGG::Vector3f::zero;
351 f32 _x = (rhs.z * x - rhs.x * z) * rhs.z - (rhs.x * y - rhs.y * x) * rhs.y;
352 f32 _y = (rhs.x * y - rhs.y * x) * rhs.x - (rhs.y * z - rhs.z * y) * rhs.z;
353 f32 _z = (rhs.y * z - rhs.z * y) * rhs.y - (rhs.z * x - rhs.x * z) * rhs.x;
366 x = stream.read_f32();
367 y = stream.read_f32();
368 z = stream.read_f32();
381inline constexpr Vector3f Vector3f::zero = Vector3f(0.0f, 0.0f, 0.0f);
382inline constexpr Vector3f Vector3f::unit = Vector3f(1.0f, 1.0f, 1.0f);
383inline constexpr Vector3f Vector3f::ex = Vector3f(1.0f, 0.0f, 0.0f);
384inline constexpr Vector3f Vector3f::ey = Vector3f(0.0f, 1.0f, 0.0f);
385inline constexpr Vector3f Vector3f::ez = Vector3f(0.0f, 0.0f, 1.0f);
388inline constexpr Vector3f Vector3f::inf = Vector3f(std::numeric_limits<f32>::infinity(),
389 std::numeric_limits<f32>::infinity(), std::numeric_limits<f32>::infinity());
A stream of data, abstracted to allow for continuous seeking.
static constexpr f32 frsqrt(f32 x)
static constexpr f32 fma(f32 x, f32 y, f32 z)
Fused multiply-add operation.
void read(Stream &stream)
Initializes a Vector2f by reading 8 bytes from the stream.
constexpr Vector3f rej(const Vector3f &rhs) const
The rejection of this vector onto rhs.
constexpr f32 normalise()
Normalizes the vector and returns the original length.
constexpr f32 squaredLength() const
The dot product between the vector and itself.
constexpr f32 ps_dot(const Vector3f &rhs) const
Paired-singles dot product implementation.
constexpr Vector3f abs() const
Returns the absolute value of each element of the vector.
constexpr f32 ps_sqDistance(const Vector3f &rhs) const
Paired-singles impl. of sqDistance.
void read(Stream &stream)
Initializes a Vector3f by reading 12 bytes from the stream.
constexpr Vector3f minimize(const Vector3f &rhs) const
Returns a vector whose elements are the min of the elements of both vectors.
constexpr f32 ps_dot() const
Paired-singles dot product implementation.
constexpr Vector3f maximize(const Vector3f &rhs) const
Returns a vector whose elements are the max of the elements of both vectors.
constexpr f32 length() const
The square root of the vector's dot product.
constexpr Vector3f proj(const Vector3f &rhs) const
The projection of this vector onto rhs.
constexpr f32 dot(const Vector3f &rhs) const
The dot product between two vectors.
constexpr f32 sqDistance(const Vector3f &rhs) const
The square of the distance between two vectors.
constexpr EGG::Vector3f multInv(f32 val) const
Multiplies a vector by the inverse of val.
constexpr f32 ps_squareMag() const
Differs from ps_dot due to variation in which operands are fused.
constexpr Vector3f perpInPlane(const EGG::Vector3f &rhs, bool normalise) const
Calculates the orthogonal vector, based on the plane defined by this vector and rhs.