]> Creatis software - gdcm.git/blobdiff - src/gdcmSerieHelper.cxx
COMP: Forgot to change string to TagKey...
[gdcm.git] / src / gdcmSerieHelper.cxx
index 32389631c0f909288eb548e54af200d2eec937ac..9ac3369398b74da8fdcf28759746825f0eea2d3d 100644 (file)
@@ -3,8 +3,8 @@
   Program:   gdcm
   Module:    $RCSfile: gdcmSerieHelper.cxx,v $
   Language:  C++
-  Date:      $Date: 2005/05/26 18:49:46 $
-  Version:   $Revision: 1.7 $
+  Date:      $Date: 2005/06/24 10:55:59 $
+  Version:   $Revision: 1.10 $
                                                                                 
   Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
   l'Image). All rights reserved. See Doc/License.txt or
@@ -20,6 +20,7 @@
 #include "gdcmDirList.h"
 #include "gdcmFile.h"
 #include "gdcmDebug.h"
+#include "gdcmUtil.h"
 
 #include <math.h>
 #include <vector>
@@ -85,23 +86,50 @@ SerieHelper::~SerieHelper()
  */
 void SerieHelper::AddFileName(std::string const &filename)
 {
-   //directly use string and not const char*:
+   // Create a DICOM file
    File *header = new File( filename ); 
-   if( header->IsReadable() )
+   if ( header->IsReadable() )
    {
-      // 0020 000e UI REL Series Instance UID
-      std::string uid =  header->GetEntryValue (0x0020, 0x000e);
-      // if uid == GDCM_UNFOUND then consistently we should find GDCM_UNFOUND
-      // no need here to do anything special
+      int allrules = 1;
+      // First step the user has defined s set of rules for the DICOM he is looking for
+      // make sure the file correspond to his set of rules:
+      for(SerieRestrictions::iterator it = Restrictions.begin();
+          it != Restrictions.end();
+          ++it)
+      {
+         const Rule &r = *it;
+         const std::string s;// = header->GetEntryValue( r.first );
+         if ( !Util::DicomStringEqual(s, r.second.c_str()) )
+         {
+           // Argh ! This rule is unmatch let's just quit
+           allrules = 0;
+           break;
+         }
+      }
+      if ( allrules ) // all rules are respected:
+      {
+         // Alright ! we have a found a DICOM that match the user expectation. 
+         // Let's add it !
+
+         // 0020 000e UI REL Series Instance UID
+         const std::string &uid = header->GetEntryValue (0x0020, 0x000e);
+         // if uid == GDCM_UNFOUND then consistently we should find GDCM_UNFOUND
+         // no need here to do anything special
 
-      if ( CoherentFileListHT.count(uid) == 0 )
+         if ( CoherentFileListHT.count(uid) == 0 )
+         {
+            gdcmDebugMacro(" New Serie UID :[" << uid << "]");
+            // create a std::list in 'uid' position
+            CoherentFileListHT[uid] = new FileList;
+         }
+         // Current Serie UID and DICOM header seems to match add the file:
+         CoherentFileListHT[uid]->push_back( header );
+      }
+      else
       {
-         gdcmWarningMacro(" New Serie UID :[" << uid << "]");
-         // create a std::list in 'uid' position
-         CoherentFileListHT[uid] = new FileList;
+         // at least one rule was unmatch we need to deallocate the file:
+         delete header;
       }
-      // Current Serie UID and DICOM header seems to match add the file:
-      CoherentFileListHT[uid]->push_back( header );
    }
    else
    {
@@ -109,6 +137,20 @@ void SerieHelper::AddFileName(std::string const &filename)
       delete header;
    }
 }
