65 for (
auto& voxel : flat_voxels) {
66 voxel->second.finalize();
70template <
typename VoxelContents>
72 const Eigen::Vector4d query = (Eigen::Vector4d() << pt[0], pt[1], pt[2], 1.0).finished();
73 const Eigen::Vector3i center = fast_floor(query * inv_leaf_size).template head<3>();
75 size_t voxel_index = 0;
76 const auto index_transform = [&](
const size_t point_index) {
return calc_index(voxel_index, point_index); };
78 KnnResult<-1,
decltype(index_transform)> result(k_indices, k_sq_dists, k, index_transform, max_sq_dist);
79 for (
const auto& offset : offsets) {
80 const Eigen::Vector3i coord = center + offset;
81 const auto found = voxels.find(coord);
82 if (found == voxels.end()) {
86 voxel_index = found->second;
87 const auto& voxel = flat_voxels[voxel_index]->second;
88 voxel.knn_search(query, result);
91 return result.num_found();
94template <
typename VoxelContents>
95std::vector<Eigen::Vector3i> IncrementalVoxelMap<VoxelContents>::neighbor_offsets(
const int neighbor_voxel_mode)
const {
96 switch (neighbor_voxel_mode) {
98 return std::vector<Eigen::Vector3i>{Eigen::Vector3i(0, 0, 0)};
100 return std::vector<Eigen::Vector3i>{
101 Eigen::Vector3i(0, 0, 0),
102 Eigen::Vector3i(1, 0, 0),
103 Eigen::Vector3i(-1, 0, 0),
104 Eigen::Vector3i(0, 1, 0),
105 Eigen::Vector3i(0, -1, 0),
106 Eigen::Vector3i(0, 0, 1),
107 Eigen::Vector3i(0, 0, -1)};
109 std::vector<Eigen::Vector3i> offsets;
110 for (
int i = -1; i <= 1; i++) {
111 for (
int j = -1; j <= 1; j++) {
112 for (
int k = -1; k <= 1; k++) {
113 if (std::abs(i) == 1 && std::abs(j) == 1 && std::abs(k) == 1) {
117 offsets.push_back(Eigen::Vector3i(i, j, k));
124 std::vector<Eigen::Vector3i> offsets;
125 for (
int i = -1; i <= 1; i++) {
126 for (
int j = -1; j <= 1; j++) {
127 for (
int k = -1; k <= 1; k++) {
128 offsets.push_back(Eigen::Vector3i(i, j, k));
136 std::cerr <<
"error: invalid neighbor voxel mode " << neighbor_voxel_mode << std::endl;
137 std::cerr <<
" : neighbor voxel mode must be 1, 7, 19, or 27" << std::endl;
138 return std::vector<Eigen::Vector3i>();
142template <
typename VoxelContents>
143bool IncrementalVoxelMap<VoxelContents>::has_points()
const {
144 return flat_voxels.empty() ? false : frame::has_points(flat_voxels.front()->second);
147template <
typename VoxelContents>
148bool IncrementalVoxelMap<VoxelContents>::has_normals()
const {
149 return flat_voxels.empty() ? false : frame::has_normals(flat_voxels.front()->second);
152template <
typename VoxelContents>
153bool IncrementalVoxelMap<VoxelContents>::has_covs()
const {
154 return flat_voxels.empty() ? false : frame::has_covs(flat_voxels.front()->second);
157template <
typename VoxelContents>
158bool IncrementalVoxelMap<VoxelContents>::has_intensities()
const {
159 return flat_voxels.empty() ? false : frame::has_intensities(flat_voxels.front()->second);
162template <
typename VoxelContents>
163std::vector<Eigen::Vector4d> IncrementalVoxelMap<VoxelContents>::voxel_points()
const {
164 std::vector<Eigen::Vector4d> points;
165 points.reserve(flat_voxels.size() * 10);
166 visit_points([&](
const auto& voxel,
const int i) { points.emplace_back(frame::point(voxel, i)); });
170template <
typename VoxelContents>
171std::vector<Eigen::Vector4d> IncrementalVoxelMap<VoxelContents>::voxel_normals()
const {
172 std::vector<Eigen::Vector4d> normals;
173 normals.reserve(flat_voxels.size() * 10);
174 visit_points([&](
const auto& voxel,
const int i) { normals.emplace_back(frame::normal(voxel, i)); });
178template <
typename VoxelContents>
179std::vector<Eigen::Matrix4d> IncrementalVoxelMap<VoxelContents>::voxel_covs()
const {
180 std::vector<Eigen::Matrix4d> covs;
181 covs.reserve(flat_voxels.size() * 10);
182 visit_points([&](
const auto& voxel,
const int i) { covs.emplace_back(frame::cov(voxel, i)); });
186template <
typename VoxelContents>
187std::vector<double> IncrementalVoxelMap<VoxelContents>::voxel_intensities()
const {
188 std::vector<double> intensities;
189 intensities.reserve(flat_voxels.size() * 10);
190 visit_points([&](
const auto& voxel,
const int i) { intensities.emplace_back(frame::intensity(voxel, i)); });
194template <
typename VoxelContents>
195PointCloudCPU::Ptr IncrementalVoxelMap<VoxelContents>::voxel_data()
const {
196 auto frame = std::make_shared<PointCloudCPU>();
197 frame->points_storage.reserve(flat_voxels.size() * 10);
199 frame->normals_storage.reserve(flat_voxels.size() * 10);
202 frame->covs_storage.reserve(flat_voxels.size() * 10);
204 if (has_intensities()) {
205 frame->intensities_storage.reserve(flat_voxels.size() * 10);
208 visit_points([&](
const auto& voxel,
const int i) {
209 frame->points_storage.emplace_back(frame::point(voxel, i));
210 if (frame::has_normals(voxel)) {
211 frame->normals_storage.emplace_back(frame::normal(voxel, i));
213 if (frame::has_covs(voxel)) {
214 frame->covs_storage.emplace_back(frame::cov(voxel, i));
216 if (frame::has_intensities(voxel)) {
217 frame->intensities_storage.emplace_back(frame::intensity(voxel, i));
221 frame->num_points = frame->points_storage.size();
222 frame->points = frame->points_storage.data();
223 frame->normals = frame->normals_storage.empty() ? nullptr : frame->normals_storage.data();
224 frame->covs = frame->covs_storage.empty() ? nullptr : frame->covs_storage.data();
225 frame->intensities = frame->intensities_storage.empty() ? nullptr : frame->intensities_storage.data();