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 }
const gtsam::Symbol key('X', 0)
const GaussianFactorGraph factors
void augment(const FG &factors, const FactorIndices *newFactorIndices=nullptr)
FastVector< FactorIndex > FactorIndices
Define collection types:
Definition: Factor.h:36
#define gttic(label)
Definition: timing.h:295
void remove(ITERATOR firstFactor, ITERATOR lastFactor, const FG &factors)
const FactorIndices & internalAt(Key variable) const
Internal version of &#39;at&#39; that asserts existence.
traits
Definition: chartTesting.h:28
void removeUnusedVariables(ITERATOR firstKey, ITERATOR lastKey)
Remove unused empty variables (in debug mode verifies they are empty).
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102
std::ptrdiff_t j
Timing utilities.


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:43