Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/ParallelSlicesCursor.cpp @ 0:351ab0da0150
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Oct 2016 15:34:11 +0200 |
parents | |
children | ff1e935768e7 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:351ab0da0150 |
---|---|
1 /** | |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 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 "ParallelSlicesCursor.h" | |
34 | |
35 #include "../Orthanc/Core/OrthancException.h" | |
36 | |
37 namespace OrthancStone | |
38 { | |
39 size_t ParallelSlicesCursor::GetDefaultSlice() | |
40 { | |
41 if (slices_.get() == NULL) | |
42 { | |
43 return 0; | |
44 } | |
45 else | |
46 { | |
47 return slices_->GetSliceCount() / 2; | |
48 } | |
49 } | |
50 | |
51 | |
52 size_t ParallelSlicesCursor::GetSliceCount() | |
53 { | |
54 boost::mutex::scoped_lock lock(mutex_); | |
55 | |
56 if (slices_.get() == NULL) | |
57 { | |
58 return 0; | |
59 } | |
60 else | |
61 { | |
62 return slices_->GetSliceCount(); | |
63 } | |
64 } | |
65 | |
66 | |
67 SliceGeometry ParallelSlicesCursor::GetSlice(size_t slice) | |
68 { | |
69 boost::mutex::scoped_lock lock(mutex_); | |
70 | |
71 if (slices_.get() == NULL) | |
72 { | |
73 return SliceGeometry(); | |
74 } | |
75 else | |
76 { | |
77 return slices_->GetSlice(slice); | |
78 } | |
79 } | |
80 | |
81 | |
82 void ParallelSlicesCursor::SetGeometry(const ParallelSlices& slices) | |
83 { | |
84 boost::mutex::scoped_lock lock(mutex_); | |
85 | |
86 slices_.reset(new ParallelSlices(slices)); | |
87 | |
88 currentSlice_ = GetDefaultSlice(); | |
89 } | |
90 | |
91 | |
92 SliceGeometry ParallelSlicesCursor::GetCurrentSlice() | |
93 { | |
94 boost::mutex::scoped_lock lock(mutex_); | |
95 | |
96 if (slices_.get() != NULL && | |
97 currentSlice_ < slices_->GetSliceCount()) | |
98 { | |
99 return slices_->GetSlice(currentSlice_); | |
100 } | |
101 else | |
102 { | |
103 return SliceGeometry(); // No slice is available, return the canonical geometry | |
104 } | |
105 } | |
106 | |
107 | |
108 bool ParallelSlicesCursor::SetDefaultSlice() | |
109 { | |
110 boost::mutex::scoped_lock lock(mutex_); | |
111 | |
112 size_t slice = GetDefaultSlice(); | |
113 | |
114 if (currentSlice_ != slice) | |
115 { | |
116 currentSlice_ = slice; | |
117 return true; | |
118 } | |
119 else | |
120 { | |
121 return false; | |
122 } | |
123 } | |
124 | |
125 | |
126 bool ParallelSlicesCursor::ApplyOffset(SliceOffsetMode mode, | |
127 int offset) | |
128 { | |
129 boost::mutex::scoped_lock lock(mutex_); | |
130 | |
131 if (slices_.get() == NULL) | |
132 { | |
133 return false; | |
134 } | |
135 | |
136 int count = slices_->GetSliceCount(); | |
137 if (count == 0) | |
138 { | |
139 return false; | |
140 } | |
141 | |
142 int slice; | |
143 if (static_cast<int>(currentSlice_) >= count) | |
144 { | |
145 slice = count - 1; | |
146 } | |
147 else | |
148 { | |
149 slice = currentSlice_; | |
150 } | |
151 | |
152 switch (mode) | |
153 { | |
154 case SliceOffsetMode_Absolute: | |
155 { | |
156 slice = offset; | |
157 break; | |
158 } | |
159 | |
160 case SliceOffsetMode_Relative: | |
161 { | |
162 slice += offset; | |
163 break; | |
164 } | |
165 | |
166 case SliceOffsetMode_Loop: | |
167 { | |
168 slice += offset; | |
169 while (slice < 0) | |
170 { | |
171 slice += count; | |
172 } | |
173 | |
174 while (slice >= count) | |
175 { | |
176 slice -= count; | |
177 } | |
178 | |
179 break; | |
180 } | |
181 | |
182 default: | |
183 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
184 } | |
185 | |
186 if (slice < 0) | |
187 { | |
188 slice = 0; | |
189 } | |
190 | |
191 if (slice >= count) | |
192 { | |
193 slice = count - 1; | |
194 } | |
195 | |
196 if (slice != static_cast<int>(currentSlice_)) | |
197 { | |
198 currentSlice_ = static_cast<int>(slice); | |
199 return true; | |
200 } | |
201 else | |
202 { | |
203 return false; | |
204 } | |
205 } | |
206 | |
207 | |
208 bool ParallelSlicesCursor::ApplyWheelEvent(MouseWheelDirection direction, | |
209 KeyboardModifiers modifiers) | |
210 { | |
211 int offset = (modifiers & KeyboardModifiers_Control ? 10 : 1); | |
212 | |
213 switch (direction) | |
214 { | |
215 case MouseWheelDirection_Down: | |
216 return ApplyOffset(SliceOffsetMode_Relative, -offset); | |
217 | |
218 case MouseWheelDirection_Up: | |
219 return ApplyOffset(SliceOffsetMode_Relative, offset); | |
220 | |
221 default: | |
222 return false; | |
223 } | |
224 } | |
225 | |
226 | |
227 bool ParallelSlicesCursor::LookupSliceContainingPoint(const Vector& p) | |
228 { | |
229 boost::mutex::scoped_lock lock(mutex_); | |
230 | |
231 size_t slice; | |
232 double distance; | |
233 | |
234 if (slices_.get() != NULL && | |
235 slices_->ComputeClosestSlice(slice, distance, p)) | |
236 { | |
237 if (currentSlice_ != slice) | |
238 { | |
239 currentSlice_ = slice; | |
240 return true; | |
241 } | |
242 } | |
243 | |
244 return false; | |
245 } | |
246 } |