From e4577dd6d99207493c13579da0bfd7da58904d8e Mon Sep 17 00:00:00 2001 From: Sameer Agarwal Date: Sat, 13 Jul 2019 11:19:27 +0200 Subject: [PATCH] Use override instead of virtual for subclasses. Now that we are using c++11, it is safer to use override in subclasses instead of virtual. This CL does it for the interface, a follow up CL will do it for other parts of the code base. Change-Id: Ice8d0f4355cb700019d7a9c1566fbff0099e97d6 --- include/ceres/autodiff_cost_function.h | 13 ++- include/ceres/autodiff_first_order_function.h | 8 +- .../ceres/autodiff_local_parameterization.h | 21 ++-- include/ceres/conditioned_cost_function.h | 6 +- include/ceres/cost_function_to_functor.h | 6 +- .../ceres/dynamic_autodiff_cost_function.h | 6 +- .../ceres/dynamic_cost_function_to_functor.h | 6 +- .../dynamic_numeric_diff_cost_function.h | 6 +- include/ceres/local_parameterization.h | 98 +++++++++---------- include/ceres/loss_function.h | 52 +++++----- include/ceres/normal_prior.h | 7 +- include/ceres/numeric_diff_cost_function.h | 6 +- 12 files changed, 114 insertions(+), 121 deletions(-) diff --git a/include/ceres/autodiff_cost_function.h b/include/ceres/autodiff_cost_function.h index db3f6af6d..667e912cc 100644 --- a/include/ceres/autodiff_cost_function.h +++ b/include/ceres/autodiff_cost_function.h @@ -181,16 +181,15 @@ class AutoDiffCostFunction : public SizedCostFunction { // // To handle variadic cost functions, some template magic is needed. It's // mostly hidden inside autodiff.h. - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const { + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override { using ParameterDims = typename SizedCostFunction::ParameterDims; if (!jacobians) { - return internal::VariadicEvaluate(*functor_, - parameters, - residuals); + return internal::VariadicEvaluate( + *functor_, parameters, residuals); } return internal::AutoDifferentiate( *functor_, @@ -198,7 +197,7 @@ class AutoDiffCostFunction : public SizedCostFunction { SizedCostFunction::num_residuals(), residuals, jacobians); - } + }; private: std::unique_ptr functor_; diff --git a/include/ceres/autodiff_first_order_function.h b/include/ceres/autodiff_first_order_function.h index 65b7d0fc8..c5bde6242 100644 --- a/include/ceres/autodiff_first_order_function.h +++ b/include/ceres/autodiff_first_order_function.h @@ -113,9 +113,9 @@ class AutoDiffFirstOrderFunction : public FirstOrderFunction { virtual ~AutoDiffFirstOrderFunction() {} - virtual bool Evaluate(const double* const parameters, - double* cost, - double* gradient) const { + bool Evaluate(const double* const parameters, + double* cost, + double* gradient) const override { if (gradient == nullptr) { return (*functor_)(parameters, cost); } @@ -141,7 +141,7 @@ class AutoDiffFirstOrderFunction : public FirstOrderFunction { return true; } - int NumParameters() const { return kNumParameters; } + int NumParameters() const override { return kNumParameters; } private: std::unique_ptr functor_; diff --git a/include/ceres/autodiff_local_parameterization.h b/include/ceres/autodiff_local_parameterization.h index 649e05dc9..455e9bba7 100644 --- a/include/ceres/autodiff_local_parameterization.h +++ b/include/ceres/autodiff_local_parameterization.h @@ -107,21 +107,20 @@ namespace ceres { template class AutoDiffLocalParameterization : public LocalParameterization { public: - AutoDiffLocalParameterization() : - functor_(new Functor()) {} + AutoDiffLocalParameterization() : functor_(new Functor()) {} // Takes ownership of functor. - explicit AutoDiffLocalParameterization(Functor* functor) : - functor_(functor) {} + explicit AutoDiffLocalParameterization(Functor* functor) + : functor_(functor) {} virtual ~AutoDiffLocalParameterization() {} - virtual bool Plus(const double* x, - const double* delta, - double* x_plus_delta) const { + bool Plus(const double* x, + const double* delta, + double* x_plus_delta) const override { return (*functor_)(x, delta, x_plus_delta); } - virtual bool ComputeJacobian(const double* x, double* jacobian) const { + bool ComputeJacobian(const double* x, double* jacobian) const override { double zero_delta[kLocalSize]; for (int i = 0; i < kLocalSize; ++i) { zero_delta[i] = 0.0; @@ -133,14 +132,14 @@ class AutoDiffLocalParameterization : public LocalParameterization { } const double* parameter_ptrs[2] = {x, zero_delta}; - double* jacobian_ptrs[2] = { NULL, jacobian }; + double* jacobian_ptrs[2] = {NULL, jacobian}; return internal::AutoDifferentiate< internal::StaticParameterDims>( *functor_, parameter_ptrs, kGlobalSize, x_plus_delta, jacobian_ptrs); } - virtual int GlobalSize() const { return kGlobalSize; } - virtual int LocalSize() const { return kLocalSize; } + int GlobalSize() const override { return kGlobalSize; } + int LocalSize() const override { return kLocalSize; } private: std::unique_ptr functor_; diff --git a/include/ceres/conditioned_cost_function.h b/include/ceres/conditioned_cost_function.h index f92787e8c..54a2e5962 100644 --- a/include/ceres/conditioned_cost_function.h +++ b/include/ceres/conditioned_cost_function.h @@ -84,9 +84,9 @@ class CERES_EXPORT ConditionedCostFunction : public CostFunction { Ownership ownership); virtual ~ConditionedCostFunction(); - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const; + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override; private: std::unique_ptr wrapped_cost_function_; diff --git a/include/ceres/cost_function_to_functor.h b/include/ceres/cost_function_to_functor.h index 678c80cd1..32ed2776f 100644 --- a/include/ceres/cost_function_to_functor.h +++ b/include/ceres/cost_function_to_functor.h @@ -38,9 +38,9 @@ // class IntrinsicProjection : public SizedCostFunction<2, 5, 3> { // public: // IntrinsicProjection(const double* observation); -// virtual bool Evaluate(double const* const* parameters, -// double* residuals, -// double** jacobians) const; +// bool Evaluate(double const* const* parameters, +// double* residuals, +// double** jacobians) const override; // }; // // is a cost function that implements the projection of a point in its diff --git a/include/ceres/dynamic_autodiff_cost_function.h b/include/ceres/dynamic_autodiff_cost_function.h index 4c1517ad3..1b60b4f38 100644 --- a/include/ceres/dynamic_autodiff_cost_function.h +++ b/include/ceres/dynamic_autodiff_cost_function.h @@ -83,9 +83,9 @@ class DynamicAutoDiffCostFunction : public DynamicCostFunction { virtual ~DynamicAutoDiffCostFunction() {} - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const { + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override { CHECK_GT(num_residuals(), 0) << "You must call DynamicAutoDiffCostFunction::SetNumResiduals() " << "before DynamicAutoDiffCostFunction::Evaluate()."; diff --git a/include/ceres/dynamic_cost_function_to_functor.h b/include/ceres/dynamic_cost_function_to_functor.h index 8284dd20b..7ea76ca60 100644 --- a/include/ceres/dynamic_cost_function_to_functor.h +++ b/include/ceres/dynamic_cost_function_to_functor.h @@ -53,9 +53,9 @@ namespace ceres { // class IntrinsicProjection : public CostFunction { // public: // IntrinsicProjection(const double* observation); -// virtual bool Evaluate(double const* const* parameters, -// double* residuals, -// double** jacobians) const; +// bool Evaluate(double const* const* parameters, +// double* residuals, +// double** jacobians) const override; // }; // // is a cost function that implements the projection of a point in its diff --git a/include/ceres/dynamic_numeric_diff_cost_function.h b/include/ceres/dynamic_numeric_diff_cost_function.h index 33ac5e142..a9d7f14df 100644 --- a/include/ceres/dynamic_numeric_diff_cost_function.h +++ b/include/ceres/dynamic_numeric_diff_cost_function.h @@ -91,9 +91,9 @@ class DynamicNumericDiffCostFunction : public DynamicCostFunction { } } - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const { + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override { using internal::NumericDiff; CHECK_GT(num_residuals(), 0) << "You must call DynamicNumericDiffCostFunction::SetNumResiduals() " diff --git a/include/ceres/local_parameterization.h b/include/ceres/local_parameterization.h index a695638e1..c63790c6a 100644 --- a/include/ceres/local_parameterization.h +++ b/include/ceres/local_parameterization.h @@ -35,6 +35,7 @@ #include #include #include + #include "ceres/internal/disable_warnings.h" #include "ceres/internal/port.h" @@ -155,17 +156,16 @@ class CERES_EXPORT IdentityParameterization : public LocalParameterization { public: explicit IdentityParameterization(int size); virtual ~IdentityParameterization() {} - virtual bool Plus(const double* x, - const double* delta, - double* x_plus_delta) const; - virtual bool ComputeJacobian(const double* x, - double* jacobian) const; - virtual bool MultiplyByJacobian(const double* x, - const int num_cols, - const double* global_matrix, - double* local_matrix) const; - virtual int GlobalSize() const { return size_; } - virtual int LocalSize() const { return size_; } + bool Plus(const double* x, + const double* delta, + double* x_plus_delta) const override; + bool ComputeJacobian(const double* x, double* jacobian) const override; + bool MultiplyByJacobian(const double* x, + const int num_cols, + const double* global_matrix, + double* local_matrix) const override; + int GlobalSize() const override { return size_; } + int LocalSize() const override { return size_; } private: const int size_; @@ -177,19 +177,18 @@ class CERES_EXPORT SubsetParameterization : public LocalParameterization { explicit SubsetParameterization(int size, const std::vector& constant_parameters); virtual ~SubsetParameterization() {} - virtual bool Plus(const double* x, - const double* delta, - double* x_plus_delta) const; - virtual bool ComputeJacobian(const double* x, - double* jacobian) const; - virtual bool MultiplyByJacobian(const double* x, - const int num_cols, - const double* global_matrix, - double* local_matrix) const; - virtual int GlobalSize() const { + bool Plus(const double* x, + const double* delta, + double* x_plus_delta) const override; + bool ComputeJacobian(const double* x, double* jacobian) const override; + bool MultiplyByJacobian(const double* x, + const int num_cols, + const double* global_matrix, + double* local_matrix) const override; + int GlobalSize() const override { return static_cast(constancy_mask_.size()); } - virtual int LocalSize() const { return local_size_; } + int LocalSize() const override { return local_size_; } private: const int local_size_; @@ -203,13 +202,12 @@ class CERES_EXPORT SubsetParameterization : public LocalParameterization { class CERES_EXPORT QuaternionParameterization : public LocalParameterization { public: virtual ~QuaternionParameterization() {} - virtual bool Plus(const double* x, - const double* delta, - double* x_plus_delta) const; - virtual bool ComputeJacobian(const double* x, - double* jacobian) const; - virtual int GlobalSize() const { return 4; } - virtual int LocalSize() const { return 3; } + bool Plus(const double* x, + const double* delta, + double* x_plus_delta) const override; + bool ComputeJacobian(const double* x, double* jacobian) const override; + int GlobalSize() const override { return 4; } + int LocalSize() const override { return 3; } }; // Implements the quaternion local parameterization for Eigen's representation @@ -227,12 +225,12 @@ class CERES_EXPORT EigenQuaternionParameterization : public ceres::LocalParameterization { public: virtual ~EigenQuaternionParameterization() {} - virtual bool Plus(const double* x, - const double* delta, - double* x_plus_delta) const; - virtual bool ComputeJacobian(const double* x, double* jacobian) const; - virtual int GlobalSize() const { return 4; } - virtual int LocalSize() const { return 3; } + bool Plus(const double* x, + const double* delta, + double* x_plus_delta) const override; + bool ComputeJacobian(const double* x, double* jacobian) const override; + int GlobalSize() const override { return 4; } + int LocalSize() const override { return 3; } }; // This provides a parameterization for homogeneous vectors which are commonly @@ -248,18 +246,17 @@ class CERES_EXPORT EigenQuaternionParameterization // remain on the sphere. We assume that the last element of x is the scalar // component. The size of the homogeneous vector is required to be greater than // 1. -class CERES_EXPORT HomogeneousVectorParameterization : - public LocalParameterization { +class CERES_EXPORT HomogeneousVectorParameterization + : public LocalParameterization { public: explicit HomogeneousVectorParameterization(int size); virtual ~HomogeneousVectorParameterization() {} - virtual bool Plus(const double* x, - const double* delta, - double* x_plus_delta) const; - virtual bool ComputeJacobian(const double* x, - double* jacobian) const; - virtual int GlobalSize() const { return size_; } - virtual int LocalSize() const { return size_ - 1; } + bool Plus(const double* x, + const double* delta, + double* x_plus_delta) const override; + bool ComputeJacobian(const double* x, double* jacobian) const override; + int GlobalSize() const override { return size_; } + int LocalSize() const override { return size_ - 1; } private: const int size_; @@ -314,13 +311,12 @@ class CERES_EXPORT ProductParameterization : public LocalParameterization { } } - virtual bool Plus(const double* x, - const double* delta, - double* x_plus_delta) const; - virtual bool ComputeJacobian(const double* x, - double* jacobian) const; - virtual int GlobalSize() const { return global_size_; } - virtual int LocalSize() const { return local_size_; } + bool Plus(const double* x, + const double* delta, + double* x_plus_delta) const override; + bool ComputeJacobian(const double* x, double* jacobian) const override; + int GlobalSize() const override { return global_size_; } + int LocalSize() const override { return local_size_; } private: std::vector> local_params_; diff --git a/include/ceres/loss_function.h b/include/ceres/loss_function.h index 97a70b6ad..e494ef00c 100644 --- a/include/ceres/loss_function.h +++ b/include/ceres/loss_function.h @@ -76,9 +76,10 @@ #define CERES_PUBLIC_LOSS_FUNCTION_H_ #include -#include "glog/logging.h" -#include "ceres/types.h" + #include "ceres/internal/disable_warnings.h" +#include "ceres/types.h" +#include "glog/logging.h" namespace ceres { @@ -118,7 +119,6 @@ class CERES_EXPORT LossFunction { // Note: in the region of interest (i.e. s < 3) we have: // TrivialLoss >= HuberLoss >= SoftLOneLoss >= CauchyLoss - // This corresponds to no robustification. // // rho(s) = s @@ -130,7 +130,7 @@ class CERES_EXPORT LossFunction { // thing. class CERES_EXPORT TrivialLoss : public LossFunction { public: - virtual void Evaluate(double, double*) const; + void Evaluate(double, double*) const override; }; // Scaling @@ -173,8 +173,8 @@ class CERES_EXPORT TrivialLoss : public LossFunction { // http://en.wikipedia.org/wiki/Huber_Loss_Function class CERES_EXPORT HuberLoss : public LossFunction { public: - explicit HuberLoss(double a) : a_(a), b_(a * a) { } - virtual void Evaluate(double, double*) const; + explicit HuberLoss(double a) : a_(a), b_(a * a) {} + void Evaluate(double, double*) const override; private: const double a_; @@ -189,8 +189,8 @@ class CERES_EXPORT HuberLoss : public LossFunction { // At s = 0: rho = [0, 1, -1/2]. class CERES_EXPORT SoftLOneLoss : public LossFunction { public: - explicit SoftLOneLoss(double a) : b_(a * a), c_(1 / b_) { } - virtual void Evaluate(double, double*) const; + explicit SoftLOneLoss(double a) : b_(a * a), c_(1 / b_) {} + void Evaluate(double, double*) const override; private: // b = a^2. @@ -206,8 +206,8 @@ class CERES_EXPORT SoftLOneLoss : public LossFunction { // At s = 0: rho = [0, 1, -1]. class CERES_EXPORT CauchyLoss : public LossFunction { public: - explicit CauchyLoss(double a) : b_(a * a), c_(1 / b_) { } - virtual void Evaluate(double, double*) const; + explicit CauchyLoss(double a) : b_(a * a), c_(1 / b_) {} + void Evaluate(double, double*) const override; private: // b = a^2. @@ -227,8 +227,8 @@ class CERES_EXPORT CauchyLoss : public LossFunction { // At s = 0: rho = [0, 1, 0]. class CERES_EXPORT ArctanLoss : public LossFunction { public: - explicit ArctanLoss(double a) : a_(a), b_(1 / (a * a)) { } - virtual void Evaluate(double, double*) const; + explicit ArctanLoss(double a) : a_(a), b_(1 / (a * a)) {} + void Evaluate(double, double*) const override; private: const double a_; @@ -267,7 +267,7 @@ class CERES_EXPORT ArctanLoss : public LossFunction { class CERES_EXPORT TolerantLoss : public LossFunction { public: explicit TolerantLoss(double a, double b); - virtual void Evaluate(double, double*) const; + void Evaluate(double, double*) const override; private: const double a_, b_, c_; @@ -284,8 +284,8 @@ class CERES_EXPORT TolerantLoss : public LossFunction { // At s = 0: rho = [0, 0.5, -1 / a^2] class CERES_EXPORT TukeyLoss : public ceres::LossFunction { public: - explicit TukeyLoss(double a) : a_squared_(a * a) { } - virtual void Evaluate(double, double*) const; + explicit TukeyLoss(double a) : a_squared_(a * a) {} + void Evaluate(double, double*) const override; private: const double a_squared_; @@ -296,10 +296,12 @@ class CERES_EXPORT TukeyLoss : public ceres::LossFunction { // The loss functions must not be NULL. class CERES_EXPORT ComposedLoss : public LossFunction { public: - explicit ComposedLoss(const LossFunction* f, Ownership ownership_f, - const LossFunction* g, Ownership ownership_g); + explicit ComposedLoss(const LossFunction* f, + Ownership ownership_f, + const LossFunction* g, + Ownership ownership_g); virtual ~ComposedLoss(); - virtual void Evaluate(double, double*) const; + void Evaluate(double, double*) const override; private: std::unique_ptr f_, g_; @@ -328,8 +330,8 @@ class CERES_EXPORT ScaledLoss : public LossFunction { // Constructs a ScaledLoss wrapping another loss function. Takes // ownership of the wrapped loss function or not depending on the // ownership parameter. - ScaledLoss(const LossFunction* rho, double a, Ownership ownership) : - rho_(rho), a_(a), ownership_(ownership) { } + ScaledLoss(const LossFunction* rho, double a, Ownership ownership) + : rho_(rho), a_(a), ownership_(ownership) {} ScaledLoss(const ScaledLoss&) = delete; void operator=(const ScaledLoss&) = delete; @@ -338,7 +340,7 @@ class CERES_EXPORT ScaledLoss : public LossFunction { rho_.release(); } } - virtual void Evaluate(double, double*) const; + void Evaluate(double, double*) const override; private: std::unique_ptr rho_; @@ -387,8 +389,7 @@ class CERES_EXPORT ScaledLoss : public LossFunction { class CERES_EXPORT LossFunctionWrapper : public LossFunction { public: LossFunctionWrapper(LossFunction* rho, Ownership ownership) - : rho_(rho), ownership_(ownership) { - } + : rho_(rho), ownership_(ownership) {} LossFunctionWrapper(const LossFunctionWrapper&) = delete; void operator=(const LossFunctionWrapper&) = delete; @@ -399,13 +400,12 @@ class CERES_EXPORT LossFunctionWrapper : public LossFunction { } } - virtual void Evaluate(double sq_norm, double out[3]) const { + void Evaluate(double sq_norm, double out[3]) const override { if (rho_.get() == NULL) { out[0] = sq_norm; out[1] = 1.0; out[2] = 0.0; - } - else { + } else { rho_->Evaluate(sq_norm, out); } } diff --git a/include/ceres/normal_prior.h b/include/ceres/normal_prior.h index cd98b4c84..1fe9f554d 100644 --- a/include/ceres/normal_prior.h +++ b/include/ceres/normal_prior.h @@ -62,10 +62,9 @@ class CERES_EXPORT NormalPrior: public CostFunction { // Check that the number of rows in the vector b are the same as the // number of columns in the matrix A, crash otherwise. NormalPrior(const Matrix& A, const Vector& b); - - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const; + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override; private: Matrix A_; Vector b_; diff --git a/include/ceres/numeric_diff_cost_function.h b/include/ceres/numeric_diff_cost_function.h index 8edf801b2..a0678a0a0 100644 --- a/include/ceres/numeric_diff_cost_function.h +++ b/include/ceres/numeric_diff_cost_function.h @@ -196,9 +196,9 @@ class NumericDiffCostFunction : public SizedCostFunction { } } - virtual bool Evaluate(double const* const* parameters, - double* residuals, - double** jacobians) const { + bool Evaluate(double const* const* parameters, + double* residuals, + double** jacobians) const override { using internal::FixedArray; using internal::NumericDiff;