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