💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
2.8 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP
  20. #define ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP
  21. #include "math/transform-type.hpp"
  22. #include "math/vector-functions.hpp"
  23. #include "math/matrix-functions.hpp"
  24. #include "math/quaternion-functions.hpp"
  25. namespace math {
  26. /// @addtogroup transform
  27. /// @{
  28. /**
  29. * Calculates the inverse of a transform.
  30. *
  31. * @param t Transform of which to take the inverse.
  32. */
  33. template <class T>
  34. transform<T> inverse(const transform<T>& t);
  35. /**
  36. * Converts a transform to a transformation matrix.
  37. *
  38. * @param t Transform.
  39. * @return Matrix representing the transformation described by `t`.
  40. */
  41. template <class T>
  42. matrix<T, 4, 4> matrix_cast(const transform<T>& t);
  43. /**
  44. * Multiplies two transforms.
  45. *
  46. * @param x First transform.
  47. * @param y Second transform.
  48. * @return Product of the two transforms.
  49. */
  50. template <class T>
  51. transform<T> mul(const transform<T>& x, const transform<T>& y);
  52. /**
  53. * Multiplies a vector by a transform.
  54. *
  55. * @param t Transform.
  56. * @param v Vector.
  57. * @return Product of the transform and vector.
  58. */
  59. template <class T>
  60. vector<T, 3> mul(const transform<T>& t, const vector<T, 3>& v);
  61. template <class T>
  62. transform<T> inverse(const transform<T>& t)
  63. {
  64. transform<T> inverse_t;
  65. inverse_t.scale = {T(1) / t.scale.x, T(1) / t.scale.y, T(1) / t.scale.z};
  66. inverse_t.rotation = conjugate(t.rotation);
  67. inverse_t.translation = negate(mul(inverse_t.rotation, t.translation));
  68. return inverse_t;
  69. }
  70. template <class T>
  71. matrix<T, 4, 4> matrix_cast(const transform<T>& t)
  72. {
  73. matrix<T, 4, 4> transformation = resize<4, 4>(matrix_cast(t.rotation));
  74. transformation[3] = {t.translation[0], t.translation[1], t.translation[2], T(1)};
  75. return scale(transformation, t.scale);
  76. }
  77. template <class T>
  78. transform<T> mul(const transform<T>& x, const transform<T>& y)
  79. {
  80. return
  81. {
  82. mul(x, y.translation),
  83. normalize(mul(x.rotation, y.rotation)),
  84. x.scale * y.scale
  85. };
  86. }
  87. template <class T>
  88. vector<T, 3> mul(const transform<T>& t, const vector<T, 3>& v)
  89. {
  90. return t.translation + (t.rotation * (v * t.scale));
  91. }
  92. /// @}
  93. } // namespace math
  94. #endif // ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP