/* * Copyright (C) 2021 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code 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. * * Antkeeper source code 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 Antkeeper source code. If not, see . */ #ifndef ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP #define ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP #include "math/transform-type.hpp" #include "math/quaternion.hpp" namespace math { /** * Calculates the inverse of a transform. * * @param t Transform of which to take the inverse. */ template transform inverse(const transform& t); /** * Converts a transform to a transformation matrix. * * @param t Transform. * @return Matrix representing the transformation described by `t`. */ template matrix matrix_cast(const transform& t); /** * Multiplies two transforms. * * @param x First transform. * @param y Second transform. * @return Product of the two transforms. */ template transform mul(const transform& x, const transform& y); /** * Multiplies a vector by a transform. * * @param t Transform. * @param v Vector. * @return Product of the transform and vector. */ template vector mul(const transform& t, const vector& v); template transform inverse(const transform& t) { transform inverse_t; inverse_t.scale = {T(1) / t.scale.x(), T(1) / t.scale.y(), T(1) / t.scale.z()}; inverse_t.rotation = conjugate(t.rotation); inverse_t.translation = negate(mul(inverse_t.rotation, t.translation)); return inverse_t; } template matrix matrix_cast(const transform& t) { matrix transformation = matrix(matrix(t.rotation)); transformation[3] = {t.translation[0], t.translation[1], t.translation[2], T(1)}; return scale(transformation, t.scale); } template transform mul(const transform& x, const transform& y) { return { mul(x, y.translation), normalize(mul(x.rotation, y.rotation)), mul(x.scale, y.scale) }; } template vector mul(const transform& t, const vector& v) { return add(t.translation, (mul(t.rotation, mul(v, t.scale)))); } } // namespace math #endif // ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP