]> Creatis software - gdcm.git/blob - src/gdcmSerieHeader.cxx
STYLE: correct typo in comment
[gdcm.git] / src / gdcmSerieHeader.cxx
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSerieHeader.cxx,v $
5   Language:  C++
6   Date:      $Date: 2005/01/14 21:30:53 $
7   Version:   $Revision: 1.7 $
8                                                                                 
9   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
10   l'Image). All rights reserved. See Doc/License.txt or
11   http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
12                                                                                 
13      This software is distributed WITHOUT ANY WARRANTY; without even
14      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15      PURPOSE.  See the above copyright notices for more information.
16                                                                                 
17 =========================================================================*/
18
19 #include "gdcmSerieHeader.h"
20 #include "gdcmDirList.h"
21 #include "gdcmHeader.h"
22 #include "gdcmDebug.h"
23
24 #include <math.h>
25 #include <algorithm>
26 #include <vector>
27
28 namespace gdcm 
29 {
30
31 typedef std::vector<Header* > GdcmHeaderVector;
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 SerieHeader::SerieHeader()
35 {
36    CoherentGdcmFileList.clear();
37    // Later will contain: 0020 000e UI REL Series Instance UID
38    CurrentSerieUID = "";
39 }
40
41 SerieHeader::~SerieHeader()
42 {
43    /// \todo
44    for ( GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
45          it != CoherentGdcmFileList.end(); ++it)
46    {
47       delete *it;
48    }
49    CoherentGdcmFileList.clear();
50 }
51
52 //-----------------------------------------------------------------------------
53 // Print
54
55 //-----------------------------------------------------------------------------
56 // Public
57 /**
58  * \brief add a File to the list based on file name
59  * @param   filename Name of the file to deal with
60  */
61 void SerieHeader::AddFileName(std::string const &filename)
62 {
63    //directly use string and not const char*:
64    Header *header = new Header( filename ); 
65    if( header->IsReadable() )
66    {
67       // 0020 000e UI REL Series Instance UID
68       std::string uid =  header->GetEntry (0x0020, 0x000e);
69       if( CurrentSerieUID == "" )
70       {
71          // Set the current one
72          CurrentSerieUID = uid;
73       }
74       if( CurrentSerieUID == uid )
75       {
76          // Current Serie UID and DICOM header seems to match add the file:
77          CoherentGdcmFileList.push_back( header );
78       }
79       else
80       {
81          gdcmVerboseMacro("Wrong Serie Instance UID should be:" << CurrentSerieUID );
82       }
83    }
84    else
85    {
86       gdcmVerboseMacro("Could not read file: " << filename );
87       delete header;
88    }
89 }
90
91 /**
92  * \brief Sets the Directory
93  * @param   dir Name of the directory to deal with
94  */
95 void SerieHeader::SetDirectory(std::string const &dir)
96 {
97    CurrentSerieUID = ""; //Reset previous Serie Instance UID
98    DirList filenames_list(dir);  //OS specific
99   
100    for( DirList::const_iterator it = filenames_list.begin(); 
101         it != filenames_list.end(); ++it)
102    {
103       AddFileName( *it );
104    }
105 }
106
107 /**
108  * \brief Sorts the File List
109  * \warning This could be implemented in a 'Strategy Pattern' approach
110  *          But as I don't know how to do it, I leave it this way
111  *          BTW, this is also a Strategy, I don't know this is the best approach :)
112  */
113 void SerieHeader::OrderGdcmFileList()
114 {
115    if( ImagePositionPatientOrdering() ) 
116    {
117       return ;
118    }
119    else if( ImageNumberOrdering() )
120    {
121       return ;
122    }
123    else  
124    {
125       FileNameOrdering();
126    }
127 }
128
129 //-----------------------------------------------------------------------------
130 // Protected
131
132 //-----------------------------------------------------------------------------
133 // Private
134 /**
135  * \ingroup Header
136  * \brief sorts the images, according to their Patient Position
137  *  We may order, considering :
138  *   -# Image Number
139  *   -# Image Position Patient
140  *   -# More to come :)
141  * @return false only if the header is bugged !
142  */
143 bool SerieHeader::ImagePositionPatientOrdering()
144 //based on Jolinda's algorithm
145 {
146    //iop is calculated based on the file file
147    float cosines[6];
148    float normal[3];
149    float ipp[3];
150    float dist;
151    float min = 0, max = 0;
152    bool first = true;
153    int n=0;
154    std::vector<float> distlist;
155
156    //!\todo rewrite this for loop.
157    for ( GdcmHeaderList::const_iterator 
158          it = CoherentGdcmFileList.begin();
159          it != CoherentGdcmFileList.end(); ++it )
160    {
161       if( first ) 
162       {
163          (*it)->GetImageOrientationPatient( cosines );
164       
165          //You only have to do this once for all slices in the volume. Next, 
166          // for each slice, calculate the distance along the slice normal 
167          // using the IPP tag ("dist" is initialized to zero before reading 
168          // the first slice) :
169          normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
170          normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
171          normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
172   
173          ipp[0] = (*it)->GetXOrigin();
174          ipp[1] = (*it)->GetYOrigin();
175          ipp[2] = (*it)->GetZOrigin();
176
177          dist = 0;
178          for ( int i = 0; i < 3; ++i )
179          {
180             dist += normal[i]*ipp[i];
181          }
182     
183          if( dist == 0 )
184          {
185             return false;
186          }
187
188          distlist.push_back( dist );
189
190          max = min = dist;
191          first = false;
192       }
193       else 
194       {
195          ipp[0] = (*it)->GetXOrigin();
196          ipp[1] = (*it)->GetYOrigin();
197          ipp[2] = (*it)->GetZOrigin();
198   
199          dist = 0;
200          for ( int i = 0; i < 3; ++i )
201          {
202             dist += normal[i]*ipp[i];
203          }
204
205          if( dist == 0 )
206          {
207             return false;
208          }
209       
210          distlist.push_back( dist );
211
212          min = (min < dist) ? min : dist;
213          max = (max > dist) ? max : dist;
214       }
215       ++n;
216    }
217
218    // Then I order the slices according to the value "dist". Finally, once
219    // I've read in all the slices, I calculate the z-spacing as the difference
220    // between the "dist" values for the first two slices.
221    GdcmHeaderVector CoherentGdcmFileVector(n);
222    // CoherentGdcmFileVector.reserve( n );
223    CoherentGdcmFileVector.resize( n );
224    // gdcmAssertMacro( CoherentGdcmFileVector.capacity() >= n );
225
226    float step = (max - min)/(n - 1);
227    int pos;
228    n = 0;
229     
230    //VC++ don't understand what scope is !! it -> it2
231    for (GdcmHeaderList::const_iterator it2  = CoherentGdcmFileList.begin();
232         it2 != CoherentGdcmFileList.end(); ++it2, ++n)
233    {
234       //2*n sort algo !!
235       //Assumption: all files are present (no one missing)
236       pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
237             
238       CoherentGdcmFileVector[pos] = *it2;
239    }
240
241    CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
242   
243    //VC++ don't understand what scope is !! it -> it3
244    for (GdcmHeaderVector::const_iterator it3  = CoherentGdcmFileVector.begin();
245         it3 != CoherentGdcmFileVector.end(); ++it3)
246    {
247       CoherentGdcmFileList.push_back( *it3 );
248    }
249
250    distlist.clear();
251    CoherentGdcmFileVector.clear();
252
253    return true;
254 }
255
256 /**
257  * \ingroup Header
258  * \brief sorts the images, according to their Image Number
259  * @return false only if the header is bugged !
260  */
261
262 bool SerieHeader::ImageNumberOrdering() 
263 {
264    int min, max, pos;
265    int n = 0;//CoherentGdcmFileList.size() is a O(N) operation
266    unsigned char *partition;
267   
268    GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
269    min = max = (*it)->GetImageNumber();
270
271    for (; it != CoherentGdcmFileList.end(); ++it, ++n)
272    {
273       pos = (*it)->GetImageNumber();
274
275       //else
276       min = (min < pos) ? min : pos;
277       max = (max > pos) ? max : pos;
278    }
279
280    // Find out if sorting worked:
281    if( min == max || max == 0 || max > (n+min)) return false;
282
283    //bzeros(partition, n); //This function is deprecated, better use memset.
284    partition = new unsigned char[n];
285    memset(partition, 0, n);
286
287    GdcmHeaderVector CoherentGdcmFileVector(n);
288
289    //VC++ don't understand what scope is !! it -> it2
290    for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
291         it2 != CoherentGdcmFileList.end(); ++it2)
292    {
293       pos = (*it2)->GetImageNumber();
294       CoherentGdcmFileVector[pos - min] = *it2;
295       partition[pos - min]++;
296    }
297   
298    unsigned char mult = 1;
299    for( int i=0; i<n ; i++ )
300    {
301       mult *= partition[i];
302    }
303
304    //VC++ don't understand what scope is !! it -> it3
305    CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
306    for ( GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
307          it3 != CoherentGdcmFileVector.end(); ++it3 )
308    {
309       CoherentGdcmFileList.push_back( *it3 );
310    }
311    CoherentGdcmFileVector.clear();
312   
313    delete[] partition;
314
315    return mult != 0;
316 }
317
318
319 /**
320  * \ingroup Header
321  * \brief sorts the images, according to their File Name
322  * @return false only if the header is bugged !
323  */
324 bool SerieHeader::FileNameOrdering()
325 {
326    //using the sort
327    //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
328    return true;
329 }
330
331 } // end namespace gdcm
332 //-----------------------------------------------------------------------------