From a365564201d98b704f43e1f52124f55f5e110d2e Mon Sep 17 00:00:00 2001 From: tbaudier Date: Wed, 4 Oct 2017 10:07:33 +0200 Subject: [PATCH] Try to debug RTStruct with rotation matrix Work for clitkDicomRTStruct2ImageFilter not for vv --- common/clitkDicomRTStruct2ImageFilter.cxx | 40 +++++++++++++++++++++++ common/clitkDicomRTStruct2ImageFilter.h | 1 + common/vvImage.cxx | 32 ++++++++++++++++++ common/vvImage.h | 1 + 4 files changed, 74 insertions(+) diff --git a/common/clitkDicomRTStruct2ImageFilter.cxx b/common/clitkDicomRTStruct2ImageFilter.cxx index fcc9e2b..bf515a4 100644 --- a/common/clitkDicomRTStruct2ImageFilter.cxx +++ b/common/clitkDicomRTStruct2ImageFilter.cxx @@ -102,10 +102,14 @@ void clitk::DicomRTStruct2ImageFilter::SetImage(vvImage::Pointer image) mSpacing.resize(3); mOrigin.resize(3); mSize.resize(3); + mDirection.resize(3); for(unsigned int i=0; i<3; i++) { mSpacing[i] = image->GetSpacing()[i]; mOrigin[i] = image->GetOrigin()[i]; mSize[i] = image->GetSize()[i]; + mDirection[i].resize(3); + for(unsigned int j=0; j<3; j++) + mDirection[i][j] = image->GetDirection()[i][j]; } } //-------------------------------------------------------------------- @@ -125,10 +129,14 @@ void clitk::DicomRTStruct2ImageFilter::SetImageFilename(std::string f) mSpacing.resize(3); mOrigin.resize(3); mSize.resize(3); + mDirection.resize(3); for(unsigned int i=0; i<3; i++) { mSpacing[i] = header->GetSpacing(i); mOrigin[i] = header->GetOrigin(i); mSize[i] = header->GetDimensions(i); + mDirection[i].resize(3); + for(unsigned int j=0; j<3; j++) + mDirection[i][j] = header->GetDirection(i)[j]; } } //-------------------------------------------------------------------- @@ -176,6 +184,38 @@ void clitk::DicomRTStruct2ImageFilter::Update() // Get bounds double *bounds=mesh->GetBounds(); + //Change mOrigin, mSize and mSpacing with respect to the directions + // Spacing is influenced by input direction + std::vector tempSpacing; + tempSpacing.resize(3); + for(int i=0; i<3; i++) { + tempSpacing[i] = 0.0; + for(int j=0; j<3; j++) { + tempSpacing[i] += mDirection[i][j] * mSpacing[j]; + } + } + for(int i=0; i<3; i++) + mSpacing[i] = tempSpacing[i]; + + // Size is influenced by affine transform matrix and input direction + // Size is converted to double, transformed and converted back to size type. + std::vector tempSize; + tempSize.resize(3); + for(int i=0; i<3; i++) { + tempSize[i] = 0.0; + for(int j=0; j<3; j++) { + tempSize[i] += mDirection[i][j] * mSize[j]; + } + } + for(int i=0; i<3; i++) { + if (tempSize[i] < 0.0) { + tempSize[i] *= -1; + mOrigin[i] += mSpacing[i]*(tempSize[i] -1); + mSpacing[i] *= -1; + } + mSize[i] = lrint(tempSize[i]); + } + // Compute origin std::vector origin; origin.resize(3); diff --git a/common/clitkDicomRTStruct2ImageFilter.h b/common/clitkDicomRTStruct2ImageFilter.h index 0df00dd..4943818 100644 --- a/common/clitkDicomRTStruct2ImageFilter.h +++ b/common/clitkDicomRTStruct2ImageFilter.h @@ -57,6 +57,7 @@ namespace clitk { std::vector mSpacing; std::vector mOrigin; std::vector mSize; + std::vector< std::vector< double> > mDirection; clitk::DicomRT_ROI * mROI; vtkSmartPointer mBinaryImage; }; diff --git a/common/vvImage.cxx b/common/vvImage.cxx index c7e13cc..9bed494 100644 --- a/common/vvImage.cxx +++ b/common/vvImage.cxx @@ -261,6 +261,38 @@ const std::vector< vtkSmartPointer >& vvImage::GetTransform() //-------------------------------------------------------------------- +//-------------------------------------------------------------------- +std::vector< std::vector > vvImage::GetDirection() +{ + int dim = this->GetNumberOfDimensions(); + vtkSmartPointer transform = vtkSmartPointer::New(); + transform->Identity(); + for (int i = 0; i < mTransform.size(); i++) + transform->Concatenate(this->mTransform[i]); + + vtkSmartPointer matrix = transform->GetMatrix(); + matrix->Invert(); + std::vector > direction0; + for (int i = 0; i < dim; i++) { + if (i != 3) { + std::vector direction1; + for (int j = 0; j < dim; j++) { + if (j != 3) +#if VTK_MAJOR_VERSION <= 6 + direction1.push_back((*matrix)[i][j]); +#else + direction1.push_back((*matrix).GetElement(i,j)) +#endif + } + direction0.push_back(direction1); + } + } + return direction0; +} +//-------------------------------------------------------------------- + + + //-------------------------------------------------------------------- bool vvImage::HaveSameSizeAndSpacingThan(vvImage * other) { diff --git a/common/vvImage.h b/common/vvImage.h index d18ee98..09cb4d9 100644 --- a/common/vvImage.h +++ b/common/vvImage.h @@ -60,6 +60,7 @@ public : std::vector GetSpacing(); std::vector GetOrigin() const; std::vector GetSize(); + std::vector< std::vector > GetDirection(); std::string GetScalarTypeAsITKString(); int GetNumberOfScalarComponents(); int GetScalarSize(); -- 2.46.0