1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHeader.cxx,v $
6 Date: $Date: 2005/01/14 21:30:53 $
7 Version: $Revision: 1.7 $
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 contain: 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 Sets the Directory
93 * @param dir Name of the directory to deal with
95 void SerieHeader::SetDirectory(std::string const &dir)
97 CurrentSerieUID = ""; //Reset previous Serie Instance UID
98 DirList filenames_list(dir); //OS specific
100 for( DirList::const_iterator it = filenames_list.begin();
101 it != filenames_list.end(); ++it)
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 :)
113 void SerieHeader::OrderGdcmFileList()
115 if( ImagePositionPatientOrdering() )
119 else if( ImageNumberOrdering() )
129 //-----------------------------------------------------------------------------
132 //-----------------------------------------------------------------------------
136 * \brief sorts the images, according to their Patient Position
137 * We may order, considering :
139 * -# Image Position Patient
141 * @return false only if the header is bugged !
143 bool SerieHeader::ImagePositionPatientOrdering()
144 //based on Jolinda's algorithm
146 //iop is calculated based on the file file
151 float min = 0, max = 0;
154 std::vector<float> distlist;
156 //!\todo rewrite this for loop.
157 for ( GdcmHeaderList::const_iterator
158 it = CoherentGdcmFileList.begin();
159 it != CoherentGdcmFileList.end(); ++it )
163 (*it)->GetImageOrientationPatient( cosines );
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];
173 ipp[0] = (*it)->GetXOrigin();
174 ipp[1] = (*it)->GetYOrigin();
175 ipp[2] = (*it)->GetZOrigin();
178 for ( int i = 0; i < 3; ++i )
180 dist += normal[i]*ipp[i];
188 distlist.push_back( dist );
195 ipp[0] = (*it)->GetXOrigin();
196 ipp[1] = (*it)->GetYOrigin();
197 ipp[2] = (*it)->GetZOrigin();
200 for ( int i = 0; i < 3; ++i )
202 dist += normal[i]*ipp[i];
210 distlist.push_back( dist );
212 min = (min < dist) ? min : dist;
213 max = (max > dist) ? max : dist;
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 );
226 float step = (max - min)/(n - 1);
230 //VC++ don't understand what scope is !! it -> it2
231 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
232 it2 != CoherentGdcmFileList.end(); ++it2, ++n)
235 //Assumption: all files are present (no one missing)
236 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
238 CoherentGdcmFileVector[pos] = *it2;
241 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
243 //VC++ don't understand what scope is !! it -> it3
244 for (GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
245 it3 != CoherentGdcmFileVector.end(); ++it3)
247 CoherentGdcmFileList.push_back( *it3 );
251 CoherentGdcmFileVector.clear();
258 * \brief sorts the images, according to their Image Number
259 * @return false only if the header is bugged !
262 bool SerieHeader::ImageNumberOrdering()
265 int n = 0;//CoherentGdcmFileList.size() is a O(N) operation
266 unsigned char *partition;
268 GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
269 min = max = (*it)->GetImageNumber();
271 for (; it != CoherentGdcmFileList.end(); ++it, ++n)
273 pos = (*it)->GetImageNumber();
276 min = (min < pos) ? min : pos;
277 max = (max > pos) ? max : pos;
280 // Find out if sorting worked:
281 if( min == max || max == 0 || max > (n+min)) return false;
283 //bzeros(partition, n); //This function is deprecated, better use memset.
284 partition = new unsigned char[n];
285 memset(partition, 0, n);
287 GdcmHeaderVector CoherentGdcmFileVector(n);
289 //VC++ don't understand what scope is !! it -> it2
290 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
291 it2 != CoherentGdcmFileList.end(); ++it2)
293 pos = (*it2)->GetImageNumber();
294 CoherentGdcmFileVector[pos - min] = *it2;
295 partition[pos - min]++;
298 unsigned char mult = 1;
299 for( int i=0; i<n ; i++ )
301 mult *= partition[i];
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 )
309 CoherentGdcmFileList.push_back( *it3 );
311 CoherentGdcmFileVector.clear();
321 * \brief sorts the images, according to their File Name
322 * @return false only if the header is bugged !
324 bool SerieHeader::FileNameOrdering()
327 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
331 } // end namespace gdcm
332 //-----------------------------------------------------------------------------