// g2o - General Graph Optimization
// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard
// 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.
//
// 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.

#include <memory>

#include "g2o/core/block_solver.h"
#include "g2o/core/optimization_algorithm.h"
#include "g2o/core/optimization_algorithm_factory.h"
#include "g2o/core/solver.h"
#include "g2o/core/sparse_optimizer.h"
#include "g2o/solvers/eigen/linear_solver_eigen.h"
#include "g2o/stuff/macros.h"
#include "solver_slam2d_linear.h"

namespace g2o {

namespace {
template <int P, int L, bool Blockorder>
std::unique_ptr<BlockSolverBase> AllocateSolver() {
  std::cerr << "# Using 2dlinear poseDim " << P << " landMarkDim " << L
            << " blockordering " << Blockorder << std::endl;
  auto linearSolver = g2o::make_unique<
      LinearSolverEigen<typename BlockSolverPL<P, L>::PoseMatrixType>>();
  linearSolver->setBlockOrdering(Blockorder);
  return g2o::make_unique<BlockSolverPL<P, L>>(std::move(linearSolver));
}
}  // namespace

/**
 * helper function for allocating
 */
static OptimizationAlgorithm* createSolver(const std::string& fullSolverName) {
  if (fullSolverName != "2dlinear") return nullptr;

  return new SolverSLAM2DLinear{AllocateSolver<3, 2, true>()};
}

class SLAM2DLinearSolverCreator : public AbstractOptimizationAlgorithmCreator {
 public:
  explicit SLAM2DLinearSolverCreator(const OptimizationAlgorithmProperty& p)
      : AbstractOptimizationAlgorithmCreator(p) {}
  std::unique_ptr<OptimizationAlgorithm> construct() override {
    return std::unique_ptr<OptimizationAlgorithm>(
        createSolver(property().name));
  }
};

G2O_REGISTER_OPTIMIZATION_LIBRARY(slam2d_linear);

G2O_REGISTER_OPTIMIZATION_ALGORITHM(
    2dlinear,
    new SLAM2DLinearSolverCreator(OptimizationAlgorithmProperty(
        "2dlinear",
        "Solve Orientation + Gauss-Newton: Works only on 2D pose graphs!!",
        "Eigen", false, 3, 3)));

}  // namespace g2o
