From e6dcc71c8c8ebc767bf4ea7105e572e5e7d24546 Mon Sep 17 00:00:00 2001
From: Kasper Marstal <kaspermarstal@gmail.com>
Date: Fri, 11 Dec 2015 17:34:44 +0100
Subject: [PATCH] WIP: CorrespondingPointsEuclideanDistanceMetric ElastixFilter
 test

---
 .../Elastix/include/elxElastixFilter.h        |  16 +--
 .../Elastix/include/elxElastixFilter.hxx      |  15 +--
 Testing/Unit/elxElastixFilterTest.cxx         | 119 ++++++++++++------
 3 files changed, 99 insertions(+), 51 deletions(-)

diff --git a/Modules/Components/Elastix/include/elxElastixFilter.h b/Modules/Components/Elastix/include/elxElastixFilter.h
index 3fe121b9..5da1b96d 100644
--- a/Modules/Components/Elastix/include/elxElastixFilter.h
+++ b/Modules/Components/Elastix/include/elxElastixFilter.h
@@ -52,13 +52,13 @@ public:
   void SetParameterObject( ParameterObjectPointer parameterObject );
   ParameterObjectConstPointer GetTransformParameters( void ) const;
 
-  itkSetMacro( FixedPointSetFileName, std::string );
-  itkGetConstMacro( FixedPointSetFileName, std::string );
-  void DeleteFixedPointSet( void ) { this->SetFixedPointSetFileName( std::string() ); };
+  itkSetMacro( FixedMeshFileName, std::string );
+  itkGetConstMacro( FixedMeshFileName, std::string );
+  void DeleteFixedMesh( void ) { this->SetFixedMeshFileName( std::string() ); };
 
-  itkSetMacro( MovingPointSetFileName, std::string );
-  itkGetConstMacro( MovingPointSetFileName, std::string );
-  void DeleteMovingPointSet( void ) { this->SetMovingPointSetFileName( std::string() ); };
+  itkSetMacro( MovingMeshFileName, std::string );
+  itkGetConstMacro( MovingMeshFileName, std::string );
+  void DeleteMovingMesh( void ) { this->SetMovingMeshFileName( std::string() ); };
 
   itkSetMacro( LogToConsole, bool );
   itkGetConstMacro( LogToConsole, bool );
@@ -82,8 +82,8 @@ private:
   DataObjectContainerPointer m_FixedMaskContainer;
   DataObjectContainerPointer m_MovingMaskContainer;
 
-  std::string m_FixedPointSetFileName;
-  std::string m_MovingPointSetFileName;
+  std::string m_FixedMeshFileName;
+  std::string m_MovingMeshFileName;
 
   bool m_LogToConsole;
   std::string m_LogToFile;
diff --git a/Modules/Components/Elastix/include/elxElastixFilter.hxx b/Modules/Components/Elastix/include/elxElastixFilter.hxx
index b2d92006..a9614a8b 100644
--- a/Modules/Components/Elastix/include/elxElastixFilter.hxx
+++ b/Modules/Components/Elastix/include/elxElastixFilter.hxx
@@ -17,8 +17,8 @@ ElastixFilter< TFixedImage, TMovingImage >
   this->m_FixedImageContainer = DataObjectContainerType::New();
   this->m_MovingImageContainer = DataObjectContainerType::New();
 
-  this->m_FixedPointSetFileName = std::string();
-  this->m_MovingPointSetFileName = std::string();
+  this->m_FixedMeshFileName = std::string();
+  this->m_MovingMeshFileName = std::string();
 }
 
 template< typename TFixedImage, typename TMovingImage >
@@ -58,15 +58,16 @@ ElastixFilter< TFixedImage, TMovingImage >
   ArgumentMapType argumentMap;
   argumentMap.insert( ArgumentMapEntryType( "-out", std::string( "output_path_not_set" ) ) );
 
-  // Set point sets
-  if( !this->m_FixedPointSetFileName.empty() )
+  // Fixed mesh (optional)
+  if( !this->m_FixedMeshFileName.empty() )
   {
-    argumentMap.insert( ArgumentMapEntryType( "-fp", std::string( this->m_FixedPointSetFileName ) ) );
+    argumentMap.insert( ArgumentMapEntryType( "-fp", std::string( this->m_FixedMeshFileName ) ) );
   }
 
-  if( !this->m_MovingPointSetFileName.empty() )
+  // Moving mesh (optional)
+  if( !this->m_MovingMeshFileName.empty() )
   {
-    argumentMap.insert( ArgumentMapEntryType( "-mp", std::string( this->m_MovingPointSetFileName ) ) );
+    argumentMap.insert( ArgumentMapEntryType( "-mp", std::string( this->m_MovingMeshFileName ) ) );
   }
 
   // Setup xout
