line_manifold.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2022 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_LINE_MANIFOLD_H_
  32. #define CERES_PUBLIC_LINE_MANIFOLD_H_
  33. #include <Eigen/Core>
  34. #include <algorithm>
  35. #include <array>
  36. #include <memory>
  37. #include <vector>
  38. #include "ceres/internal/disable_warnings.h"
  39. #include "ceres/internal/export.h"
  40. #include "ceres/internal/householder_vector.h"
  41. #include "ceres/internal/sphere_manifold_functions.h"
  42. #include "ceres/manifold.h"
  43. #include "ceres/types.h"
  44. #include "glog/logging.h"
  45. namespace ceres {
  46. // This provides a manifold for lines, where the line is
  47. // over-parameterized by an origin point and a direction vector. So the
  48. // parameter vector size needs to be two times the ambient space dimension,
  49. // where the first half is interpreted as the origin point and the second half
  50. // as the direction.
  51. //
  52. // The plus operator for the line direction is the same as for the
  53. // SphereManifold. The update of the origin point is
  54. // perpendicular to the line direction before the update.
  55. //
  56. // This manifold is a special case of the affine Grassmannian
  57. // manifold (see https://en.wikipedia.org/wiki/Affine_Grassmannian_(manifold))
  58. // for the case Graff_1(R^n).
  59. //
  60. // The class works with dynamic and static ambient space dimensions. If the
  61. // ambient space dimensions is known at compile time use
  62. //
  63. // LineManifold<3> manifold;
  64. //
  65. // If the ambient space dimensions is not known at compile time the template
  66. // parameter needs to be set to ceres::DYNAMIC and the actual dimension needs
  67. // to be provided as a constructor argument:
  68. //
  69. // LineManifold<ceres::DYNAMIC> manifold(ambient_dim);
  70. //
  71. template <int AmbientSpaceDimension>
  72. class LineManifold final : public Manifold {
  73. public:
  74. static_assert(AmbientSpaceDimension == DYNAMIC || AmbientSpaceDimension >= 2,
  75. "The ambient space must be at least 2.");
  76. static_assert(ceres::DYNAMIC == Eigen::Dynamic,
  77. "ceres::DYNAMIC needs to be the same as Eigen::Dynamic.");
  78. LineManifold();
  79. explicit LineManifold(int size);
  80. int AmbientSize() const override { return 2 * size_; }
  81. int TangentSize() const override { return 2 * (size_ - 1); }
  82. bool Plus(const double* x,
  83. const double* delta,
  84. double* x_plus_delta) const override;
  85. bool PlusJacobian(const double* x, double* jacobian) const override;
  86. bool Minus(const double* y,
  87. const double* x,
  88. double* y_minus_x) const override;
  89. bool MinusJacobian(const double* x, double* jacobian) const override;
  90. private:
  91. static constexpr bool IsDynamic = (AmbientSpaceDimension == ceres::DYNAMIC);
  92. static constexpr int TangentSpaceDimension =
  93. IsDynamic ? ceres::DYNAMIC : AmbientSpaceDimension - 1;
  94. static constexpr int DAmbientSpaceDimension =
  95. IsDynamic ? ceres::DYNAMIC : 2 * AmbientSpaceDimension;
  96. static constexpr int DTangentSpaceDimension =
  97. IsDynamic ? ceres::DYNAMIC : 2 * TangentSpaceDimension;
  98. using AmbientVector = Eigen::Matrix<double, AmbientSpaceDimension, 1>;
  99. using TangentVector = Eigen::Matrix<double, TangentSpaceDimension, 1>;
  100. using MatrixPlusJacobian = Eigen::Matrix<double,
  101. DAmbientSpaceDimension,
  102. DTangentSpaceDimension,
  103. Eigen::RowMajor>;
  104. using MatrixMinusJacobian = Eigen::Matrix<double,
  105. DTangentSpaceDimension,
  106. DAmbientSpaceDimension,
  107. Eigen::RowMajor>;
  108. const int size_{AmbientSpaceDimension};
  109. };
  110. template <int AmbientSpaceDimension>
  111. LineManifold<AmbientSpaceDimension>::LineManifold()
  112. : size_{AmbientSpaceDimension} {
  113. static_assert(
  114. AmbientSpaceDimension != Eigen::Dynamic,
  115. "The size is set to dynamic. Please call the constructor with a size.");
  116. }
  117. template <int AmbientSpaceDimension>
  118. LineManifold<AmbientSpaceDimension>::LineManifold(int size) : size_{size} {
  119. if (AmbientSpaceDimension != Eigen::Dynamic) {
  120. CHECK_EQ(AmbientSpaceDimension, size)
  121. << "Specified size by template parameter differs from the supplied "
  122. "one.";
  123. } else {
  124. CHECK_GT(size_, 1)
  125. << "The size of the manifold needs to be greater than 1.";
  126. }
  127. }
  128. template <int AmbientSpaceDimension>
  129. bool LineManifold<AmbientSpaceDimension>::Plus(const double* x_ptr,
  130. const double* delta_ptr,
  131. double* x_plus_delta_ptr) const {
  132. // We seek a box plus operator of the form
  133. //
  134. // [o*, d*] = Plus([o, d], [delta_o, delta_d])
  135. //
  136. // where o is the origin point, d is the direction vector, delta_o is
  137. // the delta of the origin point and delta_d the delta of the direction and
  138. // o* and d* is the updated origin point and direction.
  139. //
  140. // We separate the Plus operator into the origin point and directional part
  141. // d* = Plus_d(d, delta_d)
  142. // o* = Plus_o(o, d, delta_o)
  143. //
  144. // The direction update function Plus_d is the same as as the SphereManifold:
  145. //
  146. // d* = H_{v(d)} [sinc(|delta_d|) delta_d, cos(|delta_d|)]^T
  147. //
  148. // where H is the householder matrix
  149. // H_{v} = I - (2 / |v|^2) v v^T
  150. // and
  151. // v(d) = d - sign(d_n) |d| e_n.
  152. //
  153. // The origin point update function Plus_o is defined as
  154. //
  155. // o* = o + H_{v(d)} [delta_o, 0]^T.
  156. Eigen::Map<const AmbientVector> o(x_ptr, size_);
  157. Eigen::Map<const AmbientVector> d(x_ptr + size_, size_);
  158. Eigen::Map<const TangentVector> delta_o(delta_ptr, size_ - 1);
  159. Eigen::Map<const TangentVector> delta_d(delta_ptr + size_ - 1, size_ - 1);
  160. Eigen::Map<AmbientVector> o_plus_delta(x_plus_delta_ptr, size_);
  161. Eigen::Map<AmbientVector> d_plus_delta(x_plus_delta_ptr + size_, size_);
  162. const double norm_delta_d = delta_d.norm();
  163. o_plus_delta = o;
  164. // Shortcut for zero delta direction.
  165. if (norm_delta_d == 0.0) {
  166. d_plus_delta = d;
  167. if (delta_o.isZero(0.0)) {
  168. return true;
  169. }
  170. }
  171. // Calculate the householder transformation which is needed for f_d and f_o.
  172. AmbientVector v(size_);
  173. double beta;
  174. // NOTE: The explicit template arguments are needed here because
  175. // ComputeHouseholderVector is templated and some versions of MSVC
  176. // have trouble deducing the type of v automatically.
  177. internal::ComputeHouseholderVector<Eigen::Map<const AmbientVector>,
  178. double,
  179. AmbientSpaceDimension>(d, &v, &beta);
  180. if (norm_delta_d != 0.0) {
  181. internal::ComputeSphereManifoldPlus(
  182. v, beta, d, delta_d, norm_delta_d, &d_plus_delta);
  183. }
  184. // The null space is in the direction of the line, so the tangent space is
  185. // perpendicular to the line direction. This is achieved by using the
  186. // householder matrix of the direction and allow only movements
  187. // perpendicular to e_n.
  188. AmbientVector y(size_);
  189. y << delta_o, 0;
  190. o_plus_delta += internal::ApplyHouseholderVector(y, v, beta);
  191. return true;
  192. }
  193. template <int AmbientSpaceDimension>
  194. bool LineManifold<AmbientSpaceDimension>::PlusJacobian(
  195. const double* x_ptr, double* jacobian_ptr) const {
  196. Eigen::Map<const AmbientVector> d(x_ptr + size_, size_);
  197. Eigen::Map<MatrixPlusJacobian> jacobian(
  198. jacobian_ptr, 2 * size_, 2 * (size_ - 1));
  199. // Clear the Jacobian as only half of the matrix is not zero.
  200. jacobian.setZero();
  201. auto jacobian_d =
  202. jacobian
  203. .template topLeftCorner<AmbientSpaceDimension, TangentSpaceDimension>(
  204. size_, size_ - 1);
  205. auto jacobian_o = jacobian.template bottomRightCorner<AmbientSpaceDimension,
  206. TangentSpaceDimension>(
  207. size_, size_ - 1);
  208. internal::ComputeSphereManifoldPlusJacobian(d, &jacobian_d);
  209. jacobian_o = jacobian_d;
  210. return true;
  211. }
  212. template <int AmbientSpaceDimension>
  213. bool LineManifold<AmbientSpaceDimension>::Minus(const double* y_ptr,
  214. const double* x_ptr,
  215. double* y_minus_x) const {
  216. Eigen::Map<const AmbientVector> y_o(y_ptr, size_);
  217. Eigen::Map<const AmbientVector> y_d(y_ptr + size_, size_);
  218. Eigen::Map<const AmbientVector> x_o(x_ptr, size_);
  219. Eigen::Map<const AmbientVector> x_d(x_ptr + size_, size_);
  220. Eigen::Map<TangentVector> y_minus_x_o(y_minus_x, size_ - 1);
  221. Eigen::Map<TangentVector> y_minus_x_d(y_minus_x + size_ - 1, size_ - 1);
  222. AmbientVector v(size_);
  223. double beta;
  224. // NOTE: The explicit template arguments are needed here because
  225. // ComputeHouseholderVector is templated and some versions of MSVC
  226. // have trouble deducing the type of v automatically.
  227. internal::ComputeHouseholderVector<Eigen::Map<const AmbientVector>,
  228. double,
  229. AmbientSpaceDimension>(x_d, &v, &beta);
  230. internal::ComputeSphereManifoldMinus(v, beta, x_d, y_d, &y_minus_x_d);
  231. AmbientVector delta_o = y_o - x_o;
  232. const AmbientVector h_delta_o =
  233. internal::ApplyHouseholderVector(delta_o, v, beta);
  234. y_minus_x_o = h_delta_o.template head<TangentSpaceDimension>(size_ - 1);
  235. return true;
  236. }
  237. template <int AmbientSpaceDimension>
  238. bool LineManifold<AmbientSpaceDimension>::MinusJacobian(
  239. const double* x_ptr, double* jacobian_ptr) const {
  240. Eigen::Map<const AmbientVector> d(x_ptr + size_, size_);
  241. Eigen::Map<MatrixMinusJacobian> jacobian(
  242. jacobian_ptr, 2 * (size_ - 1), 2 * size_);
  243. // Clear the Jacobian as only half of the matrix is not zero.
  244. jacobian.setZero();
  245. auto jacobian_d =
  246. jacobian
  247. .template topLeftCorner<TangentSpaceDimension, AmbientSpaceDimension>(
  248. size_ - 1, size_);
  249. auto jacobian_o = jacobian.template bottomRightCorner<TangentSpaceDimension,
  250. AmbientSpaceDimension>(
  251. size_ - 1, size_);
  252. internal::ComputeSphereManifoldMinusJacobian(d, &jacobian_d);
  253. jacobian_o = jacobian_d;
  254. return true;
  255. }
  256. } // namespace ceres
  257. // clang-format off
  258. #include "ceres/internal/reenable_warnings.h"
  259. // clang-format on
  260. #endif // CERES_PUBLIC_LINE_MANIFOLD_H_