Program Listing for File local_parameterization.hpp

Return to documentation for file (include/fuse_core/local_parameterization.hpp)

/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2019, Locus Robotics
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of the copyright holder nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 */
#ifndef FUSE_CORE__LOCAL_PARAMETERIZATION_HPP_
#define FUSE_CORE__LOCAL_PARAMETERIZATION_HPP_

#include <boost/serialization/access.hpp>
#include <fuse_core/ceres_macros.hpp>
#include <fuse_core/fuse_macros.hpp>
#include <fuse_core/serialization.hpp>

#if !CERES_SUPPORTS_MANIFOLDS
// Local parameterizations is removed in favour of Manifold in
// version 2.2.0, see
// https://github.com/ceres-solver/ceres-solver/commit/0141ca090c315db2f3c38e1731f0fe9754a4e4cc
#include <ceres/local_parameterization.h>
#else
#include <Eigen/Core>
#endif

namespace fuse_core
{

class LocalParameterization
// extend ceres LocalParameterization if our version of Ceres does not support Manifolds
#if !CERES_SUPPORTS_MANIFOLDS
  : public ceres::LocalParameterization
#endif
{
public:
  FUSE_SMART_PTR_ALIASES_ONLY(LocalParameterization)


  virtual ~LocalParameterization() = default;

  virtual int GlobalSize() const = 0;

  virtual int LocalSize() const = 0;

  virtual bool Plus(
    const double * x,
    const double * delta,
    double * x_plus_delta) const = 0;

  virtual bool ComputeJacobian(const double * x, double * jacobian) const = 0;

  virtual bool Minus(
    const double * x,
    const double * y,
    double * y_minus_x) const = 0;

  virtual bool ComputeMinusJacobian(
    const double * x,
    double * jacobian) const = 0;

#if CERES_SUPPORTS_MANIFOLDS
  // If the fuse::LocalParameterization class does not inherit from the
  // ceres::LocalParameterization class then we need to provide the default implementation of the
  // MultiplyByJacobian() method

  virtual bool MultiplyByJacobian(
    const double * x,
    const int num_rows,
    const double * global_matrix,
    double * local_matrix) const
  {
    using Matrix = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
    using MatrixRef = Eigen::Map<Matrix>;
    using ConstMatrixRef = Eigen::Map<const Matrix>;

    if (LocalSize() == 0) {
      return true;
    }

    Matrix jacobian(GlobalSize(), LocalSize());
    if (!ComputeJacobian(x, jacobian.data())) {
      return false;
    }

    MatrixRef(local_matrix, num_rows, LocalSize()) =
      ConstMatrixRef(global_matrix, num_rows, GlobalSize()) * jacobian;
    return true;
  }
#endif

private:
  // Allow Boost Serialization access to private methods
  friend class boost::serialization::access;

  template<class Archive>
  void serialize(Archive & /* archive */, const unsigned int /* version */) {}
};

}  // namespace fuse_core

#endif  // FUSE_CORE__LOCAL_PARAMETERIZATION_HPP_