moved stuff around, added a grayscale test.
This commit is contained in:
19
util/vectorlogic/vec.cpp
Normal file
19
util/vectorlogic/vec.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef vec_hpp
|
||||
#define vec_hpp
|
||||
|
||||
#include "Vec4.hpp"
|
||||
#include "Vec3.hpp"
|
||||
#include "Vec2.hpp"
|
||||
|
||||
Vec4::Vec4(const Vec3& vec3, float w) : x(vec3.x), y(vec3.y), z(vec3.z), w(w) {}
|
||||
Vec3::Vec3(const Vec2& vec2, float z) : x(vec2.x), y(vec2.y), z(z) {}
|
||||
|
||||
Vec3 Vec4::xyz() const {
|
||||
return Vec3(x, y, z);
|
||||
}
|
||||
|
||||
Vec3 Vec4::rgb() const {
|
||||
return Vec3(r, g, b);
|
||||
}
|
||||
|
||||
#endif
|
||||
286
util/vectorlogic/vec2.hpp
Normal file
286
util/vectorlogic/vec2.hpp
Normal file
@@ -0,0 +1,286 @@
|
||||
#ifndef VEC2_HPP
|
||||
#define VEC2_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
|
||||
class Vec2 {
|
||||
public:
|
||||
float x, y;
|
||||
Vec2() : x(0), y(0) {}
|
||||
Vec2(float x, float y) : x(x), y(y) {}
|
||||
|
||||
Vec2 operator+(const Vec2& other) const {
|
||||
return Vec2(x + other.x, y + other.y);
|
||||
}
|
||||
|
||||
Vec2 operator-(const Vec2& other) const {
|
||||
return Vec2(x - other.x, y - other.y);
|
||||
}
|
||||
|
||||
Vec2 operator*(const Vec2& other) const {
|
||||
return Vec2(x * other.x, y * other.y);
|
||||
}
|
||||
|
||||
Vec2 operator/(const Vec2& other) const {
|
||||
return Vec2(x / other.x, y / other.y);
|
||||
}
|
||||
|
||||
Vec2 operator+(float scalar) const {
|
||||
return Vec2(x + scalar, y + scalar);
|
||||
}
|
||||
|
||||
Vec2 operator-(float scalar) const {
|
||||
return Vec2(x - scalar, y - scalar);
|
||||
}
|
||||
|
||||
Vec2 operator-() const {
|
||||
return Vec2(-x, -y);
|
||||
}
|
||||
|
||||
Vec2 operator*(float scalar) const {
|
||||
return Vec2(x * scalar, y * scalar);
|
||||
}
|
||||
|
||||
Vec2 operator/(float scalar) const {
|
||||
return Vec2(x / scalar, y / scalar);
|
||||
}
|
||||
|
||||
Vec2& operator=(float scalar) {
|
||||
x = y = scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator+=(const Vec2& other) {
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator-=(const Vec2& other) {
|
||||
x -= other.x;
|
||||
y -= other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator*=(const Vec2& other) {
|
||||
x *= other.x;
|
||||
y *= other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator/=(const Vec2& other) {
|
||||
x /= other.x;
|
||||
y /= other.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator+=(float scalar) {
|
||||
x += scalar;
|
||||
y += scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator-=(float scalar) {
|
||||
x -= scalar;
|
||||
y -= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator*=(float scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec2& operator/=(float scalar) {
|
||||
x /= scalar;
|
||||
y /= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
float dot(const Vec2& other) const {
|
||||
return x * other.x + y * other.y;
|
||||
}
|
||||
|
||||
float length() const {
|
||||
return std::sqrt(x * x + y * y);
|
||||
}
|
||||
|
||||
float lengthSquared() const {
|
||||
return x * x + y * y;
|
||||
}
|
||||
|
||||
float distance(const Vec2& other) const {
|
||||
return (*this - other).length();
|
||||
}
|
||||
|
||||
float distanceSquared(const Vec2& other) const {
|
||||
Vec2 diff = *this - other;
|
||||
return diff.x * diff.x + diff.y * diff.y;
|
||||
}
|
||||
|
||||
Vec2 normalized() const {
|
||||
float len = length();
|
||||
if (len > 0) {
|
||||
return *this / len;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const Vec2& other) const {
|
||||
return x == other.x && y == other.y;
|
||||
}
|
||||
|
||||
bool operator!=(const Vec2& other) const {
|
||||
return x != other.x || y != other.y;
|
||||
}
|
||||
|
||||
bool operator<(const Vec2& other) const {
|
||||
return (x < other.x) || (x == other.x && y < other.y);
|
||||
}
|
||||
|
||||
bool operator<=(const Vec2& other) const {
|
||||
return (x < other.x) || (x == other.x && y <= other.y);
|
||||
}
|
||||
|
||||
bool operator>(const Vec2& other) const {
|
||||
return (x > other.x) || (x == other.x && y > other.y);
|
||||
}
|
||||
|
||||
bool operator>=(const Vec2& other) const {
|
||||
return (x > other.x) || (x == other.x && y >= other.y);
|
||||
}
|
||||
|
||||
Vec2 abs() const {
|
||||
return Vec2(std::abs(x), std::abs(y));
|
||||
}
|
||||
|
||||
Vec2 floor() const {
|
||||
return Vec2(std::floor(x), std::floor(y));
|
||||
}
|
||||
|
||||
Vec2 ceil() const {
|
||||
return Vec2(std::ceil(x), std::ceil(y));
|
||||
}
|
||||
|
||||
Vec2 round() const {
|
||||
return Vec2(std::round(x), std::round(y));
|
||||
}
|
||||
|
||||
Vec2 min(const Vec2& other) const {
|
||||
return Vec2(std::min(x, other.x), std::min(y, other.y));
|
||||
}
|
||||
|
||||
Vec2 max(const Vec2& other) const {
|
||||
return Vec2(std::max(x, other.x), std::max(y, other.y));
|
||||
}
|
||||
|
||||
Vec2 clamp(const Vec2& minVal, const Vec2& maxVal) const {
|
||||
return Vec2(
|
||||
std::clamp(x, minVal.x, maxVal.x),
|
||||
std::clamp(y, minVal.y, maxVal.y)
|
||||
);
|
||||
}
|
||||
|
||||
Vec2 clamp(float minVal, float maxVal) const {
|
||||
return Vec2(
|
||||
std::clamp(x, minVal, maxVal),
|
||||
std::clamp(y, minVal, maxVal)
|
||||
);
|
||||
}
|
||||
|
||||
bool isZero(float epsilon = 1e-10f) const {
|
||||
return std::abs(x) < epsilon && std::abs(y) < epsilon;
|
||||
}
|
||||
|
||||
bool equals(const Vec2& other, float epsilon = 1e-10f) const {
|
||||
return std::abs(x - other.x) < epsilon &&
|
||||
std::abs(y - other.y) < epsilon;
|
||||
}
|
||||
|
||||
friend Vec2 operator+(float scalar, const Vec2& vec) {
|
||||
return Vec2(scalar + vec.x, scalar + vec.y);
|
||||
}
|
||||
|
||||
friend Vec2 operator-(float scalar, const Vec2& vec) {
|
||||
return Vec2(scalar - vec.x, scalar - vec.y);
|
||||
}
|
||||
|
||||
friend Vec2 operator*(float scalar, const Vec2& vec) {
|
||||
return Vec2(scalar * vec.x, scalar * vec.y);
|
||||
}
|
||||
|
||||
friend Vec2 operator/(float scalar, const Vec2& vec) {
|
||||
return Vec2(scalar / vec.x, scalar / vec.y);
|
||||
}
|
||||
|
||||
Vec2 perpendicular() const {
|
||||
return Vec2(-y, x);
|
||||
}
|
||||
|
||||
Vec2 reflect(const Vec2& normal) const {
|
||||
return *this - 2.0f * this->dot(normal) * normal;
|
||||
}
|
||||
|
||||
Vec2 lerp(const Vec2& other, float t) const {
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
return *this + (other - *this) * t;
|
||||
}
|
||||
|
||||
Vec2 slerp(const Vec2& other, float t) const {
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
float dot = this->dot(other);
|
||||
dot = std::clamp(dot, -1.0f, 1.0f);
|
||||
|
||||
float theta = std::acos(dot) * t;
|
||||
Vec2 relative = other - *this * dot;
|
||||
relative = relative.normalized();
|
||||
|
||||
return (*this * std::cos(theta)) + (relative * std::sin(theta));
|
||||
}
|
||||
|
||||
Vec2 rotate(float angle) const {
|
||||
float cosA = std::cos(angle);
|
||||
float sinA = std::sin(angle);
|
||||
return Vec2(x * cosA - y * sinA, x * sinA + y * cosA);
|
||||
}
|
||||
|
||||
float angle() const {
|
||||
return std::atan2(y, x);
|
||||
}
|
||||
|
||||
float angleTo(const Vec2& other) const {
|
||||
return std::acos(this->dot(other) / (this->length() * other.length()));
|
||||
}
|
||||
|
||||
float& operator[](int index) {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
const float& operator[](int index) const {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
std::string toString() const {
|
||||
return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Vec2& vec) {
|
||||
os << vec.toString();
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<>
|
||||
struct hash<Vec2> {
|
||||
size_t operator()(const Vec2& v) const {
|
||||
return hash<float>()(v.x) ^ (hash<float>()(v.y) << 1);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
322
util/vectorlogic/vec3.hpp
Normal file
322
util/vectorlogic/vec3.hpp
Normal file
@@ -0,0 +1,322 @@
|
||||
#ifndef VEC3_HPP
|
||||
#define VEC3_HPP
|
||||
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
|
||||
class Vec3 {
|
||||
public:
|
||||
float x, y, z;
|
||||
|
||||
Vec3() : x(0), y(0), z(0) {}
|
||||
Vec3(float x, float y, float z) : x(x), y(y), z(z) {}
|
||||
Vec3(float scalar) : x(scalar), y(scalar), z(scalar) {}
|
||||
|
||||
Vec3(const class Vec2& vec2, float z = 0.0f);
|
||||
|
||||
// Arithmetic operations
|
||||
Vec3 operator+(const Vec3& other) const {
|
||||
return Vec3(x + other.x, y + other.y, z + other.z);
|
||||
}
|
||||
|
||||
Vec3 operator-(const Vec3& other) const {
|
||||
return Vec3(x - other.x, y - other.y, z - other.z);
|
||||
}
|
||||
|
||||
Vec3 operator*(const Vec3& other) const {
|
||||
return Vec3(x * other.x, y * other.y, z * other.z);
|
||||
}
|
||||
|
||||
Vec3 operator/(const Vec3& other) const {
|
||||
return Vec3(x / other.x, y / other.y, z / other.z);
|
||||
}
|
||||
|
||||
Vec3 operator+(float scalar) const {
|
||||
return Vec3(x + scalar, y + scalar, z + scalar);
|
||||
}
|
||||
|
||||
Vec3 operator-(float scalar) const {
|
||||
return Vec3(x - scalar, y - scalar, z - scalar);
|
||||
}
|
||||
|
||||
Vec3 operator-() const {
|
||||
return Vec3(-x, -y, -z);
|
||||
}
|
||||
|
||||
Vec3 operator*(float scalar) const {
|
||||
return Vec3(x * scalar, y * scalar, z * scalar);
|
||||
}
|
||||
|
||||
Vec3 operator/(float scalar) const {
|
||||
return Vec3(x / scalar, y / scalar, z / scalar);
|
||||
}
|
||||
|
||||
Vec3& operator=(float scalar) {
|
||||
x = y = z = scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator+=(const Vec3& other) {
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
z += other.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator-=(const Vec3& other) {
|
||||
x -= other.x;
|
||||
y -= other.y;
|
||||
z -= other.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator*=(const Vec3& other) {
|
||||
x *= other.x;
|
||||
y *= other.y;
|
||||
z *= other.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator/=(const Vec3& other) {
|
||||
x /= other.x;
|
||||
y /= other.y;
|
||||
z /= other.z;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator+=(float scalar) {
|
||||
x += scalar;
|
||||
y += scalar;
|
||||
z += scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator-=(float scalar) {
|
||||
x -= scalar;
|
||||
y -= scalar;
|
||||
z -= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator*=(float scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
z *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec3& operator/=(float scalar) {
|
||||
x /= scalar;
|
||||
y /= scalar;
|
||||
z /= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
float dot(const Vec3& other) const {
|
||||
return x * other.x + y * other.y + z * other.z;
|
||||
}
|
||||
|
||||
Vec3 cross(const Vec3& other) const {
|
||||
return Vec3(
|
||||
y * other.z - z * other.y,
|
||||
z * other.x - x * other.z,
|
||||
x * other.y - y * other.x
|
||||
);
|
||||
}
|
||||
|
||||
float length() const {
|
||||
return std::sqrt(x * x + y * y + z * z);
|
||||
}
|
||||
|
||||
float lengthSquared() const {
|
||||
return x * x + y * y + z * z;
|
||||
}
|
||||
|
||||
float distance(const Vec3& other) const {
|
||||
return (*this - other).length();
|
||||
}
|
||||
|
||||
float distanceSquared(const Vec3& other) const {
|
||||
Vec3 diff = *this - other;
|
||||
return diff.x * diff.x + diff.y * diff.y + diff.z * diff.z;
|
||||
}
|
||||
|
||||
Vec3 normalized() const {
|
||||
float len = length();
|
||||
if (len > 0) {
|
||||
return *this / len;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const Vec3& other) const {
|
||||
return x == other.x && y == other.y && z == other.z;
|
||||
}
|
||||
|
||||
bool operator!=(const Vec3& other) const {
|
||||
return x != other.x || y != other.y || z != other.z;
|
||||
}
|
||||
|
||||
bool operator<(const Vec3& other) const {
|
||||
return (x < other.x) ||
|
||||
(x == other.x && y < other.y) ||
|
||||
(x == other.x && y == other.y && z < other.z);
|
||||
}
|
||||
|
||||
bool operator<=(const Vec3& other) const {
|
||||
return (x < other.x) ||
|
||||
(x == other.x && y < other.y) ||
|
||||
(x == other.x && y == other.y && z <= other.z);
|
||||
}
|
||||
|
||||
bool operator>(const Vec3& other) const {
|
||||
return (x > other.x) ||
|
||||
(x == other.x && y > other.y) ||
|
||||
(x == other.x && y == other.y && z > other.z);
|
||||
}
|
||||
|
||||
bool operator>=(const Vec3& other) const {
|
||||
return (x > other.x) ||
|
||||
(x == other.x && y > other.y) ||
|
||||
(x == other.x && y == other.y && z >= other.z);
|
||||
}
|
||||
|
||||
Vec3 abs() const {
|
||||
return Vec3(std::abs(x), std::abs(y), std::abs(z));
|
||||
}
|
||||
|
||||
Vec3 floor() const {
|
||||
return Vec3(std::floor(x), std::floor(y), std::floor(z));
|
||||
}
|
||||
|
||||
Vec3 ceil() const {
|
||||
return Vec3(std::ceil(x), std::ceil(y), std::ceil(z));
|
||||
}
|
||||
|
||||
Vec3 round() const {
|
||||
return Vec3(std::round(x), std::round(y), std::round(z));
|
||||
}
|
||||
|
||||
Vec3 min(const Vec3& other) const {
|
||||
return Vec3(std::min(x, other.x), std::min(y, other.y), std::min(z, other.z));
|
||||
}
|
||||
|
||||
Vec3 max(const Vec3& other) const {
|
||||
return Vec3(std::max(x, other.x), std::max(y, other.y), std::max(z, other.z));
|
||||
}
|
||||
|
||||
Vec3 clamp(const Vec3& minVal, const Vec3& maxVal) const {
|
||||
return Vec3(
|
||||
std::clamp(x, minVal.x, maxVal.x),
|
||||
std::clamp(y, minVal.y, maxVal.y),
|
||||
std::clamp(z, minVal.z, maxVal.z)
|
||||
);
|
||||
}
|
||||
|
||||
Vec3 clamp(float minVal, float maxVal) const {
|
||||
return Vec3(
|
||||
std::clamp(x, minVal, maxVal),
|
||||
std::clamp(y, minVal, maxVal),
|
||||
std::clamp(z, minVal, maxVal)
|
||||
);
|
||||
}
|
||||
|
||||
bool isZero(float epsilon = 1e-10f) const {
|
||||
return std::abs(x) < epsilon && std::abs(y) < epsilon && std::abs(z) < epsilon;
|
||||
}
|
||||
|
||||
bool equals(const Vec3& other, float epsilon = 1e-10f) const {
|
||||
return std::abs(x - other.x) < epsilon &&
|
||||
std::abs(y - other.y) < epsilon &&
|
||||
std::abs(z - other.z) < epsilon;
|
||||
}
|
||||
|
||||
friend Vec3 operator+(float scalar, const Vec3& vec) {
|
||||
return Vec3(scalar + vec.x, scalar + vec.y, scalar + vec.z);
|
||||
}
|
||||
|
||||
friend Vec3 operator-(float scalar, const Vec3& vec) {
|
||||
return Vec3(scalar - vec.x, scalar - vec.y, scalar - vec.z);
|
||||
}
|
||||
|
||||
friend Vec3 operator*(float scalar, const Vec3& vec) {
|
||||
return Vec3(scalar * vec.x, scalar * vec.y, scalar * vec.z);
|
||||
}
|
||||
|
||||
friend Vec3 operator/(float scalar, const Vec3& vec) {
|
||||
return Vec3(scalar / vec.x, scalar / vec.y, scalar / vec.z);
|
||||
}
|
||||
|
||||
Vec3 reflect(const Vec3& normal) const {
|
||||
return *this - 2.0f * this->dot(normal) * normal;
|
||||
}
|
||||
|
||||
Vec3 lerp(const Vec3& other, float t) const {
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
return *this + (other - *this) * t;
|
||||
}
|
||||
|
||||
Vec3 slerp(const Vec3& other, float t) const {
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
float dot = this->dot(other);
|
||||
dot = std::clamp(dot, -1.0f, 1.0f);
|
||||
|
||||
float theta = std::acos(dot) * t;
|
||||
Vec3 relative = other - *this * dot;
|
||||
relative = relative.normalized();
|
||||
|
||||
return (*this * std::cos(theta)) + (relative * std::sin(theta));
|
||||
}
|
||||
|
||||
Vec3 rotateX(float angle) const {
|
||||
float cosA = std::cos(angle);
|
||||
float sinA = std::sin(angle);
|
||||
return Vec3(x, y * cosA - z * sinA, y * sinA + z * cosA);
|
||||
}
|
||||
|
||||
Vec3 rotateY(float angle) const {
|
||||
float cosA = std::cos(angle);
|
||||
float sinA = std::sin(angle);
|
||||
return Vec3(x * cosA + z * sinA, y, -x * sinA + z * cosA);
|
||||
}
|
||||
|
||||
Vec3 rotateZ(float angle) const {
|
||||
float cosA = std::cos(angle);
|
||||
float sinA = std::sin(angle);
|
||||
return Vec3(x * cosA - y * sinA, x * sinA + y * cosA, z);
|
||||
}
|
||||
|
||||
float angleTo(const Vec3& other) const {
|
||||
return std::acos(this->dot(other) / (this->length() * other.length()));
|
||||
}
|
||||
|
||||
float& operator[](int index) {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
const float& operator[](int index) const {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
std::string toString() const {
|
||||
return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")";
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Vec3& vec) {
|
||||
os << vec.toString();
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<>
|
||||
struct hash<Vec3> {
|
||||
size_t operator()(const Vec3& v) const {
|
||||
return hash<float>()(v.x) ^ (hash<float>()(v.y) << 1) ^ (hash<float>()(v.z) << 2);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
384
util/vectorlogic/vec4.hpp
Normal file
384
util/vectorlogic/vec4.hpp
Normal file
@@ -0,0 +1,384 @@
|
||||
#ifndef VEC4_HPP
|
||||
#define VEC4_HPP
|
||||
|
||||
#include "vec3.hpp"
|
||||
#include <cmath>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <ostream>
|
||||
#include <cstdint>
|
||||
|
||||
class Vec4 {
|
||||
public:
|
||||
union {
|
||||
struct { float x, y, z, w; };
|
||||
struct { float r, g, b, a; };
|
||||
struct { float s, t, p, q; }; // For texture coordinates
|
||||
};
|
||||
|
||||
// Constructors
|
||||
Vec4() : x(0), y(0), z(0), w(0) {}
|
||||
Vec4(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
|
||||
Vec4(float scalar) : x(scalar), y(scalar), z(scalar), w(scalar) {}
|
||||
|
||||
Vec4(const Vec3& rgb, float w = 1.0f) : x(rgb.x), y(rgb.y), z(rgb.z), w(w) {}
|
||||
static Vec4 RGB(float r, float g, float b, float a = 1.0f) { return Vec4(r, g, b, a); }
|
||||
static Vec4 RGBA(float r, float g, float b, float a) { return Vec4(r, g, b, a); }
|
||||
|
||||
|
||||
Vec4 operator+(const Vec4& other) const {
|
||||
return Vec4(x + other.x, y + other.y, z + other.z, w + other.w);
|
||||
}
|
||||
|
||||
Vec4 operator-(const Vec4& other) const {
|
||||
return Vec4(x - other.x, y - other.y, z - other.z, w - other.w);
|
||||
}
|
||||
|
||||
Vec4 operator*(const Vec4& other) const {
|
||||
return Vec4(x * other.x, y * other.y, z * other.z, w * other.w);
|
||||
}
|
||||
|
||||
Vec4 operator/(const Vec4& other) const {
|
||||
return Vec4(x / other.x, y / other.y, z / other.z, w / other.w);
|
||||
}
|
||||
|
||||
Vec4 operator+(float scalar) const {
|
||||
return Vec4(x + scalar, y + scalar, z + scalar, w + scalar);
|
||||
}
|
||||
|
||||
Vec4 operator-(float scalar) const {
|
||||
return Vec4(x - scalar, y - scalar, z - scalar, w - scalar);
|
||||
}
|
||||
|
||||
Vec4 operator-() const {
|
||||
return Vec4(-x, -y, -z, -w);
|
||||
}
|
||||
|
||||
Vec4 operator*(float scalar) const {
|
||||
return Vec4(x * scalar, y * scalar, z * scalar, w * scalar);
|
||||
}
|
||||
|
||||
Vec4 operator/(float scalar) const {
|
||||
return Vec4(x / scalar, y / scalar, z / scalar, w / scalar);
|
||||
}
|
||||
|
||||
Vec4& operator=(float scalar) {
|
||||
x = y = z = w = scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator+=(const Vec4& other) {
|
||||
x += other.x;
|
||||
y += other.y;
|
||||
z += other.z;
|
||||
w += other.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator-=(const Vec4& other) {
|
||||
x -= other.x;
|
||||
y -= other.y;
|
||||
z -= other.z;
|
||||
w -= other.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator*=(const Vec4& other) {
|
||||
x *= other.x;
|
||||
y *= other.y;
|
||||
z *= other.z;
|
||||
w *= other.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator/=(const Vec4& other) {
|
||||
x /= other.x;
|
||||
y /= other.y;
|
||||
z /= other.z;
|
||||
w /= other.w;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator+=(float scalar) {
|
||||
x += scalar;
|
||||
y += scalar;
|
||||
z += scalar;
|
||||
w += scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator-=(float scalar) {
|
||||
x -= scalar;
|
||||
y -= scalar;
|
||||
z -= scalar;
|
||||
w -= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator*=(float scalar) {
|
||||
x *= scalar;
|
||||
y *= scalar;
|
||||
z *= scalar;
|
||||
w *= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vec4& operator/=(float scalar) {
|
||||
x /= scalar;
|
||||
y /= scalar;
|
||||
z /= scalar;
|
||||
w /= scalar;
|
||||
return *this;
|
||||
}
|
||||
|
||||
float dot(const Vec4& other) const {
|
||||
return x * other.x + y * other.y + z * other.z + w * other.w;
|
||||
}
|
||||
|
||||
// 4D cross product (returns vector perpendicular to 3 given vectors in 4D space)
|
||||
Vec4 cross(const Vec4& v1, const Vec4& v2, const Vec4& v3) const {
|
||||
float a = v1.y * (v2.z * v3.w - v2.w * v3.z) -
|
||||
v1.z * (v2.y * v3.w - v2.w * v3.y) +
|
||||
v1.w * (v2.y * v3.z - v2.z * v3.y);
|
||||
|
||||
float b = -v1.x * (v2.z * v3.w - v2.w * v3.z) +
|
||||
v1.z * (v2.x * v3.w - v2.w * v3.x) -
|
||||
v1.w * (v2.x * v3.z - v2.z * v3.x);
|
||||
|
||||
float c = v1.x * (v2.y * v3.w - v2.w * v3.y) -
|
||||
v1.y * (v2.x * v3.w - v2.w * v3.x) +
|
||||
v1.w * (v2.x * v3.y - v2.y * v3.x);
|
||||
|
||||
float d = -v1.x * (v2.y * v3.z - v2.z * v3.y) +
|
||||
v1.y * (v2.x * v3.z - v2.z * v3.x) -
|
||||
v1.z * (v2.x * v3.y - v2.y * v3.x);
|
||||
|
||||
return Vec4(a, b, c, d);
|
||||
}
|
||||
|
||||
float length() const {
|
||||
return std::sqrt(x * x + y * y + z * z + w * w);
|
||||
}
|
||||
|
||||
float lengthSquared() const {
|
||||
return x * x + y * y + z * z + w * w;
|
||||
}
|
||||
|
||||
float distance(const Vec4& other) const {
|
||||
return (*this - other).length();
|
||||
}
|
||||
|
||||
float distanceSquared(const Vec4& other) const {
|
||||
Vec4 diff = *this - other;
|
||||
return diff.x * diff.x + diff.y * diff.y + diff.z * diff.z + diff.w * diff.w;
|
||||
}
|
||||
|
||||
Vec4 normalized() const {
|
||||
float len = length();
|
||||
if (len > 0) {
|
||||
return *this / len;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Homogeneous normalization (divide by w)
|
||||
Vec4 homogenized() const {
|
||||
if (w != 0.0f) {
|
||||
return Vec4(x / w, y / w, z / w, 1.0f);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Clamp values between 0 and 1
|
||||
Vec4 clamped() const {
|
||||
return Vec4(
|
||||
std::clamp(r, 0.0f, 1.0f),
|
||||
std::clamp(g, 0.0f, 1.0f),
|
||||
std::clamp(b, 0.0f, 1.0f),
|
||||
std::clamp(a, 0.0f, 1.0f)
|
||||
);
|
||||
}
|
||||
|
||||
// Convert to Vec3 (ignoring alpha)
|
||||
Vec3 toVec3() const {
|
||||
return Vec3(r, g, b);
|
||||
}
|
||||
|
||||
// Convert to 8-bit color values
|
||||
void toUint8(uint8_t& red, uint8_t& green, uint8_t& blue, uint8_t& alpha) const {
|
||||
red = static_cast<uint8_t>(std::clamp(r, 0.0f, 1.0f) * 255);
|
||||
green = static_cast<uint8_t>(std::clamp(g, 0.0f, 1.0f) * 255);
|
||||
blue = static_cast<uint8_t>(std::clamp(b, 0.0f, 1.0f) * 255);
|
||||
alpha = static_cast<uint8_t>(std::clamp(a, 0.0f, 1.0f) * 255);
|
||||
}
|
||||
|
||||
void toUint8(uint8_t& red, uint8_t& green, uint8_t& blue) const {
|
||||
red = static_cast<uint8_t>(std::clamp(r, 0.0f, 1.0f) * 255);
|
||||
green = static_cast<uint8_t>(std::clamp(g, 0.0f, 1.0f) * 255);
|
||||
blue = static_cast<uint8_t>(std::clamp(b, 0.0f, 1.0f) * 255);
|
||||
}
|
||||
// Get XYZ components as Vec3
|
||||
class Vec3 xyz() const;
|
||||
|
||||
// Get RGB components as Vec3
|
||||
class Vec3 rgb() const;
|
||||
|
||||
bool operator==(const Vec4& other) const {
|
||||
return x == other.x && y == other.y && z == other.z && w == other.w;
|
||||
}
|
||||
|
||||
bool operator!=(const Vec4& other) const {
|
||||
return x != other.x || y != other.y || z != other.z || w != other.w;
|
||||
}
|
||||
|
||||
bool operator<(const Vec4& other) const {
|
||||
return (x < other.x) ||
|
||||
(x == other.x && y < other.y) ||
|
||||
(x == other.x && y == other.y && z < other.z) ||
|
||||
(x == other.x && y == other.y && z == other.z && w < other.w);
|
||||
}
|
||||
|
||||
bool operator<=(const Vec4& other) const {
|
||||
return (x < other.x) ||
|
||||
(x == other.x && y < other.y) ||
|
||||
(x == other.x && y == other.y && z < other.z) ||
|
||||
(x == other.x && y == other.y && z == other.z && w <= other.w);
|
||||
}
|
||||
|
||||
bool operator>(const Vec4& other) const {
|
||||
return (x > other.x) ||
|
||||
(x == other.x && y > other.y) ||
|
||||
(x == other.x && y == other.y && z > other.z) ||
|
||||
(x == other.x && y == other.y && z == other.z && w > other.w);
|
||||
}
|
||||
|
||||
bool operator>=(const Vec4& other) const {
|
||||
return (x > other.x) ||
|
||||
(x == other.x && y > other.y) ||
|
||||
(x == other.x && y == other.y && z > other.z) ||
|
||||
(x == other.x && y == other.y && z == other.z && w >= other.w);
|
||||
}
|
||||
|
||||
Vec4 abs() const {
|
||||
return Vec4(std::abs(x), std::abs(y), std::abs(z), std::abs(w));
|
||||
}
|
||||
|
||||
Vec4 floor() const {
|
||||
return Vec4(std::floor(x), std::floor(y), std::floor(z), std::floor(w));
|
||||
}
|
||||
|
||||
Vec4 ceil() const {
|
||||
return Vec4(std::ceil(x), std::ceil(y), std::ceil(z), std::ceil(w));
|
||||
}
|
||||
|
||||
Vec4 round() const {
|
||||
return Vec4(std::round(x), std::round(y), std::round(z), std::round(w));
|
||||
}
|
||||
|
||||
Vec4 min(const Vec4& other) const {
|
||||
return Vec4(std::min(x, other.x), std::min(y, other.y),
|
||||
std::min(z, other.z), std::min(w, other.w));
|
||||
}
|
||||
|
||||
Vec4 max(const Vec4& other) const {
|
||||
return Vec4(std::max(x, other.x), std::max(y, other.y),
|
||||
std::max(z, other.z), std::max(w, other.w));
|
||||
}
|
||||
|
||||
Vec4 clamp(float minVal, float maxVal) const {
|
||||
return Vec4(
|
||||
std::clamp(x, minVal, maxVal),
|
||||
std::clamp(y, minVal, maxVal),
|
||||
std::clamp(z, minVal, maxVal),
|
||||
std::clamp(w, minVal, maxVal)
|
||||
);
|
||||
}
|
||||
|
||||
// Color-specific clamping (clamps RGB between 0 and 1)
|
||||
Vec4 clampColor() const {
|
||||
return Vec4(
|
||||
std::clamp(r, 0.0f, 1.0f),
|
||||
std::clamp(g, 0.0f, 1.0f),
|
||||
std::clamp(b, 0.0f, 1.0f),
|
||||
std::clamp(a, 0.0f, 1.0f)
|
||||
);
|
||||
}
|
||||
|
||||
bool isZero(float epsilon = 1e-10f) const {
|
||||
return std::abs(x) < epsilon && std::abs(y) < epsilon &&
|
||||
std::abs(z) < epsilon && std::abs(w) < epsilon;
|
||||
}
|
||||
|
||||
bool equals(const Vec4& other, float epsilon = 1e-10f) const {
|
||||
return std::abs(x - other.x) < epsilon &&
|
||||
std::abs(y - other.y) < epsilon &&
|
||||
std::abs(z - other.z) < epsilon &&
|
||||
std::abs(w - other.w) < epsilon;
|
||||
}
|
||||
|
||||
friend Vec4 operator+(float scalar, const Vec4& vec) {
|
||||
return Vec4(scalar + vec.x, scalar + vec.y, scalar + vec.z, scalar + vec.w);
|
||||
}
|
||||
|
||||
friend Vec4 operator-(float scalar, const Vec4& vec) {
|
||||
return Vec4(scalar - vec.x, scalar - vec.y, scalar - vec.z, scalar - vec.w);
|
||||
}
|
||||
|
||||
friend Vec4 operator*(float scalar, const Vec4& vec) {
|
||||
return Vec4(scalar * vec.x, scalar * vec.y, scalar * vec.z, scalar * vec.w);
|
||||
}
|
||||
|
||||
friend Vec4 operator/(float scalar, const Vec4& vec) {
|
||||
return Vec4(scalar / vec.x, scalar / vec.y, scalar / vec.z, scalar / vec.w);
|
||||
}
|
||||
|
||||
Vec4 lerp(const Vec4& other, float t) const {
|
||||
t = std::clamp(t, 0.0f, 1.0f);
|
||||
return *this + (other - *this) * t;
|
||||
}
|
||||
|
||||
// Convert to grayscale using standard RGB weights
|
||||
float grayscale() const {
|
||||
return r * 0.299f + g * 0.587f + b * 0.114f;
|
||||
}
|
||||
|
||||
// Color inversion (1.0 - color)
|
||||
Vec4 inverted() const {
|
||||
return Vec4(1.0f - r, 1.0f - g, 1.0f - b, a);
|
||||
}
|
||||
|
||||
float& operator[](int index) {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
const float& operator[](int index) const {
|
||||
return (&x)[index];
|
||||
}
|
||||
|
||||
std::string toString() const {
|
||||
return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " +
|
||||
std::to_string(z) + ", " + std::to_string(w) + ")";
|
||||
}
|
||||
|
||||
std::string toColorString() const {
|
||||
return "RGBA(" + std::to_string(r) + ", " + std::to_string(g) + ", " +
|
||||
std::to_string(b) + ", " + std::to_string(a) + ")";
|
||||
}
|
||||
};
|
||||
|
||||
inline std::ostream& operator<<(std::ostream& os, const Vec4& vec) {
|
||||
os << vec.toString();
|
||||
return os;
|
||||
}
|
||||
|
||||
namespace std {
|
||||
template<>
|
||||
struct hash<Vec4> {
|
||||
size_t operator()(const Vec4& v) const {
|
||||
return hash<float>()(v.x) ^ (hash<float>()(v.y) << 1) ^
|
||||
(hash<float>()(v.z) << 2) ^ (hash<float>()(v.w) << 3);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user