Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inverse capability to KernelTransform #4529

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Modules/Core/Transform/include/itkKernelTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,25 @@ class ITK_TEMPLATE_EXPORT KernelTransform : public Transform<TParametersValueTyp
return Self::TransformCategoryEnum::Spline;
}

/** Returns a boolean indicating whether it is possible or not to compute the
* inverse of this current Transform. If it is possible, then the inverse of
* the transform is returned in the inverseTransform variable passed by the
* user. Inverting is possible if the variable is not a null pointer.
*/
bool
GetInverse(Self * inverseTransform) const;

using InverseTransformBasePointer = typename Superclass::InverseTransformBasePointer;

/** Return an inverse of this transform. */
InverseTransformBasePointer
GetInverseTransform() const override
{
Pointer inv = New();
this->GetInverse(inv);
return inv;
}

/** Stiffness of the spline. A stiffness of zero results in the
* standard interpolating spline. A non-zero stiffness allows the
* spline to approximate rather than interpolate the landmarks.
Expand Down
37 changes: 37 additions & 0 deletions Modules/Core/Transform/include/itkKernelTransform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,43 @@ KernelTransform<TParametersValueType, VDimension>::GetFixedParameters() const ->
}


template <typename TParametersValueType, unsigned int VDimension>
bool
KernelTransform<TParametersValueType, VDimension>::GetInverse(Self * inverseTransform) const
{
if (!inverseTransform)
{
return false;
}

auto deepCopyLandmarks = [](const typename PointSetType::ConstPointer source,
typename PointSetType::Pointer destination) {
typename PointSetType::PointsContainer::ConstIterator sourceIt = source->GetPoints()->Begin();

typename PointSetType::PointIdentifier i{ 0 };
while (sourceIt != source->GetPoints()->End())
{
destination->SetPoint(i, sourceIt->Value());
++i;
++sourceIt;
}
};

// make a deep copy of the source and target landmarks
typename PointSetType::Pointer sourceLandmarks = PointSetType::New();
typename PointSetType::Pointer targetLandmarks = PointSetType::New();
Comment on lines +534 to +535
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const auto would be even nicer 😇

    const auto sourceLandmarks = PointSetType::New();
    const auto targetLandmarks = PointSetType::New();

deepCopyLandmarks(this->GetSourceLandmarks(), sourceLandmarks);
deepCopyLandmarks(this->GetTargetLandmarks(), targetLandmarks);
Comment on lines +536 to +537
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider directly copying the internal STL containers of the landmarks:

  sourceLandmarks->GetPoints()->CastToSTLContainer() =
    m_SourceLandmarks->GetPoints()->CastToSTLConstContainer();
  targetLandmarks->GetPoints()->CastToSTLContainer() =
    m_TargetLandmarks->GetPoints()->CastToSTLConstContainer();


// inversion comes from reversing the landmarks
inverseTransform->SetSourceLandmarks(targetLandmarks);
inverseTransform->SetTargetLandmarks(sourceLandmarks);
inverseTransform->SetStiffness(this->GetStiffness());
Comment on lines +540 to +542
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's OK for a class to directly access its own data members, without calling Get and Set. So more like:

  inverseTransform->m_SourceLandmarks = targetLandmarks;
  inverseTransform->m_TargetLandmarks = sourceLandmarks;
  inverseTransform->m_Stiffness = m_Stiffness;

Right?

inverseTransform->ComputeWMatrix();

return true;
}

template <typename TParametersValueType, unsigned int VDimension>
void
KernelTransform<TParametersValueType, VDimension>::PrintSelf(std::ostream & os, Indent indent) const
Expand Down
Loading