mirror of
https://github.com/BlackMATov/vmath.hpp.git
synced 2025-12-13 20:17:58 +07:00
basic types non-arithmetic T support
This commit is contained in:
45
README.md
45
README.md
@@ -85,7 +85,8 @@ class vec_base;
|
||||
template < typename T >
|
||||
class vec_base<T, 2> {
|
||||
public:
|
||||
T x{}, y{};
|
||||
T x = T{0};
|
||||
T y = T{0};
|
||||
|
||||
constexpr vec_base() = default;
|
||||
constexpr explicit vec_base(T v);
|
||||
@@ -97,7 +98,9 @@ public:
|
||||
template < typename T >
|
||||
class vec_base<T, 3> {
|
||||
public:
|
||||
T x{}, y{}, z{};
|
||||
T x = T{0};
|
||||
T y = T{0};
|
||||
T z = T{0};
|
||||
|
||||
constexpr vec_base() = default;
|
||||
constexpr explicit vec_base(T v);
|
||||
@@ -110,7 +113,10 @@ public:
|
||||
template < typename T >
|
||||
class vec_base<T, 4> {
|
||||
public:
|
||||
T x{}, y{}, z{}, w{};
|
||||
T x = T{0};
|
||||
T y = T{0};
|
||||
T z = T{0};
|
||||
T w = T{0};
|
||||
|
||||
constexpr vec_base() = default;
|
||||
constexpr explicit vec_base(T v);
|
||||
@@ -142,6 +148,10 @@ public:
|
||||
|
||||
static constexpr size_t size = Size;
|
||||
|
||||
constexpr vec() = default;
|
||||
constexpr vec(const vec&) = default;
|
||||
constexpr vec& operator=(const vec&) = default;
|
||||
|
||||
void swap(vec& other) noexcept(is_nothrow_swappable_v<T>);
|
||||
|
||||
iterator begin() noexcept;
|
||||
@@ -210,8 +220,8 @@ public:
|
||||
using row_type = vec<T, 2>;
|
||||
|
||||
row_type rows[2] = {
|
||||
{1, 0},
|
||||
{0, 1}};
|
||||
row_type{T{1}, T{0}},
|
||||
row_type{T{0}, T{1}}};
|
||||
|
||||
constexpr mat_base() = default;
|
||||
constexpr explicit mat_base(T d);
|
||||
@@ -235,9 +245,9 @@ public:
|
||||
using row_type = vec<T, 3>;
|
||||
|
||||
row_type rows[3] = {
|
||||
{1, 0, 0},
|
||||
{0, 1, 0},
|
||||
{0, 0, 1}};
|
||||
row_type{T{1}, T{0}, T{0}},
|
||||
row_type{T{0}, T{1}, T{0}},
|
||||
row_type{T{0}, T{0}, T{1}}};
|
||||
|
||||
constexpr mat_base() = default;
|
||||
constexpr explicit mat_base(T d);
|
||||
@@ -267,10 +277,10 @@ public:
|
||||
using row_type = vec<T, 4>;
|
||||
|
||||
row_type rows[4] = {
|
||||
{1, 0, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1}};
|
||||
row_type{T{1}, T{0}, T{0}, T{0}},
|
||||
row_type{T{0}, T{1}, T{0}, T{0}},
|
||||
row_type{T{0}, T{0}, T{1}, T{0}},
|
||||
row_type{T{0}, T{0}, T{0}, T{1}}};
|
||||
|
||||
constexpr mat_base() = default;
|
||||
constexpr explicit mat_base(T d);
|
||||
@@ -315,6 +325,10 @@ public:
|
||||
|
||||
static constexpr size_t size = Size;
|
||||
|
||||
constexpr mat() = default;
|
||||
constexpr mat(const mat&) = default;
|
||||
constexpr mat& operator=(const mat&) = default;
|
||||
|
||||
void swap(mat& other) noexcept(is_nothrow_swappable_v<T>);
|
||||
|
||||
iterator begin() noexcept;
|
||||
@@ -377,8 +391,11 @@ using ptrdiff4x4_t = mat<ptrdiff_t, 4>;
|
||||
template < typename T >
|
||||
class qua final {
|
||||
public:
|
||||
vec<T, 3> v{0};
|
||||
T s{1};
|
||||
using imag_type = vec<T, 3>;
|
||||
using real_type = T;
|
||||
|
||||
imag_type v = imag_type{T{0}};
|
||||
real_type s = real_type{T{1}};
|
||||
public:
|
||||
using self_type = qua;
|
||||
using component_type = T;
|
||||
|
||||
@@ -20,21 +20,22 @@ namespace vmath_hpp::detail
|
||||
class mat_base<T, 2> {
|
||||
public:
|
||||
using row_type = vec<T, 2>;
|
||||
|
||||
row_type rows[2] = {
|
||||
{1, 0},
|
||||
{0, 1}};
|
||||
row_type{T{1}, T{0}},
|
||||
row_type{T{0}, T{1}}};
|
||||
public:
|
||||
constexpr mat_base() = default;
|
||||
|
||||
constexpr explicit mat_base(T d)
|
||||
: rows{
|
||||
row_type{d, 0},
|
||||
row_type{0, d}} {}
|
||||
row_type{d, T{0}},
|
||||
row_type{T{0}, d}} {}
|
||||
|
||||
constexpr explicit mat_base(const row_type& d)
|
||||
: rows{
|
||||
row_type{d[0], 0},
|
||||
row_type{0, d[1]}} {}
|
||||
row_type{d[0], T{0}},
|
||||
row_type{T{0}, d[1]}} {}
|
||||
|
||||
constexpr mat_base(
|
||||
T m11, T m12,
|
||||
@@ -65,24 +66,25 @@ namespace vmath_hpp::detail
|
||||
class mat_base<T, 3> {
|
||||
public:
|
||||
using row_type = vec<T, 3>;
|
||||
|
||||
row_type rows[3] = {
|
||||
{1, 0, 0},
|
||||
{0, 1, 0},
|
||||
{0, 0, 1}};
|
||||
row_type{T{1}, T{0}, T{0}},
|
||||
row_type{T{0}, T{1}, T{0}},
|
||||
row_type{T{0}, T{0}, T{1}}};
|
||||
public:
|
||||
constexpr mat_base() = default;
|
||||
|
||||
constexpr explicit mat_base(T d)
|
||||
: rows{
|
||||
row_type{d, 0, 0},
|
||||
row_type{0, d, 0},
|
||||
row_type{0, 0, d}} {}
|
||||
row_type{d, T{0}, T{0}},
|
||||
row_type{T{0}, d, T{0}},
|
||||
row_type{T{0}, T{0}, d}} {}
|
||||
|
||||
constexpr explicit mat_base(const row_type& d)
|
||||
: rows{
|
||||
row_type{d[0], 0, 0},
|
||||
row_type{0, d[1], 0},
|
||||
row_type{0, 0, d[2]}} {}
|
||||
row_type{d[0], T{0}, T{0}},
|
||||
row_type{T{0}, d[1], T{0}},
|
||||
row_type{T{0}, T{0}, d[2]}} {}
|
||||
|
||||
constexpr mat_base(
|
||||
T m11, T m12, T m13,
|
||||
@@ -103,16 +105,16 @@ namespace vmath_hpp::detail
|
||||
const mat_base<T, 2>& m,
|
||||
const vec_base<T, 2>& v)
|
||||
: rows{
|
||||
row_type{m.rows[0], 0},
|
||||
row_type{m.rows[1], 0},
|
||||
row_type{v, 1}} {}
|
||||
row_type{m.rows[0], T{0}},
|
||||
row_type{m.rows[1], T{0}},
|
||||
row_type{v, T{1}}} {}
|
||||
|
||||
constexpr explicit mat_base(
|
||||
const mat_base<T, 2>& other)
|
||||
: rows{
|
||||
row_type{other.rows[0], 0},
|
||||
row_type{other.rows[1], 0},
|
||||
row_type{0, 0, 1}} {}
|
||||
row_type{other.rows[0], T{0}},
|
||||
row_type{other.rows[1], T{0}},
|
||||
row_type{T{0}, T{0}, T{1}}} {}
|
||||
|
||||
constexpr explicit mat_base(
|
||||
const mat_base<T, 4>& other)
|
||||
@@ -126,27 +128,28 @@ namespace vmath_hpp::detail
|
||||
class mat_base<T, 4> {
|
||||
public:
|
||||
using row_type = vec<T, 4>;
|
||||
|
||||
row_type rows[4] = {
|
||||
{1, 0, 0, 0},
|
||||
{0, 1, 0, 0},
|
||||
{0, 0, 1, 0},
|
||||
{0, 0, 0, 1}};
|
||||
row_type{T{1}, T{0}, T{0}, T{0}},
|
||||
row_type{T{0}, T{1}, T{0}, T{0}},
|
||||
row_type{T{0}, T{0}, T{1}, T{0}},
|
||||
row_type{T{0}, T{0}, T{0}, T{1}}};
|
||||
public:
|
||||
constexpr mat_base() = default;
|
||||
|
||||
constexpr explicit mat_base(T d)
|
||||
: rows{
|
||||
row_type{d, 0, 0, 0},
|
||||
row_type{0, d, 0, 0},
|
||||
row_type{0, 0, d, 0},
|
||||
row_type{0, 0, 0, d}} {}
|
||||
row_type{d, T{0}, T{0}, T{0}},
|
||||
row_type{T{0}, d, T{0}, T{0}},
|
||||
row_type{T{0}, T{0}, d, T{0}},
|
||||
row_type{T{0}, T{0}, T{0}, d}} {}
|
||||
|
||||
constexpr explicit mat_base(const row_type& d)
|
||||
: rows{
|
||||
row_type{d[0], 0, 0, 0},
|
||||
row_type{0, d[1], 0, 0},
|
||||
row_type{0, 0, d[2], 0},
|
||||
row_type{0, 0, 0, d[3]}} {}
|
||||
row_type{d[0], T{0}, T{0}, T{0}},
|
||||
row_type{T{0}, d[1], T{0}, T{0}},
|
||||
row_type{T{0}, T{0}, d[2], T{0}},
|
||||
row_type{T{0}, T{0}, T{0}, d[3]}} {}
|
||||
|
||||
constexpr mat_base(
|
||||
T m11, T m12, T m13, T m14,
|
||||
@@ -170,26 +173,26 @@ namespace vmath_hpp::detail
|
||||
const mat_base<T, 3>& m,
|
||||
const vec_base<T, 3>& v)
|
||||
: rows{
|
||||
row_type{m.rows[0], 0},
|
||||
row_type{m.rows[1], 0},
|
||||
row_type{m.rows[2], 0},
|
||||
row_type{v, 1}} {}
|
||||
row_type{m.rows[0], T{0}},
|
||||
row_type{m.rows[1], T{0}},
|
||||
row_type{m.rows[2], T{0}},
|
||||
row_type{v, T{1}}} {}
|
||||
|
||||
constexpr explicit mat_base(
|
||||
const mat_base<T, 2>& other)
|
||||
: rows{
|
||||
row_type{other.rows[0], 0, 0},
|
||||
row_type{other.rows[1], 0, 0},
|
||||
row_type{0, 0, 1, 0},
|
||||
row_type{0, 0, 0, 1}} {}
|
||||
row_type{other.rows[0], T{0}, T{0}},
|
||||
row_type{other.rows[1], T{0}, T{0}},
|
||||
row_type{T{0}, T{0}, T{1}, T{0}},
|
||||
row_type{T{0}, T{0}, T{0}, T{1}}} {}
|
||||
|
||||
constexpr explicit mat_base(
|
||||
const mat_base<T, 3>& other)
|
||||
: rows{
|
||||
row_type{other.rows[0], 0},
|
||||
row_type{other.rows[1], 0},
|
||||
row_type{other.rows[2], 0},
|
||||
row_type{0, 0, 0, 1}} {}
|
||||
row_type{other.rows[0], T{0}},
|
||||
row_type{other.rows[1], T{0}},
|
||||
row_type{other.rows[2], T{0}},
|
||||
row_type{T{0}, T{0}, T{0}, T{1}}} {}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -253,14 +256,6 @@ namespace vmath_hpp
|
||||
return &rows[0];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference operator[](std::size_t index) noexcept {
|
||||
return rows[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept {
|
||||
return rows[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference at(std::size_t index) {
|
||||
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at"));
|
||||
return rows[index];
|
||||
@@ -270,6 +265,14 @@ namespace vmath_hpp
|
||||
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("mat::at"));
|
||||
return rows[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference operator[](std::size_t index) noexcept {
|
||||
return rows[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr const_reference operator[](std::size_t index) const noexcept {
|
||||
return rows[index];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,11 @@ namespace vmath_hpp
|
||||
template < typename T >
|
||||
class qua final {
|
||||
public:
|
||||
vec<T, 3> v{0};
|
||||
T s{1};
|
||||
using imag_type = vec<T, 3>;
|
||||
using real_type = T;
|
||||
|
||||
imag_type v = imag_type{T{0}};
|
||||
real_type s = real_type{T{1}};
|
||||
public:
|
||||
using self_type = qua;
|
||||
using component_type = T;
|
||||
@@ -85,6 +88,16 @@ namespace vmath_hpp
|
||||
return &(*this)[0];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference at(std::size_t index) {
|
||||
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
|
||||
return (*this)[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr const_reference at(std::size_t index) const {
|
||||
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
|
||||
return (*this)[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference operator[](std::size_t index) noexcept {
|
||||
switch ( index ) {
|
||||
default:
|
||||
@@ -104,16 +117,6 @@ namespace vmath_hpp
|
||||
case 3: return s;
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference at(std::size_t index) {
|
||||
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
|
||||
return (*this)[index];
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr const_reference at(std::size_t index) const {
|
||||
VMATH_HPP_THROW_IF(index >= size, std::out_of_range("qua::at"));
|
||||
return (*this)[index];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,8 @@ namespace vmath_hpp::detail
|
||||
template < typename T >
|
||||
class vec_base<T, 2> {
|
||||
public:
|
||||
T x{}, y{};
|
||||
T x = T{0};
|
||||
T y = T{0};
|
||||
public:
|
||||
constexpr vec_base() = default;
|
||||
|
||||
@@ -52,7 +53,9 @@ namespace vmath_hpp::detail
|
||||
template < typename T >
|
||||
class vec_base<T, 3> {
|
||||
public:
|
||||
T x{}, y{}, z{};
|
||||
T x = T{0};
|
||||
T y = T{0};
|
||||
T z = T{0};
|
||||
public:
|
||||
constexpr vec_base() = default;
|
||||
|
||||
@@ -93,7 +96,10 @@ namespace vmath_hpp::detail
|
||||
template < typename T >
|
||||
class vec_base<T, 4> {
|
||||
public:
|
||||
T x{}, y{}, z{}, w{};
|
||||
T x = T{0};
|
||||
T y = T{0};
|
||||
T z = T{0};
|
||||
T w = T{0};
|
||||
public:
|
||||
constexpr vec_base() = default;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user