Point Cloud Library (PCL)  1.9.0-dev
frustum_culling.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2012-, Open Perception, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of the copyright holder(s) nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 #ifndef PCL_FILTERS_IMPL_FRUSTUM_CULLING_HPP_
39 #define PCL_FILTERS_IMPL_FRUSTUM_CULLING_HPP_
40 
41 #include <pcl/filters/frustum_culling.h>
42 #include <pcl/common/io.h>
43 #include <vector>
44 
45 ///////////////////////////////////////////////////////////////////////////////
46 template <typename PointT> void
48 {
49  std::vector<int> indices;
50  if (keep_organized_)
51  {
52  bool temp = extract_removed_indices_;
53  extract_removed_indices_ = true;
54  applyFilter (indices);
55  extract_removed_indices_ = temp;
56  copyPointCloud (*input_, output);
57 
58  for (size_t rii = 0; rii < removed_indices_->size (); ++rii)
59  {
60  PointT &pt_to_remove = output.at ((*removed_indices_)[rii]);
61  pt_to_remove.x = pt_to_remove.y = pt_to_remove.z = user_filter_value_;
62  if (!pcl_isfinite (user_filter_value_))
63  output.is_dense = false;
64  }
65  }
66  else
67  {
68  output.is_dense = true;
69  applyFilter (indices);
70  copyPointCloud (*input_, indices, output);
71  }
72 }
73 
74 ///////////////////////////////////////////////////////////////////////////////
75 template <typename PointT> void
76 pcl::FrustumCulling<PointT>::applyFilter (std::vector<int> &indices)
77 {
78  Eigen::Vector4f pl_n; // near plane
79  Eigen::Vector4f pl_f; // far plane
80  Eigen::Vector4f pl_t; // top plane
81  Eigen::Vector4f pl_b; // bottom plane
82  Eigen::Vector4f pl_r; // right plane
83  Eigen::Vector4f pl_l; // left plane
84 
85  Eigen::Vector3f view = camera_pose_.block (0, 0, 3, 1); // view vector for the camera - first column of the rotation matrix
86  Eigen::Vector3f up = camera_pose_.block (0, 1, 3, 1); // up vector for the camera - second column of the rotation matrix
87  Eigen::Vector3f right = camera_pose_.block (0, 2, 3, 1); // right vector for the camera - third column of the rotation matrix
88  Eigen::Vector3f T = camera_pose_.block (0, 3, 3, 1); // The (X, Y, Z) position of the camera w.r.t origin
89 
90 
91  float vfov_rad = float (vfov_ * M_PI / 180); // degrees to radians
92  float hfov_rad = float (hfov_ * M_PI / 180); // degrees to radians
93 
94  float np_h = float (2 * tan (vfov_rad / 2) * np_dist_); // near plane height
95  float np_w = float (2 * tan (hfov_rad / 2) * np_dist_); // near plane width
96 
97  float fp_h = float (2 * tan (vfov_rad / 2) * fp_dist_); // far plane height
98  float fp_w = float (2 * tan (hfov_rad / 2) * fp_dist_); // far plane width
99 
100  Eigen::Vector3f fp_c (T + view * fp_dist_); // far plane center
101  Eigen::Vector3f fp_tl (fp_c + (up * fp_h / 2) - (right * fp_w / 2)); // Top left corner of the far plane
102  Eigen::Vector3f fp_tr (fp_c + (up * fp_h / 2) + (right * fp_w / 2)); // Top right corner of the far plane
103  Eigen::Vector3f fp_bl (fp_c - (up * fp_h / 2) - (right * fp_w / 2)); // Bottom left corner of the far plane
104  Eigen::Vector3f fp_br (fp_c - (up * fp_h / 2) + (right * fp_w / 2)); // Bottom right corner of the far plane
105 
106  Eigen::Vector3f np_c (T + view * np_dist_); // near plane center
107  //Eigen::Vector3f np_tl = np_c + (up * np_h/2) - (right * np_w/2); // Top left corner of the near plane
108  Eigen::Vector3f np_tr (np_c + (up * np_h / 2) + (right * np_w / 2)); // Top right corner of the near plane
109  Eigen::Vector3f np_bl (np_c - (up * np_h / 2) - (right * np_w / 2)); // Bottom left corner of the near plane
110  Eigen::Vector3f np_br (np_c - (up * np_h / 2) + (right * np_w / 2)); // Bottom right corner of the near plane
111 
112  pl_f.block (0, 0, 3, 1).matrix () = (fp_bl - fp_br).cross (fp_tr - fp_br); // Far plane equation - cross product of the
113  pl_f (3) = -fp_c.dot (pl_f.block (0, 0, 3, 1)); // perpendicular edges of the far plane
114 
115  pl_n.block (0, 0, 3, 1).matrix () = (np_tr - np_br).cross (np_bl - np_br); // Near plane equation - cross product of the
116  pl_n (3) = -np_c.dot (pl_n.block (0, 0, 3, 1)); // perpendicular edges of the far plane
117 
118  Eigen::Vector3f a (fp_bl - T); // Vector connecting the camera and far plane bottom left
119  Eigen::Vector3f b (fp_br - T); // Vector connecting the camera and far plane bottom right
120  Eigen::Vector3f c (fp_tr - T); // Vector connecting the camera and far plane top right
121  Eigen::Vector3f d (fp_tl - T); // Vector connecting the camera and far plane top left
122 
123  // Frustum and the vectors a, b, c and d. T is the position of the camera
124  // _________
125  // /| . |
126  // d / | c . |
127  // / | __._____|
128  // / / . .
129  // a <---/-/ . .
130  // / / . . b
131  // / .
132  // .
133  // T
134  //
135 
136  pl_r.block (0, 0, 3, 1).matrix () = b.cross (c);
137  pl_l.block (0, 0, 3, 1).matrix () = d.cross (a);
138  pl_t.block (0, 0, 3, 1).matrix () = c.cross (d);
139  pl_b.block (0, 0, 3, 1).matrix () = a.cross (b);
140 
141  pl_r (3) = -T.dot (pl_r.block (0, 0, 3, 1));
142  pl_l (3) = -T.dot (pl_l.block (0, 0, 3, 1));
143  pl_t (3) = -T.dot (pl_t.block (0, 0, 3, 1));
144  pl_b (3) = -T.dot (pl_b.block (0, 0, 3, 1));
145 
146  if (extract_removed_indices_)
147  {
148  removed_indices_->resize (indices_->size ());
149  }
150  indices.resize (indices_->size ());
151  size_t indices_ctr = 0;
152  size_t removed_ctr = 0;
153  for (size_t i = 0; i < indices_->size (); i++)
154  {
155  int idx = indices_->at (i);
156  Eigen::Vector4f pt (input_->points[idx].x,
157  input_->points[idx].y,
158  input_->points[idx].z,
159  1.0f);
160  bool is_in_fov = (pt.dot (pl_l) <= 0) &&
161  (pt.dot (pl_r) <= 0) &&
162  (pt.dot (pl_t) <= 0) &&
163  (pt.dot (pl_b) <= 0) &&
164  (pt.dot (pl_f) <= 0) &&
165  (pt.dot (pl_n) <= 0);
166  if (is_in_fov ^ negative_)
167  {
168  indices[indices_ctr++] = idx;
169  }
170  else if (extract_removed_indices_)
171  {
172  (*removed_indices_)[removed_ctr++] = idx;
173  }
174  }
175  indices.resize (indices_ctr);
176  removed_indices_->resize (removed_ctr);
177 }
178 
179 #define PCL_INSTANTIATE_FrustumCulling(T) template class PCL_EXPORTS pcl::FrustumCulling<T>;
180 
181 #endif
PCL_EXPORTS void copyPointCloud(const pcl::PCLPointCloud2 &cloud_in, const std::vector< int > &indices, pcl::PCLPointCloud2 &cloud_out)
Extract the indices of a given point cloud as a new point cloud.
void applyFilter(PointCloud &output)
Sample of point indices into a separate PointCloud.
PointCloud represents the base class in PCL for storing collections of 3D points. ...
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields)...
Definition: point_cloud.h:418
A point structure representing Euclidean xyz coordinates, and the RGB color.
const PointT & at(int column, int row) const
Obtain the point given by the (column, row) coordinates.
Definition: point_cloud.h:283