Commit 00b57586d1ce36af549b639d5298e8d5a514019f

Authored by bklare
1 parent 68a02b5a

Tranform to filter templates based on metadata values.

openbr/plugins/metadata/filtermetadata.cpp 0 → 100644
  1 +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2 + * Copyright 2012 The MITRE Corporation *
  3 + * *
  4 + * Licensed under the Apache License, Version 2.0 (the "License"); *
  5 + * you may not use this file except in compliance with the License. *
  6 + * You may obtain a copy of the License at *
  7 + * *
  8 + * http://www.apache.org/licenses/LICENSE-2.0 *
  9 + * *
  10 + * Unless required by applicable law or agreed to in writing, software *
  11 + * distributed under the License is distributed on an "AS IS" BASIS, *
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
  13 + * See the License for the specific language governing permissions and *
  14 + * limitations under the License. *
  15 + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  16 +
  17 +#include <QRegularExpression>
  18 +
  19 +#include <openbr/plugins/openbr_internal.h>
  20 +
  21 +namespace br
  22 +{
  23 +
  24 +/*!
  25 + * \ingroup transforms
  26 + * \brief Filters templates such that the only remaining template wills have metadata values witihin the
  27 + * the specified ranges.
  28 + * @param[in] key1,key2 The meta-data key(s) to filter on
  29 + * @param[in] value1,value2 The values to compare the values of key1 and key2 entires against
  30 + * @param[in] compareType1 The comparison operation to perform. "le" -> val(key1) <= value1, "lt" -> val(key1) < value1,
  31 + * "ge" -> val(key1) >= value1, "gt" -> val(key1) > value1.
  32 + * \author Brendan Klare \cite bklare
  33 + */
  34 +class FilterMetadataTransform : public UntrainableMetaTransform
  35 +{
  36 + Q_OBJECT
  37 + Q_PROPERTY(QString key1 READ get_key1 WRITE set_key1 RESET reset_key1 STORED false)
  38 + Q_PROPERTY(QString compareType1 READ get_compareType1 WRITE set_compareType1 RESET reset_compareType1 STORED false)
  39 + Q_PROPERTY(float value1 READ get_value1 WRITE set_value1 RESET reset_value1 STORED false)
  40 + BR_PROPERTY(QString, key1, "")
  41 + BR_PROPERTY(QString, compareType1, "")
  42 + BR_PROPERTY(float, value1, 0)
  43 +
  44 + bool getCompare(float val1, float val2, QString compareType) const
  45 + {
  46 + bool pass = false;
  47 + if (compareType == "gt")
  48 + pass = val1 > val2;
  49 + else if (compareType == "ge")
  50 + pass = val1 >= val2;
  51 + else if (compareType == "lt")
  52 + pass = val1 < val2;
  53 + else if (compareType == "le")
  54 + pass = val1 <= val2;
  55 + else
  56 + qDebug() << "Unknown compare type: " << compareType;
  57 + return pass;
  58 + }
  59 +
  60 + void project(const Template &, Template &) const
  61 + {
  62 + qFatal("Filter can't do anything here");
  63 + }
  64 +
  65 + void project(const TemplateList &src, TemplateList &dst) const
  66 + {
  67 + foreach (const Template &srcTemp, src)
  68 + {
  69 + if (!srcTemp.file.contains(key1))
  70 + continue;
  71 +
  72 + if (getCompare(srcTemp.file.get<float>(key1), value1, compareType1))
  73 + dst.append(srcTemp);
  74 + }
  75 + }
  76 +
  77 +};
  78 +
  79 +BR_REGISTER(Transform, FilterMetadataTransform)
  80 +
  81 +} // namespace br
  82 +
  83 +#include "metadata/filtermetadata.moc"
... ...