]> Creatis software - gdcm.git/blob - src/gdcmSerieHelper.h
Reverse order sorting now works, even with user supplied function.
[gdcm.git] / src / gdcmSerieHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSerieHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/08/30 14:15:34 $
7   Version:   $Revision: 1.18 $
8                                                                                 
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.
12                                                                                 
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.
16                                                                                 
17 =========================================================================*/
18
19 #ifndef GDCMSERIEHELPER_H
20 #define GDCMSERIEHELPER_H
21
22 #include "gdcmCommon.h" 
23 #include "gdcmDebug.h"  // for LEGACY
24  
25 #include <vector>
26 #include <iostream>
27 #include <map>
28
29 namespace gdcm 
30 {
31 class File;
32 typedef std::vector<File* > FileList;
33
34 typedef bool (*BOOL_FUNCTION_PFILE_PFILE_POINTER)(File *, File *);
35
36 //-----------------------------------------------------------------------------
37 /**
38  * \brief  
39  * - This class should be used for a stack of 2D dicom images.
40  *   It allows to explore (recursively or not) a directory and 
41  *   makes a set of 'Coherent Files' list (coherent : same Serie UID)
42  *   It allows to sort any of the Coherent File list on the image position
43  */
44 class GDCM_EXPORT SerieHelper 
45 {
46 public:
47    typedef std::map<std::string, FileList *> CoherentFileListmap;
48    typedef std::vector<File* > FileVector;
49
50    SerieHelper();
51    ~SerieHelper();
52    void Print(std::ostream &os = std::cout, std::string const &indent = "" );
53
54    /// \todo should return bool or throw error ?
55    void AddFileName(std::string const &filename);
56    void AddGdcmFile(File *header);
57
58    void SetDirectory(std::string const &dir, bool recursive=false);
59    bool IsCoherent(FileList *coherentFileList);
60    void OrderFileList(FileList *coherentFileList);
61    
62    /// \brief Gets the FIRST *coherent* File List.
63    ///        Deprecated; kept not to break the API
64    /// \note Caller must call OrderFileList first
65    /// @return the (first) *coherent* File List
66    const FileList &GetFileList() { return *CoherentFileListHT.begin()->second; }
67   
68    FileList *GetFirstCoherentFileList();
69    FileList *GetNextCoherentFileList();
70    FileList *GetCoherentFileList(std::string serieUID);
71
72    /// All the following allow user to restrict DICOM file to be part
73    /// of a particular serie
74    GDCM_LEGACY( void AddRestriction(TagKey const &key, std::string const &value) );
75    void AddRestriction(uint16_t group, uint16_t elem, 
76                        std::string const &value, int op);
77   
78 /**
79  * \brief Sets the LoadMode as a boolean string. 
80  *        NO_SEQ, NO_SHADOW, NO_SHADOWSEQ
81  *        (nothing more, right now)
82  *        WARNING : before using NO_SHADOW, be sure *all* your files
83  *        contain accurate values in the 0x0000 element (if any) 
84  *        of *each* Shadow Group. The parser will fail if the size is wrong !
85  * @param   mode Load mode to be used    
86  */
87    void SetLoadMode (int mode) { LoadMode = mode; }
88
89 /// Brief User wants the files to be sorted Direct Order (default value)
90    void SetSortOrderToDirect()  { DirectOrder = true;  }
91
92 /// Brief User wants the files to be sorted Reverse Order 
93    void SetSortOrderToReverse() { DirectOrder = false; }
94
95    /// to allow user to give is own comparison function
96    void SetUserLessThanFunction( BOOL_FUNCTION_PFILE_PFILE_POINTER userFunc ) 
97                         { UserLessThanFunction = userFunc; }   
98 private:
99    bool UserOrdering(FileList *coherentFileList);
100    bool ImagePositionPatientOrdering(FileList *coherentFileList);
101    bool ImageNumberOrdering(FileList *coherentFileList);
102    bool FileNameOrdering(FileList *coherentFileList);
103    
104    static bool ImageNumberLessThan(File *file1, File *file2);
105    static bool ImageNumberGreaterThan(File *file1, File *file2);
106    static bool FileNameLessThan(File *file1, File *file2);
107    static bool FileNameGreaterThan(File *file1, File *file2);
108
109 //Attributes:
110    CoherentFileListmap CoherentFileListHT;
111    CoherentFileListmap::iterator ItListHt;
112
113    typedef std::pair<TagKey, std::string> Rule;
114    typedef std::vector<Rule> SerieRestrictions;
115    SerieRestrictions Restrictions;
116    
117    // New style for (extented) Rules (Moreover old one doesn't compile)
118    typedef struct {
119       uint16_t group;
120       uint16_t elem;
121       std::string value;
122       int op;
123    } ExRule;
124    typedef std::vector<ExRule> SerieExRestrictions;
125    SerieExRestrictions ExRestrictions;
126
127    /// \brief Bit string integer (each one considered as a boolean)
128    ///        Bit 0 : Skip Sequences,    if possible
129    ///        Bit 1 : Skip Shadow Groups if possible
130    ///        Probabely, some more to add
131    int LoadMode;
132
133    /// \brief whether we want to sort in direct order or not (reverse order).
134    ///        To be used by aware user only
135    bool DirectOrder;
136
137    /// \brief If user knows more about his images than gdcm does,
138    ///        he may supply his own comparison function.
139     BOOL_FUNCTION_PFILE_PFILE_POINTER UserLessThanFunction;
140
141 };
142
143 } // end namespace gdcm
144
145 //-----------------------------------------------------------------------------
146 #endif