Mercurial > hg > orthanc-stone
comparison Framework/Loaders/LoadedDicomResources.cpp @ 1228:c471a0aa137b broker
adding the next generation of loaders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 09 Dec 2019 13:58:37 +0100 |
parents | |
children | a8248b08115c |
comparison
equal
deleted
inserted
replaced
1227:a1c0c9c9f9af | 1228:c471a0aa137b |
---|---|
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-2019 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 <Core/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 bool LoadedDicomResources::LookupStringValue(std::string& target, | |
104 const std::string& id, | |
105 const Orthanc::DicomTag& tag) const | |
106 { | |
107 Resources::const_iterator found = resources_.find(id); | |
108 | |
109 if (found == resources_.end()) | |
110 { | |
111 return false; | |
112 } | |
113 else | |
114 { | |
115 assert(found->second != NULL); | |
116 return found->second->LookupStringValue(target, tag, false); | |
117 } | |
118 } | |
119 | |
120 | |
121 void LoadedDicomResources::AddResource(const Orthanc::DicomMap& dicom) | |
122 { | |
123 std::string id; | |
124 | |
125 if (dicom.LookupStringValue(id, indexedTag_, false /* no binary value */) && | |
126 resources_.find(id) == resources_.end() /* Don't index twice the same resource */) | |
127 { | |
128 resources_[id] = dicom.Clone(); | |
129 flattened_.clear(); // Invalidate the flattened version | |
130 } | |
131 } | |
132 | |
133 | |
134 void LoadedDicomResources::AddFromOrthanc(const Json::Value& tags) | |
135 { | |
136 Orthanc::DicomMap dicom; | |
137 dicom.FromDicomAsJson(tags); | |
138 AddResource(dicom); | |
139 } | |
140 | |
141 | |
142 void LoadedDicomResources::AddFromDicomWeb(const Json::Value& dicomweb) | |
143 { | |
144 if (dicomweb.type() == Json::objectValue) | |
145 { | |
146 AddFromDicomWebInternal(dicomweb); | |
147 } | |
148 else if (dicomweb.type() == Json::arrayValue) | |
149 { | |
150 for (Json::Value::ArrayIndex i = 0; i < dicomweb.size(); i++) | |
151 { | |
152 if (dicomweb[i].type() == Json::objectValue) | |
153 { | |
154 AddFromDicomWebInternal(dicomweb[i]); | |
155 } | |
156 else | |
157 { | |
158 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); | |
159 } | |
160 } | |
161 } | |
162 else | |
163 { | |
164 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol); | |
165 } | |
166 } | |
167 | |
168 | |
169 bool LoadedDicomResources::LookupTagValueConsensus(std::string& target, | |
170 const Orthanc::DicomTag& tag) const | |
171 { | |
172 typedef std::map<std::string, unsigned int> Counter; | |
173 | |
174 Counter counter; | |
175 | |
176 for (Resources::const_iterator it = resources_.begin(); it != resources_.end(); ++it) | |
177 { | |
178 assert(it->second != NULL); | |
179 | |
180 std::string value; | |
181 if (it->second->LookupStringValue(value, tag, false)) | |
182 { | |
183 Counter::iterator found = counter.find(value); | |
184 if (found == counter.end()) | |
185 { | |
186 counter[value] = 1; | |
187 } | |
188 else | |
189 { | |
190 found->second ++; | |
191 } | |
192 } | |
193 } | |
194 | |
195 Counter::const_iterator best = counter.end(); | |
196 | |
197 for (Counter::const_iterator it = counter.begin(); it != counter.end(); ++it) | |
198 { | |
199 if (best == counter.end() || | |
200 best->second < it->second) | |
201 { | |
202 best = it; | |
203 } | |
204 } | |
205 | |
206 if (best == counter.end()) | |
207 { | |
208 return false; | |
209 } | |
210 else | |
211 { | |
212 target = best->first; | |
213 return true; | |
214 } | |
215 } | |
216 } |