20 #include <unordered_set> 22 #include "cartographer/mapping/proto/connected_components.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) {
58 it->second =
FindSet(it->second);
64 const int trajectory_id_b) {
65 if (trajectory_id_a == trajectory_id_b) {
71 if (forest_.count(trajectory_id_a) == 0 ||
72 forest_.count(trajectory_id_b) == 0) {
80 std::unordered_map<int, std::vector<int>> map;
82 for (
const auto& trajectory_id_entry : forest_) {
83 map[
FindSet(trajectory_id_entry.first)].push_back(
84 trajectory_id_entry.first);
87 std::vector<std::vector<int>> result;
88 result.reserve(map.size());
89 for (
auto& pair : map) {
90 result.emplace_back(std::move(pair.second));
97 const int set_id =
FindSet(trajectory_id);
98 std::vector<int> trajectory_ids;
99 for (
const auto& entry : forest_) {
100 if (
FindSet(entry.first) == set_id) {
101 trajectory_ids.push_back(entry.first);
104 return trajectory_ids;
108 const int trajectory_id_b) {
111 connection_map_.find(std::minmax(trajectory_id_a, trajectory_id_b));
112 return it != connection_map_.end() ? it->second : 0;
116 std::vector<std::vector<int>> connected_components) {
117 proto::ConnectedComponents proto;
118 for (
auto& connected_component : connected_components) {
119 std::sort(connected_component.begin(), connected_component.end());
121 std::sort(connected_components.begin(), connected_components.end());
122 for (
const auto& connected_component : connected_components) {
123 auto* proto_connected_component = proto.add_connected_component();
124 for (
const int trajectory_id : connected_component) {
125 proto_connected_component->add_trajectory_id(trajectory_id);
bool TransitivelyConnected(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)
std::vector< int > GetComponent(int trajectory_id) EXCLUDES(lock_)
std::vector< std::vector< int > > Components() EXCLUDES(lock_)
int ConnectionCount(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)
int FindSet(int trajectory_id) REQUIRES(lock_)
void Connect(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)
void Add(int trajectory_id) EXCLUDES(lock_)
proto::MapLimits ToProto(const MapLimits &map_limits)
void Union(int trajectory_id_a, int trajectory_id_b) REQUIRES(lock_)
Mutex::Locker MutexLocker