1 /******************************************************************************
2 * Copyright (c) 2020, Hobu Inc.
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #include "Filter.hpp"
36 #include "../filters/private/expr/ConditionalExpression.hpp"
37 
38 namespace pdal
39 {
40 
41 struct Filter::Args
42 {
43     expr::ConditionalExpression m_where;
44     Arg *m_whereArg;
45     Filter::WhereMergeMode m_whereMerge;
46     Arg *m_whereMergeArg;
47 };
48 
Filter()49 Filter::Filter() : m_args(new Args)
50 {}
51 
~Filter()52 Filter::~Filter()
53 {}
54 
run(PointViewPtr view)55 PointViewSet Filter::run(PointViewPtr view)
56 {
57     PointViewSet viewSet;
58     filter(*view);
59     viewSet.insert(view);
60     return viewSet;
61 }
62 
l_initialize(PointTableRef table)63 void Filter::l_initialize(PointTableRef table)
64 {
65     Stage::l_initialize(table);
66 }
67 
l_addArgs(ProgramArgs & args)68 void Filter::l_addArgs(ProgramArgs& args)
69 {
70     Stage::l_addArgs(args);
71     m_args->m_whereArg = &args.add("where",
72         "Expression describing points to be passed to this "
73         "filter", m_args->m_where);
74     m_args->m_whereMergeArg = &args.add("where_merge", "If 'where' option is set, describes "
75         "how skipped points should be merged with kept points in standard mode.",
76         m_args->m_whereMerge, WhereMergeMode::Auto);
77 }
78 
l_prepared(PointTableRef table)79 void Filter::l_prepared(PointTableRef table)
80 {
81     Stage::l_prepared(table);
82     auto status = m_args->m_where.prepare(table.layout());
83     if (!status)
84         throwError("Invalid 'where': " + status.what());
85     if (m_args->m_whereMergeArg->set() && !m_args->m_whereArg->set())
86         throwError("Can't set 'where_merge' options without also setting 'where' option.");
87 }
88 
89 
whereExpr() const90 const expr::ConditionalExpression *Filter::whereExpr() const
91 {
92     if (!m_args->m_where.valid())
93         return nullptr;
94     return &(m_args->m_where);
95 }
96 
97 
mergeMode() const98 Stage::WhereMergeMode Filter::mergeMode() const
99 {
100     return m_args->m_whereMerge;
101 }
102 
103 
104 } // namespace pdal
105 
106