diff --git a/Modules/Segmentation/Algorithms/mitkSurfaceStampImageFilter.cpp b/Modules/Segmentation/Algorithms/mitkSurfaceStampImageFilter.cpp index d6eb31db5b..4c13cfc998 100644 --- a/Modules/Segmentation/Algorithms/mitkSurfaceStampImageFilter.cpp +++ b/Modules/Segmentation/Algorithms/mitkSurfaceStampImageFilter.cpp @@ -1,296 +1,298 @@ /*=================================================================== 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 "mitkSurfaceStampImageFilter.h" #include "mitkTimeHelper.h" #include "mitkImageWriteAccessor.h" #include "mitkImageAccessByItk.h" #include #include #include #include #include #include #include #include mitk::SurfaceStampImageFilter::SurfaceStampImageFilter() : m_MakeOutputBinary( false ), m_OverwriteBackground( false ), m_ForegroundValue( 1.0 ), m_BackgroundValue( 0.0 ) { } mitk::SurfaceStampImageFilter::~SurfaceStampImageFilter() { } void mitk::SurfaceStampImageFilter::GenerateInputRequestedRegion() { mitk::Image* outputImage = this->GetOutput(); if((outputImage->IsInitialized()==false) ) return; GenerateTimeInInputRegion(outputImage, const_cast< mitk::Image * > ( this->GetInput() )); } void mitk::SurfaceStampImageFilter::GenerateOutputInformation() { mitk::Image::ConstPointer inputImage = this->GetInput(); mitk::Image::Pointer outputImage = this->GetOutput(); itkDebugMacro(<<"GenerateOutputInformation()"); if( inputImage.IsNull() || (inputImage->IsInitialized() == false) || (inputImage->GetTimeSlicedGeometry() == NULL)) return; if (m_MakeOutputBinary) outputImage->Initialize(mitk::MakeScalarPixelType() , *inputImage->GetTimeSlicedGeometry()); else outputImage->Initialize(inputImage->GetPixelType(), *inputImage->GetTimeSlicedGeometry()); outputImage->SetPropertyList(inputImage->GetPropertyList()->Clone()); } void mitk::SurfaceStampImageFilter::SetSurface(mitk::Surface *surface) { m_Surface = surface; } void mitk::SurfaceStampImageFilter::GenerateData() { mitk::Image::ConstPointer inputImage = this->GetInput(); if (inputImage.IsNull()) return; mitk::Image::Pointer outputImage = this->GetOutput(); if(outputImage->IsInitialized()==false ) return; if (m_Surface.IsNull()) return; mitk::Image::RegionType outputRegion = outputImage->GetRequestedRegion(); int tstart=outputRegion.GetIndex(3); int tmax=tstart+outputRegion.GetSize(3); if ( tmax > 0) { int t; for(t=tstart;tSurfaceStamp( t ); } } else { this->SurfaceStamp( 0 ); } } void mitk::SurfaceStampImageFilter::SurfaceStamp(int time) { mitk::Image::ConstPointer inputImage = this->GetInput(); const mitk::TimeSlicedGeometry *surfaceTimeGeometry = inputImage->GetTimeSlicedGeometry(); const mitk::TimeSlicedGeometry *imageTimeGeometry = inputImage->GetTimeSlicedGeometry(); // Convert time step from image time-frame to surface time-frame int surfaceTimeStep = surfaceTimeGeometry->TimeStepToTimeStep( imageTimeGeometry, time ); vtkPolyData * polydata = m_Surface->GetVtkPolyData( surfaceTimeStep ); if (!polydata) mitkThrow() << "Polydata is null."; vtkSmartPointer transformFilter = vtkSmartPointer::New(); transformFilter->SetInput(polydata); // transformFilter->ReleaseDataFlagOn(); vtkSmartPointer transform = vtkSmartPointer::New(); Geometry3D* geometry = surfaceTimeGeometry->GetGeometry3D( surfaceTimeStep ); geometry->TransferItkToVtkTransform(); transform->PostMultiply(); transform->Concatenate(geometry->GetVtkTransform()->GetMatrix()); // take image geometry into account. vtk-Image information will be changed to unit spacing and zero origin below. Geometry3D* imageGeometry = imageTimeGeometry->GetGeometry3D(time); imageGeometry->TransferItkToVtkTransform(); transform->Concatenate(imageGeometry->GetVtkTransform()->GetLinearInverse()); transformFilter->SetTransform(transform); transformFilter->Update(); polydata = transformFilter->GetOutput(); if ( !polydata || !polydata->GetNumberOfPoints() ) mitkThrow() << "Polydata retrieved from transformation is null or has no points."; MeshType::Pointer mesh = MeshType::New(); mesh->SetCellsAllocationMethod( MeshType::CellsAllocatedDynamicallyCellByCell ); unsigned int numberOfPoints = polydata->GetNumberOfPoints(); mesh->GetPoints()->Reserve( numberOfPoints ); vtkPoints* points = polydata->GetPoints(); MeshType::PointType point; for( int i=0; i < numberOfPoints; i++ ) { double* aux = points->GetPoint(i); point[0] = aux[0]; point[1] = aux[1]; point[2] = aux[2]; mesh->SetPoint( i, point ); } // Load the polygons into the itk::Mesh typedef MeshType::CellAutoPointer CellAutoPointerType; typedef MeshType::CellType CellType; typedef itk::TriangleCell< CellType > TriangleCellType; typedef MeshType::PointIdentifier PointIdentifierType; typedef MeshType::CellIdentifier CellIdentifierType; // Read the number of polygons CellIdentifierType numberOfPolygons = 0; numberOfPolygons = polydata->GetNumberOfPolys(); vtkCellArray* polys = polydata->GetPolys(); PointIdentifierType numberOfCellPoints = 3; CellIdentifierType i = 0; for (i=0; iGetCell(i); cellIds = vcell->GetPointIds(); CellAutoPointerType cell; TriangleCellType * triangleCell = new TriangleCellType; PointIdentifierType k; for( k = 0; k < numberOfCellPoints; k++ ) { triangleCell->SetPointId( k, cellIds->GetId(k) ); } cell.TakeOwnership( triangleCell ); mesh->SetCell( i, cell ); } if ( !mesh->GetNumberOfPoints() ) mitkThrow() << "Generated itk mesh is empty."; if (m_MakeOutputBinary) { this->SurfaceStampBinaryOutputProcessing( mesh ); } else { AccessFixedDimensionByItk_1(inputImage, SurfaceStampProcessing, 3, mesh); } } void mitk::SurfaceStampImageFilter::SurfaceStampBinaryOutputProcessing( MeshType* mesh ) { mitk::Image* inputImage = const_cast< mitk::Image* >( this->GetInput() ); mitk::Image::Pointer outputImage = this->GetOutput(); typedef itk::Image< unsigned char, 3 > BinaryImageType; BinaryImageType::Pointer itkInputImage; mitk::CastToItkImage(inputImage, itkInputImage); typedef itk::TriangleMeshToBinaryImageFilter FilterType; FilterType::Pointer filter = FilterType::New(); filter->SetInput(mesh); filter->SetInfoImage(itkInputImage); filter->SetInsideValue(1); filter->SetOutsideValue(0); filter->Update(); BinaryImageType::Pointer resultImage = filter->GetOutput(); resultImage->DisconnectPipeline(); mitk::CastToMitkImage(resultImage, outputImage); } template < typename TPixel > void mitk::SurfaceStampImageFilter::SurfaceStampProcessing(itk::Image< TPixel, 3 >* input, MeshType* mesh) { typedef itk::Image< TPixel, 3 > ImageType; typedef itk::Image< unsigned char, 3 > BinaryImageType; typedef itk::TriangleMeshToBinaryImageFilter FilterType; - typename BinaryImageType::Pointer binaryInput = BinaryImageType::New(); - binaryInput->SetRegions( input->GetLargestPossibleRegion() ); + BinaryImageType::Pointer binaryInput = BinaryImageType::New(); binaryInput->SetSpacing( input->GetSpacing() ); + binaryInput->SetOrigin( input->GetOrigin() ); + binaryInput->SetDirection( input->GetDirection() ); + binaryInput->SetRegions( input->GetLargestPossibleRegion() ); binaryInput->Allocate(); binaryInput->FillBuffer(0); - typename FilterType::Pointer filter = FilterType::New(); + FilterType::Pointer filter = FilterType::New(); filter->SetInput(mesh); filter->SetInfoImage(binaryInput); filter->SetInsideValue(1); filter->SetOutsideValue(0); filter->Update(); BinaryImageType::Pointer resultImage = filter->GetOutput(); resultImage->DisconnectPipeline(); mitk::Image::Pointer outputImage = this->GetOutput(); typename ImageType::Pointer itkOutputImage; mitk::CastToItkImage(outputImage, itkOutputImage); typedef itk::ImageRegionConstIterator< BinaryImageType > BinaryIteratorType; typedef itk::ImageRegionConstIterator< ImageType > InputIteratorType; typedef itk::ImageRegionIterator< ImageType > OutputIteratorType; BinaryIteratorType sourceIter( resultImage, resultImage->GetLargestPossibleRegion() ); sourceIter.GoToBegin(); InputIteratorType inputIter( input, input->GetLargestPossibleRegion() ); inputIter.GoToBegin(); OutputIteratorType outputIter( itkOutputImage, itkOutputImage->GetLargestPossibleRegion() ); outputIter.GoToBegin(); typename ImageType::PixelType inputValue; unsigned char sourceValue; - typename ImageType::PixelType fgValue = static_cast< ImageType::PixelType >(m_ForegroundValue); - typename ImageType::PixelType bgValue = static_cast< ImageType::PixelType >(m_BackgroundValue); + typename ImageType::PixelType fgValue = static_cast< typename ImageType::PixelType >(m_ForegroundValue); + typename ImageType::PixelType bgValue = static_cast< typename ImageType::PixelType >(m_BackgroundValue); while ( !sourceIter.IsAtEnd() ) { sourceValue = static_cast< unsigned char >(sourceIter.Get()); - inputValue = static_cast< ImageType::PixelType >(inputIter.Get()); + inputValue = static_cast< typename ImageType::PixelType >(inputIter.Get()); if (sourceValue != 0) outputIter.Set( fgValue ); else if (m_OverwriteBackground) outputIter.Set( bgValue ); else outputIter.Set( inputValue ); ++sourceIter; ++inputIter; ++outputIter; } }