Skip to content

Commit

Permalink
ENH: Support generic linear Transform
Browse files Browse the repository at this point in the history
Replace TransformGeometryImageFilter::SetRigidTransform with
SetTransform, while adding support for generic linear transform. The
Transform::ApplyToImageMetadata method is re-used for computation.
  • Loading branch information
blowekamp authored and hjmjohnson committed Sep 10, 2021
1 parent 4379b75 commit 978bc5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ class ITK_TEMPLATE_EXPORT TransformGeometryImageFilter : public InPlaceImageFilt
#endif

/** Transform type alias */
using RigidTransformType = VersorRigid3DTransform<double>;
using RigidTransformConstPointer = typename RigidTransformType::ConstPointer;
using TransformType = Transform<double, InputImageDimension, OutputImageDimension>;
using TransformConstPointer = typename TransformType::ConstPointer;

/** Set/Get required rigid transform. */
itkSetGetDecoratedObjectInputMacro(RigidTransform, RigidTransformType);
itkSetGetDecoratedObjectInputMacro(Transform, TransformType);

/** Set/Get required input image. (A wrapper to this->Set/GetInput()) */
itkSetInputMacro(InputImage, InputImageType);
Expand All @@ -156,6 +156,9 @@ class ITK_TEMPLATE_EXPORT TransformGeometryImageFilter : public InPlaceImageFilt
void
GenerateOutputInformation() override;

void
VerifyPreconditions() ITKv5_CONST override;

void
GenerateData() override;
};
Expand Down
37 changes: 18 additions & 19 deletions Modules/Core/Transform/include/itkTransformGeometryImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,21 @@ TransformGeometryImageFilter<TInputImage, TOutputImage>::TransformGeometryImageF
Self::SetPrimaryInputName("InputImage");

// "RigidTransform" required ( not numbered )
Self::AddRequiredInputName("RigidTransform");
Self::AddRequiredInputName("Transform");
}

template <typename TInputImage, typename TOutputImage>
void
TransformGeometryImageFilter<TInputImage, TOutputImage>::VerifyPreconditions() ITKv5_CONST
{
Superclass::VerifyPreconditions();

TransformConstPointer tx = this->GetTransform();

if (!tx->IsLinear())
{
itkExceptionMacro(<< "Transform set to non-linear transform of type: " << tx->GetNameOfClass());
}
}


Expand All @@ -43,25 +57,10 @@ TransformGeometryImageFilter<TInputImage, TOutputImage>::GenerateOutputInformati

Superclass::GenerateOutputInformation();

OutputImageType * outputPtr = this->GetOutput();
const InputImageType * inputPtr = this->GetInputImage();
OutputImageType * outputPtr = this->GetOutput();

// SEE HEADER FILE FOR MATH DESCRIPTION
RigidTransformConstPointer FMTxfm = this->GetRigidTransform();
const typename RigidTransformType::MatrixType inverseRotation(FMTxfm->GetMatrix().GetInverse());

// Modify the origin and direction info of the image to reflect the transform.
Vector<double, 3> newOriginVector =
inverseRotation * (inputPtr->GetOrigin().GetVectorFromOrigin() - FMTxfm->GetCenter().GetVectorFromOrigin() -
FMTxfm->GetTranslation()) // NewOrigin = [R^-1] * ( O - C - T ) + C
+ FMTxfm->GetCenter().GetVectorFromOrigin();
Point<double, 3> newOriginPoint;
for (int i = 0; i < 3; ++i)
{
newOriginPoint[i] = newOriginVector[i];
}
outputPtr->SetOrigin(newOriginPoint);
outputPtr->SetDirection(inverseRotation * inputPtr->GetDirection()); // NewDC = [R^-1][DC]
TransformConstPointer tx = this->GetTransform();
tx->ApplyToImageMetadata(outputPtr);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ itkTransformGeometryImageFilterTest(int argc, char * argv[])

filter->SetInputImage(inputImage);
ITK_TEST_SET_GET_VALUE(inputImage, filter->GetInputImage());
filter->SetRigidTransform(transform);
ITK_TEST_SET_GET_VALUE(transform, filter->GetRigidTransform());
filter->SetTransform(transform);
ITK_TEST_SET_GET_VALUE(transform, filter->GetTransform());
ITK_TRY_EXPECT_NO_EXCEPTION(filter->Update());
ImagePointer outputImage = filter->GetOutput();
ITK_TRY_EXPECT_NO_EXCEPTION(itk::WriteImage(outputImage, argv[3]));
Expand Down

0 comments on commit 978bc5b

Please sign in to comment.