Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/ViewportGeometry.cpp @ 98:a33abae66344 wasm
scrolling over series
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 30 May 2017 15:02:24 +0200 |
parents | 28956ed68280 |
children | 53bd9277b025 4cff7b1ed31d |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 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. | |
0 | 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 | |
47 | 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 | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "ViewportGeometry.h" | |
23 | |
16 | 24 #include "../../Resources/Orthanc/Core/Logging.h" |
25 #include "../../Resources/Orthanc/Core/OrthancException.h" | |
0 | 26 |
27 #include <boost/math/special_functions/round.hpp> | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 void ViewportGeometry::ComputeTransform() | |
32 { | |
33 // The following lines must be read in reverse order! | |
34 cairo_matrix_t tmp; | |
35 | |
36 // Bring the center of the scene to the center of the view | |
37 cairo_matrix_init_translate(&transform_, | |
38 panX_ + static_cast<double>(width_) / 2.0, | |
39 panY_ + static_cast<double>(height_) / 2.0); | |
40 | |
41 // Apply the zoom around (0,0) | |
42 cairo_matrix_init_scale(&tmp, zoom_, zoom_); | |
43 cairo_matrix_multiply(&transform_, &tmp, &transform_); | |
44 | |
45 // Bring the center of the scene to (0,0) | |
46 cairo_matrix_init_translate(&tmp, -(x1_ + x2_) / 2.0, -(y1_ + y2_) / 2.0); | |
47 cairo_matrix_multiply(&transform_, &tmp, &transform_); | |
48 } | |
49 | |
50 | |
51 ViewportGeometry::ViewportGeometry() | |
52 { | |
53 x1_ = 0; | |
54 y1_ = 0; | |
55 x2_ = 0; | |
56 y2_ = 0; | |
57 | |
58 width_ = 0; | |
59 height_ = 0; | |
60 | |
61 zoom_ = 1; | |
62 panX_ = 0; | |
63 panY_ = 0; | |
64 | |
65 ComputeTransform(); | |
66 } | |
67 | |
68 | |
69 void ViewportGeometry::SetDisplaySize(unsigned int width, | |
70 unsigned int height) | |
71 { | |
72 if (width_ != width || | |
73 height_ != height) | |
74 { | |
75 LOG(INFO) << "New display size: " << width << "x" << height; | |
76 | |
77 width_ = width; | |
78 height_ = height; | |
79 | |
80 ComputeTransform(); | |
81 } | |
82 } | |
83 | |
84 | |
85 void ViewportGeometry::SetSceneExtent(double x1, | |
86 double y1, | |
87 double x2, | |
88 double y2) | |
89 { | |
90 if (x1 == x1_ && | |
91 y1 == y1_ && | |
92 x2 == x2_ && | |
93 y2 == y2_) | |
94 { | |
95 return; | |
96 } | |
97 else if (x1 > x2 || | |
98 y1 > y2) | |
99 { | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
101 } | |
102 else | |
103 { | |
104 LOG(INFO) << "New scene extent: (" << x1 << "," << y1 << ") => (" << x2 << "," << y2 << ")"; | |
105 | |
106 x1_ = x1; | |
107 y1_ = y1; | |
108 x2_ = x2; | |
109 y2_ = y2; | |
110 | |
111 ComputeTransform(); | |
112 } | |
113 } | |
114 | |
115 | |
116 void ViewportGeometry::GetSceneExtent(double& x1, | |
117 double& y1, | |
118 double& x2, | |
119 double& y2) const | |
120 { | |
121 x1 = x1_; | |
122 y1 = y1_; | |
123 x2 = x2_; | |
124 y2 = y2_; | |
125 } | |
126 | |
127 | |
128 void ViewportGeometry::MapDisplayToScene(double& sceneX /* out */, | |
129 double& sceneY /* out */, | |
130 double x, | |
131 double y) const | |
132 { | |
133 cairo_matrix_t transform = transform_; | |
134 | |
135 if (cairo_matrix_invert(&transform) != CAIRO_STATUS_SUCCESS) | |
136 { | |
137 LOG(ERROR) << "Cannot invert singular matrix"; | |
138 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
139 } | |
140 | |
141 sceneX = x; | |
142 sceneY = y; | |
143 cairo_matrix_transform_point(&transform, &sceneX, &sceneY); | |
144 } | |
145 | |
146 | |
147 void ViewportGeometry::MapSceneToDisplay(int& displayX /* out */, | |
148 int& displayY /* out */, | |
149 double x, | |
150 double y) const | |
151 { | |
152 cairo_matrix_transform_point(&transform_, &x, &y); | |
153 | |
154 displayX = static_cast<int>(boost::math::iround(x)); | |
155 displayY = static_cast<int>(boost::math::iround(y)); | |
156 } | |
157 | |
158 | |
159 void ViewportGeometry::SetDefaultView() | |
160 { | |
161 if (width_ > 0 && | |
162 height_ > 0 && | |
163 x2_ > x1_ + 10 * std::numeric_limits<double>::epsilon() && | |
164 y2_ > y1_ + 10 * std::numeric_limits<double>::epsilon()) | |
165 { | |
166 double zoomX = static_cast<double>(width_) / (x2_ - x1_); | |
167 double zoomY = static_cast<double>(height_) / (y2_ - y1_); | |
168 zoom_ = zoomX < zoomY ? zoomX : zoomY; | |
169 | |
170 panX_ = 0; | |
171 panY_ = 0; | |
172 | |
173 ComputeTransform(); | |
174 } | |
175 } | |
176 | |
177 | |
178 void ViewportGeometry::ApplyTransform(CairoContext& context) const | |
179 { | |
180 cairo_set_matrix(context.GetObject(), &transform_); | |
181 } | |
182 | |
183 | |
184 void ViewportGeometry::GetPan(double& x, | |
185 double& y) const | |
186 { | |
187 x = panX_; | |
188 y = panY_; | |
189 } | |
190 | |
191 | |
192 void ViewportGeometry::SetPan(double x, | |
193 double y) | |
194 { | |
195 panX_ = x; | |
196 panY_ = y; | |
197 ComputeTransform(); | |
198 } | |
199 | |
200 | |
201 void ViewportGeometry::SetZoom(double zoom) | |
202 { | |
203 zoom_ = zoom; | |
204 ComputeTransform(); | |
205 } | |
206 } |