our_dick/include/mat.tpp

158 lines
5.2 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_TPP
#define REXY_MAT_TPP
#include <cstdlib> //size_t
#include <cmath> //sin, cos
#include <type_traits> //decay_t, declval
namespace math{
template<typename T>
matrix<T,3,3> matrix<T,3,3>::rotation(value_type angle){
value_type c = std::cos(angle);
value_type s = std::sin(angle);
return rotation(s, c);
}
template<typename T>
constexpr matrix<T,3,3> matrix<T,3,3>::rotation(value_type sin, value_type cos){
return matrix(cos, -sin, 0,
sin, cos, 0,
0, 0, 1);
}
template<typename T>
constexpr matrix<T,3,3> matrix<T,3,3>::rotation(value_type angle_x, value_type angle_y, value_type angle_z){
//TODO
}
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))
return false;
}
return true;
}
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<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);
size_t index = 0;
for(size_t i = 0;i < right.rows();++i){
for(size_t j = 0;j < left.rows();++j){
for(size_t k = 0;k < left.columns();++k){
res.get(index) += left[j][k] * right[i][k];
}
++index;
}
}
return res;
}
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);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) * std::forward<U>(right);
}
return res;
}
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);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) / std::forward<U>(right);
}
return res;
}
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);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) + right.get(i);
}
return res;
}
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);
for(size_t i = 0;i < left.size();++i){
res.get(i) = left.get(i) - right.get(i);
}
return res;
}
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);
for(size_t i = 0;i < left.size();++i){
res.get(i) = -left.get(i);
}
return res;
}
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<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<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<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<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;
}
}
#endif