comparison Framework/Scene2DViewport/LineMeasureTool.cpp @ 698:8b6adfb62a2f refactor-viewport-controller

Code is broken -- stashing ongoing work in a branch
author Benjamin Golinvaux <bgo@osimis.io>
date Wed, 15 May 2019 16:56:17 +0200
parents
children 28b9e3a54200
comparison
equal deleted inserted replaced
660:cb3b76d16234 698:8b6adfb62a2f
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 #include "LineMeasureTool.h"
22 #include "MeasureToolsToolbox.h"
23
24 #include <Core/Logging.h>
25
26
27 namespace OrthancStone
28 {
29 LineMeasureTool::~LineMeasureTool()
30 {
31 // this measuring tool is a RABI for the corresponding visual layers
32 // stored in the 2D scene
33 Disable();
34 RemoveFromScene();
35 }
36
37 void LineMeasureTool::RemoveFromScene()
38 {
39 if (layersCreated)
40 {
41 assert(GetScene()->HasLayer(polylineZIndex_));
42 assert(GetScene()->HasLayer(textZIndex_));
43 GetScene()->DeleteLayer(polylineZIndex_);
44 GetScene()->DeleteLayer(textZIndex_);
45 }
46 }
47
48
49 void LineMeasureTool::SetStart(ScenePoint2D start)
50 {
51 start_ = start;
52 RefreshScene();
53 }
54
55 void LineMeasureTool::SetEnd(ScenePoint2D end)
56 {
57 end_ = end;
58 RefreshScene();
59 }
60
61 void LineMeasureTool::Set(ScenePoint2D start, ScenePoint2D end)
62 {
63 start_ = start;
64 end_ = end;
65 RefreshScene();
66 }
67
68 PolylineSceneLayer* LineMeasureTool::GetPolylineLayer()
69 {
70 assert(GetScene()->HasLayer(polylineZIndex_));
71 ISceneLayer* layer = &(GetScene()->GetLayer(polylineZIndex_));
72 PolylineSceneLayer* concreteLayer = dynamic_cast<PolylineSceneLayer*>(layer);
73 assert(concreteLayer != NULL);
74 return concreteLayer;
75 }
76
77 TextSceneLayer* LineMeasureTool::GetTextLayer()
78 {
79 assert(GetScene()->HasLayer(textZIndex_));
80 ISceneLayer* layer = &(GetScene()->GetLayer(textZIndex_));
81 TextSceneLayer* concreteLayer = dynamic_cast<TextSceneLayer*>(layer);
82 assert(concreteLayer != NULL);
83 return concreteLayer;
84 }
85
86 void LineMeasureTool::RefreshScene()
87 {
88 if (IsEnabled())
89 {
90 if (!layersCreated)
91 {
92 // Create the layers if need be
93
94 assert(textZIndex_ == -1);
95 {
96 polylineZIndex_ = GetScene()->GetMaxDepth() + 100;
97 //LOG(INFO) << "set polylineZIndex_ to: " << polylineZIndex_;
98 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer());
99 GetScene()->SetLayer(polylineZIndex_, layer.release());
100 }
101 {
102 textZIndex_ = GetScene()->GetMaxDepth() + 100;
103 //LOG(INFO) << "set textZIndex_ to: " << textZIndex_;
104 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer());
105 GetScene()->SetLayer(textZIndex_, layer.release());
106 }
107 layersCreated = true;
108 }
109 else
110 {
111 assert(GetScene()->HasLayer(polylineZIndex_));
112 assert(GetScene()->HasLayer(textZIndex_));
113 }
114 {
115 // Fill the polyline layer with the measurement line
116
117 PolylineSceneLayer* polylineLayer = GetPolylineLayer();
118 polylineLayer->ClearAllChains();
119 polylineLayer->SetColor(0, 223, 21);
120
121 {
122 PolylineSceneLayer::Chain chain;
123 chain.push_back(start_);
124 chain.push_back(end_);
125 polylineLayer->AddChain(chain, false);
126 }
127
128 // handles
129 {
130 //void AddSquare(PolylineSceneLayer::Chain& chain,const Scene2D& scene,const ScenePoint2D& centerS,const double& sideLength)
131
132 {
133 PolylineSceneLayer::Chain chain;
134 AddSquare(chain, *GetScene(), start_, 10.0); //TODO: take DPI into account
135 polylineLayer->AddChain(chain, true);
136 }
137
138 {
139 PolylineSceneLayer::Chain chain;
140 AddSquare(chain, *GetScene(), end_, 10.0); //TODO: take DPI into account
141 polylineLayer->AddChain(chain, true);
142 }
143
144 //ScenePoint2D startC = start_.Apply(GetScene()->GetSceneToCanvasTransform());
145 //double squareSize = 10.0;
146 //double startHandleLX = startC.GetX() - squareSize/2;
147 //double startHandleTY = startC.GetY() - squareSize / 2;
148 //double startHandleRX = startC.GetX() + squareSize / 2;
149 //double startHandleBY = startC.GetY() + squareSize / 2;
150 //ScenePoint2D startLTC(startHandleLX, startHandleTY);
151 //ScenePoint2D startRTC(startHandleRX, startHandleTY);
152 //ScenePoint2D startRBC(startHandleRX, startHandleBY);
153 //ScenePoint2D startLBC(startHandleLX, startHandleBY);
154
155 //ScenePoint2D startLT = startLTC.Apply(GetScene()->GetCanvasToSceneTransform());
156 //ScenePoint2D startRT = startRTC.Apply(GetScene()->GetCanvasToSceneTransform());
157 //ScenePoint2D startRB = startRBC.Apply(GetScene()->GetCanvasToSceneTransform());
158 //ScenePoint2D startLB = startLBC.Apply(GetScene()->GetCanvasToSceneTransform());
159
160 //PolylineSceneLayer::Chain chain;
161 //chain.push_back(startLT);
162 //chain.push_back(startRT);
163 //chain.push_back(startRB);
164 //chain.push_back(startLB);
165 //polylineLayer->AddChain(chain, true);
166 }
167
168 }
169 {
170 // Set the text layer proporeties
171
172 TextSceneLayer* textLayer = GetTextLayer();
173 double deltaX = end_.GetX() - start_.GetX();
174 double deltaY = end_.GetY() - start_.GetY();
175 double squareDist = deltaX * deltaX + deltaY * deltaY;
176 double dist = sqrt(squareDist);
177 char buf[64];
178 sprintf(buf, "%0.02f units", dist);
179 textLayer->SetText(buf);
180 textLayer->SetColor(0, 223, 21);
181
182 // TODO: for now we simply position the text overlay at the middle
183 // of the measuring segment
184 double midX = 0.5*(end_.GetX() + start_.GetX());
185 double midY = 0.5*(end_.GetY() + start_.GetY());
186 textLayer->SetPosition(midX, midY);
187 }
188 }
189 else
190 {
191 if (layersCreated)
192 {
193 RemoveFromScene();
194 layersCreated = false;
195 }
196 }
197 }
198
199
200 }