mirror of
https://github.com/ceres-solver/ceres-solver.git
synced 2026-08-29 08:34:37 +08:00
GradientProblem & related classes use std::unique_ptr
Previously these classes in analogy with ceres::Problem's interface had interfaces to allow bare pointers as well as unique_ptrs. This CL changes the API to always use unique_ptr, this is less error prone and makes the default ownership semantics clearer. Change-Id: I7577a90761f341c7e009c248c820f0fec2e6f32d
This commit is contained in:
@@ -53,9 +53,9 @@ class QuadraticCostFunctor {
|
||||
};
|
||||
|
||||
TEST(AutoDiffFirstOrderFunction, BilinearDifferentiationTest) {
|
||||
std::unique_ptr<FirstOrderFunction> function(
|
||||
new AutoDiffFirstOrderFunction<QuadraticCostFunctor, 4>(
|
||||
new QuadraticCostFunctor(1.0)));
|
||||
std::unique_ptr<FirstOrderFunction> function =
|
||||
std::make_unique<AutoDiffFirstOrderFunction<QuadraticCostFunctor, 4>>(
|
||||
1.0);
|
||||
|
||||
double parameters[4] = {1.0, 2.0, 3.0, 4.0};
|
||||
double gradient[4];
|
||||
|
||||
@@ -36,24 +36,21 @@
|
||||
|
||||
namespace ceres {
|
||||
|
||||
GradientProblem::GradientProblem(FirstOrderFunction* function)
|
||||
: function_(function),
|
||||
GradientProblem::GradientProblem(std::unique_ptr<FirstOrderFunction> function)
|
||||
: function_(std::move(function)),
|
||||
manifold_(std::make_unique<EuclideanManifold<DYNAMIC>>(
|
||||
function_->NumParameters())),
|
||||
scratch_(new double[function_->NumParameters()]) {
|
||||
CHECK(function != nullptr);
|
||||
CHECK(function_ != nullptr);
|
||||
}
|
||||
|
||||
GradientProblem::GradientProblem(FirstOrderFunction* function,
|
||||
Manifold* manifold)
|
||||
: function_(function), scratch_(new double[function_->NumParameters()]) {
|
||||
CHECK(function != nullptr);
|
||||
if (manifold != nullptr) {
|
||||
manifold_.reset(manifold);
|
||||
} else {
|
||||
manifold_ = std::make_unique<EuclideanManifold<DYNAMIC>>(
|
||||
function_->NumParameters());
|
||||
}
|
||||
GradientProblem::GradientProblem(std::unique_ptr<FirstOrderFunction> function,
|
||||
std::unique_ptr<Manifold> manifold)
|
||||
: function_(std::move(function)),
|
||||
manifold_(std::move(manifold)),
|
||||
scratch_(new double[function_->NumParameters()]) {
|
||||
CHECK(function_ != nullptr);
|
||||
CHECK(manifold_ != nullptr);
|
||||
CHECK_EQ(function_->NumParameters(), manifold_->AmbientSize());
|
||||
}
|
||||
|
||||
|
||||
@@ -61,7 +61,7 @@ TEST(GradientProblemSolver, SolvesRosenbrockWithDefaultOptions) {
|
||||
|
||||
ceres::GradientProblemSolver::Options options;
|
||||
ceres::GradientProblemSolver::Summary summary;
|
||||
ceres::GradientProblem problem(new Rosenbrock());
|
||||
ceres::GradientProblem problem(std::make_unique<Rosenbrock>());
|
||||
ceres::Solve(options, problem, parameters, &summary);
|
||||
|
||||
EXPECT_EQ(CONVERGENCE, summary.termination_type);
|
||||
@@ -99,7 +99,7 @@ TEST(Solver, UpdateStateEveryIterationOption) {
|
||||
double x = 50.0;
|
||||
const double original_x = x;
|
||||
|
||||
ceres::GradientProblem problem(new QuadraticFunction);
|
||||
ceres::GradientProblem problem(std::make_unique<QuadraticFunction>());
|
||||
ceres::GradientProblemSolver::Options options;
|
||||
RememberingCallback callback(&x);
|
||||
options.callbacks.push_back(&callback);
|
||||
|
||||
@@ -64,13 +64,16 @@ class QuadraticTestFunction : public ceres::FirstOrderFunction {
|
||||
|
||||
TEST(GradientProblem, TakesOwnershipOfFirstOrderFunction) {
|
||||
bool is_destructed = false;
|
||||
{ ceres::GradientProblem problem(new QuadraticTestFunction(&is_destructed)); }
|
||||
{
|
||||
ceres::GradientProblem problem(
|
||||
std::make_unique<QuadraticTestFunction>(&is_destructed));
|
||||
}
|
||||
EXPECT_TRUE(is_destructed);
|
||||
}
|
||||
|
||||
TEST(GradientProblem, EvaluationWithManifoldAndNoGradient) {
|
||||
ceres::GradientProblem problem(new QuadraticTestFunction(),
|
||||
new EuclideanManifold<1>);
|
||||
ceres::GradientProblem problem(std::make_unique<QuadraticTestFunction>(),
|
||||
std::make_unique<EuclideanManifold<1>>());
|
||||
double x = 7.0;
|
||||
double cost = 0;
|
||||
problem.Evaluate(&x, &cost, nullptr);
|
||||
@@ -78,7 +81,7 @@ TEST(GradientProblem, EvaluationWithManifoldAndNoGradient) {
|
||||
}
|
||||
|
||||
TEST(GradientProblem, EvaluationWithoutManifoldAndWithGradient) {
|
||||
ceres::GradientProblem problem(new QuadraticTestFunction());
|
||||
ceres::GradientProblem problem(std::make_unique<QuadraticTestFunction>());
|
||||
double x = 7.0;
|
||||
double cost = 0;
|
||||
double gradient = 0;
|
||||
@@ -87,8 +90,8 @@ TEST(GradientProblem, EvaluationWithoutManifoldAndWithGradient) {
|
||||
}
|
||||
|
||||
TEST(GradientProblem, EvaluationWithManifoldAndWithGradient) {
|
||||
ceres::GradientProblem problem(new QuadraticTestFunction(),
|
||||
new EuclideanManifold<1>);
|
||||
ceres::GradientProblem problem(std::make_unique<QuadraticTestFunction>(),
|
||||
std::make_unique<EuclideanManifold<1>>());
|
||||
double x = 7.0;
|
||||
double cost = 0;
|
||||
double gradient = 0;
|
||||
|
||||
@@ -52,7 +52,8 @@ class QuadraticFirstOrderFunction : public ceres::FirstOrderFunction {
|
||||
|
||||
TEST(LineSearchMinimizerTest, FinalCostIsZero) {
|
||||
double parameters[1] = {2.0};
|
||||
ceres::GradientProblem problem(new QuadraticFirstOrderFunction);
|
||||
ceres::GradientProblem problem(
|
||||
std::make_unique<QuadraticFirstOrderFunction>());
|
||||
ceres::GradientProblemSolver::Options options;
|
||||
ceres::GradientProblemSolver::Summary summary;
|
||||
ceres::Solve(options, problem, parameters, &summary);
|
||||
|
||||
@@ -52,8 +52,7 @@ class QuadraticCostFunctor {
|
||||
|
||||
TEST(NumericDiffFirstOrderFunction, BilinearDifferentiationTestStatic) {
|
||||
auto function = std::make_unique<
|
||||
NumericDiffFirstOrderFunction<QuadraticCostFunctor, CENTRAL, 4>>(
|
||||
new QuadraticCostFunctor(1.0));
|
||||
NumericDiffFirstOrderFunction<QuadraticCostFunctor, CENTRAL, 4>>(1.0);
|
||||
|
||||
double parameters[4] = {1.0, 2.0, 3.0, 4.0};
|
||||
double gradient[4];
|
||||
@@ -77,7 +76,7 @@ TEST(NumericDiffFirstOrderFunction, BilinearDifferentiationTestStatic) {
|
||||
TEST(NumericDiffFirstOrderFunction, BilinearDifferentiationTestDynamic) {
|
||||
auto function = std::make_unique<
|
||||
NumericDiffFirstOrderFunction<QuadraticCostFunctor, CENTRAL>>(
|
||||
new QuadraticCostFunctor(1.0), 4);
|
||||
std::make_unique<QuadraticCostFunctor>(1.0), 4);
|
||||
|
||||
double parameters[4] = {1.0, 2.0, 3.0, 4.0};
|
||||
double gradient[4];
|
||||
|
||||
Reference in New Issue
Block a user