autodiff_manifold.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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: sameeragarwal@google.com (Sameer Agarwal)
  30. #ifndef CERES_PUBLIC_AUTODIFF_MANIFOLD_H_
  31. #define CERES_PUBLIC_AUTODIFF_MANIFOLD_H_
  32. #include <memory>
  33. #include "ceres/internal/autodiff.h"
  34. #include "ceres/manifold.h"
  35. namespace ceres {
  36. // Create a Manifold with Jacobians computed via automatic differentiation. For
  37. // more information on manifolds, see include/ceres/manifold.h
  38. //
  39. // To get an auto differentiated manifold, you must define a class/struct with
  40. // templated Plus and Minus functions that compute
  41. //
  42. // x_plus_delta = Plus(x, delta);
  43. // y_minus_x = Minus(y, x);
  44. //
  45. // Where, x, y and x_plus_y are vectors on the manifold in the ambient space (so
  46. // they are kAmbientSize vectors) and delta, y_minus_x are vectors in the
  47. // tangent space (so they are kTangentSize vectors).
  48. //
  49. // The Functor should have the signature:
  50. //
  51. // struct Functor {
  52. // template <typename T>
  53. // bool Plus(const T* x, const T* delta, T* x_plus_delta) const;
  54. //
  55. // template <typename T>
  56. // bool Minus(const T* y, const T* x, T* y_minus_x) const;
  57. // };
  58. //
  59. // Observe that the Plus and Minus operations are templated on the parameter T.
  60. // The autodiff framework substitutes appropriate "Jet" objects for T in order
  61. // to compute the derivative when necessary. This is the same mechanism that is
  62. // used to compute derivatives when using AutoDiffCostFunction.
  63. //
  64. // Plus and Minus should return true if the computation is successful and false
  65. // otherwise, in which case the result will not be used.
  66. //
  67. // Given this Functor, the corresponding Manifold can be constructed as:
  68. //
  69. // AutoDiffManifold<Functor, kAmbientSize, kTangentSize> manifold;
  70. //
  71. // As a concrete example consider the case of Quaternions. Quaternions form a
  72. // three dimensional manifold embedded in R^4, i.e. they have an ambient
  73. // dimension of 4 and their tangent space has dimension 3. The following Functor
  74. // (taken from autodiff_manifold_test.cc) defines the Plus and Minus operations
  75. // on the Quaternion manifold:
  76. //
  77. // NOTE: The following is only used for illustration purposes. Ceres Solver
  78. // ships with optimized production grade QuaternionManifold implementation. See
  79. // manifold.h.
  80. //
  81. // This functor assumes that the quaternions are laid out as [w,x,y,z] in
  82. // memory, i.e. the real or scalar part is the first coordinate.
  83. //
  84. // struct QuaternionFunctor {
  85. // template <typename T>
  86. // bool Plus(const T* x, const T* delta, T* x_plus_delta) const {
  87. // const T squared_norm_delta =
  88. // delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2];
  89. //
  90. // T q_delta[4];
  91. // if (squared_norm_delta > T(0.0)) {
  92. // T norm_delta = sqrt(squared_norm_delta);
  93. // const T sin_delta_by_delta = sin(norm_delta) / norm_delta;
  94. // q_delta[0] = cos(norm_delta);
  95. // q_delta[1] = sin_delta_by_delta * delta[0];
  96. // q_delta[2] = sin_delta_by_delta * delta[1];
  97. // q_delta[3] = sin_delta_by_delta * delta[2];
  98. // } else {
  99. // // We do not just use q_delta = [1,0,0,0] here because that is a
  100. // // constant and when used for automatic differentiation will
  101. // // lead to a zero derivative. Instead we take a first order
  102. // // approximation and evaluate it at zero.
  103. // q_delta[0] = T(1.0);
  104. // q_delta[1] = delta[0];
  105. // q_delta[2] = delta[1];
  106. // q_delta[3] = delta[2];
  107. // }
  108. //
  109. // QuaternionProduct(q_delta, x, x_plus_delta);
  110. // return true;
  111. // }
  112. //
  113. // template <typename T>
  114. // bool Minus(const T* y, const T* x, T* y_minus_x) const {
  115. // T minus_x[4] = {x[0], -x[1], -x[2], -x[3]};
  116. // T ambient_y_minus_x[4];
  117. // QuaternionProduct(y, minus_x, ambient_y_minus_x);
  118. // T u_norm = sqrt(ambient_y_minus_x[1] * ambient_y_minus_x[1] +
  119. // ambient_y_minus_x[2] * ambient_y_minus_x[2] +
  120. // ambient_y_minus_x[3] * ambient_y_minus_x[3]);
  121. // if (u_norm > 0.0) {
  122. // T theta = atan2(u_norm, ambient_y_minus_x[0]);
  123. // y_minus_x[0] = theta * ambient_y_minus_x[1] / u_norm;
  124. // y_minus_x[1] = theta * ambient_y_minus_x[2] / u_norm;
  125. // y_minus_x[2] = theta * ambient_y_minus_x[3] / u_norm;
  126. // } else {
  127. // // We do not use [0,0,0] here because even though the value part is
  128. // // a constant, the derivative part is not.
  129. // y_minus_x[0] = ambient_y_minus_x[1];
  130. // y_minus_x[1] = ambient_y_minus_x[2];
  131. // y_minus_x[2] = ambient_y_minus_x[3];
  132. // }
  133. // return true;
  134. // }
  135. // };
  136. //
  137. // Then given this struct, the auto differentiated Quaternion Manifold can now
  138. // be constructed as
  139. //
  140. // Manifold* manifold = new AutoDiffManifold<QuaternionFunctor, 4, 3>;
  141. template <typename Functor, int kAmbientSize, int kTangentSize>
  142. class AutoDiffManifold final : public Manifold {
  143. public:
  144. AutoDiffManifold() : functor_(std::make_unique<Functor>()) {}
  145. // Takes ownership of functor.
  146. explicit AutoDiffManifold(Functor* functor) : functor_(functor) {}
  147. int AmbientSize() const override { return kAmbientSize; }
  148. int TangentSize() const override { return kTangentSize; }
  149. bool Plus(const double* x,
  150. const double* delta,
  151. double* x_plus_delta) const override {
  152. return functor_->Plus(x, delta, x_plus_delta);
  153. }
  154. bool PlusJacobian(const double* x, double* jacobian) const override;
  155. bool Minus(const double* y,
  156. const double* x,
  157. double* y_minus_x) const override {
  158. return functor_->Minus(y, x, y_minus_x);
  159. }
  160. bool MinusJacobian(const double* x, double* jacobian) const override;
  161. const Functor& functor() const { return *functor_; }
  162. private:
  163. std::unique_ptr<Functor> functor_;
  164. };
  165. namespace internal {
  166. // The following two helper structs are needed to interface the Plus and Minus
  167. // methods of the ManifoldFunctor with the automatic differentiation which
  168. // expects a Functor with operator().
  169. template <typename Functor>
  170. struct PlusWrapper {
  171. explicit PlusWrapper(const Functor& functor) : functor(functor) {}
  172. template <typename T>
  173. bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
  174. return functor.Plus(x, delta, x_plus_delta);
  175. }
  176. const Functor& functor;
  177. };
  178. template <typename Functor>
  179. struct MinusWrapper {
  180. explicit MinusWrapper(const Functor& functor) : functor(functor) {}
  181. template <typename T>
  182. bool operator()(const T* y, const T* x, T* y_minus_x) const {
  183. return functor.Minus(y, x, y_minus_x);
  184. }
  185. const Functor& functor;
  186. };
  187. } // namespace internal
  188. template <typename Functor, int kAmbientSize, int kTangentSize>
  189. bool AutoDiffManifold<Functor, kAmbientSize, kTangentSize>::PlusJacobian(
  190. const double* x, double* jacobian) const {
  191. double zero_delta[kTangentSize];
  192. for (int i = 0; i < kTangentSize; ++i) {
  193. zero_delta[i] = 0.0;
  194. }
  195. double x_plus_delta[kAmbientSize];
  196. for (int i = 0; i < kAmbientSize; ++i) {
  197. x_plus_delta[i] = 0.0;
  198. }
  199. const double* parameter_ptrs[2] = {x, zero_delta};
  200. // PlusJacobian is D_2 Plus(x,0) so we only need to compute the Jacobian
  201. // w.r.t. the second argument.
  202. double* jacobian_ptrs[2] = {nullptr, jacobian};
  203. return internal::AutoDifferentiate<
  204. kAmbientSize,
  205. internal::StaticParameterDims<kAmbientSize, kTangentSize>>(
  206. internal::PlusWrapper<Functor>(*functor_),
  207. parameter_ptrs,
  208. kAmbientSize,
  209. x_plus_delta,
  210. jacobian_ptrs);
  211. }
  212. template <typename Functor, int kAmbientSize, int kTangentSize>
  213. bool AutoDiffManifold<Functor, kAmbientSize, kTangentSize>::MinusJacobian(
  214. const double* x, double* jacobian) const {
  215. double y_minus_x[kTangentSize];
  216. for (int i = 0; i < kTangentSize; ++i) {
  217. y_minus_x[i] = 0.0;
  218. }
  219. const double* parameter_ptrs[2] = {x, x};
  220. // MinusJacobian is D_1 Minus(x,x), so we only need to compute the Jacobian
  221. // w.r.t. the first argument.
  222. double* jacobian_ptrs[2] = {jacobian, nullptr};
  223. return internal::AutoDifferentiate<
  224. kTangentSize,
  225. internal::StaticParameterDims<kAmbientSize, kAmbientSize>>(
  226. internal::MinusWrapper<Functor>(*functor_),
  227. parameter_ptrs,
  228. kTangentSize,
  229. y_minus_x,
  230. jacobian_ptrs);
  231. }
  232. } // namespace ceres
  233. #endif // CERES_PUBLIC_AUTODIFF_MANIFOLD_H_