comparison Framework/Toolbox/ViewportGeometry.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 "ViewportGeometry.h"
34
35 #include "../Orthanc/Core/Logging.h"
36 #include "../Orthanc/Core/OrthancException.h"
37
38 #include <boost/math/special_functions/round.hpp>
39
40 namespace OrthancStone
41 {
42 void ViewportGeometry::ComputeTransform()
43 {
44 // The following lines must be read in reverse order!
45 cairo_matrix_t tmp;
46
47 // Bring the center of the scene to the center of the view
48 cairo_matrix_init_translate(&transform_,
49 panX_ + static_cast<double>(width_) / 2.0,
50 panY_ + static_cast<double>(height_) / 2.0);
51
52 // Apply the zoom around (0,0)
53 cairo_matrix_init_scale(&tmp, zoom_, zoom_);
54 cairo_matrix_multiply(&transform_, &tmp, &transform_);
55
56 // Bring the center of the scene to (0,0)
57 cairo_matrix_init_translate(&tmp, -(x1_ + x2_) / 2.0, -(y1_ + y2_) / 2.0);
58 cairo_matrix_multiply(&transform_, &tmp, &transform_);
59 }
60
61
62 ViewportGeometry::ViewportGeometry()
63 {
64 x1_ = 0;
65 y1_ = 0;
66 x2_ = 0;
67 y2_ = 0;
68
69 width_ = 0;
70 height_ = 0;
71
72 zoom_ = 1;
73 panX_ = 0;
74 panY_ = 0;
75
76 ComputeTransform();
77 }
78
79
80 void ViewportGeometry::SetDisplaySize(unsigned int width,
81 unsigned int height)
82 {
83 if (width_ != width ||
84 height_ != height)
85 {
86 LOG(INFO) << "New display size: " << width << "x" << height;
87
88 width_ = width;
89 height_ = height;
90
91 ComputeTransform();
92 }
93 }
94
95
96 void ViewportGeometry::SetSceneExtent(double x1,
97 double y1,
98 double x2,
99 double y2)
100 {
101 if (x1 == x1_ &&
102 y1 == y1_ &&
103 x2 == x2_ &&
104 y2 == y2_)
105 {
106 return;
107 }
108 else if (x1 > x2 ||
109 y1 > y2)
110 {
111 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
112 }
113 else
114 {
115 LOG(INFO) << "New scene extent: (" << x1 << "," << y1 << ") => (" << x2 << "," << y2 << ")";
116
117 x1_ = x1;
118 y1_ = y1;
119 x2_ = x2;
120 y2_ = y2;
121
122 ComputeTransform();
123 }
124 }
125
126
127 void ViewportGeometry::GetSceneExtent(double& x1,
128 double& y1,
129 double& x2,
130 double& y2) const
131 {
132 x1 = x1_;
133 y1 = y1_;
134 x2 = x2_;
135 y2 = y2_;
136 }
137
138
139 void ViewportGeometry::MapDisplayToScene(double& sceneX /* out */,
140 double& sceneY /* out */,
141 double x,
142 double y) const
143 {
144 cairo_matrix_t transform = transform_;
145
146 if (cairo_matrix_invert(&transform) != CAIRO_STATUS_SUCCESS)
147 {
148 LOG(ERROR) << "Cannot invert singular matrix";
149 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
150 }
151
152 sceneX = x;
153 sceneY = y;
154 cairo_matrix_transform_point(&transform, &sceneX, &sceneY);
155 }
156
157
158 void ViewportGeometry::MapSceneToDisplay(int& displayX /* out */,
159 int& displayY /* out */,
160 double x,
161 double y) const
162 {
163 cairo_matrix_transform_point(&transform_, &x, &y);
164
165 displayX = static_cast<int>(boost::math::iround(x));
166 displayY = static_cast<int>(boost::math::iround(y));
167 }
168
169
170 void ViewportGeometry::SetDefaultView()
171 {
172 if (width_ > 0 &&
173 height_ > 0 &&
174 x2_ > x1_ + 10 * std::numeric_limits<double>::epsilon() &&
175 y2_ > y1_ + 10 * std::numeric_limits<double>::epsilon())
176 {
177 double zoomX = static_cast<double>(width_) / (x2_ - x1_);
178 double zoomY = static_cast<double>(height_) / (y2_ - y1_);
179 zoom_ = zoomX < zoomY ? zoomX : zoomY;
180
181 panX_ = 0;
182 panY_ = 0;
183
184 ComputeTransform();
185 }
186 }
187
188
189 void ViewportGeometry::ApplyTransform(CairoContext& context) const
190 {
191 cairo_set_matrix(context.GetObject(), &transform_);
192 }
193
194
195 void ViewportGeometry::GetPan(double& x,
196 double& y) const
197 {
198 x = panX_;
199 y = panY_;
200 }
201
202
203 void ViewportGeometry::SetPan(double x,
204 double y)
205 {
206 panX_ = x;
207 panY_ = y;
208 ComputeTransform();
209 }
210
211
212 void ViewportGeometry::SetZoom(double zoom)
213 {
214 zoom_ = zoom;
215 ComputeTransform();
216 }
217 }