From: Leonardo Florez-Valencia Date: Tue, 27 Jan 2015 18:43:44 +0000 (-0500) Subject: Parameters finished X-Git-Tag: v0.1~419 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=1c9f0e4788ee1b1f924f369ddadb7be7c8921f8c;p=cpPlugins.git Parameters finished --- diff --git a/appli/examples/example_ReadImageSeriesWriteImage.cxx b/appli/examples/example_ReadImageSeriesWriteImage.cxx index c0bc7ad..fdd4341 100644 --- a/appli/examples/example_ReadImageSeriesWriteImage.cxx +++ b/appli/examples/example_ReadImageSeriesWriteImage.cxx @@ -1,7 +1,6 @@ #include #include #include -#include #include #include @@ -24,10 +23,6 @@ int main( int argc, char* argv[] ) std::string pixel_type = argv[ 4 ]; bool is_color = ( std::atoi( argv[ 5 ] ) == 1 ); - std::vector< std::string > input_image_files; - for( int i = 6; i < argc; ++i ) - input_image_files.push_back( argv[ i ] ); - // Create interface typedef cpPlugins::Interface::Interface TInterface; typedef TInterface::TClasses TClasses; @@ -59,11 +54,8 @@ int main( int argc, char* argv[] ) // Configure reader TParameters reader_params = reader->GetDefaultParameters( ); - reader_params.SetValueAsStringList( - "FileNames", - input_image_files.begin( ), - input_image_files.end( ) - ); + for( int i = 6; i < argc; ++i ) + reader_params.AddValueToStringList( "FileNames", argv[ 1 ] ); reader_params.SetValueAsString( "PixelType", pixel_type ); reader_params.SetValueAsUint( "ImageDimension", dimensions ); reader_params.SetValueAsUint( "IsColorImage", ( is_color? 1: 0 ) ); diff --git a/appli/examples/example_TestParameters.cxx b/appli/examples/example_TestParameters.cxx index 58711af..0c865ea 100644 --- a/appli/examples/example_TestParameters.cxx +++ b/appli/examples/example_TestParameters.cxx @@ -1,3 +1,171 @@ -#error ACA VOY +#include +#include + +#include +#include + +#include + +// ------------------------------------------------------------------------- +template< class L > +void PrintList( const L& lst, const std::string& desc ) +{ + std::cout << "\t " << desc << ": "; + typename L::const_iterator lIt = lst.begin( ); + for( ; lIt != lst.end( ); ++lIt ) + std::cout << *lIt << " ## "; + std::cout << std::endl; +} + +// ------------------------------------------------------------------------- +int main( int argc, char* argv[] ) +{ + typedef cpPlugins::Interface::Parameters TParameters; + typedef itk::Index< 3 > TIndex; + typedef itk::Point< float, 3 > TPoint; + + TParameters parameters; + + parameters.Configure( TParameters::String, "StringParam" ); + parameters.Configure( TParameters::Bool, "BoolParam" ); + parameters.Configure( TParameters::Int, "IntParam" ); + parameters.Configure( TParameters::Uint, "UintParam" ); + parameters.Configure( TParameters::Real, "RealParam" ); + parameters.Configure( TParameters::Index, "IndexParam" ); + parameters.Configure( TParameters::Point, "PointParam" ); + parameters.Configure( TParameters::StringList, "StringListParam" ); + parameters.Configure( TParameters::BoolList, "BoolListParam" ); + parameters.Configure( TParameters::IntList, "IntListParam" ); + parameters.Configure( TParameters::UintList, "UintListParam" ); + parameters.Configure( TParameters::RealList, "RealListParam" ); + parameters.Configure( TParameters::IndexList, "IndexListParam" ); + parameters.Configure( TParameters::PointList, "PointListParam" ); + + parameters.SetValueAsString( "StringParam", "test_value" ); + parameters.SetValueAsBool( "BoolParam", true ); + parameters.SetValueAsInt( "IntParam", -1234567 ); + parameters.SetValueAsUint( "UintParam", 1234567 ); + parameters.SetValueAsReal( "RealParam", 3.14159244444 ); + parameters.SetValueAsIndex( "IndexParam", 3, 4, -3, 2 ); + parameters.SetValueAsPoint( "PointParam", 3, 9.4, 8.3, 7.2 ); + + parameters.ClearStringList( "StringListParam" ); + parameters.ClearBoolList( "BoolListParam" ); + parameters.ClearIntList( "IntListParam" ); + parameters.ClearUintList( "UintListParam" ); + parameters.ClearRealList( "RealListParam" ); + parameters.ClearIndexList( "IndexListParam" ); + parameters.ClearPointList( "PointListParam" ); + + parameters.AddValueToStringList( "StringListParam", "one" ); + parameters.AddValueToStringList( "StringListParam", "two" ); + parameters.AddValueToStringList( "StringListParam", "three" ); + parameters.AddValueToStringList( "StringListParam", "four" ); + + parameters.AddValueToBoolList( "BoolListParam", 1 ); + parameters.AddValueToBoolList( "BoolListParam", 0 ); + parameters.AddValueToBoolList( "BoolListParam", 0 ); + parameters.AddValueToBoolList( "BoolListParam", 1 ); + parameters.AddValueToBoolList( "BoolListParam", 1 ); + + parameters.AddValueToIntList( "IntListParam", 1 ); + parameters.AddValueToIntList( "IntListParam", -2 ); + parameters.AddValueToIntList( "IntListParam", 3 ); + parameters.AddValueToIntList( "IntListParam", -4 ); + parameters.AddValueToIntList( "IntListParam", 5 ); + parameters.AddValueToIntList( "IntListParam", -6 ); + parameters.AddValueToIntList( "IntListParam", 7 ); + parameters.AddValueToIntList( "IntListParam", -8 ); + parameters.AddValueToIntList( "IntListParam", 9 ); + parameters.AddValueToIntList( "IntListParam", -10 ); + parameters.AddValueToIntList( "IntListParam", 11 ); + + parameters.AddValueToUintList( "UintListParam", 1 ); + parameters.AddValueToUintList( "UintListParam", 2 ); + parameters.AddValueToUintList( "UintListParam", 3 ); + parameters.AddValueToUintList( "UintListParam", 4 ); + parameters.AddValueToUintList( "UintListParam", 5 ); + parameters.AddValueToUintList( "UintListParam", 6 ); + parameters.AddValueToUintList( "UintListParam", 7 ); + parameters.AddValueToUintList( "UintListParam", 8 ); + parameters.AddValueToUintList( "UintListParam", 9 ); + parameters.AddValueToUintList( "UintListParam", 10 ); + parameters.AddValueToUintList( "UintListParam", 11 ); + + parameters.AddValueToRealList( "RealListParam", 1.9 ); + parameters.AddValueToRealList( "RealListParam", 2.8 ); + parameters.AddValueToRealList( "RealListParam", 3.7 ); + parameters.AddValueToRealList( "RealListParam", 4.6 ); + parameters.AddValueToRealList( "RealListParam", 5.5 ); + parameters.AddValueToRealList( "RealListParam", 6.4 ); + parameters.AddValueToRealList( "RealListParam", 7.3 ); + parameters.AddValueToRealList( "RealListParam", 8.2 ); + parameters.AddValueToRealList( "RealListParam", 9.1 ); + parameters.AddValueToRealList( "RealListParam", 1.0012 ); + parameters.AddValueToRealList( "RealListParam", 1.131245 ); + + parameters.AddValueToIndexList( "IndexListParam", 3, -1, 2, 3 ); + parameters.AddValueToIndexList( "IndexListParam", 3, 2, -3, 4 ); + parameters.AddValueToIndexList( "IndexListParam", 3, 3, 4, 7 ); + + parameters.AddValueToPointList( "PointListParam", 3, 3.45, 4.67, 7.9 ); + parameters.AddValueToPointList( "PointListParam", 3, 3.45, 4.67, 3.14 ); + parameters.AddValueToPointList( "PointListParam", 3, 1.45, 3.67, 2.14 ); + parameters.AddValueToPointList( "PointListParam", 3, 4.5, 6.7, 4.2 ); + + std::vector< TParameters::TString > names = parameters.GetParameters( ); + std::cout << "Names:" << std::endl; + for( unsigned int i = 0; i < names.size( ); ++i ) + std::cout + << "\t... " + << names[ i ] << " " + << parameters.GetRawValue( names[ i ] ) << "(" + << parameters.GetParameterType( names[ i ] ) << ")" + << std::endl; + + std::cout << std::endl << "Values with type checking" << std::endl; + std::cout + << "\t... " << "StringParam: " + << parameters.GetValueAsString( "StringParam" ) << std::endl + << "\t... " << "BoolParam: " + << parameters.GetValueAsBool( "BoolParam" ) << std::endl + << "\t... " << "IntParam: " + << parameters.GetValueAsInt( "IntParam" ) << std::endl + << "\t... " << "UintParam: " + << parameters.GetValueAsUint( "UintParam" ) << std::endl + << "\t... " << "RealParam: " + << parameters.GetValueAsReal( "RealParam" ) << std::endl + << "\t... " << "IndexParam: " + << parameters.GetValueAsIndex< TIndex >( "IndexParam" ) << std::endl + << "\t... " << "PointParam: " + << parameters.GetValueAsPoint< TPoint >( "PointParam" ) << std::endl; + + std::vector< TParameters::TString > string_lst; + std::vector< TParameters::TBool > bool_lst; + std::vector< TParameters::TInt > int_lst; + std::vector< TParameters::TUint > uint_lst; + std::vector< TParameters::TReal > real_lst; + std::vector< TIndex > index_lst; + std::vector< TPoint > point_lst; + + parameters.GetValueAsStringList( string_lst, "StringListParam" ); + parameters.GetValueAsBoolList( bool_lst, "BoolListParam" ); + parameters.GetValueAsIntList( int_lst, "IntListParam" ); + parameters.GetValueAsUintList( uint_lst, "UintListParam" ); + parameters.GetValueAsRealList( real_lst, "RealListParam" ); + parameters.GetValueAsIndexList( index_lst, "IndexListParam" ); + parameters.GetValueAsPointList( point_lst, "PointListParam" ); + + std::cout << "List parameters:" << std::endl; + PrintList( string_lst, "StringListParam" ); + PrintList( bool_lst, "BoolListParam" ); + PrintList( int_lst, "IntListParam" ); + PrintList( uint_lst, "UintListParam" ); + PrintList( real_lst, "RealListParam" ); + PrintList( index_lst, "IndexListParam" ); + PrintList( point_lst, "PointListParam" ); + + return( 0 ); +} // eof - $RCSfile$ diff --git a/lib/cpPlugins/Interface/Parameters.cxx b/lib/cpPlugins/Interface/Parameters.cxx index b8d358b..54eab8d 100644 --- a/lib/cpPlugins/Interface/Parameters.cxx +++ b/lib/cpPlugins/Interface/Parameters.cxx @@ -43,9 +43,75 @@ cpPlugins_Interface_Parameters_SetMacro( Real ); pIt->second.second = ss.str( ); \ } -cpPlugins_Interface_Parameters_SetArrayMacro( Index, long ); +cpPlugins_Interface_Parameters_SetArrayMacro( Index, int ); cpPlugins_Interface_Parameters_SetArrayMacro( Point, double ); +// ------------------------------------------------------------------------- +#define cpPlugins_Interface_Parameters_SetListMacro( TYPE ) \ + void cpPlugins::Interface::Parameters:: \ + AddValueTo##TYPE##List( const TString& name, const T##TYPE& v ) \ + { \ + TParameters::iterator pIt = this->m_Parameters.find( name ); \ + if( pIt == this->m_Parameters.end( ) ) \ + return; \ + if( pIt->second.first != Self::TYPE##List ) \ + return; \ + std::stringstream ss; \ + ss << pIt->second.second << v << ":"; \ + pIt->second.second = ss.str( ); \ + } + +cpPlugins_Interface_Parameters_SetListMacro( String ); +cpPlugins_Interface_Parameters_SetListMacro( Bool ); +cpPlugins_Interface_Parameters_SetListMacro( Int ); +cpPlugins_Interface_Parameters_SetListMacro( Uint ); +cpPlugins_Interface_Parameters_SetListMacro( Real ); + +// ------------------------------------------------------------------------- +#define cpPlugins_Interface_Parameters_SetArrayListMacro( TYPE, ATYPE ) \ + void cpPlugins::Interface::Parameters:: \ + AddValueTo##TYPE##List( const TString& name, const TUint& n, ... ) \ + { \ + TParameters::iterator pIt = this->m_Parameters.find( name ); \ + if( pIt == this->m_Parameters.end( ) ) \ + return; \ + if( pIt->second.first != Self::TYPE##List ) \ + return; \ + va_list v_lst; \ + va_start( v_lst, n ); \ + std::stringstream ss; \ + ss << pIt->second.second; \ + for( TUint i = 0; i < n; ++i ) \ + ss << va_arg( v_lst, ATYPE ) << ","; \ + va_end( v_lst ); \ + ss << ":"; \ + pIt->second.second = ss.str( ); \ + } + +cpPlugins_Interface_Parameters_SetArrayListMacro( Index, int ); +cpPlugins_Interface_Parameters_SetArrayListMacro( Point, double ); + +// ------------------------------------------------------------------------- +#define cpPlugins_Interface_Parameters_ClearListMacro( TYPE ) \ + void cpPlugins::Interface::Parameters:: \ + Clear##TYPE##List( const TString& name ) \ + { \ + TParameters::iterator pIt = this->m_Parameters.find( name ); \ + if( pIt == this->m_Parameters.end( ) ) \ + return; \ + if( pIt->second.first != Self::TYPE##List ) \ + return; \ + pIt->second.second = ""; \ + } + +cpPlugins_Interface_Parameters_ClearListMacro( String ); +cpPlugins_Interface_Parameters_ClearListMacro( Bool ); +cpPlugins_Interface_Parameters_ClearListMacro( Int ); +cpPlugins_Interface_Parameters_ClearListMacro( Uint ); +cpPlugins_Interface_Parameters_ClearListMacro( Real ); +cpPlugins_Interface_Parameters_ClearListMacro( Index ); +cpPlugins_Interface_Parameters_ClearListMacro( Point ); + // ------------------------------------------------------------------------- cpPlugins::Interface::Parameters:: Parameters( ) @@ -113,6 +179,18 @@ GetParameterType( const TString& name ) const return( pIt->second.first ); } +// ------------------------------------------------------------------------- +const cpPlugins::Interface::Parameters:: +TString& cpPlugins::Interface::Parameters:: +GetRawValue( const TString& name ) const +{ + static const TString null_str = ""; + TParameters::const_iterator pIt = this->m_Parameters.find( name ); + if( pIt == this->m_Parameters.end( ) ) + return( null_str ); + return( pIt->second.second ); +} + // ------------------------------------------------------------------------- const cpPlugins::Interface::Parameters:: TString& cpPlugins::Interface::Parameters:: @@ -136,7 +214,7 @@ GetValueAsBool( const TString& name ) const TParameters::const_iterator pIt = this->m_Parameters.find( name ); if( pIt == this->m_Parameters.end( ) ) return( TBool( false ) ); - if( pIt->second.first != Self::Int ) + if( pIt->second.first != Self::Bool ) return( TBool( false ) ); return( TBool( std::atoi( pIt->second.second.c_str( ) ) == 1 ) ); } @@ -186,30 +264,90 @@ GetValueAsStringList( std::vector< TString >& lst, const TString& name ) const { + lst.clear( ); + TParameters::const_iterator pIt = this->m_Parameters.find( name ); + if( pIt == this->m_Parameters.end( ) ) + return; + if( pIt->second.first != Self::StringList ) + return; + + std::istringstream ss( pIt->second.second ); + std::string token; + while( std::getline( ss, token, ':' ) ) + if( token != "" ) + lst.push_back( token ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Parameters:: GetValueAsBoolList( std::vector< TBool >& lst, const TString& name ) const { + lst.clear( ); + TParameters::const_iterator pIt = this->m_Parameters.find( name ); + if( pIt == this->m_Parameters.end( ) ) + return; + if( pIt->second.first != Self::BoolList ) + return; + + std::istringstream ss( pIt->second.second ); + std::string token; + while( std::getline( ss, token, ':' ) ) + if( token != "" ) + lst.push_back( TBool( std::atoi( token.c_str( ) ) == 1 ) ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Parameters:: GetValueAsIntList( std::vector< TInt >& lst, const TString& name ) const { + lst.clear( ); + TParameters::const_iterator pIt = this->m_Parameters.find( name ); + if( pIt == this->m_Parameters.end( ) ) + return; + if( pIt->second.first != Self::IntList ) + return; + + std::istringstream ss( pIt->second.second ); + std::string token; + while( std::getline( ss, token, ':' ) ) + if( token != "" ) + lst.push_back( TInt( std::atoi( token.c_str( ) ) ) ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Parameters:: GetValueAsUintList( std::vector< TUint >& lst, const TString& name ) const { + lst.clear( ); + TParameters::const_iterator pIt = this->m_Parameters.find( name ); + if( pIt == this->m_Parameters.end( ) ) + return; + if( pIt->second.first != Self::UintList ) + return; + + std::istringstream ss( pIt->second.second ); + std::string token; + while( std::getline( ss, token, ':' ) ) + if( token != "" ) + lst.push_back( TUint( std::atoi( token.c_str( ) ) ) ); } // ------------------------------------------------------------------------- void cpPlugins::Interface::Parameters:: GetValueAsRealList( std::vector< TReal >& lst, const TString& name ) const { + lst.clear( ); + TParameters::const_iterator pIt = this->m_Parameters.find( name ); + if( pIt == this->m_Parameters.end( ) ) + return; + if( pIt->second.first != Self::RealList ) + return; + + std::istringstream ss( pIt->second.second ); + std::string token; + while( std::getline( ss, token, ':' ) ) + if( token != "" ) + lst.push_back( TReal( std::atof( token.c_str( ) ) ) ); } // eof - $RCSfile$ diff --git a/lib/cpPlugins/Interface/Parameters.h b/lib/cpPlugins/Interface/Parameters.h index bbf9dfc..65bf2a8 100644 --- a/lib/cpPlugins/Interface/Parameters.h +++ b/lib/cpPlugins/Interface/Parameters.h @@ -61,43 +61,25 @@ namespace cpPlugins void SetValueAsIndex( const TString& name, const TUint& n, ... ); void SetValueAsPoint( const TString& name, const TUint& n, ... ); - template< class I > - void SetValueAsStringList( - const TString& name, const I& b, const I& e - ); - - template< class I > - void SetValueAsBoolList( - const TString& name, const I& b, const I& e - ); - - template< class I > - void SetValueAsIntList( - const TString& name, const I& b, const I& e - ); - - template< class I > - void SetValueAsUintList( - const TString& name, const I& b, const I& e - ); - - template< class I > - void SetValueAsRealList( - const TString& name, const I& b, const I& e - ); - - template< class I > - void SetValueAsIndexList( - const TString& name, const I& b, const I& e - ); - - template< class I > - void SetValueAsPointList( - const TString& name, const I& b, const I& e - ); - + void AddValueToStringList( const TString& name, const TString& v ); + void AddValueToBoolList( const TString& name, const TBool& v ); + void AddValueToIntList( const TString& name, const TInt& v ); + void AddValueToUintList( const TString& name, const TUint& v ); + void AddValueToRealList( const TString& name, const TReal& v ); + void AddValueToIndexList( const TString& name, const TUint& n, ... ); + void AddValueToPointList( const TString& name, const TUint& n, ... ); + + void ClearStringList( const TString& name ); + void ClearBoolList( const TString& name ); + void ClearIntList( const TString& name ); + void ClearUintList( const TString& name ); + void ClearRealList( const TString& name ); + void ClearIndexList( const TString& name ); + void ClearPointList( const TString& name ); + std::vector< TString > GetParameters( ) const; Self::Type GetParameterType( const TString& name ) const; + const TString& GetRawValue( const TString& name ) const; const TString& GetValueAsString( const TString& name ) const; TBool GetValueAsBool( const TString& name ) const; TInt GetValueAsInt( const TString& name ) const; diff --git a/lib/cpPlugins/Interface/Parameters.hxx b/lib/cpPlugins/Interface/Parameters.hxx index 9afcc32..e47b799 100644 --- a/lib/cpPlugins/Interface/Parameters.hxx +++ b/lib/cpPlugins/Interface/Parameters.hxx @@ -5,31 +5,6 @@ #include #include -// ------------------------------------------------------------------------- -#define cpPlugins_Interface_Parameters_SetListMacro( TYPE ) \ - template< class I > \ - void cpPlugins::Interface::Parameters:: \ - SetValueAs##TYPE##List( const TString& name, const I& b, const I& e ) \ - { \ - TParameters::iterator pIt = this->m_Parameters.find( name ); \ - if( pIt == this->m_Parameters.end( ) ) \ - return; \ - if( pIt->second.first != Self::TYPE##List ) \ - return; \ - std::stringstream ss; \ - for( I i = b; i != e; ++i ) \ - ss << *i << ":"; \ - pIt->second.second = ss.str( ); \ - } - -cpPlugins_Interface_Parameters_SetListMacro( String ); -cpPlugins_Interface_Parameters_SetListMacro( Bool ); -cpPlugins_Interface_Parameters_SetListMacro( Int ); -cpPlugins_Interface_Parameters_SetListMacro( Uint ); -cpPlugins_Interface_Parameters_SetListMacro( Real ); -cpPlugins_Interface_Parameters_SetListMacro( Index ); -cpPlugins_Interface_Parameters_SetListMacro( Point ); - // ------------------------------------------------------------------------- #define cpPlugins_Interface_Parameters_SetIndexOrPointMacro( NAME, TYPE ) \ template< class T > \ @@ -37,7 +12,7 @@ cpPlugins_Interface_Parameters_SetListMacro( Point ); GetValueAs##NAME( const TString& name ) const \ { \ T val; \ - TParameters::iterator pIt = this->m_Parameters.find( name ); \ + TParameters::const_iterator pIt = this->m_Parameters.find( name ); \ if( pIt != this->m_Parameters.end( ) ) \ { \ if( pIt->second.first == Self::NAME ) \ @@ -47,9 +22,8 @@ cpPlugins_Interface_Parameters_SetListMacro( Point ); unsigned int i = 0; \ while( std::getline( ss, token, ',' ) ) \ { \ - if( token != "" ) \ + if( token != "" ) \ val[ i++ ] = TYPE( std::atof( token.c_str( ) ) ); \ - \ } \ } \ } \ @@ -65,14 +39,30 @@ void cpPlugins::Interface::Parameters:: GetValueAsIndexList( std::vector< I >& lst, const TString& name ) const { lst.clear( ); - TParameters::iterator pIt = this->m_Parameters.find( name ); + TParameters::const_iterator pIt = this->m_Parameters.find( name ); if( pIt == this->m_Parameters.end( ) ) return; if( pIt->second.first != Self::IndexList ) return; - // TODO: - std::cerr << "TODO GetValueAsIndexList" << std::endl; + std::istringstream ss( pIt->second.second ); + std::string token; + while( std::getline( ss, token, ':' ) ) + { + if( token != "" ) + { + std::istringstream ts( token ); + std::string text; + unsigned int i = 0; + I idx; + while( std::getline( ts, text, ',' ) ) + if( text != "" ) + idx[ i++ ] = std::atoi( text.c_str( ) ); + lst.push_back( idx ); + + } // fi + + } // elihw } // ------------------------------------------------------------------------- @@ -81,14 +71,30 @@ void cpPlugins::Interface::Parameters:: GetValueAsPointList( std::vector< P >& lst, const TString& name ) const { lst.clear( ); - TParameters::iterator pIt = this->m_Parameters.find( name ); + TParameters::const_iterator pIt = this->m_Parameters.find( name ); if( pIt == this->m_Parameters.end( ) ) return; if( pIt->second.first != Self::PointList ) return; - // TODO: - std::cerr << "TODO GetValueAsPointList" << std::endl; + std::istringstream ss( pIt->second.second ); + std::string token; + while( std::getline( ss, token, ':' ) ) + { + if( token != "" ) + { + std::istringstream ts( token ); + std::string text; + unsigned int i = 0; + P pnt; + while( std::getline( ts, text, ',' ) ) + if( text != "" ) + pnt[ i++ ] = std::atof( text.c_str( ) ); + lst.push_back( pnt ); + + } // fi + + } // elihw } #endif // __CPPLUGINS__INTERFACE__PARAMETERS__HXX__