comparison OrthancServer/Sources/Search/HierarchicalMatcher.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents OrthancServer/Search/HierarchicalMatcher.cpp@2a170a8f1faf
children 05b8fd21089c
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeadersServer.h"
35 #include "HierarchicalMatcher.h"
36
37 #include "../../Core/Logging.h"
38 #include "../../Core/OrthancException.h"
39 #include "../../Core/DicomParsing/FromDcmtkBridge.h"
40 #include "../../Core/DicomParsing/ToDcmtkBridge.h"
41 #include "../OrthancConfiguration.h"
42
43 #include <dcmtk/dcmdata/dcfilefo.h>
44
45 namespace Orthanc
46 {
47 HierarchicalMatcher::HierarchicalMatcher(ParsedDicomFile& query)
48 {
49 bool caseSensitivePN;
50
51 {
52 OrthancConfiguration::ReaderLock lock;
53 caseSensitivePN = lock.GetConfiguration().GetBooleanParameter("CaseSensitivePN", false);
54 }
55
56 bool hasCodeExtensions;
57 Encoding encoding = query.DetectEncoding(hasCodeExtensions);
58 Setup(*query.GetDcmtkObject().getDataset(), caseSensitivePN, encoding, hasCodeExtensions);
59 }
60
61
62 HierarchicalMatcher::~HierarchicalMatcher()
63 {
64 for (Sequences::iterator it = sequences_.begin();
65 it != sequences_.end(); ++it)
66 {
67 if (it->second != NULL)
68 {
69 delete it->second;
70 }
71 }
72 }
73
74
75 void HierarchicalMatcher::Setup(DcmItem& dataset,
76 bool caseSensitivePN,
77 Encoding encoding,
78 bool hasCodeExtensions)
79 {
80 for (unsigned long i = 0; i < dataset.card(); i++)
81 {
82 DcmElement* element = dataset.getElement(i);
83 if (element == NULL)
84 {
85 throw OrthancException(ErrorCode_InternalError);
86 }
87
88 DicomTag tag(FromDcmtkBridge::Convert(element->getTag()));
89 if (tag == DICOM_TAG_SPECIFIC_CHARACTER_SET || // Ignore encoding
90 tag.GetElement() == 0x0000) // Ignore all "Group Length" tags
91 {
92 continue;
93 }
94
95 if (flatTags_.find(tag) != flatTags_.end() ||
96 sequences_.find(tag) != sequences_.end())
97 {
98 // A constraint already exists on this tag
99 throw OrthancException(ErrorCode_BadRequest);
100 }
101
102 if (FromDcmtkBridge::LookupValueRepresentation(tag) == ValueRepresentation_Sequence)
103 {
104 DcmSequenceOfItems& sequence = dynamic_cast<DcmSequenceOfItems&>(*element);
105
106 if (sequence.card() == 0 ||
107 (sequence.card() == 1 && sequence.getItem(0)->card() == 0))
108 {
109 // Universal matching of a sequence
110 sequences_[tag] = NULL;
111 }
112 else if (sequence.card() == 1)
113 {
114 sequences_[tag] = new HierarchicalMatcher(*sequence.getItem(0), caseSensitivePN, encoding, hasCodeExtensions);
115 }
116 else
117 {
118 throw OrthancException(ErrorCode_BadRequest);
119 }
120 }
121 else
122 {
123 flatTags_.insert(tag);
124
125 std::set<DicomTag> ignoreTagLength;
126 std::unique_ptr<DicomValue> value(FromDcmtkBridge::ConvertLeafElement
127 (*element, DicomToJsonFlags_None,
128 0, encoding, hasCodeExtensions, ignoreTagLength));
129
130 // WARNING: Also modify "DatabaseLookup::IsMatch()" if modifying this code
131 if (value.get() == NULL ||
132 value->IsNull())
133 {
134 // This is an universal constraint
135 }
136 else if (value->IsBinary())
137 {
138 if (!value->GetContent().empty())
139 {
140 LOG(WARNING) << "This C-Find modality worklist query contains a non-empty tag ("
141 << tag.Format() << ") with UN (unknown) value representation. "
142 << "It will be ignored.";
143 }
144 }
145 else if (value->GetContent().empty())
146 {
147 // This is an universal matcher
148 }
149 else
150 {
151 flatConstraints_.AddDicomConstraint
152 (tag, value->GetContent(), caseSensitivePN, true /* mandatory */);
153 }
154 }
155 }
156 }
157
158
159 std::string HierarchicalMatcher::Format(const std::string& prefix) const
160 {
161 std::string s;
162
163 std::set<DicomTag> tags;
164 for (size_t i = 0; i < flatConstraints_.GetConstraintsCount(); i++)
165 {
166 const DicomTagConstraint& c = flatConstraints_.GetConstraint(i);
167
168 s += c.Format() + "\n";
169 tags.insert(c.GetTag());
170 }
171
172 // Loop over the universal constraints
173 for (std::set<DicomTag>::const_iterator it = flatTags_.begin();
174 it != flatTags_.end(); ++it)
175 {
176 if (tags.find(*it) == tags.end())
177 {
178 s += prefix + it->Format() + " == *\n";
179 }
180 }
181
182 for (Sequences::const_iterator it = sequences_.begin();
183 it != sequences_.end(); ++it)
184 {
185 s += prefix + it->first.Format() + " ";
186
187 if (it->second == NULL)
188 {
189 s += "*\n";
190 }
191 else
192 {
193 s += "Sequence:\n" + it->second->Format(prefix + " ");
194 }
195 }
196
197 return s;
198 }
199
200
201 bool HierarchicalMatcher::Match(ParsedDicomFile& dicom) const
202 {
203 bool hasCodeExtensions;
204 Encoding encoding = dicom.DetectEncoding(hasCodeExtensions);
205
206 return MatchInternal(*dicom.GetDcmtkObject().getDataset(),
207 encoding, hasCodeExtensions);
208 }
209
210
211 bool HierarchicalMatcher::MatchInternal(DcmItem& item,
212 Encoding encoding,
213 bool hasCodeExtensions) const
214 {
215 if (!flatConstraints_.IsMatch(item, encoding, hasCodeExtensions))
216 {
217 return false;
218 }
219
220 for (Sequences::const_iterator it = sequences_.begin();
221 it != sequences_.end(); ++it)
222 {
223 if (it->second != NULL)
224 {
225 DcmTagKey tag = ToDcmtkBridge::Convert(it->first);
226
227 DcmSequenceOfItems* sequence = NULL;
228 if (!item.findAndGetSequence(tag, sequence).good() ||
229 sequence == NULL)
230 {
231 continue;
232 }
233
234 bool match = false;
235
236 for (unsigned long i = 0; i < sequence->card(); i++)
237 {
238 if (it->second->MatchInternal(*sequence->getItem(i), encoding, hasCodeExtensions))
239 {
240 match = true;
241 break;
242 }
243 }
244
245 if (!match)
246 {
247 return false;
248 }
249 }
250 }
251
252 return true;
253 }
254
255
256 DcmDataset* HierarchicalMatcher::ExtractInternal(DcmItem& source,
257 Encoding encoding,
258 bool hasCodeExtensions) const
259 {
260 std::unique_ptr<DcmDataset> target(new DcmDataset);
261
262 for (std::set<DicomTag>::const_iterator it = flatTags_.begin();
263 it != flatTags_.end(); ++it)
264 {
265 DcmTagKey tag = ToDcmtkBridge::Convert(*it);
266
267 DcmElement* element = NULL;
268 if (source.findAndGetElement(tag, element).good() &&
269 element != NULL)
270 {
271 if (it->IsPrivate())
272 {
273 throw OrthancException(ErrorCode_NotImplemented,
274 "Not applicable to private tags: " + it->Format());
275 }
276
277 std::unique_ptr<DcmElement> cloned(FromDcmtkBridge::CreateElementForTag(*it, "" /* no private creator */));
278 cloned->copyFrom(*element);
279 target->insert(cloned.release());
280 }
281 }
282
283 for (Sequences::const_iterator it = sequences_.begin();
284 it != sequences_.end(); ++it)
285 {
286 DcmTagKey tag = ToDcmtkBridge::Convert(it->first);
287
288 DcmSequenceOfItems* sequence = NULL;
289 if (source.findAndGetSequence(tag, sequence).good() &&
290 sequence != NULL)
291 {
292 std::unique_ptr<DcmSequenceOfItems> cloned(new DcmSequenceOfItems(tag));
293
294 for (unsigned long i = 0; i < sequence->card(); i++)
295 {
296 if (it->second == NULL)
297 {
298 cloned->append(new DcmItem(*sequence->getItem(i)));
299 }
300 else if (it->second->MatchInternal(*sequence->getItem(i), encoding, hasCodeExtensions)) // TODO Might be optimized
301 {
302 // It is necessary to encapsulate the child dataset into a
303 // "DcmItem" object before it can be included in a
304 // sequence. Otherwise, "dciodvfy" reports an error "Bad
305 // tag in sequence - Expecting Item or Sequence Delimiter."
306 std::unique_ptr<DcmDataset> child(it->second->ExtractInternal(*sequence->getItem(i), encoding, hasCodeExtensions));
307 cloned->append(new DcmItem(*child));
308 }
309 }
310
311 target->insert(cloned.release());
312 }
313 }
314
315 return target.release();
316 }
317
318
319 ParsedDicomFile* HierarchicalMatcher::Extract(ParsedDicomFile& dicom) const
320 {
321 bool hasCodeExtensions;
322 Encoding encoding = dicom.DetectEncoding(hasCodeExtensions);
323
324 std::unique_ptr<DcmDataset> dataset(ExtractInternal(*dicom.GetDcmtkObject().getDataset(),
325 encoding, hasCodeExtensions));
326
327 std::unique_ptr<ParsedDicomFile> result(new ParsedDicomFile(*dataset));
328 result->SetEncoding(encoding);
329
330 return result.release();
331 }
332 }