From: Vivien Delmon Date: Tue, 3 May 2011 16:34:56 +0000 (+0200) Subject: Factorize time sequence reader code X-Git-Tag: v1.2.0~1 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=e0bb2eed6925e802eb288420ebd1fc625d84c769;p=clitk.git Factorize time sequence reader code * Factorize code introduce in previous patch. --- diff --git a/common/vvFromITK.h b/common/vvFromITK.h index d4232b1..30e1bfe 100644 --- a/common/vvFromITK.h +++ b/common/vvFromITK.h @@ -29,6 +29,40 @@ * The time_sequence boolean specifies that the image is to be interpreted as a time sequence, * even if its dim is < 4. */ +template +static inline void ReadTimeSequence (vvImage::Pointer& vv_image, typename itk::Image::Pointer input, bool time_sequence=false) +{ + typedef itk::Image< PixelType, Dim > InputImageType; + typedef itk::Image< PixelType, Dim - 1 > ItkImageType; + typedef itk::ExtractImageFilter FilterType; + + //extract the 3D slices and put them in a std::vector + input->UpdateOutputInformation(); + typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion(); + typename InputImageType::SizeType inputSize = inputRegion.GetSize(); + typename InputImageType::IndexType start = inputRegion.GetIndex(); + typename InputImageType::SizeType extractedRegionSize = inputSize; + typename InputImageType::RegionType extractedRegion; + extractedRegionSize[Dim - 1] = 0; + extractedRegion.SetSize(extractedRegionSize); + + for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) { + start[Dim - 1] = i; + extractedRegion.SetIndex(start); + + typename FilterType::Pointer filter = FilterType::New(); +#if ITK_VERSION_MAJOR == 4 + filter->SetDirectionCollapseToSubmatrix(); +#endif + filter->SetExtractionRegion(extractedRegion); + filter->SetInput(input); + filter->ReleaseDataFlagOn(); + vv_image->AddItkImage(filter->GetOutput()); + } + vv_image->SetTimeSpacing(input->GetSpacing()[Dim-1]); + vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]); +} + template struct vvImageFromITK_Impl { @@ -36,41 +70,12 @@ struct vvImageFromITK_Impl { vvImage::Pointer vv_image=vvImage::New(); typedef itk::Image< PixelType, Dim > InputImageType; + if (time_sequence) //The time sequence case: create a series of VTK images - { - typedef itk::Image< PixelType, Dim - 1 > ItkImageType; - typedef itk::ExtractImageFilter FilterType; - - //extract the 3D slices and put them in a std::vector - input->UpdateOutputInformation(); - typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion(); - typename InputImageType::SizeType inputSize = inputRegion.GetSize(); - typename InputImageType::IndexType start = inputRegion.GetIndex(); - typename InputImageType::SizeType extractedRegionSize = inputSize; - typename InputImageType::RegionType extractedRegion; - extractedRegionSize[Dim - 1] = 0; - extractedRegion.SetSize(extractedRegionSize); - - for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) { - start[Dim - 1] = i; - extractedRegion.SetIndex(start); - - typename FilterType::Pointer filter = FilterType::New(); -#if ITK_VERSION_MAJOR == 4 - filter->SetDirectionCollapseToSubmatrix(); -#endif - filter->SetExtractionRegion(extractedRegion); - filter->SetInput(input); - filter->ReleaseDataFlagOn(); - vv_image->AddItkImage(filter->GetOutput()); - } - vv_image->SetTimeSpacing(input->GetSpacing()[Dim-1]); - vv_image->SetTimeOrigin(input->GetOrigin()[Dim-1]); - } + ReadTimeSequence(vv_image, input, time_sequence); else //Dim == 1,2,3 and not time_sequence - { - vv_image->AddItkImage(input); - } + vv_image->AddItkImage(input); + return vv_image; } }; @@ -80,37 +85,8 @@ struct vvImageFromITK_Impl<4u, PixelType> { static vvImage::Pointer Do (typename itk::Image::Pointer input, bool time_sequence=false) { - vvImage::Pointer vv_image=vvImage::New(); - typedef itk::Image< PixelType, 4 > InputImageType; - - typedef itk::Image< PixelType, 3 > ItkImageType; - typedef itk::ExtractImageFilter FilterType; - - //extract the 3D slices and put them in a std::vector - input->UpdateOutputInformation(); - typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion(); - typename InputImageType::SizeType inputSize = inputRegion.GetSize(); - typename InputImageType::IndexType start = inputRegion.GetIndex(); - typename InputImageType::SizeType extractedRegionSize = inputSize; - typename InputImageType::RegionType extractedRegion; - extractedRegionSize[3] = 0; - extractedRegion.SetSize(extractedRegionSize); - - for (unsigned int i = 0; i < inputSize[3]; i++) { - start[3] = i; - extractedRegion.SetIndex(start); - - typename FilterType::Pointer filter = FilterType::New(); -#if ITK_VERSION_MAJOR == 4 - filter->SetDirectionCollapseToSubmatrix(); -#endif - filter->SetExtractionRegion(extractedRegion); - filter->SetInput(input); - filter->ReleaseDataFlagOn(); - vv_image->AddItkImage(filter->GetOutput()); - } - vv_image->SetTimeSpacing(input->GetSpacing()[3]); - vv_image->SetTimeOrigin(input->GetOrigin()[3]); + vvImage::Pointer vv_image = vvImage::New(); + ReadTimeSequence<4u,PixelType>(vv_image, input, time_sequence); return vv_image; } };