]> Creatis software - gdcm.git/blob - src/gdcmSerieHelper.h
Forget to remove cout ..
[gdcm.git] / src / gdcmSerieHelper.h
1 /*=========================================================================
2                                                                                 
3   Program:   gdcm
4   Module:    $RCSfile: gdcmSerieHelper.h,v $
5   Language:  C++
6   Date:      $Date: 2006/03/30 16:51:29 $
7   Version:   $Revision: 1.38 $
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 "gdcmRefCounter.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    typedef std::vector<File* > FileList;
35 #ifndef GDCM_LEGACY_REMOVE
36 typedef std::vector<File* > GdcmFileList;
37 #endif
38
39    /// \brief XCoherent stands for 'Extra Coherent',
40    ///        (The name 'Coherent' would be enough but it was used before;
41    ///        I don't want to put a bomb in the code)
42    ///        Any 'better name' is welcome !
43    typedef std::map<std::string, FileList *> XCoherentFileSetmap;
44
45    typedef bool (*BOOL_FUNCTION_PFILE_PFILE_POINTER)(File *, File *);
46
47 //-----------------------------------------------------------------------------
48 /**
49  * \brief  
50  *  This class should be used for a stack of 2D dicom images.
51  *
52  *   - It allows to explore (recursively or not) a directory and 
53  *   makes a set of 'Single SerieUID Filesets' 
54  *   - It allows :
55  *   - - to sort any of the 'Single SerieUID Fileset' on the image position.
56  *   - - to split any of the Single SerieUID Filesets (better use this name than
57  *   'CoherentFileList' : it's NOT a std::list, files are NOT coherent ...)
58  *    into several XCoherent Filesets 
59  *   XCoherent stands for 'Extra Coherent' (same orientation, or same position)
60  */
61 class GDCM_EXPORT SerieHelper  : public RefCounter
62 {
63    gdcmTypeMacro(SerieHelper);
64    
65 public:
66    /// SingleSerieUIDFileSetmap replaces the former CoherentFileListmap
67    /// ( List were actually std::vectors, and where no coherent at all :
68    ///   They were only Single SeriesInstanceUID File sets)
69    typedef std::map<std::string, FileList *> SingleSerieUIDFileSetmap;
70
71    typedef std::vector<File* > FileVector;
72
73 /// \brief Constructs a SerieHelper with a RefCounter
74     static SerieHelper *New() {return new SerieHelper();}
75      
76    virtual ~SerieHelper();
77    void Print(std::ostream &os = std::cout, std::string const &indent = "" );
78
79    /// \todo should return bool or throw error ?
80    void AddFileName(std::string const &filename);
81    bool AddFile(File *header);
82 #ifndef GDCM_LEGACY_REMOVE
83    bool AddGdcmFile(File* header) { return AddFile(header); }
84 #endif
85
86    void SetDirectory(std::string const &dir, bool recursive=false);
87    bool IsCoherent(FileList *fileSet);
88    void OrderFileList(FileList *fileSet);
89    void Clear() { ClearAll(); }
90
91    /// \brief Gets the FIRST Single SerieUID Fileset.
92    ///        Deprecated; kept not to break the API
93    /// \note Caller must call OrderFileList first
94    /// @return the (first) Single SerieUID Fileset
95    const FileList &GetFileList()
96                            { return *SingleSerieUIDFileSetHT.begin()->second; }
97   
98    GDCM_LEGACY(   FileList *GetFirstCoherentFileList()  );
99    GDCM_LEGACY(   FileList *GetNextCoherentFileList()   );
100    GDCM_LEGACY(   FileList *GetCoherentFileList(std::string serieUID)  );
101
102    FileList *GetFirstSingleSerieUIDFileSet();
103    FileList *GetNextSingleSerieUIDFileSet();
104    FileList *GetSingleSerieUIDFileSet(std::string serieUID);
105    /// brief returns the 'Series Instance UID' Single SerieUID FileSet
106    std::string GetCurrentSerieUIDFileSetUID()
107                              { return  (*ItFileSetHt).first; }
108      
109    /// \brief returns the distance between the 2 first -adjacent- slices, 
110    ///        along the slice normal; -1.0 if process failed
111    ///        Computed during  ImagePositionPatientOrdering() process.
112    ///        To be use *immediately after* ImagePositionPatientOrdering()
113    ///        (it's a member of the FileHelper, *not* of the FileList !)   
114    double GetZSpacing() { return ZSpacing; }
115    
116    /// All the following allow user to restrict DICOM file to be part
117    /// of a particular serie
118    /// \todo : find a trick to allow user to say the restrictions are ored
119    ///         (not only anded) 
120    ///         ex : keep the images whose SerieNumber is 101 or 102 or 103.
121    void AddRestriction(TagKey const &key);
122    void AddRestriction(TagKey const &key, std::string const &value, int op);
123    void AddRestriction(uint16_t group, uint16_t elem, std::string const &value,
124                                                                     int op);
125
126    /// \brief Use additional series information such as ProtocolName
127    ///        and SeriesName to identify when a single SeriesUID contains
128    ///        multiple 3D volumes - as can occur with perfusion and DTI imaging
129    void SetUseSeriesDetails( bool useSeriesDetails )
130      {
131      m_UseSeriesDetails = useSeriesDetails;
132      }
133    bool GetUseSeriesDetails()
134      {
135      return m_UseSeriesDetails;
136      }
137    /// \brief This function will add the following DICOM tag as being part of a
138    /// 'fake' uid. This is usefull when the Serie UID is not enough to disseminate
139    /// into multiple sub serie when needed:
140    /// 0020 0011 Series Number
141    /// 0018 0024 Sequence Name
142    /// 0018 0050 Slice Thickness
143    /// 0028 0010 Rows
144    /// 0028 0011 Columns
145    void CreateDefaultUniqueSeriesIdentifier();
146
147    void AddSeriesDetail(uint16_t group, uint16_t elem, bool convert);
148
149    std::string CreateUserDefinedFileIdentifier( File * inFile );
150
151    /// \brief Create a string that uniquely identifies a series.   By default
152    //         uses the SeriesUID.   If UseSeriesDetails(true) has been called,
153    //         then additional identifying information is used.
154    std::string CreateUniqueSeriesIdentifier( File * inFile );
155  
156 /**
157  * \brief Sets the LoadMode as a boolean string. 
158  *        LD_NOSEQ, LD_NOSHADOW, LD_NOSHADOWSEQ
159  *        ... (nothing more, right now)
160  *        WARNING : before using LD_NOSHADOW, be sure *all* your files
161  *        contain accurate values in the 0x0000 element (if any) 
162  *        of *each* Shadow Group. The parser will fail if the size is wrong !
163  * @param   mode Load mode to be used    
164  */
165    void SetLoadMode (int mode) { LoadMode = mode; }
166
167 /// Brief User wants the files to be sorted Direct Order (default value)
168    void SetSortOrderToDirect()  { DirectOrder = true;  }
169
170 /// Brief User wants the files to be sorted Reverse Order 
171    void SetSortOrderToReverse() { DirectOrder = false; }
172
173    /// to allow user to give is own comparison function
174    void SetUserLessThanFunction( BOOL_FUNCTION_PFILE_PFILE_POINTER userFunc ) 
175                         { UserLessThanFunction = userFunc; }  
176
177    XCoherentFileSetmap SplitOnOrientation(FileList *fileSet); 
178    XCoherentFileSetmap SplitOnPosition(FileList *fileSet); 
179    XCoherentFileSetmap SplitOnTagValue(FileList *fileSet,
180                                                uint16_t group, uint16_t element);
181 protected :
182    SerieHelper();
183    
184 private:
185    void ClearAll();
186    bool UserOrdering(FileList *fileSet);
187    bool ImagePositionPatientOrdering(FileList *fileSet);
188    bool ImageNumberOrdering(FileList *fileSet);
189    bool FileNameOrdering(FileList *fileSet);
190    
191    static bool ImageNumberLessThan(File *file1, File *file2);
192    static bool ImageNumberGreaterThan(File *file1, File *file2);
193    static bool FileNameLessThan(File *file1, File *file2);
194    static bool FileNameGreaterThan(File *file1, File *file2);
195
196 //Attributes:
197    
198    SingleSerieUIDFileSetmap SingleSerieUIDFileSetHT;
199    SingleSerieUIDFileSetmap::iterator ItFileSetHt;
200
201 #ifndef GDCM_LEGACY_REMOVE
202    typedef std::pair<TagKey, std::string> Rule;
203    typedef std::vector<Rule> SerieRestrictions;
204    SerieRestrictions Restrictions;
205 #endif
206
207    // New style for (extented) Rules
208    typedef struct {
209       uint16_t group;
210       uint16_t elem;
211       std::string value;
212       int op;
213    } ExRule;
214    typedef std::vector<ExRule> SerieExRestrictions;
215    SerieExRestrictions ExRestrictions;
216    SerieExRestrictions ExRefine;
217
218    typedef struct {
219       uint16_t group;
220       uint16_t elem;
221       bool convert;
222    } ExDetail;
223    typedef std::vector<ExDetail> SeriesExDetails; 
224    SeriesExDetails ExDetails;
225     
226    
227    /// \brief Bit string integer (each one considered as a boolean)
228    ///        Bit 0 : Skip Sequences,    if possible
229    ///        Bit 1 : Skip Shadow Groups if possible
230    ///        Probabely, some more to add
231    int LoadMode;
232
233    /// \brief whether we want to sort in direct order or not (reverse order).
234    ///        To be used by aware user only
235    bool DirectOrder;
236
237    /// \brief If user knows more about his images than gdcm does,
238    ///        he may supply his own comparison function.
239    BOOL_FUNCTION_PFILE_PFILE_POINTER UserLessThanFunction;
240
241    void Sort(FileList *fileList, bool (*pt2Func)( File *file1, File *file2) );
242
243    bool m_UseSeriesDetails;
244    /// \brief distance between the 2 first -adjancent- slices, along the slice normal
245    ///        Computed during  ImagePositionPatientOrdering() process.
246    ///        (we need to sort the whole image set to do that)
247    ///        To be used *immediately after* ImagePositionPatientOrdering()
248    ///        (it's a member of the FileHelper, *not* of the FileList !)  
249    double ZSpacing; 
250 };
251
252 } // end namespace gdcm
253
254 //-----------------------------------------------------------------------------
255 #endif