X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=common%2FclitkDicomRT_ROI_ConvertToImageFilter.cxx;h=e3b34723fdc264865ee925ddac9cd549b836bd67;hb=fa358ee6738c92950cd9e6c45f55dda6e9b4576e;hp=ef5ecb473f466893ba1949b58ea35b21d2788d8c;hpb=163fa80ef3873595f3e3cf75fb03e53536d1a6ee;p=clitk.git diff --git a/common/clitkDicomRT_ROI_ConvertToImageFilter.cxx b/common/clitkDicomRT_ROI_ConvertToImageFilter.cxx index ef5ecb4..e3b3472 100644 --- a/common/clitkDicomRT_ROI_ConvertToImageFilter.cxx +++ b/common/clitkDicomRT_ROI_ConvertToImageFilter.cxx @@ -2,9 +2,9 @@ Program: vv http://www.creatis.insa-lyon.fr/rio/vv Main authors : XX XX XX - Authors belongs to: + Authors belongs to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -17,18 +17,25 @@ =========================================================================*/ -#include "clitkDicomRT_ROI_ConvertToImageFilter.h" +#include +#include + +// clitk +#include "clitkDicomRT_ROI_ConvertToImageFilter.h" +#include "clitkImageCommon.h" + +// vtk #include #include #include #include -#include -#include "clitkImageCommon.h" +#include + //-------------------------------------------------------------------- -clitk::DicomRT_ROI_ConvertToImageFilter::DicomRT_ROI_ConvertToImageFilter() { +clitk::DicomRT_ROI_ConvertToImageFilter::DicomRT_ROI_ConvertToImageFilter() +{ mROI = NULL; - mImageInfoIsSet = false; mWriteOutput = false; mCropMask = true; } @@ -36,28 +43,36 @@ clitk::DicomRT_ROI_ConvertToImageFilter::DicomRT_ROI_ConvertToImageFilter() { //-------------------------------------------------------------------- -clitk::DicomRT_ROI_ConvertToImageFilter::~DicomRT_ROI_ConvertToImageFilter() { - +clitk::DicomRT_ROI_ConvertToImageFilter::~DicomRT_ROI_ConvertToImageFilter() +{ + } //-------------------------------------------------------------------- +bool clitk::DicomRT_ROI_ConvertToImageFilter::ImageInfoIsSet() const +{ + return mSize.size() && mSpacing.size() && mOrigin.size(); +} //-------------------------------------------------------------------- -void clitk::DicomRT_ROI_ConvertToImageFilter::SetROI(clitk::DicomRT_ROI * roi) { +void clitk::DicomRT_ROI_ConvertToImageFilter::SetROI(clitk::DicomRT_ROI * roi) +{ mROI = roi; } //-------------------------------------------------------------------- //-------------------------------------------------------------------- -void clitk::DicomRT_ROI_ConvertToImageFilter::SetCropMaskEnabled(bool b) { +void clitk::DicomRT_ROI_ConvertToImageFilter::SetCropMaskEnabled(bool b) +{ mCropMask = b; } //-------------------------------------------------------------------- //-------------------------------------------------------------------- -void clitk::DicomRT_ROI_ConvertToImageFilter::SetOutputImageFilename(std::string s) { +void clitk::DicomRT_ROI_ConvertToImageFilter::SetOutputImageFilename(std::string s) +{ mOutputFilename = s; mWriteOutput = true; } @@ -65,7 +80,8 @@ void clitk::DicomRT_ROI_ConvertToImageFilter::SetOutputImageFilename(std::string //-------------------------------------------------------------------- -void clitk::DicomRT_ROI_ConvertToImageFilter::SetImageFilename(std::string f) { +void clitk::DicomRT_ROI_ConvertToImageFilter::SetImageFilename(std::string f) +{ itk::ImageIOBase::Pointer header = clitk::readImageHeader(f); if (header->GetNumberOfDimensions() < 3) { std::cerr << "Error. Please provide a 3D image instead of " << f << std::endl; @@ -82,47 +98,59 @@ void clitk::DicomRT_ROI_ConvertToImageFilter::SetImageFilename(std::string f) { mOrigin[i] = header->GetOrigin(i); mSize[i] = header->GetDimensions(i); } - mImageInfoIsSet = true; } //-------------------------------------------------------------------- +void clitk::DicomRT_ROI_ConvertToImageFilter::SetOutputOrigin(const double* origin) +{ + std::copy(origin,origin+3,std::back_inserter(mOrigin)); +} +//-------------------------------------------------------------------- +void clitk::DicomRT_ROI_ConvertToImageFilter::SetOutputSpacing(const double* spacing) +{ + std::copy(spacing,spacing+3,std::back_inserter(mSpacing)); +} +//-------------------------------------------------------------------- +void clitk::DicomRT_ROI_ConvertToImageFilter::SetOutputSize(const unsigned long* size) +{ + std::copy(size,size+3,std::back_inserter(mSize)); +} //-------------------------------------------------------------------- -void clitk::DicomRT_ROI_ConvertToImageFilter::Update() { +void clitk::DicomRT_ROI_ConvertToImageFilter::Update() +{ if (!mROI) { std::cerr << "Error. No ROI set, please use SetROI." << std::endl; exit(0); } - if (!mImageInfoIsSet) { + if (!ImageInfoIsSet()) { std::cerr << "Error. Please provide image info (spacing/origin) with SetImageFilename" << std::endl; exit(0); } - // DD("Update"); - + // Get Mesh vtkPolyData * mesh = mROI->GetMesh(); - DD(mesh->GetNumberOfCells()); - + // Get bounds - double *bounds=mesh->GetBounds(); + double *bounds=mesh->GetBounds(); // for(int i=0; i<6; i++){ // DD(bounds[i]); // } // Compute origin - std::vector origin; + std::vector origin; origin.resize(3); origin[0] = floor((bounds[0]-mOrigin[0])/mSpacing[0]-2)*mSpacing[0]+mOrigin[0]; origin[1] = floor((bounds[2]-mOrigin[1])/mSpacing[1]-2)*mSpacing[1]+mOrigin[1]; origin[2] = floor((bounds[4]-mOrigin[2])/mSpacing[2]-2)*mSpacing[2]+mOrigin[2]; - + // Compute extend - std::vector extend; + std::vector extend; extend.resize(3); extend[0] = ceil((bounds[1]-origin[0])/mSpacing[0]+4); extend[1] = ceil((bounds[3]-origin[1])/mSpacing[1]+4); extend[2] = ceil((bounds[5]-origin[2])/mSpacing[2]+4); - + // If no crop, set initial image size/origin if (!mCropMask) { for(int i=0; i<3; i++) { @@ -130,14 +158,14 @@ void clitk::DicomRT_ROI_ConvertToImageFilter::Update() { extend[i] = mSize[i]-1; } } - + // Create new output image - mBinaryImage = vtkImageData::New(); + mBinaryImage = vtkSmartPointer::New(); mBinaryImage->SetScalarTypeToUnsignedChar(); mBinaryImage->SetOrigin(&origin[0]); mBinaryImage->SetSpacing(&mSpacing[0]); - mBinaryImage->SetExtent(0, extend[0], - 0, extend[1], + mBinaryImage->SetExtent(0, extend[0], + 0, extend[1], 0, extend[2]); mBinaryImage->AllocateScalars(); @@ -148,14 +176,14 @@ void clitk::DicomRT_ROI_ConvertToImageFilter::Update() { // } memset(mBinaryImage->GetScalarPointer(), 0, mBinaryImage->GetDimensions()[0]*mBinaryImage->GetDimensions()[1]*mBinaryImage->GetDimensions()[2]*sizeof(unsigned char)); - + // Extrude vtkSmartPointer extrude=vtkSmartPointer::New(); extrude->SetInput(mesh); - ///We extrude in the -slice_spacing direction to respect the FOCAL convention // ????????????? + ///We extrude in the -slice_spacing direction to respect the FOCAL convention (NEEDED !) extrude->SetVector(0, 0, -mSpacing[2]); - // Binarization + // Binarization vtkSmartPointer sts=vtkSmartPointer::New(); //The following line is extremely important //http://www.nabble.com/Bug-in-vtkPolyDataToImageStencil--td23368312.html#a23370933 @@ -165,27 +193,38 @@ void clitk::DicomRT_ROI_ConvertToImageFilter::Update() { //sts->SetInput(mesh); vtkSmartPointer stencil=vtkSmartPointer::New(); - stencil->SetStencil(sts->GetOutput()); + stencil->SetStencil(sts->GetOutput()); stencil->SetInput(mBinaryImage); stencil->ReverseStencilOn(); stencil->Update(); + + /* + vtkSmartPointer w = vtkSmartPointer::New(); + w->SetInput(stencil->GetOutput()); + w->SetFileName("binary2.mhd"); + w->Write(); + */ + mBinaryImage->ShallowCopy(stencil->GetOutput()); - + if (mWriteOutput) { - typedef itk::Image ImageType; - typedef itk::VTKImageToImageFilter ConnectorType; - ConnectorType::Pointer connector = ConnectorType::New(); - connector->SetInput(GetOutput()); - connector->Update(); - clitk::writeImage(connector->GetOutput(), mOutputFilename); + typedef itk::Image ImageType; + typedef itk::VTKImageToImageFilter ConnectorType; + ConnectorType::Pointer connector = ConnectorType::New(); + connector->SetInput(GetOutput()); + connector->Update(); + clitk::writeImage(connector->GetOutput(), mOutputFilename); } } //-------------------------------------------------------------------- - + //-------------------------------------------------------------------- -vtkImageData * clitk::DicomRT_ROI_ConvertToImageFilter::GetOutput() { +vtkImageData * clitk::DicomRT_ROI_ConvertToImageFilter::GetOutput() +{ + assert(mBinaryImage); return mBinaryImage; } //-------------------------------------------------------------------- +