]> Creatis software - cpPlugins.git/blob - plugins/ImageSources/RandomImageSource.cxx
b0ef6c4b8f1fdc378d9cf8db2abe3dbbfb3589d0
[cpPlugins.git] / plugins / ImageSources / RandomImageSource.cxx
1 #include <plugins/ImageSources/RandomImageSource.h>
2 #include <cpPlugins/DataObjects/Image.h>
3
4 #include <itkRandomImageSource.h>
5 #include <itkRandomImageSource.hxx>
6
7 // -------------------------------------------------------------------------
8 cpPluginsImageSources::RandomImageSource::
9 RandomImageSource( )
10   : Superclass( )
11 {
12   this->_ConfigureOutput< cpPlugins::DataObjects::Image >( "Output" );
13
14   this->m_Parameters.ConfigureAsUintList( "Size" );
15   this->m_Parameters.ConfigureAsRealList( "Spacing" );
16
17   std::vector< std::string > pixels;
18   pixels.push_back( "char" );
19   pixels.push_back( "short" );
20   pixels.push_back( "int" );
21   pixels.push_back( "long" );
22   pixels.push_back( "uchar" );
23   pixels.push_back( "ushort" );
24   pixels.push_back( "uint" );
25   pixels.push_back( "ulong" );
26   pixels.push_back( "float" );
27   pixels.push_back( "double" );
28   this->m_Parameters.ConfigureAsChoices( "PixelType", pixels );
29   this->m_Parameters.SetSelectedChoice( "PixelType", "uchar" );
30
31   std::vector< std::string > dims;
32   dims.push_back( "1" );
33   dims.push_back( "2" );
34   dims.push_back( "3" );
35   dims.push_back( "4" );
36   this->m_Parameters.ConfigureAsChoices( "Dimension", dims );
37   this->m_Parameters.SetSelectedChoice( "Dimension", "2" );
38 }
39
40 // -------------------------------------------------------------------------
41 cpPluginsImageSources::RandomImageSource::
42 ~RandomImageSource( )
43 {
44 }
45
46 // -------------------------------------------------------------------------
47 void cpPluginsImageSources::RandomImageSource::
48 _GenerateData( )
49 {
50   auto pixel = this->m_Parameters.GetSelectedChoice( "PixelType" );
51   if     ( pixel == "char"   ) this->_GD0< char >( );
52   else if( pixel == "short"  ) this->_GD0< short >( );
53   else if( pixel == "int"    ) this->_GD0< int >( );
54   else if( pixel == "long"   ) this->_GD0< long >( );
55   else if( pixel == "uchar"  ) this->_GD0< unsigned char >( );
56   else if( pixel == "ushort" ) this->_GD0< unsigned short >( );
57   else if( pixel == "uint"   ) this->_GD0< unsigned int >( );
58   else if( pixel == "ulong"  ) this->_GD0< unsigned long >( );
59   else if( pixel == "float"  ) this->_GD0< float >( );
60   else if( pixel == "double" ) this->_GD0< double >( );
61   else this->_Error( "Invalid pixel type." );
62 }
63
64 // -------------------------------------------------------------------------
65 template< class _TPixel >
66 void cpPluginsImageSources::RandomImageSource::
67 _GD0( )
68 {
69   auto dim = this->m_Parameters.GetSelectedChoice( "Dimension" );
70   if     ( dim == "1" ) this->_GD1< _TPixel, 1 >( );
71   else if( dim == "2" ) this->_GD1< _TPixel, 2 >( );
72   else if( dim == "3" ) this->_GD1< _TPixel, 3 >( );
73   else if( dim == "4" ) this->_GD1< _TPixel, 4 >( );
74   else this->_Error( "Invalid dimension." );
75 }
76
77 // -------------------------------------------------------------------------
78 template< class _TPixel, unsigned int _VDim >
79 void cpPluginsImageSources::RandomImageSource::
80 _GD1( )
81 {
82   typedef itk::Image< _TPixel, _VDim >      _TImage;
83   typedef itk::RandomImageSource< _TImage > _TFilter;
84
85   auto size = this->m_Parameters.GetUintList( "Size" );
86   auto spacing = this->m_Parameters.GetRealList( "Spacing" );
87   if( size.size( ) < _VDim )
88     this->_Error( "Invalid size." );
89   if( spacing.size( ) < _VDim )
90     this->_Error( "Invalid spacing." );
91
92   typename _TImage::SizeType out_size;
93   typename _TImage::SpacingType out_spac;
94   for( unsigned int i = 0; i < _VDim; ++i )
95   {
96     out_size[ i ] = size[ i ];
97     out_spac[ i ] = spacing[ i ];
98
99   } // rof
100
101   _TFilter* filter = this->_CreateITK< _TFilter >( );
102   filter->SetSize( out_size );
103   filter->SetSpacing( out_spac );
104   filter->Update( );
105   this->GetOutput( "Output" )->SetITK( filter->GetOutput( ) );
106 }
107
108 // eof - $RCSfile$