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

142 lines
3.4 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_TRANSFORMATION_SE3_HPP
  20. #define ANTKEEPER_MATH_TRANSFORMATION_SE3_HPP
  21. #include <engine/math/vector.hpp>
  22. #include <engine/math/quaternion.hpp>
  23. namespace math {
  24. /**
  25. * SE(3) proper rigid transformation (rototranslation).
  26. *
  27. * @tparam T Scalar type.
  28. */
  29. template <class T>
  30. struct se3
  31. {
  32. public:
  33. /// Scalar type.
  34. using scalar_type = T;
  35. /// Vector type.
  36. using vector_type = vec3<T>;
  37. /// Quaternion type.
  38. using quaternion_type = quat<T>;
  39. /// Transformation matrix type.
  40. using matrix_type = mat4<T>;
  41. /// Vector representing the translation component of the transformation.
  42. vector_type t;
  43. /// Quaternion representing the rotation component of the transformation.
  44. quaternion_type r;
  45. /// Returns the inverse of this transformation.
  46. [[nodiscard]] constexpr se3 inverse() const noexcept
  47. {
  48. const quaternion_type inverse_r = conjugate(r);
  49. const vector_type inverse_t = -(inverse_r * t);
  50. return {inverse_t, inverse_r};
  51. }
  52. /// Returns a matrix representation of this transformation.
  53. /// @{
  54. [[nodiscard]] constexpr matrix_type matrix() const noexcept
  55. {
  56. matrix_type m = mat4<T>(mat3<T>(r));
  57. m[3].x() = t.x();
  58. m[3].y() = t.y();
  59. m[3].z() = t.z();
  60. return m;
  61. }
  62. [[nodiscard]] inline constexpr explicit operator matrix_type() const noexcept
  63. {
  64. return matrix();
  65. }
  66. /// @}
  67. /**
  68. * Transforms a vector by this transformation.
  69. *
  70. * @param v Untransformed vector.
  71. *
  72. * @return Transformed vector.
  73. */
  74. /// @{
  75. [[nodiscard]] inline constexpr vector_type transform(const vector_type& v) const noexcept
  76. {
  77. return r * v + t;
  78. }
  79. [[nodiscard]] inline constexpr vector_type operator*(const vector_type& v) const noexcept
  80. {
  81. return transform(v);
  82. }
  83. /// @}
  84. /**
  85. * Transforms an SE(3) transformation by this transformation.
  86. *
  87. * @param xf SE(3) transformation.
  88. *
  89. * @return Frame in this se3's space.
  90. */
  91. /// @{
  92. [[nodiscard]] constexpr se3 transform(const se3& xf) const noexcept
  93. {
  94. return {xf.transform(t), normalize(xf.r * r)};
  95. }
  96. [[nodiscard]] inline constexpr se3 operator*(const se3& xf) const noexcept
  97. {
  98. return transform(xf);
  99. }
  100. /// @}
  101. /*
  102. * Type-casts the transform scalars using `static_cast`.
  103. *
  104. * @tparam U Target scalar type.
  105. *
  106. * @return Type-casted transform.
  107. */
  108. template <class U>
  109. [[nodiscard]] inline constexpr explicit operator se3<U>() const noexcept
  110. {
  111. return {vec3<U>(t), quat<U>(r)};
  112. }
  113. /// Returns an identity transformation.
  114. [[nodiscard]] static inline constexpr se3 identity() noexcept
  115. {
  116. return {vector_type::zero(), quaternion_type::identity()};
  117. }
  118. };
  119. } // namespace math
  120. #endif // ANTKEEPER_MATH_TRANSFORMATION_SE3_HPP