Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Loaders/LoadedDicomResources.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/Loaders/LoadedDicomResources.cpp@30deba7bc8e2 |
children | 4fb8fdf03314 |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
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 Affero 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 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "LoadedDicomResources.h" | |
23 | |
24 #include <OrthancException.h> | |
25 | |
26 #include <cassert> | |
27 | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 void LoadedDicomResources::Flatten() | |
32 { | |
33 // Lazy generation of a "std::vector" from the "std::map" | |
34 if (flattened_.empty()) | |
35 { | |
36 flattened_.resize(resources_.size()); | |
37 | |
38 size_t pos = 0; | |
39 for (Resources::const_iterator it = resources_.begin(); it != resources_.end(); ++it) | |
40 { | |
41 assert(it->second != NULL); | |
42 flattened_[pos++] = it->second; | |
43 } | |
44 } | |
45 else | |
46 { | |
47 // No need to flatten | |
48 assert(flattened_.size() == resources_.size()); | |
49 } | |
50 } | |
51 | |
52 | |
53 void LoadedDicomResources::AddFromDicomWebInternal(const Json::Value& dicomweb) | |
54 { | |
55 assert(dicomweb.type() == Json::objectValue); | |
56 Orthanc::DicomMap dicom; | |
57 dicom.FromDicomWeb(dicomweb); | |
58 AddResource(dicom); | |
59 } | |
60 | |
61 | |
62 LoadedDicomResources::LoadedDicomResources(const LoadedDicomResources& other, | |
63 const Orthanc::DicomTag& indexedTag) : | |
64 indexedTag_(indexedTag) | |
65 { | |
66 for (Resources::const_iterator it = other.resources_.begin(); | |
67 it != other.resources_.end(); ++it) | |
68 { | |
69 assert(it->second != NULL); | |
70 AddResource(*it->second); | |
71 } | |
72 } | |
73 | |
74 void LoadedDicomResources::Clear() | |
75 { | |
76 for (Resources::iterator it = resources_.begin(); it != resources_.end(); ++it) | |
77 { | |
78 assert(it->second != NULL); | |
79 delete it->second; | |
80 } | |
81 | |
82 resources_.clear(); | |
83 flattened_.clear(); | |
84 } | |
85 | |
86 | |
87 Orthanc::DicomMap& LoadedDicomResources::GetResource(size_t index) | |
88 { | |
89 Flatten(); | |
90 | |
91 if (index >= flattened_.size()) | |
92 { | |
93 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
94 } | |
95 else | |
96 { | |
97 assert(flattened_[index] != NULL); | |
98 return *flattened_[index]; | |
99 } | |
100 } | |
101 | |
102 | |
103 void LoadedDicomResources::MergeResource(Orthanc::DicomMap& target, | |
104 const std::string& id) const | |
105 { | |
106 Resources::const_iterator it = resources_.find(id); | |
107 | |
108 if (it == resources_.end()) | |
109 { | |
110 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem); | |
111 } | |
112 else | |
113 { | |
114 assert(it->second != NULL); | |
115 target.Merge(*it->second); | |
116 } | |
117 } | |
118 | |
119 | |
120 bool LoadedDicomResources::LookupStringValue(std::string& target, | |
121 const std::string& id, | |
122 const Orthanc::DicomTag& tag) const | |
123 { | |
124 Resources::const_iterator found = resources_.find(id); | |
125 | |
126 if (found == resources_.end()) | |
127 { | |
128 return false; | |
129 } | |
130 else | |
131 { | |
132 assert(found->second != NULL); | |
133 return found->second->LookupStringValue(target, tag, false); | |
134 } | |
135 } | |
136 | |
137 | |
138 void LoadedDicomResources::AddResource(const Orthanc::DicomMap& dicom) | |
139 { | |
140 std::string id; | |
141 | |
142 if (dicom.LookupStringValue(id, indexedTag_, false /* no binary value */) && | |
143 resources_.find(id) == resources_.end() /* Don't index twice the same resource */) | |
144 { | |
145 resources_[id] = dicom.Clone(); | |
146 flattened_.clear(); // Invalidate the flattened version | |
147 } | |
148 } | |
149 | |
150 | |
151 void LoadedDicomResources::AddFromOrthanc(const Json::Value& tags) | |
152 { | |
153 Orthanc::DicomMap dicom; | |
154 dicom.FromDicomAsJson(tags); | |
155 AddResource(dicom); | |
156 } | |
157 | |
158 | |
159 void LoadedDicomResources::AddFromDicomWeb(const Json::Value& dicomweb) | |
160 { | |
161 if (dicomweb.type() == Json::objectValue) | |
162 { | |
163 AddFromDicomWebInternal(dicomweb); | |
164 } | |
165 else if (dicomweb.type() == Json::arrayValue) | |
166 { | |
167 for (Json::Value::ArrayIndex i = 0; i < dicomweb.size(); i++) | |
168 { | |
169 if (dicomweb[i].type() == Json::objectValue) | |
170 { | |
171 AddFromDicomWebInternal(dicomweb[i]); | |
172 } | |
173 else | |
174 { | |
175 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); | |
176 } | |
177 } | |
178 } | |
179 else | |
180 { | |
181 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); | |
182 } | |
183 } | |
184 | |
185 | |
186 bool LoadedDicomResources::LookupTagValueConsensus(std::string& target, | |
187 const Orthanc::DicomTag& tag) const | |
188 { | |
189 typedef std::map<std::string, unsigned int> Counter; | |
190 | |
191 Counter counter; | |
192 | |
193 for (Resources::const_iterator it = resources_.begin(); it != resources_.end(); ++it) | |
194 { | |
195 assert(it->second != NULL); | |
196 | |
197 std::string value; | |
198 if (it->second->LookupStringValue(value, tag, false)) | |
199 { | |
200 Counter::iterator found = counter.find(value); | |
201 if (found == counter.end()) | |
202 { | |
203 counter[value] = 1; | |
204 } | |
205 else | |
206 { | |
207 found->second ++; | |
208 } | |
209 } | |
210 } | |
211 | |
212 Counter::const_iterator best = counter.end(); | |
213 | |
214 for (Counter::const_iterator it = counter.begin(); it != counter.end(); ++it) | |
215 { | |
216 if (best == counter.end() || | |
217 best->second < it->second) | |
218 { | |
219 best = it; | |
220 } | |
221 } | |
222 | |
223 if (best == counter.end()) | |
224 { | |
225 return false; | |
226 } | |
227 else | |
228 { | |
229 target = best->first; | |
230 return true; | |
231 } | |
232 } | |
233 } |