Point Cloud Library (PCL)  1.11.1-dev
extract_indices.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_EXTRACT_INDICES_HPP_
41 #define PCL_FILTERS_IMPL_EXTRACT_INDICES_HPP_
42 
43 #include <pcl/filters/extract_indices.h>
44 
45 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
46 template <typename PointT> void
48 {
49  std::vector<int> indices;
50  bool temp = extract_removed_indices_;
51  extract_removed_indices_ = true;
52  this->setInputCloud (cloud);
53  applyFilterIndices (indices);
54  extract_removed_indices_ = temp;
55 
56  std::vector<pcl::PCLPointField> fields;
57  pcl::for_each_type<FieldList> (pcl::detail::FieldAdder<PointT> (fields));
58  for (int rii = 0; rii < static_cast<int> (removed_indices_->size ()); ++rii) // rii = removed indices iterator
59  {
60  std::size_t pt_index = (std::size_t) (*removed_indices_)[rii];
61  if (pt_index >= input_->size ())
62  {
63  PCL_ERROR ("[pcl::%s::filterDirectly] The index exceeds the size of the input. Do nothing.\n",
64  getClassName ().c_str ());
65  *cloud = *input_;
66  return;
67  }
68  std::uint8_t* pt_data = reinterpret_cast<std::uint8_t*> (&(*cloud)[pt_index]);
69  for (const auto &field : fields)
70  memcpy (pt_data + field.offset, &user_filter_value_, sizeof (float));
71  }
72  if (!std::isfinite (user_filter_value_))
73  cloud->is_dense = false;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
77 template <typename PointT> void
79 {
80  std::vector<int> indices;
81  if (keep_organized_)
82  {
83  bool temp = extract_removed_indices_;
84  extract_removed_indices_ = true;
85  applyFilterIndices (indices);
86  extract_removed_indices_ = temp;
87 
88  output = *input_;
89  std::vector<pcl::PCLPointField> fields;
90  pcl::for_each_type<FieldList> (pcl::detail::FieldAdder<PointT> (fields));
91  for (const auto ri : *removed_indices_) // ri = removed index
92  {
93  std::size_t pt_index = (std::size_t)ri;
94  if (pt_index >= input_->size ())
95  {
96  PCL_ERROR ("[pcl::%s::applyFilter] The index exceeds the size of the input. Do nothing.\n",
97  getClassName ().c_str ());
98  output = *input_;
99  return;
100  }
101  std::uint8_t* pt_data = reinterpret_cast<std::uint8_t*> (&output[pt_index]);
102  for (const auto &field : fields)
103  memcpy (pt_data + field.offset, &user_filter_value_, sizeof (float));
104  }
105  if (!std::isfinite (user_filter_value_))
106  output.is_dense = false;
107  }
108  else
109  {
110  applyFilterIndices (indices);
111  copyPointCloud (*input_, indices, output);
112  }
113 }
114 
115 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
116 template <typename PointT> void
118 {
119  if (indices_->size () > input_->size ())
120  {
121  PCL_ERROR ("[pcl::%s::applyFilter] The indices size exceeds the size of the input.\n", getClassName ().c_str ());
122  indices.clear ();
123  removed_indices_->clear ();
124  return;
125  }
126 
127  if (!negative_) // Normal functionality
128  {
129  indices = *indices_;
130 
131  if (extract_removed_indices_)
132  {
133  // Set up the full indices set
134  std::vector<int> full_indices (input_->size ());
135  for (int fii = 0; fii < static_cast<int> (full_indices.size ()); ++fii) // fii = full indices iterator
136  full_indices[fii] = fii;
137 
138  // Set up the sorted input indices
139  std::vector<int> sorted_input_indices = *indices_;
140  std::sort (sorted_input_indices.begin (), sorted_input_indices.end ());
141 
142  // Store the difference in removed_indices
143  removed_indices_->clear ();
144  set_difference (full_indices.begin (), full_indices.end (), sorted_input_indices.begin (), sorted_input_indices.end (), inserter (*removed_indices_, removed_indices_->begin ()));
145  }
146  }
147  else // Inverted functionality
148  {
149  // Set up the full indices set
150  std::vector<int> full_indices (input_->size ());
151  for (int fii = 0; fii < static_cast<int> (full_indices.size ()); ++fii) // fii = full indices iterator
152  full_indices[fii] = fii;
153 
154  // Set up the sorted input indices
155  std::vector<int> sorted_input_indices = *indices_;
156  std::sort (sorted_input_indices.begin (), sorted_input_indices.end ());
157 
158  // Store the difference in indices
159  indices.clear ();
160  set_difference (full_indices.begin (), full_indices.end (), sorted_input_indices.begin (), sorted_input_indices.end (), inserter (indices, indices.begin ()));
161 
162  if (extract_removed_indices_)
163  removed_indices_ = indices_;
164  }
165 }
166 
167 #define PCL_INSTANTIATE_ExtractIndices(T) template class PCL_EXPORTS pcl::ExtractIndices<T>;
168 
169 #endif // PCL_FILTERS_IMPL_EXTRACT_INDICES_HPP_
170 
pcl::PCLBase::PointCloudPtr
typename PointCloud::Ptr PointCloudPtr
Definition: pcl_base.h:73
pcl::PointCloud
PointCloud represents the base class in PCL for storing collections of 3D points.
Definition: distances.h:55
pcl::copyPointCloud
void copyPointCloud(const pcl::PointCloud< PointInT > &cloud_in, pcl::PointCloud< PointOutT > &cloud_out)
Copy all the fields from a given point cloud into a new point cloud.
Definition: io.hpp:121
pcl::PointCloud::is_dense
bool is_dense
True if no points are invalid (e.g., have NaN or Inf values in any of their floating point fields).
Definition: point_cloud.h:418
pcl::uint8_t
std::uint8_t uint8_t
Definition: types.h:54
pcl::ExtractIndices::filterDirectly
void filterDirectly(PointCloudPtr &cloud)
Apply the filter and store the results directly in the input cloud.
Definition: extract_indices.hpp:47
pcl::ExtractIndices::applyFilterIndices
void applyFilterIndices(std::vector< int > &indices)
Filtered results are indexed by an indices array.
Definition: extract_indices.hpp:117
pcl::detail::FieldAdder
Definition: conversions.h:63
pcl::ExtractIndices::applyFilter
void applyFilter(PointCloud &output) override
Filtered results are stored in a separate point cloud.
Definition: extract_indices.hpp:78