Blame include/ceres/autodiff_local_parameterization.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sergey.vfx@gmail.com (Sergey Sharybin)
Packit ea1746
//         mierle@gmail.com (Keir Mierle)
Packit ea1746
//         sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
Packit ea1746
#ifndef CERES_PUBLIC_AUTODIFF_LOCAL_PARAMETERIZATION_H_
Packit ea1746
#define CERES_PUBLIC_AUTODIFF_LOCAL_PARAMETERIZATION_H_
Packit ea1746
Packit ea1746
#include "ceres/local_parameterization.h"
Packit ea1746
#include "ceres/internal/autodiff.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
// Create local parameterization with Jacobians computed via automatic
Packit ea1746
// differentiation. For more information on local parameterizations,
Packit ea1746
// see include/ceres/local_parameterization.h
Packit ea1746
//
Packit ea1746
// To get an auto differentiated local parameterization, you must define
Packit ea1746
// a class with a templated operator() (a functor) that computes
Packit ea1746
//
Packit ea1746
//   x_plus_delta = Plus(x, delta);
Packit ea1746
//
Packit ea1746
// the template parameter T. The autodiff framework substitutes appropriate
Packit ea1746
// "Jet" objects for T in order to compute the derivative when necessary, but
Packit ea1746
// this is hidden, and you should write the function as if T were a scalar type
Packit ea1746
// (e.g. a double-precision floating point number).
Packit ea1746
//
Packit ea1746
// The function must write the computed value in the last argument (the only
Packit ea1746
// non-const one) and return true to indicate success.
Packit ea1746
//
Packit ea1746
// For example, Quaternions have a three dimensional local
Packit ea1746
// parameterization. It's plus operation can be implemented as (taken
Packit ea1746
// from internal/ceres/auto_diff_local_parameterization_test.cc)
Packit ea1746
//
Packit ea1746
//   struct QuaternionPlus {
Packit ea1746
//     template<typename T>
Packit ea1746
//     bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
Packit ea1746
//       const T squared_norm_delta =
Packit ea1746
//           delta[0] * delta[0] + delta[1] * delta[1] + delta[2] * delta[2];
Packit ea1746
//
Packit ea1746
//       T q_delta[4];
Packit ea1746
//       if (squared_norm_delta > T(0.0)) {
Packit ea1746
//         T norm_delta = sqrt(squared_norm_delta);
Packit ea1746
//         const T sin_delta_by_delta = sin(norm_delta) / norm_delta;
Packit ea1746
//         q_delta[0] = cos(norm_delta);
Packit ea1746
//         q_delta[1] = sin_delta_by_delta * delta[0];
Packit ea1746
//         q_delta[2] = sin_delta_by_delta * delta[1];
Packit ea1746
//         q_delta[3] = sin_delta_by_delta * delta[2];
Packit ea1746
//       } else {
Packit ea1746
//         // We do not just use q_delta = [1,0,0,0] here because that is a
Packit ea1746
//         // constant and when used for automatic differentiation will
Packit ea1746
//         // lead to a zero derivative. Instead we take a first order
Packit ea1746
//         // approximation and evaluate it at zero.
Packit ea1746
//         q_delta[0] = T(1.0);
Packit ea1746
//         q_delta[1] = delta[0];
Packit ea1746
//         q_delta[2] = delta[1];
Packit ea1746
//         q_delta[3] = delta[2];
Packit ea1746
//       }
Packit ea1746
//
Packit ea1746
//       QuaternionProduct(q_delta, x, x_plus_delta);
Packit ea1746
//       return true;
Packit ea1746
//     }
Packit ea1746
//   };
Packit ea1746
//
Packit ea1746
// Then given this struct, the auto differentiated local
Packit ea1746
// parameterization can now be constructed as
Packit ea1746
//
Packit ea1746
//   LocalParameterization* local_parameterization =
Packit ea1746
//     new AutoDiffLocalParameterization<QuaternionPlus, 4, 3>;
Packit ea1746
//                                                       |  |
Packit ea1746
//                            Global Size ---------------+  |
Packit ea1746
//                            Local Size -------------------+
Packit ea1746
//
Packit ea1746
// WARNING: Since the functor will get instantiated with different types for
Packit ea1746
// T, you must to convert from other numeric types to T before mixing
Packit ea1746
// computations with other variables of type T. In the example above, this is
Packit ea1746
// seen where instead of using k_ directly, k_ is wrapped with T(k_).
Packit ea1746
Packit ea1746
template <typename Functor, int kGlobalSize, int kLocalSize>
Packit ea1746
class AutoDiffLocalParameterization : public LocalParameterization {
Packit ea1746
 public:
Packit ea1746
  AutoDiffLocalParameterization() :
Packit ea1746
      functor_(new Functor()) {}
Packit ea1746
Packit ea1746
  // Takes ownership of functor.
Packit ea1746
  explicit AutoDiffLocalParameterization(Functor* functor) :
Packit ea1746
      functor_(functor) {}
Packit ea1746
Packit ea1746
  virtual ~AutoDiffLocalParameterization() {}
Packit ea1746
  virtual bool Plus(const double* x,
Packit ea1746
                    const double* delta,
Packit ea1746
                    double* x_plus_delta) const {
Packit ea1746
    return (*functor_)(x, delta, x_plus_delta);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  virtual bool ComputeJacobian(const double* x, double* jacobian) const {
Packit ea1746
    double zero_delta[kLocalSize];
Packit ea1746
    for (int i = 0; i < kLocalSize; ++i) {
Packit ea1746
      zero_delta[i] = 0.0;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    double x_plus_delta[kGlobalSize];
Packit ea1746
    for (int i = 0; i < kGlobalSize; ++i) {
Packit ea1746
      x_plus_delta[i] = 0.0;
Packit ea1746
    }
Packit ea1746
Packit ea1746
    const double* parameter_ptrs[2] = {x, zero_delta};
Packit ea1746
    double* jacobian_ptrs[2] = { NULL, jacobian };
Packit ea1746
    return internal::AutoDiff<Functor, double, kGlobalSize, kLocalSize>
Packit ea1746
        ::Differentiate(*functor_,
Packit ea1746
                        parameter_ptrs,
Packit ea1746
                        kGlobalSize,
Packit ea1746
                        x_plus_delta,
Packit ea1746
                        jacobian_ptrs);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  virtual int GlobalSize() const { return kGlobalSize; }
Packit ea1746
  virtual int LocalSize() const { return kLocalSize; }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  internal::scoped_ptr<Functor> functor_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_PUBLIC_AUTODIFF_LOCAL_PARAMETERIZATION_H_