💿🐜 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.

103 lines
2.8 KiB

  1. /*
  2. * Copyright (C) 2023 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/quaternion.hpp"
  23. namespace math {
  24. /**
  25. * Calculates the inverse of a transform.
  26. *
  27. * @param t Transform of which to take the inverse.
  28. */
  29. template <class T>
  30. [[nodiscard]] transform<T> inverse(const transform<T>& t);
  31. /**
  32. * Converts a transform to a transformation matrix.
  33. *
  34. * @param t Transform.
  35. * @return Matrix representing the transformation described by `t`.
  36. */
  37. template <class T>
  38. [[nodiscard]] matrix<T, 4, 4> matrix_cast(const transform<T>& t);
  39. /**
  40. * Multiplies two transforms.
  41. *
  42. * @param x First transform.
  43. * @param y Second transform.
  44. * @return Product of the two transforms.
  45. */
  46. template <class T>
  47. [[nodiscard]] transform<T> mul(const transform<T>& x, const transform<T>& y);
  48. /**
  49. * Multiplies a vector by a transform.
  50. *
  51. * @param t Transform.
  52. * @param v Vector.
  53. * @return Product of the transform and vector.
  54. */
  55. template <class T>
  56. [[nodiscard]] vector<T, 3> mul(const transform<T>& t, const vector<T, 3>& v);
  57. template <class T>
  58. transform<T> inverse(const transform<T>& t)
  59. {
  60. transform<T> inverse_t;
  61. inverse_t.scale = {T(1) / t.scale.x(), T(1) / t.scale.y(), T(1) / t.scale.z()};
  62. inverse_t.rotation = conjugate(t.rotation);
  63. inverse_t.translation = negate(mul(inverse_t.rotation, t.translation));
  64. return inverse_t;
  65. }
  66. template <class T>
  67. matrix<T, 4, 4> matrix_cast(const transform<T>& t)
  68. {
  69. matrix<T, 4, 4> transformation = matrix<T, 4, 4>(matrix<T, 3, 3>(t.rotation));
  70. transformation[3] = {t.translation[0], t.translation[1], t.translation[2], T(1)};
  71. return scale(transformation, t.scale);
  72. }
  73. template <class T>
  74. transform<T> mul(const transform<T>& x, const transform<T>& y)
  75. {
  76. return
  77. {
  78. mul(x, y.translation),
  79. normalize(mul(x.rotation, y.rotation)),
  80. mul(x.scale, y.scale)
  81. };
  82. }
  83. template <class T>
  84. vector<T, 3> mul(const transform<T>& t, const vector<T, 3>& v)
  85. {
  86. return add(t.translation, (mul(t.rotation, mul(v, t.scale))));
  87. }
  88. } // namespace math
  89. #endif // ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP