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