BLT/include/blt/math/vectors.h

354 lines
12 KiB
C
Raw Normal View History

/*
* Created by Brett on 28/02/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef BLT_TESTS_VECTORS_H
#define BLT_TESTS_VECTORS_H
#include <initializer_list>
#include <cmath>
2023-03-15 15:26:58 -04:00
#include <vector>
2023-04-05 17:21:19 -04:00
#include <cstdint>
2023-02-28 23:31:39 -05:00
namespace blt {
2023-03-14 18:20:10 -04:00
constexpr float EPSILON = 0.0001f;
static inline constexpr bool f_equal(float v1, float v2) {
return v1 >= v2 - EPSILON && v1 <= v2 + EPSILON;
}
2023-04-07 00:02:21 -04:00
#define MSVC_COMPILER (!defined(__GNUC__) && !defined(__clang__))
2023-03-14 18:20:10 -04:00
2023-04-21 21:04:19 -04:00
2023-04-07 00:02:21 -04:00
template<typename T, uint32_t size>
2023-02-28 23:31:39 -05:00
struct vec {
2023-04-21 21:39:34 -04:00
private:
2023-06-26 21:33:42 -04:00
T elements[size];
2023-02-28 23:31:39 -05:00
public:
vec() {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] = 0;
}
2023-02-28 23:31:39 -05:00
vec(std::initializer_list<T> args): vec() {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < args.size(); i++)
2023-02-28 23:31:39 -05:00
elements[i] = *(args.begin() + i);
}
2023-02-28 23:31:39 -05:00
2023-04-21 21:39:34 -04:00
explicit vec(const T elem[size]) {
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] = elem[i];
}
2023-04-21 21:39:34 -04:00
vec(const vec<T, size>& copy): vec(copy.elements) {}
2023-04-21 21:37:11 -04:00
template<typename o_T, uint32_t o_size>
2023-04-21 21:39:34 -04:00
explicit vec(const vec<o_T, o_size>& copy) {
2023-06-26 21:33:42 -04:00
for (uint32_t i = 0; i < std::min(o_size, size); i++)
2023-04-21 21:39:34 -04:00
elements[i] = copy[i];
}
2023-02-28 23:31:39 -05:00
2023-04-05 17:21:19 -04:00
vec& operator=(const vec<T, size>& copy) {
if (&copy == this)
return *this;
for (uint32_t i = 0; i < size; i++)
elements[i] = copy[i];
return *this;
}
[[nodiscard]] inline T x() const {
return elements[0];
}
2023-02-28 23:31:39 -05:00
[[nodiscard]] inline T y() const {
static_assert(size > 1);
return elements[1];
}
2023-02-28 23:31:39 -05:00
[[nodiscard]] inline T z() const {
static_assert(size > 2);
return elements[2];
}
2023-02-28 23:31:39 -05:00
[[nodiscard]] inline T w() const {
static_assert(size > 3);
return elements[3];
}
2023-02-28 23:31:39 -05:00
2023-03-08 23:49:57 -05:00
[[nodiscard]] inline T magnitude() const {
T total = 0;
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-03-08 23:49:57 -05:00
total += elements[i] * elements[i];
return std::sqrt(total);
}
2023-03-14 18:05:24 -04:00
[[nodiscard]] inline vec<T, size> normalize() const {
2023-04-07 00:02:21 -04:00
T mag = this->magnitude();
2023-03-14 18:04:53 -04:00
if (mag == 0)
2023-04-05 17:21:19 -04:00
return vec<T, size>(*this);
2023-03-14 18:04:53 -04:00
return *this / mag;
2023-03-08 23:49:57 -05:00
}
2023-02-28 23:31:39 -05:00
inline T& operator[](int index) {
return elements[index];
}
inline T operator[](int index) const {
return elements[index];
}
inline vec<T, size>& operator=(T v) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
elements[i] = v;
2023-02-28 23:31:39 -05:00
return *this;
}
inline vec<T, size> operator-() {
2023-04-21 21:37:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-04-21 21:37:11 -04:00
initializer[i] = -elements[i];
return vec<T, size>{initializer};
2023-02-28 23:31:39 -05:00
}
inline vec<T, size>& operator+=(const vec<T, size>& other) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] += other[i];
return *this;
}
inline vec<T, size>& operator*=(const vec<T, size>& other) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] *= other[i];
return *this;
}
inline vec<T, size>& operator+=(T f) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] += f;
return *this;
}
inline vec<T, size>& operator*=(T f) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] *= f;
return *this;
}
inline vec<T, size>& operator-=(const vec<T, size>& other) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] -= other[i];
return *this;
}
inline vec<T, size>& operator-=(T f) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] -= f;
return *this;
}
/**
* performs the dot product of left * right
*/
2023-03-14 18:20:10 -04:00
static inline constexpr T dot(const vec<T, size>& left, const vec<T, size>& right) {
2023-02-28 23:31:39 -05:00
T dot = 0;
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
dot += left[i] * right[i];
return dot;
}
2023-03-10 17:01:44 -05:00
2023-04-21 21:04:19 -04:00
static inline constexpr vec<T, size> cross(
const vec<T, size>& left, const vec<T, size>& right
) {
2023-03-10 17:01:44 -05:00
// cross is only defined on vectors of size 3. 2D could be implemented, which is a TODO
static_assert(size == 3);
return {left.y() * right.z() - left.z() * right.y(),
left.z() * right.x() - left.x() * right.z(),
left.x() * right.y() - left.y() * right.x()};
}
2023-03-10 17:24:00 -05:00
2023-04-21 21:04:19 -04:00
static inline constexpr vec<T, size> project(
const vec<T, size>& u, const vec<T, size>& v
) {
2023-04-07 00:02:21 -04:00
T du = dot(u);
T dv = dot(v);
2023-03-10 17:24:00 -05:00
return (du / dv) * v;
}
2023-02-28 23:31:39 -05:00
};
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-03-14 18:20:10 -04:00
inline constexpr vec<T, size> operator+(const vec<T, size>& left, const vec<T, size>& right) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = left[i] + right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-03-14 18:20:10 -04:00
inline constexpr vec<T, size> operator-(const vec<T, size>& left, const vec<T, size>& right) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = left[i] - right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-04-07 00:02:21 -04:00
inline constexpr vec<T, size> operator+(const vec<T, size>& left, T f) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = left[i] + f;
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-04-07 00:02:21 -04:00
inline constexpr vec<T, size> operator-(const vec<T, size>& left, T f) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = left[i] + f;
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-04-07 00:02:21 -04:00
inline constexpr vec<T, size> operator+(T f, const vec<T, size>& right) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = f + right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-04-07 00:02:21 -04:00
inline constexpr vec<T, size> operator-(T f, const vec<T, size>& right) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = f - right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-03-14 18:20:10 -04:00
inline constexpr vec<T, size> operator*(const vec<T, size>& left, const vec<T, size>& right) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = left[i] * right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-04-07 00:02:21 -04:00
inline constexpr vec<T, size> operator*(const vec<T, size>& left, T f) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = left[i] * f;
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-04-07 00:02:21 -04:00
inline constexpr vec<T, size> operator*(T f, const vec<T, size>& right) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = f * right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-04-07 00:02:21 -04:00
inline constexpr vec<T, size> operator/(const vec<T, size>& left, T f) {
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
initializer[i] = left[i] / f;
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-03-14 18:20:10 -04:00
inline constexpr bool operator==(const vec<T, size>& left, const vec<T, size>& right) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-03-14 18:20:10 -04:00
if (left[i] != right[i])
return false;
return true;
}
2024-01-02 01:18:47 -05:00
template<typename T, uint32_t size>
inline constexpr bool operator!=(const vec<T, size>& left, const vec<T, size>& right) {
return !(left == right);
}
2023-04-05 17:21:19 -04:00
template<typename T, uint32_t size>
2023-03-14 18:20:10 -04:00
inline constexpr bool operator&&(const vec<T, size>& left, const vec<T, size>& right) {
2023-04-05 17:21:19 -04:00
for (uint32_t i = 0; i < size; i++)
2023-03-14 18:20:10 -04:00
if (!f_equal(left[i], right[i]))
return false;
return true;
}
2023-02-28 23:31:39 -05:00
typedef vec<float, 2> vec2f;
typedef vec<float, 3> vec3f;
typedef vec<float, 4> vec4f;
typedef vec<double, 2> vec2d;
typedef vec<double, 3> vec3d;
typedef vec<double, 4> vec4d;
2023-04-05 17:21:19 -04:00
typedef vec<int32_t, 2> vec2i;
typedef vec<int32_t, 3> vec3i;
typedef vec<int32_t, 4> vec4i;
2023-02-28 23:31:39 -05:00
2023-04-05 17:21:19 -04:00
typedef vec<int64_t, 2> vec2l;
typedef vec<int64_t, 3> vec3l;
typedef vec<int64_t, 4> vec4l;
2023-02-28 23:31:39 -05:00
2023-04-05 17:21:19 -04:00
typedef vec<uint32_t, 2> vec2ui;
typedef vec<uint32_t, 3> vec3ui;
typedef vec<uint32_t, 4> vec4ui;
2023-02-28 23:31:39 -05:00
2023-04-05 17:21:19 -04:00
typedef vec<uint64_t, 2> vec2ul;
typedef vec<uint64_t, 3> vec3ul;
typedef vec<uint64_t, 4> vec4ul;
2023-02-28 23:31:39 -05:00
typedef vec2f vec2;
typedef vec3f vec3;
typedef vec4f vec4;
2023-03-15 15:24:03 -04:00
namespace vec_algorithm {
2023-03-15 15:25:15 -04:00
static inline void findOrthogonalBasis(const vec3& v, vec3& v1, vec3& v2, vec3& v3) {
2023-03-15 15:22:28 -04:00
v1 = v.normalize();
2023-04-21 21:04:19 -04:00
2023-03-15 15:22:28 -04:00
vec3 arbitraryVector{1, 0, 0};
if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9) {
arbitraryVector = vec3{0, 1, 0};
}
2023-04-21 21:04:19 -04:00
2023-03-15 15:22:28 -04:00
v2 = vec3::cross(v, arbitraryVector).normalize();
v3 = vec3::cross(v1, v2);
}
2023-04-21 21:04:19 -04:00
2023-03-15 15:22:28 -04:00
// Gram-Schmidt orthonormalization algorithm
2023-03-15 15:25:15 -04:00
static inline void gramSchmidt(std::vector<vec3>& vectors) {
2023-04-21 21:04:19 -04:00
int n = (int) vectors.size();
2023-03-15 15:22:28 -04:00
std::vector<vec3> basis;
2023-04-21 21:04:19 -04:00
2023-03-15 15:22:28 -04:00
// normalize first vector
basis.push_back(vectors[0]);
basis[0] = basis[0].normalize();
2023-04-21 21:04:19 -04:00
2023-03-15 15:22:28 -04:00
// iterate over the rest of the vectors
for (int i = 1; i < n; ++i) {
// subtract the projections of the vector onto the previous basis vectors
vec3 new_vector = vectors[i];
for (int j = 0; j < i; ++j) {
float projection = vec3::dot(vectors[i], basis[j]);
new_vector[0] -= projection * basis[j].x();
new_vector[1] -= projection * basis[j].y();
new_vector[2] -= projection * basis[j].z();
}
// normalize the new basis vector
new_vector = new_vector.normalize();
basis.push_back(new_vector);
}
2023-04-21 21:04:19 -04:00
2023-03-15 15:22:28 -04:00
vectors = basis;
}
}
}
#endif //BLT_TESTS_VECTORS_H