line_parameterization.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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: jodebo_beck@gmx.de (Johannes Beck)
  30. //
  31. #ifndef CERES_PUBLIC_INTERNAL_LINE_PARAMETERIZATION_H_
  32. #define CERES_PUBLIC_INTERNAL_LINE_PARAMETERIZATION_H_
  33. #include "householder_vector.h"
  34. namespace ceres {
  35. template <int AmbientSpaceDimension>
  36. bool LineParameterization<AmbientSpaceDimension>::Plus(
  37. const double* x_ptr,
  38. const double* delta_ptr,
  39. double* x_plus_delta_ptr) const {
  40. // We seek a box plus operator of the form
  41. //
  42. // [o*, d*] = Plus([o, d], [delta_o, delta_d])
  43. //
  44. // where o is the origin point, d is the direction vector, delta_o is
  45. // the delta of the origin point and delta_d the delta of the direction and
  46. // o* and d* is the updated origin point and direction.
  47. //
  48. // We separate the Plus operator into the origin point and directional part
  49. // d* = Plus_d(d, delta_d)
  50. // o* = Plus_o(o, d, delta_o)
  51. //
  52. // The direction update function Plus_d is the same as for the homogeneous
  53. // vector parameterization:
  54. //
  55. // d* = H_{v(d)} [0.5 sinc(0.5 |delta_d|) delta_d, cos(0.5 |delta_d|)]^T
  56. //
  57. // where H is the householder matrix
  58. // H_{v} = I - (2 / |v|^2) v v^T
  59. // and
  60. // v(d) = d - sign(d_n) |d| e_n.
  61. //
  62. // The origin point update function Plus_o is defined as
  63. //
  64. // o* = o + H_{v(d)} [0.5 delta_o, 0]^T.
  65. static constexpr int kDim = AmbientSpaceDimension;
  66. using AmbientVector = Eigen::Matrix<double, kDim, 1>;
  67. using AmbientVectorRef = Eigen::Map<Eigen::Matrix<double, kDim, 1>>;
  68. using ConstAmbientVectorRef =
  69. Eigen::Map<const Eigen::Matrix<double, kDim, 1>>;
  70. using ConstTangentVectorRef =
  71. Eigen::Map<const Eigen::Matrix<double, kDim - 1, 1>>;
  72. ConstAmbientVectorRef o(x_ptr);
  73. ConstAmbientVectorRef d(x_ptr + kDim);
  74. ConstTangentVectorRef delta_o(delta_ptr);
  75. ConstTangentVectorRef delta_d(delta_ptr + kDim - 1);
  76. AmbientVectorRef o_plus_delta(x_plus_delta_ptr);
  77. AmbientVectorRef d_plus_delta(x_plus_delta_ptr + kDim);
  78. const double norm_delta_d = delta_d.norm();
  79. o_plus_delta = o;
  80. // Shortcut for zero delta direction.
  81. if (norm_delta_d == 0.0) {
  82. d_plus_delta = d;
  83. if (delta_o.isZero(0.0)) {
  84. return true;
  85. }
  86. }
  87. // Calculate the householder transformation which is needed for f_d and f_o.
  88. AmbientVector v;
  89. double beta;
  90. // NOTE: The explicit template arguments are needed here because
  91. // ComputeHouseholderVector is templated and some versions of MSVC
  92. // have trouble deducing the type of v automatically.
  93. internal::ComputeHouseholderVector<ConstAmbientVectorRef, double, kDim>(
  94. d, &v, &beta);
  95. if (norm_delta_d != 0.0) {
  96. // Map the delta from the minimum representation to the over parameterized
  97. // homogeneous vector. See section A6.9.2 on page 624 of Hartley & Zisserman
  98. // (2nd Edition) for a detailed description. Note there is a typo on Page
  99. // 625, line 4 so check the book errata.
  100. const double norm_delta_div_2 = 0.5 * norm_delta_d;
  101. const double sin_delta_by_delta =
  102. std::sin(norm_delta_div_2) / norm_delta_div_2;
  103. // Apply the delta update to remain on the unit sphere. See section A6.9.3
  104. // on page 625 of Hartley & Zisserman (2nd Edition) for a detailed
  105. // description.
  106. AmbientVector y;
  107. y.template head<kDim - 1>() = 0.5 * sin_delta_by_delta * delta_d;
  108. y[kDim - 1] = std::cos(norm_delta_div_2);
  109. d_plus_delta = d.norm() * (y - v * (beta * (v.transpose() * y)));
  110. }
  111. // The null space is in the direction of the line, so the tangent space is
  112. // perpendicular to the line direction. This is achieved by using the
  113. // householder matrix of the direction and allow only movements
  114. // perpendicular to e_n.
  115. //
  116. // The factor of 0.5 is used to be consistent with the line direction
  117. // update.
  118. AmbientVector y;
  119. y << 0.5 * delta_o, 0;
  120. o_plus_delta += y - v * (beta * (v.transpose() * y));
  121. return true;
  122. }
  123. template <int AmbientSpaceDimension>
  124. bool LineParameterization<AmbientSpaceDimension>::ComputeJacobian(
  125. const double* x_ptr, double* jacobian_ptr) const {
  126. static constexpr int kDim = AmbientSpaceDimension;
  127. using AmbientVector = Eigen::Matrix<double, kDim, 1>;
  128. using ConstAmbientVectorRef =
  129. Eigen::Map<const Eigen::Matrix<double, kDim, 1>>;
  130. using MatrixRef = Eigen::Map<
  131. Eigen::Matrix<double, 2 * kDim, 2 * (kDim - 1), Eigen::RowMajor>>;
  132. ConstAmbientVectorRef d(x_ptr + kDim);
  133. MatrixRef jacobian(jacobian_ptr);
  134. // Clear the Jacobian as only half of the matrix is not zero.
  135. jacobian.setZero();
  136. AmbientVector v;
  137. double beta;
  138. // NOTE: The explicit template arguments are needed here because
  139. // ComputeHouseholderVector is templated and some versions of MSVC
  140. // have trouble deducing the type of v automatically.
  141. internal::ComputeHouseholderVector<ConstAmbientVectorRef, double, kDim>(
  142. d, &v, &beta);
  143. // The Jacobian is equal to J = 0.5 * H.leftCols(kDim - 1) where H is
  144. // the Householder matrix (H = I - beta * v * v') for the origin point. For
  145. // the line direction part the Jacobian is scaled by the norm of the
  146. // direction.
  147. for (int i = 0; i < kDim - 1; ++i) {
  148. jacobian.block(0, i, kDim, 1) = -0.5 * beta * v(i) * v;
  149. jacobian.col(i)(i) += 0.5;
  150. }
  151. jacobian.template block<kDim, kDim - 1>(kDim, kDim - 1) =
  152. jacobian.template block<kDim, kDim - 1>(0, 0) * d.norm();
  153. return true;
  154. }
  155. } // namespace ceres
  156. #endif // CERES_PUBLIC_INTERNAL_LINE_PARAMETERIZATION_H_