From 1f6de5ba70ff0bb81611cc8ecb08deccdcdef5da Mon Sep 17 00:00:00 2001 From: rexy712 Date: Sat, 26 Sep 2020 09:50:58 -0700 Subject: [PATCH] Fix mat warnings and add debug printout functions --- include/math/debug.hpp | 72 ++++++++++++++++++++++++++++++++++++++++++ include/math/mat.hpp | 2 +- include/math/mat.tpp | 60 +++++++++++++++++------------------ 3 files changed, 103 insertions(+), 31 deletions(-) create mode 100644 include/math/debug.hpp diff --git a/include/math/debug.hpp b/include/math/debug.hpp new file mode 100644 index 0000000..19651f4 --- /dev/null +++ b/include/math/debug.hpp @@ -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 . +*/ + +#ifndef OUR_DICK_MATH_DEBUG_HPP +#define OUR_DICK_MATH_DEBUG_HPP + +#include //printf +#include //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 + void dump_matrix(const matrix_base& 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 + void dump_quaternion(const quaternion& q){ + for(size_t i = 0;i < 4;++i){ + detail::print_integral(q[i]); + printf(" "); + } + printf("\n"); + } +} + +#endif diff --git a/include/math/mat.hpp b/include/math/mat.hpp index a2459dd..635604c 100644 --- a/include/math/mat.hpp +++ b/include/math/mat.hpp @@ -24,6 +24,7 @@ #include //decay_t, is_same, integral_constant #include "math_common.hpp" #include "fwd_declare.hpp" +#include namespace math{ @@ -222,7 +223,6 @@ namespace math{ template using enable_if_eq_matrix = std::enable_if_t::value,int>; - } //Logic operators diff --git a/include/math/mat.tpp b/include/math/mat.tpp index 30ffb2e..88b93cf 100644 --- a/include/math/mat.tpp +++ b/include/math/mat.tpp @@ -55,7 +55,7 @@ namespace math{ template template constexpr matrix_base::matrix_base(const matrix_base& m){ - using mat = decltype(m); + using mat = matrix_base; 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 template constexpr matrix_base& matrix_base::operator=(const matrix_base& m){ - using mat = decltype(m); + using mat = matrix_base; 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 constexpr matrix scale2d(T x, T y){ - return matrix(x, 0, 0, y); + return matrix(x, T{0}, T{0}, y); } template @@ -197,9 +197,9 @@ namespace math{ } template constexpr matrix rotation2d(T sin, T cos){ - return matrix(cos, -sin, 0, - sin, cos, 0, - 0, 0, 1); + return matrix(cos, -sin, T{0}, + sin, cos, T{0}, + T{0}, T{0}, T{1}); } template matrix rotation2d(T x, T y, T z){ @@ -210,10 +210,10 @@ namespace math{ template matrix fov_projection(T fov, T asp, T near, T far){ T r = near * std::tan(fov / T{2.0}); - return matrix((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((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 matrix 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(((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{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 matrix ortho_projection(T w, T h, T n, T f){ - return matrix(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{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 matrix ortho_asymetric_projection(T l, T r, T b, T t, T n, T f){ - return matrix(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{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 constexpr matrix rotation3d(T angle_x, T angle_y, T angle_z){ @@ -248,17 +248,17 @@ namespace math{ } template constexpr matrix translation3d(T x, T y, T z){ - return matrix(1, 0, 0, 0, - 0, 1, 0, 0, - 0, 0, 1, 0, - x, y, z, 1); + return matrix(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 constexpr matrix scale3d(T x, T y, T z){ - return matrix(x, 0, 0, 0, - 0, y, 0, 0, - 0, 0, z, 0, - 0, 0, 0, 1); + return matrix(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}); }