19 #if defined( _WIN32 ) && !defined( __CYGWIN__ ) 23 #include "elxElastixMain.h" 25 #include "itkMultiThreader.h" 43 xoutsimple_type g_WarningXout;
44 xoutsimple_type g_ErrorXout;
45 xoutsimple_type g_StandardXout;
46 xoutsimple_type g_CoutOnlyXout;
47 xoutsimple_type g_LogOnlyXout;
48 std::ofstream g_LogFileStream;
58 xoutSetup(
const char * logfilename,
bool setupLogging,
bool setupCout )
69 g_LogFileStream.open( logfilename );
70 if( !g_LogFileStream.is_open() )
72 std::cerr <<
"ERROR: LogFile cannot be opened!" << std::endl;
80 returndummy |= xout.AddOutput(
"log", &g_LogFileStream );
84 returndummy |= xout.AddOutput(
"cout", &std::cout );
88 returndummy |= g_LogOnlyXout.AddOutput(
"log", &g_LogFileStream );
89 returndummy |= g_CoutOnlyXout.AddOutput(
"cout", &std::cout );
92 g_WarningXout.SetOutputs( xout.GetCOutputs() );
93 g_ErrorXout.SetOutputs( xout.GetCOutputs() );
94 g_StandardXout.SetOutputs( xout.GetCOutputs() );
96 g_WarningXout.SetOutputs( xout.GetXOutputs() );
97 g_ErrorXout.SetOutputs( xout.GetXOutputs() );
98 g_StandardXout.SetOutputs( xout.GetXOutputs() );
101 returndummy |= xout.AddTargetCell(
"warning", &g_WarningXout );
102 returndummy |= xout.AddTargetCell(
"error", &g_ErrorXout );
103 returndummy |= xout.AddTargetCell(
"standard", &g_StandardXout );
104 returndummy |= xout.AddTargetCell(
"logonly", &g_LogOnlyXout );
105 returndummy |= xout.AddTargetCell(
"coutonly", &g_CoutOnlyXout );
108 xout[
"standard" ] << std::fixed;
109 xout[
"standard" ] << std::showpoint;
125 this->m_Configuration = ConfigurationType::New();
129 this->m_FixedImagePixelType =
"";
130 this->m_FixedImageDimension = 0;
132 this->m_MovingImagePixelType =
"";
133 this->m_MovingImageDimension = 0;
137 this->m_FixedImageContainer = 0;
138 this->m_MovingImageContainer = 0;
140 this->m_FixedMaskContainer = 0;
141 this->m_MovingMaskContainer = 0;
143 this->m_ResultImageContainer = 0;
145 this->m_FinalTransform = 0;
146 this->m_InitialTransform = 0;
147 this->m_TransformParametersMap.clear();
157 ElastixMain::ComponentLoaderPointer ElastixMain::s_ComponentLoader = 0;
181 int dummy = this->m_Configuration->Initialize( argmap );
184 xout[
"error" ] <<
"ERROR: Something went wrong during initialisation " 185 <<
"of the configuration object." << std::endl;
203 int dummy = this->m_Configuration->Initialize( argmap, inputMap );
206 xout[
"error" ] <<
"ERROR: Something went wrong during initialisation of the configuration object." << std::endl;
219 std::vector< ParameterMapType > & inputMaps )
221 this->m_Configurations.clear();
222 this->m_Configurations.resize( inputMaps.size() );
224 for(
size_t i = 0; i < inputMaps.size(); ++i )
229 this->m_Configurations[ i ] = ConfigurationType::New();
230 int dummy = this->m_Configurations[ i ]->Initialize( argmap, inputMaps[ i ] );
233 xout[
"error" ] <<
"ERROR: Something went wrong during initialisation of configuration object " << i <<
"." << std::endl;
238 this->m_Configuration = this->m_Configurations[ inputMaps.size() - 1 ];
254 this->SetProcessPriority();
255 this->SetMaximumNumberOfThreads();
258 int errorCode = this->InitDBIndex();
268 this->m_Elastix = this->CreateComponent(
"Elastix" );
270 catch( itk::ExceptionObject & excp )
273 xl::xout[
"error" ] << excp << std::endl;
279 this->GetElastixBase()->SetConfiguration( this->m_Configuration );
280 this->GetElastixBase()->SetComponentDatabase( this->s_CDB );
281 this->GetElastixBase()->SetDBIndex( this->m_DBIndex );
287 this->GetElastixBase()->SetRegistrationContainer(
288 this->CreateComponents(
"Registration",
"MultiResolutionRegistration",
291 this->GetElastixBase()->SetFixedImagePyramidContainer(
292 this->CreateComponents(
"FixedImagePyramid",
"FixedSmoothingImagePyramid",
295 this->GetElastixBase()->SetMovingImagePyramidContainer(
296 this->CreateComponents(
"MovingImagePyramid",
"MovingSmoothingImagePyramid",
299 this->GetElastixBase()->SetImageSamplerContainer(
300 this->CreateComponents(
"ImageSampler",
"", errorCode,
false ) );
302 this->GetElastixBase()->SetInterpolatorContainer(
303 this->CreateComponents(
"Interpolator",
"BSplineInterpolator",
306 this->GetElastixBase()->SetMetricContainer(
307 this->CreateComponents(
"Metric",
"", errorCode ) );
309 this->GetElastixBase()->SetOptimizerContainer(
310 this->CreateComponents(
"Optimizer",
"", errorCode ) );
312 this->GetElastixBase()->SetResampleInterpolatorContainer(
313 this->CreateComponents(
"ResampleInterpolator",
"FinalBSplineInterpolator",
316 this->GetElastixBase()->SetResamplerContainer(
317 this->CreateComponents(
"Resampler",
"DefaultResampler",
320 this->GetElastixBase()->SetTransformContainer(
321 this->CreateComponents(
"Transform",
"", errorCode ) );
326 xout[
"error" ] <<
"ERROR:" << std::endl;
327 xout[
"error" ] <<
"One or more components could not be created." << std::endl;
334 this->GetElastixBase()->SetFixedImageContainer( this->GetFixedImageContainer() );
335 this->GetElastixBase()->SetMovingImageContainer( this->GetMovingImageContainer() );
336 this->GetElastixBase()->SetFixedMaskContainer( this->GetFixedMaskContainer() );
337 this->GetElastixBase()->SetMovingMaskContainer( this->GetMovingMaskContainer() );
338 this->GetElastixBase()->SetResultImageContainer( this->GetResultImageContainer() );
341 this->GetElastixBase()->SetInitialTransform( this->GetInitialTransform() );
346 this->GetElastixBase()->SetOriginalFixedImageDirectionFlat(
347 this->GetOriginalFixedImageDirectionFlat() );
352 errorCode = this->GetElastixBase()->Run();
354 catch( itk::ExceptionObject & excp1 )
357 xl::xout[
"error" ] << excp1 << std::endl;
360 catch( std::exception & excp2 )
363 xl::xout[
"error" ] <<
"std: " << excp2.what() << std::endl;
369 xl::xout[
"error" ] <<
"ERROR: an unknown non-ITK, non-std exception was caught.\n" 370 <<
"Please report this to elastix@bigr.nl." << std::endl;
375 this->m_FinalTransform = this->GetElastixBase()->GetFinalTransform();
378 this->m_TransformParametersMap = this->GetElastixBase()->GetTransformParametersMap();
381 this->SetFixedImageContainer( this->GetElastixBase()->GetFixedImageContainer() );
382 this->SetMovingImageContainer( this->GetElastixBase()->GetMovingImageContainer() );
383 this->SetFixedMaskContainer( this->GetElastixBase()->GetFixedMaskContainer() );
384 this->SetMovingMaskContainer( this->GetElastixBase()->GetMovingMaskContainer() );
385 this->SetResultImageContainer( this->GetElastixBase()->GetResultImageContainer() );
389 this->SetOriginalFixedImageDirectionFlat(
390 this->GetElastixBase()->GetOriginalFixedImageDirectionFlat() );
405 this->EnterCommandLineArguments( argmap );
419 this->EnterCommandLineArguments( argmap, inputMap );
436 if( this->m_Configuration->IsInitialized() )
439 if( this->m_FixedImagePixelType.empty() )
442 this->m_FixedImagePixelType =
"float";
443 this->m_Configuration->ReadParameter( this->m_FixedImagePixelType,
444 "FixedInternalImagePixelType", 0 );
448 if( this->m_FixedImageDimension == 0 )
450 #ifndef _ELASTIX_BUILD_LIBRARY 452 std::string fixedImageFileName
453 = this->m_Configuration->GetCommandLineArgument(
"-f" );
454 if( fixedImageFileName ==
"" )
456 fixedImageFileName = this->m_Configuration->GetCommandLineArgument(
"-f0" );
460 this->GetImageInformationFromFile( fixedImageFileName,
461 this->m_FixedImageDimension );
463 catch( itk::ExceptionObject & err )
465 xout[
"error" ] <<
"ERROR: could not read fixed image." << std::endl;
466 xout[
"error" ] << err << std::endl;
474 unsigned int fixDimParameterFile = 0;
475 bool foundInParameterFile = this->m_Configuration->ReadParameter( fixDimParameterFile,
476 "FixedImageDimension", 0,
false );
479 if( foundInParameterFile )
481 if( fixDimParameterFile != this->m_FixedImageDimension )
483 xout[
"error" ] <<
"ERROR: problem defining fixed image dimension.\n" 484 <<
" The parameter file says: " << fixDimParameterFile <<
"\n" 485 <<
" The fixed image header says: " << this->m_FixedImageDimension <<
"\n" 486 <<
" Note that from elastix 4.6 the parameter file definition \"FixedImageDimension\" " 487 <<
"is not needed anymore.\n Please remove this entry from your parameter file." 493 this->m_Configuration->ReadParameter( this->m_FixedImageDimension,
494 "FixedImageDimension", 0,
false );
498 if( this->m_FixedImageDimension == 0 )
500 xout[
"error" ] <<
"ERROR:" << std::endl;
501 xout[
"error" ] <<
"The FixedImageDimension is not given." << std::endl;
507 if( this->m_MovingImagePixelType.empty() )
510 this->m_MovingImagePixelType =
"float";
511 this->m_Configuration->ReadParameter( this->m_MovingImagePixelType,
512 "MovingInternalImagePixelType", 0 );
516 if( this->m_MovingImageDimension == 0 )
518 #ifndef _ELASTIX_BUILD_LIBRARY 520 std::string movingImageFileName
521 = this->m_Configuration->GetCommandLineArgument(
"-m" );
522 if( movingImageFileName ==
"" )
524 movingImageFileName = this->m_Configuration->GetCommandLineArgument(
"-m0" );
528 this->GetImageInformationFromFile( movingImageFileName,
529 this->m_MovingImageDimension );
531 catch( itk::ExceptionObject & err )
533 xout[
"error" ] <<
"ERROR: could not read moving image." << std::endl;
534 xout[
"error" ] << err << std::endl;
542 unsigned int movDimParameterFile = 0;
543 bool foundInParameterFile = this->m_Configuration->ReadParameter( movDimParameterFile,
544 "MovingImageDimension", 0,
false );
547 if( foundInParameterFile )
549 if( movDimParameterFile != this->m_MovingImageDimension )
551 xout[
"error" ] <<
"ERROR: problem defining moving image dimension.\n" 552 <<
" The parameter file says: " << movDimParameterFile <<
"\n" 553 <<
" The moving image header says: " << this->m_MovingImageDimension <<
"\n" 554 <<
" Note that from elastix 4.6 the parameter file definition \"MovingImageDimension\" " 555 <<
"is not needed anymore.\n Please remove this entry from your parameter file." 562 this->m_Configuration->ReadParameter( this->m_MovingImageDimension,
563 "MovingImageDimension", 0,
false );
567 if( this->m_MovingImageDimension == 0 )
569 xout[
"error" ] <<
"ERROR:" << std::endl;
570 xout[
"error" ] <<
"The MovingImageDimension is not given." << std::endl;
576 if( this->s_CDB.IsNull() )
578 int loadReturnCode = this->LoadComponents();
579 if( loadReturnCode != 0 )
581 xout[
"error" ] <<
"Loading components failed" << std::endl;
582 return loadReturnCode;
586 if( this->s_CDB.IsNotNull() )
589 this->m_DBIndex = this->s_CDB->GetIndex(
590 this->m_FixedImagePixelType,
591 this->m_FixedImageDimension,
592 this->m_MovingImagePixelType,
593 this->m_MovingImageDimension );
594 if( this->m_DBIndex == 0 )
596 xout[
"error" ] <<
"ERROR:" << std::endl;
597 xout[
"error" ] <<
"Something went wrong in the ComponentDatabase" << std::endl;
605 xout[
"error" ] <<
"ERROR:" << std::endl;
606 xout[
"error" ] <<
"The configuration object has not been initialized." << std::endl;
624 this->m_Configuration->SetElastixLevel( level );
637 return this->m_Configuration->GetElastixLevel();
650 this->m_Configuration->SetTotalNumberOfElastixLevels( levels );
663 return this->m_Configuration->GetTotalNumberOfElastixLevels();
679 if( this->s_CDB.IsNull() )
681 this->s_CDB = ComponentDatabaseType::New();
685 if( this->s_ComponentLoader.IsNull() )
687 this->s_ComponentLoader = ComponentLoaderType::New();
688 this->s_ComponentLoader->SetComponentDatabase( s_CDB );
693 = this->m_Configuration->GetCommandLineArgument(
"-argv0" ).c_str();
696 return this->s_ComponentLoader->LoadComponents( argv0 );
709 s_ComponentLoader->SetComponentDatabase( 0 );
711 if( s_ComponentLoader )
713 s_ComponentLoader->UnloadComponents();
716 s_ComponentLoader = 0;
731 testpointer =
dynamic_cast< ElastixBaseType *
>( this->m_Elastix.GetPointer() );
734 itkExceptionMacro( <<
"Probably GetElastixBase() is called before having called Run()" );
746 ElastixMain::ObjectPointer
748 const ComponentDescriptionType & name )
751 PtrToCreator testcreator = 0;
752 ObjectPointer testpointer = 0;
753 testcreator = this->s_CDB->GetCreator( name, this->m_DBIndex );
754 testpointer = testcreator ? testcreator() : NULL;
755 if( testpointer.IsNull() )
757 itkExceptionMacro( <<
"The following component could not be created: " << name );
769 ElastixMain::ObjectContainerPointer
771 const std::string & key,
772 const ComponentDescriptionType & defaultComponentName,
773 int & errorcode,
bool mandatoryComponent )
775 ComponentDescriptionType componentName = defaultComponentName;
776 unsigned int componentnr = 0;
777 ObjectContainerPointer objectContainer = ObjectContainerType::New();
778 objectContainer->Initialize();
784 bool found = this->m_Configuration->ReadParameter(
785 componentName, key, componentnr,
true );
791 if( !found && ( defaultComponentName ==
"" ) )
793 if( mandatoryComponent )
796 <<
"ERROR: the following component has not been specified: " 799 return objectContainer;
805 return objectContainer;
812 objectContainer->CreateElementAt( componentnr )
813 = this->CreateComponent( componentName );
815 catch( itk::ExceptionObject & excp )
818 <<
"ERROR: error occurred while creating " 820 << componentnr <<
"." << std::endl;
821 xout[
"error" ] << excp << std::endl;
823 return objectContainer;
830 found = this->m_Configuration->ReadParameter(
831 componentName, key, componentnr,
false );
836 objectContainer->CreateElementAt( componentnr )
837 = this->CreateComponent( componentName );
839 catch( itk::ExceptionObject & excp )
842 <<
"ERROR: error occurred while creating " 844 << componentnr <<
"." << std::endl;
845 xout[
"error" ] << excp << std::endl;
847 return objectContainer;
852 return objectContainer;
865 std::string processPriority =
"";
866 processPriority = this->m_Configuration->GetCommandLineArgument(
"-priority" );
867 if( processPriority ==
"high" )
869 #if defined( _WIN32 ) && !defined( __CYGWIN__ ) 870 SetPriorityClass( GetCurrentProcess(), HIGH_PRIORITY_CLASS );
873 else if( processPriority ==
"abovenormal" )
875 #if defined( _WIN32 ) && !defined( __CYGWIN__ ) 876 SetPriorityClass( GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS );
879 else if( processPriority ==
"normal" )
881 #if defined( _WIN32 ) && !defined( __CYGWIN__ ) 882 SetPriorityClass( GetCurrentProcess(), NORMAL_PRIORITY_CLASS );
885 else if( processPriority ==
"belownormal" )
887 #if defined( _WIN32 ) && !defined( __CYGWIN__ ) 888 SetPriorityClass( GetCurrentProcess(), BELOW_NORMAL_PRIORITY_CLASS );
891 else if( processPriority ==
"idle" )
893 #if defined( _WIN32 ) && !defined( __CYGWIN__ ) 894 SetPriorityClass( GetCurrentProcess(), IDLE_PRIORITY_CLASS );
897 else if( processPriority !=
"" )
899 xl::xout[
"warning" ]
900 <<
"Unsupported -priority value. Specify one of <high, abovenormal, normal, belownormal, idle, ''>." << std::endl;
914 std::string maximumNumberOfThreadsString =
"";
915 maximumNumberOfThreadsString
916 = this->m_Configuration->GetCommandLineArgument(
"-threads" );
919 if( maximumNumberOfThreadsString !=
"" )
921 const int maximumNumberOfThreads
922 = atoi( maximumNumberOfThreadsString.c_str() );
923 itk::MultiThreader::SetGlobalMaximumNumberOfThreads(
924 maximumNumberOfThreads );
935 const FlatDirectionCosinesType & arg )
937 this->m_OriginalFixedImageDirection = arg;
945 const ElastixMain::FlatDirectionCosinesType &
948 return this->m_OriginalFixedImageDirection;
959 return this->m_TransformParametersMap;
969 const std::string & filename,
970 ImageDimensionType & imageDimension )
const 975 const unsigned int DummyDimension = 3;
976 typedef short DummyPixelType;
977 typedef itk::Image< DummyPixelType, DummyDimension > DummyImageType;
980 typedef itk::ImageFileReader< DummyImageType > ReaderType;
981 ReaderType::Pointer testReader = ReaderType::New();
982 testReader->SetFileName( filename.c_str() );
985 testReader->GenerateOutputInformation();
988 itk::ImageIOBase::Pointer testImageIO = testReader->GetImageIO();
991 if( testImageIO.IsNull() )
995 itkExceptionMacro( <<
"ERROR: ImageIO object was not created, but no exception was thrown." );
997 imageDimension = testImageIO->GetNumberOfDimensions();
virtual void EnterCommandLineArguments(ArgumentMapType &argmap)
virtual int LoadComponents(void)
void GetImageInformationFromFile(const std::string &filename, ImageDimensionType &imageDimension) const
int xoutSetup(const char *logfilename, bool setupLogging, bool setupCout)
virtual int InitDBIndex(void)
ElastixBase ElastixBaseType
virtual ParameterMapType GetTransformParametersMap(void) const
void SetTotalNumberOfElastixLevels(unsigned int levels)
virtual const FlatDirectionCosinesType & GetOriginalFixedImageDirectionFlat(void) const
virtual ObjectPointer CreateComponent(const ComponentDescriptionType &name)
virtual void SetProcessPriority(void) const
virtual void SetOriginalFixedImageDirectionFlat(const FlatDirectionCosinesType &arg)
static void UnloadComponents(void)
virtual ObjectContainerPointer CreateComponents(const ComponentDescriptionType &key, const ComponentDescriptionType &defaultComponentName, int &errorcode, bool mandatoryComponent=true)
static ComponentDatabasePointer s_CDB
void SetElastixLevel(unsigned int level)
itk::ParameterMapInterface::ParameterMapType ParameterMapType
unsigned int GetTotalNumberOfElastixLevels(void)
unsigned int GetElastixLevel(void)
virtual ElastixBaseType * GetElastixBase(void) const
virtual void SetMaximumNumberOfThreads(void) const