00001 /* 00002 * Copyright 2017 The Cartographer Authors 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #ifndef CARTOGRAPHER_MAPPING_INTERNAL_TRAJECTORY_CONNECTIVITY_STATE_H_ 00018 #define CARTOGRAPHER_MAPPING_INTERNAL_TRAJECTORY_CONNECTIVITY_STATE_H_ 00019 00020 #include "cartographer/common/time.h" 00021 #include "cartographer/mapping/internal/connected_components.h" 00022 00023 namespace cartographer { 00024 namespace mapping { 00025 00026 // A class that tracks the connectivity state between trajectories. Compared to 00027 // ConnectedComponents it tracks additionally the last time that a global 00028 // constraint connected to trajectories. 00029 // 00030 // This class is thread-compatible. 00031 class TrajectoryConnectivityState { 00032 public: 00033 TrajectoryConnectivityState() {} 00034 00035 TrajectoryConnectivityState(const TrajectoryConnectivityState&) = delete; 00036 TrajectoryConnectivityState& operator=(const TrajectoryConnectivityState&) = 00037 delete; 00038 00039 // Add a trajectory which is initially connected to only itself. 00040 void Add(int trajectory_id); 00041 00042 // Connect two trajectories. If either trajectory is untracked, it will be 00043 // tracked. This function is invariant to the order of its arguments. Repeated 00044 // calls to Connect increment the connectivity count and update the last 00045 // connected time. 00046 void Connect(int trajectory_id_a, int trajectory_id_b, common::Time time); 00047 00048 // Determines if two trajectories have been (transitively) connected. If 00049 // either trajectory is not being tracked, returns false, except when it is 00050 // the same trajectory, where it returns true. This function is invariant to 00051 // the order of its arguments. 00052 bool TransitivelyConnected(int trajectory_id_a, int trajectory_id_b) const; 00053 00054 // The trajectory IDs, grouped by connectivity. 00055 std::vector<std::vector<int>> Components() const; 00056 00057 // Return the last connection count between the two trajectories. If either of 00058 // the trajectories is untracked or they have never been connected returns the 00059 // beginning of time. 00060 common::Time LastConnectionTime(int trajectory_id_a, int trajectory_id_b); 00061 00062 private: 00063 // ConnectedComponents are thread safe. 00064 mutable ConnectedComponents connected_components_; 00065 00066 // Tracks the last time a direct connection between two trajectories has 00067 // been added. The exception is when a connection between two trajectories 00068 // connects two formerly unconnected connected components. In this case all 00069 // bipartite trajectories entries for these components are updated with the 00070 // new connection time. 00071 std::map<std::pair<int, int>, common::Time> last_connection_time_map_; 00072 }; 00073 00074 } // namespace mapping 00075 } // namespace cartographer 00076 00077 #endif // CARTOGRAPHER_MAPPING_INTERNAL_TRAJECTORY_CONNECTIVITY_STATE_H_