comparison OrthancServer/SliceOrdering.cpp @ 1702:9980875edc7c db-changes

started work on SliceOrdering
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 12 Oct 2015 17:49:23 +0200
parents
children b80e76dd1d56
comparison
equal deleted inserted replaced
1701:4aaaecae5803 1702:9980875edc7c
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 General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * License, or (at your option) any later version.
10 *
11 * In addition, as a special exception, the copyright holders of this
12 * program give permission to link the code of its release with the
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
14 * that use the same license as the "OpenSSL" library), and distribute
15 * the linked executables. You must obey the GNU General Public License
16 * in all respects for all of the code used other than "OpenSSL". If you
17 * modify file(s) with this exception, you may extend this exception to
18 * your version of the file(s), but you are not obligated to do so. If
19 * you do not wish to do so, delete this exception statement from your
20 * version. If you delete this exception statement from all source files
21 * in the program, then also delete it here.
22 *
23 * This program is distributed in the hope that it will be useful, but
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 * General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 **/
31
32
33 #include "PrecompiledHeadersServer.h"
34 #include "SliceOrdering.h"
35
36 #include "../Core/Toolbox.h"
37
38 #include <boost/lexical_cast.hpp>
39
40
41 namespace Orthanc
42 {
43 static bool TokenizeVector(std::vector<float>& result,
44 const std::string& value,
45 unsigned int expectedSize)
46 {
47 std::vector<std::string> tokens;
48 Toolbox::TokenizeString(tokens, value, '\\');
49
50 if (tokens.size() != expectedSize)
51 {
52 return false;
53 }
54
55 result.resize(tokens.size());
56
57 for (size_t i = 0; i < tokens.size(); i++)
58 {
59 try
60 {
61 result[i] = boost::lexical_cast<float>(tokens[i]);
62 }
63 catch (boost::bad_lexical_cast&)
64 {
65 return false;
66 }
67 }
68
69 return true;
70 }
71
72
73 static bool TokenizeVector(std::vector<float>& result,
74 const DicomMap& map,
75 const DicomTag& tag,
76 unsigned int expectedSize)
77 {
78 const DicomValue* value = map.TestAndGetValue(tag);
79
80 if (value == NULL ||
81 value->IsNull())
82 {
83 return false;
84 }
85 else
86 {
87 return TokenizeVector(result, value->AsString(), expectedSize);
88 }
89 }
90
91
92 struct SliceOrdering::Instance
93 {
94 std::string instanceId_;
95 bool hasPosition_;
96 Vector position_;
97 bool hasIndexInSeries_;
98 size_t indexInSeries_;
99
100 Instance(ServerIndex& index,
101 const std::string& instanceId) :
102 instanceId_(instanceId)
103 {
104 DicomMap instance;
105 if (!index.GetMainDicomTags(instance, instanceId, ResourceType_Instance, ResourceType_Instance))
106 {
107 throw OrthancException(ErrorCode_UnknownResource);
108 }
109
110 std::vector<float> tmp;
111 hasPosition_ = TokenizeVector(tmp, instance, DICOM_TAG_IMAGE_POSITION_PATIENT, 3);
112
113 if (hasPosition_)
114 {
115 position_[0] = tmp[0];
116 position_[1] = tmp[1];
117 position_[2] = tmp[2];
118 }
119
120 std::string s;
121 hasIndexInSeries_ = false;
122
123 try
124 {
125 if (index.LookupMetadata(s, instanceId, MetadataType_Instance_IndexInSeries))
126 {
127 indexInSeries_ = boost::lexical_cast<size_t>(s);
128 hasIndexInSeries_ = true;
129 }
130 }
131 catch (boost::bad_lexical_cast&)
132 {
133 }
134 }
135 };
136
137
138 void SliceOrdering::ComputeNormal()
139 {
140 DicomMap series;
141 if (!index_.GetMainDicomTags(series, seriesId_, ResourceType_Series, ResourceType_Series))
142 {
143 throw OrthancException(ErrorCode_UnknownResource);
144 }
145
146 std::vector<float> cosines;
147 hasNormal_ = TokenizeVector(cosines, series, DICOM_TAG_IMAGE_ORIENTATION_PATIENT, 6);
148
149 if (hasNormal_)
150 {
151 normal_[0] = cosines[1] * cosines[5] - cosines[2] * cosines[4];
152 normal_[1] = cosines[2] * cosines[3] - cosines[0] * cosines[5];
153 normal_[2] = cosines[0] * cosines[4] - cosines[1] * cosines[3];
154 }
155 }
156
157
158 void SliceOrdering::CreateInstances()
159 {
160 std::list<std::string> instancesId;
161 index_.GetChildren(instancesId, seriesId_);
162
163 instances_.reserve(instancesId.size());
164 for (std::list<std::string>::const_iterator
165 it = instancesId.begin(); it != instancesId.end(); ++it)
166 {
167 instances_.push_back(new Instance(index_, *it));
168 }
169 }
170
171
172 bool SliceOrdering::SortUsingPositions()
173 {
174 if (!hasNormal_)
175 {
176 return false;
177 }
178
179 for (size_t i = 0; i < instances_.size(); i++)
180 {
181 assert(instances_[i] != NULL);
182 if (!instances_[i]->hasPosition_)
183 {
184 return false;
185 }
186 }
187
188
189
190 return true;
191 }
192
193
194 SliceOrdering::SliceOrdering(ServerIndex& index,
195 const std::string& seriesId) :
196 index_(index),
197 seriesId_(seriesId)
198 {
199 ComputeNormal();
200 CreateInstances();
201 }
202
203
204 SliceOrdering::~SliceOrdering()
205 {
206 for (std::vector<Instance*>::iterator
207 it = instances_.begin(); it != instances_.end(); ++it)
208 {
209 if (*it != NULL)
210 {
211 delete *it;
212 }
213 }
214 }
215 }