NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
values.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 
19 #ifndef NODES_VALUES_H_
20 #define NODES_VALUES_H_
21 
23 
24 #include <gtsam/base/serialization.h>
25 #include <gtsam/nonlinear/Values.h>
26 
27 #include <boost/optional.hpp>
28 
29 namespace nodes {
30 // Wrapper class around GTSAM values.
31 // Use one instance of this per graph and pass the same Values shared_pointer
32 // to Nodes classes used in the same graph.
33 class Values {
34  public:
35  explicit Values(std::shared_ptr<gtsam::Values> values = std::make_shared<gtsam::Values>());
36 
37  template <typename ValueType>
38  boost::optional<ValueType> Value(const gtsam::Key& key) const;
39 
40  // Returns key for newly added value
41  template <typename ValueType>
42  gtsam::Key Add(const ValueType& value);
43 
44  bool Remove(const gtsam::Key& key);
45 
46  // TODO(rsoussan): Test this
47  bool Remove(const gtsam::KeyVector& keys);
48 
49  bool Contains(const gtsam::Key& key) const;
50 
51  size_t size() const;
52 
53  const gtsam::Values& gtsam_values() const { return *values_; }
54 
55  gtsam::Values& gtsam_values() { return *values_; }
56 
57  private:
59  template <class ARCHIVE>
60  void serialize(ARCHIVE& ar, const unsigned int /*version*/);
61 
62  std::shared_ptr<gtsam::Values> values_;
63  gtsam::Key latest_key_;
64 };
65 
66 // Implementation
67 template <typename ValueType>
68 boost::optional<ValueType> Values::Value(const gtsam::Key& key) const {
69  try {
70  return values_->at<ValueType>(key);
71  } catch (...) {
72  return boost::none;
73  }
74 }
75 
76 template <typename ValueType>
77 gtsam::Key Values::Add(const ValueType& value) {
78  // Since latest_key_ is always incremented when a new key is added,
79  // we don't need to worry about it already being in values_.
80  values_->insert(++latest_key_, value);
81  return latest_key_;
82 }
83 
84 template <class ARCHIVE>
85 void Values::serialize(ARCHIVE& ar, const unsigned int /*version*/) {
86  ar& BOOST_SERIALIZATION_NVP(values_);
87  ar& BOOST_SERIALIZATION_NVP(latest_key_);
88 }
89 } // namespace nodes
90 
91 #endif // NODES_VALUES_H_
nodes::Values
Definition: values.h:33
nodes::Values::Values
Values(std::shared_ptr< gtsam::Values > values=std::make_shared< gtsam::Values >())
Definition: values.cc:21
nodes::Values::Add
gtsam::Key Add(const ValueType &value)
Definition: values.h:77
logger.h
nodes
Definition: combined_nav_state_nodes.h:24
nodes::Values::access
friend class boost::serialization::access
Definition: values.h:58
nodes::Values::Contains
bool Contains(const gtsam::Key &key) const
Definition: values.cc:23
nodes::Values::Value
boost::optional< ValueType > Value(const gtsam::Key &key) const
Definition: values.h:68
nodes::Values::gtsam_values
const gtsam::Values & gtsam_values() const
Definition: values.h:53
nodes::Values::Remove
bool Remove(const gtsam::Key &key)
Definition: values.cc:25
nodes::Values::gtsam_values
gtsam::Values & gtsam_values()
Definition: values.h:55
nodes::Values::size
size_t size() const
Definition: values.cc:39