comparison Framework/Deprecated/Widgets/PanZoomMouseTracker.cpp @ 732:c35e98d22764

move Deprecated classes to a separate folder
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 21 May 2019 14:27:35 +0200
parents Framework/Widgets/PanZoomMouseTracker.cpp@4f2416d519b4
children 2d8ab34c8c91
comparison
equal deleted inserted replaced
729:529189f399ec 732:c35e98d22764
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-2019 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 "PanZoomMouseTracker.h"
23
24 #include <Core/Logging.h>
25 #include <Core/OrthancException.h>
26 #include <math.h>
27
28 namespace Deprecated
29 {
30 Touch GetCenter(const std::vector<Touch>& touches)
31 {
32 return Touch((touches[0].x + touches[1].x) / 2.0f, (touches[0].y + touches[1].y) / 2.0f);
33 }
34
35 double GetDistance(const std::vector<Touch>& touches)
36 {
37 float dx = touches[0].x - touches[1].x;
38 float dy = touches[0].y - touches[1].y;
39 return sqrt((double)(dx * dx) + (double)(dy * dy));
40 }
41
42
43 PanZoomMouseTracker::PanZoomMouseTracker(WorldSceneWidget& that,
44 const std::vector<Touch>& startTouches)
45 : that_(that),
46 originalZoom_(that.GetView().GetZoom())
47 {
48 that.GetView().GetPan(originalPanX_, originalPanY_);
49 that.GetView().MapPixelCenterToScene(originalSceneTouches_, startTouches);
50
51 originalDisplayCenter_ = GetCenter(startTouches);
52 originalSceneCenter_ = GetCenter(originalSceneTouches_);
53 originalDisplayDistanceBetweenTouches_ = GetDistance(startTouches);
54
55 // printf("original Pan %f %f\n", originalPanX_, originalPanY_);
56 // printf("original Zoom %f \n", originalZoom_);
57 // printf("original distance %f \n", (float)originalDisplayDistanceBetweenTouches_);
58 // printf("original display touches 0 %f %f\n", startTouches[0].x, startTouches[0].y);
59 // printf("original display touches 1 %f %f\n", startTouches[1].x, startTouches[1].y);
60 // printf("original Scene center %f %f\n", originalSceneCenter_.x, originalSceneCenter_.y);
61
62 unsigned int height = that.GetView().GetDisplayHeight();
63
64 if (height <= 3)
65 {
66 idle_ = true;
67 LOG(WARNING) << "image is too small to zoom (current height = " << height << ")";
68 }
69 else
70 {
71 idle_ = false;
72 normalization_ = 1.0 / static_cast<double>(height - 1);
73 }
74
75 }
76
77
78 void PanZoomMouseTracker::Render(OrthancStone::CairoContext& context,
79 double zoom)
80 {
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
82 }
83
84
85 void PanZoomMouseTracker::MouseMove(int displayX,
86 int displayY,
87 double x,
88 double y,
89 const std::vector<Touch>& displayTouches,
90 const std::vector<Touch>& sceneTouches)
91 {
92 ViewportGeometry view = that_.GetView();
93
94 // printf("Display touches 0 %f %f\n", displayTouches[0].x, displayTouches[0].y);
95 // printf("Display touches 1 %f %f\n", displayTouches[1].x, displayTouches[1].y);
96 // printf("Scene touches 0 %f %f\n", sceneTouches[0].x, sceneTouches[0].y);
97 // printf("Scene touches 1 %f %f\n", sceneTouches[1].x, sceneTouches[1].y);
98
99 // printf("zoom = %f\n", view.GetZoom());
100 Touch currentSceneCenter = GetCenter(sceneTouches);
101 double panX = originalPanX_ + (currentSceneCenter.x - originalSceneCenter_.x) * view.GetZoom();
102 double panY = originalPanY_ + (currentSceneCenter.y - originalSceneCenter_.y) * view.GetZoom();
103
104 view.SetPan(panX, panY);
105
106 static const double MIN_ZOOM = -4;
107 static const double MAX_ZOOM = 4;
108
109 if (!idle_)
110 {
111 double currentDistanceBetweenTouches = GetDistance(displayTouches);
112
113 double dy = static_cast<double>(currentDistanceBetweenTouches - originalDisplayDistanceBetweenTouches_) * normalization_; // In the range [-1,1]
114 double z;
115
116 // Linear interpolation from [-1, 1] to [MIN_ZOOM, MAX_ZOOM]
117 if (dy < -1.0)
118 {
119 z = MIN_ZOOM;
120 }
121 else if (dy > 1.0)
122 {
123 z = MAX_ZOOM;
124 }
125 else
126 {
127 z = MIN_ZOOM + (MAX_ZOOM - MIN_ZOOM) * (dy + 1.0) / 2.0;
128 }
129
130 z = pow(2.0, z);
131
132 view.SetZoom(z * originalZoom_);
133 }
134
135 that_.SetView(view);
136 }
137 }