diff --git a/Code/Common/otbPersistentImageToVectorDataFilter.h b/Code/Common/otbPersistentImageToVectorDataFilter.h new file mode 100644 index 0000000000000000000000000000000000000000..88fb34dc46566e6b66b582900316af66ab234f9f --- /dev/null +++ b/Code/Common/otbPersistentImageToVectorDataFilter.h @@ -0,0 +1,114 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + Some parts of this code are derived from ITK. See ITKCopyright.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 __otbPersistentImageToVectorDataFilter_h +#define __otbPersistentImageToVectorDataFilter_h + +#include "otbPersistentImageFilter.h" +#include "itkExtractImageFilter.h" + +#include "otbConcatenateVectorDataFilter.h" + +namespace otb +{ + +/** \class PersistentImageToVectorDataFilter + * \brief Perform vectorization in a persistent way. + * + * This filter is a generic PersistentImageFilter, which encapsulate any filter + * which produces VectorData from an input Image. + * + * It provides a default implementation which merge + * the VectorData from the different tiles/strips used during streaming into + * a single VectorData, which can be accessed via GetVectorData() + * + * \sa PersistentImageFilter + * + */ +template<class TImage, class TOutputVectorData> +class ITK_EXPORT PersistentImageToVectorDataFilter : + public PersistentImageFilter<TImage,TImage> +{ +public: + /** Standard Self typedef */ + typedef PersistentImageToVectorDataFilter Self; + typedef PersistentImageFilter<TImage,TImage> Superclass; + typedef itk::SmartPointer<Self> Pointer; + typedef itk::SmartPointer<const Self> ConstPointer; + + /** Runtime information support. */ + itkTypeMacro(PersistentImageToVectorDataFilter, PersistentImageFilter); + + typedef TImage InputImageType; + typedef typename InputImageType::Pointer InputImagePointer; + typedef typename InputImageType::RegionType RegionType; + typedef typename InputImageType::SizeType SizeType; + typedef typename InputImageType::IndexType IndexType; + typedef typename InputImageType::PixelType PixelType; + typedef typename InputImageType::InternalPixelType InternalPixelType; + + typedef TOutputVectorData OutputVectorDataType; + typedef typename TOutputVectorData::Pointer OutputVectorDataPointerType; + + typedef itk::ExtractImageFilter<InputImageType, InputImageType> ExtractImageFilterType; + typedef typename ExtractImageFilterType::Pointer ExtractImageFilterPointerType; + + typedef otb::ConcatenateVectorDataFilter<OutputVectorDataType> ConcatenateVectorDataFilterType; + typedef typename ConcatenateVectorDataFilterType::Pointer ConcatenateVectorDataFilterPointerType; + + /** Smart Pointer type to a DataObject. */ + typedef itk::DataObject::Pointer DataObjectPointer; + + OutputVectorDataType* GetOutputVectorData() const; + + /** Make a DataObjectof the correct type to be used as the specified + * output. */ + virtual DataObjectPointer MakeOutput(unsigned int idx); + + void AllocateOutputs(); + + virtual void Reset(void); + + virtual void Synthetize(void); + +protected: + PersistentImageToVectorDataFilter(); + virtual ~PersistentImageToVectorDataFilter() {} + + void PrintSelf(std::ostream& os, itk::Indent indent) const; + + virtual void GenerateData(); + + ExtractImageFilterPointerType m_ExtractFilter; + +private: + PersistentImageToVectorDataFilter(const Self &); //purposely not implemented + void operator =(const Self&); //purposely not implemented + + virtual OutputVectorDataPointerType ProcessTile(const InputImageType* inputImage) = 0; + +}; // end of class +} // end namespace otb + +#ifndef OTB_MANUAL_INSTANTIATION +#include "otbPersistentImageToVectorDataFilter.txx" +#endif + +#endif diff --git a/Code/Common/otbPersistentImageToVectorDataFilter.txx b/Code/Common/otbPersistentImageToVectorDataFilter.txx new file mode 100644 index 0000000000000000000000000000000000000000..785c9ddebcdd98e7f8e7143970455268a1750900 --- /dev/null +++ b/Code/Common/otbPersistentImageToVectorDataFilter.txx @@ -0,0 +1,139 @@ +/*========================================================================= + + Program: ORFEO Toolbox + Language: C++ + Date: $Date$ + Version: $Revision$ + + + Copyright (c) Centre National d'Etudes Spatiales. All rights reserved. + See OTBCopyright.txt for details. + + Some parts of this code are derived from ITK. See ITKCopyright.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 __otbPersistentImageToVectorDataFilter_txx +#define __otbPersistentImageToVectorDataFilter_txx + +#include "otbPersistentImageToVectorDataFilter.h" + +namespace otb +{ + +template<class TImage, class TOutputVectorData> +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::PersistentImageToVectorDataFilter() +{ + this->itk::ProcessObject::SetNumberOfRequiredOutputs(2); + this->itk::ProcessObject::SetNthOutput(1, this->MakeOutput(1).GetPointer()); + + m_ExtractFilter = ExtractImageFilterType::New(); +} + +template<class TImage, class TOutputVectorData> +typename PersistentImageToVectorDataFilter<TImage, TOutputVectorData>::DataObjectPointer +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::MakeOutput(unsigned int i) +{ + if (i == 1) + { + return static_cast<itk::DataObject*>(OutputVectorDataType::New().GetPointer()); + } + else + { + return Superclass::MakeOutput(i); + } +} + +template<class TImage, class TOutputVectorData> +typename PersistentImageToVectorDataFilter<TImage, TOutputVectorData>::OutputVectorDataType * +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::GetOutputVectorData() const +{ + return static_cast<OutputVectorDataType*>(const_cast<itk::DataObject*>(this->itk::ProcessObject::GetOutput(1))); +} + +template<class TImage, class TOutputVectorData> +void +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::AllocateOutputs() +{ + // Nothing that needs to be allocated for the outputs : the output is not meant to be used + //this->GetOutputVectorData()->Clear(); +} + +template<class TImage, class TOutputVectorData> +void +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::Reset() +{ + typedef typename OutputVectorDataType::DataNodeType DataNodeType; + typedef typename DataNodeType::Pointer DataNodePointerType; + + this->GetOutputVectorData()->Clear(); + DataNodePointerType root = DataNodeType::New(); + root->SetNodeId("Root"); + this->GetOutputVectorData()->GetDataTree()->SetRoot(root); + + DataNodePointerType folder = DataNodeType::New(); + folder->SetNodeType(otb::FOLDER); + + DataNodePointerType document = DataNodeType::New() ; + document->SetNodeType(otb::DOCUMENT); + + this->GetOutputVectorData()->GetDataTree()->Add(folder, this->GetOutputVectorData()->GetDataTree()->GetRoot()->Get()); + this->GetOutputVectorData()->GetDataTree()->Add(document , folder); +} + +template<class TImage, class TOutputVectorData> +void +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::Synthetize() +{ + +} + +template<class TImage, class TOutputVectorData> +void +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::GenerateData() +{ + // make an extract to handle filter which request + // the largest possible region of their input + m_ExtractFilter = ExtractImageFilterType::New(); + m_ExtractFilter->SetInput( this->GetInput() ); + m_ExtractFilter->SetExtractionRegion( this->GetInput()->GetBufferedRegion() ); + m_ExtractFilter->Update(); + InputImagePointer image = m_ExtractFilter->GetOutput(); + + // call the processing function for this tile + OutputVectorDataPointerType vd = this->ProcessTile(image); + + // merge the result into the output vector data object + OutputVectorDataPointerType output = GetOutputVectorData(); + + ConcatenateVectorDataFilterPointerType concatenate = ConcatenateVectorDataFilterType::New(); + concatenate->AddInput(vd); + concatenate->AddInput(output); + concatenate->Update(); + + // copy metadata and reference the same data tree + output->Graft( concatenate->GetOutput() ); +} + +template<class TImage, class TOutputVectorData> +void +PersistentImageToVectorDataFilter<TImage, TOutputVectorData> +::PrintSelf(std::ostream& os, itk::Indent indent) const +{ + Superclass::PrintSelf(os, indent); +} + +} // end namespace otb +#endif