+/**
+ * \brief add a rules for restricting a DICOM file to be in the serie we are
+ * trying to find. For example you can select only the DICOM file from a
+ * directory which would have a particular EchoTime==4.0.
+ * This method is a user level, value is not required to be formatted as a DICOM
+ * string
+ */
+void SerieHelper::AddRestriction(TagKey const &key, std::string const &value)
+{
+   Rule r;
+   r.first = key;
+   r.second = value;
+   Restrictions.push_back( r ); 
+}
 
 /**
  * \brief Sets the root Directory
@@ -133,19 +175,19 @@ void SerieHelper::SetDirectory(std::string const &dir, bool recursive)
  *          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 SerieHelper::OrderFileList(FileList *CoherentFileList)
+void SerieHelper::OrderFileList(FileList *coherentFileList)
 {
-   if( ImagePositionPatientOrdering( CoherentFileList ) )
+   if ( ImagePositionPatientOrdering( coherentFileList ) )
    {
       return ;
    }
-   else if( ImageNumberOrdering(CoherentFileList ) )
+   else if ( ImageNumberOrdering(coherentFileList ) )
    {
       return ;
    }
    else  
    {
-      FileNameOrdering(CoherentFileList );
+      FileNameOrdering(coherentFileList );
    }
 }
 
@@ -156,7 +198,7 @@ void SerieHelper::OrderFileList(FileList *CoherentFileList)
 FileList *SerieHelper::GetFirstCoherentFileList()
 {
    ItListHt = CoherentFileListHT.begin();
-   if( ItListHt != CoherentFileListHT.end() )
+   if ( ItListHt != CoherentFileListHT.end() )
       return ItListHt->second;
    return NULL;
 }
@@ -179,7 +221,7 @@ FileList *SerieHelper::GetNextCoherentFileList()
 /**
  * \brief   Get the Coherent Files list according to its Serie UID
  * @param SerieUID SerieUID
- * \return  pointer to the Coherent Filseslist if found, otherwhise NULL
+ * \return  pointer to the Coherent Files list if found, otherwhise NULL
  */
 FileList *SerieHelper::GetCoherentFileList(std::string SerieUID)
 {
@@ -199,6 +241,7 @@ FileList *SerieHelper::GetCoherentFileList(std::string SerieUID)
  *   -# Image Position Patient
  *   -# Image Number
  *   -# More to come :-)
+ * WARNING : FileList = std::vector<File* >
  * @param fileList Coherent File list (same Serie UID) to sort
  * @return false only if the header is bugged !
  */
@@ -220,7 +263,7 @@ bool SerieHelper::ImagePositionPatientOrdering( FileList *fileList )
          it = fileList->begin();
          it != fileList->end(); ++it )
    {
-      if( first ) 
+      if ( first ) 
       {
          (*it)->GetImageOrientationPatient( cosines );
       
@@ -276,7 +319,7 @@ bool SerieHelper::ImagePositionPatientOrdering( FileList *fileList )
    // gdcmAssertMacro( CoherentFileVector.capacity() >= n );
 
    // Find out if min/max are coherent
-   if( min == max )
+   if ( min == max )
      {
      gdcmWarningMacro( "Looks like all images have the exact same image position...");
      return false;
@@ -351,7 +394,7 @@ bool SerieHelper::ImageNumberOrdering(FileList *fileList)
    }
 
    // Find out if image numbers are coherent (consecutive)
-   if( min == max || max == 0 || max >= (n+min))
+   if ( min == max || max == 0 || max >= (n+min) )
       return false;
 
    std::sort(fileList->begin(), fileList->end(), SerieHelper::ImageNumberLessThan );
@@ -380,7 +423,7 @@ bool SerieHelper::FileNameOrdering(FileList *fileList)
 /**
  * \brief   Canonical printer.
  */
-void SerieHelper::Print(std::ostream &os, std::string const & indent)
+void SerieHelper::Print(std::ostream &os, std::string const &indent)
 {
    // For all the Coherent File lists of the gdcm::Serie
    CoherentFileListmap::iterator itl = CoherentFileListHT.begin();