00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef PCL_FILTERS_IMPL_FRUSTUM_CULLING_HPP_
00039 #define PCL_FILTERS_IMPL_FRUSTUM_CULLING_HPP_
00040
00041 #include <pcl/filters/frustum_culling.h>
00042 #include <pcl/common/io.h>
00043 #include <vector>
00044
00046 template <typename PointT> void
00047 pcl::FrustumCulling<PointT>::applyFilter (PointCloud& output)
00048 {
00049 std::vector<int> indices;
00050 if (keep_organized_)
00051 {
00052 bool temp = extract_removed_indices_;
00053 extract_removed_indices_ = true;
00054 applyFilter (indices);
00055 extract_removed_indices_ = temp;
00056 copyPointCloud (*input_, output);
00057
00058 for (size_t rii = 0; rii < removed_indices_->size (); ++rii)
00059 {
00060 PointT &pt_to_remove = output.at ((*removed_indices_)[rii]);
00061 pt_to_remove.x = pt_to_remove.y = pt_to_remove.z = user_filter_value_;
00062 if (!pcl_isfinite (user_filter_value_))
00063 output.is_dense = false;
00064 }
00065 }
00066 else
00067 {
00068 output.is_dense = true;
00069 applyFilter (indices);
00070 copyPointCloud (*input_, indices, output);
00071 }
00072 }
00073
00075 template <typename PointT> void
00076 pcl::FrustumCulling<PointT>::applyFilter (std::vector<int> &indices)
00077 {
00078 Eigen::Vector4f pl_n;
00079 Eigen::Vector4f pl_f;
00080 Eigen::Vector4f pl_t;
00081 Eigen::Vector4f pl_b;
00082 Eigen::Vector4f pl_r;
00083 Eigen::Vector4f pl_l;
00084
00085 Eigen::Vector3f view = camera_pose_.block (0, 0, 3, 1);
00086 Eigen::Vector3f up = camera_pose_.block (0, 1, 3, 1);
00087 Eigen::Vector3f right = camera_pose_.block (0, 2, 3, 1);
00088 Eigen::Vector3f T = camera_pose_.block (0, 3, 3, 1);
00089
00090
00091 float vfov_rad = float (vfov_ * M_PI / 180);
00092 float hfov_rad = float (hfov_ * M_PI / 180);
00093
00094 float np_h = float (2 * tan (vfov_rad / 2) * np_dist_);
00095 float np_w = float (2 * tan (hfov_rad / 2) * np_dist_);
00096
00097 float fp_h = float (2 * tan (vfov_rad / 2) * fp_dist_);
00098 float fp_w = float (2 * tan (hfov_rad / 2) * fp_dist_);
00099
00100 Eigen::Vector3f fp_c (T + view * fp_dist_);
00101 Eigen::Vector3f fp_tl (fp_c + (up * fp_h / 2) - (right * fp_w / 2));
00102 Eigen::Vector3f fp_tr (fp_c + (up * fp_h / 2) + (right * fp_w / 2));
00103 Eigen::Vector3f fp_bl (fp_c - (up * fp_h / 2) - (right * fp_w / 2));
00104 Eigen::Vector3f fp_br (fp_c - (up * fp_h / 2) + (right * fp_w / 2));
00105
00106 Eigen::Vector3f np_c (T + view * np_dist_);
00107
00108 Eigen::Vector3f np_tr (np_c + (up * np_h / 2) + (right * np_w / 2));
00109 Eigen::Vector3f np_bl (np_c - (up * np_h / 2) - (right * np_w / 2));
00110 Eigen::Vector3f np_br (np_c - (up * np_h / 2) + (right * np_w / 2));
00111
00112 pl_f.block (0, 0, 3, 1).matrix () = (fp_bl - fp_br).cross (fp_tr - fp_br);
00113 pl_f (3) = -fp_c.dot (pl_f.block (0, 0, 3, 1));
00114
00115 pl_n.block (0, 0, 3, 1).matrix () = (np_tr - np_br).cross (np_bl - np_br);
00116 pl_n (3) = -np_c.dot (pl_n.block (0, 0, 3, 1));
00117
00118 Eigen::Vector3f a (fp_bl - T);
00119 Eigen::Vector3f b (fp_br - T);
00120 Eigen::Vector3f c (fp_tr - T);
00121 Eigen::Vector3f d (fp_tl - T);
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136 pl_r.block (0, 0, 3, 1).matrix () = b.cross (c);
00137 pl_l.block (0, 0, 3, 1).matrix () = d.cross (a);
00138 pl_t.block (0, 0, 3, 1).matrix () = c.cross (d);
00139 pl_b.block (0, 0, 3, 1).matrix () = a.cross (b);
00140
00141 pl_r (3) = -T.dot (pl_r.block (0, 0, 3, 1));
00142 pl_l (3) = -T.dot (pl_l.block (0, 0, 3, 1));
00143 pl_t (3) = -T.dot (pl_t.block (0, 0, 3, 1));
00144 pl_b (3) = -T.dot (pl_b.block (0, 0, 3, 1));
00145
00146 if (extract_removed_indices_)
00147 {
00148 removed_indices_->resize (indices_->size ());
00149 }
00150 indices.resize (indices_->size ());
00151 size_t indices_ctr = 0;
00152 size_t removed_ctr = 0;
00153 for (size_t i = 0; i < indices_->size (); i++)
00154 {
00155 int idx = indices_->at (i);
00156 Eigen::Vector4f pt (input_->points[idx].x,
00157 input_->points[idx].y,
00158 input_->points[idx].z,
00159 1.0f);
00160 bool is_in_fov = (pt.dot (pl_l) <= 0) &&
00161 (pt.dot (pl_r) <= 0) &&
00162 (pt.dot (pl_t) <= 0) &&
00163 (pt.dot (pl_b) <= 0) &&
00164 (pt.dot (pl_f) <= 0) &&
00165 (pt.dot (pl_n) <= 0);
00166 if (is_in_fov ^ negative_)
00167 {
00168 indices[indices_ctr++] = idx;
00169 }
00170 else if (extract_removed_indices_)
00171 {
00172 (*removed_indices_)[removed_ctr++] = idx;
00173 }
00174 }
00175 indices.resize (indices_ctr);
00176 removed_indices_->resize (removed_ctr);
00177 }
00178
00179 #define PCL_INSTANTIATE_FrustumCulling(T) template class PCL_EXPORTS pcl::FrustumCulling<T>;
00180
00181 #endif