Skip to content

Commit

Permalink
feat(sourcepp): add vector operators, more typedefs
Browse files Browse the repository at this point in the history
  • Loading branch information
craftablescience committed Jun 13, 2024
1 parent 19fcbde commit 7cc5cde
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 15 deletions.
10 changes: 8 additions & 2 deletions include/sourcepp/math/Integer.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#pragma once

#include <cstdint>
#include <type_traits>

// Integer types are intentionally outside the sourcepp namespace

using std::int8_t;
using std::int16_t;
using std::int32_t;
using std::int64_t;

using std::uint8_t;
using std::uint16_t;
using std::uint32_t;
using std::uint64_t;

namespace sourcepp::math {

template<typename T>
concept Arithmetic = std::is_arithmetic_v<T>;

} // namespace sourcepp::math
4 changes: 1 addition & 3 deletions include/sourcepp/math/Matrix.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#pragma once

#include <concepts>

#include "Integer.h"

namespace sourcepp::math {

template<uint8_t M, uint8_t N, std::floating_point P = float>
template<uint8_t M, uint8_t N, Arithmetic P = float>
class Matrix {
public:
[[nodiscard]] P* operator[](uint8_t i) { return this->data[i]; }
Expand Down
98 changes: 88 additions & 10 deletions include/sourcepp/math/Vector.h
Original file line number Diff line number Diff line change
@@ -1,34 +1,112 @@
#pragma once

#include <concepts>
#include "Integer.h"

namespace sourcepp::math {

template<std::floating_point P>
template<Arithmetic P>
struct Vec2 {
P x;
P y;

[[nodiscard]] Vec2 operator+(const Vec2& other) const {
return {this->x + other.x, this->y + other.y};
}

[[nodiscard]] Vec2 operator-(const Vec2& other) const {
return {this->x - other.x, this->y - other.y};
}

[[nodiscard]] static constexpr Vec2 zero() {
return {{}, {}};
}
};
using Vec2f = Vec2<float>;
using Vec2d = Vec2<double>;

template<std::floating_point P>
using Vec2i8 = Vec2<int8_t>;
using Vec2i16 = Vec2<int16_t>;
using Vec2i32 = Vec2<int32_t>;
using Vec2i64 = Vec2<int64_t>;
using Vec2i = Vec2i32;

using Vec2ui8 = Vec2<uint8_t>;
using Vec2ui16 = Vec2<uint16_t>;
using Vec2ui32 = Vec2<uint32_t>;
using Vec2ui64 = Vec2<uint64_t>;
using Vec2ui = Vec2ui32;

using Vec2f32 = Vec2<float>;
using Vec2f64 = Vec2<double>;
using Vec2f = Vec2f32;

template<Arithmetic P>
struct Vec3 {
P x;
P y;
P z;

[[nodiscard]] Vec3 operator+(const Vec3& other) const {
return {this->x + other.x, this->y + other.y, this->z + other.z};
}

[[nodiscard]] Vec3 operator-(const Vec3& other) const {
return {this->x - other.x, this->y - other.y, this->z - other.z};
}

[[nodiscard]] static constexpr Vec3 zero() {
return {{}, {}, {}};
}
};
using Vec3f = Vec3<float>;
using Vec3d = Vec3<double>;

template<std::floating_point P>
using Vec3i8 = Vec3<int8_t>;
using Vec3i16 = Vec3<int16_t>;
using Vec3i32 = Vec3<int32_t>;
using Vec3i64 = Vec3<int64_t>;
using Vec3i = Vec3i32;

using Vec3ui8 = Vec3<uint8_t>;
using Vec3ui16 = Vec3<uint16_t>;
using Vec3ui32 = Vec3<uint32_t>;
using Vec3ui64 = Vec3<uint64_t>;
using Vec3ui = Vec3ui32;

using Vec3f32 = Vec3<float>;
using Vec3f64 = Vec3<double>;
using Vec3f = Vec3f32;

template<Arithmetic P>
struct Vec4 {
P x;
P y;
P z;
P w;

[[nodiscard]] Vec4 operator+(const Vec4& other) const {
return {this->x + other.x, this->y + other.y, this->z + other.z, this->w + other.w};
}

[[nodiscard]] Vec4 operator-(const Vec4& other) const {
return {this->x - other.x, this->y - other.y, this->z - other.z, this->w - other.w};
}

[[nodiscard]] static constexpr Vec4 zero() {
return {{}, {}, {}, {}};
}
};
using Vec4f = Vec4<float>;
using Vec4d = Vec4<double>;

using Vec4i8 = Vec4<int8_t>;
using Vec4i16 = Vec4<int16_t>;
using Vec4i32 = Vec4<int32_t>;
using Vec4i64 = Vec4<int64_t>;
using Vec4i = Vec4i32;

using Vec4ui8 = Vec4<uint8_t>;
using Vec4ui16 = Vec4<uint16_t>;
using Vec4ui32 = Vec4<uint32_t>;
using Vec4ui64 = Vec4<uint64_t>;
using Vec4ui = Vec4ui32;

using Vec4f32 = Vec4<float>;
using Vec4f64 = Vec4<double>;
using Vec4f = Vec4f32;

} // namespace sourcepp::math

0 comments on commit 7cc5cde

Please sign in to comment.