Program Listing for File marginal_constraint.hpp

Return to documentation for file (/tmp/ws/src/fuse/fuse_constraints/include/fuse_constraints/marginal_constraint.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_CONSTRAINTS__MARGINAL_CONSTRAINT_HPP_
#define FUSE_CONSTRAINTS__MARGINAL_CONSTRAINT_HPP_

#include <ceres/cost_function.h>

#include <algorithm>
#include <cassert>
#include <ostream>
#include <string>
#include <vector>

#include <fuse_core/constraint.hpp>
#include <fuse_core/eigen.hpp>
#include <fuse_core/local_parameterization.hpp>
#include <fuse_core/fuse_macros.hpp>
#include <fuse_core/serialization.hpp>
#include <fuse_core/variable.hpp>

#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/zip_iterator.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/tuple/tuple.hpp>


namespace fuse_constraints
{

class MarginalConstraint : public fuse_core::Constraint
{
public:
  FUSE_CONSTRAINT_DEFINITIONS(MarginalConstraint)


  MarginalConstraint() = default;

  template<typename VariableIterator, typename MatrixIterator>
  MarginalConstraint(
    const std::string & source,
    VariableIterator first_variable,
    VariableIterator last_variable,
    MatrixIterator first_A,
    MatrixIterator last_A,
    const fuse_core::VectorXd & b);

  virtual ~MarginalConstraint() = default;

  const std::vector<fuse_core::MatrixXd> & A() const {return A_;}

  const fuse_core::VectorXd & b() const {return b_;}

  const std::vector<fuse_core::VectorXd> & x_bar() const {return x_bar_;}

  const std::vector<fuse_core::LocalParameterization::SharedPtr> & localParameterizations() const
  {
    return local_parameterizations_;
  }

  void print(std::ostream & stream = std::cout) const override;

  ceres::CostFunction * costFunction() const override;

protected:
  std::vector<fuse_core::MatrixXd> A_;
  fuse_core::VectorXd b_;

  std::vector<fuse_core::LocalParameterization::SharedPtr> local_parameterizations_;

  std::vector<fuse_core::VectorXd> x_bar_;

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

  template<class Archive>
  void serialize(Archive & archive, const unsigned int /* version */)
  {
    archive & boost::serialization::base_object<fuse_core::Constraint>(*this);
    archive & A_;
    archive & b_;
    archive & local_parameterizations_;
    archive & x_bar_;
  }
};

namespace detail
{

inline const fuse_core::UUID getUuid(const fuse_core::Variable & variable)
{
  return variable.uuid();
}

inline const fuse_core::VectorXd getCurrentValue(const fuse_core::Variable & variable)
{
  return Eigen::Map<const fuse_core::VectorXd>(variable.data(), variable.size());
}

inline fuse_core::LocalParameterization::SharedPtr const getLocalParameterization(
  const fuse_core::Variable & variable)
{
  return fuse_core::LocalParameterization::SharedPtr(variable.localParameterization());
}

}  // namespace detail

template<typename VariableIterator, typename MatrixIterator>
MarginalConstraint::MarginalConstraint(
  const std::string & source,
  VariableIterator first_variable,
  VariableIterator last_variable,
  MatrixIterator first_A,
  MatrixIterator last_A,
  const fuse_core::VectorXd & b)
: Constraint(source,
    boost::make_transform_iterator(first_variable, &fuse_constraints::detail::getUuid),
    boost::make_transform_iterator(last_variable, &fuse_constraints::detail::getUuid)),
  A_(first_A, last_A),
  b_(b),
  local_parameterizations_(boost::make_transform_iterator(first_variable,
    &fuse_constraints::detail::getLocalParameterization),
    boost::make_transform_iterator(last_variable,
    &fuse_constraints::detail::getLocalParameterization)),
  x_bar_(boost::make_transform_iterator(first_variable, &fuse_constraints::detail::getCurrentValue),
    boost::make_transform_iterator(last_variable, &fuse_constraints::detail::getCurrentValue))
{
  assert(!A_.empty());
  assert(A_.size() == x_bar_.size());
  assert(A_.size() == local_parameterizations_.size());
  assert(b_.rows() > 0);
  assert(
    std::all_of(
      A_.begin(), A_.end(), [this](const auto & A) {
        return A.rows() == this->b_.rows();
      }));  // NOLINT
  assert(
    std::all_of(
      boost::make_zip_iterator(boost::make_tuple(A_.begin(), first_variable)),
      boost::make_zip_iterator(boost::make_tuple(A_.end(), last_variable)),
      [](const boost::tuple<const fuse_core::MatrixXd &, const fuse_core::Variable &> & tuple)  // NOLINT
      {
        return static_cast<size_t>(tuple.get<0>().cols()) == tuple.get<1>().localSize();
      }));  // NOLINT
}

}  // namespace fuse_constraints

BOOST_CLASS_EXPORT_KEY(fuse_constraints::MarginalConstraint);

#endif  // FUSE_CONSTRAINTS__MARGINAL_CONSTRAINT_HPP_