comparison Framework/Radiography/RadiographyLayer.cpp @ 410:6decc0ba9da5

rename RadiographyScene::Layer as RadiographyLayer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 12 Nov 2018 15:52:03 +0100
parents
children b85f635f1eb5 b70e9be013e4
comparison
equal deleted inserted replaced
409:99c9b3238008 410:6decc0ba9da5
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-2018 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/
20
21
22 #include "RadiographyLayer.h"
23
24 #include <Core/OrthancException.h>
25
26
27 namespace OrthancStone
28 {
29 static double Square(double x)
30 {
31 return x * x;
32 }
33
34
35 void RadiographyLayer::UpdateTransform()
36 {
37 transform_ = AffineTransform2D::CreateScaling(pixelSpacingX_, pixelSpacingY_);
38
39 double centerX, centerY;
40 GetCenter(centerX, centerY);
41
42 transform_ = AffineTransform2D::Combine(
43 AffineTransform2D::CreateOffset(panX_ + centerX, panY_ + centerY),
44 AffineTransform2D::CreateRotation(angle_),
45 AffineTransform2D::CreateOffset(-centerX, -centerY),
46 transform_);
47
48 transformInverse_ = AffineTransform2D::Invert(transform_);
49 }
50
51
52 void RadiographyLayer::AddToExtent(Extent2D& extent,
53 double x,
54 double y) const
55 {
56 transform_.Apply(x, y);
57 extent.AddPoint(x, y);
58 }
59
60
61 void RadiographyLayer::GetCornerInternal(double& x,
62 double& y,
63 Corner corner,
64 unsigned int cropX,
65 unsigned int cropY,
66 unsigned int cropWidth,
67 unsigned int cropHeight) const
68 {
69 double dx = static_cast<double>(cropX);
70 double dy = static_cast<double>(cropY);
71 double dwidth = static_cast<double>(cropWidth);
72 double dheight = static_cast<double>(cropHeight);
73
74 switch (corner)
75 {
76 case Corner_TopLeft:
77 x = dx;
78 y = dy;
79 break;
80
81 case Corner_TopRight:
82 x = dx + dwidth;
83 y = dy;
84 break;
85
86 case Corner_BottomLeft:
87 x = dx;
88 y = dy + dheight;
89 break;
90
91 case Corner_BottomRight:
92 x = dx + dwidth;
93 y = dy + dheight;
94 break;
95
96 default:
97 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
98 }
99
100 transform_.Apply(x, y);
101 }
102
103
104 bool RadiographyLayer::Contains(double x,
105 double y) const
106 {
107 transformInverse_.Apply(x, y);
108
109 unsigned int cropX, cropY, cropWidth, cropHeight;
110 GetCrop(cropX, cropY, cropWidth, cropHeight);
111
112 return (x >= cropX && x <= cropX + cropWidth &&
113 y >= cropY && y <= cropY + cropHeight);
114 }
115
116
117 void RadiographyLayer::DrawBorders(CairoContext& context,
118 double zoom)
119 {
120 unsigned int cx, cy, width, height;
121 GetCrop(cx, cy, width, height);
122
123 double dx = static_cast<double>(cx);
124 double dy = static_cast<double>(cy);
125 double dwidth = static_cast<double>(width);
126 double dheight = static_cast<double>(height);
127
128 cairo_t* cr = context.GetObject();
129 cairo_set_line_width(cr, 2.0 / zoom);
130
131 double x, y;
132 x = dx;
133 y = dy;
134 transform_.Apply(x, y);
135 cairo_move_to(cr, x, y);
136
137 x = dx + dwidth;
138 y = dy;
139 transform_.Apply(x, y);
140 cairo_line_to(cr, x, y);
141
142 x = dx + dwidth;
143 y = dy + dheight;
144 transform_.Apply(x, y);
145 cairo_line_to(cr, x, y);
146
147 x = dx;
148 y = dy + dheight;
149 transform_.Apply(x, y);
150 cairo_line_to(cr, x, y);
151
152 x = dx;
153 y = dy;
154 transform_.Apply(x, y);
155 cairo_line_to(cr, x, y);
156
157 cairo_stroke(cr);
158 }
159
160
161 RadiographyLayer::RadiographyLayer() :
162 index_(0),
163 hasSize_(false),
164 width_(0),
165 height_(0),
166 hasCrop_(false),
167 pixelSpacingX_(1),
168 pixelSpacingY_(1),
169 panX_(0),
170 panY_(0),
171 angle_(0),
172 resizeable_(false)
173 {
174 UpdateTransform();
175 }
176
177
178 void RadiographyLayer::SetCrop(unsigned int x,
179 unsigned int y,
180 unsigned int width,
181 unsigned int height)
182 {
183 if (!hasSize_)
184 {
185 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
186 }
187
188 if (x + width > width_ ||
189 y + height > height_)
190 {
191 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
192 }
193
194 hasCrop_ = true;
195 cropX_ = x;
196 cropY_ = y;
197 cropWidth_ = width;
198 cropHeight_ = height;
199
200 UpdateTransform();
201 }
202
203
204 void RadiographyLayer::GetCrop(unsigned int& x,
205 unsigned int& y,
206 unsigned int& width,
207 unsigned int& height) const
208 {
209 if (hasCrop_)
210 {
211 x = cropX_;
212 y = cropY_;
213 width = cropWidth_;
214 height = cropHeight_;
215 }
216 else
217 {
218 x = 0;
219 y = 0;
220 width = width_;
221 height = height_;
222 }
223 }
224
225
226 void RadiographyLayer::SetAngle(double angle)
227 {
228 angle_ = angle;
229 UpdateTransform();
230 }
231
232
233 void RadiographyLayer::SetSize(unsigned int width,
234 unsigned int height)
235 {
236 if (hasSize_ &&
237 (width != width_ ||
238 height != height_))
239 {
240 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageSize);
241 }
242
243 hasSize_ = true;
244 width_ = width;
245 height_ = height;
246
247 UpdateTransform();
248 }
249
250
251 Extent2D RadiographyLayer::GetExtent() const
252 {
253 Extent2D extent;
254
255 unsigned int x, y, width, height;
256 GetCrop(x, y, width, height);
257
258 double dx = static_cast<double>(x);
259 double dy = static_cast<double>(y);
260 double dwidth = static_cast<double>(width);
261 double dheight = static_cast<double>(height);
262
263 AddToExtent(extent, dx, dy);
264 AddToExtent(extent, dx + dwidth, dy);
265 AddToExtent(extent, dx, dy + dheight);
266 AddToExtent(extent, dx + dwidth, dy + dheight);
267
268 return extent;
269 }
270
271
272 bool RadiographyLayer::GetPixel(unsigned int& imageX,
273 unsigned int& imageY,
274 double sceneX,
275 double sceneY) const
276 {
277 if (width_ == 0 ||
278 height_ == 0)
279 {
280 return false;
281 }
282 else
283 {
284 transformInverse_.Apply(sceneX, sceneY);
285
286 int x = static_cast<int>(std::floor(sceneX));
287 int y = static_cast<int>(std::floor(sceneY));
288
289 if (x < 0)
290 {
291 imageX = 0;
292 }
293 else if (x >= static_cast<int>(width_))
294 {
295 imageX = width_;
296 }
297 else
298 {
299 imageX = static_cast<unsigned int>(x);
300 }
301
302 if (y < 0)
303 {
304 imageY = 0;
305 }
306 else if (y >= static_cast<int>(height_))
307 {
308 imageY = height_;
309 }
310 else
311 {
312 imageY = static_cast<unsigned int>(y);
313 }
314
315 return true;
316 }
317 }
318
319
320 void RadiographyLayer::SetPan(double x,
321 double y)
322 {
323 panX_ = x;
324 panY_ = y;
325 UpdateTransform();
326 }
327
328
329 void RadiographyLayer::SetPixelSpacing(double x,
330 double y)
331 {
332 pixelSpacingX_ = x;
333 pixelSpacingY_ = y;
334 UpdateTransform();
335 }
336
337
338 void RadiographyLayer::GetCenter(double& centerX,
339 double& centerY) const
340 {
341 centerX = static_cast<double>(width_) / 2.0;
342 centerY = static_cast<double>(height_) / 2.0;
343 transform_.Apply(centerX, centerY);
344 }
345
346
347 void RadiographyLayer::GetCorner(double& x /* out */,
348 double& y /* out */,
349 Corner corner) const
350 {
351 unsigned int cropX, cropY, cropWidth, cropHeight;
352 GetCrop(cropX, cropY, cropWidth, cropHeight);
353 GetCornerInternal(x, y, corner, cropX, cropY, cropWidth, cropHeight);
354 }
355
356
357 bool RadiographyLayer::LookupCorner(Corner& corner /* out */,
358 double x,
359 double y,
360 double zoom,
361 double viewportDistance) const
362 {
363 static const Corner CORNERS[] = {
364 Corner_TopLeft,
365 Corner_TopRight,
366 Corner_BottomLeft,
367 Corner_BottomRight
368 };
369
370 unsigned int cropX, cropY, cropWidth, cropHeight;
371 GetCrop(cropX, cropY, cropWidth, cropHeight);
372
373 double threshold = Square(viewportDistance / zoom);
374
375 for (size_t i = 0; i < 4; i++)
376 {
377 double cx, cy;
378 GetCornerInternal(cx, cy, CORNERS[i], cropX, cropY, cropWidth, cropHeight);
379
380 double d = Square(cx - x) + Square(cy - y);
381
382 if (d <= threshold)
383 {
384 corner = CORNERS[i];
385 return true;
386 }
387 }
388
389 return false;
390 }
391 }