make vec class trivially copyable. Also add a couple new useful constructors

v1
Brett 2024-02-06 00:00:35 -05:00
parent db158fcff3
commit e0f2069b19
2 changed files with 225 additions and 114 deletions

View File

@ -11,141 +11,185 @@
#include <cmath>
#include <vector>
#include <cstdint>
#include <array>
#include <type_traits>
namespace blt {
namespace blt
{
constexpr float EPSILON = 0.0001f;
static inline constexpr bool f_equal(float v1, float v2) {
static inline constexpr bool f_equal(float v1, float v2)
{
return v1 >= v2 - EPSILON && v1 <= v2 + EPSILON;
}
#define MSVC_COMPILER (!defined(__GNUC__) && !defined(__clang__))
template<typename T, uint32_t size>
struct vec {
template<typename T, std::uint32_t size>
struct vec
{
static_assert(std::is_arithmetic_v<T> && "blt::vec must be created using an arithmetic type!");
private:
T elements[size];
std::array<T, size> elements;
public:
vec() {
for (uint32_t i = 0; i < size; i++)
elements[i] = 0;
vec()
{
for (auto& v : elements)
v = static_cast<T>(0);
}
vec(std::initializer_list<T> args): vec() {
for (uint32_t i = 0; i < args.size(); i++)
elements[i] = *(args.begin() + i);
/**
* 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
*/
vec(T t, std::initializer_list<T> args)
{
auto b = args.begin();
for (auto& v : elements)
{
if (b == args.end())
{
v = t;
continue;
}
v = *b;
++b;
}
}
explicit vec(const T elem[size]) {
for (uint32_t i = 0; i < size; i++)
/**
* 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
*/
vec(std::initializer_list<T> args): vec(static_cast<T>(0), args)
{}
explicit vec(T t)
{
for (auto& v : elements)
v = t;
}
explicit vec(const T elem[size])
{
for (size_t i = 0; i < size; i++)
elements[i] = elem[i];
}
vec(const vec<T, size>& copy): vec(copy.elements) {}
template<typename o_T, uint32_t o_size>
explicit vec(const vec<o_T, o_size>& copy) {
for (uint32_t i = 0; i < std::min(o_size, size); i++)
elements[i] = copy[i];
explicit vec(std::array<T, size> elem)
{
auto b = elem.begin();
for (auto& v : elements)
{
v = *b;
++b;
}
}
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 {
[[nodiscard]] inline T x() const
{
return elements[0];
}
[[nodiscard]] inline T y() const {
[[nodiscard]] inline T y() const
{
static_assert(size > 1);
return elements[1];
}
[[nodiscard]] inline T z() const {
[[nodiscard]] inline T z() const
{
static_assert(size > 2);
return elements[2];
}
[[nodiscard]] inline T w() const {
[[nodiscard]] inline T w() const
{
static_assert(size > 3);
return elements[3];
}
[[nodiscard]] inline T magnitude() const {
[[nodiscard]] inline T magnitude() const
{
T total = 0;
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
total += elements[i] * elements[i];
return std::sqrt(total);
}
[[nodiscard]] inline vec<T, size> normalize() const {
[[nodiscard]] inline vec<T, size> normalize() const
{
T mag = this->magnitude();
if (mag == 0)
return vec<T, size>(*this);
return *this / mag;
}
inline T& operator[](int index) {
inline T& operator[](int index)
{
return elements[index];
}
inline T operator[](int index) const {
inline T operator[](int index) const
{
return elements[index];
}
inline vec<T, size>& operator=(T v) {
for (uint32_t i = 0; i < size; i++)
inline vec<T, size>& operator=(T v)
{
for (std::uint32_t i = 0; i < size; i++)
elements[i] = v;
return *this;
}
inline vec<T, size> operator-() {
inline vec<T, size> operator-()
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = -elements[i];
return vec<T, size>{initializer};
}
inline vec<T, size>& operator+=(const vec<T, size>& other) {
for (uint32_t i = 0; i < size; i++)
inline vec<T, size>& operator+=(const vec<T, size>& other)
{
for (std::uint32_t i = 0; i < size; i++)
elements[i] += other[i];
return *this;
}
inline vec<T, size>& operator*=(const vec<T, size>& other) {
for (uint32_t i = 0; i < size; i++)
inline vec<T, size>& operator*=(const vec<T, size>& other)
{
for (std::uint32_t i = 0; i < size; i++)
elements[i] *= other[i];
return *this;
}
inline vec<T, size>& operator+=(T f) {
for (uint32_t i = 0; i < size; i++)
inline vec<T, size>& operator+=(T f)
{
for (std::uint32_t i = 0; i < size; i++)
elements[i] += f;
return *this;
}
inline vec<T, size>& operator*=(T f) {
for (uint32_t i = 0; i < size; i++)
inline vec<T, size>& operator*=(T f)
{
for (std::uint32_t i = 0; i < size; i++)
elements[i] *= f;
return *this;
}
inline vec<T, size>& operator-=(const vec<T, size>& other) {
for (uint32_t i = 0; i < size; i++)
inline vec<T, size>& operator-=(const vec<T, size>& other)
{
for (std::uint32_t i = 0; i < size; i++)
elements[i] -= other[i];
return *this;
}
inline vec<T, size>& operator-=(T f) {
for (uint32_t i = 0; i < size; i++)
inline vec<T, size>& operator-=(T f)
{
for (std::uint32_t i = 0; i < size; i++)
elements[i] -= f;
return *this;
}
@ -153,16 +197,18 @@ namespace blt {
/**
* performs the dot product of left * right
*/
static inline constexpr T dot(const vec<T, size>& left, const vec<T, size>& right) {
static inline constexpr T dot(const vec<T, size>& left, const vec<T, size>& right)
{
T dot = 0;
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
dot += left[i] * right[i];
return dot;
}
static inline constexpr vec<T, size> cross(
const vec<T, size>& left, const vec<T, size>& right
) {
)
{
// 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(),
@ -172,109 +218,153 @@ namespace blt {
static inline constexpr vec<T, size> project(
const vec<T, size>& u, const vec<T, size>& v
) {
)
{
T du = dot(u);
T dv = dot(v);
return (du / dv) * v;
}
auto begin()
{
return elements.begin();
}
auto end()
{
return elements.end();
}
auto rbegin()
{
return elements.rbegin();
}
auto rend()
{
return elements.rend();
}
[[nodiscard]] auto cbegin() const
{
return elements.cbegin();
}
[[nodiscard]] auto cend() const
{
return elements.cend();
}
};
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator+(const vec<T, size>& left, const vec<T, size>& right) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator+(const vec<T, size>& left, const vec<T, size>& right)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = left[i] + right[i];
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator-(const vec<T, size>& left, const vec<T, size>& right) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator-(const vec<T, size>& left, const vec<T, size>& right)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = left[i] - right[i];
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator+(const vec<T, size>& left, T f) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator+(const vec<T, size>& left, T f)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = left[i] + f;
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator-(const vec<T, size>& left, T f) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator-(const vec<T, size>& left, T f)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = left[i] + f;
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator+(T f, const vec<T, size>& right) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator+(T f, const vec<T, size>& right)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = f + right[i];
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator-(T f, const vec<T, size>& right) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator-(T f, const vec<T, size>& right)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = f - right[i];
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator*(const vec<T, size>& left, const vec<T, size>& right) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator*(const vec<T, size>& left, const vec<T, size>& right)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = left[i] * right[i];
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator*(const vec<T, size>& left, T f) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator*(const vec<T, size>& left, T f)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = left[i] * f;
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator*(T f, const vec<T, size>& right) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator*(T f, const vec<T, size>& right)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = f * right[i];
return initializer;
}
template<typename T, uint32_t size>
inline constexpr vec<T, size> operator/(const vec<T, size>& left, T f) {
template<typename T, std::uint32_t size>
inline constexpr vec<T, size> operator/(const vec<T, size>& left, T f)
{
vec<T, size> initializer{};
for (uint32_t i = 0; i < size; i++)
for (std::uint32_t i = 0; i < size; i++)
initializer[i] = left[i] / f;
return initializer;
}
template<typename T, uint32_t size>
inline constexpr bool operator==(const vec<T, size>& left, const vec<T, size>& right) {
for (uint32_t i = 0; i < size; i++)
template<typename T, std::uint32_t size>
inline constexpr bool operator==(const vec<T, size>& left, const vec<T, size>& right)
{
for (std::uint32_t i = 0; i < size; i++)
if (left[i] != right[i])
return false;
return true;
}
template<typename T, uint32_t size>
inline constexpr bool operator!=(const vec<T, size>& left, const vec<T, size>& right) {
template<typename T, std::uint32_t size>
inline constexpr bool operator!=(const vec<T, size>& left, const vec<T, size>& right)
{
return !(left == right);
}
template<typename T, uint32_t size>
inline constexpr bool operator&&(const vec<T, size>& left, const vec<T, size>& right) {
for (uint32_t i = 0; i < size; i++)
template<typename T, std::uint32_t size>
inline constexpr bool operator&&(const vec<T, size>& left, const vec<T, size>& right)
{
for (std::uint32_t i = 0; i < size; i++)
if (!f_equal(left[i], right[i]))
return false;
return true;
@ -288,32 +378,35 @@ namespace blt {
typedef vec<double, 3> vec3d;
typedef vec<double, 4> vec4d;
typedef vec<int32_t, 2> vec2i;
typedef vec<int32_t, 3> vec3i;
typedef vec<int32_t, 4> vec4i;
typedef vec<std::int32_t, 2> vec2i;
typedef vec<std::int32_t, 3> vec3i;
typedef vec<std::int32_t, 4> vec4i;
typedef vec<int64_t, 2> vec2l;
typedef vec<int64_t, 3> vec3l;
typedef vec<int64_t, 4> vec4l;
typedef vec<std::int64_t, 2> vec2l;
typedef vec<std::int64_t, 3> vec3l;
typedef vec<std::int64_t, 4> vec4l;
typedef vec<uint32_t, 2> vec2ui;
typedef vec<uint32_t, 3> vec3ui;
typedef vec<uint32_t, 4> vec4ui;
typedef vec<std::uint32_t, 2> vec2ui;
typedef vec<std::uint32_t, 3> vec3ui;
typedef vec<std::uint32_t, 4> vec4ui;
typedef vec<uint64_t, 2> vec2ul;
typedef vec<uint64_t, 3> vec3ul;
typedef vec<uint64_t, 4> vec4ul;
typedef vec<std::uint64_t, 2> vec2ul;
typedef vec<std::uint64_t, 3> vec3ul;
typedef vec<std::uint64_t, 4> vec4ul;
typedef vec2f vec2;
typedef vec3f vec3;
typedef vec4f vec4;
namespace vec_algorithm {
static inline void findOrthogonalBasis(const vec3& v, vec3& v1, vec3& v2, vec3& v3) {
namespace vec_algorithm
{
static inline void findOrthogonalBasis(const vec3& v, vec3& v1, vec3& v2, vec3& v3)
{
v1 = v.normalize();
vec3 arbitraryVector{1, 0, 0};
if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9) {
if (std::abs(vec3::dot(v, arbitraryVector)) > 0.9)
{
arbitraryVector = vec3{0, 1, 0};
}
@ -322,7 +415,8 @@ namespace blt {
}
// Gram-Schmidt orthonormalization algorithm
static inline void gramSchmidt(std::vector<vec3>& vectors) {
static inline void gramSchmidt(std::vector<vec3>& vectors)
{
int n = (int) vectors.size();
std::vector<vec3> basis;
@ -331,10 +425,12 @@ namespace blt {
basis[0] = basis[0].normalize();
// iterate over the rest of the vectors
for (int i = 1; i < n; ++i) {
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) {
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();

View File

@ -56,6 +56,21 @@ int (* func_func_in)(int) = &test_as_func;
int main(int argc, const char** argv)
{
// BLT_INFO("Is blt::vec3 trivial? %b", std::is_trivial_v<blt::vec3>);
// BLT_INFO("Is blt::vec3 trivially constructable? %b", std::is_trivially_constructible_v<blt::vec3>);
// BLT_INFO("Is blt::vec3 trivially copy constructable? %b", std::is_trivially_copy_constructible_v<blt::vec3>);
// BLT_INFO("Is blt::vec3 trivially copy assignment? %b", std::is_trivially_copy_assignable_v<blt::vec3>);
// BLT_INFO("Is blt::vec3 trivially copyable? %b", std::is_trivially_copyable_v<blt::vec3>);
// BLT_INFO("Is blt::vec3 trivially destructable? %b", std::is_trivially_destructible_v<blt::vec3>);
// BLT_INFO("Is blt::vec3 trivially move constructable? %b", std::is_trivially_move_constructible_v<blt::vec3>);
// BLT_INFO("Is blt::vec3 trivially move assignable? %b", std::is_trivially_move_assignable_v<blt::vec3>);
//
// blt::vec3 trivial;
// for (int i = 0; i < 3; i++)
// BLT_TRACE(trivial[i]);
//
// return 0;
blt::arg_parse parser;
parser.addArgument(