Skip to content
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
4 changes: 4 additions & 0 deletions Core/ComponentBaseClasses/elxTransformBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,10 @@ class ITK_TEMPLATE_EXPORT TransformBase : public BaseComponentSE<TElastix>
void
ReadInitialTransformFromConfiguration(const Configuration::ConstPointer);

/** Read an initial ITK transform from a pointer. */
void
ReadInitialItkTransformFromConfiguration(const itk::TransformBase::ConstPointer itkTransform);

/** Execute stuff before everything else:
* \li Check the appearance of an initial transform.
*/
Expand Down
67 changes: 62 additions & 5 deletions Core/ComponentBaseClasses/elxTransformBase.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,77 @@ TransformBase<TElastix>::ReadInitialTransformFromFile(const std::string & transf
* the transformParameterFileName. */
const auto configurationInitialTransform = Configuration::New();

if (configurationInitialTransform->Initialize({ { "-tp", transformParameterFileName } }) != 0)
if (transformParameterFileName.size() >= 8 &&
transformParameterFileName.compare(transformParameterFileName.size() - 8, 8, ".itk.txt") == 0)
{
itkGenericExceptionMacro("ERROR: Reading initial transform parameters failed: " << transformParameterFileName);
this->ReadInitialItkTransformFromConfiguration(TransformIO::Read(transformParameterFileName));
}
else
{
if (configurationInitialTransform->Initialize({ { "-tp", transformParameterFileName } }) != 0)
{
itkGenericExceptionMacro("ERROR: Reading initial transform parameters failed: " << transformParameterFileName);
}
this->ReadInitialTransformFromConfiguration(configurationInitialTransform);
}
} // end ReadInitialTransformFromFile()

this->ReadInitialTransformFromConfiguration(configurationInitialTransform);
/**
* \brief Read an initial ITK transform (e.g. from a .itk.txt file) and wrap it as an elastix-compatible transform.
*
* This function is used when the user provides an ITK transform file via the `-t0` argument
* (e.g. a file saved using `TransformIO::Write`). It wraps the transform into an elastix-compatible
* component and registers it as the initial transform.
*
* \param itkTransform The ITK transform to use as initial transform.
*/
template <typename TElastix>
void
TransformBase<TElastix>::ReadInitialItkTransformFromConfiguration(const itk::TransformBase::ConstPointer itkTransform)
{
if (!itkTransform)
{
itkExceptionMacro("ERROR: Provided ITK transform is null.");
}

} // end ReadInitialTransformFromFile()
const std::string elastixClassName =
TransformIO::ConvertITKNameOfClassToElastixClassName(itkTransform->GetNameOfClass());

const PtrToCreator creator =
ElastixMain::GetComponentDatabase().GetCreator(elastixClassName, this->m_Elastix->GetDBIndex());

if (!creator)
{
itkExceptionMacro("ERROR: No elastix component found for ITK transform class: " << itkTransform->GetNameOfClass());
}

const itk::Object::Pointer elxTransformObject = creator();

auto * const elxTransform = dynamic_cast<Self *>(elxTransformObject.GetPointer());
if (!elxTransform)
{
itkExceptionMacro("ERROR: Created component is not a valid elastix transform.");
}

elxTransform->SetElastix(this->GetElastix());
elxTransform->SetConfiguration(this->GetConfiguration());

elxTransform->SetReadWriteTransformParameters(false);
elxTransform->SetTransformParameterFileName(""); // avoid overwriting

const auto & optimizerParams = itkTransform->GetParameters();
elxTransform->GetAsITKBaseType()->SetParameters(optimizerParams);

const auto fixedParams = itkTransform->GetFixedParameters();
elxTransform->GetAsITKBaseType()->SetFixedParameters(fixedParams);

this->SetInitialTransform(elxTransform->GetAsITKBaseType());
}


/**
* ******************* ReadInitialTransformFromConfiguration *****************************
*/

template <typename TElastix>
void
TransformBase<TElastix>::ReadInitialTransformFromConfiguration(
Expand Down
Loading