1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://www.centreleonberard.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ===========================================================================**/
18 #ifndef clitkBLUTDIRCommandIterationUpdateDVF_h
19 #define clitkBLUTDIRCommandIterationUpdateDVF_h
21 /* =================================================
22 * @file clitkBLUTDIRCommandIterationUpdateDVF.h
23 * @author Romulo Pinho
26 * @brief ITK observer class to respond to iteration events of B-LUT DIR. This
27 * observer is meant to output the DVF of the registration at each iteration.
29 ===================================================*/
31 #include "clitkBLUTDIRGenericFilter.h"
35 template <class FixedImageType, class OptimizerType, class TransformType>
36 class CommandIterationUpdateDVF : public itk::Command
39 typedef CommandIterationUpdateDVF Self;
40 typedef itk::Command Superclass;
41 typedef itk::SmartPointer<Self> Pointer;
44 CommandIterationUpdateDVF() : m_Iteration(0) {};
47 typedef typename FixedImageType::Pointer ImagePointer;
48 typedef typename TransformType::Pointer TransformPointer;
50 // Set the fixed image, from the specs of the DVF are obtained.
51 void SetFixedImage(ImagePointer i) { m_FixedImage = i; }
54 void SetTransform(TransformPointer t){ m_Transform = t;}
56 // Set command line parameters
57 void SetArgsInfo(args_info_clitkBLUTDIR a){m_ArgsInfo=a;}
60 void Execute(itk::Object *object, const itk::EventObject & event)
62 //Execute( (const itk::Object *)caller, event);
63 if( !(itk::IterationEvent().CheckEvent( &event )) )
65 std::cout << "CommandIterationUpdateDVF::Execute(): check event failed!\n";
69 std::cout << "CommandIterationUpdateDVF::Execute()\n";
71 typedef typename OptimizerType::ParametersType ParametersType;
72 OptimizerType* optmizer = dynamic_cast<OptimizerType*>(object);
73 ParametersType parameters = optmizer->GetCurrentPosition();
74 std::cout << "Iteration " << ++m_Iteration << endl;// << " -> Optimizer parameters: " << parameters << endl;
76 //=======================================================
77 // RP: Copy-paste from BLUTDIRGenericFilter::UpdateWithInputImageType()
78 //=======================================================
79 //=======================================================
80 // Get the BSpline coefficient images and write them
81 //=======================================================
82 int niter = m_ArgsInfo.coeffEveryN_arg;
83 if ((m_Iteration % niter == 0) && m_ArgsInfo.coeff_given)
85 typedef typename TransformType::CoefficientImageType CoefficientImageType;
86 std::vector<typename CoefficientImageType::Pointer> coefficientImages = m_Transform->GetCoefficientImages();
87 typedef itk::ImageFileWriter<CoefficientImageType> CoeffWriterType;
88 typename CoeffWriterType::Pointer coeffWriter = CoeffWriterType::New();
89 unsigned nLabels = m_Transform->GetnLabels();
91 std::string fname(m_ArgsInfo.coeff_arg);
92 int dotpos = fname.length() - 1;
93 while (dotpos >= 0 && fname[dotpos] != '.')
96 for (unsigned i = 0; i < nLabels; ++i)
98 std::ostringstream osfname;
99 osfname << fname.substr(0, dotpos) << '_' << i << '_' << m_Iteration << fname.substr(dotpos);
100 coeffWriter->SetInput(coefficientImages[i]);
101 coeffWriter->SetFileName(osfname.str());
102 coeffWriter->Update();
107 void Execute(const itk::Object * object, const itk::EventObject & event)
109 std::cout << "const CommandIterationUpdateDVF::Execute()\n";
112 unsigned int m_Iteration;
113 ImagePointer m_FixedImage;
114 TransformPointer m_Transform;
115 args_info_clitkBLUTDIR m_ArgsInfo;
119 #endif // clitkBLUTDIRCommandIterationUpdateDVF_h