From 64000bb747c7407fa4f4dbf80731ba0c5c5bd6d1 Mon Sep 17 00:00:00 2001 From: dellaert Date: Tue, 25 Nov 2014 13:45:59 +0100 Subject: [PATCH] Cleaned up handling of constraints, removed Constrained smart option as was not used, and discovered there was already an "isConstrained" method. --- gtsam/linear/NoiseModel.cpp | 89 ++++++++++++----------------- gtsam/linear/NoiseModel.h | 37 +++++------- gtsam/nonlinear/NonlinearFactor.cpp | 5 +- 3 files changed, 53 insertions(+), 78 deletions(-) diff --git a/gtsam/linear/NoiseModel.cpp b/gtsam/linear/NoiseModel.cpp index 3b9c5d79c..2031a4b73 100644 --- a/gtsam/linear/NoiseModel.cpp +++ b/gtsam/linear/NoiseModel.cpp @@ -16,20 +16,19 @@ * @author Frank Dellaert */ -#include -#include -#include -#include +#include +#include #include #include #include #include -#include -#include +#include +#include +#include +#include -static double inf = std::numeric_limits::infinity(); using namespace std; namespace gtsam { @@ -290,42 +289,42 @@ void Diagonal::WhitenInPlace(Eigen::Block H) const { // Constrained /* ************************************************************************* */ +namespace internal { +// switch precisions and invsigmas to finite value +// TODO: why?? And, why not just ask s==0.0 below ? +static void fix(const Vector& sigmas, Vector& precisions, Vector& invsigmas) { + for (size_t i = 0; i < sigmas.size(); ++i) + if (!std::isfinite(1. / sigmas[i])) { + precisions[i] = 0.0; + invsigmas[i] = 0.0; + } +} +} + /* ************************************************************************* */ Constrained::Constrained(const Vector& sigmas) : Diagonal(sigmas), mu_(repeat(sigmas.size(), 1000.0)) { - for (int i=0; i H) const { - // selective scaling - // Scale row i of H by sigmas[i], basically multiplying H with diag(sigmas) - // Set inf_mask flag is true so that if invsigmas[i] is inf, i.e. sigmas[i] = 0, - // indicating a hard constraint, we leave H's row i in place. - const Vector& _invsigmas = invsigmas(); - for(DenseIndex row = 0; row < _invsigmas.size(); ++row) - if(isfinite(_invsigmas(row))) - H.row(row) *= _invsigmas(row); + for (DenseIndex i=0; i<(DenseIndex)dim_; ++i) + if (!constrained(i)) // if constrained, leave row of H as is + H.row(i) *= invsigmas_(i); } /* ************************************************************************* */ Constrained::shared_ptr Constrained::unit() const { Vector sigmas = ones(dim()); for (size_t i=0; isigmas_(i) == 0.0) + if (constrained(i)) sigmas(i) = 0.0; return MixedSigmas(mu_, sigmas); } @@ -472,7 +457,7 @@ SharedDiagonal Constrained::QR(Matrix& Ab) const { const size_t& j = t.get<0>(); const Vector& rd = t.get<1>(); precisions(i) = t.get<2>(); - if (precisions(i)==inf) mixed = true; + if (constrained(i)) mixed = true; for (size_t j2=0; j2 NoiseModelFactor::linearize( } // TODO pass unwhitened + noise model to Gaussian factor - if (noiseModel_ && noiseModel_->is_constrained()) + using noiseModel::Constrained; + if (noiseModel_ && noiseModel_->isConstrained()) return GaussianFactor::shared_ptr( new JacobianFactor(terms, b, - boost::static_pointer_cast(noiseModel_)->unit())); + boost::static_pointer_cast(noiseModel_)->unit())); else return GaussianFactor::shared_ptr(new JacobianFactor(terms, b)); }