libLAS API Reference  1.8.1
filter.hpp
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id$
3  *
4  * Project: libLAS - http://liblas.org - A BSD library for LAS format data.
5  * Purpose: LAS filter class
6  * Author: Howard Butler, hobu.inc@gmail.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2010, Howard Butler
10  *
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following
15  * conditions are met:
16  *
17  * * Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  * * Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in
21  * the documentation and/or other materials provided
22  * with the distribution.
23  * * Neither the name of the Martin Isenburg or Iowa Department
24  * of Natural Resources nor the names of its contributors may be
25  * used to endorse or promote products derived from this software
26  * without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
35  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
36  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
38  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
39  * OF SUCH DAMAGE.
40  ****************************************************************************/
41 
42 #ifndef LIBLAS_LASFILTER_HPP_INCLUDED
43 #define LIBLAS_LASFILTER_HPP_INCLUDED
44 
45 #include <liblas/version.hpp>
46 #include <liblas/header.hpp>
47 #include <liblas/point.hpp>
48 #include <liblas/detail/fwd.hpp>
49 #include <liblas/export.hpp>
50 // boost
51 #include <boost/function.hpp>
52 #include <boost/lexical_cast.hpp>
53 #include <boost/shared_ptr.hpp>
54 // std
55 #include <vector>
56 #include <functional>
57 #include <string>
58 
59 namespace liblas {
60 
63 {
64 public:
65 
69  {
70  eExclusion = 0,
71  eInclusion = 1
72  };
73 
77  virtual bool filter(const Point& point) = 0;
78 
81  void SetType(FilterType t) {m_type = t;}
82 
84  FilterType GetType() const {return m_type; }
85 
86  virtual ~FilterI() {}
87 
89  FilterI(FilterType t) : m_type(t) {}
90 
91 private:
92 
93  FilterI(FilterI const& other);
94  FilterI& operator=(FilterI const& rhs);
95 
96  FilterType m_type;
97 };
98 
99 typedef boost::shared_ptr<FilterI> FilterPtr;
100 
104 {
105 public:
106 
107  BoundsFilter(double minx, double miny, double maxx, double maxy);
108  BoundsFilter(double minx, double miny, double minz, double maxx, double maxy, double maxz);
109  BoundsFilter(Bounds<double> const& b);
110  bool filter(const Point& point);
111 
112 private:
113 
114  liblas::Bounds<double> bounds;
115 
116  BoundsFilter(BoundsFilter const& other);
117  BoundsFilter& operator=(BoundsFilter const& rhs);
118 };
119 
122 {
123 public:
124 
125  typedef std::vector<liblas::Classification> class_list_type;
126 
127  ClassificationFilter(class_list_type classes);
128  bool filter(const Point& point);
129 
130 private:
131 
132  class_list_type m_classes;
133 
135  ClassificationFilter& operator=(ClassificationFilter const& rhs);
136 };
137 
140 {
141 public:
142 
144  ThinFilter(uint32_t thin);
145  bool filter(const liblas::Point& point);
146 
147 
148 private:
149 
150  ThinFilter(ThinFilter const& other);
151  ThinFilter& operator=(ThinFilter const& rhs);
152 
153  uint32_t thin_amount;
154  uint32_t thin_count;
155 };
156 
157 
160 {
161 public:
162 
163  typedef std::vector<uint16_t> return_list_type;
164 
165  ReturnFilter(return_list_type returns, bool last_only);
166  bool filter(const Point& point);
167 
168 private:
169 
170  return_list_type m_returns;
171  bool last_only;
172 
173  ReturnFilter(ReturnFilter const& other);
174  ReturnFilter& operator=(ReturnFilter const& rhs);
175 };
176 
177 
179 {
180 public:
181 
183  bool filter(const Point& point);
184 
185 private:
186 
187  ValidationFilter(ValidationFilter const& other);
188  ValidationFilter& operator=(ValidationFilter const& rhs);
189 };
190 
191 
196 template <typename T>
198 {
199 
200 
201  //
202 public:
203  typedef boost::function<T (const Point*)> filter_func;
204  typedef boost::function<bool(T, T)> compare_func;
205 
214 
218 
223 
228  ContinuousValueFilter(filter_func f, T value, compare_func c)
229  : liblas::FilterI(eInclusion), f(f), c(c),value(value)
230  {}
231 
232 
245 
248 
254 
259 
260  ContinuousValueFilter(filter_func f, std::string const& filter_string)
261  : liblas::FilterI(eInclusion), f(f)
262  {
263  compare_func compare;
264 
265  bool gt = HasPredicate(filter_string, ">");
266  bool gte = HasPredicate(filter_string, ">=");
267  bool lt = HasPredicate(filter_string, "<");
268  bool lte = HasPredicate(filter_string, "<=");
269  bool eq = HasPredicate(filter_string, "==");
270 
271  std::string::size_type pos=0;
272  std::string out;
273 
274  if (gte) // >=
275  {
276  // std::cout<<"have gte!" << std::endl;
277  c = std::greater_equal<T>();
278  pos = filter_string.find_first_of("=") + 1;
279  }
280  else if (gt) // .
281  {
282  // std::cout<<"have gt!" << std::endl;
283  c = std::greater<T>();
284  pos = filter_string.find_first_of(">") + 1;
285  }
286  else if (lte) // <=
287  {
288  // std::cout<<"have lte!" << std::endl;
289  c = std::less_equal<T>();
290  pos = filter_string.find_first_of("=") +1;
291  }
292  else if (lt) // <
293  {
294  // std::cout<<"have le!" << std::endl;
295  c = std::less<T>();
296  pos = filter_string.find_first_of("<") + 1;
297  }
298  else if (eq) // ==
299  {
300  // std::cout<<"have eq!" << std::endl;
301  c = std::equal_to<T>();
302  pos = filter_string.find_last_of("=") + 1;
303 
304  }
305 
306  out = filter_string.substr(pos, filter_string.size());
307 
308  value = boost::lexical_cast<T>(out);
309  // std::cout << "Value is: " << value << " pos " << pos << " out " << out << std::endl;
310  }
311 
312  bool filter(const liblas::Point& p)
313  {
314  bool output = false;
315 
316  T v = f(&p);
317  // std::cout << std::endl<< "Checking c(v, value) v: " << v << " value: " << value;
318  if (c(v, value)){
319  // std::cout<< " ... succeeded "<<std::endl;
320  if (GetType() == eInclusion) {
321  output = true;
322  } else {
323  // std::cout << "Filter type is eExclusion and test passed" << std::endl;
324  output = false;
325  }
326  } else {
327  // std::cout<<" ... failed" <<std::endl;
328  if (GetType() == eInclusion) {
329  output = false;
330  } else {
331  // std::cout << "Filter type is eExclusion and test failed" << std::endl;
332  output = true;
333  }
334  }
335  // std::cout << " returning " << output << std::endl;
336  return output;
337  }
338 
339 private:
340 
342  ContinuousValueFilter& operator=(ContinuousValueFilter const& rhs);
343  filter_func f;
344  compare_func c;
345  T value;
346 
347  bool HasPredicate(std::string const& parse_string, std::string predicate)
348  {
349  // Check if the given string contains all of the characters of predicate
350  // For example, does '>=300' have both > and = (as given in the predicate string)
351  bool output = false;
352  // We must have all of the characters in the predicate to return true
353  for (std::string::const_iterator i = predicate.begin(); i!=predicate.end(); ++i) {
354  std::string::size_type pred = parse_string.find_first_of(*i);
355  if (pred != std::string::npos) {
356  output = true;
357  } else {
358  return false;
359  }
360  }
361  return output;
362  }
363 
364 
365 };
366 
369 {
370 public:
371 
372  ColorFilter(liblas::Color const& low,
373  liblas::Color const& high);
374 
376  liblas::Color::value_type high_red,
377  liblas::Color::value_type low_blue,
378  liblas::Color::value_type high_blue,
379  liblas::Color::value_type low_green,
380  liblas::Color::value_type high_green);
381  bool filter(const Point& point);
382 
383 private:
384 
385  liblas::Color m_low;
386  liblas::Color m_high;
387 
388  ColorFilter(ColorFilter const& other);
389  ColorFilter& operator=(ColorFilter const& rhs);
390  bool DoExclude();
391 };
392 
393 } // namespace liblas
394 
395 #endif // ndef LIBLAS_LASFILTER_HPP_INCLUDED
A filter for keeping or rejecting a list of return ids.
Definition: filter.hpp:159
#define LAS_DLL
Definition: export.hpp:58
FilterI(FilterType t)
Base constructor. Initializes the FilterType.
Definition: filter.hpp:89
std::vector< liblas::Classification > class_list_type
Definition: filter.hpp:125
A filter for keeping or rejecting points that fall within a specified bounds.
Definition: filter.hpp:103
ContinuousValueFilter(filter_func f, T value, compare_func c)
Construct the filter with a filter_func, a comparison value, and a compare_func.
Definition: filter.hpp:228
Defines public interface to LAS filter implementation.
Definition: filter.hpp:62
virtual ~FilterI()
Definition: filter.hpp:86
boost::shared_ptr< FilterI > FilterPtr
Definition: filter.hpp:99
A templated class that allows you to create complex filters using functions that are callable from th...
Definition: filter.hpp:197
A filter simple decimation.
Definition: filter.hpp:139
std::vector< uint16_t > return_list_type
Definition: filter.hpp:163
Definition: filter.hpp:178
void SetType(FilterType t)
Sets whether the filter is one that keeps data that matches construction criteria or rejects them...
Definition: filter.hpp:81
FilterType
Determines whether or not the filter keeps or rejects points that meet filtering criteria.
Definition: filter.hpp:68
FilterType GetType() const
Gets the type of filter.
Definition: filter.hpp:84
boost::function< bool(T, T)> compare_func
Definition: filter.hpp:204
ContinuousValueFilter(filter_func f, std::string const &filter_string)
Construct the filter with a filter_func and a simple expression.
Definition: filter.hpp:260
Namespace grouping all elements of libLAS public interface.
Definition: bounds.hpp:60
Point data record composed with X, Y, Z coordinates and attributes.
Definition: point.hpp:68
A filter for keeping or rejecting a list of classification ids.
Definition: filter.hpp:121
boost::function< T(const Point *)> filter_func
Definition: filter.hpp:203
bool filter(const liblas::Point &p)
Function called by liblas::Reader::ReadNextPoint to apply the (list) of filter to the point...
Definition: filter.hpp:312
A filter for color ranges.
Definition: filter.hpp:368
RGB color container.
Definition: color.hpp:55
uint16_t value_type
Definition: color.hpp:59