Fix mat warnings and add debug printout functions
This commit is contained in:
72
include/math/debug.hpp
Normal file
72
include/math/debug.hpp
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
This file is a part of our_dick
|
||||
Copyright (C) 2020 rexy712
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Affero General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef OUR_DICK_MATH_DEBUG_HPP
|
||||
#define OUR_DICK_MATH_DEBUG_HPP
|
||||
|
||||
#include <cstdio> //printf
|
||||
#include <cstdlib> //size_t
|
||||
|
||||
#include "quat.hpp"
|
||||
#include "mat.hpp"
|
||||
|
||||
namespace math{
|
||||
namespace detail{
|
||||
static inline void print_integral(int i){
|
||||
printf("%d", i);
|
||||
}
|
||||
static inline void print_integral(float f){
|
||||
printf("%f", f);
|
||||
}
|
||||
static inline void print_double(double d){
|
||||
printf("%lf", d);
|
||||
}
|
||||
static inline void print_integral(unsigned int i){
|
||||
printf("%u", i);
|
||||
}
|
||||
static inline void print_integral(long int i){
|
||||
printf("%li", i);
|
||||
}
|
||||
static inline void print_integral(unsigned long int i){
|
||||
printf("%lu", i);
|
||||
}
|
||||
}
|
||||
|
||||
//Debug
|
||||
template<typename T, size_t R, size_t C>
|
||||
void dump_matrix(const matrix_base<T,R,C>& mat){
|
||||
for(size_t i = 0;i < R;++i){
|
||||
for(size_t j = 0;j < C;++j){
|
||||
detail::print_integral(mat[i][j]);
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
template<typename T>
|
||||
void dump_quaternion(const quaternion<T>& q){
|
||||
for(size_t i = 0;i < 4;++i){
|
||||
detail::print_integral(q[i]);
|
||||
printf(" ");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <type_traits> //decay_t, is_same, integral_constant
|
||||
#include "math_common.hpp"
|
||||
#include "fwd_declare.hpp"
|
||||
#include <cstdio>
|
||||
|
||||
namespace math{
|
||||
|
||||
@@ -222,7 +223,6 @@ namespace math{
|
||||
|
||||
template<typename M1, typename M2>
|
||||
using enable_if_eq_matrix = std::enable_if_t<are_same_size_matrix<M1,M2>::value,int>;
|
||||
|
||||
}
|
||||
|
||||
//Logic operators
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace math{
|
||||
template<typename T, size_t R, size_t C>
|
||||
template<typename U>
|
||||
constexpr matrix_base<T,R,C>::matrix_base(const matrix_base<U,Columns,Rows>& m){
|
||||
using mat = decltype(m);
|
||||
using mat = matrix_base<U,Columns,Rows>;
|
||||
for(typename mat::size_type i = 0; i < mat::Columns*mat::Rows; ++i)
|
||||
m_data[i] = m.get(i);
|
||||
}
|
||||
@@ -63,7 +63,7 @@ namespace math{
|
||||
template<typename T, size_t R, size_t C>
|
||||
template<typename U>
|
||||
constexpr matrix_base<T,R,C>& matrix_base<T,R,C>::operator=(const matrix_base<U,Columns,Rows>& m){
|
||||
using mat = decltype(m);
|
||||
using mat = matrix_base<U,Columns,Rows>;
|
||||
for(typename mat::size_type i = 0; i < mat::Columns*mat::Rows; ++i)
|
||||
m_data[i] = m.get(i);
|
||||
return *this;
|
||||
@@ -188,7 +188,7 @@ namespace math{
|
||||
}
|
||||
template<typename T>
|
||||
constexpr matrix<T,2,2> scale2d(T x, T y){
|
||||
return matrix<T,2,2>(x, 0, 0, y);
|
||||
return matrix<T,2,2>(x, T{0}, T{0}, y);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
@@ -197,9 +197,9 @@ namespace math{
|
||||
}
|
||||
template<typename T>
|
||||
constexpr matrix<T,3,3> rotation2d(T sin, T cos){
|
||||
return matrix<T,3,3>(cos, -sin, 0,
|
||||
sin, cos, 0,
|
||||
0, 0, 1);
|
||||
return matrix<T,3,3>(cos, -sin, T{0},
|
||||
sin, cos, T{0},
|
||||
T{0}, T{0}, T{1});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,3,3> rotation2d(T x, T y, T z){
|
||||
@@ -210,10 +210,10 @@ namespace math{
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_projection(T fov, T asp, T near, T far){
|
||||
T r = near * std::tan(fov / T{2.0});
|
||||
return matrix<T,4,4>((near / r) / asp, 0, 0, 0,
|
||||
0, (near / r), 0, 0,
|
||||
0, 0, (far + near) / (near - far), -1,
|
||||
0, 0, (2 * near * far) / (near - far), 0);
|
||||
return matrix<T,4,4>((near / r) / asp, T{0}, T{0}, T{0},
|
||||
T{0}, (near / r), T{0}, T{0},
|
||||
T{0}, T{0}, (far + near) / (near - far), -T{1},
|
||||
T{0}, T{0}, (T{2} * near * far) / (near - far), T{0});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> fov_asymetric_projection(T fovl, T fovr, T fovb, T fovt, T asp, T n, T f){
|
||||
@@ -222,24 +222,24 @@ namespace math{
|
||||
T b = n * std::tan(fovb);
|
||||
T t = n * std::tan(fovt);
|
||||
|
||||
return matrix<T,4,4>(((2 * n) / (r - l)) * asp, 0, 0, 0,
|
||||
0, (2 * n) / (t - b), 0, 0,
|
||||
(r + l) / (r - l), (t + b) / (t - b), (f + n) / (n - f), -1,
|
||||
0, 0, (2 * n * f) / (n - f), 0);
|
||||
return matrix<T,4,4>(((T{2} * n) / (r - l)) * asp, T{0}, T{0}, T{0},
|
||||
T{0}, (T{2} * n) / (t - b), T{0}, T{0},
|
||||
(r + l) / (r - l), (t + b) / (t - b), (f + n) / (n - f), -T{1},
|
||||
T{0}, T{0}, (T{2} * n * f) / (n - f), T{0});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_projection(T w, T h, T n, T f){
|
||||
return matrix<T,4,4>(2 / w, 0, 0, 0,
|
||||
0, 2 / h, 0, 0,
|
||||
0, 0, 2 / (n - f), 0,
|
||||
0, 0, (n + f) / (n - f), 1);
|
||||
return matrix<T,4,4>(T{2} / w, T{0}, T{0}, T{0},
|
||||
T{0}, T{2} / h, T{0}, T{0},
|
||||
T{0}, T{0}, T{2} / (n - f), T{0},
|
||||
T{0}, T{0}, (n + f) / (n - f), T{1});
|
||||
}
|
||||
template<typename T>
|
||||
matrix<T,4,4> ortho_asymetric_projection(T l, T r, T b, T t, T n, T f){
|
||||
return matrix<T,4,4>(2 / (r - l), 0, 0, 0,
|
||||
0, 2 / (t - b), 0, 0,
|
||||
0, 0, 2 / (n - f), 0,
|
||||
(r + l) / (l - r), (t + b) / (b - t), (n + f) / (n - f), 1);
|
||||
return matrix<T,4,4>(T{2} / (r - l), T{0}, T{0}, T{0},
|
||||
T{0}, T{2} / (t - b), T{0}, T{0},
|
||||
T{0}, T{0}, T{2} / (n - f), T{0},
|
||||
(r + l) / (l - r), (t + b) / (b - t), (n + f) / (n - f), T{1});
|
||||
}
|
||||
template<typename T>
|
||||
constexpr matrix<T,4,4> rotation3d(T angle_x, T angle_y, T angle_z){
|
||||
@@ -248,17 +248,17 @@ namespace math{
|
||||
}
|
||||
template<typename T>
|
||||
constexpr matrix<T,4,4> translation3d(T x, T y, T z){
|
||||
return matrix<T,4,4>(1, 0, 0, 0,
|
||||
0, 1, 0, 0,
|
||||
0, 0, 1, 0,
|
||||
x, y, z, 1);
|
||||
return matrix<T,4,4>(T{1}, T{0}, T{0}, T{0},
|
||||
T{0}, T{1}, T{0}, T{0},
|
||||
T{0}, T{0}, T{1}, T{0},
|
||||
x, y, z, T{1});
|
||||
}
|
||||
template<typename T>
|
||||
constexpr matrix<T,4,4> scale3d(T x, T y, T z){
|
||||
return matrix<T,4,4>(x, 0, 0, 0,
|
||||
0, y, 0, 0,
|
||||
0, 0, z, 0,
|
||||
0, 0, 0, 1);
|
||||
return matrix<T,4,4>(x, T{0}, T{0}, T{0},
|
||||
T{0}, y, T{0}, T{0},
|
||||
T{0}, T{0}, z, T{0},
|
||||
T{0}, T{0}, T{0}, T{1});
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user