Point Cloud Library (PCL)  1.11.0
crop_box.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2009-2011, Willow Garage, Inc.
6  * Copyright (c) 2015, Google, Inc.
7  *
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of the copyright holder(s) nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *
37  * $Id: extract_indices.hpp 1897 2011-07-26 20:35:49Z rusu $
38  *
39  */
40 
41 #ifndef PCL_FILTERS_IMPL_CROP_BOX_H_
42 #define PCL_FILTERS_IMPL_CROP_BOX_H_
43 
44 #include <pcl/filters/crop_box.h>
45 #include <pcl/common/io.h>
46 
47 ///////////////////////////////////////////////////////////////////////////////
48 template<typename PointT> void
49 pcl::CropBox<PointT>::applyFilter (std::vector<int> &indices)
50 {
51  indices.resize (input_->points.size ());
52  removed_indices_->resize (input_->points.size ());
53  int indices_count = 0;
54  int removed_indices_count = 0;
55 
56  Eigen::Affine3f transform = Eigen::Affine3f::Identity ();
57  Eigen::Affine3f inverse_transform = Eigen::Affine3f::Identity ();
58 
59  if (rotation_ != Eigen::Vector3f::Zero ())
60  {
61  pcl::getTransformation (0, 0, 0,
62  rotation_ (0), rotation_ (1), rotation_ (2),
63  transform);
64  inverse_transform = transform.inverse ();
65  }
66 
67  bool transform_matrix_is_identity = transform_.matrix ().isIdentity ();
68  bool translation_is_zero = (translation_ == Eigen::Vector3f::Zero ());
69  bool inverse_transform_matrix_is_identity = inverse_transform.matrix ().isIdentity ();
70 
71  for (const auto index : *indices_)
72  {
73  if (!input_->is_dense)
74  // Check if the point is invalid
75  if (!isFinite (input_->points[index]))
76  continue;
77 
78  // Get local point
79  PointT local_pt = input_->points[index];
80 
81  // Transform point to world space
82  if (!transform_matrix_is_identity)
83  local_pt = pcl::transformPoint<PointT> (local_pt, transform_);
84 
85  if (!translation_is_zero)
86  {
87  local_pt.x -= translation_ (0);
88  local_pt.y -= translation_ (1);
89  local_pt.z -= translation_ (2);
90  }
91 
92  // Transform point to local space of crop box
93  if (!inverse_transform_matrix_is_identity)
94  local_pt = pcl::transformPoint<PointT> (local_pt, inverse_transform);
95 
96  // If outside the cropbox
97  if ( (local_pt.x < min_pt_[0] || local_pt.y < min_pt_[1] || local_pt.z < min_pt_[2]) ||
98  (local_pt.x > max_pt_[0] || local_pt.y > max_pt_[1] || local_pt.z > max_pt_[2]))
99  {
100  if (negative_)
101  indices[indices_count++] = index;
102  else if (extract_removed_indices_)
103  (*removed_indices_)[removed_indices_count++] = static_cast<int> (index);
104  }
105  // If inside the cropbox
106  else
107  {
108  if (negative_ && extract_removed_indices_)
109  (*removed_indices_)[removed_indices_count++] = static_cast<int> (index);
110  else if (!negative_)
111  indices[indices_count++] = index;
112  }
113  }
114  indices.resize (indices_count);
115  removed_indices_->resize (removed_indices_count);
116 }
117 
118 #define PCL_INSTANTIATE_CropBox(T) template class PCL_EXPORTS pcl::CropBox<T>;
119 
120 #endif // PCL_FILTERS_IMPL_CROP_BOX_H_
void applyFilter(std::vector< int > &indices) override
Sample of point indices.
Definition: crop_box.hpp:49
void getTransformation(Scalar x, Scalar y, Scalar z, Scalar roll, Scalar pitch, Scalar yaw, Eigen::Transform< Scalar, 3, Eigen::Affine > &t)
Create a transformation from the given translation and Euler angles (XYZ-convention)
Definition: eigen.hpp:608
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
Definition: point_tests.h:55
A point structure representing Euclidean xyz coordinates, and the RGB color.