Point Cloud Library (PCL)  1.11.0
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
47 pcl::FrustumCulling<PointT>::applyFilter (std::vector<int> &indices)
48 {
49  Eigen::Vector4f pl_n; // near plane
50  Eigen::Vector4f pl_f; // far plane
51  Eigen::Vector4f pl_t; // top plane
52  Eigen::Vector4f pl_b; // bottom plane
53  Eigen::Vector4f pl_r; // right plane
54  Eigen::Vector4f pl_l; // left plane
55 
56  Eigen::Vector3f view = camera_pose_.block<3, 1> (0, 0); // view vector for the camera - first column of the rotation matrix
57  Eigen::Vector3f up = camera_pose_.block<3, 1> (0, 1); // up vector for the camera - second column of the rotation matrix
58  Eigen::Vector3f right = camera_pose_.block<3, 1> (0, 2); // right vector for the camera - third column of the rotation matrix
59  Eigen::Vector3f T = camera_pose_.block<3, 1> (0, 3); // The (X, Y, Z) position of the camera w.r.t origin
60 
61 
62  float vfov_rad = float (vfov_ * M_PI / 180); // degrees to radians
63  float hfov_rad = float (hfov_ * M_PI / 180); // degrees to radians
64 
65  float np_h = float (2 * tan (vfov_rad / 2) * np_dist_); // near plane height
66  float np_w = float (2 * tan (hfov_rad / 2) * np_dist_); // near plane width
67 
68  float fp_h = float (2 * tan (vfov_rad / 2) * fp_dist_); // far plane height
69  float fp_w = float (2 * tan (hfov_rad / 2) * fp_dist_); // far plane width
70 
71  Eigen::Vector3f fp_c (T + view * fp_dist_); // far plane center
72  Eigen::Vector3f fp_tl (fp_c + (up * fp_h / 2) - (right * fp_w / 2)); // Top left corner of the far plane
73  Eigen::Vector3f fp_tr (fp_c + (up * fp_h / 2) + (right * fp_w / 2)); // Top right corner of the far plane
74  Eigen::Vector3f fp_bl (fp_c - (up * fp_h / 2) - (right * fp_w / 2)); // Bottom left corner of the far plane
75  Eigen::Vector3f fp_br (fp_c - (up * fp_h / 2) + (right * fp_w / 2)); // Bottom right corner of the far plane
76 
77  Eigen::Vector3f np_c (T + view * np_dist_); // near plane center
78  //Eigen::Vector3f np_tl = np_c + (up * np_h/2) - (right * np_w/2); // Top left corner of the near plane
79  Eigen::Vector3f np_tr (np_c + (up * np_h / 2) + (right * np_w / 2)); // Top right corner of the near plane
80  Eigen::Vector3f np_bl (np_c - (up * np_h / 2) - (right * np_w / 2)); // Bottom left corner of the near plane
81  Eigen::Vector3f np_br (np_c - (up * np_h / 2) + (right * np_w / 2)); // Bottom right corner of the near plane
82 
83  pl_f.head<3> () = (fp_bl - fp_br).cross (fp_tr - fp_br); // Far plane equation - cross product of the
84  pl_f (3) = -fp_c.dot (pl_f.head<3> ()); // perpendicular edges of the far plane
85 
86  pl_n.head<3> () = (np_tr - np_br).cross (np_bl - np_br); // Near plane equation - cross product of the
87  pl_n (3) = -np_c.dot (pl_n.head<3> ()); // perpendicular edges of the far plane
88 
89  Eigen::Vector3f a (fp_bl - T); // Vector connecting the camera and far plane bottom left
90  Eigen::Vector3f b (fp_br - T); // Vector connecting the camera and far plane bottom right
91  Eigen::Vector3f c (fp_tr - T); // Vector connecting the camera and far plane top right
92  Eigen::Vector3f d (fp_tl - T); // Vector connecting the camera and far plane top left
93 
94  // Frustum and the vectors a, b, c and d. T is the position of the camera
95  // _________
96  // /| . |
97  // d / | c . |
98  // / | __._____|
99  // / / . .
100  // a <---/-/ . .
101  // / / . . b
102  // / .
103  // .
104  // T
105  //
106 
107  pl_r.head<3> () = b.cross (c);
108  pl_l.head<3> () = d.cross (a);
109  pl_t.head<3> () = c.cross (d);
110  pl_b.head<3> () = a.cross (b);
111 
112  pl_r (3) = -T.dot (pl_r.head<3> ());
113  pl_l (3) = -T.dot (pl_l.head<3> ());
114  pl_t (3) = -T.dot (pl_t.head<3> ());
115  pl_b (3) = -T.dot (pl_b.head<3> ());
116 
117  if (extract_removed_indices_)
118  {
119  removed_indices_->resize (indices_->size ());
120  }
121  indices.resize (indices_->size ());
122  std::size_t indices_ctr = 0;
123  std::size_t removed_ctr = 0;
124  for (std::size_t i = 0; i < indices_->size (); i++)
125  {
126  int idx = indices_->at (i);
127  Eigen::Vector4f pt (input_->points[idx].x,
128  input_->points[idx].y,
129  input_->points[idx].z,
130  1.0f);
131  bool is_in_fov = (pt.dot (pl_l) <= 0) &&
132  (pt.dot (pl_r) <= 0) &&
133  (pt.dot (pl_t) <= 0) &&
134  (pt.dot (pl_b) <= 0) &&
135  (pt.dot (pl_f) <= 0) &&
136  (pt.dot (pl_n) <= 0);
137  if (is_in_fov ^ negative_)
138  {
139  indices[indices_ctr++] = idx;
140  }
141  else if (extract_removed_indices_)
142  {
143  (*removed_indices_)[removed_ctr++] = idx;
144  }
145  }
146  indices.resize (indices_ctr);
147  removed_indices_->resize (removed_ctr);
148 }
149 
150 #define PCL_INSTANTIATE_FrustumCulling(T) template class PCL_EXPORTS pcl::FrustumCulling<T>;
151 
152 #endif
void applyFilter(std::vector< int > &indices) override
Sample of point indices.