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