Mercurial > hg > orthanc-stone
comparison OrthancStone/Resources/Graveyard/RTStructTentativeReimplementation-BGO/DicomStructureSet2.cpp @ 1908:affde38b84de
moved tentative bgo reimplementation of rt-struct into graveyard
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 01 Feb 2022 08:38:32 +0100 |
parents | OrthancStone/Sources/Toolbox/DicomStructureSet2.cpp@7053b8a0aaec |
children | 07964689cb0b |
comparison
equal
deleted
inserted
replaced
1907:0208f99b8bde | 1908:affde38b84de |
---|---|
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-2022 Osimis S.A., Belgium | |
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium | |
7 * | |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Lesser General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Lesser General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Lesser General Public | |
19 * License along with this program. If not, see | |
20 * <http://www.gnu.org/licenses/>. | |
21 **/ | |
22 | |
23 #ifdef BGO_ENABLE_DICOMSTRUCTURESETLOADER2 | |
24 | |
25 #include "DicomStructureSet2.h" | |
26 | |
27 #include "../Toolbox/LinearAlgebra.h" | |
28 #include "../StoneException.h" | |
29 | |
30 #include <Logging.h> | |
31 #include <OrthancException.h> | |
32 #include <Toolbox.h> | |
33 #include <DicomFormat/DicomTag.h> | |
34 | |
35 #include "DicomStructure2.h" | |
36 #include "GenericToolbox.h" | |
37 #include "OrthancDatasets/DicomDatasetReader.h" | |
38 | |
39 namespace OrthancStone | |
40 { | |
41 static const Orthanc::DicomTag DICOM_TAG_CONTOUR_GEOMETRIC_TYPE(0x3006, 0x0042); | |
42 static const Orthanc::DicomTag DICOM_TAG_CONTOUR_IMAGE_SEQUENCE(0x3006, 0x0016); | |
43 static const Orthanc::DicomTag DICOM_TAG_CONTOUR_SEQUENCE(0x3006, 0x0040); | |
44 static const Orthanc::DicomTag DICOM_TAG_CONTOUR_DATA(0x3006, 0x0050); | |
45 static const Orthanc::DicomTag DICOM_TAG_NUMBER_OF_CONTOUR_POINTS(0x3006, 0x0046); | |
46 static const Orthanc::DicomTag DICOM_TAG_REFERENCED_SOP_INSTANCE_UID(0x0008, 0x1155); | |
47 static const Orthanc::DicomTag DICOM_TAG_ROI_CONTOUR_SEQUENCE(0x3006, 0x0039); | |
48 static const Orthanc::DicomTag DICOM_TAG_ROI_DISPLAY_COLOR(0x3006, 0x002a); | |
49 static const Orthanc::DicomTag DICOM_TAG_ROI_NAME(0x3006, 0x0026); | |
50 static const Orthanc::DicomTag DICOM_TAG_RT_ROI_INTERPRETED_TYPE(0x3006, 0x00a4); | |
51 static const Orthanc::DicomTag DICOM_TAG_RT_ROI_OBSERVATIONS_SEQUENCE(0x3006, 0x0080); | |
52 static const Orthanc::DicomTag DICOM_TAG_STRUCTURE_SET_ROI_SEQUENCE(0x3006, 0x0020); | |
53 | |
54 static inline uint8_t ConvertAndClipToByte(double v) | |
55 { | |
56 if (v < 0) | |
57 { | |
58 return 0; | |
59 } | |
60 else if (v >= 255) | |
61 { | |
62 return 255; | |
63 } | |
64 else | |
65 { | |
66 return static_cast<uint8_t>(v); | |
67 } | |
68 } | |
69 | |
70 static bool ReadDicomToVector(Vector& target, | |
71 const IDicomDataset& dataset, | |
72 const Orthanc::DicomPath& tag) | |
73 { | |
74 std::string value; | |
75 return (dataset.GetStringValue(value, tag) && | |
76 GenericToolbox::FastParseVector(target, value)); | |
77 } | |
78 | |
79 | |
80 void DicomPathToString(std::string& s, const Orthanc::DicomPath& dicomPath) | |
81 { | |
82 std::stringstream tmp; | |
83 for (size_t i = 0; i < dicomPath.GetPrefixLength(); ++i) | |
84 { | |
85 Orthanc::DicomTag tag = dicomPath.GetPrefixTag(i); | |
86 | |
87 // We use this other object to be able to use Format | |
88 Orthanc::DicomTag tag2(tag.GetGroup(), tag.GetElement()); | |
89 size_t index = dicomPath.GetPrefixIndex(i); | |
90 tmp << " (" << tag2.Format() << ") [" << index << "] / "; | |
91 } | |
92 const Orthanc::DicomTag& tag = dicomPath.GetFinalTag(); | |
93 Orthanc::DicomTag tag2(tag.GetGroup(), tag.GetElement()); | |
94 tmp << " (" << tag2.Format() << ")"; | |
95 s = tmp.str(); | |
96 } | |
97 | |
98 std::ostream& operator<<(std::ostream& s, const Orthanc::DicomPath& dicomPath) | |
99 { | |
100 std::string tmp; | |
101 DicomPathToString(tmp, dicomPath); | |
102 s << tmp; | |
103 return s; | |
104 } | |
105 | |
106 | |
107 DicomStructureSet2::DicomStructureSet2() | |
108 { | |
109 | |
110 } | |
111 | |
112 | |
113 DicomStructureSet2::~DicomStructureSet2() | |
114 { | |
115 | |
116 } | |
117 | |
118 void DicomStructureSet2::SetContents(const FullOrthancDataset& tags) | |
119 { | |
120 FillStructuresFromDataset(tags); | |
121 ComputeDependentProperties(); | |
122 } | |
123 | |
124 void DicomStructureSet2::ComputeDependentProperties() | |
125 { | |
126 for (size_t i = 0; i < structures_.size(); ++i) | |
127 { | |
128 structures_[i].ComputeDependentProperties(); | |
129 } | |
130 } | |
131 | |
132 void DicomStructureSet2::FillStructuresFromDataset(const FullOrthancDataset& tags) | |
133 { | |
134 DicomDatasetReader reader(tags); | |
135 | |
136 // a few sanity checks | |
137 size_t count = 0, tmp = 0; | |
138 | |
139 // DICOM_TAG_RT_ROI_OBSERVATIONS_SEQUENCE (0x3006, 0x0080); | |
140 // DICOM_TAG_ROI_CONTOUR_SEQUENCE (0x3006, 0x0039); | |
141 // DICOM_TAG_STRUCTURE_SET_ROI_SEQUENCE (0x3006, 0x0020); | |
142 if (!tags.GetSequenceSize(count, Orthanc::DicomPath(DICOM_TAG_RT_ROI_OBSERVATIONS_SEQUENCE)) || | |
143 !tags.GetSequenceSize(tmp, Orthanc::DicomPath(DICOM_TAG_ROI_CONTOUR_SEQUENCE)) || | |
144 tmp != count || | |
145 !tags.GetSequenceSize(tmp, Orthanc::DicomPath(DICOM_TAG_STRUCTURE_SET_ROI_SEQUENCE)) || | |
146 tmp != count) | |
147 { | |
148 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
149 } | |
150 | |
151 // let's now parse the structures stored in the dicom file | |
152 // DICOM_TAG_RT_ROI_OBSERVATIONS_SEQUENCE (0x3006, 0x0080) | |
153 // DICOM_TAG_RT_ROI_INTERPRETED_TYPE (0x3006, 0x00a4) | |
154 // DICOM_TAG_ROI_DISPLAY_COLOR (0x3006, 0x002a) | |
155 // DICOM_TAG_ROI_NAME (0x3006, 0x0026) | |
156 structures_.resize(count); | |
157 for (size_t i = 0; i < count; i++) | |
158 { | |
159 // (0x3006, 0x0080)[i]/(0x3006, 0x00a4) | |
160 structures_[i].interpretation_ = reader.GetStringValue | |
161 (Orthanc::DicomPath(DICOM_TAG_RT_ROI_OBSERVATIONS_SEQUENCE, i, | |
162 DICOM_TAG_RT_ROI_INTERPRETED_TYPE), | |
163 "No interpretation"); | |
164 | |
165 // (0x3006, 0x0020)[i]/(0x3006, 0x0026) | |
166 structures_[i].name_ = reader.GetStringValue | |
167 (Orthanc::DicomPath(DICOM_TAG_STRUCTURE_SET_ROI_SEQUENCE, i, | |
168 DICOM_TAG_ROI_NAME), | |
169 "No name"); | |
170 | |
171 Vector color; | |
172 // (0x3006, 0x0039)[i]/(0x3006, 0x002a) | |
173 if (ReadDicomToVector(color, tags, Orthanc::DicomPath( | |
174 DICOM_TAG_ROI_CONTOUR_SEQUENCE, i, DICOM_TAG_ROI_DISPLAY_COLOR)) | |
175 && color.size() == 3) | |
176 { | |
177 structures_[i].red_ = ConvertAndClipToByte(color[0]); | |
178 structures_[i].green_ = ConvertAndClipToByte(color[1]); | |
179 structures_[i].blue_ = ConvertAndClipToByte(color[2]); | |
180 } | |
181 else | |
182 { | |
183 structures_[i].red_ = 255; | |
184 structures_[i].green_ = 0; | |
185 structures_[i].blue_ = 0; | |
186 } | |
187 | |
188 size_t countSlices; | |
189 // DICOM_TAG_ROI_CONTOUR_SEQUENCE (0x3006, 0x0039); | |
190 // DICOM_TAG_CONTOUR_SEQUENCE (0x3006, 0x0040); | |
191 if (!tags.GetSequenceSize(countSlices, Orthanc::DicomPath( | |
192 DICOM_TAG_ROI_CONTOUR_SEQUENCE, i, DICOM_TAG_CONTOUR_SEQUENCE))) | |
193 { | |
194 LOG(WARNING) << "DicomStructureSet2::SetContents | structure \"" << structures_[i].name_ << "\" has no slices!"; | |
195 countSlices = 0; | |
196 } | |
197 | |
198 LOG(INFO) << "New RT structure: \"" << structures_[i].name_ | |
199 << "\" with interpretation \"" << structures_[i].interpretation_ | |
200 << "\" containing " << countSlices << " slices (color: " | |
201 << static_cast<int>(structures_[i].red_) << "," | |
202 << static_cast<int>(structures_[i].green_) << "," | |
203 << static_cast<int>(structures_[i].blue_) << ")"; | |
204 | |
205 // These temporary variables avoid allocating many vectors in the loop below | |
206 | |
207 // (0x3006, 0x0039)[i]/(0x3006, 0x0040)[0]/(0x3006, 0x0046) | |
208 Orthanc::DicomPath countPointsPath( | |
209 DICOM_TAG_ROI_CONTOUR_SEQUENCE, i, | |
210 DICOM_TAG_CONTOUR_SEQUENCE, 0, | |
211 DICOM_TAG_NUMBER_OF_CONTOUR_POINTS); | |
212 | |
213 Orthanc::DicomPath geometricTypePath( | |
214 DICOM_TAG_ROI_CONTOUR_SEQUENCE, i, | |
215 DICOM_TAG_CONTOUR_SEQUENCE, 0, | |
216 DICOM_TAG_CONTOUR_GEOMETRIC_TYPE); | |
217 | |
218 Orthanc::DicomPath imageSequencePath( | |
219 DICOM_TAG_ROI_CONTOUR_SEQUENCE, i, | |
220 DICOM_TAG_CONTOUR_SEQUENCE, 0, | |
221 DICOM_TAG_CONTOUR_IMAGE_SEQUENCE); | |
222 | |
223 // (3006,0039)[i] / (0x3006, 0x0040)[0] / (0x3006, 0x0016)[0] / (0x0008, 0x1155) | |
224 Orthanc::DicomPath referencedInstancePath( | |
225 DICOM_TAG_ROI_CONTOUR_SEQUENCE, i, | |
226 DICOM_TAG_CONTOUR_SEQUENCE, 0, | |
227 DICOM_TAG_CONTOUR_IMAGE_SEQUENCE, 0, | |
228 DICOM_TAG_REFERENCED_SOP_INSTANCE_UID); | |
229 | |
230 Orthanc::DicomPath contourDataPath( | |
231 DICOM_TAG_ROI_CONTOUR_SEQUENCE, i, | |
232 DICOM_TAG_CONTOUR_SEQUENCE, 0, | |
233 DICOM_TAG_CONTOUR_DATA); | |
234 | |
235 for (size_t j = 0; j < countSlices; j++) | |
236 { | |
237 unsigned int countPoints = 0; | |
238 | |
239 countPointsPath.SetPrefixIndex(1, j); | |
240 if (!reader.GetUnsignedIntegerValue(countPoints, countPointsPath)) | |
241 { | |
242 std::string s; | |
243 DicomPathToString(s, countPointsPath); | |
244 LOG(ERROR) << "Dicom path " << s << " is not valid (should contain an unsigned integer)"; | |
245 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
246 } | |
247 | |
248 //LOG(INFO) << "Parsing slice containing " << countPoints << " vertices"; | |
249 | |
250 geometricTypePath.SetPrefixIndex(1, j); | |
251 std::string type = reader.GetMandatoryStringValue(geometricTypePath); | |
252 if (type != "CLOSED_PLANAR") | |
253 { | |
254 // TODO: support points!! | |
255 LOG(WARNING) << "Ignoring contour with geometry type: " << type; | |
256 continue; | |
257 } | |
258 | |
259 size_t size = 0; | |
260 | |
261 imageSequencePath.SetPrefixIndex(1, j); | |
262 if (!tags.GetSequenceSize(size, imageSequencePath) || size != 1) | |
263 { | |
264 LOG(ERROR) << "The ContourImageSequence sequence (tag 3006,0016) must be present and contain one entry."; | |
265 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
266 } | |
267 | |
268 referencedInstancePath.SetPrefixIndex(1, j); | |
269 std::string sopInstanceUid = reader.GetMandatoryStringValue(referencedInstancePath); | |
270 | |
271 contourDataPath.SetPrefixIndex(1, j); | |
272 std::string slicesData = reader.GetMandatoryStringValue(contourDataPath); | |
273 | |
274 Vector points; | |
275 if (!GenericToolbox::FastParseVector(points, slicesData) || | |
276 points.size() != 3 * countPoints) | |
277 { | |
278 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat); | |
279 } | |
280 | |
281 // seen in real world | |
282 if (Orthanc::Toolbox::StripSpaces(sopInstanceUid) == "") | |
283 { | |
284 LOG(ERROR) << "WARNING. The following Dicom tag (Referenced SOP Instance UID) contains an empty value : // (3006,0039)[" << i << "] / (0x3006, 0x0040)[0] / (0x3006, 0x0016)[0] / (0x0008, 0x1155)"; | |
285 } | |
286 | |
287 DicomStructurePolygon2 polygon(sopInstanceUid,type); | |
288 polygon.Reserve(countPoints); | |
289 | |
290 for (size_t k = 0; k < countPoints; k++) | |
291 { | |
292 Vector v(3); | |
293 v[0] = points[3 * k]; | |
294 v[1] = points[3 * k + 1]; | |
295 v[2] = points[3 * k + 2]; | |
296 polygon.AddPoint(v); | |
297 } | |
298 structures_[i].AddPolygon(polygon); | |
299 } | |
300 } | |
301 } | |
302 | |
303 | |
304 void DicomStructureSet2::Clear() | |
305 { | |
306 structures_.clear(); | |
307 } | |
308 | |
309 } | |
310 | |
311 #endif | |
312 // BGO_ENABLE_DICOMSTRUCTURESETLOADER2 | |
313 |