diff --git a/Modules/AlgorithmsExt/Testing/files.cmake b/Modules/AlgorithmsExt/Testing/files.cmake index 9a9b325349..4423805431 100644 --- a/Modules/AlgorithmsExt/Testing/files.cmake +++ b/Modules/AlgorithmsExt/Testing/files.cmake @@ -1,9 +1,10 @@ set(MODULE_TESTS mitkAutoCropImageFilterTest.cpp mitkBoundingObjectCutterTest.cpp + mitkImageToUnstructuredGridFilterTest.cpp mitkSimpleHistogramTest.cpp ) set(MODULE_CUSTOM_TESTS mitkLabeledImageToSurfaceFilterTest.cpp ) diff --git a/Modules/AlgorithmsExt/Testing/mitkImageToUnstructuredGridFilterTest.cpp b/Modules/AlgorithmsExt/Testing/mitkImageToUnstructuredGridFilterTest.cpp new file mode 100644 index 0000000000..c0a5264e56 --- /dev/null +++ b/Modules/AlgorithmsExt/Testing/mitkImageToUnstructuredGridFilterTest.cpp @@ -0,0 +1,91 @@ +/*=================================================================== + +The Medical Imaging Interaction Toolkit (MITK) + +Copyright (c) German Cancer Research Center, +Division of Medical and Biological Informatics. +All rights reserved. + +This software is distributed WITHOUT ANY WARRANTY; without +even the implied warranty of MERCHANTABILITY or FITNESS FOR +A PARTICULAR PURPOSE. + +See LICENSE.txt or http://www.mitk.org for details. + +===================================================================*/ + +#include "mitkTestingMacros.h" +#include +#include + +#include + +class mitkImageToUnstructuredGridFilterTestSuite : public mitk::TestFixture +{ + CPPUNIT_TEST_SUITE(mitkImageToUnstructuredGridFilterTestSuite); + MITK_TEST(testImageToUnstructuredGridFilterInitialization); + MITK_TEST(testInput); + MITK_TEST(testUnstructuredGridGeneration); + MITK_TEST(testThreshold); + CPPUNIT_TEST_SUITE_END(); + +private: + + /** Members used inside the different test methods. All members are initialized via setUp().*/ + mitk::Image::Pointer m_BallImage; + +public: + + /** + * @brief Setup Always call this method before each Test-case to ensure correct and new intialization of the used members for a new test case. (If the members are not used in a test, the method does not need to be called). + */ + void setUp() + { + m_BallImage = mitk::IOUtil::LoadImage(GetTestDataFilePath("BallBinary30x30x30.nrrd")); + } + + void testImageToUnstructuredGridFilterInitialization() + { + mitk::ImageToUnstructuredGridFilter::Pointer testFilter = mitk::ImageToUnstructuredGridFilter::New(); + CPPUNIT_ASSERT_MESSAGE("Testing instantiation of test object", testFilter.IsNotNull()); + CPPUNIT_ASSERT_MESSAGE("Testing initialization of threshold member variable",testFilter->GetThreshold() == -0.1); + } + + void testInput() + { + mitk::ImageToUnstructuredGridFilter::Pointer testFilter = mitk::ImageToUnstructuredGridFilter::New(); + testFilter->SetInput(m_BallImage); + CPPUNIT_ASSERT_MESSAGE("Testing set / get input!", testFilter->GetInput() == m_BallImage); + } + + void testUnstructuredGridGeneration() + { + mitk::ImageToUnstructuredGridFilter::Pointer testFilter = mitk::ImageToUnstructuredGridFilter::New(); + testFilter->SetInput(m_BallImage); + testFilter->Update(); + CPPUNIT_ASSERT_MESSAGE("Testing surface generation!", testFilter->GetOutput() != NULL); + } + + void testThreshold() + { + mitk::ImageToUnstructuredGridFilter::Pointer testFilter1 = mitk::ImageToUnstructuredGridFilter::New(); + testFilter1->SetInput(m_BallImage); + testFilter1->Update(); + + int numberOfPoints1 = testFilter1->GetNumberOfExtractedPoints(); + + mitk::ImageToUnstructuredGridFilter::Pointer testFilter2 = mitk::ImageToUnstructuredGridFilter::New(); + testFilter2->SetInput(m_BallImage); + testFilter2->SetThreshold(1.0); + testFilter2->Update(); + + int numberOfPoints2 = testFilter2->GetNumberOfExtractedPoints(); + + std::cout << "1: " << numberOfPoints1 << " 2: " << numberOfPoints2 << std::endl; + + CPPUNIT_ASSERT_MESSAGE("Testing Threshold", numberOfPoints1 > numberOfPoints2); + } + +}; + +MITK_TEST_SUITE_REGISTRATION(mitkImageToUnstructuredGridFilter) diff --git a/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.cpp b/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.cpp index 7b22c824c0..70dd0d8c6f 100644 --- a/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.cpp +++ b/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.cpp @@ -1,120 +1,127 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include mitk::ImageToUnstructuredGridFilter::ImageToUnstructuredGridFilter(): m_NumberOfExtractedPoints(0), -m_Threshold(0.0) +m_Threshold(-0.1) { this->m_UnstructGrid = mitk::UnstructuredGrid::New(); } mitk::ImageToUnstructuredGridFilter::~ImageToUnstructuredGridFilter(){} void mitk::ImageToUnstructuredGridFilter::GenerateData() { mitk::UnstructuredGrid::Pointer unstructGrid = this->GetOutput(); const mitk::Image* image = this->GetInput(); if(image == NULL || !image->IsInitialized()) { MITK_ERROR << "Wrong input image set" << std::endl; return; } m_Geometry = image->GetGeometry(); + m_NumberOfExtractedPoints = 0; + AccessByItk(image, ExtractPoints) } void mitk::ImageToUnstructuredGridFilter::SetInput(const mitk::Image* image) { this->ProcessObject::SetNthInput(0, const_cast< mitk::Image* >( image ) ); } const mitk::Image* mitk::ImageToUnstructuredGridFilter::GetInput(void) { if (this->GetNumberOfInputs() < 1) { MITK_ERROR << "No input set" << std::endl; return 0; } return static_cast( this->ProcessObject::GetInput(0) ); } template void mitk::ImageToUnstructuredGridFilter:: ExtractPoints(const itk::Image* image) { typedef itk::Image InputImageType; typename itk::ImageRegionConstIterator it(image, image->GetRequestedRegion()); vtkSmartPointer points = vtkSmartPointer::New(); it.GoToBegin(); while( !it.IsAtEnd() ) { if(it.Get() > m_Threshold) { mitk::Point3D imagePoint; mitk::Point3D worldPoint; imagePoint[0] = it.GetIndex()[0]; imagePoint[1] = it.GetIndex()[1]; imagePoint[2] = it.GetIndex()[2]; m_Geometry->IndexToWorld(imagePoint, worldPoint); points->InsertNextPoint(worldPoint[0],worldPoint[1],worldPoint[2]); m_NumberOfExtractedPoints++; } ++it; } vtkSmartPointer vtkUnstructGrid = vtkSmartPointer::New(); vtkUnstructGrid->SetPoints(points); m_UnstructGrid->SetVtkUnstructuredGrid(vtkUnstructGrid); } -void mitk::ImageToUnstructuredGridFilter::SetThreshold(int threshold) +void mitk::ImageToUnstructuredGridFilter::SetThreshold(double threshold) { this->m_Threshold = threshold; } +double mitk::ImageToUnstructuredGridFilter::GetThreshold() +{ + return this->m_Threshold; +} + void mitk::ImageToUnstructuredGridFilter::GenerateOutputInformation() { mitk::Image::ConstPointer inputImage = this->GetInput(); m_UnstructGrid = this->GetOutput(); itkDebugMacro(<<"GenerateOutputInformation()"); if(inputImage.IsNull()) return; } diff --git a/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.h b/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.h index 637fabba3e..0d1ef51316 100644 --- a/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.h +++ b/Modules/AlgorithmsExt/mitkImageToUnstructuredGridFilter.h @@ -1,105 +1,107 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef _MITKIMAGETOUNSTRUCTUREDGRIDFILTER_h__ #define _MITKIMAGETOUNSTRUCTUREDGRIDFILTER_h__ #include #include #include #include #include namespace mitk { /** * @brief Converts an Image into an UnstructuredGrid represented by Points. * The filter uses a Threshold to extract every pixel, with value higher than * the threshold, as point. * If no threshold is set, every pixel is extracted as a point. */ class MitkAlgorithmsExt_EXPORT ImageToUnstructuredGridFilter : public UnstructuredGridSource { public: mitkClassMacro(ImageToUnstructuredGridFilter, UnstructuredGridSource) itkFactorylessNewMacro(Self) itkCloneMacro(Self) /** This method is called by Update(). */ virtual void GenerateData(); /** Initializes the output information */ virtual void GenerateOutputInformation(); /** Returns a const reference to the input image */ const mitk::Image* GetInput(void); /** Set the source image. As input every mitk 3D image can be used. */ using itk::ProcessObject::SetInput; virtual void SetInput(const mitk::Image *image); /** * Set the threshold for extracting points. Every pixel, which value * is higher than this value, will be a point. */ - void SetThreshold(int threshold); + void SetThreshold(double threshold); + + /** Returns the threshold */ + double GetThreshold(); - itkGetMacro(Threshold, double) itkGetMacro(NumberOfExtractedPoints, int) protected: /** Constructor */ ImageToUnstructuredGridFilter(); /** Destructor */ virtual ~ImageToUnstructuredGridFilter(); /** * Access method for extracting the points from the input image */ template void ExtractPoints(const itk::Image* image); private: /** * Geometry of the input image, needed to tranform the image points * into world points */ mitk::BaseGeometry* m_Geometry; /** The number of points extracted by the filter */ int m_NumberOfExtractedPoints; /** Threshold for extracting the points */ double m_Threshold; /** The output of the filter, which contains the extracted points */ mitk::UnstructuredGrid::Pointer m_UnstructGrid; }; } // namespace mitk #endif //_MITKIMAGETOUNSTRUCTUREDGRIDFILTER_h__