Point Cloud Library (PCL)  1.11.0
passthrough.hpp
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, 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  * $Id$
37  *
38  */
39 
40 #ifndef PCL_FILTERS_IMPL_PASSTHROUGH_HPP_
41 #define PCL_FILTERS_IMPL_PASSTHROUGH_HPP_
42 
43 #include <pcl/filters/passthrough.h>
44 #include <pcl/common/io.h>
45 
46 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
47 template <typename PointT> void
49 {
50  // The arrays to be used
51  indices.resize (indices_->size ());
52  removed_indices_->resize (indices_->size ());
53  int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator
54 
55  // Has a field name been specified?
56  if (filter_field_name_.empty ())
57  {
58  // Only filter for non-finite entries then
59  for (const auto ii : *indices_) // ii = input index
60  {
61  // Non-finite entries are always passed to removed indices
62  if (!std::isfinite (input_->points[ii].x) ||
63  !std::isfinite (input_->points[ii].y) ||
64  !std::isfinite (input_->points[ii].z))
65  {
66  if (extract_removed_indices_)
67  (*removed_indices_)[rii++] = ii;
68  continue;
69  }
70  indices[oii++] = ii;
71  }
72  }
73  else
74  {
75  // Attempt to get the field name's index
76  std::vector<pcl::PCLPointField> fields;
77  int distance_idx = pcl::getFieldIndex<PointT> (filter_field_name_, fields);
78  if (distance_idx == -1)
79  {
80  PCL_WARN ("[pcl::%s::applyFilter] Unable to find field name in point type.\n", getClassName ().c_str ());
81  indices.clear ();
82  removed_indices_->clear ();
83  return;
84  }
85 
86  // Filter for non-finite entries and the specified field limits
87  for (const auto ii : *indices_) // ii = input index
88  {
89  // Non-finite entries are always passed to removed indices
90  if (!std::isfinite (input_->points[ii].x) ||
91  !std::isfinite (input_->points[ii].y) ||
92  !std::isfinite (input_->points[ii].z))
93  {
94  if (extract_removed_indices_)
95  (*removed_indices_)[rii++] = ii;
96  continue;
97  }
98 
99  // Get the field's value
100  const std::uint8_t* pt_data = reinterpret_cast<const std::uint8_t*> (&input_->points[ii]);
101  float field_value = 0;
102  memcpy (&field_value, pt_data + fields[distance_idx].offset, sizeof (float));
103 
104  // Remove NAN/INF/-INF values. We expect passthrough to output clean valid data.
105  if (!std::isfinite (field_value))
106  {
107  if (extract_removed_indices_)
108  (*removed_indices_)[rii++] = ii;
109  continue;
110  }
111 
112  // Outside of the field limits are passed to removed indices
113  if (!negative_ && (field_value < filter_limit_min_ || field_value > filter_limit_max_))
114  {
115  if (extract_removed_indices_)
116  (*removed_indices_)[rii++] = ii;
117  continue;
118  }
119 
120  // Inside of the field limits are passed to removed indices if negative was set
121  if (negative_ && field_value >= filter_limit_min_ && field_value <= filter_limit_max_)
122  {
123  if (extract_removed_indices_)
124  (*removed_indices_)[rii++] = ii;
125  continue;
126  }
127 
128  // Otherwise it was a normal point for output (inlier)
129  indices[oii++] = ii;
130  }
131  }
132 
133  // Resize the output arrays
134  indices.resize (oii);
135  removed_indices_->resize (rii);
136 }
137 
138 #define PCL_INSTANTIATE_PassThrough(T) template class PCL_EXPORTS pcl::PassThrough<T>;
139 
140 #endif // PCL_FILTERS_IMPL_PASSTHROUGH_HPP_
141 
pcl::PassThrough::applyFilterIndices
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.
Definition: passthrough.hpp:48
pcl::uint8_t
std::uint8_t uint8_t
Definition: types.h:54