]> Creatis software - gdcm.git/commitdiff
ENH: * 3'tabify
authormalaterre <malaterre>
Fri, 25 Jun 2004 20:48:25 +0000 (20:48 +0000)
committermalaterre <malaterre>
Fri, 25 Jun 2004 20:48:25 +0000 (20:48 +0000)
* Fix bug where gdcmHeader was passed a const char*
* use const_iterator where possible
* made some method const correct
* moved a method in class (inline)
...hopefully should be more readable

src/gdcmHeaderHelper.cxx
src/gdcmHeaderHelper.h

index 4f53b9704dd3f5ec53cacb6eb81c765b1fadd004..164254ef0f4c194a50efab943e7e39b90af25c4d 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmHeaderHelper.cxx,v $
   Language:  C++
-  Date:      $Date: 2004/06/25 19:37:05 $
-  Version:   $Revision: 1.39 $
+  Date:      $Date: 2004/06/25 20:48:25 $
+  Version:   $Revision: 1.40 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
 #include <algorithm>
 #include <vector>
 
+typedef std::vector<gdcmHeader* > GdcmHeaderVector;
 //-----------------------------------------------------------------------------
 // Constructor / Destructor
-gdcmSerieHeader::~gdcmSerieHeader(){
-  /// \todo
-  for (std::list<gdcmHeader*>::iterator it  = CoherentGdcmFileList.begin();
-        it != CoherentGdcmFileList.end(); it++)
-  {
-    delete *it;
-  }
-  CoherentGdcmFileList.clear();
+gdcmSerieHeader::gdcmSerieHeader()
+{
+   CoherentGdcmFileList.clear();
+}
+
+gdcmSerieHeader::~gdcmSerieHeader()
+{
+   /// \todo
+   for ( GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
+         it != CoherentGdcmFileList.end(); ++it)
+   {
+      delete *it;
+   }
+   CoherentGdcmFileList.clear();
 }
 
 //-----------------------------------------------------------------------------
@@ -45,32 +52,36 @@ gdcmSerieHeader::~gdcmSerieHeader(){
  * \brief add a gdcmFile to the list based on file name
  * @param   filename Name of the file to deal with
  */
-void gdcmSerieHeader::AddFileName(std::string filename) {
-  gdcmHeader *GdcmFile = new gdcmHeader( filename );
-  this->CoherentGdcmFileList.push_back( GdcmFile );
+void gdcmSerieHeader::AddFileName(std::string const & filename)
+{
+   gdcmHeader *header = new gdcmHeader( filename );
+   this->CoherentGdcmFileList.push_back( header );
 }
 
 /**
  * \brief add a gdcmFile to the list
  * @param   file gdcmHeader to add
  */
-void gdcmSerieHeader::AddGdcmFile(gdcmHeader *file){
-  this->CoherentGdcmFileList.push_back( file );
+void gdcmSerieHeader::AddGdcmFile(gdcmHeader *file)
+{
+   this->CoherentGdcmFileList.push_back( file );
 }
 
 /**
  * \brief Sets the Directory
  * @param   dir Name of the directory to deal with
  */
-void gdcmSerieHeader::SetDirectory(std::string dir){
-  gdcmDirList filenames_list(dir);  //OS specific
+void gdcmSerieHeader::SetDirectory(std::string const & dir)
+{
+   gdcmDirList filenames_list(dir);  //OS specific
   
-  for(gdcmDirList::iterator it = filenames_list.begin(); 
-      it !=filenames_list.end(); it++)
-  {
-    gdcmHeader *file = new gdcmHeader( it->c_str() );
-    this->CoherentGdcmFileList.push_back( file );
-  }
+   for( gdcmDirList::const_iterator it = filenames_list.begin(); 
+        it != filenames_list.end(); ++it)
+   {
+      //use string and not const char*:
+      gdcmHeader *header = new gdcmHeader( *it ); 
+      this->CoherentGdcmFileList.push_back( header );
+   }
 }
 
 /**
@@ -78,24 +89,21 @@ void gdcmSerieHeader::SetDirectory(std::string dir){
  * \warning This could be implemented in a 'Strategy Pattern' approach
  *          But as I don't know how to do it, I leave it this way
  *          BTW, this is also a Strategy, I don't know this is the best approach :)
-*/
-void gdcmSerieHeader::OrderGdcmFileList(){
-  if( ImagePositionPatientOrdering() ) {
-    return ;
-  }
-  else if( ImageNumberOrdering() ) {
-    return ;
-  } else  {
-    FileNameOrdering();
-  }
-}
-
-/**
- * \brief Gets the *coherent* File List
- * @return the *coherent* File List
-*/
-std::list<gdcmHeader*> &gdcmSerieHeader::GetGdcmFileList() {
-  return CoherentGdcmFileList;
+ */
+void gdcmSerieHeader::OrderGdcmFileList()
+{
+   if( ImagePositionPatientOrdering() ) 
+   {
+      return ;
+   }
+   else if( ImageNumberOrdering() )
+   {
+      return ;
+   }
+   else  
+   {
+      FileNameOrdering();
+   }
 }
 
 //-----------------------------------------------------------------------------
@@ -115,109 +123,117 @@ std::list<gdcmHeader*> &gdcmSerieHeader::GetGdcmFileList() {
 bool gdcmSerieHeader::ImagePositionPatientOrdering()
 //based on Jolinda's algorithm
 {
-  //iop is calculated based on the file file
-  float *cosines = new float[6];
-  float normal[3];
-  float ipp[3];
-  float dist;
-  float min = 0, max = 0;
-  bool first = true;
-  int n=0;
-  std::vector<float> distlist;
-
-  //!\todo rewrite this for loop.
-  for (std::list<gdcmHeader*>::iterator it  = CoherentGdcmFileList.begin();
-        it != CoherentGdcmFileList.end(); it++)
-  {
-    if(first) {
-      (*it)->GetImageOrientationPatient(cosines);
+   //iop is calculated based on the file file
+   float *cosines = new float[6];
+   float normal[3];
+   float ipp[3];
+   float dist;
+   float min = 0, max = 0;
+   bool first = true;
+   int n=0;
+   std::vector<float> distlist;
+
+   //!\todo rewrite this for loop.
+   for ( GdcmHeaderList::const_iterator 
+         it = CoherentGdcmFileList.begin();
+         it != CoherentGdcmFileList.end(); ++it )
+   {
+      if( first ) 
+      {
+         (*it)->GetImageOrientationPatient( cosines );
       
-      //You only have to do this once for all slices in the volume. Next, for
-      //each slice, calculate the distance along the slice normal using the IPP
-      //tag ("dist" is initialized to zero before reading the first slice) :
-      normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
-      normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
-      normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
+         //You only have to do this once for all slices in the volume. Next, 
+         // for each slice, calculate the distance along the slice normal 
+         // using the IPP tag ("dist" is initialized to zero before reading 
+         // the first slice) :
+         normal[0] = cosines[1]*cosines[5] - cosines[2]*cosines[4];
+         normal[1] = cosines[2]*cosines[3] - cosines[0]*cosines[5];
+         normal[2] = cosines[0]*cosines[4] - cosines[1]*cosines[3];
   
-      ipp[0] = (*it)->GetXOrigin();
-      ipp[1] = (*it)->GetYOrigin();
-      ipp[2] = (*it)->GetZOrigin();
-
-      dist = 0;
-      for (int i = 0; i < 3; ++i)
-          dist += normal[i]*ipp[i];
+         ipp[0] = (*it)->GetXOrigin();
+         ipp[1] = (*it)->GetYOrigin();
+         ipp[2] = (*it)->GetZOrigin();
+
+         dist = 0;
+         for ( int i = 0; i < 3; ++i )
+         {
+            dist += normal[i]*ipp[i];
+         }
     
-      if( dist == 0 )
-      {
-        delete[] cosines;
-        return false;
-      }
+         if( dist == 0 )
+         {
+            delete[] cosines;
+            return false;
+         }
 
-      distlist.push_back( dist );
+         distlist.push_back( dist );
 
-      max = min = dist;
-      first = false;
-    }
-    else {
-      ipp[0] = (*it)->GetXOrigin();
-      ipp[1] = (*it)->GetYOrigin();
-      ipp[2] = (*it)->GetZOrigin();
+         max = min = dist;
+         first = false;
+      }
+      else 
+      {
+         ipp[0] = (*it)->GetXOrigin();
+         ipp[1] = (*it)->GetYOrigin();
+         ipp[2] = (*it)->GetZOrigin();
   
-      dist = 0;
-      for (int i = 0; i < 3; ++i)
-          dist += normal[i]*ipp[i];
+         dist = 0;
+         for ( int i = 0; i < 3; ++i )
+         {
+            dist += normal[i]*ipp[i];
+         }
+
+         if( dist == 0 )
+         {
+            delete[] cosines;
+            return false;
+         }
+      
+         distlist.push_back( dist );
 
-      if( dist == 0 )
-      {
-        delete[] cosines;
-        return false;
+         min = (min < dist) ? min : dist;
+         max = (max > dist) ? max : dist;
       }
-      
-      distlist.push_back( dist );
-
-      min = (min < dist) ? min : dist;
-      max = (max > dist) ? max : dist;
-    }
-    n++;
-  }
-
-    //Then I order the slices according to the value "dist". Finally, once
-    //I've read in all the slices, I calculate the z-spacing as the difference
-    //between the "dist" values for the first two slices.
-    std::vector<gdcmHeader*> CoherentGdcmFileVector(n);
-    //CoherentGdcmFileVector.reserve( n );
-    CoherentGdcmFileVector.resize( n );
-    //assert( CoherentGdcmFileVector.capacity() >= n );
-
-    float step = (max - min)/(n - 1);
-    int pos;
-    n = 0;
+      ++n;
+   }
+
+   // Then I order the slices according to the value "dist". Finally, once
+   // I've read in all the slices, I calculate the z-spacing as the difference
+   // between the "dist" values for the first two slices.
+   GdcmHeaderVector CoherentGdcmFileVector(n);
+   // CoherentGdcmFileVector.reserve( n );
+   CoherentGdcmFileVector.resize( n );
+   // assert( CoherentGdcmFileVector.capacity() >= n );
+
+   float step = (max - min)/(n - 1);
+   int pos;
+   n = 0;
     
-    //VC++ don't understand what scope is !! it -> it2
-    for (std::list<gdcmHeader*>::iterator it2  = CoherentGdcmFileList.begin();
-        it2 != CoherentGdcmFileList.end(); it2++, n++)
-    {
+   //VC++ don't understand what scope is !! it -> it2
+   for (GdcmHeaderList::const_iterator it2  = CoherentGdcmFileList.begin();
+        it2 != CoherentGdcmFileList.end(); ++it2, ++n)
+   {
       //2*n sort algo !!
       //Assumption: all files are present (no one missing)
       pos = (int)( fabs( (distlist[n]-min)/step) + .5 );
             
       CoherentGdcmFileVector[pos] = *it2;
-    }
+   }
 
-  CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
-  
-  //VC++ don't understand what scope is !! it -> it3
-  for (std::vector<gdcmHeader*>::iterator it3  = CoherentGdcmFileVector.begin();
-        it3 != CoherentGdcmFileVector.end(); it3++)
-  {
-    CoherentGdcmFileList.push_back( *it3 );
-  }
-
-  distlist.clear();
-  CoherentGdcmFileVector.clear();
-  delete[] cosines;
+   CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
   
-  return true;
+   //VC++ don't understand what scope is !! it -> it3
+   for (GdcmHeaderVector::const_iterator it3  = CoherentGdcmFileVector.begin();
+        it3 != CoherentGdcmFileVector.end(); ++it3)
+   {
+      CoherentGdcmFileList.push_back( *it3 );
+   }
+
+   distlist.clear();
+   CoherentGdcmFileVector.clear();
+   delete[] cosines;
+
+   return true;
 }
 
 /**
@@ -226,54 +242,56 @@ bool gdcmSerieHeader::ImagePositionPatientOrdering()
  * @return false only if the header is bugged !
  */
 
-bool gdcmSerieHeader::ImageNumberOrdering() {
-  int min, max, pos;
-  int n = 0;//CoherentGdcmFileList.size() is a O(N) operation !!
-  unsigned char *partition;
+bool gdcmSerieHeader::ImageNumberOrdering() 
+{
+   int min, max, pos;
+   int n = 0;//CoherentGdcmFileList.size() is a O(N) operation !!
+   unsigned char *partition;
   
-  std::list<gdcmHeader*>::iterator it  = CoherentGdcmFileList.begin();
-  min = max = (*it)->GetImageNumber();
-
-  for (; it != CoherentGdcmFileList.end(); it++, n++)
-  {
-    pos = (*it)->GetImageNumber();
-
-    //else
-    min = (min < pos) ? min : pos;
-  }
-
-  //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
-  partition = new unsigned char[n];
-  memset(partition, 0, n);
-
-  std::vector<gdcmHeader*> CoherentGdcmFileVector(n);
-
-  //VC++ don't understand what scope is !! it -> it2
-  for (std::list<gdcmHeader*>::iterator it2  = CoherentGdcmFileList.begin();
-        it2 != CoherentGdcmFileList.end(); it2++)
-  {
-    pos = (*it2)->GetImageNumber();
-    CoherentGdcmFileVector[pos - min] = *it2;
-    partition[pos - min]++;
-  }
+   GdcmHeaderList::const_iterator it = CoherentGdcmFileList.begin();
+   min = max = (*it)->GetImageNumber();
+
+   for (; it != CoherentGdcmFileList.end(); ++it, ++n)
+   {
+      pos = (*it)->GetImageNumber();
+
+      //else
+      min = (min < pos) ? min : pos;
+   }
+
+   //bzeros(partition, n); //Cette fonction est déconseillée, utilisez plutôt memset.
+   partition = new unsigned char[n];
+   memset(partition, 0, n);
+
+   GdcmHeaderVector CoherentGdcmFileVector(n);
+
+   //VC++ don't understand what scope is !! it -> it2
+   for (GdcmHeaderList::const_iterator it2 = CoherentGdcmFileList.begin();
+        it2 != CoherentGdcmFileList.end(); ++it2)
+   {
+      pos = (*it2)->GetImageNumber();
+      CoherentGdcmFileVector[pos - min] = *it2;
+      partition[pos - min]++;
+   }
   
-  unsigned char mult = 1;
-  for(int i=0; i<n ; i++)
-  {
-    mult *= partition[i];
-  }
-
-  //VC++ don't understand what scope is !! it -> it3
-  CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
-  for (std::vector<gdcmHeader*>::iterator it3  = CoherentGdcmFileVector.begin();
-        it3 != CoherentGdcmFileVector.end(); it3++)
-  {
-    CoherentGdcmFileList.push_back( *it3 );
-  }
-  CoherentGdcmFileVector.clear();
+   unsigned char mult = 1;
+   for( int i=0; i<n ; i++ )
+   {
+      mult *= partition[i];
+   }
+
+   //VC++ don't understand what scope is !! it -> it3
+   CoherentGdcmFileList.clear();  //this doesn't delete list's element, node only
+   for ( GdcmHeaderVector::const_iterator it3 = CoherentGdcmFileVector.begin();
+         it3 != CoherentGdcmFileVector.end(); ++it3 )
+   {
+      CoherentGdcmFileList.push_back( *it3 );
+   }
+   CoherentGdcmFileVector.clear();
   
-  delete[] partition;
-  return (mult!=0);
+   delete[] partition;
+
+   return (mult != 0);
 }
 
 
@@ -282,10 +300,11 @@ bool gdcmSerieHeader::ImageNumberOrdering() {
  * \brief sorts the images, according to their File Name
  * @return false only if the header is bugged !
  */
- bool gdcmSerieHeader::FileNameOrdering() {
-  //using the sort
-  //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
-  return true;
+bool gdcmSerieHeader::FileNameOrdering()
+{
+   //using the sort
+   //sort(CoherentGdcmFileList.begin(), CoherentGdcmFileList.end());
+   return true;
 }
 
 //-----------------------------------------------------------------------------
index 18d950e5c2c30c6756111ef0f5b5c3001cc13165..1cf6601558e17abe0f9c71773ebd096030770f2d 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmHeaderHelper.h,v $
   Language:  C++
-  Date:      $Date: 2004/06/21 21:51:01 $
-  Version:   $Revision: 1.18 $
+  Date:      $Date: 2004/06/25 20:48:25 $
+  Version:   $Revision: 1.19 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
  * - This class should be used for a stack of 2D dicom images.
  * - For a multiframe dicom image better use directly gdcmHeaderHelper
 */
-class GDCM_EXPORT gdcmSerieHeader {
+class GDCM_EXPORT gdcmSerieHeader 
+{
 public:
-    gdcmSerieHeader() {};
+    gdcmSerieHeader();
     ~gdcmSerieHeader();
 
-   void AddFileName(std::string filename); //should return bool or throw error ?
+   void AddFileName(std::string const & filename); //should return bool or throw error ?
    void AddGdcmFile(gdcmHeader *file);
-   void SetDirectory(std::string dir);
+   void SetDirectory(std::string const & dir);
    void OrderGdcmFileList();
    
    inline gdcmHeader *GetGdcmHeader()
    {
-      //Assume all element in the list have the same global infos
+      // Assume all element in the list have the same global infos
+      // Assume the list is not empty
       return CoherentGdcmFileList.front();
    }
-   
-   std::list<gdcmHeader*>& GetGdcmFileList();
+
+   typedef std::list<gdcmHeader* > GdcmHeaderList;
+   /**
+    * \brief Gets the *coherent* File List
+    * @return the *coherent* File List
+    */
+   const GdcmHeaderList& GetGdcmFileList()
+   {
+     return CoherentGdcmFileList;
+   }
 
 private:
    bool ImagePositionPatientOrdering();
    bool ImageNumberOrdering();
    bool FileNameOrdering();
    
-   std::list<gdcmHeader*> CoherentGdcmFileList;
+   GdcmHeaderList CoherentGdcmFileList;
 };
 
 //-----------------------------------------------------------------------------