Mercurial > hg > orthanc
annotate OrthancFramework/Sources/DicomFormat/DicomPath.cpp @ 4683:7182f5732480
use of DicomPath in ParsedDicomFile
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 08 Jun 2021 12:37:48 +0200 |
parents | d38a7040474a |
children | 693f049729ba |
rev | line source |
---|---|
4681 | 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-2021 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 Lesser General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Lesser General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Lesser General Public | |
18 * License along with this program. If not, see | |
19 * <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "../PrecompiledHeaders.h" | |
24 #include "DicomPath.h" | |
25 | |
26 | |
27 #if !defined(ORTHANC_ENABLE_DCMTK) | |
28 # error Macro ORTHANC_ENABLE_DCMTK must be defined | |
29 #endif | |
30 | |
31 #if ORTHANC_ENABLE_DCMTK == 1 | |
32 # include "../DicomParsing/FromDcmtkBridge.h" | |
33 #endif | |
34 | |
35 #include "../OrthancException.h" | |
36 #include "../Toolbox.h" | |
37 | |
38 #include <boost/lexical_cast.hpp> | |
39 | |
40 | |
41 namespace Orthanc | |
42 { | |
43 DicomPath::PrefixItem::PrefixItem(DicomTag tag, | |
44 bool isUniversal, | |
45 size_t index) : | |
46 tag_(tag), | |
47 isUniversal_(isUniversal), | |
48 index_(index) | |
49 { | |
50 } | |
51 | |
52 | |
53 size_t DicomPath::PrefixItem::GetIndex() const | |
54 { | |
55 if (isUniversal_) | |
56 { | |
57 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
58 } | |
59 else | |
60 { | |
61 return index_; | |
62 } | |
63 } | |
64 | |
65 | |
66 DicomTag DicomPath::ParseTag(const std::string& token) | |
67 { | |
68 DicomTag tag(0,0); | |
69 | |
70 if (token[0] == '(' && | |
71 token[token.size() - 1] == ')') | |
72 { | |
73 std::string hex = token.substr(1, token.size() - 2); | |
74 if (!DicomTag::ParseHexadecimal(tag, hex.c_str())) | |
75 { | |
76 throw OrthancException(ErrorCode_UnknownDicomTag, "Cannot parse tag: " + token); | |
77 } | |
78 } | |
79 else | |
80 { | |
81 #if ORTHANC_ENABLE_DCMTK == 1 | |
82 tag = FromDcmtkBridge::ParseTag(token); | |
83 #else | |
84 if (!DicomTag::ParseHexadecimal(tag, token.c_str())) | |
85 { | |
86 throw OrthancException(ErrorCode_UnknownDicomTag, "Cannot parse tag without DCMTK: " + token); | |
87 } | |
88 #endif | |
89 } | |
90 | |
91 return tag; | |
92 } | |
93 | |
94 | |
95 const DicomPath::PrefixItem& DicomPath::GetLevel(size_t i) const | |
96 { | |
97 if (i >= prefix_.size()) | |
98 { | |
99 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
100 } | |
101 else | |
102 { | |
103 return prefix_[i]; | |
104 } | |
105 } | |
106 | |
107 | |
108 DicomPath::DicomPath(const Orthanc::DicomTag& sequence, | |
109 size_t index, | |
110 const Orthanc::DicomTag& tag) : | |
111 finalTag_(tag) | |
112 { | |
113 AddIndexedTagToPrefix(sequence, index); | |
114 } | |
115 | |
116 | |
117 DicomPath::DicomPath(const Orthanc::DicomTag& sequence1, | |
118 size_t index1, | |
119 const Orthanc::DicomTag& sequence2, | |
120 size_t index2, | |
121 const Orthanc::DicomTag& tag) : | |
122 finalTag_(tag) | |
123 { | |
124 AddIndexedTagToPrefix(sequence1, index1); | |
125 AddIndexedTagToPrefix(sequence2, index2); | |
126 } | |
127 | |
128 | |
129 DicomPath::DicomPath(const Orthanc::DicomTag& sequence1, | |
130 size_t index1, | |
131 const Orthanc::DicomTag& sequence2, | |
132 size_t index2, | |
133 const Orthanc::DicomTag& sequence3, | |
134 size_t index3, | |
135 const Orthanc::DicomTag& tag) : | |
136 finalTag_(tag) | |
137 { | |
138 AddIndexedTagToPrefix(sequence1, index1); | |
139 AddIndexedTagToPrefix(sequence2, index2); | |
140 AddIndexedTagToPrefix(sequence3, index3); | |
141 } | |
142 | |
143 | |
4683
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
144 DicomPath::DicomPath(const std::vector<Orthanc::DicomTag>& parentTags, |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
145 const std::vector<size_t> parentIndexes, |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
146 const Orthanc::DicomTag& finalTag) : |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
147 finalTag_(finalTag) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
148 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
149 if (parentTags.size() != parentIndexes.size()) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
150 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
151 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
152 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
153 else |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
154 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
155 prefix_.reserve(parentTags.size()); |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
156 |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
157 for (size_t i = 0; i < prefix_.size(); i++) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
158 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
159 prefix_.push_back(PrefixItem::CreateIndexed(parentTags[i], parentIndexes[i])); |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
160 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
161 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
162 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
163 |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
164 |
4681 | 165 void DicomPath::AddIndexedTagToPrefix(const Orthanc::DicomTag& tag, |
166 size_t index) | |
167 { | |
168 prefix_.push_back(PrefixItem::CreateIndexed(tag, index)); | |
169 } | |
170 | |
171 | |
172 void DicomPath::AddUniversalTagToPrefix(const Orthanc::DicomTag& tag) | |
173 { | |
174 prefix_.push_back(PrefixItem::CreateUniversal(tag)); | |
175 } | |
176 | |
177 | |
4682
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
178 bool DicomPath::HasUniversal() const |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
179 { |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
180 for (size_t i = 0; i < prefix_.size(); i++) |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
181 { |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
182 if (prefix_[i].IsUniversal()) |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
183 { |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
184 return true; |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
185 } |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
186 } |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
187 |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
188 return false; |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
189 } |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
190 |
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
191 |
4681 | 192 std::string DicomPath::Format() const |
193 { | |
194 std::string s; | |
195 | |
196 for (size_t i = 0; i < prefix_.size(); i++) | |
197 { | |
198 s += "(" + prefix_[i].GetTag().Format() + ")"; | |
199 | |
200 if (prefix_[i].IsUniversal()) | |
201 { | |
202 s += "[*]."; | |
203 } | |
204 else | |
205 { | |
206 s += "[" + boost::lexical_cast<std::string>(prefix_[i].GetIndex()) + "]."; | |
207 } | |
208 } | |
209 | |
210 return s + "(" + finalTag_.Format() + ")"; | |
211 } | |
212 | |
213 | |
4682
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
214 DicomPath DicomPath::Parse(const std::string& s) |
4681 | 215 { |
216 std::vector<std::string> tokens; | |
217 Toolbox::TokenizeString(tokens, s, '.'); | |
218 | |
219 if (tokens.empty()) | |
220 { | |
221 throw OrthancException(ErrorCode_ParameterOutOfRange, "Empty path to DICOM tags"); | |
222 } | |
223 | |
224 const DicomTag finalTag = ParseTag(Toolbox::StripSpaces(tokens[tokens.size() - 1])); | |
225 | |
226 DicomPath path(finalTag); | |
227 | |
228 for (size_t i = 0; i < tokens.size() - 1; i++) | |
229 { | |
230 size_t pos = tokens[i].find('['); | |
231 if (pos == std::string::npos) | |
232 { | |
233 throw OrthancException(ErrorCode_ParameterOutOfRange, "Parent path doesn't contain an index"); | |
234 } | |
235 else | |
236 { | |
237 const std::string left = Orthanc::Toolbox::StripSpaces(tokens[i].substr(0, pos)); | |
238 const std::string right = Orthanc::Toolbox::StripSpaces(tokens[i].substr(pos + 1)); | |
239 | |
240 if (left.empty()) | |
241 { | |
242 throw OrthancException(ErrorCode_ParameterOutOfRange, "Parent path doesn't contain a tag"); | |
243 } | |
244 else if (right.empty() || | |
245 right[right.size() - 1] != ']') | |
246 { | |
247 throw OrthancException(ErrorCode_ParameterOutOfRange, "Parent path doesn't contain the end of the index"); | |
248 } | |
249 else | |
250 { | |
251 DicomTag tag = ParseTag(left); | |
252 | |
253 try | |
254 { | |
255 std::string s = Toolbox::StripSpaces(right.substr(0, right.size() - 1)); | |
256 if (s == "*") | |
257 { | |
4682
d38a7040474a
FromDcmtkBridge::RemovePath() and FromDcmtkBridge::ReplacePath()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4681
diff
changeset
|
258 path.AddUniversalTagToPrefix(tag); |
4681 | 259 } |
260 else | |
261 { | |
262 int index = boost::lexical_cast<int>(s); | |
263 if (index < 0) | |
264 { | |
265 throw OrthancException(ErrorCode_ParameterOutOfRange, "Negative index in parent path: " + s); | |
266 } | |
267 else | |
268 { | |
269 path.AddIndexedTagToPrefix(tag, static_cast<size_t>(index)); | |
270 } | |
271 } | |
272 } | |
273 catch (boost::bad_lexical_cast&) | |
274 { | |
275 throw OrthancException(ErrorCode_ParameterOutOfRange, "Not a valid index in parent path: [" + right); | |
276 } | |
277 } | |
278 } | |
279 } | |
280 | |
281 return path; | |
282 } | |
4683
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
283 |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
284 |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
285 bool DicomPath::IsMatch(const DicomPath& pattern, |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
286 const DicomPath& path) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
287 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
288 if (path.HasUniversal()) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
289 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
290 throw OrthancException(ErrorCode_BadParameterType); |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
291 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
292 else if (path.GetPrefixLength() < pattern.GetPrefixLength()) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
293 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
294 return false; |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
295 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
296 else |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
297 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
298 for (size_t i = 0; i < pattern.GetPrefixLength(); i++) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
299 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
300 if (path.GetPrefixTag(i) != pattern.GetPrefixTag(i) || |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
301 (!pattern.IsPrefixUniversal(i) && |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
302 path.GetPrefixIndex(i) != pattern.GetPrefixIndex(i))) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
303 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
304 return false; |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
305 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
306 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
307 |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
308 if (path.GetPrefixLength() == pattern.GetPrefixLength()) |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
309 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
310 return (path.GetFinalTag() == pattern.GetFinalTag()); |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
311 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
312 else |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
313 { |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
314 return (path.GetPrefixTag(pattern.GetPrefixLength()) == pattern.GetFinalTag()); |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
315 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
316 } |
7182f5732480
use of DicomPath in ParsedDicomFile
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4682
diff
changeset
|
317 } |
4681 | 318 } |