diff --git a/Code/BasicFilters/otbUnaryFunctorObjectListBooleanFilter.h b/Code/BasicFilters/otbUnaryFunctorObjectListBooleanFilter.h new file mode 100644 index 0000000000000000000000000000000000000000..d3abb94a9a8a4989e343a5a73152f488603c9693 --- /dev/null +++ b/Code/BasicFilters/otbUnaryFunctorObjectListBooleanFilter.h @@ -0,0 +1,115 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbUnaryFunctorObjectListBooleanFilter_h +#define __otbUnaryFunctorObjectListBooleanFilter_h + +#include "otbObjectListToObjectListFilter.h" + +namespace otb +{ + +/** \class UnaryFunctorObjectListBooleanFilter + * \brief Filter an ObjectList and keep elements according to the functor value + * + * Only elements of the list for which the functor evaluate as true are kept in the + * output list. This is similar to the python filter() function. + * + * + * \ingroup ObjectListFilter + */ +template <class TInputList, class TOutputList, class TFunction > + class ITK_EXPORT UnaryFunctorObjectListBooleanFilter : public otb::ObjectListToObjectListFilter<TInputList,TOutputList> +{ +public: + /** Standard class typedefs. */ + typedef UnaryFunctorObjectListBooleanFilter Self; + typedef otb::ObjectListToObjectListFilter<TInputList,TOutputList> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(UnaryFunctorObjectListBooleanFilter, InPlaceImageFilter); + + /** Some typedefs. */ + typedef TFunction FunctorType; + typedef TInputList InputListType; + typedef TOutputList OutputListType; + typedef typename TInputList::ConstPointer InputListPointer; + typedef typename TOutputList::Pointer OutputListPointer; + typedef typename TInputList::ConstIterator InputListIterator; + + typedef itk::DataObject::Pointer DataObjectPointer; + + + /** Get the functor object. The functor is returned by reference. + * (Functors do not have to derive from itk::LightObject, so they do + * not necessarily have a reference count. So we cannot return a + * SmartPointer.) */ + FunctorType& GetFunctor() { return m_Functor; }; + const FunctorType& GetFunctor() const { return m_Functor; }; + + /** Set the functor object. This replaces the current Functor with a + * copy of the specified Functor. This allows the user to specify a + * functor that has ivars set differently than the default functor. + * This method requires an operator!=() be defined on the functor + * (or the compiler's default implementation of operator!=() being + * appropriate). */ + void SetFunctor(const FunctorType& functor) + { + if (m_Functor != functor) + { + m_Functor = functor; + this->Modified(); + } + } + + +protected: + UnaryFunctorObjectListBooleanFilter(); + virtual ~UnaryFunctorObjectListBooleanFilter() {}; + + + /** UnaryFunctorObjectListBooleanFilter can be implemented as a multithreaded filter. + * Therefore, this implementation provides a ThreadedGenerateData() routine + * which is called for each processing thread. The output image data is + * allocated automatically by the superclass prior to calling + * ThreadedGenerateData(). ThreadedGenerateData can only write to the + * portion of the output image specified by the parameter + * "outputRegionForThread" + * + * \sa ImageToImageFilter::ThreadedGenerateData(), + * ImageToImageFilter::GenerateData() */ + void GenerateData(void); + +private: + UnaryFunctorObjectListBooleanFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + FunctorType m_Functor; +}; + +} // end namespace otb + +#ifndef ITK_MANUAL_INSTANTIATION +#include "otbUnaryFunctorObjectListBooleanFilter.txx" +#endif + +#endif diff --git a/Code/BasicFilters/otbUnaryFunctorObjectListBooleanFilter.txx b/Code/BasicFilters/otbUnaryFunctorObjectListBooleanFilter.txx new file mode 100644 index 0000000000000000000000000000000000000000..3f71cd877e6c7bcf4241bd63e32d8e1b579c9a3e --- /dev/null +++ b/Code/BasicFilters/otbUnaryFunctorObjectListBooleanFilter.txx @@ -0,0 +1,68 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbUnaryFunctorObjectListBooleanFilter_txx +#define __otbUnaryFunctorObjectListBooleanFilter_txx + +#include "otbUnaryFunctorObjectListBooleanFilter.h" +#include "itkProgressReporter.h" + +namespace otb +{ + +/** + * Constructor + */ +template <class TInputList, class TOutputList, class TFunction > +UnaryFunctorObjectListBooleanFilter<TInputList,TOutputList,TFunction> +::UnaryFunctorObjectListBooleanFilter() +{ +} + + + +/** + * GenerateData Performs the pixel-wise addition + */ +template <class TInputList, class TOutputList, class TFunction > +void +UnaryFunctorObjectListBooleanFilter<TInputList,TOutputList,TFunction> +::GenerateData(void) +{ + InputListPointer inputPtr = this->GetInput(); + OutputListPointer outputPtr = this->GetOutput(); + + + itk::ProgressReporter progress(this, 0, inputPtr->Size()); + + // Define the iterators + for(InputListIterator it = inputPtr->Begin(); it != inputPtr->End(); ++it) + { + if(m_Functor(it.Get())) + { + outputPtr->PushBack(it.Get()); + } + + progress.CompletedPixel(); + + } + +} + +} // end namespace otb + +#endif diff --git a/Code/BasicFilters/otbUnaryFunctorObjectListFilter.h b/Code/BasicFilters/otbUnaryFunctorObjectListFilter.h new file mode 100644 index 0000000000000000000000000000000000000000..36a3db786e09c0e56d0e17ddf199b401cd17fcd0 --- /dev/null +++ b/Code/BasicFilters/otbUnaryFunctorObjectListFilter.h @@ -0,0 +1,115 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbUnaryFunctorObjectListFilter_h +#define __otbUnaryFunctorObjectListFilter_h + +#include "otbObjectListToObjectListFilter.h" + +namespace otb +{ + +/** \class UnaryFunctorObjectListFilter + * \brief Process each element in the ObjectList with the functor + * + * Each element of the ObjectList is processed by the functor. + * This is similar to the python map() function. + * + * + * \ingroup ObjectListFilter + */ +template <class TInputList, class TOutputList, class TFunction > + class ITK_EXPORT UnaryFunctorObjectListFilter : public otb::ObjectListToObjectListFilter<TInputList,TOutputList> +{ +public: + /** Standard class typedefs. */ + typedef UnaryFunctorObjectListFilter Self; + typedef otb::ObjectListToObjectListFilter<TInputList,TOutputList> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + + /** Method for creation through the object factory. */ + itkNewMacro(Self); + + /** Run-time type information (and related methods). */ + itkTypeMacro(UnaryFunctorObjectListFilter, InPlaceImageFilter); + + /** Some typedefs. */ + typedef TFunction FunctorType; + typedef TInputList InputListType; + typedef TOutputList OutputListType; + typedef typename TInputList::ConstPointer InputListPointer; + typedef typename TOutputList::Pointer OutputListPointer; + typedef typename TInputList::ConstIterator InputListIterator; + + typedef itk::DataObject::Pointer DataObjectPointer; + + + /** Get the functor object. The functor is returned by reference. + * (Functors do not have to derive from itk::LightObject, so they do + * not necessarily have a reference count. So we cannot return a + * SmartPointer.) */ + FunctorType& GetFunctor() { return m_Functor; }; + const FunctorType& GetFunctor() const { return m_Functor; }; + + /** Set the functor object. This replaces the current Functor with a + * copy of the specified Functor. This allows the user to specify a + * functor that has ivars set differently than the default functor. + * This method requires an operator!=() be defined on the functor + * (or the compiler's default implementation of operator!=() being + * appropriate). */ + void SetFunctor(const FunctorType& functor) + { + if (m_Functor != functor) + { + m_Functor = functor; + this->Modified(); + } + } + + +protected: + UnaryFunctorObjectListFilter(); + virtual ~UnaryFunctorObjectListFilter() {}; + + + /** UnaryFunctorObjectListFilter can be implemented as a multithreaded filter. + * Therefore, this implementation provides a ThreadedGenerateData() routine + * which is called for each processing thread. The output image data is + * allocated automatically by the superclass prior to calling + * ThreadedGenerateData(). ThreadedGenerateData can only write to the + * portion of the output image specified by the parameter + * "outputRegionForThread" + * + * \sa ImageToImageFilter::ThreadedGenerateData(), + * ImageToImageFilter::GenerateData() */ + void GenerateData(void); + +private: + UnaryFunctorObjectListFilter(const Self&); //purposely not implemented + void operator=(const Self&); //purposely not implemented + + FunctorType m_Functor; +}; + +} // end namespace otb + +#ifndef ITK_MANUAL_INSTANTIATION +#include "otbUnaryFunctorObjectListFilter.txx" +#endif + +#endif diff --git a/Code/BasicFilters/otbUnaryFunctorObjectListFilter.txx b/Code/BasicFilters/otbUnaryFunctorObjectListFilter.txx new file mode 100644 index 0000000000000000000000000000000000000000..bf170b54e2e2dbf3af994e72299b2817e91a0e6f --- /dev/null +++ b/Code/BasicFilters/otbUnaryFunctorObjectListFilter.txx @@ -0,0 +1,65 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + + This software is distributed WITHOUT ANY WARRANTY; without even + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the above copyright notices for more information. + +=========================================================================*/ +#ifndef __otbUnaryFunctorObjectListFilter_txx +#define __otbUnaryFunctorObjectListFilter_txx + +#include "otbUnaryFunctorObjectListFilter.h" +#include "itkProgressReporter.h" + +namespace otb +{ + +/** + * Constructor + */ +template <class TInputList, class TOutputList, class TFunction > +UnaryFunctorObjectListFilter<TInputList,TOutputList,TFunction> +::UnaryFunctorObjectListFilter() +{ +} + + + +/** + * GenerateData Performs the pixel-wise addition + */ +template <class TInputList, class TOutputList, class TFunction > +void +UnaryFunctorObjectListFilter<TInputList,TOutputList,TFunction> +::GenerateData(void) +{ + InputListPointer inputPtr = this->GetInput(); + OutputListPointer outputPtr = this->GetOutput(); + + + itk::ProgressReporter progress(this, 0, inputPtr->Size()); + + // Define the iterators + for(InputListIterator it = inputPtr->Begin(); it != inputPtr->End(); ++it) + { + outputPtr->PushBack(m_Functor(it.Get())); + + progress.CompletedPixel(); + + } + +} + +} // end namespace otb + +#endif