]> Creatis software - cpPlugins.git/blob - plugins/ImageGenericFilters/MinimumMaximumCalculator.cxx
822f61fb3ef02ea883f64f44f18a4f3aca513708
[cpPlugins.git] / plugins / ImageGenericFilters / MinimumMaximumCalculator.cxx
1 #include <ImageGenericFilters/MinimumMaximumCalculator.h>
2 #include <cpPlugins/DataObjects/Image.h>
3 #include <cpPlugins/DataObjects/Mesh.h>
4
5 #include <itkMinimumMaximumImageCalculator.h>
6
7 // -------------------------------------------------------------------------
8 cpPluginsImageGenericFilters::MinimumMaximumCalculator::
9 MinimumMaximumCalculator( )
10   : Superclass( )
11 {
12   this->_ConfigureInput< cpPlugins::DataObjects::Image >( "Input", true, false );
13   this->_ConfigureOutput< cpPlugins::DataObjects::Mesh >( "Maximum" );
14   this->_ConfigureOutput< cpPlugins::DataObjects::Mesh >( "Minimum" );
15 }
16
17 // -------------------------------------------------------------------------
18 cpPluginsImageGenericFilters::MinimumMaximumCalculator::
19 ~MinimumMaximumCalculator( )
20 {
21 }
22
23 // -------------------------------------------------------------------------
24 void cpPluginsImageGenericFilters::MinimumMaximumCalculator::
25 _GenerateData( )
26 {
27   auto o = this->GetInputData( "Input" );
28   cpPlugins_Demangle_ImageScalars_Dims( o, _GD0 );
29   else this->_Error( "Invalid input image." );
30 }
31
32 // -------------------------------------------------------------------------
33 template< class _TImage >
34 void cpPluginsImageGenericFilters::MinimumMaximumCalculator::
35 _GD0( _TImage* input )
36 {
37   typedef itk::MinimumMaximumImageCalculator< _TImage > _TFilter;
38   typedef typename _TImage::IndexType _TIndex;
39   typedef typename _TImage::PointType _TPoint;
40
41   auto filter = this->_CreateITK< _TFilter >( );
42   filter->SetImage( input );
43   filter->Compute( );
44
45   _TIndex min_idx = filter->GetIndexOfMinimum( );
46   _TIndex max_idx = filter->GetIndexOfMaximum( );
47
48   _TPoint min_pnt, max_pnt;
49   input->TransformIndexToPhysicalPoint( min_idx, min_pnt );
50   input->TransformIndexToPhysicalPoint( max_idx, max_pnt );
51
52   auto min_pd = this->GetOutputData< vtkPolyData >( "Minimum" );
53   if( min_pd == NULL )
54   {
55     auto points = vtkSmartPointer< vtkPoints >::New( );
56     auto verts = vtkSmartPointer< vtkCellArray >::New( );
57     auto lines = vtkSmartPointer< vtkCellArray >::New( );
58     auto polys = vtkSmartPointer< vtkCellArray >::New( );
59     auto strips = vtkSmartPointer< vtkCellArray >::New( );
60     auto pd = vtkSmartPointer< vtkPolyData >::New( );
61     pd->SetPoints( points );
62     pd->SetVerts( verts );
63     pd->SetLines( lines );
64     pd->SetPolys( polys );
65     pd->SetStrips( strips );
66
67     points->InsertNextPoint( 0, 0, 0 );
68     verts->InsertNextCell( 1 );
69     verts->InsertCellPoint( 0 );
70
71     this->GetOutput( "Minimum" )->SetVTK( pd );
72     min_pd = this->GetOutputData< vtkPolyData >( "Minimum" );
73
74   } // fi
75
76   if( _TImage::ImageDimension == 1 )
77     min_pd->GetPoints( )->SetPoint( 0, min_pnt[ 0 ], 0, 0 );
78   else if( _TImage::ImageDimension == 2 )
79     min_pd->GetPoints( )->SetPoint( 0, min_pnt[ 0 ], min_pnt[ 1 ], 0 );
80   else if( _TImage::ImageDimension > 2 )
81     min_pd->GetPoints( )->SetPoint( 0, min_pnt[ 0 ], min_pnt[ 1 ], min_pnt[ 2 ] );
82
83   auto max_pd = this->GetOutputData< vtkPolyData >( "Maximum" );
84   if( max_pd == NULL )
85   {
86     auto points = vtkSmartPointer< vtkPoints >::New( );
87     auto verts = vtkSmartPointer< vtkCellArray >::New( );
88     auto lines = vtkSmartPointer< vtkCellArray >::New( );
89     auto polys = vtkSmartPointer< vtkCellArray >::New( );
90     auto strips = vtkSmartPointer< vtkCellArray >::New( );
91     auto pd = vtkSmartPointer< vtkPolyData >::New( );
92     pd->SetPoints( points );
93     pd->SetVerts( verts );
94     pd->SetLines( lines );
95     pd->SetPolys( polys );
96     pd->SetStrips( strips );
97
98     points->InsertNextPoint( 0, 0, 0 );
99     verts->InsertNextCell( 1 );
100     verts->InsertCellPoint( 0 );
101
102     this->GetOutput( "Maximum" )->SetVTK( pd );
103     max_pd = this->GetOutputData< vtkPolyData >( "Maximum" );
104
105   } // fi
106
107   if( _TImage::ImageDimension == 1 )
108     max_pd->GetPoints( )->SetPoint( 0, max_pnt[ 0 ], 0, 0 );
109   else if( _TImage::ImageDimension == 2 )
110     max_pd->GetPoints( )->SetPoint( 0, max_pnt[ 0 ], max_pnt[ 1 ], 0 );
111   else if( _TImage::ImageDimension > 2 )
112     max_pd->GetPoints( )->SetPoint( 0, max_pnt[ 0 ], max_pnt[ 1 ], max_pnt[ 2 ] );
113 }
114
115 // eof - $RCSfile$