1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHeader.cxx,v $
6 Date: $Date: 2005/01/06 20:03:28 $
7 Version: $Revision: 1.3 $
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.
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.
17 =========================================================================*/
19 #include "gdcmSerieHeader.h"
20 #include "gdcmDirList.h"
21 #include "gdcmHeader.h"
30 typedef std::vector<Header* > GdcmHeaderVector;
31 //-----------------------------------------------------------------------------
32 // Constructor / Destructor
33 SerieHeader::SerieHeader()
35 CoherentGdcmFileList.clear();
38 SerieHeader::~SerieHeader()
41 for ( GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
42 it != CoherentGdcmFileList.end(); ++it)
46 CoherentGdcmFileList.clear();
49 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
55 * \brief add a File to the list based on file name
56 * @param filename Name of the file to deal with
58 void SerieHeader::AddFileName(std::string const &filename)
60 Header *header = new Header( filename );
61 CoherentGdcmFileList.push_back( header );
65 * \brief add a File to the list
66 * @param file Header to add
68 void SerieHeader::AddGdcmFile(Header *file)
70 CoherentGdcmFileList.push_back( file );
74 * \brief Sets the Directory
75 * @param dir Name of the directory to deal with
77 void SerieHeader::SetDirectory(std::string const &dir)
79 DirList filenames_list(dir); //OS specific
81 for( DirList::const_iterator it = filenames_list.begin();
82 it != filenames_list.end(); ++it)
84 //directly use string and not const char*:
85 Header *header = new Header( *it );
86 if( header->IsReadable() )
88 CoherentGdcmFileList.push_back( header );
98 * \brief Sorts the File List
99 * \warning This could be implemented in a 'Strategy Pattern' approach
100 * But as I don't know how to do it, I leave it this way
101 * BTW, this is also a Strategy, I don't know this is the best approach :)
103 void SerieHeader::OrderGdcmFileList()
105 if( ImagePositionPatientOrdering() )
109 else if( ImageNumberOrdering() )
119 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
126 * \brief sorts the images, according to their Patient Position
127 * We may order, considering :
129 * -# Image Position Patient
131 * @return false only if the header is bugged !
133 bool SerieHeader::ImagePositionPatientOrdering()
134 //based on Jolinda's algorithm
136 //iop is calculated based on the file file
137 float *cosines = new float[6];
141 float min = 0, max = 0;
144 std::vector<float> distlist;
146 //!\todo rewrite this for loop.
147 for ( GdcmHeaderList::const_iterator
148 it = CoherentGdcmFileList.begin();
149 it != CoherentGdcmFileList.end(); ++it )
153 (*it)->GetImageOrientationPatient( cosines );
155 //You only have to do this once for all slices in the volume. Next,
156 // for each slice, calculate the distance along the slice normal
157 // using the IPP tag ("dist" is initialized to zero before reading
158 // the first slice) :
159 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
160 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
161 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
163 ipp[0] = (*it)->GetXOrigin();
164 ipp[1] = (*it)->GetYOrigin();
165 ipp[2] = (*it)->GetZOrigin();
168 for ( int i = 0; i < 3; ++i )
170 dist += normal[i]*ipp[i];
179 distlist.push_back( dist );
186 ipp[0] = (*it)->GetXOrigin();
187 ipp[1] = (*it)->GetYOrigin();
188 ipp[2] = (*it)->GetZOrigin();
191 for ( int i = 0; i < 3; ++i )
193 dist += normal[i]*ipp[i];
202 distlist.push_back( dist );
204 min = (min < dist) ? min : dist;
205 max = (max > dist) ? max : dist;
210 // Then I order the slices according to the value "dist". Finally, once
211 // I've read in all the slices, I calculate the z-spacing as the difference
212 // between the "dist" values for the first two slices.
213 GdcmHeaderVector CoherentGdcmFileVector(n);
214 // CoherentGdcmFileVector.reserve( n );
215 CoherentGdcmFileVector.resize( n );
216 // assert( CoherentGdcmFileVector.capacity() >= n );
218 float step = (max - min)/(n - 1);
222 //VC++ don't understand what scope is !! it -> it2
223 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
224 it2 != CoherentGdcmFileList.end(); ++it2, ++n)
227 //Assumption: all files are present (no one missing)
228 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
230 CoherentGdcmFileVector[pos] = *it2;
233 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
235 //VC++ don't understand what scope is !! it -> it3
236 for (GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
237 it3 != CoherentGdcmFileVector.end(); ++it3)
239 CoherentGdcmFileList.push_back( *it3 );
243 CoherentGdcmFileVector.clear();
251 * \brief sorts the images, according to their Image Number
252 * @return false only if the header is bugged !
255 bool SerieHeader::ImageNumberOrdering()
258 int n = 0;//CoherentGdcmFileList.size() is a O(N) operation
259 unsigned char *partition;
261 GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
262 min = max = (*it)->GetImageNumber();
264 for (; it != CoherentGdcmFileList.end(); ++it, ++n)
266 pos = (*it)->GetImageNumber();
269 min = (min < pos) ? min : pos;
270 max = (max > pos) ? max : pos;
273 // Find out if sorting worked:
274 if( min == max || max == 0 || max > (n+min)) return false;
276 //bzeros(partition, n); //This function is deprecated, better use memset.
277 partition = new unsigned char[n];
278 memset(partition, 0, n);
280 GdcmHeaderVector CoherentGdcmFileVector(n);
282 //VC++ don't understand what scope is !! it -> it2
283 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
284 it2 != CoherentGdcmFileList.end(); ++it2)
286 pos = (*it2)->GetImageNumber();
287 CoherentGdcmFileVector[pos - min] = *it2;
288 partition[pos - min]++;
291 unsigned char mult = 1;
292 for( int i=0; i<n ; i++ )
294 mult *= partition[i];
297 //VC++ don't understand what scope is !! it -> it3
298 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
299 for ( GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
300 it3 != CoherentGdcmFileVector.end(); ++it3 )
302 CoherentGdcmFileList.push_back( *it3 );
304 CoherentGdcmFileVector.clear();
314 * \brief sorts the images, according to their File Name
315 * @return false only if the header is bugged !
317 bool SerieHeader::FileNameOrdering()
320 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
324 } // end namespace gdcm
325 //-----------------------------------------------------------------------------