diff --git a/Testing/Unit/elxElastixFilterTest.cxx b/Testing/Unit/elxElastixFilterTest.cxx
index a94d63c3..c0d71833 100644
--- a/Testing/Unit/elxElastixFilterTest.cxx
+++ b/Testing/Unit/elxElastixFilterTest.cxx
@@ -1,6 +1,8 @@
 #include "elxElastixFilter.h"
 #include "elxParameterObject.h"
 
+#include "itkMesh.h"
+#include "itkMeshFileReader.h"
 #include "itkImageFileReader.h"
 #include "itkImageFileWriter.h"
 
@@ -15,43 +17,54 @@ protected:
 
   typedef DataManager DataManagerType;
 
-  typedef itk::Image< float, 2u >                         ImageType;
+  // Mesh typedef 
+  typedef itk::Mesh< float, 2u >                            MeshType;
+  typedef itk::MeshFileReader< MeshType >                   MeshReaderType;
+
+  // Image typedefs
+  typedef itk::Image< float, 2u >                           ImageType;
+  typedef itk::ImageFileReader< ImageType >                 ImageReaderType;
+  typedef itk::ImageFileWriter< ImageType >                 ImageWriterType;
+
+  // Parameter typedefs
+  typedef ParameterObject::ParameterValuesType              ParameterValuesType;
+  typedef ParameterObject::ParameterMapType                 ParameterMapType;
+
+  // Elastix typedefs
+  typedef ElastixFilter< ImageType, ImageType >             ElastixFilterType;
+  typedef ElastixFilterType::DataObjectContainerType        DataObjectContainerType;
+  typedef ElastixFilterType::DataObjectContainerPointer     DataObjectContainerPointer;
+
+  // Variables used by multiple tests
   ImageType::Pointer fixedImage;
   ImageType::Pointer movingImage;
   ImageType::Pointer resultImage;
 
-  typedef itk::ImageFileReader< ImageType >               ImageReaderType;
-  typedef itk::ImageFileWriter< ImageType >               ImageWriterType;
+  std::string fixedMeshFileName;
+  std::string movingMeshFileName;
 
-  typedef ParameterObject::ParameterValuesType            ParameterValuesType;
-  typedef ParameterObject::ParameterMapType               ParameterMapType;
   ParameterObject::Pointer parameterObject;
-  ParameterObject::ConstPointer TransformParameterObject;
-
-  typedef ElastixFilter< ImageType, ImageType >           ElastixFilterType;
-  typedef ElastixFilterType::DataObjectContainerType      DataObjectContainerType;
-  typedef ElastixFilterType::DataObjectContainerPointer   DataObjectContainerPointer;
-
+  ParameterObject::Pointer correspondingPointsParameterObject;
+  ParameterObject::ConstPointer transformParameterObject;
 
   virtual void SetUp()
   {
     DataManagerType::Pointer dataManager = DataManagerType::New();
 
-    ImageReaderType::Pointer reader = ImageReaderType::New();
-
-    reader->SetFileName( dataManager->GetInputFile( "cthead1-Float.mha" ) );
-    fixedImage = reader->GetOutput();
+    // TODO: All calls to update here should not be needed but should be propagated to reader by ElastixFilter
 
-    // TODO: Only call update on writer
-    reader->Update();
+    // Input images
+    ImageReaderType::Pointer fixedImageReader = ImageReaderType::New();
+    fixedImageReader->SetFileName( dataManager->GetInputFile( "cthead1-Float.mha" ) );
+    fixedImage = fixedImageReader->GetOutput();
+    fixedImageReader->Update();
 
-    reader->SetFileName( dataManager->GetInputFile( "cthead1-Float.mha" ) );
-    movingImage = reader->GetOutput();
+    ImageReaderType::Pointer movingImageReader = ImageReaderType::New();
+    movingImageReader->SetFileName( dataManager->GetInputFile( "cthead1-Float.mha" ) );
+    movingImage = movingImageReader->GetOutput();
+    movingImageReader->Update();
 
-    // TODO: Only call update on writer
-    reader->Update();
-
-    // ParameterMap
+    // Nonrigid ParameterMap
     ParameterMapType parameterMap                       = ParameterMapType();
 
     // Images
@@ -93,8 +106,18 @@ protected:
     parameterMap[ "Metric" ]                            = ParameterValuesType( 1, "AdvancedMattesMutualInformation" );
     parameterMap[ "MaximumNumberOfIterations" ]         = ParameterValuesType( 1, "128" );
 
+    ParameterMapType nonRigidParameterMap = parameterMap;
     parameterObject = ParameterObject::New();
-    parameterObject->SetParameterMap( parameterMap );
+    parameterObject->SetParameterMap( nonRigidParameterMap );
+
+    ParameterMapType correspondingPointsParameterMap = parameterMap;
+
+    // TODO: WHY DOES THIS SEGFAULT?
+    //correspondingPointsParameterMap[ "Metric" ][ 1 ] = "CorrespondingPointsEuclideanDistanceMetric";
+    //correspondingPointsParameterObject->SetParameterMap( correspondingPointsParameterMap );
+
+    fixedMeshFileName = dataManager->GetInputFile( "bioid_0000.vtk" );
+    movingMeshFileName = dataManager->GetInputFile( "bioid_0001.vtk" );
   }
 };
 
