so3.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /************************************************************
  2. *
  3. * Copyright (c) 2021, University of California, Los Angeles
  4. *
  5. * Authors: Kenny J. Chen, Brett T. Lopez
  6. * Contact: kennyjchen@ucla.edu, btlopez@ucla.edu
  7. *
  8. ***********************************************************/
  9. /***********************************************************************
  10. * BSD 3-Clause License
  11. *
  12. * Copyright (c) 2020, SMRT-AIST
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice, this
  19. * list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * 3. Neither the name of the copyright holder nor the names of its
  26. * contributors may be used to endorse or promote products derived from
  27. * this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  32. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  33. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  35. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  36. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  37. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *************************************************************************/
  40. #ifndef NANO_GICP_SO3_HPP
  41. #define NANO_GICP_SO3_HPP
  42. #include <Eigen/Core>
  43. #include <Eigen/Geometry>
  44. namespace nano_gicp {
  45. inline Eigen::Matrix3f skew(const Eigen::Vector3f& x) {
  46. Eigen::Matrix3f skew = Eigen::Matrix3f::Zero();
  47. skew(0, 1) = -x[2];
  48. skew(0, 2) = x[1];
  49. skew(1, 0) = x[2];
  50. skew(1, 2) = -x[0];
  51. skew(2, 0) = -x[1];
  52. skew(2, 1) = x[0];
  53. return skew;
  54. }
  55. inline Eigen::Matrix3d skewd(const Eigen::Vector3d& x) {
  56. Eigen::Matrix3d skew = Eigen::Matrix3d::Zero();
  57. skew(0, 1) = -x[2];
  58. skew(0, 2) = x[1];
  59. skew(1, 0) = x[2];
  60. skew(1, 2) = -x[0];
  61. skew(2, 0) = -x[1];
  62. skew(2, 1) = x[0];
  63. return skew;
  64. }
  65. /*
  66. * SO3 expmap code taken from Sophus
  67. * https://github.com/strasdat/Sophus/blob/593db47500ea1a2de5f0e6579c86147991509c59/sophus/so3.hpp#L585
  68. *
  69. * Copyright 2011-2017 Hauke Strasdat
  70. * 2012-2017 Steven Lovegrove
  71. *
  72. * Permission is hereby granted, free of charge, to any person obtaining a copy
  73. * of this software and associated documentation files (the "Software"), to
  74. * deal in the Software without restriction, including without limitation the
  75. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  76. * sell copies of the Software, and to permit persons to whom the Software is
  77. * furnished to do so, subject to the following conditions:
  78. *
  79. * The above copyright notice and this permission notice shall be included in
  80. * all copies or substantial portions of the Software.
  81. *
  82. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  83. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  84. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  85. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  86. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  87. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  88. * IN THE SOFTWARE.
  89. */
  90. inline Eigen::Quaterniond so3_exp(const Eigen::Vector3d& omega) {
  91. double theta_sq = omega.dot(omega);
  92. double theta;
  93. double imag_factor;
  94. double real_factor;
  95. if(theta_sq < 1e-10) {
  96. theta = 0;
  97. double theta_quad = theta_sq * theta_sq;
  98. imag_factor = 0.5 - 1.0 / 48.0 * theta_sq + 1.0 / 3840.0 * theta_quad;
  99. real_factor = 1.0 - 1.0 / 8.0 * theta_sq + 1.0 / 384.0 * theta_quad;
  100. } else {
  101. theta = std::sqrt(theta_sq);
  102. double half_theta = 0.5 * theta;
  103. imag_factor = std::sin(half_theta) / theta;
  104. real_factor = std::cos(half_theta);
  105. }
  106. return Eigen::Quaterniond(real_factor, imag_factor * omega.x(), imag_factor * omega.y(), imag_factor * omega.z());
  107. }
  108. } // namespace nano_gicp
  109. #endif