Apollo  6.0
Open source self driving car software
ndt_voxel_grid_covariance.h
Go to the documentation of this file.
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 
66 namespace apollo {
67 namespace localization {
68 namespace ndt {
69 
72 struct Leaf {
73  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
74  Leaf()
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 
94 };
96 typedef Leaf *LeafPtr;
98 typedef const Leaf *LeafConstPtr;
99 
102 template <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;
111  PointCloudConstPtr input_;
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:
125  : min_points_per_voxel_(6),
126  leaves_(),
127  voxel_centroids_(),
128  voxel_centroids_leaf_indices_(),
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 ";
146  min_points_per_voxel_ = 3;
147  }
148  }
149 
151  inline int GetMinPointPerVoxel() { return min_points_per_voxel_; }
152 
154  inline void filter(const std::vector<Leaf> &cell_leaf,
155  bool searchable = true) {
156  voxel_centroids_ = PointCloudPtr(new PointCloud);
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)) *
182  inverse_leaf_size_[0]) -
183  min_b_[0];
184  int ijk1 = static_cast<int>((p->y - map_left_top_corner_(1)) *
185  inverse_leaf_size_[1]) -
186  min_b_[1];
187  int ijk2 = static_cast<int>((p->z - map_left_top_corner_(2)) *
188  inverse_leaf_size_[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)) *
209  inverse_leaf_size_[0]) -
210  min_b_[0];
211  int ijk1 = static_cast<int>((p->y() - map_left_top_corner_(1)) *
212  inverse_leaf_size_[1]) -
213  min_b_[1];
214  int ijk2 = static_cast<int>((p->z() - map_left_top_corner_(2)) *
215  inverse_leaf_size_[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 
235  inline PointCloudPtr GetCentroids() { return voxel_centroids_; }
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 
272  PointCloudPtr voxel_centroids_;
273 
276 
278  pcl::KdTreeFLANN<PointT> kdtree_;
279 
282 };
283 
284 } // namespace ndt
285 } // namespace localization
286 } // namespace apollo
287 
288 #include "modules/localization/ndt/ndt_locator/ndt_voxel_grid_covariance.hpp"
LeafConstPtr GetLeaf(PointT *p)
Get the voxel containing point p.
Definition: ndt_voxel_grid_covariance.h:179
Eigen::Vector4i div_b_
Definition: ndt_voxel_grid_covariance.h:119
void SetVoxelGridResolution(float lx, float ly, float lz)
Definition: ndt_voxel_grid_covariance.h:250
PointCloudPtr GetCentroids()
Get a pointcloud containing the voxel centroids.
Definition: ndt_voxel_grid_covariance.h:235
const std::map< size_t, Leaf > & GetLeaves()
Get the leaf structure map.
Definition: ndt_voxel_grid_covariance.h:232
pcl::PointCloud< PointT > PointCloud
Definition: ndt_voxel_grid_covariance.h:108
std::map< size_t, Leaf > leaves_
Voxel structure containing all leaf nodes (includes voxels with less than a sufficient number of poin...
Definition: ndt_voxel_grid_covariance.h:268
Eigen::Vector3d GetMean() const
Get the voxel centroid.
Definition: ndt_voxel_grid_covariance.h:83
Eigen::Vector4i max_b_
Definition: ndt_voxel_grid_covariance.h:117
Eigen::Vector4i divb_mul_
Definition: ndt_voxel_grid_covariance.h:120
Eigen::Vector3f Vector3f
Definition: base_map_fwd.h:29
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
int GetPointCount() const
Get the number of points contained by this voxel.
Definition: ndt_voxel_grid_covariance.h:80
Eigen::Vector4i min_b_
Definition: ndt_voxel_grid_covariance.h:116
Leaf * LeafPtr
Pointer to VoxelGridCovariance leaf structure.
Definition: ndt_voxel_grid_covariance.h:96
void SetInputCloud(const PointCloudConstPtr &cloud)
Provide a pointer to the input dataset.
Definition: ndt_voxel_grid_covariance.h:136
LeafConstPtr GetLeaf(Eigen::Vector3f *p)
Get the voxel containing point p.*.
Definition: ndt_voxel_grid_covariance.h:206
Eigen::Array4f inverse_leaf_size_
Definition: ndt_voxel_grid_covariance.h:114
Eigen::Vector4f leaf_size_
Definition: ndt_voxel_grid_covariance.h:113
PointCloudConstPtr input_
Definition: ndt_voxel_grid_covariance.h:111
Eigen::Vector3d Vector3d
Definition: frame_transform.h:27
LeafConstPtr GetLeaf(int index)
Get the voxel containing point p.
Definition: ndt_voxel_grid_covariance.h:166
Eigen::Matrix3d GetInverseCov() const
Get the inverse of the voxel covariance.
Definition: ndt_voxel_grid_covariance.h:86
pcl::PointCloud< Point >::Ptr PointCloudPtr
Definition: types.h:65
int nr_points_
Number of points contained by voxel.
Definition: ndt_voxel_grid_covariance.h:89
void SetMapLeftTopCorner(const Eigen::Vector3d &left_top_corner)
Definition: ndt_voxel_grid_covariance.h:246
const Leaf * LeafConstPtr
Const pointer to VoxelGridCovariance leaf structure.
Definition: ndt_voxel_grid_covariance.h:98
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 ...
Definition: ndt_voxel_grid_covariance.h:140
boost::shared_ptr< const PointCloud > PointCloudConstPtr
Definition: ndt_voxel_grid_covariance.h:110
boost::shared_ptr< PointCloud > PointCloudPtr
Definition: ndt_voxel_grid_covariance.h:109
EIGEN_MAKE_ALIGNED_OPERATOR_NEW Leaf()
Definition: ndt_voxel_grid_covariance.h:74
int min_points_per_voxel_
Minimum points contained with in a voxel to allow it to be usable.
Definition: ndt_voxel_grid_covariance.h:264
PointCloudPtr voxel_centroids_
Point cloud containing centroids of voxels containing at least minimum number of points.
Definition: ndt_voxel_grid_covariance.h:272
VoxelGridCovariance()
Constructor.
Definition: ndt_voxel_grid_covariance.h:124
std::vector< int > voxel_centroids_leaf_indices_
Indices of leaf structurs associated with each point.
Definition: ndt_voxel_grid_covariance.h:275
A searchable voxel structure containing the mean and covariance of the data.
Definition: ndt_voxel_grid_covariance.h:103
int GetMinPointPerVoxel()
Get the minimum number of points required for a cell to be used.
Definition: ndt_voxel_grid_covariance.h:151
Eigen::Vector3d map_left_top_corner_
Left top corner.
Definition: ndt_voxel_grid_covariance.h:281
Eigen::Matrix3d icov_
Inverse of voxel covariance matrix.
Definition: ndt_voxel_grid_covariance.h:93
Simple structure to hold a centroid, covarince and the number of points in a leaf.
Definition: ndt_voxel_grid_covariance.h:72
pcl::KdTreeFLANN< PointT > kdtree_
KdTree generated using voxel_centroids_ (used for searching).
Definition: ndt_voxel_grid_covariance.h:278
#define AWARN
Definition: log.h:43
Eigen::Vector3d mean_
3D voxel centroid.
Definition: ndt_voxel_grid_covariance.h:91
Eigen::Matrix3d Matrix3d
Definition: base_map_fwd.h:33
void filter(const std::vector< Leaf > &cell_leaf, bool searchable=true)
Initializes voxel structure.
Definition: ndt_voxel_grid_covariance.h:154