]> Creatis software - cpPlugins.git/blobdiff - lib/cpPlugins/Interface/Parameters.hxx
Merge branch 'master' of ssh://git.creatis.insa-lyon.fr/cpPlugins
[cpPlugins.git] / lib / cpPlugins / Interface / Parameters.hxx
index 0a73b373b2ca558f7a3ed21a81a2eb8cf51f455a..791d526263ad5584ab0b0bfe73010e7403d7ba6a 100644 (file)
 #ifndef __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
 #define __CPPLUGINS__INTERFACE__PARAMETERS__HXX__
 
-#include <cstdlib>
-#include <iostream>
-#include <sstream>
+// -------------------------------------------------------------------------
+template< class I >
+void cpPlugins::Interface::Parameters::
+ConfigureAsIndex( const TString& name, const TUint& dim, const I& v )
+{
+  std::stringstream str;
+  str << v[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    str << ";" << v[ d ];
+  std::string s = str.str( );
+  this->m_Parameters[ name ] = TParameter( Self::Index, TValues( s, s ) );
+  this->Modified( );
+}
 
 // -------------------------------------------------------------------------
-#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( Int );
-cpPlugins_Interface_Parameters_SetListMacro( Uint );
-cpPlugins_Interface_Parameters_SetListMacro( Real );
-cpPlugins_Interface_Parameters_SetListMacro( Index );
-cpPlugins_Interface_Parameters_SetListMacro( Point );
+template< class P >
+void cpPlugins::Interface::Parameters::
+ConfigureAsPoint( const TString& name, const TUint& dim, const P& v )
+{
+  std::stringstream str;
+  str << v[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    str << ";" << v[ d ];
+  std::string s = str.str( );
+  this->m_Parameters[ name ] = TParameter( Self::Point, TValues( s, s ) );
+  this->Modified( );
+}
 
 // -------------------------------------------------------------------------
 template< class I >
 I cpPlugins::Interface::Parameters::
-GetValueAsIndex( const TString& name ) const
+GetIndex( const TString& name, const TUint& dim ) const
 {
-  I idx;
-  TParameters::iterator pIt = this->m_Parameters.find( name );
-  if( pIt != this->m_Parameters.end( ) )
+  I v;
+  TParameters::const_iterator i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
   {
-    if( pIt->second.first == Self::Index )
+    if( i->second.first == Self::Index )
     {
-      std::istringstream ss( pIt->second.second );
+      std::istringstream str( i->second.second.second );
       std::string token;
-      unsigned int i = 0;
-      while( std::getline( ss, token, ',' ) )
+      unsigned int d = 0;
+      while( std::getline( str, token, ';' ) && d < dim )
       {
-        if( token != "" )
-          idx[ i++ ] = std::atoi( token.c_str( ) );
+        v[ d ] = std::atoi( token.c_str( ) );
+        d++;
 
       } // elihw
+      return( v );
 
     } // fi
 
   } // fi
-  return( idx );
+
+  // If parameter not found
+  for( unsigned int d = 0; d < dim; ++d )
+    v[ d ] = 0;
+  return( v );
 }
 
 // -------------------------------------------------------------------------
 template< class P >
 P cpPlugins::Interface::Parameters::
-GetValueAsPoint( const TString& name ) const
+GetPoint( const TString& name, const TUint& dim ) const
 {
-  P pnt;
-  TParameters::iterator pIt = this->m_Parameters.find( name );
-  if( pIt != this->m_Parameters.end( ) )
+  P v;
+  TParameters::const_iterator i = this->m_Parameters.find( name );
+  if( i != this->m_Parameters.end( ) )
   {
-    if( pIt->second.first == Self::Point )
+    if( i->second.first == Self::Point )
     {
-      std::istringstream ss( pIt->second.second );
+      std::istringstream str( i->second.second.second );
       std::string token;
-      unsigned int i = 0;
-      while( std::getline( ss, token, ',' ) )
+      unsigned int d = 0;
+      while( std::getline( str, token, ';' ) && d < dim )
       {
-        if( token != "" )
-          pnt[ i++ ] = std::atof( token.c_str( ) );
+        v[ d ] = std::atof( token.c_str( ) );
+        d++;
 
       } // elihw
+      return( v );
 
     } // fi
 
   } // fi
-  return( pnt );
+
+  // If parameter not found
+  for( unsigned int d = 0; d < dim; ++d )
+    v[ d ] = float( 0 );
+  return( v );
 }
 
 // -------------------------------------------------------------------------
 template< class I >
 void cpPlugins::Interface::Parameters::
-GetValueAsIndexList( std::vector< I >& lst, const TString& name ) const
+GetIndexList(
+  std::vector< I >& lst, const TString& name, const TUint& dim
+  ) const
 {
   lst.clear( );
-  TParameters::iterator pIt = this->m_Parameters.find( name );
-  if( pIt == this->m_Parameters.end( ) )
+
+  TParameters::const_iterator i = this->m_Parameters.find( name );
+  if( i == this->m_Parameters.end( ) )
     return;
-  if( pIt->second.first != Self::IndexList )
+  if( i->second.first == Self::IndexList )
     return;
 
-  // TODO:
-  std::cerr << "TODO GetValueAsIndexList" << std::endl;
+  std::istringstream str( i->second.second.second );
+  std::string token;
+  unsigned int d = 0;
+  while( std::getline( str, token, '#' ) )
+  {
+    std::istringstream str2( token );
+    std::string token2;
+    unsigned int d = 0;
+    I v;
+    while( std::getline( str2, token2, ';' ) && d < dim )
+    {
+      v[ d ] = std::atoi( token.c_str( ) );
+      d++;
+
+    } // elihw
+    lst.push_back( v );
+
+  } // elihw
 }
 
 // -------------------------------------------------------------------------
 template< class P >
 void cpPlugins::Interface::Parameters::
-GetValueAsPointList( std::vector< P >& lst, const TString& name ) const
+GetPointList(
+  std::vector< P >& lst, const TString& name, const TUint& dim
+  ) const
 {
   lst.clear( );
-  TParameters::iterator pIt = this->m_Parameters.find( name );
-  if( pIt == this->m_Parameters.end( ) )
+
+  TParameters::const_iterator i = this->m_Parameters.find( name );
+  if( i == this->m_Parameters.end( ) )
+    return;
+  if( i->second.first == Self::PointList )
+    return;
+
+  std::istringstream str( i->second.second.second );
+  std::string token;
+  unsigned int d = 0;
+  while( std::getline( str, token, '#' ) )
+  {
+    std::istringstream str2( token );
+    std::string token2;
+    unsigned int d = 0;
+    P v;
+    while( std::getline( str2, token2, ';' ) && d < dim )
+    {
+      v[ d ] = std::atof( token.c_str( ) );
+      d++;
+
+    } // elihw
+    lst.push_back( v );
+
+  } // elihw
+}
+
+// -------------------------------------------------------------------------
+template< class I >
+void cpPlugins::Interface::Parameters::
+SetIndex( const TString& name, const TUint& dim, const I& v )
+{
+  TParameters::iterator i = this->m_Parameters.find( name );
+  if( i == this->m_Parameters.end( ) )
+    return;
+  if( i->second.first != Self::Index )
+    return;
+
+  std::stringstream str;
+  str << v[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    str << ";" << v[ d ];
+  i->second.second.second = str.str( );
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+template< class P >
+void cpPlugins::Interface::Parameters::
+SetPoint( const TString& name, const TUint& dim, const P& v )
+{
+  TParameters::iterator i = this->m_Parameters.find( name );
+  if( i == this->m_Parameters.end( ) )
+    return;
+  if( i->second.first != Self::Point )
+    return;
+
+  std::stringstream str;
+  str << v[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    str << ";" << v[ d ];
+  i->second.second.second = str.str( );
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+template< class I >
+void cpPlugins::Interface::Parameters::
+AddToIndexList( const TString& name, const TUint& dim, const I& v )
+{
+  TParameters::iterator i = this->m_Parameters.find( name );
+  if( i == this->m_Parameters.end( ) )
+    return;
+  if( i->second.first != Self::IndexList )
+    return;
+
+  std::stringstream str;
+  if( i->second.second.second == "" )
+    str << v[ 0 ];
+  else
+    str << "#" << v[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    str << ";" << v[ d ];
+  i->second.second.second += str.str( );
+  this->Modified( );
+}
+
+// -------------------------------------------------------------------------
+template< class P >
+void cpPlugins::Interface::Parameters::
+AddToPointList( const TString& name, const TUint& dim, const P& v )
+{
+  TParameters::iterator i = this->m_Parameters.find( name );
+  if( i == this->m_Parameters.end( ) )
     return;
-  if( pIt->second.first != Self::PointList )
+  if( i->second.first != Self::PointList )
     return;
 
-  // TODO:
-  std::cerr << "TODO GetValueAsPointList" << std::endl;
+  std::stringstream str;
+  if( i->second.second.second == "" )
+    str << v[ 0 ];
+  else
+    str << "#" << v[ 0 ];
+  for( unsigned int d = 1; d < dim; ++d )
+    str << ";" << v[ d ];
+  i->second.second.second += str.str( );
+  this->Modified( );
 }
 
 #endif // __CPPLUGINS__INTERFACE__PARAMETERS__HXX__