]> Creatis software - gdcm.git/blob - src/gdcmSerieHelper.h
SerieHelper Normalization : stage 1
[gdcm.git] / src / gdcmSerieHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSerieHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2005/10/17 09:52:41 $
7   Version:   $Revision: 1.23 $
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    /// XCoherent stands for 'Extra Coherent', 
34    /// (The name 'Coherent' would be enough but it was used before;
35    /// I don't want to put a bomb in the code)
36    /// Any 'better name' is welcome !
37 typedef std::map<std::string, FileList *> XCoherentFileSetmap;
38    
39 typedef bool (*BOOL_FUNCTION_PFILE_PFILE_POINTER)(File *, File *);
40
41 //-----------------------------------------------------------------------------
42 /**
43  * \brief  
44  * - This class should be used for a stack of 2D dicom images.
45  *   It allows to explore (recursively or not) a directory and 
46  *   makes a set of 'Coherent Files' lists (coherent : same SerieUID)
47  *   It allows :
48  *   - to sort any of the Coherent File list on the image position.
49  *   - to split any of the Single SerieUID Filesets (better use this name than
50  *   'Coherent File List' : it's NOT a std::list, files are NOT coherent ...)
51  *    into several XCoherent Filesets 
52  *   XCoherent stands for 'Extra Coherent' (same orientation, or same position)
53  */
54 class GDCM_EXPORT SerieHelper 
55 {
56 public:
57    // SingleSerieUIDFileSetmap replaces the former CoherentFileListmap
58    // ( List were actually std::vectors, and wher no coherent at all :
59    //   They were only Single SeriesInstanceUID File sets)
60    typedef std::map<std::string, FileList *> SingleSerieUIDFileSetmap;
61
62    typedef std::vector<File* > FileVector;
63    
64    SerieHelper();
65    ~SerieHelper();
66    void Print(std::ostream &os = std::cout, std::string const &indent = "" );
67
68    /// \todo should return bool or throw error ?
69    void AddFileName(std::string const &filename);
70    void AddGdcmFile(File *header);
71
72    void SetDirectory(std::string const &dir, bool recursive=false);
73    bool IsCoherent(FileList *fileSet);
74    void OrderFileList(FileList *fileSet);
75    
76    /// \brief Gets the FIRST Single SerieUID Fileset.
77    ///        Deprecated; kept not to break the API
78    /// \note Caller must call OrderFileList first
79    /// @return the (first) Single SerieUID Fileset
80    const FileList &GetFileList()
81                            { return *SingleSerieUIDFileSetHT.begin()->second; }
82   
83    GDCM_LEGACY(   FileList *GetFirstCoherentFileList()  );
84    GDCM_LEGACY(   FileList *GetNextCoherentFileList()   );
85    GDCM_LEGACY(   FileList *GetCoherentFileList(std::string serieUID)  );
86
87    FileList *GetFirstSingleSerieUIDFileSet();
88    FileList *GetNextSingleSerieUIDFileSet();
89    FileList *GetSingleSerieUIDFileSet(std::string serieUID);
90
91    /// All the following allow user to restrict DICOM file to be part
92    /// of a particular serie
93    GDCM_LEGACY( void AddRestriction(TagKey const &key, std::string const &value) );
94    void AddRestriction(uint16_t group, uint16_t elem, 
95                        std::string const &value, int op);
96  
97 /**
98  * \brief Sets the LoadMode as a boolean string. 
99  *        LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
100  *        ... (nothing more, right now)
101  *        WARNING : before using LD_NOSHADOW, be sure *all* your files
102  *        contain accurate values in the 0x0000 element (if any) 
103  *        of *each* Shadow Group. The parser will fail if the size is wrong !
104  * @param   mode Load mode to be used    
105  */
106    void SetLoadMode (int mode) { LoadMode = mode; }
107
108 /// Brief User wants the files to be sorted Direct Order (default value)
109    void SetSortOrderToDirect()  { DirectOrder = true;  }
110
111 /// Brief User wants the files to be sorted Reverse Order 
112    void SetSortOrderToReverse() { DirectOrder = false; }
113
114    /// to allow user to give is own comparison function
115    void SetUserLessThanFunction( BOOL_FUNCTION_PFILE_PFILE_POINTER userFunc ) 
116                         { UserLessThanFunction = userFunc; }  
117
118    XCoherentFileSetmap SplitOnOrientation(FileList *fileSet); 
119    XCoherentFileSetmap SplitOnPosition(FileList *fileSet); 
120    XCoherentFileSetmap SplitOnTagValue(FileList *fileSet,
121                                                  uint16_t group, uint16_t element);
122 private:
123    void ClearAll();
124    bool UserOrdering(FileList *fileSet);
125    bool ImagePositionPatientOrdering(FileList *fileSet);
126    bool ImageNumberOrdering(FileList *fileSet);
127    bool FileNameOrdering(FileList *fileSet);
128    
129    static bool ImageNumberLessThan(File *file1, File *file2);
130    static bool ImageNumberGreaterThan(File *file1, File *file2);
131    static bool FileNameLessThan(File *file1, File *file2);
132    static bool FileNameGreaterThan(File *file1, File *file2);
133
134 //Attributes:
135    
136    SingleSerieUIDFileSetmap SingleSerieUIDFileSetHT;
137    SingleSerieUIDFileSetmap::iterator ItFileSetHt;
138    
139    typedef std::pair<TagKey, std::string> Rule;
140    typedef std::vector<Rule> SerieRestrictions;
141    SerieRestrictions Restrictions;
142    
143    // New style for (extented) Rules (Moreover old one doesn't compile)
144    typedef struct {
145       uint16_t group;
146       uint16_t elem;
147       std::string value;
148       int op;
149    } ExRule;
150    typedef std::vector<ExRule> SerieExRestrictions;
151    SerieExRestrictions ExRestrictions;
152
153    /// \brief Bit string integer (each one considered as a boolean)
154    ///        Bit 0 : Skip Sequences,    if possible
155    ///        Bit 1 : Skip Shadow Groups if possible
156    ///        Probabely, some more to add
157    int LoadMode;
158
159    /// \brief whether we want to sort in direct order or not (reverse order).
160    ///        To be used by aware user only
161    bool DirectOrder;
162
163    /// \brief If user knows more about his images than gdcm does,
164    ///        he may supply his own comparison function.
165     BOOL_FUNCTION_PFILE_PFILE_POINTER UserLessThanFunction;
166
167 };
168
169 } // end namespace gdcm
170
171 //-----------------------------------------------------------------------------
172 #endif