NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
utilities.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 #ifndef NODE_ADDERS_UTILITIES_H_
19 #define NODE_ADDERS_UTILITIES_H_
20 
23 
24 #include <gtsam/inference/Key.h>
25 #include <gtsam/nonlinear/NonlinearFactorGraph.h>
26 
27 namespace node_adders {
28 // Removes the FactorType factor in factors containing each of the provided keys if it exists
29 template <typename FactorType>
30 bool RemoveFactor(const gtsam::KeyVector& keys, gtsam::NonlinearFactorGraph& factors);
31 
32 // Removes the FactorType factor in factors containing each of the keys for nodes at timestamp a and b if it exists.
33 template <typename FactorType, typename NodesType>
34 bool RemoveRelativeFactor(const localization_common::Time timestamp_a, const localization_common::Time timestamp_b,
35  const NodesType& nodes, gtsam::NonlinearFactorGraph& factors);
36 
37 // Returns the covariance from a robust, gaussian shared noise model.
38 gtsam::Matrix Covariance(const gtsam::SharedNoiseModel& robust_gaussian_noise);
39 
40 // Implementation
41 template <typename FactorType>
42 bool RemoveFactor(const gtsam::KeyVector& keys, gtsam::NonlinearFactorGraph& factors) {
43  for (auto factor_it = factors.begin(); factor_it != factors.end(); ++factor_it) {
44  if (!dynamic_cast<FactorType*>(factor_it->get())) continue;
45  bool contains_keys = true;
46  for (const auto& key : keys) {
47  if ((*factor_it)->find(key) == std::end((*factor_it)->keys())) {
48  contains_keys = false;
49  continue;
50  }
51  }
52  if (contains_keys) {
53  factors.erase(factor_it);
54  return true;
55  }
56  }
57  return false;
58 }
59 
60 template <typename FactorType, typename NodesType>
62  const NodesType& nodes, gtsam::NonlinearFactorGraph& factors) {
63  const auto keys_a = nodes.Keys(timestamp_a);
64  if (keys_a.empty()) {
65  LogError("RemoveRelativeFactor: Failed to get keys for timestamp_a.");
66  return false;
67  }
68 
69  const auto keys_b = nodes.Keys(timestamp_b);
70  if (keys_b.empty()) {
71  LogError("RemoveRelativeFactor: Failed to get keys for timestamp_b.");
72  return false;
73  }
74 
75  gtsam::KeyVector combined_keys = keys_a;
76  combined_keys.insert(combined_keys.cend(), keys_b.cbegin(), keys_b.cend());
77  return RemoveFactor<FactorType>(combined_keys, factors);
78 }
79 } // namespace node_adders
80 
81 #endif // NODE_ADDERS_UTILITIES_H_
node_adders::Covariance
gtsam::Matrix Covariance(const gtsam::SharedNoiseModel &robust_gaussian_noise)
Definition: utilities.cc:22
node_adders::RemoveFactor
bool RemoveFactor(const gtsam::KeyVector &keys, gtsam::NonlinearFactorGraph &factors)
Definition: utilities.h:42
logger.h
node_adders::RemoveRelativeFactor
bool RemoveRelativeFactor(const localization_common::Time timestamp_a, const localization_common::Time timestamp_b, const NodesType &nodes, gtsam::NonlinearFactorGraph &factors)
Definition: utilities.h:61
LogError
#define LogError(msg)
Definition: logger.h:55
nodes
Definition: combined_nav_state_nodes.h:24
node_adders
Definition: between_factor_node_adder_model.h:35
time.h
localization_common::Time
double Time
Definition: time.h:23