]> Creatis software - clitk.git/blob - common/clitkElastix.h
Fix compile issue (add include <iterator>)
[clitk.git] / common / clitkElastix.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
19 #ifndef clitkElastix_h
20 #define clitkElastix_h
21
22 #include <itkEuler3DTransform.h>
23 #include <iterator>
24
25 //--------------------------------------------------------------------
26 namespace clitk {
27
28 //-------------------------------------------------------------------
29 bool
30 GetElastixValueFromTag(std::ifstream & is,
31                        std::string tag,
32                        std::string & value)
33 {
34   std::string line;
35   is.seekg (0, is.beg);
36   while(std::getline(is, line))   {
37     unsigned pos = line.find(tag);
38     if (pos<line.size()) {
39       value=line.substr(pos+tag.size(),line.size()-2);// remove last ')'
40       value.erase (std::remove (value.begin(), value.end(), '"'), value.end());
41       value.erase (std::remove (value.begin(), value.end(), ')'), value.end());
42       return true;
43     }
44   }
45   return false;
46 }
47 //-------------------------------------------------------------------
48
49
50 //-------------------------------------------------------------------
51 void
52 GetValuesFromValue(const std::string & s,
53                    std::vector<std::string> & values)
54 {
55   std::stringstream strstr(s);
56   std::istream_iterator<std::string> it(strstr);
57   std::istream_iterator<std::string> end;
58   std::vector<std::string> results(it, end);
59   values.clear();
60   values.resize(results.size());
61   for(uint i=0; i<results.size(); i++) values[i] = results[i];
62 }
63 //-------------------------------------------------------------------
64
65
66 //-------------------------------------------------------------------
67 template<unsigned int Dimension>
68 typename itk::Matrix<double, Dimension+1, Dimension+1>
69 createMatrixFromElastixFile(std::vector<std::string> & filename, bool verbose=true) {
70   if (Dimension != 3) {
71     FATAL("Only 3D yet" << std::endl);
72   }
73   typename itk::Matrix<double, Dimension+1, Dimension+1> matrix;
74
75   itk::Euler3DTransform<double>::Pointer mat = itk::Euler3DTransform<double>::New();
76   itk::Euler3DTransform<double>::Pointer previous;
77   for(uint j=0; j<filename.size(); j++) {
78
79     // Open file
80     if (verbose) std::cout << "Read elastix parameters in " << filename[j] << std::endl;
81     std::ifstream is;
82     clitk::openFileForReading(is, filename[j]);
83
84     // Check Transform
85     std::string s;
86     bool b = GetElastixValueFromTag(is, "Transform ", s);
87     if (!b) {
88       FATAL("Error must read 'Transform' in " << filename[j] << std::endl);
89     }
90     if (s != "EulerTransform") {
91       FATAL("Sorry only 'EulerTransform'" << std::endl);
92     }
93
94     // FIXME check
95     //    (InitialTransformParametersFilename[j] "NoInitialTransform")
96
97     // Get CenterOfRotationPoint
98     GetElastixValueFromTag(is, "CenterOfRotationPoint ", s); // space is needed
99     if (!b) {
100       FATAL("Error must read 'CenterOfRotationPoint' in " << filename[j] << std::endl);
101     }
102     std::vector<std::string> cor;
103     GetValuesFromValue(s, cor);
104     itk::Euler3DTransform<double>::CenterType c;
105     for(uint i=0; i<3; i++)
106       c[i] = atof(cor[i].c_str());
107     mat->SetCenter(c);
108
109     // Get Transformparameters
110     GetElastixValueFromTag(is, "ComputeZYX ", s); // space is needed
111     mat->SetComputeZYX( s==std::string("true") );
112
113     // Get Transformparameters
114     GetElastixValueFromTag(is, "TransformParameters ", s); // space is needed
115     if (!b) {
116       FATAL("Error must read 'TransformParameters' in " << filename[j] << std::endl);
117     }
118     std::vector<std::string> results;
119     GetValuesFromValue(s, results);
120
121     // construct a stream from the string
122     itk::Euler3DTransform<double>::ParametersType p;
123     p.SetSize(6);
124     for(uint i=0; i<3; i++)
125       p[i] = atof(results[i].c_str()); // Rotation
126     for(uint i=0; i<3; i++)
127       p[i+3] = atof(results[i+3].c_str()); // Translation
128     mat->SetParameters(p);
129
130     if (verbose) {
131       std::cout << "Rotation      (deg) : " << rad2deg(p[0]) << " " << rad2deg(p[1]) << " " << rad2deg(p[2]) << std::endl;
132       std::cout << "Center of rot (phy) : " << c[0] << " " << c[1] << " " << c[2] << std::endl;
133       std::cout << "Translation   (phy) : " << p[3] << " " << p[4] << " " << p[5] << std::endl;
134     }
135
136     // Compose with previous if needed
137     if (j!=0) {
138       mat->Compose(previous);
139       if (verbose) {
140         std::cout << "Composed rotation      (deg) : " << rad2deg(mat->GetAngleX()) << " " << rad2deg(mat->GetAngleY()) << " " << rad2deg(mat->GetAngleZ()) << std::endl;
141         std::cout << "Composed center of rot (phy) : " << mat->GetCenter() << std::endl;
142         std::cout << "Compsoed translation   (phy) : " << mat->GetTranslation() << std::endl;
143       }
144     }
145     // previous = mat->Clone(); // ITK4
146     previous = itk::Euler3DTransform<double>::New();
147     previous->SetParameters(mat->GetParameters());
148     previous->SetCenter(c);
149     previous->SetComputeZYX(mat->GetComputeZYX());
150   }
151
152   mat = previous;
153   for(uint i=0; i<3; i++)
154     for(uint j=0; j<3; j++)
155       matrix[i][j] = mat->GetMatrix()[i][j];
156   // Offset is -Rc + t + c
157   matrix[0][3] = mat->GetOffset()[0];
158   matrix[1][3] = mat->GetOffset()[1];
159   matrix[2][3] = mat->GetOffset()[2];
160   matrix[3][3] = 1;
161
162   return matrix;
163 }
164 }
165 //-------------------------------------------------------------------
166
167 #endif