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