VariableIndex-inl.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
18 #pragma once
19 
21 #include <gtsam/base/timing.h>
22 
23 namespace gtsam {
24 
25 /* ************************************************************************* */
26 template<class FG>
27 void VariableIndex::augment(const FG& factors, const FactorIndices* newFactorIndices) {
28  gttic(VariableIndex_augment);
29 
30  // Augment index for each factor
31  for (size_t i = 0; i < factors.size(); ++i) {
32  if (factors[i]) {
33  const size_t globalI =
34  newFactorIndices ? (*newFactorIndices)[i] : nFactors_;
35  for(const Key key: *factors[i]) {
36  index_[key].push_back(globalI);
37  ++nEntries_;
38  }
39  }
40 
41  // Increment factor count even if factors are null, to keep indices consistent
42  if (newFactorIndices) {
43  if ((*newFactorIndices)[i] >= nFactors_)
44  nFactors_ = (*newFactorIndices)[i] + 1;
45  } else {
46  ++nFactors_;
47  }
48  }
49 }
50 
51 /* ************************************************************************* */
52 template<typename ITERATOR, class FG>
53 void VariableIndex::remove(ITERATOR firstFactor, ITERATOR lastFactor,
54  const FG& factors) {
55  gttic(VariableIndex_remove);
56 
57  // NOTE: We intentionally do not decrement nFactors_ because the factor
58  // indices need to remain consistent. Removing factors from a factor graph
59  // does not shift the indices of other factors. Also, we keep nFactors_
60  // one greater than the highest-numbered factor referenced in a VariableIndex.
61  ITERATOR factorIndex = firstFactor;
62  size_t i = 0;
63  for (; factorIndex != lastFactor; ++factorIndex, ++i) {
64  if (i >= factors.size())
65  throw std::invalid_argument(
66  "Internal error, requested inconsistent number of factor indices and factors in VariableIndex::remove");
67  if (factors[i]) {
68  for(Key j: *factors[i]) {
69  FactorIndices& factorEntries = internalAt(j);
70  auto entry = std::find(factorEntries.begin(),
71  factorEntries.end(), *factorIndex);
72  if (entry == factorEntries.end())
73  throw std::invalid_argument(
74  "Internal error, indices and factors passed into VariableIndex::remove are not consistent with the existing variable index");
75  factorEntries.erase(entry);
76  --nEntries_;
77  }
78  }
79  }
80 }
81 
82 /* ************************************************************************* */
83 template<typename ITERATOR>
84 void VariableIndex::removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey) {
85  for (ITERATOR key = firstKey; key != lastKey; ++key) {
86  KeyMap::iterator entry = index_.find(*key);
87  if (!entry->second.empty())
88  throw std::invalid_argument(
89  "Asking to remove variables from the variable index that are not unused");
90  index_.erase(entry);
91  }
92 }
93 
94 }
timing.h
Timing utilities.
gtsam::VariableIndex::augment
void augment(const FG &factors, const FactorIndices *newFactorIndices=nullptr)
Definition: VariableIndex-inl.h:27
simple_graph::factors
const GaussianFactorGraph factors
Definition: testJacobianFactor.cpp:213
gtsam::VariableIndex::remove
void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG &factors)
Definition: VariableIndex-inl.h:53
gtsam::VariableIndex::internalAt
const FactorIndices & internalAt(Key variable) const
Internal version of 'at' that asserts existence.
Definition: VariableIndex.h:179
j
std::ptrdiff_t j
Definition: tut_arithmetic_redux_minmax.cpp:2
gtsam::VariableIndex::removeUnusedVariables
void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey)
Remove unused empty variables (in debug mode verifies they are empty).
Definition: VariableIndex-inl.h:84
key
const gtsam::Symbol key('X', 0)
gtsam::VariableIndex::index_
KeyMap index_
Definition: VariableIndex.h:49
gtsam
traits
Definition: SFMdata.h:40
gtsam::VariableIndex::nFactors_
size_t nFactors_
Definition: VariableIndex.h:50
gtsam::VariableIndex::nEntries_
size_t nEntries_
Definition: VariableIndex.h:51
VariableIndex.h
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gttic
#define gttic(label)
Definition: timing.h:295
gtsam::FactorIndices
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:37


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:09:41