comparison UnitTestsSources/SortedFramesTests.cpp @ 1478:fab6c6e795a3

Framework/Toolbox/SortedFrames.cpp
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 18 Jun 2020 16:01:00 +0200
parents 5732edec7cbd
children
comparison
equal deleted inserted replaced
1477:5732edec7cbd 1478:fab6c6e795a3
19 **/ 19 **/
20 20
21 21
22 #include <gtest/gtest.h> 22 #include <gtest/gtest.h>
23 23
24 #include "../Framework/Toolbox/SortedFrames.h"
25
24 #include <OrthancException.h> 26 #include <OrthancException.h>
25 #include <DicomFormat/DicomMap.h>
26
27 #include "../Framework/Toolbox/GeometryToolbox.h"
28
29 namespace OrthancStone
30 {
31 class SortedFrames : public boost::noncopyable
32 {
33 private:
34 class Instance : public boost::noncopyable
35 {
36 private:
37 bool hasPosition_;
38 Orthanc::DicomMap tags_;
39 std::string sopInstanceUid_;
40 unsigned int numberOfFrames_;
41 Vector normal_;
42 Vector position_;
43
44 public:
45 Instance(const Orthanc::DicomMap& tags)
46 {
47 tags_.Assign(tags);
48
49 if (!tags.LookupStringValue(sopInstanceUid_, Orthanc::DICOM_TAG_SOP_INSTANCE_UID, false))
50 {
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
52 }
53
54 uint32_t tmp;
55 if (tags.ParseUnsignedInteger32(tmp, Orthanc::DICOM_TAG_NUMBER_OF_FRAMES))
56 {
57 numberOfFrames_ = tmp;
58 }
59 else
60 {
61 numberOfFrames_ = 1;
62 }
63
64 hasPosition_ = (
65 LinearAlgebra::ParseVector(position_, tags, Orthanc::DICOM_TAG_IMAGE_POSITION_PATIENT) &&
66 position_.size() == 3 &&
67 GeometryToolbox::ComputeNormal(normal_, tags));
68 }
69
70 const Orthanc::DicomMap& GetTags() const
71 {
72 return tags_;
73 }
74
75 const std::string& GetSopInstanceUid() const
76 {
77 return sopInstanceUid_;
78 }
79
80 unsigned int GetNumberOfFrames() const
81 {
82 return numberOfFrames_;
83 }
84
85 bool HasPosition() const
86 {
87 return hasPosition_;
88 }
89
90 const Vector& GetNormal() const
91 {
92 if (hasPosition_)
93 {
94 return normal_;
95 }
96 else
97 {
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
99 }
100 }
101
102 const Vector& GetPosition() const
103 {
104 if (hasPosition_)
105 {
106 return position_;
107 }
108 else
109 {
110 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
111 }
112 }
113 };
114
115 struct Frame
116 {
117 private:
118 const Instance& instance_;
119 unsigned int frameIndex_;
120
121 public:
122 Frame(const Instance& instance,
123 unsigned int frameIndex) :
124 instance_(instance),
125 frameIndex_(frameIndex)
126 {
127 if (frameIndex >= instance.GetNumberOfFrames())
128 {
129 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
130 }
131 }
132
133 const Instance& GetInstance() const
134 {
135 return instance_;
136 }
137
138 unsigned int GetFrameIndex() const
139 {
140 return frameIndex_;
141 }
142 };
143
144 std::string studyInstanceUid_;
145 std::string seriesInstanceUid_;
146 std::vector<Instance*> instances_;
147 std::vector<Frame> frames_;
148 bool sorted_;
149
150 const Instance& GetInstance(size_t index) const
151 {
152 if (index >= instances_.size())
153 {
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
155 }
156 else
157 {
158 assert(instances_[index] != NULL);
159 return *instances_[index];
160 }
161 }
162
163 const Frame& GetFrame(size_t index) const
164 {
165 if (!sorted_)
166 {
167 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls,
168 "Sort() has not been called");
169 }
170 if (index >= frames_.size())
171 {
172 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
173 }
174 else
175 {
176 return frames_[index];
177 }
178 }
179
180 public:
181 SortedFrames() :
182 sorted_(true)
183 {
184 }
185
186 ~SortedFrames()
187 {
188 for (size_t i = 0; i < instances_.size(); i++)
189 {
190 assert(instances_[i] != NULL);
191 delete instances_[i];
192 }
193 }
194
195 const std::string& GetStudyInstanceUid() const
196 {
197 return studyInstanceUid_;
198 }
199
200 const std::string& GetSeriesInstanceUid() const
201 {
202 return seriesInstanceUid_;
203 }
204
205 void AddInstance(const Orthanc::DicomMap& tags)
206 {
207 std::unique_ptr<Instance> instance(new Instance(tags));
208
209 std::string studyInstanceUid, seriesInstanceUid;
210 if (!tags.LookupStringValue(studyInstanceUid, Orthanc::DICOM_TAG_STUDY_INSTANCE_UID, false) ||
211 !tags.LookupStringValue(seriesInstanceUid, Orthanc::DICOM_TAG_SERIES_INSTANCE_UID, false))
212 {
213 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadFileFormat);
214 }
215
216 if (instances_.empty())
217 {
218 studyInstanceUid_ = studyInstanceUid;
219 seriesInstanceUid_ = seriesInstanceUid;
220 }
221 else
222 {
223 if (studyInstanceUid_ != studyInstanceUid ||
224 seriesInstanceUid_ != seriesInstanceUid)
225 {
226 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange,
227 "Mixing instances from different series");
228 }
229 }
230
231 instances_.push_back(instance.release());
232 sorted_ = false;
233 frames_.clear();
234 }
235
236 size_t GetInstancesCount() const
237 {
238 return instances_.size();
239 }
240
241 const Orthanc::DicomMap& GetInstanceTags(size_t index) const
242 {
243 return GetInstance(index).GetTags();
244 }
245
246 const std::string& GetSopInstanceUid(size_t index) const
247 {
248 return GetInstance(index).GetSopInstanceUid();
249 }
250
251 bool IsSorted() const
252 {
253 return sorted_;
254 }
255
256 size_t GetFramesCount() const
257 {
258 if (sorted_)
259 {
260 return frames_.size();
261 }
262 else
263 {
264 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls,
265 "Sort() has not been called");
266 }
267 }
268
269 const Orthanc::DicomMap& GetFrameTags(size_t index) const
270 {
271 return GetFrame(index).GetInstance().GetTags();
272 }
273
274 const std::string& GetFrameSopInstanceUid(size_t index) const
275 {
276 return GetFrame(index).GetInstance().GetSopInstanceUid();
277 }
278
279 const unsigned int GetFrameSiblingsCount(size_t index) const
280 {
281 return GetFrame(index).GetInstance().GetNumberOfFrames();
282 }
283
284 const unsigned int GetFrameIndex(size_t index) const
285 {
286 return GetFrame(index).GetFrameIndex();
287 }
288
289
290 void Sort()
291 {
292 if (!sorted_)
293 {
294 size_t totalFrames = 0;
295 std::set<size_t> remainingInstances;
296
297 for (size_t i = 0; i < instances_.size(); i++)
298 {
299 assert(instances_[i] != NULL);
300 totalFrames += instances_[i]->GetNumberOfFrames();
301
302 remainingInstances.insert(i);
303 }
304
305 frames_.clear();
306 frames_.reserve(totalFrames);
307
308 SortUsingIntegerTag(remainingInstances, Orthanc::DICOM_TAG_INSTANCE_NUMBER); // VR is "IS"
309 SortUsingIntegerTag(remainingInstances, Orthanc::DICOM_TAG_IMAGE_INDEX); // VR is "US"
310 SortUsing3DLocation(remainingInstances);
311 SortUsingSopInstanceUid(remainingInstances);
312
313 // The following could in theory happen if several instances
314 // have the same SOPInstanceUID, no ordering is available
315 for (std::set<size_t>::const_iterator it = remainingInstances.begin();
316 it != remainingInstances.end(); it++)
317 {
318 AddFramesOfInstance(remainingInstances, *it);
319 }
320
321 if (frames_.size() != totalFrames ||
322 !remainingInstances.empty())
323 {
324 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
325 }
326
327 sorted_ = true;
328 }
329 }
330
331
332 private:
333 void AddFramesOfInstance(std::set<size_t>& remainingInstances,
334 size_t index)
335 {
336 assert(instances_[index] != NULL);
337 const Instance& instance = *instances_[index];
338
339 for (unsigned int i = 0; i < instance.GetNumberOfFrames(); i++)
340 {
341 frames_.push_back(Frame(instance, i));
342 }
343
344 assert(remainingInstances.find(index) != remainingInstances.end());
345 remainingInstances.erase(index);
346 }
347
348
349 template<typename T>
350 class SortableItem
351 {
352 private:
353 T value_;
354 size_t instance_;
355 std::string sopInstanceUid_;
356
357 public:
358 SortableItem(const T& value,
359 size_t instance,
360 const std::string& sopInstanceUid) :
361 value_(value),
362 instance_(instance),
363 sopInstanceUid_(sopInstanceUid)
364 {
365 }
366
367 size_t GetInstanceIndex() const
368 {
369 return instance_;
370 }
371
372 bool operator< (const SortableItem& other) const
373 {
374 return (value_ < other.value_ ||
375 (value_ == other.value_ &&
376 sopInstanceUid_ < other.sopInstanceUid_));
377 }
378 };
379
380
381 void SortUsingIntegerTag(std::set<size_t>& remainingInstances,
382 const Orthanc::DicomTag& tag)
383 {
384 std::vector< SortableItem<int32_t> > items;
385 items.reserve(remainingInstances.size());
386
387 for (std::set<size_t>::const_iterator it = remainingInstances.begin();
388 it != remainingInstances.end(); ++it)
389 {
390 assert(instances_[*it] != NULL);
391 const Instance& instance = *instances_[*it];
392
393 int32_t value;
394 std::string sopInstanceUid;
395 if (instance.GetTags().ParseInteger32(value, tag) &&
396 instance.GetTags().LookupStringValue(
397 sopInstanceUid, Orthanc::DICOM_TAG_SOP_INSTANCE_UID, false))
398 {
399 items.push_back(SortableItem<int32_t>(value, *it, sopInstanceUid));
400 }
401 }
402
403 std::sort(items.begin(), items.end());
404
405 for (size_t i = 0; i < items.size(); i++)
406 {
407 AddFramesOfInstance(remainingInstances, items[i].GetInstanceIndex());
408 }
409 }
410
411
412 void SortUsingSopInstanceUid(std::set<size_t>& remainingInstances)
413 {
414 std::vector<SortableItem<int32_t> > items;
415 items.reserve(remainingInstances.size());
416
417 for (std::set<size_t>::const_iterator it = remainingInstances.begin();
418 it != remainingInstances.end(); ++it)
419 {
420 assert(instances_[*it] != NULL);
421 const Instance& instance = *instances_[*it];
422
423 std::string sopInstanceUid;
424 if (instance.GetTags().LookupStringValue(
425 sopInstanceUid, Orthanc::DICOM_TAG_SOP_INSTANCE_UID, false))
426 {
427 items.push_back(SortableItem<int32_t>(0 /* arbitrary value */, *it, sopInstanceUid));
428 }
429 }
430
431 std::sort(items.begin(), items.end());
432
433 for (size_t i = 0; i < items.size(); i++)
434 {
435 AddFramesOfInstance(remainingInstances, items[i].GetInstanceIndex());
436 }
437 }
438
439
440 void SortUsing3DLocation(std::set<size_t>& remainingInstances)
441 {
442 /**
443 * Compute the mean of the normal vectors, using the recursive
444 * formula for arithmetic means for numerical stability.
445 * https://diego.assencio.com/?index=c34d06f4f4de2375658ed41f70177d59
446 **/
447
448 Vector meanNormal;
449 LinearAlgebra::AssignVector(meanNormal, 0, 0, 0);
450
451 unsigned int n = 0;
452
453 for (std::set<size_t>::const_iterator it = remainingInstances.begin();
454 it != remainingInstances.end(); ++it)
455 {
456 assert(instances_[*it] != NULL);
457 const Instance& instance = *instances_[*it];
458
459 if (instance.HasPosition())
460 {
461 n += 1;
462 meanNormal += (instance.GetNormal() - meanNormal) / static_cast<float>(n);
463 }
464 }
465
466 std::vector<SortableItem<float> > items;
467 items.reserve(n);
468
469 for (std::set<size_t>::const_iterator it = remainingInstances.begin();
470 it != remainingInstances.end(); ++it)
471 {
472 assert(instances_[*it] != NULL);
473 const Instance& instance = *instances_[*it];
474
475 std::string sopInstanceUid;
476 if (instance.HasPosition() &&
477 instance.GetTags().LookupStringValue(
478 sopInstanceUid, Orthanc::DICOM_TAG_SOP_INSTANCE_UID, false))
479 {
480 double p = LinearAlgebra::DotProduct(meanNormal, instance.GetPosition());
481 items.push_back(SortableItem<float>(p, *it, sopInstanceUid));
482 }
483 }
484
485 assert(items.size() <= n);
486
487 std::sort(items.begin(), items.end());
488 printf(">> %d\n", items.size());
489
490 for (size_t i = 0; i < items.size(); i++)
491 {
492 AddFramesOfInstance(remainingInstances, items[i].GetInstanceIndex());
493 }
494 }
495 };
496 }
497 27
498 28
499 TEST(SortedFrames, Basic) 29 TEST(SortedFrames, Basic)
500 { 30 {
501 OrthancStone::SortedFrames f; 31 OrthancStone::SortedFrames f;