comparison OrthancStone/Sources/Scene2D/GrayscaleWindowingSceneTracker.cpp @ 1533:82279abb92d0

GrayscaleWindowingSceneTracker
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 07 Aug 2020 15:55:59 +0200
parents
children 4fb8fdf03314
comparison
equal deleted inserted replaced
1532:c7a37c3a0b8e 1533:82279abb92d0
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 "GrayscaleWindowingSceneTracker.h"
23
24 #include "../Scene2DViewport/ViewportController.h"
25 #include "FloatTextureSceneLayer.h"
26
27 #include <OrthancException.h>
28
29 namespace OrthancStone
30 {
31 namespace
32 {
33 class GrayscaleLayerAccessor : public boost::noncopyable
34 {
35 private:
36 std::unique_ptr<IViewport::ILock> lock_;
37 FloatTextureSceneLayer* layer_;
38
39 public:
40 GrayscaleLayerAccessor(boost::shared_ptr<IViewport> viewport,
41 int layerIndex) :
42 layer_(NULL)
43 {
44 if (viewport != NULL)
45 {
46 lock_.reset(viewport->Lock());
47
48 if (lock_->GetController().GetScene().HasLayer(layerIndex))
49 {
50 ISceneLayer& layer = lock_->GetController().GetScene().GetLayer(layerIndex);
51 if (layer.GetType() == ISceneLayer::Type_FloatTexture)
52 {
53 layer_ = &dynamic_cast<FloatTextureSceneLayer&>(layer);
54 }
55 }
56 }
57 }
58
59 bool IsValid() const
60 {
61 return layer_ != NULL;
62 }
63
64 FloatTextureSceneLayer& GetLayer() const
65 {
66 if (layer_ == NULL)
67 {
68 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
69 }
70 else
71 {
72 return *layer_;
73 }
74 }
75
76 void Invalidate()
77 {
78 if (lock_.get() != NULL)
79 {
80 lock_->Invalidate();
81 }
82 }
83 };
84 }
85
86 void GrayscaleWindowingSceneTracker::SetWindowing(float center,
87 float width)
88 {
89 if (active_)
90 {
91 GrayscaleLayerAccessor accessor(viewport_, layerIndex_);
92
93 if (accessor.IsValid())
94 {
95 accessor.GetLayer().SetCustomWindowing(center, width);
96 accessor.Invalidate();
97 }
98 }
99 }
100
101
102 GrayscaleWindowingSceneTracker::GrayscaleWindowingSceneTracker(boost::shared_ptr<IViewport> viewport,
103 int layerIndex,
104 const PointerEvent& event,
105 unsigned int canvasWidth,
106 unsigned int canvasHeight) :
107 OneGesturePointerTracker(viewport),
108 layerIndex_(layerIndex),
109 clickX_(event.GetMainPosition().GetX()),
110 clickY_(event.GetMainPosition().GetY())
111 {
112 active_ = false;
113
114 if (canvasWidth > 3 &&
115 canvasHeight > 3)
116 {
117 GrayscaleLayerAccessor accessor(viewport_, layerIndex_);
118
119 if (accessor.IsValid())
120 {
121 FloatTextureSceneLayer& layer = accessor.GetLayer();
122
123 layer.GetWindowing(originalCenter_, originalWidth_);
124
125 float minValue, maxValue;
126 layer.GetRange(minValue, maxValue);
127
128 normalization_ = (maxValue - minValue) / static_cast<double>(std::min(canvasWidth, canvasHeight) - 1);
129 active_ = true;
130 }
131 else
132 {
133 LOG(INFO) << "Cannot create GrayscaleWindowingSceneTracker on a non-float texture";
134 }
135 }
136 }
137
138 void GrayscaleWindowingSceneTracker::PointerMove(const PointerEvent& event)
139 {
140 if (active_)
141 {
142 const double x = event.GetMainPosition().GetX();
143 const double y = event.GetMainPosition().GetY();
144
145 float center = originalCenter_ + (x - clickX_) * normalization_;
146 float width = originalWidth_ + (y - clickY_) * normalization_;
147
148 if (width <= 1)
149 {
150 width = 1;
151 }
152
153 SetWindowing(center, width);
154 }
155 }
156
157 void GrayscaleWindowingSceneTracker::Cancel()
158 {
159 SetWindowing(originalCenter_, originalWidth_);
160 }
161 }