From 83dd3e6234ce31f6cb051e613437fdee84d2005a Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Thu, 21 Oct 2010 06:35:21 +0000 Subject: [PATCH] Utility functions --- linear/GaussianFactor.cpp | 92 +++++++++++++++++++++++++-------------- linear/GaussianFactor.h | 2 + 2 files changed, 62 insertions(+), 32 deletions(-) diff --git a/linear/GaussianFactor.cpp b/linear/GaussianFactor.cpp index 27567aba6..4c484aaf5 100644 --- a/linear/GaussianFactor.cpp +++ b/linear/GaussianFactor.cpp @@ -593,6 +593,56 @@ struct _RowSource { template GaussianFactor::shared_ptr GaussianFactor::Combine(const FactorGraph&, const GaussianVariableIndex&, const vector&, const vector&, const std::vector >&); template GaussianFactor::shared_ptr GaussianFactor::Combine(const FactorGraph&, const GaussianVariableIndex&, const vector&, const vector&, const std::vector >&); +// Utility function to determine row count and check if any noise models are constrained +// TODO: would be nicer if this function was split and are factorgraph methods +static std::pair rowCount(const FactorGraph& factorGraph, + const vector& factorIndices) +{ + static const bool debug = false; + tic("Combine: count sizes"); + size_t m = 0; + bool constrained = false; + BOOST_FOREACH(const size_t i, factorIndices) + { + assert(factorGraph[i] != NULL); + assert(!factorGraph[i]->keys().empty()); + m += factorGraph[i]->numberOfRows(); + if (debug) cout << "Combining factor " << i << endl; + if (debug) factorGraph[i]->print(" :"); + if (!constrained && factorGraph[i]->isConstrained()) { + constrained = true; + if (debug) std::cout << "Found a constraint!" << std::endl; + } + } + toc("Combine: count sizes"); + return make_pair(m,constrained); +} + +// Determine row and column counts and check if any noise models are constrained +template +static vector columnDimensions( + const GaussianVariableIndex& variableIndex, + const vector& variables) +{ + tic("Combine: count dims"); + static const bool debug = false; + vector dims(variables.size() + 1); + size_t n = 0; + { + size_t j = 0; + BOOST_FOREACH(const Index& var, variables) + { + if (debug) cout << "Have variable " << var << endl; + dims[j] = variableIndex.dim(var); + n += dims[j]; + ++j; + } + dims[j] = 1; + } + toc("Combine: count dims"); + return dims; +} + template GaussianFactor::shared_ptr GaussianFactor::Combine(const FactorGraph& factorGraph, const GaussianVariableIndex& variableIndex, const vector& factorIndices, @@ -604,39 +654,17 @@ GaussianFactor::shared_ptr GaussianFactor::Combine(const FactorGraphkeys_.empty()); - m += factorGraph[i]->numberOfRows(); - if(debug) cout << "Combining factor " << i << endl; - if(debug) factorGraph[i]->print(" :"); - if (!constrained && factorGraph[i]->model_->isConstrained()) { - constrained = true; - if (debug) std::cout << "Found a constraint!" << std::endl; - } - } - size_t dims[variables.size()+1]; - size_t n = 0; - { - size_t j=0; - BOOST_FOREACH(const Index& var, variables) { - if(debug) cout << "Have variable " << var << endl; - dims[j] = variableIndex.dim(var); - n += dims[j]; - ++ j; - } - dims[j] = 1; - } - toc("Combine: count sizes"); + // Determine row count and check if any noise models are constrained + size_t m; bool constrained; + boost::tie(m,constrained) = rowCount(factorGraph,factorIndices); + + // Determine column dimensions + vector dims = columnDimensions(variableIndex,variables); // Allocate return value, the combined factor, the augmented Ab matrix and other arrays tic("Combine: set up empty"); shared_ptr combinedFactor(boost::make_shared()); - combinedFactor->Ab_.copyStructureFrom(ab_type(combinedFactor->matrix_, dims, dims+variables.size()+1, m)); + combinedFactor->Ab_.copyStructureFrom(ab_type(combinedFactor->matrix_, dims.begin(), dims.end(), m)); ublas::noalias(combinedFactor->matrix_) = ublas::zero_matrix(combinedFactor->matrix_.size1(), combinedFactor->matrix_.size2()); combinedFactor->firstNonzeroBlocks_.resize(m); Vector sigmas(m); @@ -653,8 +681,8 @@ GaussianFactor::shared_ptr GaussianFactor::Combine(const FactorGraph rowSources; rowSources.reserve(m); size_t i1 = 0; - BOOST_FOREACH(const size_t factorII, factorIndices) { - const GaussianFactor& factor(*factorGraph[factorII]); + BOOST_FOREACH(const size_t i2, factorIndices) { + const GaussianFactor& factor(*factorGraph[i2]); size_t factorRowI = 0; assert(factor.firstNonzeroBlocks_.size() == factor.numberOfRows()); BOOST_FOREACH(const size_t factorFirstNonzeroVarpos, factor.firstNonzeroBlocks_) { @@ -679,7 +707,7 @@ GaussianFactor::shared_ptr GaussianFactor::Combine(const FactorGraphisConstrained();} + /** * return the number of rows from the b vector * @return a integer with the number of rows from the b vector