2014-09-05 11:56:29 -07:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2015-03-17 22:30:16 -07:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
|
// http://ceres-solver.org/
|
2014-09-05 11:56:29 -07:00
|
|
|
//
|
|
|
|
|
// 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 Google Inc. 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 OWNER 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.
|
|
|
|
|
//
|
|
|
|
|
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
|
|
|
|
|
|
|
|
#include "ceres/gradient_problem.h"
|
2020-09-20 21:45:24 +02:00
|
|
|
|
2022-01-19 10:19:16 -08:00
|
|
|
#include <memory>
|
|
|
|
|
|
2014-09-05 11:56:29 -07:00
|
|
|
#include "ceres/local_parameterization.h"
|
2021-12-28 07:05:03 -08:00
|
|
|
#include "ceres/manifold_adapter.h"
|
2014-09-05 11:56:29 -07:00
|
|
|
#include "glog/logging.h"
|
|
|
|
|
|
|
|
|
|
namespace ceres {
|
|
|
|
|
|
|
|
|
|
GradientProblem::GradientProblem(FirstOrderFunction* function)
|
2022-01-19 07:00:32 -08:00
|
|
|
: function_(function),
|
2022-01-19 10:19:16 -08:00
|
|
|
manifold_(
|
|
|
|
|
std::make_unique<EuclideanManifold>(function_->NumParameters())),
|
2022-01-19 07:00:32 -08:00
|
|
|
scratch_(new double[function_->NumParameters()]) {
|
|
|
|
|
CHECK(function != nullptr);
|
|
|
|
|
}
|
2014-09-05 11:56:29 -07:00
|
|
|
|
|
|
|
|
GradientProblem::GradientProblem(FirstOrderFunction* function,
|
|
|
|
|
LocalParameterization* parameterization)
|
2022-01-19 07:00:32 -08:00
|
|
|
: function_(function),
|
|
|
|
|
parameterization_(parameterization),
|
2020-09-20 21:45:24 +02:00
|
|
|
scratch_(new double[function_->NumParameters()]) {
|
2022-01-19 07:00:32 -08:00
|
|
|
CHECK(function != nullptr);
|
|
|
|
|
if (parameterization != nullptr) {
|
2022-02-02 13:17:29 -08:00
|
|
|
manifold_ =
|
|
|
|
|
std::make_unique<internal::ManifoldAdapter>(parameterization_.get());
|
2022-01-19 07:00:32 -08:00
|
|
|
} else {
|
2022-02-02 13:17:29 -08:00
|
|
|
manifold_ = std::make_unique<EuclideanManifold>(function_->NumParameters());
|
2022-01-19 07:00:32 -08:00
|
|
|
}
|
2021-12-28 07:05:03 -08:00
|
|
|
CHECK_EQ(function_->NumParameters(), manifold_->AmbientSize());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GradientProblem::GradientProblem(FirstOrderFunction* function,
|
|
|
|
|
Manifold* manifold)
|
2022-01-19 10:19:16 -08:00
|
|
|
: function_(function), scratch_(new double[function_->NumParameters()]) {
|
2022-01-19 07:00:32 -08:00
|
|
|
CHECK(function != nullptr);
|
2022-01-19 10:19:16 -08:00
|
|
|
manifold_.reset(manifold != nullptr
|
|
|
|
|
? manifold
|
|
|
|
|
: new EuclideanManifold(function_->NumParameters()));
|
2021-12-28 07:05:03 -08:00
|
|
|
CHECK_EQ(function_->NumParameters(), manifold_->AmbientSize());
|
2014-09-05 11:56:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GradientProblem::NumParameters() const {
|
|
|
|
|
return function_->NumParameters();
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-28 07:05:03 -08:00
|
|
|
int GradientProblem::NumTangentParameters() const {
|
|
|
|
|
return manifold_->TangentSize();
|
2014-09-05 11:56:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GradientProblem::Evaluate(const double* parameters,
|
|
|
|
|
double* cost,
|
|
|
|
|
double* gradient) const {
|
2021-12-28 07:05:03 -08:00
|
|
|
if (gradient == nullptr) {
|
|
|
|
|
return function_->Evaluate(parameters, cost, nullptr);
|
2014-09-05 11:56:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (function_->Evaluate(parameters, cost, scratch_.get()) &&
|
2021-12-28 07:05:03 -08:00
|
|
|
manifold_->RightMultiplyByPlusJacobian(
|
2020-09-20 21:45:24 +02:00
|
|
|
parameters, 1, scratch_.get(), gradient));
|
2014-09-05 11:56:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GradientProblem::Plus(const double* x,
|
|
|
|
|
const double* delta,
|
|
|
|
|
double* x_plus_delta) const {
|
2021-12-28 07:05:03 -08:00
|
|
|
return manifold_->Plus(x, delta, x_plus_delta);
|
2014-09-05 11:56:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace ceres
|