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

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