@@ -107,16 +130,16 @@ TEST_F( ElastixFilterTest, PairwiseRegistration )
 {
   ElastixFilterType::Pointer elastixFilter = ElastixFilterType::New();
 
-  elastixFilter->LogToConsoleOn();
-  elastixFilter->SetFixedImage( fixedImage );
-  elastixFilter->SetMovingImage( movingImage );
-  elastixFilter->SetParameterObject( parameterObject );
+  EXPECT_NO_THROW( elastixFilter->LogToConsoleOn() );
+  EXPECT_NO_THROW( elastixFilter->SetFixedImage( fixedImage ) );
+  EXPECT_NO_THROW( elastixFilter->SetMovingImage( movingImage ) );
+  EXPECT_NO_THROW( elastixFilter->SetParameterObject( parameterObject ) );
 
   // TODO: This update should not be needed
-  elastixFilter->Update();
+  EXPECT_NO_THROW( elastixFilter->Update() );
 
   EXPECT_NO_THROW( resultImage = elastixFilter->GetOutput() );
-  EXPECT_NO_THROW( TransformParameterObject = elastixFilter->GetTransformParameters() );
+  EXPECT_NO_THROW( transformParameterObject = elastixFilter->GetTransformParameters() );
 
   ImageWriterType::Pointer writer = ImageWriterType::New();
   writer->SetFileName( "ElastixResultImage.nii" );
@@ -138,21 +161,45 @@ TEST_F( ElastixFilterTest, MultiPairwiseRegistration )
   movingImages->CreateElementAt( 0 ) = static_cast< itk::DataObject* >( movingImage );
   movingImages->CreateElementAt( 1 ) = static_cast< itk::DataObject* >( movingImage );
 
-  elastixFilter->LogToConsoleOn();
-  elastixFilter->SetFixedImage( fixedImages );
-  elastixFilter->SetMovingImage( movingImages );
-  elastixFilter->SetParameterObject( parameterObject );
+  EXPECT_NO_THROW( elastixFilter->LogToConsoleOn() );
+  EXPECT_NO_THROW( elastixFilter->SetFixedImage( fixedImages ) );
+  EXPECT_NO_THROW( elastixFilter->SetMovingImage( movingImages ) );
+  EXPECT_NO_THROW( elastixFilter->SetParameterObject( parameterObject ) );
+
+  // TODO: This update should not be needed
+  EXPECT_NO_THROW( elastixFilter->Update() );
+
+  EXPECT_NO_THROW( resultImage = elastixFilter->GetOutput() );
+  EXPECT_NO_THROW( transformParameterObject = elastixFilter->GetTransformParameters() );
+
+  ImageWriterType::Pointer writer = ImageWriterType::New();
+  writer->SetFileName( "ElastixResultImage.nii" );
+  writer->SetInput( elastixFilter->GetOutput() );
+  EXPECT_NO_THROW( writer->Update() );
+}
+
+TEST_F( ElastixFilterTest, PointSetRegistration )
+{
+  ElastixFilterType::Pointer elastixFilter = ElastixFilterType::New();
+
+  EXPECT_NO_THROW( elastixFilter->LogToConsoleOn() );
+  EXPECT_NO_THROW( elastixFilter->SetFixedImage( fixedImage ) );
+  EXPECT_NO_THROW( elastixFilter->SetFixedMeshFileName( fixedMeshFileName  ) );
+  EXPECT_NO_THROW( elastixFilter->SetMovingImage( movingImage ) );
+  EXPECT_NO_THROW( elastixFilter->SetMovingMeshFileName( movingMeshFileName ) );
+  EXPECT_NO_THROW( elastixFilter->SetParameterObject( parameterObject ) );
 
   // TODO: This update should not be needed
-  elastixFilter->Update();
+  EXPECT_NO_THROW( elastixFilter->Update() );
 
   EXPECT_NO_THROW( resultImage = elastixFilter->GetOutput() );
-  EXPECT_NO_THROW( TransformParameterObject = elastixFilter->GetTransformParameters() );
+  EXPECT_NO_THROW( transformParameterObject = elastixFilter->GetTransformParameters() );
 
   ImageWriterType::Pointer writer = ImageWriterType::New();
   writer->SetFileName( "ElastixResultImage.nii" );
   writer->SetInput( elastixFilter->GetOutput() );
   EXPECT_NO_THROW( writer->Update() );
+
 }
 
 // TODO: Write test with point sets
-- 
GitLab