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

136 lines
3.3 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. namespace transformation {
  25. /**
  26. * 3-dimensional Euclidean proper rigid transformation in SE(3).
  27. *
  28. * @tparam T Scalar type.
  29. */
  30. template <class T>
  31. struct se3
  32. {
  33. public:
  34. /// Scalar type.
  35. typedef T scalar_type;
  36. /// Vector type.
  37. typedef math::vec3<T> vector_type;
  38. /// Quaternion type.
  39. typedef math::quaternion<T> quaternion_type;
  40. /// Transformation matrix type.
  41. typedef math::mat4<T> matrix_type;
  42. /// Vector representing the translation component of this SE(3) transformation.
  43. vector_type t;
  44. /// Quaternion representing the rotation component of this SE(3) transformation.
  45. quaternion_type r;
  46. /// Returns the inverse of this SE(3) transformation.
  47. [[nodiscard]] se3 inverse() const;
  48. /// Returns a matrix representation of the SE(3) transformation.
  49. [[nodiscard]] matrix_type matrix() const;
  50. /**
  51. * Transforms a vector by this SE(3) transformation.
  52. *
  53. * @param x Untransformed vector.
  54. * @return Transformed vector.
  55. */
  56. [[nodiscard]] vector_type transform(const vector_type& x) const;
  57. /**
  58. * Transforms an SE(3) transformation by this SE(3) transformation.
  59. *
  60. * @param x Other SE(3) transformation.
  61. * @return Frame in this se3's space.
  62. */
  63. [[nodiscard]] se3 transform(const se3& x) const;
  64. /// @copydoc se3::transform(const vector_type&) const
  65. [[nodiscard]] vector_type operator*(const vector_type& x) const;
  66. /// @copydoc se3::transform(const se3&) const
  67. [[nodiscard]] se3 operator*(const se3& x) const;
  68. };
  69. template <class T>
  70. se3<T> se3<T>::inverse() const
  71. {
  72. const quaternion_type inverse_r = math::conjugate(r);
  73. const vector_type inverse_t = -(inverse_r * t);
  74. return se3{inverse_t, inverse_r};
  75. }
  76. template <class T>
  77. typename se3<T>::matrix_type se3<T>::matrix() const
  78. {
  79. matrix_type m = math::mat4<T>(math::mat3<T>(r));
  80. m[3].x() = t.x();
  81. m[3].y() = t.y();
  82. m[3].z() = t.z();
  83. return m;
  84. }
  85. template <class T>
  86. typename se3<T>::vector_type se3<T>::transform(const vector_type& x) const
  87. {
  88. return r * x + t;
  89. }
  90. template <class T>
  91. se3<T> se3<T>::transform(const se3& x) const
  92. {
  93. return se3
  94. {
  95. x.transform(t),
  96. math::normalize(x.r * r)
  97. };
  98. }
  99. template <class T>
  100. typename se3<T>::vector_type se3<T>::operator*(const vector_type& x) const
  101. {
  102. return transform(x);
  103. }
  104. template <class T>
  105. se3<T> se3<T>::operator*(const se3& x) const
  106. {
  107. return transform(x);
  108. }
  109. } // namespace transformation
  110. } // namespace math
  111. #endif // ANTKEEPER_MATH_TRANSFORMATION_SE3_HPP