Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/ViewportGeometry.cpp @ 546:fb7f4a5bdfc0 ct-pet-dose-struct
Merged in dev (pull request #1)
Merge from dev
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Tue, 02 Apr 2019 09:38:50 +0000 |
parents | 3b4df9925db6 |
children | 9a474e90e832 |
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 | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., 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 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <Core/Logging.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
25 #include <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) | |
109
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
46 cairo_matrix_init_translate(&tmp, |
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
47 -(sceneExtent_.GetX1() + sceneExtent_.GetX2()) / 2.0, |
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
48 -(sceneExtent_.GetY1() + sceneExtent_.GetY2()) / 2.0); |
0 | 49 cairo_matrix_multiply(&transform_, &tmp, &transform_); |
50 } | |
51 | |
52 | |
53 ViewportGeometry::ViewportGeometry() | |
54 { | |
55 width_ = 0; | |
56 height_ = 0; | |
57 | |
58 zoom_ = 1; | |
59 panX_ = 0; | |
60 panY_ = 0; | |
61 | |
62 ComputeTransform(); | |
63 } | |
64 | |
65 | |
66 void ViewportGeometry::SetDisplaySize(unsigned int width, | |
67 unsigned int height) | |
68 { | |
69 if (width_ != width || | |
70 height_ != height) | |
71 { | |
72 LOG(INFO) << "New display size: " << width << "x" << height; | |
73 | |
74 width_ = width; | |
75 height_ = height; | |
76 | |
77 ComputeTransform(); | |
78 } | |
79 } | |
80 | |
81 | |
111
7665ccbf33db
rename Extent as Extent2D
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
109
diff
changeset
|
82 void ViewportGeometry::SetSceneExtent(const Extent2D& extent) |
0 | 83 { |
109
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
84 LOG(INFO) << "New scene extent: (" |
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
85 << extent.GetX1() << "," << extent.GetY1() << ") => (" |
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
86 << extent.GetX2() << "," << extent.GetY2() << ")"; |
0 | 87 |
109
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
88 sceneExtent_ = extent; |
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
89 ComputeTransform(); |
0 | 90 } |
91 | |
92 | |
93 void ViewportGeometry::MapDisplayToScene(double& sceneX /* out */, | |
94 double& sceneY /* out */, | |
95 double x, | |
96 double y) const | |
97 { | |
98 cairo_matrix_t transform = transform_; | |
99 | |
100 if (cairo_matrix_invert(&transform) != CAIRO_STATUS_SUCCESS) | |
101 { | |
102 LOG(ERROR) << "Cannot invert singular matrix"; | |
103 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
104 } | |
105 | |
106 sceneX = x; | |
107 sceneY = y; | |
108 cairo_matrix_transform_point(&transform, &sceneX, &sceneY); | |
109 } | |
110 | |
111 | |
112 void ViewportGeometry::MapSceneToDisplay(int& displayX /* out */, | |
113 int& displayY /* out */, | |
114 double x, | |
115 double y) const | |
116 { | |
117 cairo_matrix_transform_point(&transform_, &x, &y); | |
118 | |
119 displayX = static_cast<int>(boost::math::iround(x)); | |
120 displayY = static_cast<int>(boost::math::iround(y)); | |
121 } | |
122 | |
123 | |
457
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
124 void ViewportGeometry::MapPixelCenterToScene(std::vector<Touch>& sceneTouches /* out */, |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
125 const std::vector<Touch>& displayTouches) const |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
126 { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
127 double sceneX, sceneY; |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
128 sceneTouches.clear(); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
129 for (size_t t = 0; t < displayTouches.size(); t++) |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
130 { |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
131 MapPixelCenterToScene(sceneX, sceneY, displayTouches[t].x, displayTouches[t].y); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
132 sceneTouches.push_back(Touch((float)sceneX, (float)sceneY)); |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
133 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
134 } |
3b4df9925db6
added support for 'touch' in mouse trackers. This is still a bit hacky and we need to refactor it to make it clean. Thanks to that, Pan and zoom are available together with 2 touches
Alain Mazy <alain@mazy.be>
parents:
439
diff
changeset
|
135 |
332
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
136 void ViewportGeometry::MapPixelCenterToScene(double& sceneX, |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
137 double& sceneY, |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
138 int x, |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
139 int y) const |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
140 { |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
141 // Take the center of the pixel |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
142 MapDisplayToScene(sceneX, sceneY, |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
143 static_cast<double>(x) + 0.5, |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
144 static_cast<double>(y) + 0.5); |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
145 } |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
146 |
50e5ec1bdd46
separating ZoomMouseTracker and PanMouseTracker from WorldSceneWidget
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
330
diff
changeset
|
147 |
330 | 148 void ViewportGeometry::FitContent() |
0 | 149 { |
150 if (width_ > 0 && | |
151 height_ > 0 && | |
109
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
152 !sceneExtent_.IsEmpty()) |
0 | 153 { |
109
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
154 double zoomX = static_cast<double>(width_) / (sceneExtent_.GetX2() - sceneExtent_.GetX1()); |
53bd9277b025
using the Extent class
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
155 double zoomY = static_cast<double>(height_) / (sceneExtent_.GetY2() - sceneExtent_.GetY1()); |
0 | 156 zoom_ = zoomX < zoomY ? zoomX : zoomY; |
157 | |
158 panX_ = 0; | |
159 panY_ = 0; | |
160 | |
161 ComputeTransform(); | |
162 } | |
163 } | |
164 | |
165 | |
166 void ViewportGeometry::ApplyTransform(CairoContext& context) const | |
167 { | |
168 cairo_set_matrix(context.GetObject(), &transform_); | |
169 } | |
170 | |
171 | |
172 void ViewportGeometry::GetPan(double& x, | |
173 double& y) const | |
174 { | |
175 x = panX_; | |
176 y = panY_; | |
177 } | |
178 | |
179 | |
180 void ViewportGeometry::SetPan(double x, | |
181 double y) | |
182 { | |
183 panX_ = x; | |
184 panY_ = y; | |
185 ComputeTransform(); | |
186 } | |
187 | |
188 | |
189 void ViewportGeometry::SetZoom(double zoom) | |
190 { | |
191 zoom_ = zoom; | |
192 ComputeTransform(); | |
193 } | |
340
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
194 |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
195 |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
196 Matrix ViewportGeometry::GetMatrix() const |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
197 { |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
198 Matrix m(3, 3); |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
199 |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
200 m(0, 0) = transform_.xx; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
201 m(0, 1) = transform_.xy; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
202 m(0, 2) = transform_.x0; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
203 m(1, 0) = transform_.yx; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
204 m(1, 1) = transform_.yy; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
205 m(1, 2) = transform_.y0; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
206 m(2, 0) = 0; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
207 m(2, 1) = 0; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
208 m(2, 2) = 1; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
209 |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
210 return m; |
f5d5814a41a0
rendering BitmapStack
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
332
diff
changeset
|
211 } |
0 | 212 } |