BLT/include/blt/math/vectors.h

536 lines
17 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>
#include <array>
#include <type_traits>
2024-04-09 19:09:50 -04:00
#include <blt/std/types.h>
namespace blt
{
2024-04-09 18:03:33 -04:00
#define MSVC_COMPILER (!defined(__GNUC__) && !defined(__clang__))
2023-02-28 23:31:39 -05:00
2023-03-14 18:20:10 -04:00
constexpr float EPSILON = 0.0001f;
static inline constexpr bool f_equal(float v1, float v2)
{
2023-03-14 18:20:10 -04:00
return v1 >= v2 - EPSILON && v1 <= v2 + EPSILON;
}
2024-04-09 19:09:50 -04:00
template<typename T, blt::u32 size>
2024-05-14 21:30:28 -04:00
struct vec
{
static_assert(std::is_arithmetic_v<T> && "blt::vec must be created using an arithmetic type!");
2023-04-21 21:39:34 -04:00
private:
std::array<T, size> elements;
2023-02-28 23:31:39 -05:00
public:
2024-05-03 15:57:04 -04:00
constexpr vec()
{
for (auto& v : elements)
v = static_cast<T>(0);
}
2023-02-28 23:31:39 -05:00
/**
* Create a vector with initializer list, if the initializer list doesn't contain enough values to fill this vec, it will use t
* @param t default value to fill with
* @param args list of args
*/
2024-04-09 19:09:50 -04:00
template<typename U, std::enable_if_t<std::is_same_v<T, U> || std::is_convertible_v<U, T>, bool> = true>
2024-05-03 15:57:04 -04:00
constexpr vec(U t, std::initializer_list<U> args): elements()
{
auto b = args.begin();
2024-05-14 21:30:28 -04:00
for (auto& v : elements)
{
if (b == args.end())
{
v = t;
continue;
}
v = *b;
++b;
}
}
2023-02-28 23:31:39 -05:00
/**
* Create a vector from an initializer list, if the list doesn't have enough elements it will be filled with the default value (0)
* @param args
*/
2024-04-09 19:09:50 -04:00
template<typename U, std::enable_if_t<std::is_same_v<T, U> || std::is_convertible_v<U, T>, bool> = true>
2024-05-03 15:57:04 -04:00
constexpr vec(std::initializer_list<U> args): vec(U(), args)
2024-04-09 19:09:50 -04:00
{}
template<typename... Args>
2024-05-03 15:57:04 -04:00
constexpr explicit vec(Args... args): vec(std::array<T, size>{static_cast<T>(args)...})
{}
2023-02-28 23:31:39 -05:00
2024-05-03 15:57:04 -04:00
constexpr explicit vec(T t)
{
for (auto& v : elements)
v = t;
}
2023-04-21 21:39:34 -04:00
2024-05-03 15:57:04 -04:00
constexpr explicit vec(const T elem[size])
{
for (size_t i = 0; i < size; i++)
elements[i] = elem[i];
2023-04-21 21:39:34 -04:00
}
2023-02-28 23:31:39 -05:00
2024-05-03 15:57:04 -04:00
constexpr explicit vec(std::array<T, size> elem): elements(elem)
{}
template<typename G, size_t base_size, std::enable_if_t<std::is_convertible_v<G, T>, bool> = true>
constexpr explicit vec(std::array<G, base_size> el): elements()
{
2024-05-03 15:57:04 -04:00
auto b = el.begin();
auto m = elements.begin();
while (b != el.end() && m != elements.end())
{
2024-05-03 15:57:04 -04:00
*m = *b;
++m;
++b;
}
2023-04-05 17:21:19 -04:00
}
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr inline T x() const
{
return elements[0];
}
2023-02-28 23:31:39 -05:00
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr inline T y() const
{
static_assert(size > 1);
return elements[1];
}
2023-02-28 23:31:39 -05:00
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr inline T z() const
{
static_assert(size > 2);
return elements[2];
}
2023-02-28 23:31:39 -05:00
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr inline T w() const
{
static_assert(size > 3);
return elements[3];
}
2023-02-28 23:31:39 -05:00
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr inline T magnitude() const
{
2023-03-08 23:49:57 -05:00
T total = 0;
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-03-08 23:49:57 -05:00
total += elements[i] * elements[i];
return std::sqrt(total);
}
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr 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
}
2024-05-03 15:57:04 -04:00
constexpr inline T& operator[](int index)
{
2023-02-28 23:31:39 -05:00
return elements[index];
}
2024-05-03 15:57:04 -04:00
constexpr inline T operator[](int index) const
{
2023-02-28 23:31:39 -05:00
return elements[index];
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size>& operator=(T v)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
elements[i] = v;
2023-02-28 23:31:39 -05:00
return *this;
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size> operator-()
{
2023-04-21 21:37:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 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
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size>& operator+=(const vec<T, size>& other)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] += other[i];
return *this;
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size>& operator*=(const vec<T, size>& other)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] *= other[i];
return *this;
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size>& operator+=(T f)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] += f;
return *this;
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size>& operator*=(T f)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] *= f;
return *this;
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size>& operator-=(const vec<T, size>& other)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-02-28 23:31:39 -05:00
elements[i] -= other[i];
return *this;
}
2024-05-03 15:57:04 -04:00
constexpr inline vec<T, size>& operator-=(T f)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 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
*/
2024-05-03 15:57:04 -04:00
constexpr static inline T dot(const vec<T, size>& left, const vec<T, size>& right)
{
2023-02-28 23:31:39 -05:00
T dot = 0;
2024-04-09 19:09:50 -04:00
for (blt::u32 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
2024-05-03 15:57:04 -04:00
constexpr static inline vec<T, size> cross(
2023-04-21 21:04:19 -04:00
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
2024-05-03 15:57:04 -04:00
constexpr static inline vec<T, size> project(
2023-04-21 21:04:19 -04:00
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;
}
2024-05-03 15:57:04 -04:00
constexpr inline auto* data()
2024-04-12 00:03:56 -04:00
{
2024-04-12 00:04:23 -04:00
return elements.data();
2024-04-12 00:03:56 -04:00
}
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr inline const auto* data() const
2024-04-12 00:03:56 -04:00
{
2024-04-12 00:04:23 -04:00
return elements.data();
2024-04-12 00:03:56 -04:00
}
2024-05-03 15:57:04 -04:00
constexpr auto begin()
{
return elements.begin();
}
2024-05-03 15:57:04 -04:00
constexpr auto end()
{
return elements.end();
}
2024-05-03 15:57:04 -04:00
constexpr auto rbegin()
{
return elements.rbegin();
}
2024-05-03 15:57:04 -04:00
constexpr auto rend()
{
return elements.rend();
}
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr auto cbegin() const
{
return elements.cbegin();
}
2024-05-03 15:57:04 -04:00
[[nodiscard]] constexpr auto cend() const
{
return elements.cend();
}
2023-02-28 23:31:39 -05:00
};
2024-04-09 19:09:50 -04:00
template<typename T, blt::u32 size>
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{};
2024-04-09 19:09:50 -04:00
for (blt::u32 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
}
2024-04-09 19:09:50 -04:00
template<typename T, blt::u32 size>
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{};
2024-04-09 19:09:50 -04:00
for (blt::u32 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
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator+(const vec<T, size>& left, G right)
{
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
initializer[i] = left[i] + static_cast<T>(right);
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator-(const vec<T, size>& left, G right)
{
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
initializer[i] = left[i] + static_cast<T>(right);
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator+(G left, const vec<T, size>& right)
{
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
initializer[i] = static_cast<T>(left) + right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator-(G left, const vec<T, size>& right)
{
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
initializer[i] = static_cast<T>(left) - right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2024-04-09 19:09:50 -04:00
template<typename T, blt::u32 size>
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{};
2024-04-09 19:09:50 -04:00
for (blt::u32 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
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator*(const vec<T, size>& left, G right)
{
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
initializer[i] = left[i] * static_cast<T>(right);
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator*(G left, const vec<T, size>& right)
{
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
initializer[i] = static_cast<T>(left) * right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator/(const vec<T, size>& left, G right)
{
2023-04-21 21:35:11 -04:00
vec<T, size> initializer{};
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
initializer[i] = left[i] / static_cast<T>(right);
2024-05-01 21:25:29 -04:00
return initializer;
}
template<typename T, typename G, blt::u32 size>
inline constexpr vec<T, size> operator/(G left, const vec<T, size>& right)
2024-05-01 21:25:29 -04:00
{
vec<T, size> initializer{};
for (blt::u32 i = 0; i < size; i++)
initializer[i] = static_cast<T>(left) / right[i];
2023-04-21 21:35:11 -04:00
return initializer;
2023-02-28 23:31:39 -05:00
}
2024-04-09 19:09:50 -04:00
template<typename T, blt::u32 size>
inline constexpr bool operator==(const vec<T, size>& left, const vec<T, size>& right)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-03-14 18:20:10 -04:00
if (left[i] != right[i])
return false;
return true;
}
2024-04-09 19:09:50 -04:00
template<typename T, blt::u32 size>
inline constexpr bool operator!=(const vec<T, size>& left, const vec<T, size>& right)
{
2024-01-02 01:18:47 -05:00
return !(left == right);
}
2024-04-09 19:09:50 -04:00
template<typename T, blt::u32 size>
inline constexpr bool operator&&(const vec<T, size>& left, const vec<T, size>& right)
{
2024-04-09 19:09:50 -04:00
for (blt::u32 i = 0; i < size; i++)
2023-03-14 18:20:10 -04:00
if (!f_equal(left[i], right[i]))
return false;
return true;
}
2024-04-09 19:09:50 -04:00
using vec2f = vec<float, 2>;
using vec3f = vec<float, 3>;
using vec4f = vec<float, 4>;
2023-02-28 23:31:39 -05:00
2024-04-09 19:09:50 -04:00
using vec2d = vec<double, 2>;
using vec3d = vec<double, 3>;
using vec4d = vec<double, 4>;
2023-02-28 23:31:39 -05:00
2024-04-09 19:09:50 -04:00
using vec2i = vec<blt::i32, 2>;
using vec3i = vec<blt::i32, 3>;
using vec4i = vec<blt::i32, 4>;
2023-02-28 23:31:39 -05:00
2024-04-09 19:09:50 -04:00
using vec2l = vec<blt::i64, 2>;
using vec3l = vec<blt::i64, 3>;
using vec4l = vec<blt::i64, 4>;
2023-02-28 23:31:39 -05:00
2024-04-09 19:09:50 -04:00
using vec2ui = vec<blt::u32, 2>;
using vec3ui = vec<blt::u32, 3>;
using vec4ui = vec<blt::u32, 4>;
2023-02-28 23:31:39 -05:00
2024-04-09 19:09:50 -04:00
using vec2ul = vec<blt::u64, 2>;
using vec3ul = vec<blt::u64, 3>;
using vec4ul = vec<blt::u64, 4>;
2023-02-28 23:31:39 -05:00
2024-04-09 19:09:50 -04:00
using vec2 = vec2f;
using vec3 = vec3f;
using vec4 = vec4f;
2023-02-28 23:31:39 -05:00
2024-04-29 21:35:16 -04:00
using color4 = vec4;
using color3 = vec3;
2024-05-14 21:31:59 -04:00
inline constexpr color4 make_color(float r, float g, float b)
2024-04-29 21:35:16 -04:00
{
2024-04-29 21:46:32 -04:00
return color4{r, g, b, 1.0f};
2024-04-29 21:35:16 -04:00
}
2024-05-01 21:12:16 -04:00
template<typename ValueType, u32 size>
2024-05-14 21:31:59 -04:00
inline constexpr blt::vec<ValueType, 2> make_vec2(const blt::vec<ValueType, size>& t, size_t fill = 0)
2024-05-01 21:12:16 -04:00
{
2024-05-14 21:30:28 -04:00
if constexpr (size >= 2)
{
2024-05-01 21:12:16 -04:00
return blt::vec<ValueType, 2>(t.x(), t.y());
2024-05-14 21:30:28 -04:00
} else
{
2024-05-01 21:12:16 -04:00
return blt::vec<ValueType, 2>(t.x(), fill);
}
}
template<typename ValueType, u32 size>
2024-05-14 21:31:59 -04:00
inline constexpr blt::vec<ValueType, 3> make_vec3(const blt::vec<ValueType, size>& t, size_t fill = 0)
2024-05-01 21:12:16 -04:00
{
2024-05-14 21:30:28 -04:00
if constexpr (size >= 3)
{
2024-05-01 21:12:16 -04:00
return blt::vec<ValueType, 3>(t.x(), t.y(), t.z());
2024-05-14 21:30:28 -04:00
} else
{
2024-05-01 21:12:16 -04:00
blt::vec<ValueType, 3> ret;
for (size_t i = 0; i < size; i++)
ret[i] = t[i];
for (size_t i = size; i < 3; i++)
ret[i] = fill;
return ret;
}
}
template<typename ValueType, u32 size>
2024-05-14 21:31:59 -04:00
inline constexpr blt::vec<ValueType, 4> make_vec4(const blt::vec<ValueType, size>& t, size_t fill = 0)
2024-05-01 21:12:16 -04:00
{
2024-05-14 21:30:28 -04:00
if constexpr (size >= 4)
{
2024-05-01 21:12:16 -04:00
return blt::vec<ValueType, 4>(t.x(), t.y(), t.z(), t.w());
2024-05-14 21:30:28 -04:00
} else
{
2024-05-01 21:12:16 -04:00
blt::vec<ValueType, 4> ret;
for (size_t i = 0; i < size; i++)
ret[i] = t[i];
for (size_t i = size; i < 4; i++)
ret[i] = fill;
return ret;
}
}
namespace vec_algorithm
{
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};
2024-05-14 21:30:28 -04:00
if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9)
{
2023-03-15 15:22:28 -04:00
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
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
2024-05-14 21:30:28 -04:00
for (int i = 1; i < n; ++i)
{
2023-03-15 15:22:28 -04:00
// subtract the projections of the vector onto the previous basis vectors
vec3 new_vector = vectors[i];
2024-05-14 21:30:28 -04:00
for (int j = 0; j < i; ++j)
{
2023-03-15 15:22:28 -04:00
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