]> Creatis software - clitk.git/blob - registration/clitkBLUTDIRCommandIterationUpdateDVF.h
Debug RTStruct conversion with empty struc
[clitk.git] / registration / clitkBLUTDIRCommandIterationUpdateDVF.h
1 /*=========================================================================
2 Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4 Authors belong to:
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
8
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.
12
13 It is distributed under dual licence
14
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
20
21 /* =================================================
22  * @file   clitkBLUTDIRCommandIterationUpdateDVF.h
23  * @author      Romulo Pinho
24  * @date        16/02/2011
25  *
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.
28  *
29  ===================================================*/
30
31 #include "clitkBLUTDIRGenericFilter.h"
32
33 namespace clitk
34 {
35   template <class FixedImageType, class OptimizerType, class TransformType>
36   class CommandIterationUpdateDVF : public itk::Command
37   {
38     public:
39       typedef  CommandIterationUpdateDVF   Self;
40       typedef  itk::Command             Superclass;
41       typedef  itk::SmartPointer<Self>  Pointer;
42       itkNewMacro( Self );
43     protected:
44       CommandIterationUpdateDVF() : m_Iteration(0) {};
45     public:
46
47       typedef typename FixedImageType::Pointer ImagePointer;
48       typedef typename TransformType::Pointer TransformPointer;
49
50       // Set the fixed image, from the specs of the DVF are obtained.
51       void SetFixedImage(ImagePointer i) { m_FixedImage = i; }
52
53       // Set the transform
54       void SetTransform(TransformPointer t){ m_Transform = t;}
55
56       // Set command line parameters
57       void SetArgsInfo(args_info_clitkBLUTDIR a){m_ArgsInfo=a;}
58
59       // Execute
60       void Execute(itk::Object *object, const itk::EventObject & event)
61       {
62         //Execute( (const itk::Object *)caller, event);
63        if( !(itk::IterationEvent().CheckEvent( &event )) )
64         {
65           std::cout << "CommandIterationUpdateDVF::Execute(): check event failed!\n";
66           return;
67         }
68
69         std::cout << "CommandIterationUpdateDVF::Execute()\n";
70
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;
75
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)
84         {
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();
90
91           std::string fname(m_ArgsInfo.coeff_arg);
92           int dotpos = fname.length() - 1;
93           while (dotpos >= 0 && fname[dotpos] != '.')
94             dotpos--;
95
96           for (unsigned i = 0; i < nLabels; ++i)
97           {
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();
103           }
104         }
105       }
106
107       void Execute(const itk::Object * object, const itk::EventObject & event)
108       {
109         std::cout << "const CommandIterationUpdateDVF::Execute()\n";
110       }
111
112       unsigned int m_Iteration;
113       ImagePointer m_FixedImage;
114       TransformPointer m_Transform;
115       args_info_clitkBLUTDIR m_ArgsInfo;
116   };
117 }
118
119 #endif // clitkBLUTDIRCommandIterationUpdateDVF_h