comparison Plugin/SeriesVolumeSorter.cpp @ 0:02f7a0400a91

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 25 Feb 2015 13:45:35 +0100
parents
children a6492d20b2a8
comparison
equal deleted inserted replaced
-1:000000000000 0:02f7a0400a91
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 *
6 * This program is free software: you can redistribute it and/or
7 * modify it under the terms of the GNU Affero General Public License
8 * as published by the Free Software Foundation, either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20
21 #include "SeriesVolumeSorter.h"
22
23 #include <algorithm>
24 #include <limits>
25 #include <math.h>
26 #include <cassert>
27
28 namespace OrthancPlugins
29 {
30 SeriesVolumeSorter::SeriesVolumeSorter() :
31 isVolume_(true),
32 sorted_(true)
33 {
34 }
35
36
37 void SeriesVolumeSorter::Reserve(size_t countInstances)
38 {
39 positions_.reserve(countInstances);
40 indexes_.reserve(countInstances);
41 }
42
43
44 void SeriesVolumeSorter::AddInstance(const std::string& instanceId,
45 const InstanceInformation& instance)
46 {
47 if (instance.HasIndexInSeries())
48 {
49 indexes_.push_back(std::make_pair(instanceId, instance.GetIndexInSeries()));
50 }
51
52 if (!isVolume_ ||
53 !instance.HasPosition())
54 {
55 isVolume_ = false;
56 }
57 else
58 {
59 if (positions_.size() == 0)
60 {
61 // This is the first slice in a possible 3D volume. Remember its normal.
62 normal_[0] = instance.GetNormal(0);
63 normal_[1] = instance.GetNormal(1);
64 normal_[2] = instance.GetNormal(2);
65 }
66 else
67 {
68 static const float THRESHOLD = 10.0f * std::numeric_limits<float>::epsilon();
69
70 // This is still a possible 3D volume. Check whether the normal
71 // is constant wrt. the previous slices.
72 if (fabs(normal_[0] - instance.GetNormal(0)) > THRESHOLD ||
73 fabs(normal_[1] - instance.GetNormal(1)) > THRESHOLD ||
74 fabs(normal_[2] - instance.GetNormal(2)) > THRESHOLD)
75 {
76 // The normal is not constant, not a 3D volume.
77 isVolume_ = false;
78 positions_.clear();
79 }
80 }
81
82 if (isVolume_)
83 {
84 float distance = (normal_[0] * instance.GetPosition(0) +
85 normal_[1] * instance.GetPosition(1) +
86 normal_[2] * instance.GetPosition(2));
87 positions_.push_back(std::make_pair(instanceId, distance));
88 }
89 }
90
91 sorted_ = false;
92 }
93
94
95 std::string SeriesVolumeSorter::GetInstance(size_t index)
96 {
97 if (!sorted_)
98 {
99 if (isVolume_)
100 {
101 assert(indexes_.size() == positions_.size());
102 std::sort(positions_.begin(), positions_.end(), ComparePosition);
103
104 float a = positions_.front().second;
105 float b = positions_.back().second;
106 assert(a <= b);
107
108 if (fabs(b - a) <= 10.0f * std::numeric_limits<float>::epsilon())
109 {
110 // Not enough difference between the minimum and maximum
111 // positions along the normal of the volume
112 isVolume_ = false;
113 }
114 }
115
116 if (!isVolume_)
117 {
118 std::sort(indexes_.begin(), indexes_.end(), CompareIndex);
119 }
120 }
121
122 if (isVolume_)
123 {
124 return positions_[index].first;
125 }
126 else
127 {
128 return indexes_[index].first;
129 }
130 }
131 }