Apollo 10.0
自动驾驶开放平台
ndt_voxel_grid_covariance.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16/*
17 * Software License Agreement (BSD License)
18 *
19 * Point Cloud Library (PCL) - www.pointclouds.org
20 * Copyright (c) 2010-2011, Willow Garage, Inc.
21 *
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions
26 * are met:
27 *
28 * * Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * * Redistributions in binary form must reproduce the above
31 * copyright notice, this list of conditions and the following
32 * disclaimer in the documentation and/or other materials provided
33 * with the distribution.
34 * * Neither the name of the copyright holder(s) nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
42 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
48 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49 * POSSIBILITY OF SUCH DAMAGE.
50 *
51 */
52
53#pragma once
54
55#include <map>
56#include <vector>
57
58#include "pcl/filters/boost.h"
59#include "pcl/filters/voxel_grid.h"
60#include "pcl/kdtree/kdtree_flann.h"
61#include "pcl/point_types.h"
62
63#include "cyber/common/log.h"
65
66namespace apollo {
67namespace localization {
68namespace ndt {
69
72struct Leaf {
73 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
75 : nr_points_(0),
76 mean_(Eigen::Vector3d::Zero()),
77 icov_(Eigen::Matrix3d::Zero()) {}
78
80 int GetPointCount() const { return nr_points_; }
81
83 Eigen::Vector3d GetMean() const { return mean_; }
84
86 Eigen::Matrix3d GetInverseCov() const { return icov_; }
87
91 Eigen::Vector3d mean_;
93 Eigen::Matrix3d icov_;
94};
96typedef Leaf *LeafPtr;
98typedef const Leaf *LeafConstPtr;
99
102template <typename PointT>
104 public:
105 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
106
107 protected:
108 typedef pcl::PointCloud<PointT> PointCloud;
109 typedef boost::shared_ptr<PointCloud> PointCloudPtr;
110 typedef boost::shared_ptr<const PointCloud> PointCloudConstPtr;
112
113 Eigen::Vector4f leaf_size_;
114 Eigen::Array4f inverse_leaf_size_;
115
116 Eigen::Vector4i min_b_;
117 Eigen::Vector4i max_b_;
118
119 Eigen::Vector4i div_b_;
120 Eigen::Vector4i divb_mul_;
121
122 public:
126 leaves_(),
129 kdtree_() {
130 leaf_size_.setZero();
131 min_b_.setZero();
132 max_b_.setZero();
133 }
134
136 void SetInputCloud(const PointCloudConstPtr &cloud) { input_ = cloud; }
137
140 inline void SetMinPointPerVoxel(int min_points_per_voxel) {
141 if (min_points_per_voxel > 2) {
142 min_points_per_voxel_ = min_points_per_voxel;
143 } else {
144 AWARN << "Covariance calculation requires at least 3 "
145 << "points, setting Min Point per Voxel to 3 ";
147 }
148 }
149
152
154 inline void filter(const std::vector<Leaf> &cell_leaf,
155 bool searchable = true) {
157 SetMap(cell_leaf, voxel_centroids_);
158 if (voxel_centroids_->size() > 0) {
159 kdtree_.setInputCloud(voxel_centroids_);
160 }
161 }
162
163 void SetMap(const std::vector<Leaf> &map_leaves, PointCloudPtr output);
164
166 inline LeafConstPtr GetLeaf(int index) {
167 typename std::map<size_t, Leaf>::iterator leaf_iter = leaves_.find(index);
168 if (leaf_iter == leaves_.end()) {
169 return nullptr;
170 }
171 LeafConstPtr ret(&(leaf_iter->second));
172 return ret;
173 }
174
179 inline LeafConstPtr GetLeaf(PointT *p) {
180 // Generate index associated with p
181 int ijk0 = static_cast<int>((p->x - map_left_top_corner_(0)) *
183 min_b_[0];
184 int ijk1 = static_cast<int>((p->y - map_left_top_corner_(1)) *
186 min_b_[1];
187 int ijk2 = static_cast<int>((p->z - map_left_top_corner_(2)) *
189 min_b_[2];
190
191 // Compute the centroid leaf index
192 int idx = ijk0 * divb_mul_[0] + ijk1 * divb_mul_[1] + ijk2 * divb_mul_[2];
193
194 // Find leaf associated with index
195 typename std::map<size_t, Leaf>::iterator leaf_iter = leaves_.find(idx);
196 if (leaf_iter == leaves_.end()) {
197 return nullptr;
198 }
199 // If such a leaf exists return the pointer to the leaf structure
200 LeafConstPtr ret(&(leaf_iter->second));
201 return ret;
202 }
203
206 inline LeafConstPtr GetLeaf(Eigen::Vector3f *p) {
207 // Generate index associated with p
208 int ijk0 = static_cast<int>((p->x() - map_left_top_corner_(0)) *
210 min_b_[0];
211 int ijk1 = static_cast<int>((p->y() - map_left_top_corner_(1)) *
213 min_b_[1];
214 int ijk2 = static_cast<int>((p->z() - map_left_top_corner_(2)) *
216 min_b_[2];
217
218 // Compute the centroid leaf index
219 int idx = ijk0 * divb_mul_[0] + ijk1 * divb_mul_[1] + ijk2 * divb_mul_[2];
220
221 // Find leaf associated with index
222 typename std::map<size_t, Leaf>::iterator leaf_iter = leaves_.find(idx);
223 if (leaf_iter == leaves_.end()) {
224 return nullptr;
225 }
226 // If such a leaf exists return the pointer to the leaf structure
227 LeafConstPtr ret(&(leaf_iter->second));
228 return ret;
229 }
230
232 inline const std::map<size_t, Leaf> &GetLeaves() { return leaves_; }
233
236
239 int RadiusSearch(const PointT &point, double radius,
240 std::vector<LeafConstPtr> *k_leaves,
241 std::vector<float> *k_sqr_distances,
242 unsigned int max_nn = 0);
243
244 void GetDisplayCloud(pcl::PointCloud<pcl::PointXYZ> *cell_cloud);
245
246 inline void SetMapLeftTopCorner(const Eigen::Vector3d &left_top_corner) {
247 map_left_top_corner_ = left_top_corner;
248 }
249
250 inline void SetVoxelGridResolution(float lx, float ly, float lz) {
251 leaf_size_[0] = lx;
252 leaf_size_[1] = ly;
253 leaf_size_[2] = lz;
254 // Avoid division errors
255 if (leaf_size_[3] == 0) {
256 leaf_size_[3] = 1;
257 } // Use multiplications instead of divisions
258 inverse_leaf_size_ = Eigen::Array4f::Ones() / leaf_size_.array();
259 }
260
261 protected:
265
268 std::map<size_t, Leaf> leaves_;
269
273
276
278 pcl::KdTreeFLANN<PointT> kdtree_;
279
281 Eigen::Vector3d map_left_top_corner_;
282};
283
284} // namespace ndt
285} // namespace localization
286} // namespace apollo
287
288#include "modules/localization/ndt/ndt_locator/ndt_voxel_grid_covariance.hpp"
A searchable voxel structure containing the mean and covariance of the data.
void SetMinPointPerVoxel(int min_points_per_voxel)
Set the minimum number of points required for a cell to be used (must be 3 or greater for covariance ...
LeafConstPtr GetLeaf(int index)
Get the voxel containing point p.
pcl::KdTreeFLANN< PointT > kdtree_
KdTree generated using voxel_centroids_ (used for searching).
LeafConstPtr GetLeaf(PointT *p)
Get the voxel containing point p.
void SetMapLeftTopCorner(const Eigen::Vector3d &left_top_corner)
PointCloudPtr GetCentroids()
Get a pointcloud containing the voxel centroids.
int RadiusSearch(const PointT &point, double radius, std::vector< LeafConstPtr > *k_leaves, std::vector< float > *k_sqr_distances, unsigned int max_nn=0)
Search for all the nearest occupied voxels of the query point in a given radius.
void SetInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
int min_points_per_voxel_
Minimum points contained with in a voxel to allow it to be usable.
int GetMinPointPerVoxel()
Get the minimum number of points required for a cell to be used.
LeafConstPtr GetLeaf(Eigen::Vector3f *p)
Get the voxel containing point p.
void SetVoxelGridResolution(float lx, float ly, float lz)
void filter(const std::vector< Leaf > &cell_leaf, bool searchable=true)
Initializes voxel structure.
const std::map< size_t, Leaf > & GetLeaves()
Get the leaf structure map.
std::vector< int > voxel_centroids_leaf_indices_
Indices of leaf structurs associated with each point.
boost::shared_ptr< const PointCloud > PointCloudConstPtr
PointCloudPtr voxel_centroids_
Point cloud containing centroids of voxels containing at least minimum number of points.
void SetMap(const std::vector< Leaf > &map_leaves, PointCloudPtr output)
std::map< size_t, Leaf > leaves_
Voxel structure containing all leaf nodes (includes voxels with less than a sufficient number of poin...
void GetDisplayCloud(pcl::PointCloud< pcl::PointXYZ > *cell_cloud)
#define AWARN
Definition log.h:43
const Leaf * LeafConstPtr
Const pointer to VoxelGridCovariance leaf structure
Leaf * LeafPtr
Pointer to VoxelGridCovariance leaf structure
class register implement
Definition arena_queue.h:37
Simple structure to hold a centroid, covarince and the number of points in a leaf.
int nr_points_
Number of points contained by voxel.
Eigen::Vector3d GetMean() const
Get the voxel centroid.
Eigen::Matrix3d icov_
Inverse of voxel covariance matrix.
Eigen::Matrix3d GetInverseCov() const
Get the inverse of the voxel covariance.
int GetPointCount() const
Get the number of points contained by this voxel.
Eigen::Vector3d mean_
3D voxel centroid.
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Leaf()