diff --git a/Modules/AlgorithmsExt/Testing/files.cmake b/Modules/AlgorithmsExt/Testing/files.cmake index ab9cbcc095..fc97d9b4a9 100644 --- a/Modules/AlgorithmsExt/Testing/files.cmake +++ b/Modules/AlgorithmsExt/Testing/files.cmake @@ -1,12 +1,13 @@ set(MODULE_TESTS mitkAutoCropImageFilterTest.cpp mitkBoundingObjectCutterTest.cpp mitkImageToUnstructuredGridFilterTest.cpp mitkSimpleHistogramTest.cpp mitkCovarianceMatrixCalculatorTest.cpp mitkAnisotropicIterativeClosestPointRegistrationTest.cpp + mitkUnstructuredGridToUnstructuredGridFilterTest.cpp ) set(MODULE_CUSTOM_TESTS mitkLabeledImageToSurfaceFilterTest.cpp ) diff --git a/Modules/AlgorithmsExt/Testing/mitkUnstructuredGridToUnstructuredGridFilterTest.cpp b/Modules/AlgorithmsExt/Testing/mitkUnstructuredGridToUnstructuredGridFilterTest.cpp new file mode 100644 index 0000000000..4fb78dfea7 --- /dev/null +++ b/Modules/AlgorithmsExt/Testing/mitkUnstructuredGridToUnstructuredGridFilterTest.cpp @@ -0,0 +1,108 @@ +/*=================================================================== + +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 + +#include + +#include +#include +#include + +class mitkUnstructuredGridToUnstructuredGridFilterTestSuite : public mitk::TestFixture +{ + CPPUNIT_TEST_SUITE(mitkUnstructuredGridToUnstructuredGridFilterTestSuite); + MITK_TEST(testUnstructuredGridToUnstructuredGridFilterInitialization); + MITK_TEST(testInput); + MITK_TEST(testMultipleInputs); + MITK_TEST(testUnstructuredGridGeneration); + CPPUNIT_TEST_SUITE_END(); + +private: + + mitk::UnstructuredGrid::Pointer m_UnstructuredGrid; + mitk::UnstructuredGrid::Pointer m_2ndUnstructuredGrid; + +public: + + void setUp() + { + m_UnstructuredGrid = mitk::UnstructuredGrid::New(); + m_2ndUnstructuredGrid = mitk::UnstructuredGrid::New(); + vtkSmartPointer vtkGrid = vtkSmartPointer::New(); + vtkSmartPointer vtkGrid2 = vtkSmartPointer::New(); + vtkSmartPointer points = vtkSmartPointer::New(); + vtkSmartPointer points2 = vtkSmartPointer::New(); + + for(int i=0; i<3; i++) + { + for(int j=0; j<3; j++) + { + for(int k=0; k<3; k++) + { + mitk::Point3D point; + point[0]=i; + point[1]=j; + point[2]=k; + + points->InsertNextPoint(point[0],point[1],point[2]); + points2->InsertNextPoint(point[0]+5,point[1]+5,point[2]+5); + } + } + } + + vtkGrid->SetPoints(points); + vtkGrid2->SetPoints(points2); + m_UnstructuredGrid->SetVtkUnstructuredGrid(vtkGrid); + m_2ndUnstructuredGrid->SetVtkUnstructuredGrid(vtkGrid2); + } + + void testUnstructuredGridToUnstructuredGridFilterInitialization() + { + mitk::UnstructuredGridToUnstructuredGridFilter::Pointer testFilter = mitk::UnstructuredGridToUnstructuredGridFilter::New(); + CPPUNIT_ASSERT_MESSAGE("Testing instantiation of test object", testFilter.IsNotNull()); + } + + void testInput() + { + mitk::UnstructuredGridToUnstructuredGridFilter::Pointer testFilter = mitk::UnstructuredGridToUnstructuredGridFilter::New(); + testFilter->SetInput(m_UnstructuredGrid); + CPPUNIT_ASSERT_MESSAGE("Testing set / get input!", testFilter->GetInput() == m_UnstructuredGrid); + } + + void testMultipleInputs() + { + mitk::UnstructuredGridToUnstructuredGridFilter::Pointer testFilter = mitk::UnstructuredGridToUnstructuredGridFilter::New(); + testFilter->SetInput(0, m_UnstructuredGrid); + testFilter->SetInput(1, m_2ndUnstructuredGrid); + CPPUNIT_ASSERT_MESSAGE("Testing first input!", testFilter->GetInput(0) == m_UnstructuredGrid); + CPPUNIT_ASSERT_MESSAGE("Testing second input!", testFilter->GetInput(1) == m_2ndUnstructuredGrid); + } + + void testUnstructuredGridGeneration() + { + mitk::UnstructuredGridToUnstructuredGridFilter::Pointer testFilter = mitk::UnstructuredGridToUnstructuredGridFilter::New(); + testFilter->SetInput(m_UnstructuredGrid); + testFilter->Update(); + CPPUNIT_ASSERT_MESSAGE("Testing unstructured grid generation!", testFilter->GetOutput() != NULL); + } + +}; + +MITK_TEST_SUITE_REGISTRATION(mitkUnstructuredGridToUnstructuredGridFilter) diff --git a/Modules/AlgorithmsExt/files.cmake b/Modules/AlgorithmsExt/files.cmake index e300f6176f..7df78a43aa 100644 --- a/Modules/AlgorithmsExt/files.cmake +++ b/Modules/AlgorithmsExt/files.cmake @@ -1,28 +1,29 @@ set(CPP_FILES mitkAutoCropImageFilter.cpp mitkBoundingObjectCutter.cpp mitkBoundingObjectToSegmentationFilter.cpp mitkGeometryClipImageFilter.cpp mitkHeightFieldSurfaceClipImageFilter.cpp mitkImageToUnstructuredGridFilter.cpp mitkLabeledImageToSurfaceFilter.cpp mitkMaskAndCutRoiImageFilter.cpp mitkMaskImageFilter.cpp mitkMovieGenerator.cpp mitkNonBlockingAlgorithm.cpp mitkPadImageFilter.cpp mitkPlaneLandmarkProjector.cpp mitkPointLocator.cpp mitkSimpleHistogram.cpp mitkSimpleUnstructuredGridHistogram.cpp mitkCovarianceMatrixCalculator.cpp mitkAnisotropicIterativeClosestPointRegistration.cpp mitkWeightedPointTransform.cpp mitkAnisotropicRegistrationCommon.cpp + mitkUnstructuredGridToUnstructuredGridFilter.cpp ) if(WIN32 AND NOT MINGW) list(APPEND CPP_FILES mitkMovieGeneratorWin32.cpp ) endif() diff --git a/Modules/AlgorithmsExt/mitkUnstructuredGridToUnstructuredGridFilter.cpp b/Modules/AlgorithmsExt/mitkUnstructuredGridToUnstructuredGridFilter.cpp new file mode 100644 index 0000000000..d842c90524 --- /dev/null +++ b/Modules/AlgorithmsExt/mitkUnstructuredGridToUnstructuredGridFilter.cpp @@ -0,0 +1,90 @@ +/*=================================================================== + +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::UnstructuredGridToUnstructuredGridFilter::UnstructuredGridToUnstructuredGridFilter() +{ + this->m_UnstructGrid = mitk::UnstructuredGrid::New(); +} + +mitk::UnstructuredGridToUnstructuredGridFilter::~UnstructuredGridToUnstructuredGridFilter(){} + +void mitk::UnstructuredGridToUnstructuredGridFilter::SetInput(const mitk::UnstructuredGrid* grid) +{ + this->ProcessObject::SetNthInput(0, const_cast< mitk::UnstructuredGrid* >( grid ) ); +} + +void mitk::UnstructuredGridToUnstructuredGridFilter::CreateOutputsForAllInputs(unsigned int /*idx*/) +{ + this->SetNumberOfIndexedOutputs( this->GetNumberOfIndexedInputs() ); + for (unsigned int idx = 0; idx < this->GetNumberOfIndexedInputs(); ++idx) + { + if (this->GetOutput(idx) == NULL) + { + DataObjectPointer newOutput = this->MakeOutput(idx); + this->SetNthOutput(idx, newOutput); + } + this->GetOutput( idx )->Graft( this->GetInput( idx) ); + } + this->Modified(); +} + +void mitk::UnstructuredGridToUnstructuredGridFilter::SetInput(unsigned int idx, const mitk::UnstructuredGrid* grid ) +{ + if ( this->GetInput(idx) != grid ) + { + this->SetNthInput( idx, const_cast( grid ) ); + this->CreateOutputsForAllInputs(idx); + this->Modified(); + } +} + +const mitk::UnstructuredGrid* mitk::UnstructuredGridToUnstructuredGridFilter::GetInput(void) +{ + if (this->GetNumberOfInputs() < 1) + return NULL; + + return static_cast( this->ProcessObject::GetInput(0) ); +} + +const mitk::UnstructuredGrid* mitk::UnstructuredGridToUnstructuredGridFilter::GetInput( unsigned int idx) +{ + if (this->GetNumberOfInputs() < 1) + return NULL; + + return static_cast(this->ProcessObject::GetInput(idx)); +} + +void mitk::UnstructuredGridToUnstructuredGridFilter::GenerateOutputInformation() +{ + mitk::UnstructuredGrid::ConstPointer inputImage = this->GetInput(); + + m_UnstructGrid = this->GetOutput(); + + itkDebugMacro(<<"GenerateOutputInformation()"); + + if(inputImage.IsNull()) return; +} diff --git a/Modules/AlgorithmsExt/mitkUnstructuredGridToUnstructuredGridFilter.h b/Modules/AlgorithmsExt/mitkUnstructuredGridToUnstructuredGridFilter.h new file mode 100644 index 0000000000..a7da64e067 --- /dev/null +++ b/Modules/AlgorithmsExt/mitkUnstructuredGridToUnstructuredGridFilter.h @@ -0,0 +1,77 @@ +/*=================================================================== + +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 _MITKUNSTRUCTUREDGRIDTOUNSTRUCTUREDGRID_h__ +#define _MITKUNSTRUCTUREDGRIDTOUNSTRUCTUREDGRID_h__ + +#include + +#include + +#include +#include +#include + + +namespace mitk { + + class MitkAlgorithmsExt_EXPORT UnstructuredGridToUnstructuredGridFilter + : public UnstructuredGridSource + { + public: + + mitkClassMacro(UnstructuredGridToUnstructuredGridFilter, UnstructuredGridSource) + itkFactorylessNewMacro(Self) + itkCloneMacro(Self) + + /** Initializes the output information */ + virtual void GenerateOutputInformation(); + + /** Returns a const reference to the input image */ + const mitk::UnstructuredGrid* GetInput(void); + + virtual const mitk::UnstructuredGrid* GetInput( unsigned int idx ); + + /** Set the source grid. As input every mitk unstructured grid can be used. */ + using itk::ProcessObject::SetInput; + virtual void SetInput(const UnstructuredGrid *grid); + + virtual void SetInput(unsigned int idx, const UnstructuredGrid *grid ); + + virtual void CreateOutputsForAllInputs(unsigned int idx); + + + protected: + + /** Constructor */ + UnstructuredGridToUnstructuredGridFilter(); + + /** Destructor */ + virtual ~UnstructuredGridToUnstructuredGridFilter(); + + + private: + + /** The output of the filter */ + mitk::UnstructuredGrid::Pointer m_UnstructGrid; + + }; + +} // namespace mitk + +#endif //_MITKUNSTRUCTUREDGRIDTOUNSTRUCTUREDGRID_h__ + + diff --git a/Modules/DataTypesExt/mitkUnstructuredGrid.cpp b/Modules/DataTypesExt/mitkUnstructuredGrid.cpp index 8dfe2970d8..bc9577bba7 100644 --- a/Modules/DataTypesExt/mitkUnstructuredGrid.cpp +++ b/Modules/DataTypesExt/mitkUnstructuredGrid.cpp @@ -1,248 +1,265 @@ /*=================================================================== 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 "mitkUnstructuredGrid.h" #include void mitk::UnstructuredGrid::SetVtkUnstructuredGrid( vtkUnstructuredGrid* grid, unsigned int t ) { this->Expand(t); if(m_GridSeries[ t ] != NULL) { m_GridSeries[ t ]->Delete(); } m_GridSeries[ t ] = grid; // call m_VtkPolyData->Register(NULL) to tell the reference counting that we // want to keep a reference on the object if (m_GridSeries[t] != 0) m_GridSeries[t]->Register(grid); this->Modified(); m_CalculateBoundingBox = true; } void mitk::UnstructuredGrid::Expand(unsigned int timeSteps) { // check if the vector is long enough to contain the new element // at the given position. If not, expand it with sufficient zero-filled elements. if(timeSteps > m_GridSeries.size()) { Superclass::Expand(timeSteps); vtkUnstructuredGrid* pdnull = 0; m_GridSeries.resize( timeSteps, pdnull ); m_CalculateBoundingBox = true; } } void mitk::UnstructuredGrid::ClearData() { for ( VTKUnstructuredGridSeries::iterator it = m_GridSeries.begin(); it != m_GridSeries.end(); ++it ) { if ( ( *it ) != 0 ) ( *it )->Delete(); } m_GridSeries.clear(); Superclass::ClearData(); } void mitk::UnstructuredGrid::InitializeEmpty() { vtkUnstructuredGrid* pdnull = 0; m_GridSeries.resize( 1, pdnull ); Superclass::InitializeTimeGeometry(1); m_Initialized = true; } vtkUnstructuredGrid* mitk::UnstructuredGrid::GetVtkUnstructuredGrid(unsigned int t) { if ( t < m_GridSeries.size() ) { vtkUnstructuredGrid* grid = m_GridSeries[ t ]; if((grid == 0) && (GetSource().GetPointer() != 0)) { RegionType requestedregion; requestedregion.SetIndex(3, t); requestedregion.SetSize(3, 1); SetRequestedRegion(&requestedregion); GetSource()->Update(); } grid = m_GridSeries[ t ]; return grid; } else return 0; } +void mitk::UnstructuredGrid::Graft(const DataObject* data) +{ + const UnstructuredGrid* grid = dynamic_cast(data); + + if(grid == NULL) + mitkThrow() << "Data object used to graft surface is not a mitk::Surface."; + + this->CopyInformation(data); + m_GridSeries.clear(); + + for (unsigned int i = 0; i < grid->m_GridSeries.size(); ++i) + { + m_GridSeries.push_back(vtkUnstructuredGrid::New()); + m_GridSeries.back()->DeepCopy(const_cast(grid)->GetVtkUnstructuredGrid(i)); + } +} + mitk::UnstructuredGrid::UnstructuredGrid() : m_CalculateBoundingBox( false ) { this->InitializeEmpty(); } mitk::UnstructuredGrid::UnstructuredGrid(const mitk::UnstructuredGrid &other) : BaseData(other), m_LargestPossibleRegion(other.m_LargestPossibleRegion), m_CalculateBoundingBox( other.m_CalculateBoundingBox ) { if(!other.m_Initialized) { this->InitializeEmpty(); } else { m_GridSeries = other.m_GridSeries; m_Initialized = other.m_Initialized; } this->SetRequestedRegion( const_cast(&other) ); } mitk::UnstructuredGrid::~UnstructuredGrid() { this->ClearData(); } void mitk::UnstructuredGrid::UpdateOutputInformation() { if ( this->GetSource() ) { this->GetSource()->UpdateOutputInformation(); } if ( ( m_CalculateBoundingBox ) && ( m_GridSeries.size() > 0 ) ) CalculateBoundingBox(); else GetTimeGeometry()->Update(); } void mitk::UnstructuredGrid::CalculateBoundingBox() { // // first make sure, that the associated time sliced geometry has // the same number of geometry 3d's as vtkUnstructuredGrids are present // TimeGeometry* timeGeometry = GetTimeGeometry(); if ( timeGeometry->CountTimeSteps() != m_GridSeries.size() ) { itkExceptionMacro(<<"timeGeometry->CountTimeSteps() != m_GridSeries.size() -- use Initialize(timeSteps) with correct number of timeSteps!"); } // // Iterate over the vtkUnstructuredGrids and update the Geometry // information of each of the items. // for ( unsigned int i = 0 ; i < m_GridSeries.size() ; ++i ) { vtkUnstructuredGrid* grid = m_GridSeries[ i ]; double bounds[ ] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; if ( ( grid != 0 ) && ( grid->GetNumberOfCells() > 0 ) ) { // grid->Update(); //VTK6_TODO grid->ComputeBounds(); grid->GetBounds( bounds ); } mitk::BaseGeometry::Pointer g3d = timeGeometry->GetGeometryForTimeStep( i ); assert( g3d.IsNotNull() ); g3d->SetFloatBounds( bounds ); } timeGeometry->Update(); mitk::BoundingBox::Pointer bb = const_cast( timeGeometry->GetBoundingBoxInWorld() ); itkDebugMacro( << "boundingbox min: "<< bb->GetMinimum()); itkDebugMacro( << "boundingbox max: "<< bb->GetMaximum()); m_CalculateBoundingBox = false; } void mitk::UnstructuredGrid::SetRequestedRegionToLargestPossibleRegion() { m_RequestedRegion = GetLargestPossibleRegion(); } bool mitk::UnstructuredGrid::RequestedRegionIsOutsideOfTheBufferedRegion() { RegionType::IndexValueType end = m_RequestedRegion.GetIndex(3)+m_RequestedRegion.GetSize(3); if(((RegionType::IndexValueType)m_GridSeries.size()) < end) return true; for( RegionType::IndexValueType t=m_RequestedRegion.GetIndex(3); t < end; ++t ) if(m_GridSeries[t] == 0) return true; return false; } bool mitk::UnstructuredGrid::VerifyRequestedRegion() { if( (m_RequestedRegion.GetIndex(3)>=0) && (m_RequestedRegion.GetIndex(3)+m_RequestedRegion.GetSize(3)<=m_GridSeries.size()) ) return true; return false; } void mitk::UnstructuredGrid::SetRequestedRegion(const itk::DataObject *data ) { const mitk::UnstructuredGrid *gridData; gridData = dynamic_cast(data); if (gridData) { m_RequestedRegion = gridData->GetRequestedRegion(); } else { // pointer could not be cast back down itkExceptionMacro( << "mitk::UnstructuredGrid::SetRequestedRegion(DataObject*) cannot cast " << typeid(data).name() << " to " << typeid(UnstructuredGrid*).name() ); } } void mitk::UnstructuredGrid::SetRequestedRegion(UnstructuredGrid::RegionType *region) //by arin { if(region != 0) { m_RequestedRegion = *region; } else { // pointer could not be cast back down itkExceptionMacro( << "mitk::UnstructuredGrid::SetRequestedRegion(UnstructuredGrid::RegionType*) cannot cast " << typeid(region).name() << " to " << typeid(UnstructuredGrid*).name() ); } } void mitk::UnstructuredGrid::CopyInformation( const itk::DataObject * data ) { Superclass::CopyInformation(data); } void mitk::UnstructuredGrid::Update() { if ( GetSource().IsNull() ) { for ( VTKUnstructuredGridSeries::iterator it = m_GridSeries.begin() ; it != m_GridSeries.end() ; ++it ) { // if ( ( *it ) != 0 ) // ( *it )->Update(); //VTK6_TODO } } Superclass::Update(); } diff --git a/Modules/DataTypesExt/mitkUnstructuredGrid.h b/Modules/DataTypesExt/mitkUnstructuredGrid.h index c027fd79d5..5640d2f256 100644 --- a/Modules/DataTypesExt/mitkUnstructuredGrid.h +++ b/Modules/DataTypesExt/mitkUnstructuredGrid.h @@ -1,112 +1,114 @@ /*=================================================================== 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 _MITK_UNSTRUCTURED_GRID_H_ #define _MITK_UNSTRUCTURED_GRID_H_ #include "mitkBaseData.h" #include "MitkDataTypesExtExports.h" #include "itkImageRegion.h" class vtkUnstructuredGrid; namespace mitk { //##Documentation //## @brief Class for storing unstructured grids (vtkUnstructuredGrid) //## @ingroup Data class MitkDataTypesExt_EXPORT UnstructuredGrid : public BaseData { public: // not yet the best choice of a region-type for surfaces, but it works for the time being typedef itk::ImageRegion< 5 > RegionType; mitkClassMacro(UnstructuredGrid, BaseData); itkFactorylessNewMacro(Self) itkCloneMacro(Self) virtual void SetVtkUnstructuredGrid(vtkUnstructuredGrid* grid, unsigned int t = 0); virtual vtkUnstructuredGrid* GetVtkUnstructuredGrid(unsigned int t = 0); virtual void UpdateOutputInformation(); virtual void SetRequestedRegionToLargestPossibleRegion(); virtual bool RequestedRegionIsOutsideOfTheBufferedRegion(); virtual bool VerifyRequestedRegion(); virtual void SetRequestedRegion( const itk::DataObject *data); virtual void SetRequestedRegion(UnstructuredGrid::RegionType *region); + virtual void Graft(const DataObject* data); + virtual void CopyInformation(const itk::DataObject *data); virtual void Update(); // Initialize should not be called manually; // The polydata vector is initialized automatically when enlarged; virtual void Expand( unsigned int timeSteps = 1 ); const RegionType& GetLargestPossibleRegion() const { m_LargestPossibleRegion.SetIndex(3, 0); m_LargestPossibleRegion.SetSize(3, GetTimeGeometry()->CountTimeSteps()); return m_LargestPossibleRegion; } //##Documentation //## Get the region object that defines the size and starting index //## for the region of the image requested (i.e., the region of the //## image to be operated on by a filter). virtual const RegionType& GetRequestedRegion() const { return m_RequestedRegion; } void CalculateBoundingBox(); protected: mitkCloneMacro(Self); typedef std::vector< vtkUnstructuredGrid* > VTKUnstructuredGridSeries; UnstructuredGrid(); UnstructuredGrid(const mitk::UnstructuredGrid & other); virtual ~UnstructuredGrid(); virtual void ClearData(); virtual void InitializeEmpty(); VTKUnstructuredGridSeries m_GridSeries; mutable RegionType m_LargestPossibleRegion; RegionType m_RequestedRegion; bool m_CalculateBoundingBox; }; } // namespace mitk #endif /* _MITK_UNSTRUCTURED_GRID_H_ */