From 1d75664d7d2bcac9ff9c80b11281a9bce6d86212 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Thu, 16 Mar 2023 14:06:03 +0100 Subject: [PATCH] ENH: Find previous configuration without interpreting filename as index Allowed `TransformBase::ReadFromFile()` to find the previous configuration (when ElastixBase has multiple configurations) without having to reinterpret the value of "InitialTransformParametersFileName" as an index into the vector of configurations. This could replace the hack introduced with commit f9a51b81bfb7d631b75c9ceba1c4638787f72158 (June 13, 2013) --- .../ComponentBaseClasses/elxTransformBase.hxx | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Core/ComponentBaseClasses/elxTransformBase.hxx b/Core/ComponentBaseClasses/elxTransformBase.hxx index 1ef61c9bd..9862616bd 100644 --- a/Core/ComponentBaseClasses/elxTransformBase.hxx +++ b/Core/ComponentBaseClasses/elxTransformBase.hxx @@ -336,8 +336,27 @@ TransformBase::ReadFromFile() std::string fileName = "NoInitialTransform"; configuration.ReadParameter(fileName, "InitialTransformParametersFileName", 0); + const ElastixBase & elastixBase = Deref(Superclass::GetElastix()); + /** Call the function ReadInitialTransformFromFile. */ - if (fileName != "NoInitialTransform") + if (fileName == "NoInitialTransform") + { + const auto numberOfConfigurations = elastixBase.GetNumberOfConfigurations(); + + if ((numberOfConfigurations > 1) && (&configuration != elastixBase.GetConfiguration(0))) + { + for (size_t index{ 1 }; index < numberOfConfigurations; ++index) + { + if (elastixBase.GetConfiguration(index) == &configuration) + { + // Use the previous configuration (at position index - 1) for the initial transform. + this->ReadInitialTransformFromConfiguration(elastixBase.GetConfiguration(index - 1)); + break; + } + } + } + } + else { // The value of "InitialTransformParametersFileName" is either an index // (size_t) into the vector of configuration objects, or the file name of