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

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