20 #include <unordered_set> 22 #include "cartographer/mapping/proto/trajectory_connectivity.pb.h" 23 #include "glog/logging.h" 29 : lock_(), forest_(), connection_map_() {}
33 forest_.emplace(trajectory_id, trajectory_id);
37 const int trajectory_id_b) {
39 Union(trajectory_id_a, trajectory_id_b);
40 auto sorted_pair = std::minmax(trajectory_id_a, trajectory_id_b);
41 ++connection_map_[sorted_pair];
45 const int trajectory_id_b) {
46 forest_.emplace(trajectory_id_a, trajectory_id_a);
47 forest_.emplace(trajectory_id_b, trajectory_id_b);
48 const int representative_a =
FindSet(trajectory_id_a);
49 const int representative_b =
FindSet(trajectory_id_b);
50 forest_[representative_a] = representative_b;
54 auto it = forest_.find(trajectory_id);
55 CHECK(it != forest_.end());
56 if (it->first != it->second) {
57 it->second =
FindSet(it->second);
63 const int trajectory_id_b) {
66 if (forest_.count(trajectory_id_a) == 0 ||
67 forest_.count(trajectory_id_b) == 0) {
75 std::unordered_map<int, std::vector<int>> map;
77 for (
const auto& trajectory_id_entry : forest_) {
78 map[
FindSet(trajectory_id_entry.first)].push_back(
79 trajectory_id_entry.first);
82 std::vector<std::vector<int>> result;
83 result.reserve(map.size());
84 for (
auto& pair : map) {
85 result.emplace_back(std::move(pair.second));
91 const int trajectory_id_b) {
94 connection_map_.find(std::minmax(trajectory_id_a, trajectory_id_b));
95 return it != connection_map_.end() ? it->second : 0;
99 std::vector<std::vector<int>> connected_components) {
100 proto::TrajectoryConnectivity proto;
101 for (
auto& connected_component : connected_components) {
102 std::sort(connected_component.begin(), connected_component.end());
104 std::sort(connected_components.begin(), connected_components.end());
105 for (
const auto& connected_component : connected_components) {
106 auto* proto_connected_component = proto.add_connected_component();
107 for (
const int trajectory_id : connected_component) {
108 proto_connected_component->add_trajectory_id(trajectory_id);
115 const proto::TrajectoryConnectivity& trajectory_connectivity,
116 const int trajectory_id) {
117 for (
const auto& connected_component :
118 trajectory_connectivity.connected_component()) {
119 if (std::find(connected_component.trajectory_id().begin(),
120 connected_component.trajectory_id().end(),
121 trajectory_id) != connected_component.trajectory_id().end()) {
122 return connected_component;
126 proto::TrajectoryConnectivity::ConnectedComponent connected_component;
127 connected_component.add_trajectory_id(trajectory_id);
128 return connected_component;
void Union(int trajectory_id_a, int trajectory_id_b) REQUIRES(lock_)
void Add(int trajectory_id) EXCLUDES(lock_)
int FindSet(int trajectory_id) REQUIRES(lock_)
bool TransitivelyConnected(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)
proto::TrajectoryConnectivity::ConnectedComponent FindConnectedComponent(const proto::TrajectoryConnectivity &trajectory_connectivity, const int trajectory_id)
void Connect(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)
std::vector< std::vector< int > > ConnectedComponents() EXCLUDES(lock_)
proto::SparsePoseGraph::Constraint::Tag ToProto(const SparsePoseGraph::Constraint::Tag &tag)
Mutex::Locker MutexLocker
int ConnectionCount(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)