go home Home | Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages
itkParameterMapInterface.h
Go to the documentation of this file.
00001 /*======================================================================
00002 
00003 This file is part of the elastix software.
00004 
00005 Copyright (c) University Medical Center Utrecht. All rights reserved.
00006 See src/CopyrightElastix.txt or http://elastix.isi.uu.nl/legal.php for
00007 details.
00008 
00009 This software is distributed WITHOUT ANY WARRANTY; without even
00010 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00011 PURPOSE. See the above copyright notices for more information.
00012 
00013 ======================================================================*/
00014 
00015 #ifndef __itkParameterMapInterface_h
00016 #define __itkParameterMapInterface_h
00017 
00018 #include "itkObject.h"
00019 #include "itkObjectFactory.h"
00020 #include "itkMacro.h"
00021 #include "itkNumericTraits.h"
00022 
00023 #include "itkParameterFileParser.h"
00024 
00025 #include <iostream>
00026 
00027 
00028 namespace itk
00029 {
00030 
00070 class ParameterMapInterface : public Object
00071 {
00072 public:
00073 
00075   typedef ParameterMapInterface       Self;
00076   typedef Object                      Superclass;
00077   typedef SmartPointer< Self >        Pointer;
00078   typedef SmartPointer< const Self >  ConstPointer;
00079 
00081   itkNewMacro( Self );
00082 
00084   itkTypeMacro( ParameterMapInterface, Object );
00085 
00087   typedef ParameterFileParser::ParameterValuesType  ParameterValuesType;
00088   typedef ParameterFileParser::ParameterMapType     ParameterMapType;
00089 
00091   void SetParameterMap( const ParameterMapType & parMap );
00092 
00096   // \todo: we could think of a warning level. (maybe you want warnings, but
00097   // not when for example a parameter is not found at entry entry_nr but at entry 0 instead
00098   itkSetMacro( PrintErrorMessages, bool );
00099   itkGetConstMacro( PrintErrorMessages, bool );
00100 
00102   std::size_t CountNumberOfParameterEntries(
00103     const std::string & parameterName ) const;
00104 
00119   template <class T>
00120   bool ReadParameter( T & parameterValue,
00121     const std::string & parameterName,
00122     const unsigned int entry_nr,
00123     const bool printThisErrorMessage,
00124     std::string & errorMessage ) const
00125   {
00127     errorMessage = "";
00128 
00130     std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
00131       parameterName );
00132 
00134     if ( numberOfEntries == 0 )
00135     {
00136       std::stringstream ss;
00137       ss << "WARNING: The parameter \"" << parameterName
00138         << "\", requested at entry number " << entry_nr
00139         << ", does not exist at all.\n"
00140         << "  The default value \"" << parameterValue
00141         << "\" is used instead." << std::endl;
00142       if ( printThisErrorMessage && this->m_PrintErrorMessages )
00143       {
00144         errorMessage = ss.str();
00145       }
00146 
00147       return false;
00148     }
00149 
00151     const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
00152 
00154     if ( entry_nr >= numberOfEntries )
00155     {
00156       std::stringstream ss;
00157       ss << "WARNING: The parameter \"" << parameterName
00158         << "\" does not exist at entry number " << entry_nr
00159         << ".\n  The default value \"" << parameterValue
00160         << "\" is used instead." << std::endl;
00161       if ( printThisErrorMessage && this->m_PrintErrorMessages )
00162       {
00163         errorMessage = ss.str();
00164       }
00165       return false;
00166     }
00167 
00169     bool castSuccesful = this->StringCast( vec[ entry_nr ], parameterValue );
00170 
00172     if ( !castSuccesful )
00173     {
00174       std::stringstream ss;
00175       ss << "ERROR: Casting entry number " << entry_nr
00176         << " for the parameter \"" << parameterName
00177         << "\" failed!\n"
00178         << "  You tried to cast \"" << vec[ entry_nr ]
00179         << "\" from std::string to "
00180         << typeid( parameterValue ).name() << std::endl;
00181 
00182       itkExceptionMacro( << ss.str() );
00183     }
00184 
00185     return true;
00186 
00187   } // end ReadParameter()
00188 
00190   bool ReadParameter( bool & parameterValue,
00191     const std::string & parameterName,
00192     const unsigned int entry_nr,
00193     const bool printThisErrorMessage,
00194     std::string & errorMessage ) const;
00195 
00199   template <class T>
00200   bool ReadParameter( T & parameterValue,
00201     const std::string & parameterName,
00202     const unsigned int entry_nr,
00203     std::string & errorMessage ) const
00204   {
00205     return this->ReadParameter( parameterValue, parameterName, entry_nr,
00206       true, errorMessage );
00207   }
00208 
00214   template <class T>
00215   bool ReadParameter( T & parameterValue,
00216     const std::string & parameterName,
00217     const std::string & prefix,
00218     const unsigned int entry_nr,
00219     const int default_entry_nr,
00220     const bool printThisErrorMessage,
00221     std::string & errorMessage ) const
00222   {
00223     std::string fullname = prefix + parameterName;
00224     bool found = false;
00225 
00227     std::string dummyString = "";
00228     if ( default_entry_nr >= 0 )
00229     {
00231       unsigned int uintdefault = static_cast<unsigned int>( default_entry_nr );
00232       found |= this->ReadParameter( parameterValue, parameterName, uintdefault,
00233         false, dummyString );
00234       found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
00235         false, dummyString );
00236       found |= this->ReadParameter( parameterValue, fullname, uintdefault,
00237         false, dummyString );
00238       found |= this->ReadParameter( parameterValue, fullname, entry_nr,
00239         false, dummyString );
00240     }
00241     else
00242     {
00244       found |= this->ReadParameter( parameterValue, parameterName, entry_nr,
00245         false, dummyString );
00246       found |= this->ReadParameter( parameterValue, fullname, entry_nr,
00247         false, dummyString );
00248     }
00249 
00253     if ( !found && printThisErrorMessage && this->m_PrintErrorMessages )
00254     {
00255       return this->ReadParameter( parameterValue, parameterName, entry_nr,
00256         true, errorMessage );
00257     }
00258 
00259     return found;
00260 
00261   }
00262 
00266   template <class T>
00267   bool ReadParameter( T & parameterValue,
00268     const std::string & parameterName,
00269     const std::string & prefix,
00270     const unsigned int entry_nr,
00271     const unsigned int default_entry_nr,
00272     std::string & errorMessage ) const
00273   {
00274     return this->ReadParameter( parameterValue, parameterName, prefix,
00275       entry_nr, default_entry_nr, true, errorMessage );
00276   }
00277 
00279   template <class T>
00280   bool ReadParameter(
00281     std::vector< T > & parameterValues,
00282     const std::string & parameterName,
00283     const unsigned int entry_nr_start,
00284     const unsigned int entry_nr_end,
00285     const bool printThisErrorMessage,
00286     std::string & errorMessage ) const
00287   {
00289     errorMessage = "";
00290 
00292     std::size_t numberOfEntries = this->CountNumberOfParameterEntries(
00293       parameterName );
00294 
00296     if ( numberOfEntries == 0 )
00297     {
00298       std::stringstream ss;
00299       ss << "WARNING: The parameter \"" << parameterName
00300         << "\", requested between entry numbers " << entry_nr_start
00301         << " and " << entry_nr_end
00302         << ", does not exist at all.\n"
00303         << "  The default values are used instead." << std::endl;
00304       if ( printThisErrorMessage && this->m_PrintErrorMessages )
00305       {
00306         errorMessage = ss.str();
00307       }
00308       return false;
00309     }
00310 
00312     if ( entry_nr_start > entry_nr_end )
00313     {
00314       std::stringstream ss;
00315       ss << "WARNING: The entry number start (" << entry_nr_start
00316         << ") should be smaller than entry number end (" << entry_nr_end
00317         << "). It was requested for parameter \"" << parameterName
00318         << "\"." << std::endl;
00319 
00321       itkExceptionMacro( << ss.str() );
00322     }
00323 
00325     if ( entry_nr_end >= numberOfEntries )
00326     {
00327       std::stringstream ss;
00328       ss << "WARNING: The parameter \"" << parameterName
00329         << "\" does not exist at entry number " << entry_nr_end
00330         << ".\nThe default value \"" << itk::NumericTraits<T>::Zero
00331         << "\" is used instead." << std::endl;
00332       itkExceptionMacro( << ss.str() );
00333     }
00334 
00336     const ParameterValuesType & vec = this->m_ParameterMap.find( parameterName )->second;
00337 
00345     unsigned int j = 0;
00346     for ( unsigned int i = entry_nr_start; i < entry_nr_end + 1; ++i )
00347     {
00349       bool castSuccesful = this->StringCast( vec[ i ], parameterValues[ j ] );
00350       j++;
00351 
00353       if ( !castSuccesful )
00354       {
00355         std::stringstream ss;
00356         ss << "ERROR: Casting entry number " << i
00357           << " for the parameter \"" << parameterName
00358           << "\" failed!\n"
00359           << "  You tried to cast \"" << vec[ i ]
00360           << "\" from std::string to "
00361           << typeid( parameterValues[ 0 ] ).name() << std::endl;
00362 
00363         itkExceptionMacro( << ss.str() );
00364       }
00365     }
00366 
00367     return true;
00368   }
00369 
00371   bool ReadParameter(
00372     std::vector<std::string> & parameterValues,
00373     const std::string & parameterName,
00374     const unsigned int entry_nr_start,
00375     const unsigned int entry_nr_end,
00376     const bool printThisErrorMessage,
00377     std::string & errorMessage ) const;
00378 
00379 protected:
00380   ParameterMapInterface();
00381   virtual ~ParameterMapInterface();
00382 
00383 private:
00384   ParameterMapInterface(const Self&); // purposely not implemented
00385   void operator=(const Self&);        // purposely not implemented
00386 
00388   ParameterMapType  m_ParameterMap;
00389 
00390   bool              m_PrintErrorMessages;
00391 
00396   template <class T>
00397   bool StringCast( const std::string & parameterValue, T & casted ) const
00398   {
00399     std::stringstream ss( parameterValue );
00400     ss >> casted;
00401     if ( ss.bad() || ss.fail() )
00402     {
00403       return false;
00404     }
00405     return true;
00406 
00407   } // end StringCast()
00408 
00412   bool StringCast( const std::string & parameterValue, std::string & casted ) const;
00413 
00414 }; // end class ParameterMapInterface
00415 
00416 } // end of namespace itk
00417 
00418 #endif // end __itkParameterMapInterface_h


Generated on 24-05-2012 for elastix by doxygen 1.7.6.1 elastix logo