X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FvvFromITK.h;h=30e1bfe6325c3205a6cb47dee118483ce8c120a0;hb=3c2462eafabcf428b4d2775c414a7b5f62511d3f;hp=5a00f65d1220ae87964dd27a45fa74927a14761b;hpb=573d80d0f7a17607d2ee883c21c940c0ba020282;p=clitk.git diff --git a/common/vvFromITK.h b/common/vvFromITK.h index 5a00f65..30e1bfe 100644 --- a/common/vvFromITK.h +++ b/common/vvFromITK.h @@ -28,45 +28,73 @@ /**Converts the itk image to vv, handling the 4D problem * The time_sequence boolean specifies that the image is to be interpreted as a time sequence, * even if its dim is < 4. */ -template vvImage::Pointer vvImageFromITK(typename itk::Image::Pointer input, bool time_sequence=false) + +template +static inline void ReadTimeSequence (vvImage::Pointer& vv_image, typename itk::Image::Pointer input, bool time_sequence=false) { - assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p) + 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 +{ + static vvImage::Pointer Do (typename itk::Image::Pointer input, bool time_sequence=false) + { vvImage::Pointer vv_image=vvImage::New(); typedef itk::Image< PixelType, Dim > InputImageType; - if (Dim == 4 || 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(); - 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]); - } + if (time_sequence) //The time sequence case: create a series of VTK images + 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; + } +}; + +template +struct vvImageFromITK_Impl<4u, PixelType> +{ + static vvImage::Pointer Do (typename itk::Image::Pointer input, bool time_sequence=false) + { + vvImage::Pointer vv_image = vvImage::New(); + ReadTimeSequence<4u,PixelType>(vv_image, input, time_sequence); return vv_image; + } +}; + +template vvImage::Pointer vvImageFromITK(typename itk::Image::Pointer input, bool time_sequence=false) +{ + assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p) + return vvImageFromITK_Impl::Do(input, time_sequence); } //------------------------------------------------------------------------------