1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHeader.cxx,v $
6 Date: $Date: 2005/01/14 21:03:54 $
7 Version: $Revision: 1.6 $
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"
22 #include "gdcmDebug.h"
31 typedef std::vector<Header* > GdcmHeaderVector;
32 //-----------------------------------------------------------------------------
33 // Constructor / Destructor
34 SerieHeader::SerieHeader()
36 CoherentGdcmFileList.clear();
37 // Later will contains: 0020 000e UI REL Series Instance UID
41 SerieHeader::~SerieHeader()
44 for ( GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
45 it != CoherentGdcmFileList.end(); ++it)
49 CoherentGdcmFileList.clear();
52 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
58 * \brief add a File to the list based on file name
59 * @param filename Name of the file to deal with
61 void SerieHeader::AddFileName(std::string const &filename)
63 //directly use string and not const char*:
64 Header *header = new Header( filename );
65 if( header->IsReadable() )
67 // 0020 000e UI REL Series Instance UID
68 std::string uid = header->GetEntry (0x0020, 0x000e);
69 if( CurrentSerieUID == "" )
71 // Set the current one
72 CurrentSerieUID = uid;
74 if( CurrentSerieUID == uid )
76 // Current Serie UID and DICOM header seems to match add the file:
77 CoherentGdcmFileList.push_back( header );
81 gdcmVerboseMacro("Wrong Serie Instance UID should be:" << CurrentSerieUID );
86 gdcmVerboseMacro("Could not read file: " << filename );
92 * \brief add a File to the list
93 * @param file Header to add
95 void SerieHeader::AddGdcmFile(Header *file)
97 if( file->IsReadable() )
99 CoherentGdcmFileList.push_back( file );
103 gdcmVerboseMacro("Could not add file: " << file->GetFileName() );
108 * \brief Sets the Directory
109 * @param dir Name of the directory to deal with
111 void SerieHeader::SetDirectory(std::string const &dir)
113 DirList filenames_list(dir); //OS specific
115 for( DirList::const_iterator it = filenames_list.begin();
116 it != filenames_list.end(); ++it)
123 * \brief Sorts the File List
124 * \warning This could be implemented in a 'Strategy Pattern' approach
125 * But as I don't know how to do it, I leave it this way
126 * BTW, this is also a Strategy, I don't know this is the best approach :)
128 void SerieHeader::OrderGdcmFileList()
130 if( ImagePositionPatientOrdering() )
134 else if( ImageNumberOrdering() )
144 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
151 * \brief sorts the images, according to their Patient Position
152 * We may order, considering :
154 * -# Image Position Patient
156 * @return false only if the header is bugged !
158 bool SerieHeader::ImagePositionPatientOrdering()
159 //based on Jolinda's algorithm
161 //iop is calculated based on the file file
166 float min = 0, max = 0;
169 std::vector<float> distlist;
171 //!\todo rewrite this for loop.
172 for ( GdcmHeaderList::const_iterator
173 it = CoherentGdcmFileList.begin();
174 it != CoherentGdcmFileList.end(); ++it )
178 (*it)->GetImageOrientationPatient( cosines );
180 //You only have to do this once for all slices in the volume. Next,
181 // for each slice, calculate the distance along the slice normal
182 // using the IPP tag ("dist" is initialized to zero before reading
183 // the first slice) :
184 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
185 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
186 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
188 ipp[0] = (*it)->GetXOrigin();
189 ipp[1] = (*it)->GetYOrigin();
190 ipp[2] = (*it)->GetZOrigin();
193 for ( int i = 0; i < 3; ++i )
195 dist += normal[i]*ipp[i];
203 distlist.push_back( dist );
210 ipp[0] = (*it)->GetXOrigin();
211 ipp[1] = (*it)->GetYOrigin();
212 ipp[2] = (*it)->GetZOrigin();
215 for ( int i = 0; i < 3; ++i )
217 dist += normal[i]*ipp[i];
225 distlist.push_back( dist );
227 min = (min < dist) ? min : dist;
228 max = (max > dist) ? max : dist;
233 // Then I order the slices according to the value "dist". Finally, once
234 // I've read in all the slices, I calculate the z-spacing as the difference
235 // between the "dist" values for the first two slices.
236 GdcmHeaderVector CoherentGdcmFileVector(n);
237 // CoherentGdcmFileVector.reserve( n );
238 CoherentGdcmFileVector.resize( n );
239 // gdcmAssertMacro( CoherentGdcmFileVector.capacity() >= n );
241 float step = (max - min)/(n - 1);
245 //VC++ don't understand what scope is !! it -> it2
246 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
247 it2 != CoherentGdcmFileList.end(); ++it2, ++n)
250 //Assumption: all files are present (no one missing)
251 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
253 CoherentGdcmFileVector[pos] = *it2;
256 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
258 //VC++ don't understand what scope is !! it -> it3
259 for (GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
260 it3 != CoherentGdcmFileVector.end(); ++it3)
262 CoherentGdcmFileList.push_back( *it3 );
266 CoherentGdcmFileVector.clear();
273 * \brief sorts the images, according to their Image Number
274 * @return false only if the header is bugged !
277 bool SerieHeader::ImageNumberOrdering()
280 int n = 0;//CoherentGdcmFileList.size() is a O(N) operation
281 unsigned char *partition;
283 GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
284 min = max = (*it)->GetImageNumber();
286 for (; it != CoherentGdcmFileList.end(); ++it, ++n)
288 pos = (*it)->GetImageNumber();
291 min = (min < pos) ? min : pos;
292 max = (max > pos) ? max : pos;
295 // Find out if sorting worked:
296 if( min == max || max == 0 || max > (n+min)) return false;
298 //bzeros(partition, n); //This function is deprecated, better use memset.
299 partition = new unsigned char[n];
300 memset(partition, 0, n);
302 GdcmHeaderVector CoherentGdcmFileVector(n);
304 //VC++ don't understand what scope is !! it -> it2
305 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
306 it2 != CoherentGdcmFileList.end(); ++it2)
308 pos = (*it2)->GetImageNumber();
309 CoherentGdcmFileVector[pos - min] = *it2;
310 partition[pos - min]++;
313 unsigned char mult = 1;
314 for( int i=0; i<n ; i++ )
316 mult *= partition[i];
319 //VC++ don't understand what scope is !! it -> it3
320 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
321 for ( GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
322 it3 != CoherentGdcmFileVector.end(); ++it3 )
324 CoherentGdcmFileList.push_back( *it3 );
326 CoherentGdcmFileVector.clear();
336 * \brief sorts the images, according to their File Name
337 * @return false only if the header is bugged !
339 bool SerieHeader::FileNameOrdering()
342 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
346 } // end namespace gdcm
347 //-----------------------------------------------------------------------------