Skip to content

Commit 05a4f4c

Browse files
committed
massive change in API: replace match_and_add function with get_closures
1 parent 1224c07 commit 05a4f4c

File tree

5 files changed

+47
-48
lines changed

5 files changed

+47
-48
lines changed

cpp/map_closures/MapClosures.cpp

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ MapClosures::MapClosures(const Config &config) : config_(config) {
6767
cv::ORB::ScoreType(score_type), patch_size, fast_threshold);
6868
}
6969

70-
void MapClosures::MatchAndAdd(const int id, const std::vector<Eigen::Vector3d> &local_map) {
70+
void MapClosures::MatchAndAddToDatabase(const int id,
71+
const std::vector<Eigen::Vector3d> &local_map) {
7172
Eigen::Matrix4d T_ground = AlignToLocalGround(local_map, ground_alignment_resolution);
7273
DensityMap density_map = GenerateDensityMap(local_map, T_ground, config_.density_map_resolution,
7374
config_.density_threshold);
@@ -104,9 +105,37 @@ void MapClosures::MatchAndAdd(const int id, const std::vector<Eigen::Vector3d> &
104105
srrg_hbst::SplittingStrategy::SplitEven);
105106
}
106107

108+
ClosureCandidate MapClosures::ValidateClosure(const int reference_id, const int query_id) const {
109+
const Tree::MatchVector &matches = descriptor_matches_.at(reference_id);
110+
const size_t num_matches = matches.size();
111+
112+
ClosureCandidate closure;
113+
if (num_matches > min_no_of_matches) {
114+
std::vector<PointPair> keypoint_pairs(num_matches);
115+
std::transform(matches.cbegin(), matches.cend(), keypoint_pairs.begin(),
116+
[&](const Tree::Match &match) {
117+
auto query_point =
118+
Eigen::Vector2d(match.object_query.pt.y, match.object_query.pt.x);
119+
auto ref_point = Eigen::Vector2d(match.object_references[0].pt.y,
120+
match.object_references[0].pt.x);
121+
return PointPair(ref_point, query_point);
122+
});
123+
124+
const auto &[pose2d, number_of_inliers] = RansacAlignment2D(keypoint_pairs);
125+
closure.source_id = reference_id;
126+
closure.target_id = query_id;
127+
closure.pose.block<2, 2>(0, 0) = pose2d.linear();
128+
closure.pose.block<2, 1>(0, 3) = pose2d.translation() * config_.density_map_resolution;
129+
closure.pose = ground_alignments_.at(query_id).inverse() * closure.pose *
130+
ground_alignments_.at(reference_id);
131+
closure.number_of_inliers = number_of_inliers;
132+
}
133+
return closure;
134+
}
135+
107136
ClosureCandidate MapClosures::GetBestClosure(const int query_id,
108137
const std::vector<Eigen::Vector3d> &local_map) {
109-
MatchAndAdd(query_id, local_map);
138+
MatchAndAddToDatabase(query_id, local_map);
110139
auto compare_closure_candidates = [](ClosureCandidate a,
111140
const ClosureCandidate &b) -> ClosureCandidate {
112141
return a.number_of_inliers > b.number_of_inliers ? a : b;
@@ -126,7 +155,7 @@ ClosureCandidate MapClosures::GetBestClosure(const int query_id,
126155

127156
std::vector<ClosureCandidate> MapClosures::GetTopKClosures(
128157
const int query_id, const std::vector<Eigen::Vector3d> &local_map, const int k) {
129-
MatchAndAdd(query_id, local_map);
158+
MatchAndAddToDatabase(query_id, local_map);
130159
auto compare_closure_candidates = [](const ClosureCandidate &a, const ClosureCandidate &b) {
131160
return a.number_of_inliers >= b.number_of_inliers;
132161
};
@@ -156,32 +185,4 @@ std::vector<ClosureCandidate> MapClosures::GetTopKClosures(
156185
std::back_inserter(top_k_closures));
157186
return top_k_closures;
158187
}
159-
160-
ClosureCandidate MapClosures::ValidateClosure(const int reference_id, const int query_id) const {
161-
const Tree::MatchVector &matches = descriptor_matches_.at(reference_id);
162-
const size_t num_matches = matches.size();
163-
164-
ClosureCandidate closure;
165-
if (num_matches > min_no_of_matches) {
166-
std::vector<PointPair> keypoint_pairs(num_matches);
167-
std::transform(matches.cbegin(), matches.cend(), keypoint_pairs.begin(),
168-
[&](const Tree::Match &match) {
169-
auto query_point =
170-
Eigen::Vector2d(match.object_query.pt.y, match.object_query.pt.x);
171-
auto ref_point = Eigen::Vector2d(match.object_references[0].pt.y,
172-
match.object_references[0].pt.x);
173-
return PointPair(ref_point, query_point);
174-
});
175-
176-
const auto &[pose2d, number_of_inliers] = RansacAlignment2D(keypoint_pairs);
177-
closure.source_id = reference_id;
178-
closure.target_id = query_id;
179-
closure.pose.block<2, 2>(0, 0) = pose2d.linear();
180-
closure.pose.block<2, 1>(0, 3) = pose2d.translation() * config_.density_map_resolution;
181-
closure.pose = ground_alignments_.at(query_id).inverse() * closure.pose *
182-
ground_alignments_.at(reference_id);
183-
closure.number_of_inliers = number_of_inliers;
184-
}
185-
return closure;
186-
}
187188
} // namespace map_closures

cpp/map_closures/MapClosures.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ struct Config {
4747
};
4848

4949
struct ClosureCandidate {
50-
ClosureCandidate() = default;
5150
int source_id = -1;
5251
int target_id = -1;
5352
Eigen::Matrix4d pose = Eigen::Matrix4d::Identity();
@@ -74,8 +73,8 @@ class MapClosures {
7473
}
7574

7675
protected:
76+
void MatchAndAddToDatabase(const int id, const std::vector<Eigen::Vector3d> &local_map);
7777
ClosureCandidate ValidateClosure(const int reference_id, const int query_id) const;
78-
void MatchAndAdd(const int id, const std::vector<Eigen::Vector3d> &local_map);
7978

8079
Config config_;
8180
Tree::MatchVectorMap descriptor_matches_;

python/map_closures/map_closures.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,21 @@ def __init__(self, config: MapClosuresConfig = MapClosuresConfig()):
3636
self._config = config
3737
self._pipeline = map_closures_pybind._MapClosures(self._config.model_dump())
3838

39-
def match_and_add(self, map_idx: int, local_map: np.ndarray):
39+
def get_best_closures(self, query_idx: int, local_map: np.ndarray) -> ClosureCandidate:
4040
pcd = map_closures_pybind._Vector3dVector(local_map)
41-
self._pipeline._MatchAndAdd(map_idx, pcd)
42-
43-
def get_best_closures(self, query_idx: int) -> ClosureCandidate:
44-
closure = self._pipeline._GetBestClosure(query_idx)
41+
closure = self._pipeline._GetBestClosure(query_idx, pcd)
4542
return closure
4643

47-
def get_top_k_closures(self, query_idx: int, k: int) -> List[ClosureCandidate]:
48-
top_k_closures = self._pipeline._GetTopKClosures(query_idx, k)
44+
def get_top_k_closures(
45+
self, query_idx: int, local_map: np.ndarray, k: int
46+
) -> List[ClosureCandidate]:
47+
pcd = map_closures_pybind._Vector3dVector(local_map)
48+
top_k_closures = self._pipeline._GetTopKClosures(query_idx, pcd, k)
4949
return top_k_closures
5050

51-
def get_closures(self, query_idx: int) -> List[ClosureCandidate]:
52-
closures = self._pipeline._GetClosures(query_idx)
51+
def get_closures(self, query_idx: int, local_map: np.ndarray) -> List[ClosureCandidate]:
52+
pcd = map_closures_pybind._Vector3dVector(local_map)
53+
closures = self._pipeline._GetClosures(query_idx, pcd)
5354
return closures
5455

5556
def get_density_map_from_id(self, map_id: int) -> np.ndarray:

python/map_closures/pipeline.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def _run_pipeline(self):
153153
scan_idx == self._n_scans - 1
154154
):
155155
local_map_pointcloud = self.voxel_local_map.point_cloud()
156-
self.map_closures.match_and_add(map_idx, local_map_pointcloud)
156+
closures = self.map_closures.get_closures(map_idx, local_map_pointcloud)
157157

158158
scan_indices_in_local_map.append(scan_idx)
159159
poses_in_local_map.append(current_frame_pose)
@@ -171,7 +171,6 @@ def _run_pipeline(self):
171171
self.local_maps[-1].density_map,
172172
current_map_pose,
173173
)
174-
closures = self.map_closures.get_closures(map_idx)
175174
for closure in closures:
176175
if closure.number_of_inliers > self.closure_config.inliers_threshold:
177176
reference_local_map = self.local_maps[closure.source_id]

python/map_closures/pybind/map_closures_pybind.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ PYBIND11_MODULE(map_closures_pybind, m) {
7777
cv::cv2eigen(density_map.grid, density_map_eigen);
7878
return density_map_eigen;
7979
})
80-
.def("_MatchAndAdd", &MapClosures::MatchAndAdd, "map_id"_a, "local_map"_a)
81-
.def("_GetBestClosure", &MapClosures::GetBestClosure, "query_id"_a)
82-
.def("_GetTopKClosures", &MapClosures::GetTopKClosures, "query_id"_a, "k"_a)
83-
.def("_GetClosures", &MapClosures::GetClosures, "query_id"_a);
80+
.def("_GetBestClosure", &MapClosures::GetBestClosure, "query_id"_a, "local_map"_a)
81+
.def("_GetTopKClosures", &MapClosures::GetTopKClosures, "query_id"_a, "local_map"_a, "k"_a)
82+
.def("_GetClosures", &MapClosures::GetClosures, "query_id"_a, "local_map"_a);
8483
}
8584
} // namespace map_closures

0 commit comments

Comments
 (0)