1 /*=========================================================================
4 Module: $RCSfile: gdcmSerieHeader.cxx,v $
6 Date: $Date: 2005/01/28 16:56:49 $
7 Version: $Revision: 1.14 $
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"
22 #include "gdcmDebug.h"
31 typedef std::vector<File* > GdcmFileVector;
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 ( GdcmFileList::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 File *header = new File( filename );
65 if( header->IsReadable() )
67 // 0020 000e UI REL Series Instance UID
68 std::string uid = header->GetEntryValue (0x0020, 0x000e);
69 // if uid == GDCM_UNFOUND then consistenly we should find GDCM_UNFOUND
70 // no need here to do anything special
71 if( CurrentSerieUID == "" )
73 // Set the current one
74 CurrentSerieUID = uid;
76 if( CurrentSerieUID == uid )
78 // Current Serie UID and DICOM header seems to match add the file:
79 CoherentGdcmFileList.push_back( header );
83 gdcmVerboseMacro("Wrong Serie Instance UID should be:" << CurrentSerieUID );
88 gdcmVerboseMacro("Could not read file: " << filename );
94 * \brief Sets the Directory
95 * @param dir Name of the directory to deal with
96 * @param recursive whether we want explore recursively the Directory
98 void SerieHeader::SetDirectory(std::string const &dir, bool recursive)
100 CurrentSerieUID = ""; // Reset previous Serie Instance UID
101 DirList dirList(dir, recursive); // OS specific
103 DirListType filenames_list = dirList.GetFilenames();
104 for( DirListType::const_iterator it = filenames_list.begin();
105 it != filenames_list.end(); ++it)
112 * \brief Sorts the File List
113 * \warning This could be implemented in a 'Strategy Pattern' approach
114 * But as I don't know how to do it, I leave it this way
115 * BTW, this is also a Strategy, I don't know this is the best approach :)
117 void SerieHeader::OrderGdcmFileList()
119 if( ImagePositionPatientOrdering() )
123 else if( ImageNumberOrdering() )
133 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
139 * \brief sorts the images, according to their Patient Position
140 * We may order, considering :
142 * -# Image Position Patient
144 * @return false only if the header is bugged !
146 bool SerieHeader::ImagePositionPatientOrdering()
147 //based on Jolinda's algorithm
149 //iop is calculated based on the file file
154 float min = 0, max = 0;
157 std::vector<float> distlist;
159 //!\todo rewrite this for loop.
160 for ( GdcmFileList::const_iterator
161 it = CoherentGdcmFileList.begin();
162 it != CoherentGdcmFileList.end(); ++it )
166 (*it)->GetImageOrientationPatient( cosines );
168 //You only have to do this once for all slices in the volume. Next,
169 // for each slice, calculate the distance along the slice normal
170 // using the IPP tag ("dist" is initialized to zero before reading
171 // the first slice) :
172 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
173 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
174 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
176 ipp[0] = (*it)->GetXOrigin();
177 ipp[1] = (*it)->GetYOrigin();
178 ipp[2] = (*it)->GetZOrigin();
181 for ( int i = 0; i < 3; ++i )
183 dist += normal[i]*ipp[i];
191 distlist.push_back( dist );
198 ipp[0] = (*it)->GetXOrigin();
199 ipp[1] = (*it)->GetYOrigin();
200 ipp[2] = (*it)->GetZOrigin();
203 for ( int i = 0; i < 3; ++i )
205 dist += normal[i]*ipp[i];
213 distlist.push_back( dist );
215 min = (min < dist) ? min : dist;
216 max = (max > dist) ? max : dist;
221 // Then I order the slices according to the value "dist". Finally, once
222 // I've read in all the slices, I calculate the z-spacing as the difference
223 // between the "dist" values for the first two slices.
224 GdcmFileVector CoherentGdcmFileVector(n);
225 // CoherentGdcmFileVector.reserve( n );
226 CoherentGdcmFileVector.resize( n );
227 // gdcmAssertMacro( CoherentGdcmFileVector.capacity() >= n );
229 float step = (max - min)/(n - 1);
233 //VC++ don't understand what scope is !! it -> it2
234 for (GdcmFileList::const_iterator it2 = CoherentGdcmFileList.begin();
235 it2 != CoherentGdcmFileList.end(); ++it2, ++n)
238 //Assumption: all files are present (no one missing)
239 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
241 // a Dicom 'Serie' may contain scout views
242 // and images may have differents directions
243 // -> More than one may have the same 'pos'
244 // Sorting has then NO meaning !
245 if (CoherentGdcmFileVector[pos]==NULL)
246 CoherentGdcmFileVector[pos] = *it2;
251 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
253 //VC++ don't understand what scope is !! it -> it3
254 for (GdcmFileVector::const_iterator it3 = CoherentGdcmFileVector.begin();
255 it3 != CoherentGdcmFileVector.end(); ++it3)
257 CoherentGdcmFileList.push_back( *it3 );
261 CoherentGdcmFileVector.clear();
267 * \brief sorts the images, according to their Image Number
268 * @return false only if the header is bugged !
271 bool SerieHeader::ImageNumberOrdering()
274 int n = 0;//CoherentGdcmFileList.size() is a O(N) operation
275 unsigned char *partition;
277 GdcmFileList::const_iterator it = CoherentGdcmFileList.begin();
278 min = max = (*it)->GetImageNumber();
280 for (; it != CoherentGdcmFileList.end(); ++it, ++n)
282 pos = (*it)->GetImageNumber();
285 min = (min < pos) ? min : pos;
286 max = (max > pos) ? max : pos;
289 // Find out if sorting worked:
290 if( min == max || max == 0 || max > (n+min)) return false;
292 //bzeros(partition, n); //This function is deprecated, better use memset.
293 partition = new unsigned char[n];
294 memset(partition, 0, n);
296 GdcmFileVector CoherentGdcmFileVector(n);
298 //VC++ don't understand what scope is !! it -> it2
299 for (GdcmFileList::const_iterator it2 = CoherentGdcmFileList.begin();
300 it2 != CoherentGdcmFileList.end(); ++it2)
302 pos = (*it2)->GetImageNumber();
303 CoherentGdcmFileVector[pos - min] = *it2;
304 partition[pos - min]++;
307 unsigned char mult = 1;
308 for( int i=0; i<n ; i++ )
310 mult *= partition[i];
313 //VC++ don't understand what scope is !! it -> it3
314 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
315 for ( GdcmFileVector::const_iterator it3 = CoherentGdcmFileVector.begin();
316 it3 != CoherentGdcmFileVector.end(); ++it3 )
318 CoherentGdcmFileList.push_back( *it3 );
320 CoherentGdcmFileVector.clear();
329 * \brief sorts the images, according to their File Name
330 * @return false only if the header is bugged !
332 bool SerieHeader::FileNameOrdering()
335 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
339 } // end namespace gdcm
340 //-----------------------------------------------------------------------------