Skip to content

Commit

Permalink
Major improvement -> no need to copy keys anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
dellaert committed Sep 27, 2024
1 parent bc555ae commit 2b26b3c
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions gtsam/hybrid/HybridGaussianConditional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,35 @@
#include <gtsam/linear/GaussianBayesNet.h>
#include <gtsam/linear/GaussianFactorGraph.h>

#include <cstddef>

namespace gtsam {
/* *******************************************************************************/
struct HybridGaussianConditional::ConstructorHelper {
KeyVector frontals, parents;
std::optional<size_t> nrFrontals;
HybridGaussianFactor::FactorValuePairs pairs;
double negLogConstant;
/// Compute all variables needed for the private constructor below.
ConstructorHelper(const Conditionals &conditionals) {
negLogConstant = std::numeric_limits<double>::infinity();
double minNegLogConstant;

auto func = [&](const GaussianConditional::shared_ptr &c)
/// Compute all variables needed for the private constructor below.
ConstructorHelper(const Conditionals &conditionals)
: minNegLogConstant(std::numeric_limits<double>::infinity()) {
auto func = [this](const GaussianConditional::shared_ptr &c)
-> GaussianFactorValuePair {
double value = 0.0;
if (c) {
if (frontals.empty()) {
frontals = KeyVector(c->frontals().begin(), c->frontals().end());
parents = KeyVector(c->parents().begin(), c->parents().end());
if (!nrFrontals.has_value()) {
nrFrontals = c->nrFrontals();
}
value = c->negLogConstant();
negLogConstant = std::min(negLogConstant, value);
minNegLogConstant = std::min(minNegLogConstant, value);
}
return {std::dynamic_pointer_cast<GaussianFactor>(c), value};
};
pairs = HybridGaussianFactor::FactorValuePairs(conditionals, func);
if (!nrFrontals.has_value()) {
throw std::runtime_error(
"HybridGaussianConditional: need at least one frontal variable.");
}
}
};

Expand All @@ -60,9 +65,9 @@ HybridGaussianConditional::HybridGaussianConditional(
const HybridGaussianConditional::Conditionals &conditionals,
const ConstructorHelper &helper)
: BaseFactor(discreteParents, helper.pairs),
BaseConditional(helper.frontals.size()),
BaseConditional(*helper.nrFrontals),
conditionals_(conditionals),
negLogConstant_(helper.negLogConstant) {}
negLogConstant_(helper.minNegLogConstant) {}

HybridGaussianConditional::HybridGaussianConditional(
const DiscreteKeys &discreteParents,
Expand Down

0 comments on commit 2b26b3c

Please sign in to comment.