connected_components.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_INTERNAL_CONNECTED_COMPONENTS_H_
18 #define CARTOGRAPHER_MAPPING_INTERNAL_CONNECTED_COMPONENTS_H_
19 
20 #include <map>
21 #include <unordered_map>
22 
24 #include "cartographer/mapping/proto/connected_components.pb.h"
26 
27 namespace cartographer {
28 namespace mapping {
29 
30 // A class that tracks the connectivity structure between trajectories.
31 //
32 // Connectivity includes both the count ("How many times have I _directly_
33 // connected trajectories i and j?") and the transitive connectivity.
34 //
35 // This class is thread-safe.
37  public:
39 
42 
43  // Add a trajectory which is initially connected to only itself.
44  void Add(int trajectory_id) EXCLUDES(lock_);
45 
46  // Connect two trajectories. If either trajectory is untracked, it will be
47  // tracked. This function is invariant to the order of its arguments. Repeated
48  // calls to Connect increment the connectivity count.
49  void Connect(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_);
50 
51  // Determines if two trajectories have been (transitively) connected. If
52  // either trajectory is not being tracked, returns false, except when it is
53  // the same trajectory, where it returns true. This function is invariant to
54  // the order of its arguments.
55  bool TransitivelyConnected(int trajectory_id_a, int trajectory_id_b)
56  EXCLUDES(lock_);
57 
58  // Return the number of _direct_ connections between 'trajectory_id_a' and
59  // 'trajectory_id_b'. If either trajectory is not being tracked, returns 0.
60  // This function is invariant to the order of its arguments.
61  int ConnectionCount(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_);
62 
63  // The trajectory IDs, grouped by connectivity.
64  std::vector<std::vector<int>> Components() EXCLUDES(lock_);
65 
66  // The list of trajectory IDs that belong to the same connected component as
67  // 'trajectory_id'.
68  std::vector<int> GetComponent(int trajectory_id) EXCLUDES(lock_);
69 
70  private:
71  // Find the representative and compresses the path to it.
72  int FindSet(int trajectory_id) REQUIRES(lock_);
73  void Union(int trajectory_id_a, int trajectory_id_b) REQUIRES(lock_);
74 
75  common::Mutex lock_;
76  // Tracks transitive connectivity using a disjoint set forest, i.e. each
77  // entry points towards the representative for the given trajectory.
78  std::map<int, int> forest_ GUARDED_BY(lock_);
79  // Tracks the number of direct connections between a pair of trajectories.
80  std::map<std::pair<int, int>, int> connection_map_ GUARDED_BY(lock_);
81 };
82 
83 // Returns a proto encoding connected components.
84 proto::ConnectedComponents ToProto(
85  std::vector<std::vector<int>> connected_components);
86 
87 } // namespace mapping
88 } // namespace cartographer
89 
90 #endif // CARTOGRAPHER_MAPPING_INTERNAL_CONNECTED_COMPONENTS_H_
bool TransitivelyConnected(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)
std::map< int, int > forest_ GUARDED_BY(lock_)
std::vector< int > GetComponent(int trajectory_id) EXCLUDES(lock_)
#define EXCLUDES(...)
Definition: mutex.h:53
std::vector< std::vector< int > > Components() EXCLUDES(lock_)
int ConnectionCount(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_)
#define REQUIRES(...)
Definition: mutex.h:44
int FindSet(int trajectory_id) REQUIRES(lock_)
ConnectedComponents & operator=(const ConnectedComponents &)=delete
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)
Definition: map_limits.h:92
void Union(int trajectory_id_a, int trajectory_id_b) REQUIRES(lock_)


cartographer
Author(s): The Cartographer Authors
autogenerated on Mon Feb 28 2022 22:00:58