From d8d3ef29ab1a1f4f268d28b1040d300b0965abf7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leonardo=20Fl=C3=B3rez-Valencia?= Date: Wed, 7 Dec 2016 12:10:14 -0500 Subject: [PATCH] ... --- lib/cpExtensions/Algorithms/SkeletonReader.h | 85 +++++++++ .../Algorithms/SkeletonReader.hxx | 176 ++++++++++++++++++ .../Algorithms/SkeletonWriter.hxx | 6 +- lib/cpInstances/Skeleton.h | 2 - .../RegionOfInterestImageFilter.cxx | 9 + plugins/cpExtensions/SkeletonReader.cxx | 93 +++++++++ plugins/cpExtensions/SkeletonReader.h | 33 ++++ plugins/cpExtensions/SkeletonWriter.cxx | 2 +- plugins/cpExtensions/cpExtensions.i | 2 + 9 files changed, 402 insertions(+), 6 deletions(-) create mode 100644 lib/cpExtensions/Algorithms/SkeletonReader.h create mode 100644 lib/cpExtensions/Algorithms/SkeletonReader.hxx create mode 100644 plugins/cpExtensions/SkeletonReader.cxx create mode 100644 plugins/cpExtensions/SkeletonReader.h diff --git a/lib/cpExtensions/Algorithms/SkeletonReader.h b/lib/cpExtensions/Algorithms/SkeletonReader.h new file mode 100644 index 0000000..e1a3064 --- /dev/null +++ b/lib/cpExtensions/Algorithms/SkeletonReader.h @@ -0,0 +1,85 @@ +// ------------------------------------------------------------------------- +// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co) +// ------------------------------------------------------------------------- + +#ifndef __cpExtensions__Algorithms__SkeletonReader__h__ +#define __cpExtensions__Algorithms__SkeletonReader__h__ + +#include +#include + +// ------------------------------------------------------------------------- +namespace cpExtensions +{ + namespace Algorithms + { + /** + */ + template< class _TSkeleton > + class SkeletonReader + : public itk::ProcessObject + { + public: + // Basic types + typedef SkeletonReader Self; + typedef itk::ProcessObject Superclass; + typedef itk::SmartPointer< Self > Pointer; + typedef itk::SmartPointer< const Self > ConstPointer; + + typedef _TSkeleton TSkeleton; + + public: + itkNewMacro( Self ); + itkTypeMacro( SkeletonReader, itk::ImageSource ); + + itkGetConstMacro( FileName, std::string ); + itkSetMacro( FileName, std::string ); + + public: + TSkeleton* GetOutput( ); + TSkeleton* GetOutput( unsigned int i ); + + virtual void GraftOutput( itk::DataObject* out ); + virtual void GraftOutput( + const typename Superclass::DataObjectIdentifierType& key, + itk::DataObject* out + ); + virtual void GraftNthOutput( unsigned int i, itk::DataObject* out ); + virtual itk::DataObject::Pointer MakeOutput( + itk::ProcessObject::DataObjectPointerArraySizeType i + ) cpExtensions_OVERRIDE; + + virtual void Update( ) cpExtensions_OVERRIDE + { this->GenerateData( ); } + + protected: + SkeletonReader( ); + virtual ~SkeletonReader( ); + + virtual void GenerateData( ) cpExtensions_OVERRIDE; + + // Do nothing + virtual void GenerateOutputInformation( ) cpExtensions_OVERRIDE + { } + + private: + // Purposely not implemented + SkeletonReader( const Self& ); + void operator=( const Self& ); + + protected: + std::string m_FileName; + }; + + } // ecapseman + +} // ecapseman + +// ------------------------------------------------------------------------- +#ifndef ITK_MANUAL_INSTANTIATION +# include +#endif // ITK_MANUAL_INSTANTIATION + +#endif // __cpExtensions__Algorithms__SkeletonReader__h__ + +// eof - $RCSfile$ diff --git a/lib/cpExtensions/Algorithms/SkeletonReader.hxx b/lib/cpExtensions/Algorithms/SkeletonReader.hxx new file mode 100644 index 0000000..744bd04 --- /dev/null +++ b/lib/cpExtensions/Algorithms/SkeletonReader.hxx @@ -0,0 +1,176 @@ +// ------------------------------------------------------------------------- +// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co) +// ------------------------------------------------------------------------- + +#ifndef __cpExtensions__Algorithms__SkeletonReader__hxx__ +#define __cpExtensions__Algorithms__SkeletonReader__hxx__ + +#include + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +_TSkeleton* cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +GetOutput( ) +{ + return( + itkDynamicCastInDebugMode< TSkeleton* >( this->GetPrimaryOutput( ) ) + ); +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +_TSkeleton* cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +GetOutput( unsigned int i ) +{ + return( + itkDynamicCastInDebugMode< TSkeleton* >( + this->itk::ProcessObject::GetOutput( i ) + ) + ); +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +GraftOutput( itk::DataObject* out ) +{ + this->GraftNthOutput( 0, out ); +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +GraftOutput( + const typename Superclass::DataObjectIdentifierType& key, + itk::DataObject* out + ) +{ + if( out == NULL ) + { + itkExceptionMacro( + << "Requested to graft output that is a NULL pointer" + ); + + } // fi + itk::DataObject* output = this->itk::ProcessObject::GetOutput( key ); + output->Graft( out ); +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +GraftNthOutput( unsigned int i, itk::DataObject* out ) +{ + if( i >= this->GetNumberOfIndexedOutputs( ) ) + { + itkExceptionMacro( + << "Requested to graft output " << i + << " but this filter only has " + << this->GetNumberOfIndexedOutputs( ) + << " indexed Outputs." + ); + + } // fi + this->GraftOutput( this->MakeNameFromOutputIndex( i ), out ); +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +itk::DataObject::Pointer +cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +MakeOutput( itk::ProcessObject::DataObjectPointerArraySizeType i ) +{ + return( TSkeleton::New( ).GetPointer( ) ); +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +SkeletonReader( ) + : Superclass( ) +{ + typename TSkeleton::Pointer out = + static_cast< TSkeleton* >( this->MakeOutput( 0 ).GetPointer( ) ); + this->itk::ProcessObject::SetNumberOfRequiredInputs( 0 ); + this->itk::ProcessObject::SetNumberOfRequiredOutputs( 1 ); + this->itk::ProcessObject::SetNthOutput( 0, out.GetPointer( ) ); +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +~SkeletonReader( ) +{ +} + +// ------------------------------------------------------------------------- +template< class _TSkeleton > +void cpExtensions::Algorithms::SkeletonReader< _TSkeleton >:: +GenerateData( ) +{ + typedef typename TSkeleton::TPath _TPath; + typedef typename _TPath::TSpacing _TSpacing; + typedef typename _TPath::TPoint _TPoint; + typedef typename _TPath::TDirection _TDirection; + typedef typename _TPath::TContinuousIndex _TContinuousIndex; + + std::string buffer; + if( !( cpExtensions::Read( buffer, this->m_FileName ) ) ) + { + itkExceptionMacro( + << "Error reading skeleton from \"" << this->m_FileName << "\"" + ); + return; + + } // fi + + std::istringstream in( buffer ); + unsigned int dim; + in >> dim; + if( dim != TSkeleton::Dimension ) + { + itkExceptionMacro( + << "Mismatched skeletons dimension: " << dim + << " != " << TSkeleton::Dimension + ); + return; + + } // fi + + TSkeleton* out = this->GetOutput( ); + unsigned long size; + in >> size; + while( size > 0 ) + { + _TSpacing spa; + _TPoint ori; + _TDirection dir; + for( unsigned int d = 0; d < dim; ++d ) + in >> spa[ d ]; + for( unsigned int d = 0; d < dim; ++d ) + in >> ori[ d ]; + for( unsigned int d = 0; d < dim; ++d ) + for( unsigned int e = 0; e < dim; ++e ) + in >> dir[ d ][ e ]; + + typename _TPath::Pointer path = _TPath::New( ); + path->SetSpacing( spa ); + path->SetOrigin( ori ); + path->SetDirection( dir ); + for( unsigned long s = 0; s < size; ++s ) + { + _TContinuousIndex idx; + for( unsigned int d = 0; d < dim; ++d ) + in >> idx[ d ]; + path->AddVertex( idx ); + + } // rof + out->AddBranch( path ); + in >> size; + + } // elihw +} + +#endif // __cpExtensions__Algorithms__SkeletonReader__hxx__ + +// eof - $RCSfile$ diff --git a/lib/cpExtensions/Algorithms/SkeletonWriter.hxx b/lib/cpExtensions/Algorithms/SkeletonWriter.hxx index be39b90..3be1d79 100644 --- a/lib/cpExtensions/Algorithms/SkeletonWriter.hxx +++ b/lib/cpExtensions/Algorithms/SkeletonWriter.hxx @@ -5,7 +5,7 @@ #ifndef __cpExtensions__Algorithms__SkeletonWriter__hxx__ #define __cpExtensions__Algorithms__SkeletonWriter__hxx__ -#include +#include // ------------------------------------------------------------------------- template< class _TSkeleton > @@ -105,7 +105,7 @@ GenerateData( ) out << v[ d ] << " "; } // rof - std::cout << std::endl; + out << std::endl; } // rof @@ -114,7 +114,7 @@ GenerateData( ) } // rof out << "0" << std::endl; - if( !( cpPlugins::Write( out.str( ), this->m_FileName ) ) ) + if( !( cpExtensions::Write( out.str( ), this->m_FileName ) ) ) itkExceptionMacro( << "Unable to write skeleton to \"" << this->m_FileName diff --git a/lib/cpInstances/Skeleton.h b/lib/cpInstances/Skeleton.h index b86aed0..7a1bba2 100644 --- a/lib/cpInstances/Skeleton.h +++ b/lib/cpInstances/Skeleton.h @@ -6,8 +6,6 @@ #include #include -class vtkSkeletonData; - namespace cpInstances { /** diff --git a/plugins/ITKImageGenericFilters_1/RegionOfInterestImageFilter.cxx b/plugins/ITKImageGenericFilters_1/RegionOfInterestImageFilter.cxx index 9254bf4..61f2f58 100644 --- a/plugins/ITKImageGenericFilters_1/RegionOfInterestImageFilter.cxx +++ b/plugins/ITKImageGenericFilters_1/RegionOfInterestImageFilter.cxx @@ -48,9 +48,18 @@ _GD0( _TImage* input ) auto bb = this->GetInput< _TBBox >( "BoundingBox" ); + _TRegion in_region = input->GetRequestedRegion( ); + _TIndex in_i0 = in_region.GetIndex( ); + _TIndex in_i1 = in_i0 + in_region.GetSize( ); _TIndex i0, i1; input->TransformPhysicalPointToIndex( bb->GetMinimum< _TPoint >( ), i0 ); input->TransformPhysicalPointToIndex( bb->GetMaximum< _TPoint >( ), i1 ); + for( unsigned int d = 0; d < _TImage::ImageDimension; ++d ) + { + if( i0[ d ] < in_i0[ d ] ) i0[ d ] = in_i0[ d ]; + if( i1[ d ] > in_i1[ d ] ) i1[ d ] = in_i1[ d ]; + + } // rof _TSize size; for( unsigned int d = 0; d < _TImage::ImageDimension; ++d ) diff --git a/plugins/cpExtensions/SkeletonReader.cxx b/plugins/cpExtensions/SkeletonReader.cxx new file mode 100644 index 0000000..7792a32 --- /dev/null +++ b/plugins/cpExtensions/SkeletonReader.cxx @@ -0,0 +1,93 @@ +#include +#include + +#include +#include +#include + +#ifdef cpPlugins_QT4 +# include +#endif // cpPlugins_QT4 + +// ------------------------------------------------------------------------- +QDialog* cpPluginscpExtensions::SkeletonReader:: +CreateQDialog( ) +{ +#ifdef cpPlugins_QT4 + cpPlugins::QT::OpenFileDialog* dlg = NULL; + if( QApplication::instance( ) != NULL ) + { + dlg = new cpPlugins::QT::OpenFileDialog( ); + dlg->SetParameters( &( this->m_Parameters ), "FileName" ); + + } // fi + return( dlg ); +#else // cpPlugins_QT4 + return( NULL ); +#endif // cpPlugins_QT4 +} + +// ------------------------------------------------------------------------- +cpPluginscpExtensions::SkeletonReader:: +SkeletonReader( ) + : Superclass( ) +{ + this->_ConfigureOutput< cpInstances::Skeleton >( "Output" ); + this->m_Parameters.ConfigureAsOpenFileName( "FileName", "" ); + this->m_Parameters.SetAcceptedFileExtensions( + "FileName", + "Skeleton files (*.txt)" + ); +} + +// ------------------------------------------------------------------------- +cpPluginscpExtensions::SkeletonReader:: +~SkeletonReader( ) +{ +} + +// ------------------------------------------------------------------------- +void cpPluginscpExtensions::SkeletonReader:: +_GenerateData( ) +{ + std::string success = ""; +#ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_1 + success = this->_GD0< 1 >( ); +#endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_1 +#ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_2 + if( success != "" ) success = this->_GD0< 2 >( ); +#endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_2 +#ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_3 + if( success != "" ) success = this->_GD0< 3 >( ); +#endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_3 +#ifdef cpPlugins_CONFIG_PROCESS_DIMENSIONS_4 + if( success != "" ) success = this->_GD0< 4 >( ); +#endif // cpPlugins_CONFIG_PROCESS_DIMENSIONS_4 + if( success != "" ) + this->_Error( success ); +} + +// ------------------------------------------------------------------------- +template< unsigned int _VDim > +std::string cpPluginscpExtensions::SkeletonReader:: +_GD0( ) +{ + typedef cpExtensions::DataStructures::Skeleton< _VDim > _TSkeleton; + typedef cpExtensions::Algorithms::SkeletonReader< _TSkeleton > _TReader; + + auto filter = this->_CreateITK< _TReader >( ); + filter->SetFileName( this->m_Parameters.GetOpenFileName( "FileName" ) ); + try + { + filter->Update( ); + } + catch( std::exception& err ) + { + return( err.what( ) ); + + } // yrt + this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) ); + return( "" ); +} + +// eof - $RCSfile$ diff --git a/plugins/cpExtensions/SkeletonReader.h b/plugins/cpExtensions/SkeletonReader.h new file mode 100644 index 0000000..0d2903e --- /dev/null +++ b/plugins/cpExtensions/SkeletonReader.h @@ -0,0 +1,33 @@ +#ifndef __cpPluginscpExtensions__SkeletonReader__h__ +#define __cpPluginscpExtensions__SkeletonReader__h__ + +#include +#include + +namespace cpPluginscpExtensions +{ + /** + */ + class cpPluginscpExtensions_EXPORT SkeletonReader + : public cpPlugins::BaseObjects::ProcessObject + { + cpPluginsObject( + SkeletonReader, + cpPlugins::BaseObjects::ProcessObject, + IO + ); + + public: + virtual QDialog* CreateQDialog( ) cpPlugins_OVERRIDE; + + protected: + template< unsigned int _VDim > + inline std::string _GD0( ); + }; + +} // ecapseman + +#endif // __cpPluginscpExtensions__SkeletonReader__h__ + + +// eof - $RCSfile$ diff --git a/plugins/cpExtensions/SkeletonWriter.cxx b/plugins/cpExtensions/SkeletonWriter.cxx index a192cfa..4770b8d 100644 --- a/plugins/cpExtensions/SkeletonWriter.cxx +++ b/plugins/cpExtensions/SkeletonWriter.cxx @@ -50,7 +50,7 @@ cpPluginscpExtensions::SkeletonWriter:: void cpPluginscpExtensions::SkeletonWriter:: _GenerateData( ) { - auto o = this->GetInputData( "Skeleton" ); + auto o = this->GetInputData( "Input" ); cpPlugins_Demangle_Skeleton_All_1( o, _GD0 ) this->_Error( "Invalid input skeleton." ); } diff --git a/plugins/cpExtensions/cpExtensions.i b/plugins/cpExtensions/cpExtensions.i index 7a182f8..4d3e1ea 100644 --- a/plugins/cpExtensions/cpExtensions.i +++ b/plugins/cpExtensions/cpExtensions.i @@ -1,11 +1,13 @@ header #define ITK_MANUAL_INSTANTIATION tinclude cpExtensions/Algorithms/SkeletonToImageFilter:h|hxx +tinclude cpExtensions/Algorithms/SkeletonReader:h|hxx tinclude cpExtensions/Algorithms/SkeletonWriter:h|hxx cinclude cpExtensions/DataStructures/Skeleton.h instances cpExtensions::Algorithms::SkeletonToImageFilter< cpExtensions::DataStructures::Skeleton< #pdims# >, itk::Image< unsigned char, #pdims# > > +instances cpExtensions::Algorithms::SkeletonReader< cpExtensions::DataStructures::Skeleton< #pdims# > > instances cpExtensions::Algorithms::SkeletonWriter< cpExtensions::DataStructures::Skeleton< #pdims# > > ** eof - $RCSfile$ -- 2.45.0