Fix standard compliance with matrix class

This commit is contained in:
rexy712 2020-08-15 14:04:08 -07:00
parent def1740b7d
commit bbcdd15a11
3 changed files with 86 additions and 50 deletions

View File

@ -1,3 +1,21 @@
/**
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_DETAIL_MATH_HPP
#define REXY_DETAIL_MATH_HPP

View File

@ -1,3 +1,21 @@
/**
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_DETAIL_MATRIX_HPP
#define REXY_DETAIL_MATRIX_HPP
@ -48,7 +66,7 @@ namespace math::detail{
using tuple = typename id_initialization_matrix<W>::tuple;
};
template<class T, size_t R>
template<typename T, size_t R>
class mat_ref_obj
{
public:
@ -68,7 +86,7 @@ namespace math::detail{
}
};
template<class T, size_t W, size_t H>
template<typename T, size_t W, size_t H>
class matrix_base
{
static_assert(W > 0, "Cannot have 0 columns matrix");
@ -94,14 +112,14 @@ namespace math::detail{
public:
//Default construct as identity when square, zero otherwise
constexpr matrix_base(void):
constexpr matrix_base():
matrix_base(typename detail::default_initialization_matrix<Columns,Rows>::tuple{}){}
//Range initializing constructors
constexpr explicit matrix_base(zero_initialize_t):
m_data{}{}
constexpr explicit matrix_base(no_initialize_t){}
template<class U = void>
template<typename U = void>
constexpr explicit matrix_base(id_initialize_t):
matrix_base()
{
@ -113,23 +131,23 @@ namespace math::detail{
for(size_type i = 0;i < Columns*Rows;++i)
m_data[i] = v;
}
template<class... Args>
template<typename... Args>
constexpr explicit matrix_base(Args&&... args):
m_data{std::forward<Args>(args)...}{}
//Copying constructors
constexpr matrix_base(const matrix_base&) = default;
constexpr matrix_base(matrix_base&&) = default;
template<class U>
template<typename U>
constexpr matrix_base(const matrix_base<U,Columns,Rows>& m){
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
m_data[i] = m.get(i);
}
~matrix_base(void) = default;
~matrix_base() = default;
//Assignement
template<class U>
template<typename U>
constexpr matrix_base& operator=(const matrix_base<U,Columns,Rows>& m){
using mat = decltype(m);
for(typename mat::size_type i = 0;i < mat::Columns*mat::Rows;++i)
@ -160,26 +178,26 @@ namespace math::detail{
return m_data[i];
}
constexpr size_type columns(void)const{
constexpr size_type columns()const{
return Columns;
}
constexpr size_type rows(void)const{
constexpr size_type rows()const{
return Rows;
}
constexpr size_type size(void)const{
constexpr size_type size()const{
return Columns*Rows;
}
constexpr pointer raw(void){
constexpr pointer raw(){
return m_data;
}
constexpr const_pointer raw(void)const{
constexpr const_pointer raw()const{
return m_data;
}
constexpr operator pointer(void){
constexpr operator pointer(){
return m_data;
}
constexpr operator const_pointer(void)const{
constexpr operator const_pointer()const{
return m_data;
}

View File

@ -28,7 +28,7 @@
namespace math{
template<class T, size_t C, size_t R>
template<typename T, size_t C, size_t R>
class matrix : public detail::matrix_base<T,C,R>
{
private:
@ -45,7 +45,7 @@ namespace math{
using detail::matrix_base<T,C,R>::operator=;
};
template<class T>
template<typename T>
class matrix<T,3,3> : public detail::matrix_base<T,3,3>
{
private:
@ -61,13 +61,13 @@ namespace math{
using detail::matrix_base<T,3,3>::matrix_base;
using detail::matrix_base<T,3,3>::operator=;
template<class U = void>
template<typename U = void>
static constexpr matrix rotation(value_type angle){
value_type c = std::cos(angle);
value_type s = std::sin(angle);
return rotation(s, c);
}
template<class U = void>
template<typename U = void>
static constexpr matrix rotation(value_type sin, value_type cos){
return matrix(cos, -sin, 0,
sin, cos, 0,
@ -80,9 +80,9 @@ namespace math{
namespace detail{
template<class T>
template<typename T>
struct is_matrix_helper {
template<class U, size_t W, size_t H>
template<typename U, size_t W, size_t H>
static std::true_type test(matrix<U,W,H>*);
static std::false_type test(void*);
@ -91,28 +91,28 @@ namespace math{
}
template<class... Ms>
template<typename... Ms>
struct is_matrix {
static constexpr bool value = (detail::is_matrix_helper<Ms>::value && ...);
};
namespace detail{
template<class M1, class M2>
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<class... Ms>
template<typename... Ms>
using enable_if_matrix = std::enable_if_t<is_matrix<Ms...>::value,int>;
template<class M1, class M2>
template<typename M1, typename M2>
using enable_if_eq_matrix = std::enable_if_t<are_same_size_matrix<M1,M2>::value,int>;
}
template<class T, class U, size_t W, size_t H>
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){
for(size_t i = 0;i < left.size();++i){
if(left.get(i) != right.get(i))
@ -120,12 +120,12 @@ namespace math{
}
return true;
}
template<class T, class U, size_t W, size_t H>
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){
return !(left == right);
}
template<class T, class U, size_t R1, size_t C1, size_t C2>
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){
using res_t = decltype(std::declval<T>() * std::declval<U>());
matrix<res_t,C2,R1> res(no_initialize);
@ -140,7 +140,7 @@ namespace math{
}
return res;
}
template<class T, class U, size_t C, size_t R>
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator*(const matrix<T,C,R>& left, U&& right){
using res_t = decltype(std::declval<T>() * std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
@ -149,7 +149,7 @@ namespace math{
}
return res;
}
template<class T, class U, size_t C, size_t R>
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator/(const matrix<T,C,R>& left, U&& right){
using res_t = decltype(std::declval<T>() / std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
@ -158,7 +158,7 @@ namespace math{
}
return res;
}
template<class T, class U, size_t C, size_t R>
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){
using res_t = decltype(std::declval<T>() + std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
@ -167,7 +167,7 @@ namespace math{
}
return res;
}
template<class T, class U, size_t C, size_t R>
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){
using res_t = decltype(std::declval<T>() - std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
@ -176,7 +176,7 @@ namespace math{
}
return res;
}
template<class T, class U, size_t C, size_t R>
template<typename T, typename U, size_t C, size_t R>
constexpr auto operator-(const matrix<T,C,R>& left){
using res_t = decltype(std::declval<T>() - std::declval<U>());
matrix<res_t,C,R> res(no_initialize);
@ -187,34 +187,34 @@ namespace math{
}
template<class T, class U, size_t R1, size_t C1, size_t C2>
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){
//have to evaluate entire expression first since matrix multiplication depends on reusing many elements
//cannot be expression templatized, TODO
return (left = (left * right));
}
template<class T, class U, size_t C, size_t R>
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator*=(matrix<T,C,R>& left, U&& right){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) * std::forward<U>(right);
}
return left;
}
template<class T, class U, size_t C, size_t R>
template<typename T, typename U, size_t C, size_t R>
constexpr decltype(auto) operator/=(matrix<T,C,R>& left, U&& right){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) / std::forward<U>(right);
}
return left;
}
template<class T, class U, size_t C, size_t R>
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){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) + right.get(i);
}
return left;
}
template<class T, class U, size_t C, size_t R>
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){
for(size_t i = 0;i < left.size();++i){
left.get(i) = left.get(i) - right.get(i);