1 /*=========================================================================
4 Module: $RCSfile: gdcmHeaderHelper.cxx,v $
6 Date: $Date: 2004/10/12 04:35:46 $
7 Version: $Revision: 1.43 $
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 "gdcmHeaderHelper.h"
20 #include "gdcmDirList.h"
21 #include "gdcmDebug.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 //use string and not const char*:
85 Header *header = new Header( *it );
86 CoherentGdcmFileList.push_back( header );
91 * \brief Sorts the File List
92 * \warning This could be implemented in a 'Strategy Pattern' approach
93 * But as I don't know how to do it, I leave it this way
94 * BTW, this is also a Strategy, I don't know this is the best approach :)
96 void SerieHeader::OrderGdcmFileList()
98 if( ImagePositionPatientOrdering() )
102 else if( ImageNumberOrdering() )
112 //-----------------------------------------------------------------------------
115 //-----------------------------------------------------------------------------
119 * \brief sorts the images, according to their Patient Position
120 * We may order, considering :
122 * -# Image Position Patient
124 * @return false only if the header is bugged !
126 bool SerieHeader::ImagePositionPatientOrdering()
127 //based on Jolinda's algorithm
129 //iop is calculated based on the file file
130 float *cosines = new float[6];
134 float min = 0, max = 0;
137 std::vector<float> distlist;
139 //!\todo rewrite this for loop.
140 for ( GdcmHeaderList::const_iterator
141 it = CoherentGdcmFileList.begin();
142 it != CoherentGdcmFileList.end(); ++it )
146 (*it)->GetImageOrientationPatient( cosines );
148 //You only have to do this once for all slices in the volume. Next,
149 // for each slice, calculate the distance along the slice normal
150 // using the IPP tag ("dist" is initialized to zero before reading
151 // the first slice) :
152 normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
153 normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
154 normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
156 ipp[0] = (*it)->GetXOrigin();
157 ipp[1] = (*it)->GetYOrigin();
158 ipp[2] = (*it)->GetZOrigin();
161 for ( int i = 0; i < 3; ++i )
163 dist += normal[i]*ipp[i];
172 distlist.push_back( dist );
179 ipp[0] = (*it)->GetXOrigin();
180 ipp[1] = (*it)->GetYOrigin();
181 ipp[2] = (*it)->GetZOrigin();
184 for ( int i = 0; i < 3; ++i )
186 dist += normal[i]*ipp[i];
195 distlist.push_back( dist );
197 min = (min < dist) ? min : dist;
198 max = (max > dist) ? max : dist;
203 // Then I order the slices according to the value "dist". Finally, once
204 // I've read in all the slices, I calculate the z-spacing as the difference
205 // between the "dist" values for the first two slices.
206 GdcmHeaderVector CoherentGdcmFileVector(n);
207 // CoherentGdcmFileVector.reserve( n );
208 CoherentGdcmFileVector.resize( n );
209 // assert( CoherentGdcmFileVector.capacity() >= n );
211 float step = (max - min)/(n - 1);
215 //VC++ don't understand what scope is !! it -> it2
216 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
217 it2 != CoherentGdcmFileList.end(); ++it2, ++n)
220 //Assumption: all files are present (no one missing)
221 pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
223 CoherentGdcmFileVector[pos] = *it2;
226 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
228 //VC++ don't understand what scope is !! it -> it3
229 for (GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
230 it3 != CoherentGdcmFileVector.end(); ++it3)
232 CoherentGdcmFileList.push_back( *it3 );
236 CoherentGdcmFileVector.clear();
244 * \brief sorts the images, according to their Image Number
245 * @return false only if the header is bugged !
248 bool SerieHeader::ImageNumberOrdering()
251 int n = 0;//CoherentGdcmFileList.size() is a O(N) operation !!
252 unsigned char *partition;
254 GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
255 min = max = (*it)->GetImageNumber();
257 for (; it != CoherentGdcmFileList.end(); ++it, ++n)
259 pos = (*it)->GetImageNumber();
262 min = (min < pos) ? min : pos;
265 //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
266 partition = new unsigned char[n];
267 memset(partition, 0, n);
269 GdcmHeaderVector CoherentGdcmFileVector(n);
271 //VC++ don't understand what scope is !! it -> it2
272 for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
273 it2 != CoherentGdcmFileList.end(); ++it2)
275 pos = (*it2)->GetImageNumber();
276 CoherentGdcmFileVector[pos - min] = *it2;
277 partition[pos - min]++;
280 unsigned char mult = 1;
281 for( int i=0; i<n ; i++ )
283 mult *= partition[i];
286 //VC++ don't understand what scope is !! it -> it3
287 CoherentGdcmFileList.clear(); //this doesn't delete list's element, node only
288 for ( GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
289 it3 != CoherentGdcmFileVector.end(); ++it3 )
291 CoherentGdcmFileList.push_back( *it3 );
293 CoherentGdcmFileVector.clear();
303 * \brief sorts the images, according to their File Name
304 * @return false only if the header is bugged !
306 bool SerieHeader::FileNameOrdering()
309 //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
313 } // end namespace gdcm
314 //-----------------------------------------------------------------------------