Point Cloud Library (PCL)  1.14.0-dev
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/eigen.h> // for getTransformation
46 #include <pcl/common/point_tests.h> // for isFinite
47 #include <pcl/common/transforms.h> // for transformPoint
48 
49 ///////////////////////////////////////////////////////////////////////////////
50 template<typename PointT> void
52 {
53  indices.resize (input_->size ());
54  removed_indices_->resize (input_->size ());
55  int indices_count = 0;
56  int removed_indices_count = 0;
57 
58  Eigen::Affine3f transform = Eigen::Affine3f::Identity ();
59  Eigen::Affine3f inverse_transform = Eigen::Affine3f::Identity ();
60 
61  if (rotation_ != Eigen::Vector3f::Zero ())
62  {
63  pcl::getTransformation (0, 0, 0,
64  rotation_ (0), rotation_ (1), rotation_ (2),
65  transform);
66  inverse_transform = transform.inverse ();
67  }
68 
69  bool transform_matrix_is_identity = transform_.matrix ().isIdentity ();
70  bool translation_is_zero = (translation_ == Eigen::Vector3f::Zero ());
71  bool inverse_transform_matrix_is_identity = inverse_transform.matrix ().isIdentity ();
72 
73  for (const auto index : *indices_)
74  {
75  if (!input_->is_dense)
76  // Check if the point is invalid
77  if (!isFinite ((*input_)[index]))
78  continue;
79 
80  // Get local point
81  PointT local_pt = (*input_)[index];
82 
83  // Transform point to world space
84  if (!transform_matrix_is_identity)
85  local_pt = pcl::transformPoint<PointT> (local_pt, transform_);
86 
87  if (!translation_is_zero)
88  {
89  local_pt.x -= translation_ (0);
90  local_pt.y -= translation_ (1);
91  local_pt.z -= translation_ (2);
92  }
93 
94  // Transform point to local space of crop box
95  if (!inverse_transform_matrix_is_identity)
96  local_pt = pcl::transformPoint<PointT> (local_pt, inverse_transform);
97 
98  // If outside the cropbox
99  if ( (local_pt.x < min_pt_[0] || local_pt.y < min_pt_[1] || local_pt.z < min_pt_[2]) ||
100  (local_pt.x > max_pt_[0] || local_pt.y > max_pt_[1] || local_pt.z > max_pt_[2]))
101  {
102  if (negative_)
103  indices[indices_count++] = index;
104  else if (extract_removed_indices_)
105  (*removed_indices_)[removed_indices_count++] = index;
106  }
107  // If inside the cropbox
108  else
109  {
110  if (negative_ && extract_removed_indices_)
111  (*removed_indices_)[removed_indices_count++] = index;
112  else if (!negative_)
113  indices[indices_count++] = index;
114  }
115  }
116  indices.resize (indices_count);
117  removed_indices_->resize (removed_indices_count);
118 }
119 
120 #define PCL_INSTANTIATE_CropBox(T) template class PCL_EXPORTS pcl::CropBox<T>;
121 
122 #endif // PCL_FILTERS_IMPL_CROP_BOX_H_
void applyFilter(Indices &indices) override
Sample of point indices.
Definition: crop_box.hpp:51
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 (intrinsic rotations,...
Definition: eigen.hpp:618
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
IndicesAllocator<> Indices
Type used for indices in PCL.
Definition: types.h:133
A point structure representing Euclidean xyz coordinates, and the RGB color.