our_dick/include/mat.hpp
2020-08-16 06:47:52 -07:00

260 lines
8.6 KiB
C++

/**
This file is a part of the rexy/r0nk/atlas project
Copyright (C) 2020 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef REXY_MAT_HPP
#define REXY_MAT_HPP
#include <cstdlib> //size_t
#include <utility> //integer_sequence
#include <type_traits> //decay_t, is_same, integral_constant
#include "detail/math.hpp"
namespace math{
template<typename T, size_t W, size_t H>
class matrix_base
{
static_assert(W > 0, "Cannot have 0 columns matrix");
static_assert(H > 0, "Cannot have 0 rows matrix");
public:
using value_type = T;
using size_type = size_t;
using pointer = value_type*;
using const_pointer = const value_type*;
using reference = value_type&;
using const_reference = const value_type&;
static constexpr size_type Columns = W;
static constexpr size_type Rows = H;
protected:
value_type m_data[W*H];
protected:
template<size_t... Ss>
constexpr matrix_base(std::integer_sequence<size_type,Ss...>);
public:
//Default construct as identity when square, zero otherwise
constexpr matrix_base();
//Range initializing constructors
constexpr explicit matrix_base(detail::zero_initialize_t);
constexpr explicit matrix_base(detail::no_initialize_t);
//Value initializing constructors
constexpr explicit matrix_base(value_type v);
template<typename... Args>
constexpr explicit matrix_base(Args&&... args);
//Copying constructors
constexpr matrix_base(const matrix_base&) = default;
constexpr matrix_base(matrix_base&&) = default;
template<typename U>
constexpr matrix_base(const matrix_base<U,Columns,Rows>& m);
~matrix_base() = default;
constexpr matrix_base& operator=(const matrix_base&) = default;
constexpr matrix_base& operator=(matrix_base&&) = default;
template<typename U>
constexpr matrix_base& operator=(const matrix_base<U,Columns,Rows>& m);
//Getters/Setters
constexpr auto operator[](size_type x);
constexpr auto operator[](size_type x)const;
constexpr reference get(size_type x, size_type y);
constexpr const_reference get(size_type x, size_type y)const;
constexpr reference get(size_type i);
constexpr const_reference get(size_type i)const;
constexpr size_type columns()const;
constexpr size_type rows()const;
constexpr size_type size()const;
constexpr pointer raw();
constexpr const_pointer raw()const;
constexpr operator pointer();
constexpr operator const_pointer()const;
};
template<typename T, size_t W, size_t H>
class matrix : public matrix_base<T,W,H>
{
private:
using base = matrix_base<T,W,H>;
public:
using value_type = typename base::value_type;
using size_type = typename base::size_type;
using pointer = typename base::pointer;
using const_pointer = typename base::const_pointer;
using reference = typename base::reference;
using const_reference = typename base::const_reference;
public:
using base::base;
constexpr matrix(const matrix&) = default;
constexpr matrix(matrix&&) = default;
~matrix() = default;
//Assignement
constexpr matrix& operator=(const matrix&) = default;
constexpr matrix& operator=(matrix&&) = default;
template<typename U>
constexpr matrix& operator=(const matrix<U,W,H>& m);
};
template<typename T, size_t W>
class matrix<T,W,W> : public matrix_base<T,W,W>
{
private:
using base = matrix_base<T,W,W>;
public:
using value_type = typename base::value_type;
using size_type = typename base::size_type;
using pointer = typename base::pointer;
using const_pointer = typename base::const_pointer;
using reference = typename base::reference;
using const_reference = typename base::const_reference;
public:
using base::base;
constexpr matrix(const matrix&) = default;
constexpr matrix(matrix&&) = default;
constexpr explicit matrix(detail::id_initialize_t);
~matrix() = default;
//Assignement
constexpr matrix& operator=(const matrix&) = default;
constexpr matrix& operator=(matrix&&) = default;
template<typename U>
constexpr matrix& operator=(const matrix<U,W,W>& m);
//square matrix arithmetic operations
constexpr value_type determinate()const;
constexpr value_type trace()const;
constexpr matrix transpose()const;
constexpr matrix inverse()const;
};
template<typename T, size_t R>
constexpr T determinate(const matrix<T,R,R>& m);
template<typename T, size_t R>
constexpr matrix<T,R,R> inverse(const matrix<T,R,R>& m);
template<typename T>
matrix<T,2,2> rotation2d_pure(T angle);
template<typename T>
constexpr matrix<T,2,2> rotation2d_pure(T sin, T cos);
template<typename T>
constexpr matrix<T,2,2> scale2d(T x, T y);
template<typename T>
matrix<T,3,3> rotation2d(T angle);
template<typename T>
constexpr matrix<T,3,3> rotation2d(T sin, T cos);
template<typename T>
matrix<T,3,3> rotation2d(T x, T y, T z);
template<typename T>
matrix<T,4,4> fov_projection(T fov, T asp, T near, T far);
template<typename T>
matrix<T,4,4> fov_asymetric_projection(T fovl, T fovr, T fovb, T fovt, T asp, T near, T far);
template<typename T>
matrix<T,4,4> ortho_projection(T w, T h, T n, T f);
template<typename T>
matrix<T,4,4> ortho_asymetric_projection(T l, T r, T b, T t, T n, T f);
template<typename T>
constexpr matrix<T,4,4> rotation3d(T angle_x, T angle_y, T angle_z);
template<typename T>
constexpr matrix<T,4,4> translation3d(T x, T y, T z);
template<typename T>
constexpr matrix<T,4,4> scale3d(T x, T y, T z);
namespace detail{
template<typename T>
struct is_matrix_helper {
template<typename U, size_t W, size_t H>
static std::true_type test(matrix<U,W,H>*);
static std::false_type test(void*);
static constexpr bool value = std::is_same<std::true_type,decltype(test(static_cast<std::decay_t<T>*>(nullptr)))>::value;
};
}
template<typename... Ms>
struct is_matrix {
static constexpr bool value = (detail::is_matrix_helper<Ms>::value && ...);
};
namespace detail{
template<typename M1, typename M2>
struct are_same_size_matrix {
using l = std::decay_t<M1>;
using r = std::decay_t<M2>;
static constexpr bool value = is_matrix<M1,M2>::value && l::Columns == r::Columns && l::Rows == r::Rows;
};
template<typename... Ms>
using enable_if_matrix = std::enable_if_t<is_matrix<Ms...>::value,int>;
template<typename M1, typename M2>
using enable_if_eq_matrix = std::enable_if_t<are_same_size_matrix<M1,M2>::value,int>;
}
template<typename T, typename U, size_t W, size_t H>
constexpr bool operator==(const matrix<T,W,H>& left, const matrix<U,W,H> right);
template<typename T, typename U, size_t W, size_t H>
constexpr bool operator!=(const matrix<T,W,H>& left, const matrix<U,W,H> right);
template<typename T, typename U, size_t R1, size_t C1, size_t C2>
constexpr auto operator*(const matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right);
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator*(const matrix<T,C,R>& left, U&& right);
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator*(U&& left, const matrix<T,C,R>& right);
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator/(const matrix<T,C,R>& left, U&& right);
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator+(const matrix<T,C,R>& left, const matrix<U,C,R>& right);
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator-(const matrix<T,C,R>& left, const matrix<U,C,R>& right);
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator-(const matrix<T,C,R>& left);
template<typename T, typename U, size_t R1, size_t C1, size_t C2>
constexpr decltype(auto) operator*=(matrix<T,C1,R1>& left, const matrix<U,C2,C1>& right);
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator*=(matrix<T,C,R>& left, U&& right);
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator/=(matrix<T,C,R>& left, U&& right);
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator+=(matrix<T,C,R>& left, const matrix<U,C,R>& right);
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator-=(matrix<T,C,R>& left, const matrix<U,C,R>& right);
}
#include "mat.tpp"
#endif