Point Cloud Library (PCL)  1.6.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
passthrough.hpp
Go to the documentation of this file.
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 Willow Garage, Inc. 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: passthrough.hpp 6144 2012-07-04 22:06:28Z rusu $
37  *
38  */
39 
40 #ifndef PCL_FILTERS_IMPL_PASSTHROUGH_HPP_
41 #define PCL_FILTERS_IMPL_PASSTHROUGH_HPP_
42 
44 #include <pcl/common/io.h>
45 
47 template <typename PointT> void
49 {
50  std::vector<int> indices;
51  if (keep_organized_)
52  {
53  bool temp = extract_removed_indices_;
54  extract_removed_indices_ = true;
55  applyFilterIndices (indices);
56  extract_removed_indices_ = temp;
57 
58  output = *input_;
59  for (int rii = 0; rii < static_cast<int> (removed_indices_->size ()); ++rii) // rii = removed indices iterator
60  output.points[(*removed_indices_)[rii]].x = output.points[(*removed_indices_)[rii]].y = output.points[(*removed_indices_)[rii]].z = user_filter_value_;
61  if (!pcl_isfinite (user_filter_value_))
62  output.is_dense = false;
63  }
64  else
65  {
66  applyFilterIndices (indices);
67  copyPointCloud (*input_, indices, output);
68  }
69 }
70 
72 template <typename PointT> void
73 pcl::PassThrough<PointT>::applyFilterIndices (std::vector<int> &indices)
74 {
75  // The arrays to be used
76  indices.resize (indices_->size ());
77  removed_indices_->resize (indices_->size ());
78  int oii = 0, rii = 0; // oii = output indices iterator, rii = removed indices iterator
79 
80  // Has a field name been specified?
81  if (filter_field_name_.empty ())
82  {
83  // Only filter for non-finite entries then
84  for (int iii = 0; iii < static_cast<int> (indices_->size ()); ++iii) // iii = input indices iterator
85  {
86  // Non-finite entries are always passed to removed indices
87  if (!pcl_isfinite (input_->points[(*indices_)[iii]].x) ||
88  !pcl_isfinite (input_->points[(*indices_)[iii]].y) ||
89  !pcl_isfinite (input_->points[(*indices_)[iii]].z))
90  {
91  if (extract_removed_indices_)
92  (*removed_indices_)[rii++] = (*indices_)[iii];
93  continue;
94  }
95  indices[oii++] = (*indices_)[iii];
96  }
97  }
98  else
99  {
100  // Attempt to get the field name's index
101  std::vector<sensor_msgs::PointField> fields;
102  int distance_idx = pcl::getFieldIndex (*input_, filter_field_name_, fields);
103  if (distance_idx == -1)
104  {
105  PCL_WARN ("[pcl::%s::applyFilter] Unable to find field name in point type.\n", getClassName ().c_str ());
106  indices.clear ();
107  removed_indices_->clear ();
108  return;
109  }
110 
111  // Filter for non-finite entries and the specified field limits
112  for (int iii = 0; iii < static_cast<int> (indices_->size ()); ++iii) // iii = input indices iterator
113  {
114  // Non-finite entries are always passed to removed indices
115  if (!pcl_isfinite (input_->points[(*indices_)[iii]].x) ||
116  !pcl_isfinite (input_->points[(*indices_)[iii]].y) ||
117  !pcl_isfinite (input_->points[(*indices_)[iii]].z))
118  {
119  if (extract_removed_indices_)
120  (*removed_indices_)[rii++] = (*indices_)[iii];
121  continue;
122  }
123 
124  // Get the field's value
125  const uint8_t* pt_data = reinterpret_cast<const uint8_t*> (&input_->points[(*indices_)[iii]]);
126  float field_value = 0;
127  memcpy (&field_value, pt_data + fields[distance_idx].offset, sizeof (float));
128 
129  // Outside of the field limits are passed to removed indices
130  if (!negative_ && (field_value < filter_limit_min_ || field_value > filter_limit_max_))
131  {
132  if (extract_removed_indices_)
133  (*removed_indices_)[rii++] = (*indices_)[iii];
134  continue;
135  }
136 
137  // Inside of the field limits are passed to removed indices if negative was set
138  if (negative_ && field_value > filter_limit_min_ && field_value < filter_limit_max_)
139  {
140  if (extract_removed_indices_)
141  (*removed_indices_)[rii++] = (*indices_)[iii];
142  continue;
143  }
144 
145  // Otherwise it was a normal point for output (inlier)
146  indices[oii++] = (*indices_)[iii];
147  }
148  }
149 
150  // Resize the output arrays
151  indices.resize (oii);
152  removed_indices_->resize (rii);
153 }
154 
155 #define PCL_INSTANTIATE_PassThrough(T) template class PCL_EXPORTS pcl::PassThrough<T>;
156 
157 #endif // PCL_FILTERS_IMPL_PASSTHROUGH_HPP_
158