]> Creatis software - cpPlugins.git/blobdiff - plugins/ITKImageGenericFilters_1/MinimumMaximumImageCalculator.cxx
yet another refactoring
[cpPlugins.git] / plugins / ITKImageGenericFilters_1 / MinimumMaximumImageCalculator.cxx
diff --git a/plugins/ITKImageGenericFilters_1/MinimumMaximumImageCalculator.cxx b/plugins/ITKImageGenericFilters_1/MinimumMaximumImageCalculator.cxx
new file mode 100644 (file)
index 0000000..c30cad3
--- /dev/null
@@ -0,0 +1,120 @@
+#include <ITKImageGenericFilters_1/MinimumMaximumImageCalculator.h>
+#include <cpInstances/DataObjects/Image.h>
+#include <cpInstances/DataObjects/Mesh.h>
+
+#include <itkImage.h>
+#include <itkMinimumMaximumImageCalculator.h>
+#include <vtkPolyData.h>
+
+// -------------------------------------------------------------------------
+cpPluginsITKImageGenericFilters_1::MinimumMaximumImageCalculator::
+MinimumMaximumImageCalculator( )
+  : Superclass( )
+{
+  typedef cpInstances::DataObjects::Image _TImage;
+  typedef cpInstances::DataObjects::Mesh _TMesh;
+
+  this->_ConfigureInput< _TImage >( "Input", true, false );
+  this->_ConfigureOutput< _TMesh >( "Maximum" );
+  this->_ConfigureOutput< _TMesh >( "Minimum" );
+}
+
+// -------------------------------------------------------------------------
+cpPluginsITKImageGenericFilters_1::MinimumMaximumImageCalculator::
+~MinimumMaximumImageCalculator( )
+{
+}
+
+// -------------------------------------------------------------------------
+void cpPluginsITKImageGenericFilters_1::MinimumMaximumImageCalculator::
+_GenerateData( )
+{
+  auto o = this->GetInputData( "Input" );
+  cpPlugins_Demangle_Image_ScalarPixels_AllDims_1( o, _GD0 )
+    this->_Error( "Invalid input image." );
+}
+
+// -------------------------------------------------------------------------
+template< class _TImage >
+void cpPluginsITKImageGenericFilters_1::MinimumMaximumImageCalculator::
+_GD0( _TImage* input )
+{
+  typedef itk::MinimumMaximumImageCalculator< _TImage > _TFilter;
+  typedef typename _TImage::IndexType _TIndex;
+  typedef typename _TImage::PointType _TPoint;
+
+  auto filter = this->_CreateITK< _TFilter >( );
+  filter->SetImage( input );
+  filter->Compute( );
+
+  _TIndex min_idx = filter->GetIndexOfMinimum( );
+  _TIndex max_idx = filter->GetIndexOfMaximum( );
+
+  _TPoint min_pnt, max_pnt;
+  input->TransformIndexToPhysicalPoint( min_idx, min_pnt );
+  input->TransformIndexToPhysicalPoint( max_idx, max_pnt );
+
+  auto min_pd = this->GetOutputData< vtkPolyData >( "Minimum" );
+  if( min_pd == NULL )
+  {
+    auto points = vtkSmartPointer< vtkPoints >::New( );
+    auto verts = vtkSmartPointer< vtkCellArray >::New( );
+    auto lines = vtkSmartPointer< vtkCellArray >::New( );
+    auto polys = vtkSmartPointer< vtkCellArray >::New( );
+    auto strips = vtkSmartPointer< vtkCellArray >::New( );
+    auto pd = vtkSmartPointer< vtkPolyData >::New( );
+    pd->SetPoints( points );
+    pd->SetVerts( verts );
+    pd->SetLines( lines );
+    pd->SetPolys( polys );
+    pd->SetStrips( strips );
+
+    points->InsertNextPoint( 0, 0, 0 );
+    verts->InsertNextCell( 1 );
+    verts->InsertCellPoint( 0 );
+
+    this->GetOutput( "Minimum" )->SetVTK( pd );
+    min_pd = this->GetOutputData< vtkPolyData >( "Minimum" );
+
+  } // fi
+
+  if( _TImage::ImageDimension == 1 )
+    min_pd->GetPoints( )->SetPoint( 0, min_pnt[ 0 ], 0, 0 );
+  else if( _TImage::ImageDimension == 2 )
+    min_pd->GetPoints( )->SetPoint( 0, min_pnt[ 0 ], min_pnt[ 1 ], 0 );
+  else if( _TImage::ImageDimension > 2 )
+    min_pd->GetPoints( )->SetPoint( 0, min_pnt[ 0 ], min_pnt[ 1 ], min_pnt[ 2 ] );
+
+  auto max_pd = this->GetOutputData< vtkPolyData >( "Maximum" );
+  if( max_pd == NULL )
+  {
+    auto points = vtkSmartPointer< vtkPoints >::New( );
+    auto verts = vtkSmartPointer< vtkCellArray >::New( );
+    auto lines = vtkSmartPointer< vtkCellArray >::New( );
+    auto polys = vtkSmartPointer< vtkCellArray >::New( );
+    auto strips = vtkSmartPointer< vtkCellArray >::New( );
+    auto pd = vtkSmartPointer< vtkPolyData >::New( );
+    pd->SetPoints( points );
+    pd->SetVerts( verts );
+    pd->SetLines( lines );
+    pd->SetPolys( polys );
+    pd->SetStrips( strips );
+
+    points->InsertNextPoint( 0, 0, 0 );
+    verts->InsertNextCell( 1 );
+    verts->InsertCellPoint( 0 );
+
+    this->GetOutput( "Maximum" )->SetVTK( pd );
+    max_pd = this->GetOutputData< vtkPolyData >( "Maximum" );
+
+  } // fi
+
+  if( _TImage::ImageDimension == 1 )
+    max_pd->GetPoints( )->SetPoint( 0, max_pnt[ 0 ], 0, 0 );
+  else if( _TImage::ImageDimension == 2 )
+    max_pd->GetPoints( )->SetPoint( 0, max_pnt[ 0 ], max_pnt[ 1 ], 0 );
+  else if( _TImage::ImageDimension > 2 )
+    max_pd->GetPoints( )->SetPoint( 0, max_pnt[ 0 ], max_pnt[ 1 ], max_pnt[ 2 ] );
+}
+
+// eof - $RCSfile$