so3.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #ifndef FAST_GICP_SO3_HPP
  2. #define FAST_GICP_SO3_HPP
  3. #include <Eigen/Core>
  4. #include <Eigen/Geometry>
  5. namespace fast_gicp {
  6. inline Eigen::Matrix3f skew(const Eigen::Vector3f& x) {
  7. Eigen::Matrix3f skew = Eigen::Matrix3f::Zero();
  8. skew(0, 1) = -x[2];
  9. skew(0, 2) = x[1];
  10. skew(1, 0) = x[2];
  11. skew(1, 2) = -x[0];
  12. skew(2, 0) = -x[1];
  13. skew(2, 1) = x[0];
  14. return skew;
  15. }
  16. inline Eigen::Matrix3d skewd(const Eigen::Vector3d& x) {
  17. Eigen::Matrix3d skew = Eigen::Matrix3d::Zero();
  18. skew(0, 1) = -x[2];
  19. skew(0, 2) = x[1];
  20. skew(1, 0) = x[2];
  21. skew(1, 2) = -x[0];
  22. skew(2, 0) = -x[1];
  23. skew(2, 1) = x[0];
  24. return skew;
  25. }
  26. /*
  27. * SO3 expmap code taken from Sophus
  28. * https://github.com/strasdat/Sophus/blob/593db47500ea1a2de5f0e6579c86147991509c59/sophus/so3.hpp#L585
  29. *
  30. * Copyright 2011-2017 Hauke Strasdat
  31. * 2012-2017 Steven Lovegrove
  32. *
  33. * Permission is hereby granted, free of charge, to any person obtaining a copy
  34. * of this software and associated documentation files (the "Software"), to
  35. * deal in the Software without restriction, including without limitation the
  36. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  37. * sell copies of the Software, and to permit persons to whom the Software is
  38. * furnished to do so, subject to the following conditions:
  39. *
  40. * The above copyright notice and this permission notice shall be included in
  41. * all copies or substantial portions of the Software.
  42. *
  43. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  44. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  45. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  46. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  47. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  48. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  49. * IN THE SOFTWARE.
  50. */
  51. inline Eigen::Quaterniond so3_exp(const Eigen::Vector3d& omega) {
  52. double theta_sq = omega.dot(omega);
  53. double theta;
  54. double imag_factor;
  55. double real_factor;
  56. if(theta_sq < 1e-10) {
  57. theta = 0;
  58. double theta_quad = theta_sq * theta_sq;
  59. imag_factor = 0.5 - 1.0 / 48.0 * theta_sq + 1.0 / 3840.0 * theta_quad;
  60. real_factor = 1.0 - 1.0 / 8.0 * theta_sq + 1.0 / 384.0 * theta_quad;
  61. } else {
  62. theta = std::sqrt(theta_sq);
  63. double half_theta = 0.5 * theta;
  64. imag_factor = std::sin(half_theta) / theta;
  65. real_factor = std::cos(half_theta);
  66. }
  67. return Eigen::Quaterniond(real_factor, imag_factor * omega.x(), imag_factor * omega.y(), imag_factor * omega.z());
  68. }
  69. } // namespace fast_gicp
  70. #endif