mirror of
https://github.com/ceres-solver/ceres-solver.git
synced 2026-08-30 17:10:38 +08:00
Simplify instantiation of cost functions and their functors
If arguments are passed to a cost function that can be used to construct the functor, the latter will be instantiated by the cost function using std::make_unique to ensure exception safety. This not only avoids static analysis warnings caused by calling new but also spelling the cost functor type name multiple times. Also expand deduction guides for instantiating Dynamic(Auto|Numeric)DiffCostFunction from std::unique_ptr enabled constructor overloads. Finally, make CostFunction default move constructible and assignable but only through derived classes. This in turn allows derived classes to be movable without relying on custom implementations of corresponding operators. Change-Id: Idee8b9871d862bc9f9f8b5a8d0bedc52863e93c0
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
// Ceres Solver - A fast non-linear least squares minimizer
|
||||
// Copyright 2023 Google Inc. All rights reserved.
|
||||
// Copyright 2024 Google Inc. All rights reserved.
|
||||
// http://ceres-solver.org/
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
@@ -149,9 +149,8 @@
|
||||
// The numerically differentiated version of a cost function for a cost function
|
||||
// can be constructed as follows:
|
||||
//
|
||||
// CostFunction* cost_function
|
||||
// = new NumericDiffCostFunction<MyCostFunction, CENTRAL, 1, 4, 8>(
|
||||
// new MyCostFunction(...), TAKE_OWNERSHIP);
|
||||
// auto* cost_function
|
||||
// = new NumericDiffCostFunction<MyCostFunction, CENTRAL, 1, 4, 8>();
|
||||
//
|
||||
// where MyCostFunction has 1 residual and 2 parameter blocks with sizes 4 and 8
|
||||
// respectively. Look at the tests for a more detailed example.
|
||||
@@ -163,6 +162,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "Eigen/Dense"
|
||||
#include "ceres/cost_function.h"
|
||||
@@ -171,7 +171,6 @@
|
||||
#include "ceres/numeric_diff_options.h"
|
||||
#include "ceres/sized_cost_function.h"
|
||||
#include "ceres/types.h"
|
||||
#include "glog/logging.h"
|
||||
|
||||
namespace ceres {
|
||||
|
||||
@@ -187,16 +186,40 @@ class NumericDiffCostFunction final
|
||||
Ownership ownership = TAKE_OWNERSHIP,
|
||||
int num_residuals = kNumResiduals,
|
||||
const NumericDiffOptions& options = NumericDiffOptions())
|
||||
: functor_(functor), ownership_(ownership), options_(options) {
|
||||
if (kNumResiduals == DYNAMIC) {
|
||||
SizedCostFunction<kNumResiduals, Ns...>::set_num_residuals(num_residuals);
|
||||
}
|
||||
}
|
||||
: NumericDiffCostFunction{std::unique_ptr<CostFunctor>{functor},
|
||||
ownership,
|
||||
num_residuals,
|
||||
options} {}
|
||||
|
||||
NumericDiffCostFunction(NumericDiffCostFunction&& other)
|
||||
: functor_(std::move(other.functor_)), ownership_(other.ownership_) {}
|
||||
explicit NumericDiffCostFunction(
|
||||
std::unique_ptr<CostFunctor> functor,
|
||||
int num_residuals = kNumResiduals,
|
||||
const NumericDiffOptions& options = NumericDiffOptions())
|
||||
: NumericDiffCostFunction{
|
||||
std::move(functor), TAKE_OWNERSHIP, num_residuals, options} {}
|
||||
|
||||
virtual ~NumericDiffCostFunction() {
|
||||
// Constructs the CostFunctor on the heap and takes the ownership.
|
||||
// Invocable only if the number of residuals is known at compile-time.
|
||||
template <class... Args,
|
||||
bool kIsDynamic = kNumResiduals == DYNAMIC,
|
||||
std::enable_if_t<!kIsDynamic &&
|
||||
std::is_constructible_v<CostFunctor, Args&&...>>* =
|
||||
nullptr>
|
||||
explicit NumericDiffCostFunction(Args&&... args)
|
||||
// NOTE We explicitly use direct initialization using parentheses instead
|
||||
// of uniform initialization using braces to avoid narrowing conversion
|
||||
// warnings.
|
||||
: NumericDiffCostFunction{
|
||||
std::make_unique<CostFunctor>(std::forward<Args>(args)...),
|
||||
TAKE_OWNERSHIP} {}
|
||||
|
||||
NumericDiffCostFunction(NumericDiffCostFunction&& other) noexcept = default;
|
||||
NumericDiffCostFunction& operator=(NumericDiffCostFunction&& other) noexcept =
|
||||
default;
|
||||
NumericDiffCostFunction(const NumericDiffCostFunction&) = delete;
|
||||
NumericDiffCostFunction& operator=(const NumericDiffCostFunction&) = delete;
|
||||
|
||||
~NumericDiffCostFunction() override {
|
||||
if (ownership_ != TAKE_OWNERSHIP) {
|
||||
functor_.release();
|
||||
}
|
||||
@@ -250,6 +273,16 @@ class NumericDiffCostFunction final
|
||||
const CostFunctor& functor() const { return *functor_; }
|
||||
|
||||
private:
|
||||
explicit NumericDiffCostFunction(std::unique_ptr<CostFunctor> functor,
|
||||
Ownership ownership,
|
||||
[[maybe_unused]] int num_residuals,
|
||||
const NumericDiffOptions& options)
|
||||
: functor_(std::move(functor)), ownership_(ownership), options_(options) {
|
||||
if constexpr (kNumResiduals == DYNAMIC) {
|
||||
SizedCostFunction<kNumResiduals, Ns...>::set_num_residuals(num_residuals);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<CostFunctor> functor_;
|
||||
Ownership ownership_;
|
||||
NumericDiffOptions options_;
|
||||
|
||||
Reference in New Issue
Block a user