From e9ec28e404b1afd732d6e787dd0f07f8c7b47d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Tue, 27 Feb 2024 17:10:34 -0500 Subject: [PATCH 1/2] ENH: Refactor AverageAffineTransformFunction (remove NoRigid variant) --- Examples/AverageAffineTransformNoRigid.cxx | 5 +- Utilities/itkAverageAffineTransformFunction.h | 3 + .../itkAverageAffineTransformFunction.hxx | 7 +- ...itkAverageAffineTransformNoRigidFunction.h | 200 ---------- ...kAverageAffineTransformNoRigidFunction.hxx | 344 ------------------ 5 files changed, 11 insertions(+), 548 deletions(-) delete mode 100644 Utilities/itkAverageAffineTransformNoRigidFunction.h delete mode 100644 Utilities/itkAverageAffineTransformNoRigidFunction.hxx diff --git a/Examples/AverageAffineTransformNoRigid.cxx b/Examples/AverageAffineTransformNoRigid.cxx index daa8bcccc..13530b6ff 100644 --- a/Examples/AverageAffineTransformNoRigid.cxx +++ b/Examples/AverageAffineTransformNoRigid.cxx @@ -7,7 +7,7 @@ #include "itkMatrixOffsetTransformBase.h" #include "itkTransformFactory.h" -#include "itkAverageAffineTransformNoRigidFunction.h" +#include "itkAverageAffineTransformFunction.h" #include "itkTransformFileReader.h" #include "itkTransformFileWriter.h" @@ -118,7 +118,7 @@ AverageAffineTransformNoRigid(char * output_affine_txt, char * reference_affine_ // typedef itk::WarpImageMultiTransformFilter WarperType; - using WarperType = itk::AverageAffineTransformNoRigidFunction; + using WarperType = itk::AverageAffineTransformFunction; itk::TransformFactory::RegisterTransform(); @@ -129,6 +129,7 @@ AverageAffineTransformNoRigid(char * output_affine_txt, char * reference_affine_ // typename ImageFileReaderType::Pointer reader_img_ref = ImageFileReaderType::New(); WarperType average_func; + average_func.useRigid = false; // warper->SetInput(img_mov); // warper->SetEdgePaddingValue( 0); // VectorType pad; diff --git a/Utilities/itkAverageAffineTransformFunction.h b/Utilities/itkAverageAffineTransformFunction.h index 0f1118ffa..24233670f 100644 --- a/Utilities/itkAverageAffineTransformFunction.h +++ b/Utilities/itkAverageAffineTransformFunction.h @@ -160,6 +160,9 @@ class AverageAffineTransformFunction void AverageMultipleAffineTransform(const PointType & center_output, GenericAffineTransformPointerType & affine_output); + /** Set to false to ignore rotation and translation. */ + bool useRigid = true; + #ifdef ITK_USE_CONCEPT_CHECKING /** Begin concept checking */ itkConceptMacro(SameDimensionCheck1, (Concept::SameDimension)); diff --git a/Utilities/itkAverageAffineTransformFunction.hxx b/Utilities/itkAverageAffineTransformFunction.hxx index 11b6e011c..c252206c7 100644 --- a/Utilities/itkAverageAffineTransformFunction.hxx +++ b/Utilities/itkAverageAffineTransformFunction.hxx @@ -100,8 +100,11 @@ AverageAffineTransformFunction::AverageMultipleAffineTransform( HelperType::ComputeAverageScaleParameters(m_InternalTransformList, average_parameters); HelperType::ComputeAverageShearingParameters(m_InternalTransformList, average_parameters); - HelperType::ComputeAverageRotationParameters(m_InternalTransformList, average_parameters); - HelperType::ComputeAverageTranslationParameters(m_InternalTransformList, average_parameters); + if (useRigid) + { + HelperType::ComputeAverageRotationParameters(m_InternalTransformList, average_parameters); + HelperType::ComputeAverageTranslationParameters(m_InternalTransformList, average_parameters); + } average_iaff->SetParameters(average_parameters); average_iaff->SetCenter(reference_center); diff --git a/Utilities/itkAverageAffineTransformNoRigidFunction.h b/Utilities/itkAverageAffineTransformNoRigidFunction.h deleted file mode 100644 index c84899fc2..000000000 --- a/Utilities/itkAverageAffineTransformNoRigidFunction.h +++ /dev/null @@ -1,200 +0,0 @@ -/*========================================================================= - - Program: Advanced Normalization Tools - - Copyright (c) ConsortiumOfANTS. All rights reserved. - See accompanying COPYING.txt or - https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notices for more information. - - =========================================================================*/ -#ifndef __itkAverageAffineTransformNoRigidFunction_h -#define __itkAverageAffineTransformNoRigidFunction_h - -#include "itkMacro.h" -#include "itkConceptChecking.h" - -#include "itkANTSCenteredAffine2DTransform.h" -#include "itkANTSAffine3DTransform.h" - -#include - -namespace itk -{ -/* - */ - -namespace AverageAffineTransformNoRigidFunctionHelperNameSpace -{ -template -struct Dispatcher -{}; - -template -struct HelperCommonType -{ - typedef TAffine InternalAffineTransformType; - - typedef typename InternalAffineTransformType::Pointer InternalAffineTransformPointerType; - typedef struct - { - InternalAffineTransformPointerType aff; - double weight; - } SingleInternalTransformItemType; - - typedef std::list InternalTransformListType; - typedef typename InternalAffineTransformType::ParametersType ParametersType; - - static void - ComputeAveragePartialParameters(InternalTransformListType & transform_list, - ParametersType & average_parameters, - unsigned int iStart, - unsigned int iEnd); -}; - -template -class HelperType; - -// { -// // purposely not include any types -// }; - -// explicit specialization for 2D affine transform -template -class HelperType, TParametersValueType> -{ -public: - typedef ANTSCenteredAffine2DTransform InternalAffineTransformType; - - typedef typename HelperCommonType::InternalAffineTransformPointerType - InternalAffineTransformPointerType; - typedef typename HelperCommonType::SingleInternalTransformItemType - SingleInternalTransformItemType; - typedef typename HelperCommonType::InternalTransformListType InternalTransformListType; - typedef typename HelperCommonType::ParametersType ParametersType; - - static void - ComputeAverageScaleParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); - - static void - ComputeAverageShearingParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); - - static void - ComputeAverageRotationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); - - static void - ComputeAverageTranslationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); -}; - -// explicit specialization for 3D affine transform -template -class HelperType, TParametersValueType> -{ -public: - typedef ANTSAffine3DTransform InternalAffineTransformType; - - typedef typename HelperCommonType::InternalAffineTransformPointerType - InternalAffineTransformPointerType; - typedef typename HelperCommonType::SingleInternalTransformItemType - SingleInternalTransformItemType; - typedef typename HelperCommonType::InternalTransformListType InternalTransformListType; - typedef typename HelperCommonType::ParametersType ParametersType; - - static void - ComputeAverageScaleParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); - - static void - ComputeAverageShearingParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); - - static void - ComputeAverageRotationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); - - static void - ComputeAverageTranslationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); -}; -} // namespace AverageAffineTransformNoRigidFunctionHelperNameSpace - -template -class AverageAffineTransformNoRigidFunction -{ -public: - typedef TTransform GenericAffineTransformType; - - static constexpr unsigned int InputSpaceDimension = GenericAffineTransformType::InputSpaceDimension; - static constexpr unsigned int OutputSpaceDimension = GenericAffineTransformType::OutputSpaceDimension; - static constexpr unsigned int SpaceDimension = InputSpaceDimension; - - typedef typename TTransform::ParametersValueType InternalScalarType; - typedef Point PointType; - - AverageAffineTransformNoRigidFunction(); - ~AverageAffineTransformNoRigidFunction() = default; - - // void PrintSelf(std::ostream& os, Indent indent) const; - - typedef typename GenericAffineTransformType::Pointer GenericAffineTransformPointerType; - - typedef struct - { - GenericAffineTransformPointerType aff; - double weight; - } SingleTransformItemType; - - typedef std::list TransformListType; - - TransformListType & - GetTransformList() - { - return m_TransformList; - } - - void - PrintTransformList(); - - void - PushBackAffineTransform(const GenericAffineTransformType * t, double weight); - - void - AverageMultipleAffineTransform(const PointType & center_output, GenericAffineTransformPointerType & affine_output); - -#ifdef ITK_USE_CONCEPT_CHECKING - /** Begin concept checking */ - itkConceptMacro(SameDimensionCheck1, (Concept::SameDimension)); - /** End concept checking */ -#endif -protected: - TransformListType m_TransformList; - - // type declaration to include support both 2D and 3D affine transform -protected: - typedef typename ::itk::AverageAffineTransformNoRigidFunctionHelperNameSpace::HelperType< - ::itk::AverageAffineTransformNoRigidFunctionHelperNameSpace::Dispatcher, - InternalScalarType> - HelperType; - - typedef typename HelperType::InternalAffineTransformType InternalAffineTransformType; - typedef typename HelperType::InternalAffineTransformPointerType InternalAffineTransformPointerType; - typedef typename HelperType::SingleInternalTransformItemType SingleInternalTransformItemType; - typedef typename HelperType::InternalTransformListType InternalTransformListType; - - InternalTransformListType m_InternalTransformList; - - void - ConvertGenericAffineToInternalAffineByFixingCenter(GenericAffineTransformPointerType & aff, - InternalAffineTransformPointerType & iaff, - const PointType & center); - - void - ConvertInternalAffineToGenericAffine(InternalAffineTransformPointerType & iaff, - GenericAffineTransformPointerType & aff); -}; -} // end namespace itk - -#ifndef ITK_MANUAL_INSTANTIATION -# include "itkAverageAffineTransformNoRigidFunction.hxx" -#endif - -#endif /*__itkAverageAffineTransformNoRigidFunction_h*/ diff --git a/Utilities/itkAverageAffineTransformNoRigidFunction.hxx b/Utilities/itkAverageAffineTransformNoRigidFunction.hxx deleted file mode 100644 index 636e94fc4..000000000 --- a/Utilities/itkAverageAffineTransformNoRigidFunction.hxx +++ /dev/null @@ -1,344 +0,0 @@ -/*========================================================================= - - Program: Advanced Normalization Tools - - Copyright (c) ConsortiumOfANTS. All rights reserved. - See accompanying COPYING.txt or - https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt for details. - - This software is distributed WITHOUT ANY WARRANTY; without even - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - PURPOSE. See the above copyright notices for more information. - - =========================================================================*/ -#ifndef __itkAverageAffineTransformNoRigidFunction_hxx -#define __itkAverageAffineTransformNoRigidFunction_hxx - -#include "itkNumericTraits.h" -#include - -namespace itk -{ -/** - * Default constructor. - */ -template -AverageAffineTransformNoRigidFunction::AverageAffineTransformNoRigidFunction() = default; - -template -void -AverageAffineTransformNoRigidFunction::PrintTransformList() -{ - std::cout << "transform list: " << std::endl; - - typename TransformListType::iterator it = (m_TransformList.begin()); - for (int ii = 0; it != m_TransformList.end(); it++, ii++) - { - std::cout << '[' << ii << ":" << it->weight << "]:" << it->aff << std::endl; - } -} - -// /** -// * Standard PrintSelf method. -// */ -// template -// void -// WarpImageMultiTransformFilter -// ::PrintSelf(std::ostream& os, Indent indent) const -// { -// -// Superclass::PrintSelf(os, indent); -// } - -template -void -AverageAffineTransformNoRigidFunction::PushBackAffineTransform(const GenericAffineTransformType * t, - double weight) -{ - if (t) - { - SingleTransformItemType item; - item.aff = const_cast(t); - item.weight = weight; - m_TransformList.push_back(SingleTransformItemType(item)); - } -} - -template -void -AverageAffineTransformNoRigidFunction::AverageMultipleAffineTransform( - const PointType & reference_center, - GenericAffineTransformPointerType & affine_output) -{ - // std::cout << "test " ; - // TransformTypePointer affine_output = TransformType::New(); - - affine_output->SetIdentity(); - affine_output->SetCenter(reference_center); - - unsigned int number_of_affine = m_TransformList.size(); - - number_of_affine--; - - // std::cout << affine_output; - - typename TransformListType::iterator it = m_TransformList.begin(); - - // typename InternalAffineTransformType::InputPointType center_ref = m_ReferenceCenter; - typename InternalAffineTransformType::Pointer average_iaff = InternalAffineTransformType::New(); - - typename InternalAffineTransformType::ParametersType average_parameters = average_iaff->GetParameters(); - for (; it != m_TransformList.end(); it++) - { - SingleInternalTransformItemType internal_item; - internal_item.aff = InternalAffineTransformType::New(); - ConvertGenericAffineToInternalAffineByFixingCenter(it->aff, internal_item.aff, reference_center); - internal_item.weight = it->weight; - m_InternalTransformList.push_back(internal_item); - - std::cout << "internal_transform: " << internal_item.aff << std::endl; - } - - HelperType::ComputeAverageScaleParameters(m_InternalTransformList, average_parameters); - HelperType::ComputeAverageShearingParameters(m_InternalTransformList, average_parameters); - // HelperType::ComputeAverageRotationParameters(m_InternalTransformList, - // average_parameters); - // HelperType::ComputeAverageTranslationParameters(m_InternalTransformList, - // average_parameters); - - average_iaff->SetParameters(average_parameters); - average_iaff->SetCenter(reference_center); - - std::cout << "average_iaff" << average_iaff << std::endl; - - ConvertInternalAffineToGenericAffine(average_iaff, affine_output); - - std::cout << "affine_output" << affine_output << std::endl; - return; -} - -template -void -AverageAffineTransformNoRigidFunction::ConvertGenericAffineToInternalAffineByFixingCenter( - GenericAffineTransformPointerType & aff, - InternalAffineTransformPointerType & iaff, - const PointType & center) -{ - iaff->SetCenter(center); - iaff->SetMatrix(aff->GetMatrix()); - iaff->SetTranslation(aff->GetTranslation()); - - return; -} - -template -void -AverageAffineTransformNoRigidFunction::ConvertInternalAffineToGenericAffine( - InternalAffineTransformPointerType & iaff, - GenericAffineTransformPointerType & aff) -{ - aff->SetCenter(iaff->GetCenter()); - aff->SetTranslation(iaff->GetTranslation()); - aff->SetMatrix(iaff->GetMatrix()); - - return; -} - -namespace AverageAffineTransformNoRigidFunctionHelperNameSpace -{ -template -void -HelperCommonType::ComputeAveragePartialParameters(InternalTransformListType & transform_list, - ParametersType & average_parameters, - unsigned int istart, - unsigned int iend) -{ - double w = 0.0; - - // initialize partial parameters to zero - for (unsigned int k = istart; k <= iend; k++) - { - average_parameters[k] = 0.0; - } - - typename InternalTransformListType::iterator it = transform_list.begin(); - unsigned int cnt = 0; - for (; it != transform_list.end(); it++) - { - ParametersType current_parameters = it->aff->GetParameters(); - w += it->weight; - - std::cout << "[" << cnt++ << "]:" << it->weight << "\t"; - for (unsigned int k = istart; k <= iend; k++) - { - average_parameters[k] += it->weight * current_parameters[k]; - - std::cout << current_parameters[k] << " "; - } - - std::cout << std::endl; - } - - if (w <= 0.0) - { - std::cout << "Total weight smaller than 0!!!" << std::endl; - std::exception(); - } - - // normalize by weight - std::cout << "sum:w=" << w << "\t"; - for (unsigned int k = istart; k <= iend; k++) - { - std::cout << average_parameters[k] << " "; - } - std::cout << std::endl; - - // normalize by weight - std::cout << "average" - << "\t"; - for (unsigned int k = istart; k <= iend; k++) - { - average_parameters[k] /= w; - std::cout << average_parameters[k] << " "; - } - - std::cout << std::endl; - return; -} - -template -void -HelperType, TParametersValueType>::ComputeAverageScaleParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 1; - unsigned int iend = 2; - - std::cout << "average 2D scale parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); -} - -template -void -HelperType, TParametersValueType>::ComputeAverageShearingParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 3; - unsigned int iend = 3; - - std::cout << "average 2D shearing parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); -} - -template -void -HelperType, TParametersValueType>::ComputeAverageRotationParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 0; - unsigned int iend = 0; - - std::cout << "average 2D rotation parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); -} - -template -void -HelperType, TParametersValueType>::ComputeAverageTranslationParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 6; - unsigned int iend = 7; - - std::cout << "average 2D translation parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); -} - -template -void -HelperType, TParametersValueType>::ComputeAverageScaleParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 4; - unsigned int iend = 6; - - std::cout << "average 3D scale parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); -} - -template -void -HelperType, TParametersValueType>::ComputeAverageShearingParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 7; - unsigned int iend = 9; - - std::cout << "average 3D shearing parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); -} - -template -void -HelperType, TParametersValueType>::ComputeAverageRotationParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 0; - unsigned int iend = 3; - - std::cout << "average 3D rotation parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); - - // extra normalization for quaternion - - double quat_mag = 0.0; - for (unsigned int j = istart; j <= iend; j++) - { - quat_mag += average_parameters[j] * average_parameters[j]; - } - quat_mag = sqrt(quat_mag); - for (unsigned int j = 0; j < 4; j++) - { - average_parameters[j] /= quat_mag; - } -} - -template -void -HelperType, TParametersValueType>::ComputeAverageTranslationParameters( - InternalTransformListType & transform_list, - ParametersType & average_parameters) -{ - unsigned int istart = 10; - unsigned int iend = 12; - - std::cout << "average 3D translation parameter " << std::endl; - - HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); -} -} // end namespace AverageAffineTransformNoRigidFunctionHelperNameSpace -} // end namespace itk - -#endif // __itkAverageAffineTransformNoRigidFunction_hxx From 911eadfd8d113fee797d692e39d68b7a8d409c68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C5=BEenan=20Zuki=C4=87?= Date: Tue, 27 Feb 2024 17:19:25 -0500 Subject: [PATCH 2/2] ENH: Control AverageAffineTransformFunction's verbosity via an ivar Verbose is off by default in the class. Executables turn on verbosity for backwards compatibility. --- Examples/AverageAffineTransform.cxx | 1 + Examples/AverageAffineTransformNoRigid.cxx | 1 + Utilities/itkAverageAffineTransformFunction.h | 21 ++- .../itkAverageAffineTransformFunction.hxx | 156 ++++++++++++------ 4 files changed, 118 insertions(+), 61 deletions(-) diff --git a/Examples/AverageAffineTransform.cxx b/Examples/AverageAffineTransform.cxx index 97a49dc07..74137d853 100644 --- a/Examples/AverageAffineTransform.cxx +++ b/Examples/AverageAffineTransform.cxx @@ -129,6 +129,7 @@ AverageAffineTransform(char * output_affine_txt, char * reference_affine_txt, TR // typename ImageFileReaderType::Pointer reader_img_ref = ImageFileReaderType::New(); WarperType average_func; + average_func.verbose = true; // warper->SetInput(img_mov); // warper->SetEdgePaddingValue( 0); // VectorType pad; diff --git a/Examples/AverageAffineTransformNoRigid.cxx b/Examples/AverageAffineTransformNoRigid.cxx index 13530b6ff..056f62fb3 100644 --- a/Examples/AverageAffineTransformNoRigid.cxx +++ b/Examples/AverageAffineTransformNoRigid.cxx @@ -129,6 +129,7 @@ AverageAffineTransformNoRigid(char * output_affine_txt, char * reference_affine_ // typename ImageFileReaderType::Pointer reader_img_ref = ImageFileReaderType::New(); WarperType average_func; + average_func.verbose = true; average_func.useRigid = false; // warper->SetInput(img_mov); // warper->SetEdgePaddingValue( 0); diff --git a/Utilities/itkAverageAffineTransformFunction.h b/Utilities/itkAverageAffineTransformFunction.h index 24233670f..8d78c7140 100644 --- a/Utilities/itkAverageAffineTransformFunction.h +++ b/Utilities/itkAverageAffineTransformFunction.h @@ -52,7 +52,7 @@ struct HelperCommonType ComputeAveragePartialParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, unsigned int iStart, - unsigned int iEnd); + unsigned int iEnd, bool verbose); }; template @@ -77,16 +77,16 @@ class HelperType, TParametersValueType> typedef typename HelperCommonType::ParametersType ParametersType; static void - ComputeAverageScaleParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageScaleParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); static void - ComputeAverageShearingParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageShearingParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); static void - ComputeAverageRotationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageRotationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); static void - ComputeAverageTranslationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageTranslationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); }; // explicit specialization for 3D affine transform @@ -104,16 +104,16 @@ class HelperType, TParametersValueType> typedef typename HelperCommonType::ParametersType ParametersType; static void - ComputeAverageScaleParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageScaleParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); static void - ComputeAverageShearingParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageShearingParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); static void - ComputeAverageRotationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageRotationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); static void - ComputeAverageTranslationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters); + ComputeAverageTranslationParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, bool verbose); }; } // namespace AverageAffineTransformFunctionHelperNameSpace @@ -160,6 +160,9 @@ class AverageAffineTransformFunction void AverageMultipleAffineTransform(const PointType & center_output, GenericAffineTransformPointerType & affine_output); + /** Whether progress and debugging information are printed to standard output (cout). */ + bool verbose = false; + /** Set to false to ignore rotation and translation. */ bool useRigid = true; diff --git a/Utilities/itkAverageAffineTransformFunction.hxx b/Utilities/itkAverageAffineTransformFunction.hxx index c252206c7..a5e854d99 100644 --- a/Utilities/itkAverageAffineTransformFunction.hxx +++ b/Utilities/itkAverageAffineTransformFunction.hxx @@ -69,8 +69,7 @@ AverageAffineTransformFunction::AverageMultipleAffineTransform( const PointType & reference_center, GenericAffineTransformPointerType & affine_output) { - // std::cout << "test " ; - // TransformTypePointer affine_output = TransformType::New(); + // TransformTypePointer affine_output = TransformType::New(); affine_output->SetIdentity(); affine_output->SetCenter(reference_center); @@ -79,11 +78,13 @@ AverageAffineTransformFunction::AverageMultipleAffineTransform( number_of_affine--; - // std::cout << affine_output; + // if (verbose) + // { + // std::cout << affine_output; + // } typename TransformListType::iterator it = m_TransformList.begin(); - // typename InternalAffineTransformType::InputPointType center_ref = m_ReferenceCenter; typename InternalAffineTransformType::Pointer average_iaff = InternalAffineTransformType::New(); typename InternalAffineTransformType::ParametersType average_parameters = average_iaff->GetParameters(); @@ -95,25 +96,34 @@ AverageAffineTransformFunction::AverageMultipleAffineTransform( internal_item.weight = it->weight; m_InternalTransformList.push_back(internal_item); - std::cout << "internal_transform: " << internal_item.aff << std::endl; + if (verbose) + { + std::cout << "internal_transform: " << internal_item.aff << std::endl; + } } - HelperType::ComputeAverageScaleParameters(m_InternalTransformList, average_parameters); - HelperType::ComputeAverageShearingParameters(m_InternalTransformList, average_parameters); + HelperType::ComputeAverageScaleParameters(m_InternalTransformList, average_parameters, verbose); + HelperType::ComputeAverageShearingParameters(m_InternalTransformList, average_parameters, verbose); if (useRigid) { - HelperType::ComputeAverageRotationParameters(m_InternalTransformList, average_parameters); - HelperType::ComputeAverageTranslationParameters(m_InternalTransformList, average_parameters); + HelperType::ComputeAverageRotationParameters(m_InternalTransformList, average_parameters, verbose); + HelperType::ComputeAverageTranslationParameters(m_InternalTransformList, average_parameters, verbose); } average_iaff->SetParameters(average_parameters); average_iaff->SetCenter(reference_center); - std::cout << "average_iaff" << average_iaff << std::endl; + if (verbose) + { + std::cout << "average_iaff" << average_iaff << std::endl; + } ConvertInternalAffineToGenericAffine(average_iaff, affine_output); - std::cout << "affine_output" << affine_output << std::endl; + if (verbose) + { + std::cout << "affine_output" << affine_output << std::endl; + } return; } @@ -151,7 +161,7 @@ void HelperCommonType::ComputeAveragePartialParameters(InternalTransformListType & transform_list, ParametersType & average_parameters, unsigned int istart, - unsigned int iend) + unsigned int iend, bool verbose) { double w = 0.0; @@ -167,42 +177,58 @@ HelperCommonType::ComputeAveragePartialParameters(InternalTransformList { ParametersType current_parameters = it->aff->GetParameters(); w += it->weight; - - std::cout << "[" << cnt++ << "]:" << it->weight << "\t"; + ++cnt; for (unsigned int k = istart; k <= iend; k++) { average_parameters[k] += it->weight * current_parameters[k]; - - std::cout << current_parameters[k] << " "; } - std::cout << std::endl; + if (verbose) + { + std::cout << "[" << cnt << "]:" << it->weight << "\t"; + for (unsigned int k = istart; k <= iend; k++) + { + std::cout << current_parameters[k] << " "; + } + std::cout << std::endl; + } } if (w <= 0.0) { - std::cout << "Total weight smaller than 0!!!" << std::endl; - std::exception(); + if (verbose) + { + std::cout << "Total weight smaller than 0!!!" << std::endl; + } + // throw std::runtime_error("Total weight smaller than 0"); } - // normalize by weight - std::cout << "sum:w=" << w << "\t"; - for (unsigned int k = istart; k <= iend; k++) + if (verbose) { - std::cout << average_parameters[k] << " "; + std::cout << "sum:w=" << w << "\t"; + for (unsigned int k = istart; k <= iend; k++) + { + std::cout << average_parameters[k] << " "; + } + std::cout << std::endl; } - std::cout << std::endl; // normalize by weight - std::cout << "average" - << "\t"; for (unsigned int k = istart; k <= iend; k++) { average_parameters[k] /= w; - std::cout << average_parameters[k] << " "; } - std::cout << std::endl; + if (verbose) + { + std::cout << "average\t"; + for (unsigned int k = istart; k <= iend; k++) + { + std::cout << average_parameters[k] << " "; + } + std::cout << std::endl; + } + return; } @@ -210,105 +236,127 @@ template void HelperType, TParametersValueType>::ComputeAverageScaleParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, bool verbose) { unsigned int istart = 1; unsigned int iend = 2; - std::cout << "average 2D scale parameter " << std::endl; + if (verbose) + { + std::cout << "average 2D scale parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); } template void HelperType, TParametersValueType>::ComputeAverageShearingParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, bool verbose) { unsigned int istart = 3; unsigned int iend = 3; - std::cout << "average 2D shearing parameter " << std::endl; + if (verbose) + { + std::cout << "average 2D shearing parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); } template void HelperType, TParametersValueType>::ComputeAverageRotationParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, bool verbose) { unsigned int istart = 0; unsigned int iend = 0; - std::cout << "average 2D rotation parameter " << std::endl; + if (verbose) + { + std::cout << "average 2D rotation parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); } template void HelperType, TParametersValueType>::ComputeAverageTranslationParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, bool verbose) { unsigned int istart = 6; unsigned int iend = 7; - std::cout << "average 2D translation parameter " << std::endl; + if (verbose) + { + std::cout << "average 2D translation parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); } template void HelperType, TParametersValueType>::ComputeAverageScaleParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, bool verbose) { unsigned int istart = 4; unsigned int iend = 6; - std::cout << "average 3D scale parameter " << std::endl; + if (verbose) + { + std::cout << "average 3D scale parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); } template void HelperType, TParametersValueType>::ComputeAverageShearingParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, bool verbose) { unsigned int istart = 7; unsigned int iend = 9; - std::cout << "average 3D shearing parameter " << std::endl; + if (verbose) + { + std::cout << "average 3D shearing parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); } template void HelperType, TParametersValueType>::ComputeAverageRotationParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, + bool verbose) { unsigned int istart = 0; unsigned int iend = 3; - std::cout << "average 3D rotation parameter " << std::endl; + if (verbose) + { + std::cout << "average 3D rotation parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); // extra normalization for quaternion @@ -328,15 +376,19 @@ template void HelperType, TParametersValueType>::ComputeAverageTranslationParameters( InternalTransformListType & transform_list, - ParametersType & average_parameters) + ParametersType & average_parameters, + bool verbose) { unsigned int istart = 10; unsigned int iend = 12; - std::cout << "average 3D translation parameter " << std::endl; + if (verbose) + { + std::cout << "average 3D translation parameter " << std::endl; + } HelperCommonType::ComputeAveragePartialParameters( - transform_list, average_parameters, istart, iend); + transform_list, average_parameters, istart, iend, verbose); } } // end namespace AverageAffineTransformFunctionHelperNameSpace } // end namespace itk