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