From 1a367f0549d1adcee6c4c1a38e20d92f0dd188a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20Sch=C3=BCtte?= Date: Thu, 7 Sep 2017 17:11:06 +0200 Subject: [PATCH] Rename TrajectoryConnectivity ConnectedComponents. (#510) * Rename TrajectoryConnectivity ConnectedComponents. --- ...onnectivity.cc => connected_components.cc} | 32 +++++----- ..._connectivity.h => connected_components.h} | 26 ++++---- ...y_test.cc => connected_components_test.cc} | 62 +++++++++---------- ...ivity.proto => connected_components.proto} | 4 +- cartographer/mapping_2d/sparse_pose_graph.cc | 8 +-- cartographer/mapping_2d/sparse_pose_graph.h | 4 +- cartographer/mapping_3d/sparse_pose_graph.cc | 8 +-- cartographer/mapping_3d/sparse_pose_graph.h | 4 +- 8 files changed, 74 insertions(+), 74 deletions(-) rename cartographer/mapping/{trajectory_connectivity.cc => connected_components.cc} (78%) rename cartographer/mapping/{trajectory_connectivity.h => connected_components.h} (80%) rename cartographer/mapping/{trajectory_connectivity_test.cc => connected_components_test.cc} (57%) rename cartographer/mapping/proto/{trajectory_connectivity.proto => connected_components.proto} (90%) diff --git a/cartographer/mapping/trajectory_connectivity.cc b/cartographer/mapping/connected_components.cc similarity index 78% rename from cartographer/mapping/trajectory_connectivity.cc rename to cartographer/mapping/connected_components.cc index 5d9d148..9787316 100644 --- a/cartographer/mapping/trajectory_connectivity.cc +++ b/cartographer/mapping/connected_components.cc @@ -14,26 +14,26 @@ * limitations under the License. */ -#include "cartographer/mapping/trajectory_connectivity.h" +#include "cartographer/mapping/connected_components.h" #include #include -#include "cartographer/mapping/proto/trajectory_connectivity.pb.h" +#include "cartographer/mapping/proto/connected_components.pb.h" #include "glog/logging.h" namespace cartographer { namespace mapping { -TrajectoryConnectivity::TrajectoryConnectivity() +ConnectedComponents::ConnectedComponents() : lock_(), forest_(), connection_map_() {} -void TrajectoryConnectivity::Add(const int trajectory_id) { +void ConnectedComponents::Add(const int trajectory_id) { common::MutexLocker locker(&lock_); forest_.emplace(trajectory_id, trajectory_id); } -void TrajectoryConnectivity::Connect(const int trajectory_id_a, +void ConnectedComponents::Connect(const int trajectory_id_a, const int trajectory_id_b) { common::MutexLocker locker(&lock_); Union(trajectory_id_a, trajectory_id_b); @@ -41,7 +41,7 @@ void TrajectoryConnectivity::Connect(const int trajectory_id_a, ++connection_map_[sorted_pair]; } -void TrajectoryConnectivity::Union(const int trajectory_id_a, +void ConnectedComponents::Union(const int trajectory_id_a, const int trajectory_id_b) { forest_.emplace(trajectory_id_a, trajectory_id_a); forest_.emplace(trajectory_id_b, trajectory_id_b); @@ -50,7 +50,7 @@ void TrajectoryConnectivity::Union(const int trajectory_id_a, forest_[representative_a] = representative_b; } -int TrajectoryConnectivity::FindSet(const int trajectory_id) { +int ConnectedComponents::FindSet(const int trajectory_id) { auto it = forest_.find(trajectory_id); CHECK(it != forest_.end()); if (it->first != it->second) { @@ -59,7 +59,7 @@ int TrajectoryConnectivity::FindSet(const int trajectory_id) { return it->second; } -bool TrajectoryConnectivity::TransitivelyConnected(const int trajectory_id_a, +bool ConnectedComponents::TransitivelyConnected(const int trajectory_id_a, const int trajectory_id_b) { if (trajectory_id_a == trajectory_id_b) { return true; @@ -74,7 +74,7 @@ bool TrajectoryConnectivity::TransitivelyConnected(const int trajectory_id_a, return FindSet(trajectory_id_a) == FindSet(trajectory_id_b); } -std::vector> TrajectoryConnectivity::ConnectedComponents() { +std::vector> ConnectedComponents::Components() { // Map from cluster exemplar -> growing cluster. std::unordered_map> map; common::MutexLocker locker(&lock_); @@ -91,7 +91,7 @@ std::vector> TrajectoryConnectivity::ConnectedComponents() { return result; } -int TrajectoryConnectivity::ConnectionCount(const int trajectory_id_a, +int ConnectedComponents::ConnectionCount(const int trajectory_id_a, const int trajectory_id_b) { common::MutexLocker locker(&lock_); const auto it = @@ -99,9 +99,9 @@ int TrajectoryConnectivity::ConnectionCount(const int trajectory_id_a, return it != connection_map_.end() ? it->second : 0; } -proto::TrajectoryConnectivity ToProto( +proto::ConnectedComponents ToProto( std::vector> connected_components) { - proto::TrajectoryConnectivity proto; + proto::ConnectedComponents proto; for (auto& connected_component : connected_components) { std::sort(connected_component.begin(), connected_component.end()); } @@ -115,11 +115,11 @@ proto::TrajectoryConnectivity ToProto( return proto; } -proto::TrajectoryConnectivity::ConnectedComponent FindConnectedComponent( - const proto::TrajectoryConnectivity& trajectory_connectivity, +proto::ConnectedComponents::ConnectedComponent FindConnectedComponent( + const proto::ConnectedComponents& connected_components, const int trajectory_id) { for (const auto& connected_component : - trajectory_connectivity.connected_component()) { + connected_components.connected_component()) { if (std::find(connected_component.trajectory_id().begin(), connected_component.trajectory_id().end(), trajectory_id) != connected_component.trajectory_id().end()) { @@ -127,7 +127,7 @@ proto::TrajectoryConnectivity::ConnectedComponent FindConnectedComponent( } } - proto::TrajectoryConnectivity::ConnectedComponent connected_component; + proto::ConnectedComponents::ConnectedComponent connected_component; connected_component.add_trajectory_id(trajectory_id); return connected_component; } diff --git a/cartographer/mapping/trajectory_connectivity.h b/cartographer/mapping/connected_components.h similarity index 80% rename from cartographer/mapping/trajectory_connectivity.h rename to cartographer/mapping/connected_components.h index b3a6046..b0ed21d 100644 --- a/cartographer/mapping/trajectory_connectivity.h +++ b/cartographer/mapping/connected_components.h @@ -14,14 +14,14 @@ * limitations under the License. */ -#ifndef CARTOGRAPHER_MAPPING_TRAJECTORY_CONNECTIVITY_H_ -#define CARTOGRAPHER_MAPPING_TRAJECTORY_CONNECTIVITY_H_ +#ifndef CARTOGRAPHER_MAPPING_CONNECTED_COMPONENTS_H_ +#define CARTOGRAPHER_MAPPING_CONNECTED_COMPONENTS_H_ #include #include #include "cartographer/common/mutex.h" -#include "cartographer/mapping/proto/trajectory_connectivity.pb.h" +#include "cartographer/mapping/proto/connected_components.pb.h" #include "cartographer/mapping/submaps.h" namespace cartographer { @@ -33,12 +33,12 @@ namespace mapping { // connected trajectories i and j?") and the transitive connectivity. // // This class is thread-safe. -class TrajectoryConnectivity { +class ConnectedComponents { public: - TrajectoryConnectivity(); + ConnectedComponents(); - TrajectoryConnectivity(const TrajectoryConnectivity&) = delete; - TrajectoryConnectivity& operator=(const TrajectoryConnectivity&) = delete; + ConnectedComponents(const ConnectedComponents&) = delete; + ConnectedComponents& operator=(const ConnectedComponents&) = delete; // Add a trajectory which is initially connected to only itself. void Add(int trajectory_id) EXCLUDES(lock_); @@ -61,7 +61,7 @@ class TrajectoryConnectivity { int ConnectionCount(int trajectory_id_a, int trajectory_id_b) EXCLUDES(lock_); // The trajectory IDs, grouped by connectivity. - std::vector> ConnectedComponents() EXCLUDES(lock_); + std::vector> Components() EXCLUDES(lock_); private: // Find the representative and compresses the path to it. @@ -77,16 +77,16 @@ class TrajectoryConnectivity { }; // Returns a proto encoding connected components. -proto::TrajectoryConnectivity ToProto( +proto::ConnectedComponents ToProto( std::vector> connected_components); // Returns the connected component containing 'trajectory_id'. -proto::TrajectoryConnectivity::ConnectedComponent FindConnectedComponent( - const cartographer::mapping::proto::TrajectoryConnectivity& - trajectory_connectivity, +proto::ConnectedComponents::ConnectedComponent FindConnectedComponent( + const cartographer::mapping::proto::ConnectedComponents& + connected_components, int trajectory_id); } // namespace mapping } // namespace cartographer -#endif // CARTOGRAPHER_MAPPING_TRAJECTORY_CONNECTIVITY_H_ +#endif // CARTOGRAPHER_MAPPING_CONNECTED_COMPONENTS_H_ diff --git a/cartographer/mapping/trajectory_connectivity_test.cc b/cartographer/mapping/connected_components_test.cc similarity index 57% rename from cartographer/mapping/trajectory_connectivity_test.cc rename to cartographer/mapping/connected_components_test.cc index 0c7d75a..999b0b3 100644 --- a/cartographer/mapping/trajectory_connectivity_test.cc +++ b/cartographer/mapping/connected_components_test.cc @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "cartographer/mapping/trajectory_connectivity.h" +#include "cartographer/mapping/connected_components.h" #include #include @@ -28,49 +28,49 @@ namespace { constexpr int kNumTrajectories = 10; -TEST(TrajectoryConnectivityTest, TransitivelyConnected) { - TrajectoryConnectivity trajectory_connectivity; +TEST(ConnectedComponentsTest, TransitivelyConnected) { + ConnectedComponents connected_components; // Make sure nothing's connected until we connect some things. for (int trajectory_a = 0; trajectory_a < kNumTrajectories; ++trajectory_a) { for (int trajectory_b = 0; trajectory_b < kNumTrajectories; ++trajectory_b) { EXPECT_EQ(trajectory_a == trajectory_b, - trajectory_connectivity.TransitivelyConnected(trajectory_a, + connected_components.TransitivelyConnected(trajectory_a, trajectory_b)); } } // Connect some stuff up. - trajectory_connectivity.Connect(0, 1); - EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(0, 1)); - trajectory_connectivity.Connect(8, 9); - EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(8, 9)); - EXPECT_FALSE(trajectory_connectivity.TransitivelyConnected(0, 9)); + connected_components.Connect(0, 1); + EXPECT_TRUE(connected_components.TransitivelyConnected(0, 1)); + connected_components.Connect(8, 9); + EXPECT_TRUE(connected_components.TransitivelyConnected(8, 9)); + EXPECT_FALSE(connected_components.TransitivelyConnected(0, 9)); - trajectory_connectivity.Connect(1, 8); + connected_components.Connect(1, 8); for (int i : {0, 1}) { for (int j : {8, 9}) { - EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(i, j)); + EXPECT_TRUE(connected_components.TransitivelyConnected(i, j)); } } } -TEST(TrajectoryConnectivityTest, EmptyConnectedComponents) { - TrajectoryConnectivity trajectory_connectivity; - auto connections = trajectory_connectivity.ConnectedComponents(); +TEST(ConnectedComponentsTest, EmptyConnectedComponents) { + ConnectedComponents connected_components; + auto connections = connected_components.Components(); EXPECT_EQ(0, connections.size()); } -TEST(TrajectoryConnectivityTest, ConnectedComponents) { - TrajectoryConnectivity trajectory_connectivity; +TEST(ConnectedComponentsTest, ConnectedComponents) { + ConnectedComponents connected_components; for (int i = 0; i <= 4; ++i) { - trajectory_connectivity.Connect(0, i); + connected_components.Connect(0, i); } for (int i = 5; i <= 9; ++i) { - trajectory_connectivity.Connect(5, i); + connected_components.Connect(5, i); } - auto connections = trajectory_connectivity.ConnectedComponents(); + auto connections = connected_components.Components(); ASSERT_EQ(2, connections.size()); // The clustering is arbitrary; we need to figure out which one is which. const std::vector* zero_cluster = nullptr; @@ -91,25 +91,25 @@ TEST(TrajectoryConnectivityTest, ConnectedComponents) { } } -TEST(TrajectoryConnectivityTest, ConnectionCount) { - TrajectoryConnectivity trajectory_connectivity; +TEST(ConnectedComponentsTest, ConnectionCount) { + ConnectedComponents connected_components; for (int i = 0; i < kNumTrajectories; ++i) { - trajectory_connectivity.Connect(0, 1); + connected_components.Connect(0, 1); // Permute the arguments to check invariance. - EXPECT_EQ(i + 1, trajectory_connectivity.ConnectionCount(1, 0)); + EXPECT_EQ(i + 1, connected_components.ConnectionCount(1, 0)); } for (int i = 1; i < 9; ++i) { - EXPECT_EQ(0, trajectory_connectivity.ConnectionCount(i, i + 1)); + EXPECT_EQ(0, connected_components.ConnectionCount(i, i + 1)); } } -TEST(TrajectoryConnectivityTest, ReflexiveConnectivity) { - TrajectoryConnectivity trajectory_connectivity; - EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(0, 0)); - EXPECT_EQ(0, trajectory_connectivity.ConnectionCount(0, 0)); - trajectory_connectivity.Add(0); - EXPECT_TRUE(trajectory_connectivity.TransitivelyConnected(0, 0)); - EXPECT_EQ(0, trajectory_connectivity.ConnectionCount(0, 0)); +TEST(ConnectedComponentsTest, ReflexiveConnectivity) { + ConnectedComponents connected_components; + EXPECT_TRUE(connected_components.TransitivelyConnected(0, 0)); + EXPECT_EQ(0, connected_components.ConnectionCount(0, 0)); + connected_components.Add(0); + EXPECT_TRUE(connected_components.TransitivelyConnected(0, 0)); + EXPECT_EQ(0, connected_components.ConnectionCount(0, 0)); } } // namespace diff --git a/cartographer/mapping/proto/trajectory_connectivity.proto b/cartographer/mapping/proto/connected_components.proto similarity index 90% rename from cartographer/mapping/proto/trajectory_connectivity.proto rename to cartographer/mapping/proto/connected_components.proto index e435a3f..2cdab4f 100644 --- a/cartographer/mapping/proto/trajectory_connectivity.proto +++ b/cartographer/mapping/proto/connected_components.proto @@ -18,10 +18,10 @@ package cartographer.mapping.proto; // This is how proto2 calls the outer class since there is already a message // with the same name in the file. -option java_outer_classname = "TrajectoryConnectivityOuterClass"; +option java_outer_classname = "ConnectedComponentsOuterClass"; // Connectivity structure between trajectories. -message TrajectoryConnectivity { +message ConnectedComponents { message ConnectedComponent { repeated int32 trajectory_id = 1; } diff --git a/cartographer/mapping_2d/sparse_pose_graph.cc b/cartographer/mapping_2d/sparse_pose_graph.cc index 4830b77..c57c8ac 100644 --- a/cartographer/mapping_2d/sparse_pose_graph.cc +++ b/cartographer/mapping_2d/sparse_pose_graph.cc @@ -104,7 +104,7 @@ void SparsePoseGraph::AddScan( trajectory_nodes_.Append( trajectory_id, mapping::TrajectoryNode{constant_data, optimized_pose}); ++num_trajectory_nodes_; - trajectory_connectivity_.Add(trajectory_id); + connected_components_.Add(trajectory_id); // Test if the 'insertion_submap.back()' is one we never saw before. if (trajectory_id >= submap_data_.num_trajectories() || @@ -180,7 +180,7 @@ void SparsePoseGraph::ComputeConstraint(const mapping::NodeId& node_id, submap_id, submap_data_.at(submap_id).submap.get(), node_id, trajectory_nodes_.at(node_id).constant_data.get()); } else { - if (trajectory_connectivity_.TransitivelyConnected( + if (connected_components_.TransitivelyConnected( node_id.trajectory_id, submap_id.trajectory_id)) { const transform::Rigid2d initial_relative_pose = optimization_problem_.submap_data() @@ -314,7 +314,7 @@ void SparsePoseGraph::HandleWorkQueue() { for (const Constraint& constraint : result) { CHECK_EQ(constraint.tag, mapping::SparsePoseGraph::Constraint::INTER_SUBMAP); - trajectory_connectivity_.Connect(constraint.node_id.trajectory_id, + connected_components_.Connect(constraint.node_id.trajectory_id, constraint.submap_id.trajectory_id); } @@ -503,7 +503,7 @@ transform::Rigid3d SparsePoseGraph::GetLocalToGlobalTransform( } std::vector> SparsePoseGraph::GetConnectedTrajectories() { - return trajectory_connectivity_.ConnectedComponents(); + return connected_components_.Components(); } int SparsePoseGraph::num_submaps(const int trajectory_id) { diff --git a/cartographer/mapping_2d/sparse_pose_graph.h b/cartographer/mapping_2d/sparse_pose_graph.h index 79a95a6..c265ace 100644 --- a/cartographer/mapping_2d/sparse_pose_graph.h +++ b/cartographer/mapping_2d/sparse_pose_graph.h @@ -32,9 +32,9 @@ #include "cartographer/common/mutex.h" #include "cartographer/common/thread_pool.h" #include "cartographer/common/time.h" +#include "cartographer/mapping/connected_components.h" #include "cartographer/mapping/pose_graph_trimmer.h" #include "cartographer/mapping/sparse_pose_graph.h" -#include "cartographer/mapping/trajectory_connectivity.h" #include "cartographer/mapping_2d/sparse_pose_graph/constraint_builder.h" #include "cartographer/mapping_2d/sparse_pose_graph/optimization_problem.h" #include "cartographer/mapping_2d/submaps.h" @@ -172,7 +172,7 @@ class SparsePoseGraph : public mapping::SparsePoseGraph { GUARDED_BY(mutex_); // How our various trajectories are related. - mapping::TrajectoryConnectivity trajectory_connectivity_; + mapping::ConnectedComponents connected_components_; // We globally localize a fraction of the scans from each trajectory. std::unordered_map> diff --git a/cartographer/mapping_3d/sparse_pose_graph.cc b/cartographer/mapping_3d/sparse_pose_graph.cc index 9643971..3f182fe 100644 --- a/cartographer/mapping_3d/sparse_pose_graph.cc +++ b/cartographer/mapping_3d/sparse_pose_graph.cc @@ -102,7 +102,7 @@ void SparsePoseGraph::AddScan( trajectory_nodes_.Append( trajectory_id, mapping::TrajectoryNode{constant_data, optimized_pose}); ++num_trajectory_nodes_; - trajectory_connectivity_.Add(trajectory_id); + connected_components_.Add(trajectory_id); // Test if the 'insertion_submap.back()' is one we never saw before. if (trajectory_id >= submap_data_.num_trajectories() || @@ -212,7 +212,7 @@ void SparsePoseGraph::ComputeConstraint(const mapping::NodeId& node_id, trajectory_nodes_.at(node_id).constant_data.get(), submap_nodes, initial_relative_pose.rotation()); } else { - if (trajectory_connectivity_.TransitivelyConnected( + if (connected_components_.TransitivelyConnected( node_id.trajectory_id, submap_id.trajectory_id)) { constraint_builder_.MaybeAddConstraint( submap_id, submap_data_.at(submap_id).submap.get(), node_id, @@ -330,7 +330,7 @@ void SparsePoseGraph::HandleWorkQueue() { for (const Constraint& constraint : result) { CHECK_EQ(constraint.tag, mapping::SparsePoseGraph::Constraint::INTER_SUBMAP); - trajectory_connectivity_.Connect(constraint.node_id.trajectory_id, + connected_components_.Connect(constraint.node_id.trajectory_id, constraint.submap_id.trajectory_id); } @@ -534,7 +534,7 @@ transform::Rigid3d SparsePoseGraph::GetLocalToGlobalTransform( } std::vector> SparsePoseGraph::GetConnectedTrajectories() { - return trajectory_connectivity_.ConnectedComponents(); + return connected_components_.Components(); } int SparsePoseGraph::num_submaps(const int trajectory_id) { diff --git a/cartographer/mapping_3d/sparse_pose_graph.h b/cartographer/mapping_3d/sparse_pose_graph.h index e081479..eae42b1 100644 --- a/cartographer/mapping_3d/sparse_pose_graph.h +++ b/cartographer/mapping_3d/sparse_pose_graph.h @@ -32,9 +32,9 @@ #include "cartographer/common/mutex.h" #include "cartographer/common/thread_pool.h" #include "cartographer/common/time.h" +#include "cartographer/mapping/connected_components.h" #include "cartographer/mapping/pose_graph_trimmer.h" #include "cartographer/mapping/sparse_pose_graph.h" -#include "cartographer/mapping/trajectory_connectivity.h" #include "cartographer/mapping_3d/sparse_pose_graph/constraint_builder.h" #include "cartographer/mapping_3d/sparse_pose_graph/optimization_problem.h" #include "cartographer/mapping_3d/submaps.h" @@ -177,7 +177,7 @@ class SparsePoseGraph : public mapping::SparsePoseGraph { GUARDED_BY(mutex_); // How our various trajectories are related. - mapping::TrajectoryConnectivity trajectory_connectivity_; + mapping::ConnectedComponents connected_components_; // We globally localize a fraction of the scans from each trajectory. std::unordered_map>