From be5d30c57a6c27192173073d42c3744146ec2340 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sat, 25 Jan 2025 15:10:30 -0500 Subject: [PATCH] rvalue constructor --- gtsam/linear/JacobianFactor-inl.h | 41 ++++++++++++++++++++++--------- gtsam/linear/JacobianFactor.h | 22 +++++++++++------ 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/gtsam/linear/JacobianFactor-inl.h b/gtsam/linear/JacobianFactor-inl.h index 6c4cb969a..c1ef25520 100644 --- a/gtsam/linear/JacobianFactor-inl.h +++ b/gtsam/linear/JacobianFactor-inl.h @@ -30,26 +30,43 @@ namespace gtsam { } /* ************************************************************************* */ - template - JacobianFactor::JacobianFactor( - const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix, const SharedDiagonal& model) : - Base(keys), Ab_(augmentedMatrix) - { + template + JacobianFactor::JacobianFactor(const KEYS& keys, + const VerticalBlockMatrix& augmentedMatrix, + const SharedDiagonal& model) + : Base(keys), Ab_(augmentedMatrix) { + checkAndAssignModel(model, augmentedMatrix); + } + + /* ************************************************************************* */ + template + JacobianFactor::JacobianFactor(const KEYS& keys, + VerticalBlockMatrix&& augmentedMatrix, + const SharedDiagonal& model) + : Base(keys), Ab_(std::move(augmentedMatrix)) { + checkAndAssignModel(model, Ab_); + } + + /* ************************************************************************* */ + void JacobianFactor::checkAndAssignModel( + const SharedDiagonal& model, const VerticalBlockMatrix& augmentedMatrix) { // Check noise model dimension - if(model && (DenseIndex)model->dim() != augmentedMatrix.rows()) + if (model && (DenseIndex)model->dim() != augmentedMatrix.rows()) throw InvalidNoiseModel(augmentedMatrix.rows(), model->dim()); // Check number of variables - if((DenseIndex)Base::keys_.size() != augmentedMatrix.nBlocks() - 1) + if ((DenseIndex)Base::keys_.size() != augmentedMatrix.nBlocks() - 1) throw std::invalid_argument( - "Error in JacobianFactor constructor input. Number of provided keys plus\n" - "one for the RHS vector must equal the number of provided matrix blocks."); + "Error in JacobianFactor constructor input. Number of provided keys " + "plus one for the RHS vector must equal the number of provided " + "matrix blocks."); // Check RHS dimension - if(augmentedMatrix(augmentedMatrix.nBlocks() - 1).cols() != 1) + if (augmentedMatrix(augmentedMatrix.nBlocks() - 1).cols() != 1) throw std::invalid_argument( - "Error in JacobianFactor constructor input. The last provided matrix block\n" - "must be the RHS vector, but the last provided block had more than one column."); + "Error in JacobianFactor constructor input. The last provided " + "matrix block must be the RHS vector, but the last provided block " + "had more than one column."); // Take noise model model_ = model; diff --git a/gtsam/linear/JacobianFactor.h b/gtsam/linear/JacobianFactor.h index a9933374f..1e82eb051 100644 --- a/gtsam/linear/JacobianFactor.h +++ b/gtsam/linear/JacobianFactor.h @@ -145,13 +145,17 @@ namespace gtsam { template JacobianFactor(const TERMS& terms, const Vector& b, const SharedDiagonal& model = SharedDiagonal()); - /** Constructor with arbitrary number keys, and where the augmented matrix is given all together - * instead of in block terms. Note that only the active view of the provided augmented matrix - * is used, and that the matrix data is copied into a newly-allocated matrix in the constructed - * factor. */ - template - JacobianFactor( - const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix, const SharedDiagonal& sigmas = SharedDiagonal()); + /** Constructor with arbitrary number keys, and where the augmented matrix + * is given all together instead of in block terms. + */ + template + JacobianFactor(const KEYS& keys, const VerticalBlockMatrix& augmentedMatrix, + const SharedDiagonal& sigmas = SharedDiagonal()); + + /** Construct with an rvalue VerticalBlockMatrix, to allow std::move. */ + template + JacobianFactor(const KEYS& keys, VerticalBlockMatrix&& augmentedMatrix, + const SharedDiagonal& model); /** * Build a dense joint factor from all the factors in a factor graph. If a VariableSlots @@ -398,6 +402,10 @@ namespace gtsam { template void fillTerms(const TERMS& terms, const Vector& b, const SharedDiagonal& noiseModel); + /// Common code between VerticalBlockMatrix constructors + void checkAndAssignModel(const SharedDiagonal& model, + const VerticalBlockMatrix& augmentedMatrix); + private: /**