19 #ifndef NODE_ADDERS_BETWEEN_FACTOR_NODE_ADDER_MODEL_H_
20 #define NODE_ADDERS_BETWEEN_FACTOR_NODE_ADDER_MODEL_H_
27 #include <gtsam/inference/Key.h>
28 #include <gtsam/geometry/Pose3.h>
29 #include <gtsam/nonlinear/NonlinearFactorGraph.h>
30 #include <gtsam/slam/BetweenFactor.h>
38 template <
typename NodeType,
typename NodeAdderModelType>
42 using Base = NodeAdderModelType;
46 void AddPriors(
const NodeType& node,
const std::vector<gtsam::SharedNoiseModel>& noise_models,
48 gtsam::NonlinearFactorGraph& factors)
const final;
53 gtsam::NonlinearFactorGraph& factors)
const final;
57 const NodesType&
nodes, gtsam::NonlinearFactorGraph& factors)
const final;
60 const NodesType&
nodes, gtsam::NonlinearFactorGraph& factors)
const final;
66 gtsam::NonlinearFactorGraph& factors)
const;
74 virtual boost::optional<std::pair<NodeType, gtsam::SharedNoiseModel>> RelativeNodeAndNoise(
79 template <
class Archive>
80 void serialize(Archive& ar,
const unsigned int file_version) {
81 ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(
Base);
86 template <
typename NodeType,
typename NodeAdderModelType>
88 const NodeType& node,
const std::vector<gtsam::SharedNoiseModel>& noise_models,
90 const auto keys =
nodes.Keys(timestamp);
92 LogError(
"AddPriors: Failed to get keys.");
96 gtsam::PriorFactor<NodeType> prior_factor(keys[0], node, noise_models[0]);
97 factors.push_back(prior_factor);
100 template <
typename NodeType,
typename NodeAdderModelType>
103 gtsam::NonlinearFactorGraph& factors)
const {
104 if (!
nodes.Contains(timestamp_b)) {
105 const auto keys = AddNode(timestamp_b,
nodes);
107 LogError(
"AddNodesAndRelativeFactors: Failed to add node.");
112 if (!AddRelativeFactors(timestamp_a, timestamp_b,
nodes, factors)) {
113 LogError(
"AddNodesAndRelativeFactors: Failed to add relative factor.");
119 template <
typename NodeType,
typename NodeAdderModelType>
122 gtsam::NonlinearFactorGraph& factors)
const {
123 const auto keys_a =
nodes.Keys(timestamp_a);
124 if (keys_a.empty()) {
125 LogError(
"AddRelativeFactors: Failed to get keys a.");
128 const auto keys_b =
nodes.Keys(timestamp_b);
129 if (keys_b.empty()) {
130 LogError(
"AddRelativeFactors: Failed to get keys b.");
133 if (!AddRelativeFactors(keys_a, timestamp_a, keys_b, timestamp_b, factors)) {
134 LogError(
"AddRelativeFactor: Failed to add relative factor.");
140 template <
typename NodeType,
typename NodeAdderModelType>
144 const auto relative_node_and_noise = RelativeNodeAndNoise(timestamp_a, timestamp_b);
145 if (!relative_node_and_noise) {
146 LogError(
"AddRelativeFactor: Failed to add relative node and noise.");
150 typename gtsam::BetweenFactor<NodeType>::shared_ptr relative_factor(
new gtsam::BetweenFactor<NodeType>(
151 keys_a[0], keys_b[0], relative_node_and_noise->first, relative_node_and_noise->second));
152 factors.push_back(relative_factor);
156 template <
typename NodeType,
typename NodeAdderModelType>
159 gtsam::NonlinearFactorGraph& factors)
const {
160 return RemoveRelativeFactor<gtsam::BetweenFactor<NodeType>>(timestamp_a, timestamp_b,
nodes, factors);
164 template <
typename MeasurementType,
typename NodeType>
168 template <
typename NodeType>
173 #endif // NODE_ADDERS_BETWEEN_FACTOR_NODE_ADDER_MODEL_H_