sphere_manifold_functions.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: vitus@google.com (Mike Vitus)
  30. // jodebo_beck@gmx.de (Johannes Beck)
  31. #ifndef CERES_PUBLIC_INTERNAL_SPHERE_MANIFOLD_HELPERS_H_
  32. #define CERES_PUBLIC_INTERNAL_SPHERE_MANIFOLD_HELPERS_H_
  33. #include "ceres/constants.h"
  34. #include "ceres/internal/householder_vector.h"
  35. // This module contains functions to compute the SphereManifold plus and minus
  36. // operator and their Jacobians.
  37. //
  38. // As the parameters to these functions are shared between them, they are
  39. // described here: The following variable names are used:
  40. // Plus(x, delta) = x + delta = x_plus_delta,
  41. // Minus(y, x) = y - x = y_minus_x.
  42. //
  43. // The remaining ones are v and beta which describe the Householder
  44. // transformation of x, and norm_delta which is the norm of delta.
  45. //
  46. // The types of x, y, x_plus_delta and y_minus_x need to be equivalent to
  47. // Eigen::Matrix<double, AmbientSpaceDimension, 1> and the type of delta needs
  48. // to be equivalent to Eigen::Matrix<double, TangentSpaceDimension, 1>.
  49. //
  50. // The type of Jacobian plus needs to be equivalent to Eigen::Matrix<double,
  51. // AmbientSpaceDimension, TangentSpaceDimension, Eigen::RowMajor> and for
  52. // Jacobian minus Eigen::Matrix<double, TangentSpaceDimension,
  53. // AmbientSpaceDimension, Eigen::RowMajor>.
  54. //
  55. // For all vector / matrix inputs and outputs, template parameters are
  56. // used in order to allow also Eigen::Ref and Eigen block expressions to
  57. // be passed to the function.
  58. namespace ceres::internal {
  59. template <typename VT, typename XT, typename DeltaT, typename XPlusDeltaT>
  60. inline void ComputeSphereManifoldPlus(const VT& v,
  61. double beta,
  62. const XT& x,
  63. const DeltaT& delta,
  64. const double norm_delta,
  65. XPlusDeltaT* x_plus_delta) {
  66. constexpr int AmbientDim = VT::RowsAtCompileTime;
  67. // Map the delta from the minimum representation to the over parameterized
  68. // homogeneous vector. See B.2 p.25 equation (106) - (107) for more details.
  69. const double sin_delta_by_delta = std::sin(norm_delta) / norm_delta;
  70. Eigen::Matrix<double, AmbientDim, 1> y(v.size());
  71. y << sin_delta_by_delta * delta, std::cos(norm_delta);
  72. // Apply the delta update to remain on the sphere.
  73. *x_plus_delta = x.norm() * ApplyHouseholderVector(y, v, beta);
  74. }
  75. template <typename VT, typename JacobianT>
  76. inline void ComputeSphereManifoldPlusJacobian(const VT& x,
  77. JacobianT* jacobian) {
  78. constexpr int AmbientSpaceDim = VT::RowsAtCompileTime;
  79. using AmbientVector = Eigen::Matrix<double, AmbientSpaceDim, 1>;
  80. const int ambient_size = x.size();
  81. const int tangent_size = x.size() - 1;
  82. AmbientVector v(ambient_size);
  83. double beta;
  84. // NOTE: The explicit template arguments are needed here because
  85. // ComputeHouseholderVector is templated and some versions of MSVC
  86. // have trouble deducing the type of v automatically.
  87. ComputeHouseholderVector<VT, double, AmbientSpaceDim>(x, &v, &beta);
  88. // The Jacobian is equal to J = H.leftCols(size_ - 1) where H is the
  89. // Householder matrix (H = I - beta * v * v').
  90. for (int i = 0; i < tangent_size; ++i) {
  91. (*jacobian).col(i) = -beta * v(i) * v;
  92. (*jacobian)(i, i) += 1.0;
  93. }
  94. (*jacobian) *= x.norm();
  95. }
  96. template <typename VT, typename XT, typename YT, typename YMinusXT>
  97. inline void ComputeSphereManifoldMinus(
  98. const VT& v, double beta, const XT& x, const YT& y, YMinusXT* y_minus_x) {
  99. constexpr int AmbientSpaceDim = VT::RowsAtCompileTime;
  100. constexpr int TangentSpaceDim =
  101. AmbientSpaceDim == Eigen::Dynamic ? Eigen::Dynamic : AmbientSpaceDim - 1;
  102. using AmbientVector = Eigen::Matrix<double, AmbientSpaceDim, 1>;
  103. const int tangent_size = v.size() - 1;
  104. const AmbientVector hy = ApplyHouseholderVector(y, v, beta) / x.norm();
  105. // Calculate y - x. See B.2 p.25 equation (108).
  106. const double y_last = hy[tangent_size];
  107. const double hy_norm = hy.template head<TangentSpaceDim>(tangent_size).norm();
  108. if (hy_norm == 0.0) {
  109. y_minus_x->setZero();
  110. y_minus_x->data()[tangent_size - 1] = y_last >= 0 ? 0.0 : constants::pi;
  111. } else {
  112. *y_minus_x = std::atan2(hy_norm, y_last) / hy_norm *
  113. hy.template head<TangentSpaceDim>(tangent_size);
  114. }
  115. }
  116. template <typename VT, typename JacobianT>
  117. inline void ComputeSphereManifoldMinusJacobian(const VT& x,
  118. JacobianT* jacobian) {
  119. constexpr int AmbientSpaceDim = VT::RowsAtCompileTime;
  120. using AmbientVector = Eigen::Matrix<double, AmbientSpaceDim, 1>;
  121. const int ambient_size = x.size();
  122. const int tangent_size = x.size() - 1;
  123. AmbientVector v(ambient_size);
  124. double beta;
  125. // NOTE: The explicit template arguments are needed here because
  126. // ComputeHouseholderVector is templated and some versions of MSVC
  127. // have trouble deducing the type of v automatically.
  128. ComputeHouseholderVector<VT, double, AmbientSpaceDim>(x, &v, &beta);
  129. // The Jacobian is equal to J = H.leftCols(size_ - 1) where H is the
  130. // Householder matrix (H = I - beta * v * v').
  131. for (int i = 0; i < tangent_size; ++i) {
  132. // NOTE: The transpose is used for correctness (the product is expected to
  133. // be a row vector), although here there seems to be no difference between
  134. // transposing or not for Eigen (possibly a compile-time auto fix).
  135. (*jacobian).row(i) = -beta * v(i) * v.transpose();
  136. (*jacobian)(i, i) += 1.0;
  137. }
  138. (*jacobian) /= x.norm();
  139. }
  140. } // namespace ceres::internal
  141. #endif