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

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