Skip to content
Snippets Groups Projects
Commit 3ff58cee authored by Emmanuel Christophe's avatar Emmanuel Christophe
Browse files

ENH: add multithread capability to UnaryFunctorObjectListBooleanFilter

parent fb456685
No related branches found
No related tags found
No related merge requests found
......@@ -33,7 +33,8 @@ namespace otb
* \ingroup ObjectListFilter
*/
template <class TInputList, class TOutputList, class TFunction >
class ITK_EXPORT UnaryFunctorObjectListBooleanFilter : public otb::ObjectListToObjectListFilter<TInputList,TOutputList>
class ITK_EXPORT UnaryFunctorObjectListBooleanFilter :
public otb::ObjectListToObjectListFilter<TInputList,TOutputList>
{
public:
/** Standard class typedefs. */
......@@ -55,8 +56,8 @@ public:
typedef typename TInputList::ConstPointer InputListPointer;
typedef typename TOutputList::Pointer OutputListPointer;
typedef typename TInputList::ConstIterator InputListIterator;
typedef typename TOutputList::ConstIterator OutputListIterator;
// typedef itk::DataObject::Pointer DataObjectPointer;
/** Get the functor object. The functor is returned by reference.
......@@ -93,17 +94,24 @@ protected:
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);
/** Multi-threading implementation */
virtual void AfterThreadedGenerateData();
/** startIndex and stopIndex represent the indices of the Objects to
examine in thread threadId */
virtual void ThreadedGenerateData(unsigned int startIndex, unsigned int stopIndex, int threadId);
/** Internal structure used for passing image data into the threading library */
struct ThreadStruct
{
Pointer Filter;
};
/** End Multi-threading implementation */
private:
UnaryFunctorObjectListBooleanFilter(const Self&); //purposely not implemented
......
......@@ -34,36 +34,64 @@ UnaryFunctorObjectListBooleanFilter<TInputList,TOutputList,TFunction>
}
/**
* GenerateData Performs the pixel-wise addition
*/
template <class TInputList, class TOutputList, class TFunction >
void
UnaryFunctorObjectListBooleanFilter<TInputList,TOutputList,TFunction>
::GenerateData(void)
void
UnaryFunctorObjectListBooleanFilter<TInputList,TOutputList,TFunction>
::ThreadedGenerateData(unsigned int startIndex, unsigned int stopIndex,int threadId)
{
this->AllocateOutputs();
InputListPointer inputPtr = this->GetInput();
OutputListPointer outputPtr = this->GetOutput();
this->m_ObjectListPerThread[threadId] = OutputListType::New();
itk::ProgressReporter progress(this, 0, inputPtr->Size());
itk::ProgressReporter progress(this, threadId, stopIndex-startIndex);
// Define the iterators
for (InputListIterator it = inputPtr->Begin(); it != inputPtr->End(); ++it)
InputListIterator it = inputPtr->Begin();
unsigned int count = 0;
while ((count < startIndex) && (it != inputPtr->End()))
{
++it;
++count;
}
while ((count < stopIndex) && (it != inputPtr->End()))
{
if (m_Functor(it.Get()))
{
outputPtr->PushBack(it.Get());
this->m_ObjectListPerThread[threadId]->PushBack(it.Get());
}
progress.CompletedPixel();
++it;
++count;
}
}
template <class TInputList, class TOutputList, class TFunction >
void
UnaryFunctorObjectListBooleanFilter<TInputList,TOutputList,TFunction>
::AfterThreadedGenerateData()
{
// copy the lists to the output
OutputListPointer outputPtr = this->GetOutput();
for (unsigned int i=0; i< this->m_ObjectListPerThread.size(); ++i)
{
if (this->m_ObjectListPerThread[i].IsNotNull())
{
for (OutputListIterator it = this->m_ObjectListPerThread[i]->Begin();
it != this->m_ObjectListPerThread[i]->End();
++it)
{
outputPtr->PushBack(it.Get());
}
}
}
}
} // end namespace otb
#endif
......@@ -33,7 +33,8 @@ namespace otb
* \ingroup ObjectListFilter
*/
template <class TInputList, class TOutputList, class TFunction >
class ITK_EXPORT UnaryFunctorObjectListFilter : public otb::ObjectListToObjectListFilter<TInputList,TOutputList>
class ITK_EXPORT UnaryFunctorObjectListFilter :
public otb::ObjectListToObjectListFilter<TInputList,TOutputList>
{
public:
/** Standard class typedefs. */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment