id.h
Go to the documentation of this file.
1 /*
2  * Copyright 2016 The Cartographer Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef CARTOGRAPHER_MAPPING_ID_H_
18 #define CARTOGRAPHER_MAPPING_ID_H_
19 
20 #include <algorithm>
21 #include <ostream>
22 #include <tuple>
23 #include <vector>
24 
25 namespace cartographer {
26 namespace mapping {
27 
28 // Uniquely identifies a trajectory node using a combination of a unique
29 // trajectory ID and a zero-based index of the node inside that trajectory.
30 struct NodeId {
33 
34  bool operator<(const NodeId& other) const {
35  return std::forward_as_tuple(trajectory_id, node_index) <
36  std::forward_as_tuple(other.trajectory_id, other.node_index);
37  }
38 };
39 
40 inline std::ostream& operator<<(std::ostream& os, const NodeId& v) {
41  return os << "(" << v.trajectory_id << ", " << v.node_index << ")";
42 }
43 
44 // Uniquely identifies a submap using a combination of a unique trajectory ID
45 // and a zero-based index of the submap inside that trajectory.
46 struct SubmapId {
49 
50  bool operator==(const SubmapId& other) const {
51  return std::forward_as_tuple(trajectory_id, submap_index) ==
52  std::forward_as_tuple(other.trajectory_id, other.submap_index);
53  }
54 
55  bool operator!=(const SubmapId& other) const { return !operator==(other); }
56 
57  bool operator<(const SubmapId& other) const {
58  return std::forward_as_tuple(trajectory_id, submap_index) <
59  std::forward_as_tuple(other.trajectory_id, other.submap_index);
60  }
61 };
62 
63 inline std::ostream& operator<<(std::ostream& os, const SubmapId& v) {
64  return os << "(" << v.trajectory_id << ", " << v.submap_index << ")";
65 }
66 
67 template <typename ValueType, typename IdType>
69  public:
70  // Appends data to a trajectory, creating trajectories as needed.
71  IdType Append(int trajectory_id, const ValueType& value) {
72  data_.resize(std::max<size_t>(data_.size(), trajectory_id + 1));
73  const IdType id{trajectory_id,
74  static_cast<int>(data_[trajectory_id].size())};
75  data_[trajectory_id].push_back(value);
76  return id;
77  }
78 
79  const ValueType& at(const IdType& id) const {
80  return data_.at(id.trajectory_id).at(GetIndex(id));
81  }
82  ValueType& at(const IdType& id) {
83  return data_.at(id.trajectory_id).at(GetIndex(id));
84  }
85 
86  int num_trajectories() const { return static_cast<int>(data_.size()); }
87  int num_indices(int trajectory_id) const {
88  return static_cast<int>(data_.at(trajectory_id).size());
89  }
90 
91  // TODO(whess): Remove once no longer needed.
92  const std::vector<std::vector<ValueType>> data() const { return data_; }
93 
94  private:
95  static int GetIndex(const NodeId& id) { return id.node_index; }
96  static int GetIndex(const SubmapId& id) { return id.submap_index; }
97 
98  std::vector<std::vector<ValueType>> data_;
99 };
100 
101 } // namespace mapping
102 } // namespace cartographer
103 
104 #endif // CARTOGRAPHER_MAPPING_ID_H_
ValueType & at(const IdType &id)
Definition: id.h:82
static int GetIndex(const SubmapId &id)
Definition: id.h:96
int num_indices(int trajectory_id) const
Definition: id.h:87
bool operator<(const SubmapId &other) const
Definition: id.h:57
const ValueType & at(const IdType &id) const
Definition: id.h:79
bool operator!=(const SubmapId &other) const
Definition: id.h:55
static int GetIndex(const NodeId &id)
Definition: id.h:95
IdType Append(int trajectory_id, const ValueType &value)
Definition: id.h:71
bool operator==(const SubmapId &other) const
Definition: id.h:50
bool operator<(const NodeId &other) const
Definition: id.h:34
float value
std::vector< std::vector< ValueType > > data_
Definition: id.h:98
std::ostream & operator<<(std::ostream &os, const NodeId &v)
Definition: id.h:40
const std::vector< std::vector< ValueType > > data() const
Definition: id.h:92


cartographer
Author(s):
autogenerated on Mon Jun 10 2019 12:51:38