Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Deprecated/Radiography/RadiographyLayerResizeTracker.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/Deprecated/Radiography/RadiographyLayerResizeTracker.cpp@30deba7bc8e2 |
children |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
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-2020 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 "RadiographyLayerResizeTracker.h" | |
23 | |
24 #include "RadiographySceneCommand.h" | |
25 | |
26 #include <OrthancException.h> | |
27 | |
28 #include <boost/math/special_functions/round.hpp> | |
29 | |
30 | |
31 namespace OrthancStone | |
32 { | |
33 static double ComputeDistance(double x1, | |
34 double y1, | |
35 double x2, | |
36 double y2) | |
37 { | |
38 double dx = x1 - x2; | |
39 double dy = y1 - y2; | |
40 return sqrt(dx * dx + dy * dy); | |
41 } | |
42 | |
43 | |
44 class RadiographyLayerResizeTracker::UndoRedoCommand : public RadiographySceneCommand | |
45 { | |
46 private: | |
47 double sourceSpacingX_; | |
48 double sourceSpacingY_; | |
49 double sourcePanX_; | |
50 double sourcePanY_; | |
51 double targetSpacingX_; | |
52 double targetSpacingY_; | |
53 double targetPanX_; | |
54 double targetPanY_; | |
55 | |
56 protected: | |
57 virtual void UndoInternal(RadiographyLayer& layer) const | |
58 { | |
59 layer.SetPixelSpacing(sourceSpacingX_, sourceSpacingY_); | |
60 layer.SetPan(sourcePanX_, sourcePanY_); | |
61 } | |
62 | |
63 virtual void RedoInternal(RadiographyLayer& layer) const | |
64 { | |
65 layer.SetPixelSpacing(targetSpacingX_, targetSpacingY_); | |
66 layer.SetPan(targetPanX_, targetPanY_); | |
67 } | |
68 | |
69 public: | |
70 UndoRedoCommand(const RadiographyLayerResizeTracker& tracker) : | |
71 RadiographySceneCommand(tracker.accessor_), | |
72 sourceSpacingX_(tracker.originalSpacingX_), | |
73 sourceSpacingY_(tracker.originalSpacingY_), | |
74 sourcePanX_(tracker.originalPanX_), | |
75 sourcePanY_(tracker.originalPanY_), | |
76 targetSpacingX_(tracker.accessor_.GetLayer().GetGeometry().GetPixelSpacingX()), | |
77 targetSpacingY_(tracker.accessor_.GetLayer().GetGeometry().GetPixelSpacingY()), | |
78 targetPanX_(tracker.accessor_.GetLayer().GetGeometry().GetPanX()), | |
79 targetPanY_(tracker.accessor_.GetLayer().GetGeometry().GetPanY()) | |
80 { | |
81 } | |
82 }; | |
83 | |
84 | |
85 RadiographyLayerResizeTracker::RadiographyLayerResizeTracker(UndoRedoStack& undoRedoStack, | |
86 RadiographyScene& scene, | |
87 size_t layer, | |
88 const ControlPoint& startControlPoint, | |
89 bool roundScaling) : | |
90 undoRedoStack_(undoRedoStack), | |
91 accessor_(scene, layer), | |
92 roundScaling_(roundScaling) | |
93 { | |
94 if (accessor_.IsValid() && | |
95 accessor_.GetLayer().GetGeometry().IsResizeable()) | |
96 { | |
97 originalSpacingX_ = accessor_.GetLayer().GetGeometry().GetPixelSpacingX(); | |
98 originalSpacingY_ = accessor_.GetLayer().GetGeometry().GetPixelSpacingY(); | |
99 originalPanX_ = accessor_.GetLayer().GetGeometry().GetPanX(); | |
100 originalPanY_ = accessor_.GetLayer().GetGeometry().GetPanY(); | |
101 | |
102 size_t oppositeControlPointType; | |
103 switch (startControlPoint.index) | |
104 { | |
105 case RadiographyControlPointType_TopLeftCorner: | |
106 oppositeControlPointType = RadiographyControlPointType_BottomRightCorner; | |
107 break; | |
108 | |
109 case RadiographyControlPointType_TopRightCorner: | |
110 oppositeControlPointType = RadiographyControlPointType_BottomLeftCorner; | |
111 break; | |
112 | |
113 case RadiographyControlPointType_BottomLeftCorner: | |
114 oppositeControlPointType = RadiographyControlPointType_TopRightCorner; | |
115 break; | |
116 | |
117 case RadiographyControlPointType_BottomRightCorner: | |
118 oppositeControlPointType = RadiographyControlPointType_TopLeftCorner; | |
119 break; | |
120 | |
121 default: | |
122 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
123 } | |
124 | |
125 accessor_.GetLayer().GetControlPoint(startOppositeControlPoint_, oppositeControlPointType); | |
126 | |
127 double d = ComputeDistance(startControlPoint.x, startControlPoint.y, startOppositeControlPoint_.x, startOppositeControlPoint_.y); | |
128 if (d >= std::numeric_limits<float>::epsilon()) | |
129 { | |
130 baseScaling_ = 1.0 / d; | |
131 } | |
132 else | |
133 { | |
134 // Avoid division by zero in extreme cases | |
135 accessor_.Invalidate(); | |
136 } | |
137 } | |
138 } | |
139 | |
140 | |
141 void RadiographyLayerResizeTracker::Render(CairoContext& context, | |
142 double zoom) | |
143 { | |
144 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
145 } | |
146 | |
147 | |
148 void RadiographyLayerResizeTracker::MouseUp() | |
149 { | |
150 if (accessor_.IsValid() && | |
151 accessor_.GetLayer().GetGeometry().IsResizeable()) | |
152 { | |
153 undoRedoStack_.Add(new UndoRedoCommand(*this)); | |
154 } | |
155 } | |
156 | |
157 | |
158 void RadiographyLayerResizeTracker::MouseMove(int displayX, | |
159 int displayY, | |
160 double sceneX, | |
161 double sceneY, | |
162 const std::vector<Deprecated::Touch>& displayTouches, | |
163 const std::vector<Deprecated::Touch>& sceneTouches) | |
164 { | |
165 static const double ROUND_SCALING = 0.1; | |
166 | |
167 if (accessor_.IsValid() && | |
168 accessor_.GetLayer().GetGeometry().IsResizeable()) | |
169 { | |
170 double scaling = ComputeDistance(startOppositeControlPoint_.x, startOppositeControlPoint_.y, sceneX, sceneY) * baseScaling_; | |
171 | |
172 if (roundScaling_) | |
173 { | |
174 scaling = boost::math::round<double>((scaling / ROUND_SCALING) * ROUND_SCALING); | |
175 } | |
176 | |
177 RadiographyLayer& layer = accessor_.GetLayer(); | |
178 layer.SetPixelSpacing(scaling * originalSpacingX_, | |
179 scaling * originalSpacingY_); | |
180 | |
181 // Keep the opposite corner at a fixed location | |
182 ControlPoint currentOppositeCorner; | |
183 layer.GetControlPoint(currentOppositeCorner, startOppositeControlPoint_.index); | |
184 layer.SetPan(layer.GetGeometry().GetPanX() + startOppositeControlPoint_.x - currentOppositeCorner.x, | |
185 layer.GetGeometry().GetPanY() + startOppositeControlPoint_.y - currentOppositeCorner.y); | |
186 } | |
187 } | |
188 } |