]> Creatis software - creaImageIO.git/blob - src/creaImageIOTreeComparators.h
#3185 creaImageIO Feature New Normal - Clean code
[creaImageIO.git] / src / creaImageIOTreeComparators.h
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image 
5 #                        pour la Santé)
6 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 #
10 #  This software is governed by the CeCILL-B license under French law and 
11 #  abiding by the rules of distribution of free software. You can  use, 
12 #  modify and/ or redistribute the software under the terms of the CeCILL-B 
13 #  license as circulated by CEA, CNRS and INRIA at the following URL 
14 #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
15 #  or in the file LICENSE.txt.
16 #
17 #  As a counterpart to the access to the source code and  rights to copy,
18 #  modify and redistribute granted by the license, users are provided only
19 #  with a limited warranty  and the software's author,  the holder of the
20 #  economic rights,  and the successive licensors  have only  limited
21 #  liability. 
22 #
23 #  The fact that you are presently reading this means that you have had
24 #  knowledge of the CeCILL-B license and that you accept its terms.
25 # ------------------------------------------------------------------------
26 */
27
28
29 #ifndef __creaImageIOTreeNodeComparators_h_INCLUDED__
30 #define __creaImageIOTreeNodeComparators_h_INCLUDED__
31
32 #include <vector>
33 #include <iostream>
34
35 namespace creaImageIO
36 {
37
38   namespace tree
39   {
40
41     
42     class Node;
43     
44     
45         /**
46         * \ingroup Tree
47         */
48   //=====================================================================
49     /// Abstract definition of a comparator of Node
50   struct Comparator
51   {
52     virtual ~Comparator() {}
53     virtual bool operator() (Node* const & x, Node* const & y) = 0;
54   };
55   //=====================================================================
56
57
58
59   //=====================================================================
60     /// Abstract Comparator whose order can be reversed
61   struct ComparatorWithOrder : public Comparator
62   {
63     ComparatorWithOrder(bool reverse_order = false) 
64       : mReverseOrder(reverse_order) {}
65     virtual ~ComparatorWithOrder() {}
66
67
68     virtual bool compare(Node* const &, Node* const &) = 0;
69
70     virtual bool operator() (Node* const & x, Node* const & y)
71     {
72       if (mReverseOrder) return this->compare(y,x);
73       return this->compare(x,y);
74     };
75
76     bool mReverseOrder;
77   };
78   //=====================================================================
79
80
81
82   //=====================================================================
83     /// A Comparator which stores a vector of Comparators and 
84                                /// which performs lexicographical comparison
85   class LexicographicalComparator : public Comparator
86   {
87   public:
88     LexicographicalComparator(const std::string& name) 
89       : mName(name) {}
90     ~LexicographicalComparator() {}
91
92     const std::string& GetName() const { return mName; }
93     void SetName(const std::string& s) { mName = s; }
94     void Clear() { mComparator.clear(); }
95     void DeleteComparators() 
96     { 
97        std::vector<Comparator*>::iterator i;
98        for (i =mComparator.begin();
99             i!=mComparator.end();
100             ++i)
101          {
102            delete *i;
103          }
104        mComparator.clear(); 
105     }
106     void Add(Comparator* c) { mComparator.push_back(c); }
107     
108     bool operator() (Node* const & x, Node * const & y);
109
110   private:
111     std::string mName;
112     std::vector<Comparator*> mComparator;
113   };
114   //=====================================================================
115
116
117
118
119   //=================================================================== 
120     /// Comparator which compares the values of a given Attribute of the Nodes which is decoded as an int value
121   struct IntComparator : 
122     public ComparatorWithOrder
123   {    
124     IntComparator(const std::string& key,
125                                      bool reverse_order)
126       :
127       ComparatorWithOrder(reverse_order),
128       mKey(key)
129     {}
130     virtual bool compare(Node* const & x, Node* const & y);
131
132   private:
133     std::string mKey;
134   };
135   //===================================================================
136
137   //===================================================================
138     /// Comparator which compares the values of a given Attribute of the Nodes which is decoded as an float value
139   struct FloatComparator : 
140     public ComparatorWithOrder
141   {    
142     FloatComparator(const std::string& key,
143                                        bool reverse_order )
144       : 
145       ComparatorWithOrder(reverse_order),
146       mKey(key)
147     {}
148
149     virtual bool compare(Node* const & x, Node* const & y);
150
151   private:
152     std::string mKey;
153   };
154   //===================================================================
155
156   //===================================================================
157     /// Comparator which compares the values of a given Attribute of the Nodes which is decoded as a string value
158   struct StringComparator : 
159     public ComparatorWithOrder
160   {    
161     StringComparator(const std::string& key,
162                                    bool reverse_order )
163       : 
164       ComparatorWithOrder(reverse_order),
165       mKey(key)
166     {}
167     
168     virtual bool compare(Node* const & x, Node* const & y);
169     
170   private:
171     std::string mKey;
172   };
173   //===================================================================
174
175   //===================================================================
176 #define INT_FIELD_COMP(NAME,FIELD)      \
177   struct Node##NAME##Comparator :       \
178     public IntComparator        \
179   {                                             \
180     Node##NAME##Comparator(bool o = false) :    \
181       IntComparator(FIELD,o)    \
182     {}                                          \
183   }
184   //==================================================================
185   
186   //===================================================================
187 #define FLOAT_FIELD_COMP(NAME,FIELD)            \
188   struct Node##NAME##Comparator :       \
189     public FloatComparator      \
190   {                                             \
191     Node##NAME##Comparator(bool o = false) :    \
192       FloatComparator(FIELD,o)  \
193     {}                                          \
194   }
195   //===================================================================
196
197   //===================================================================
198 #define STRING_FIELD_COMP(NAME,FIELD)           \
199   struct Node##NAME##Comparator :       \
200     public StringComparator     \
201   {                                             \
202     Node##NAME##Comparator(bool o = false) :    \
203       StringComparator(FIELD,o) \
204     {}                                          \
205   }
206   //===================================================================
207
208
209
210   //===================================================================
211   // Patient comparators
212   ///Compares the names of the patients
213   STRING_FIELD_COMP(PatientName,"A0010_0010");
214   ///Compares the sex of the patients
215   STRING_FIELD_COMP(PatientSex, "A0010_0040");
216   ///Compares the birthdays of the patients
217   STRING_FIELD_COMP(PatientBirthday, "A0010_0030");
218   //===================================================================
219
220   //===================================================================
221   // Study comparators
222   ///Compares the dates of the studies
223   STRING_FIELD_COMP(StudyDate,"A0008_0020");
224   ///Compares the description of the studies
225   STRING_FIELD_COMP(StudyDescription,"A0008_1030");
226   //===================================================================
227
228   //===================================================================
229   // Series comparators
230   ///Compares the modality of the series
231   STRING_FIELD_COMP(Modality,"A0008_0060");
232   ///Compares the description of the series
233   STRING_FIELD_COMP(SeriesDescription,"A0008_103E");
234   ///Compares the date of the series
235   STRING_FIELD_COMP(SeriesDate,"A0008_0021");
236   //===================================================================
237
238   //===================================================================
239   // Image comparators
240   ///Compares the number of the images
241   INT_FIELD_COMP(ImageNumber,"A0020_0013");
242   ///Compares the location of the images
243   FLOAT_FIELD_COMP(SliceLocation,"A0020_1041");
244   ///Compares the filename of the images
245   STRING_FIELD_COMP(FullFileName,"FullFileName");
246   //===================================================================
247   } // namespace tree
248
249 } // namespace creaImageIO
250
251
252
253 #endif // #ifndef __creaImageIOComparators_h_INCLUDED__