Skip to content

Commit 28307a7

Browse files
authored
compute mean and normals inside the alignment function (#95)
1 parent 6e9e335 commit 28307a7

File tree

7 files changed

+28
-77
lines changed

7 files changed

+28
-77
lines changed

cpp/map_closures/GroundAlign.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <utility>
3434
#include <vector>
3535

36+
#include "VoxelMap.hpp"
37+
3638
namespace {
3739
using Vector3dVector = std::vector<Eigen::Vector3d>;
3840
using LinearSystem = std::pair<Eigen::Matrix3d, Eigen::Vector3d>;
@@ -151,8 +153,11 @@ static constexpr int max_iterations = 10;
151153
} // namespace
152154

153155
namespace map_closures {
154-
Eigen::Matrix4d AlignToLocalGround(const Vector3dVector &voxel_means,
155-
const Vector3dVector &voxel_normals) {
156+
Eigen::Matrix4d AlignToLocalGround(const Vector3dVector &pointcloud, const double resolution) {
157+
VoxelMap voxel_map(resolution, 100.0);
158+
voxel_map.AddPoints(pointcloud);
159+
const auto &[voxel_means, voxel_normals] = voxel_map.PerVoxelMeanAndNormal();
160+
156161
auto [ground_samples, T] = SampleGroundPoints(voxel_means, voxel_normals);
157162
TransformPoints(T, ground_samples);
158163
for (int iters = 0; iters < max_iterations; iters++) {

cpp/map_closures/GroundAlign.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@
2727
#include <vector>
2828

2929
namespace map_closures {
30-
Eigen::Matrix4d AlignToLocalGround(const std::vector<Eigen::Vector3d> &voxel_means,
31-
const std::vector<Eigen::Vector3d> &voxel_normals);
30+
Eigen::Matrix4d AlignToLocalGround(const std::vector<Eigen::Vector3d> &pointcloud,
31+
const double resolution);
3232
} // namespace map_closures

cpp/map_closures/MapClosures.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ MapClosures::MapClosures(const Config &config) : config_(config) {
6868
}
6969

7070
void MapClosures::MatchAndAddToDatabase(const int id,
71-
const std::vector<Eigen::Vector3d> &local_map,
72-
const std::vector<Eigen::Vector3d> &voxel_means,
73-
const std::vector<Eigen::Vector3d> &voxel_normals) {
74-
const Eigen::Matrix4d T_ground = AlignToLocalGround(voxel_means, voxel_normals);
71+
const std::vector<Eigen::Vector3d> &local_map) {
72+
const Eigen::Matrix4d T_ground = AlignToLocalGround(local_map, config_.density_map_resolution);
7573
DensityMap density_map = GenerateDensityMap(local_map, T_ground, config_.density_map_resolution,
7674
config_.density_threshold);
7775
cv::Mat orb_descriptors;
@@ -143,12 +141,8 @@ ClosureCandidate MapClosures::ValidateClosure(const int reference_id, const int
143141
}
144142

145143
std::vector<ClosureCandidate> MapClosures::GetTopKClosures(
146-
const int query_id,
147-
const std::vector<Eigen::Vector3d> &local_map,
148-
const std::vector<Eigen::Vector3d> &voxel_means,
149-
const std::vector<Eigen::Vector3d> &voxel_normals,
150-
const int k) {
151-
MatchAndAddToDatabase(query_id, local_map, voxel_means, voxel_normals);
144+
const int query_id, const std::vector<Eigen::Vector3d> &local_map, const int k) {
145+
MatchAndAddToDatabase(query_id, local_map);
152146
auto compare_closure_candidates = [](const ClosureCandidate &a, const ClosureCandidate &b) {
153147
return a.number_of_inliers >= b.number_of_inliers;
154148
};

cpp/map_closures/MapClosures.hpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,19 @@ class MapClosures {
6161
~MapClosures() = default;
6262

6363
ClosureCandidate GetBestClosure(const int query_id,
64-
const std::vector<Eigen::Vector3d> &local_map,
65-
const std::vector<Eigen::Vector3d> &voxel_means,
66-
const std::vector<Eigen::Vector3d> &voxel_normals) {
67-
const auto &closures = GetTopKClosures(query_id, local_map, voxel_means, voxel_normals, 1);
64+
const std::vector<Eigen::Vector3d> &local_map) {
65+
const auto &closures = GetTopKClosures(query_id, local_map, 1);
6866
if (closures.empty()) {
6967
return ClosureCandidate();
7068
}
7169
return closures.front();
7270
}
7371
std::vector<ClosureCandidate> GetTopKClosures(const int query_id,
7472
const std::vector<Eigen::Vector3d> &local_map,
75-
const std::vector<Eigen::Vector3d> &voxel_means,
76-
const std::vector<Eigen::Vector3d> &voxel_normals,
7773
const int k);
7874
std::vector<ClosureCandidate> GetClosures(const int query_id,
79-
const std::vector<Eigen::Vector3d> &local_map,
80-
const std::vector<Eigen::Vector3d> &voxel_means,
81-
const std::vector<Eigen::Vector3d> &voxel_normals) {
82-
return GetTopKClosures(query_id, local_map, voxel_means, voxel_normals, -1);
75+
const std::vector<Eigen::Vector3d> &local_map) {
76+
return GetTopKClosures(query_id, local_map, -1);
8377
}
8478

8579
const DensityMap &getDensityMapFromId(const int map_id) const {
@@ -95,10 +89,7 @@ class MapClosures {
9589
}
9690

9791
protected:
98-
void MatchAndAddToDatabase(const int id,
99-
const std::vector<Eigen::Vector3d> &local_map,
100-
const std::vector<Eigen::Vector3d> &voxel_means,
101-
const std::vector<Eigen::Vector3d> &voxel_normals);
92+
void MatchAndAddToDatabase(const int id, const std::vector<Eigen::Vector3d> &local_map);
10293
ClosureCandidate ValidateClosure(const int reference_id, const int query_id) const;
10394

10495
Config config_;

python/map_closures/map_closures.py

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,51 +37,18 @@ def __init__(self, config: MapClosuresConfig = MapClosuresConfig()):
3737
self._config = config
3838
self._pipeline = map_closures_pybind._MapClosures(self._config.model_dump())
3939

40-
def get_best_closure(
41-
self,
42-
query_idx: int,
43-
local_map: np.ndarray,
44-
voxel_means: np.ndarray,
45-
voxel_normals: np.ndarray,
46-
) -> ClosureCandidate:
47-
closure = self._pipeline._GetBestClosure(
48-
query_idx,
49-
Vector3dVector(local_map),
50-
Vector3dVector(voxel_means),
51-
Vector3dVector(voxel_normals),
52-
)
40+
def get_best_closure(self, query_idx: int, local_map: np.ndarray) -> ClosureCandidate:
41+
closure = self._pipeline._GetBestClosure(query_idx, Vector3dVector(local_map))
5342
return closure
5443

5544
def get_top_k_closures(
56-
self,
57-
query_idx: int,
58-
local_map: np.ndarray,
59-
voxel_means: np.ndarray,
60-
voxel_normals: np.ndarray,
61-
k: int,
45+
self, query_idx: int, local_map: np.ndarray, k: int
6246
) -> List[ClosureCandidate]:
63-
top_k_closures = self._pipeline._GetTopKClosures(
64-
query_idx,
65-
Vector3dVector(local_map),
66-
Vector3dVector(voxel_means),
67-
Vector3dVector(voxel_normals),
68-
k,
69-
)
47+
top_k_closures = self._pipeline._GetTopKClosures(query_idx, Vector3dVector(local_map), k)
7048
return top_k_closures
7149

72-
def get_closures(
73-
self,
74-
query_idx: int,
75-
local_map: np.ndarray,
76-
voxel_means: np.ndarray,
77-
voxel_normals: np.ndarray,
78-
) -> List[ClosureCandidate]:
79-
closures = self._pipeline._GetClosures(
80-
query_idx,
81-
Vector3dVector(local_map),
82-
Vector3dVector(voxel_means),
83-
Vector3dVector(voxel_normals),
84-
)
50+
def get_closures(self, query_idx: int, local_map: np.ndarray) -> List[ClosureCandidate]:
51+
closures = self._pipeline._GetClosures(query_idx, Vector3dVector(local_map))
8552
return closures
8653

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

python/map_closures/pipeline.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,7 @@ def _run_pipeline(self):
127127
scan_idx == self._n_scans - 1
128128
):
129129
local_map_pointcloud = self.voxel_local_map.point_cloud()
130-
points, normals = self.voxel_local_map.per_voxel_mean_and_normal()
131-
closures = self.map_closures.get_closures(
132-
map_idx, local_map_pointcloud, points, normals
133-
)
130+
closures = self.map_closures.get_closures(map_idx, local_map_pointcloud)
134131

135132
density_map = self.map_closures.get_density_map_from_id(map_idx)
136133
self.data.append_localmap(

python/map_closures/pybind/map_closures_pybind.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,9 @@ PYBIND11_MODULE(map_closures_pybind, m) {
7878
return density_map_eigen;
7979
})
8080
.def("_getGroundAlignmentFromId", &MapClosures::getGroundAlignmentFromId, "map_id"_a)
81-
.def("_GetBestClosure", &MapClosures::GetBestClosure, "query_id"_a, "local_map"_a,
82-
"voxel_means"_a, "voxel_normals"_a)
83-
.def("_GetTopKClosures", &MapClosures::GetTopKClosures, "query_id"_a, "local_map"_a,
84-
"voxel_means"_a, "voxel_normals"_a, "k"_a)
85-
.def("_GetClosures", &MapClosures::GetClosures, "query_id"_a, "local_map"_a,
86-
"voxel_means"_a, "voxel_normals"_a)
81+
.def("_GetBestClosure", &MapClosures::GetBestClosure, "query_id"_a, "local_map"_a)
82+
.def("_GetTopKClosures", &MapClosures::GetTopKClosures, "query_id"_a, "local_map"_a, "k"_a)
83+
.def("_GetClosures", &MapClosures::GetClosures, "query_id"_a, "local_map"_a)
8784
.def("_SaveHbstDatabase", &MapClosures::SaveHbstDatabase, "database_path"_a);
8885

8986
py::class_<VoxelMap> internal_map(m, "_VoxelMap", "Don't use this");

0 commit comments

Comments
 (0)