644
|
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 "TrackerSampleApp.h"
|
|
22
|
|
23 #include "../Common/CreateLineMeasureTracker.h"
|
|
24
|
|
25 #include "../../Applications/Sdl/SdlOpenGLWindow.h"
|
|
26
|
|
27 #include "../../Framework/Scene2D/PanSceneTracker.h"
|
|
28 #include "../../Framework/Scene2D/RotateSceneTracker.h"
|
|
29 #include "../../Framework/Scene2D/Scene2D.h"
|
|
30 #include "../../Framework/Scene2D/ZoomSceneTracker.h"
|
|
31 #include "../../Framework/Scene2D/CairoCompositor.h"
|
|
32 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h"
|
|
33 #include "../../Framework/Scene2D/OpenGLCompositor.h"
|
|
34 #include "../../Framework/StoneInitialization.h"
|
|
35
|
|
36 // From Orthanc framework
|
|
37 #include <Core/Logging.h>
|
|
38 #include <Core/OrthancException.h>
|
|
39 #include <Core/Images/Image.h>
|
|
40 #include <Core/Images/ImageProcessing.h>
|
|
41 #include <Core/Images/PngWriter.h>
|
|
42
|
|
43 #include <SDL.h>
|
|
44 #include <stdio.h>
|
|
45
|
|
46 using namespace Orthanc;
|
|
47
|
|
48 namespace OrthancStone
|
|
49 {
|
|
50 const char* MeasureToolToString(size_t i)
|
|
51 {
|
|
52 static const char* descs[] = {
|
|
53 "GuiTool_Rotate",
|
|
54 "GuiTool_Pan",
|
|
55 "GuiTool_Zoom",
|
|
56 "GuiTool_LineMeasure",
|
|
57 "GuiTool_CircleMeasure",
|
|
58 "GuiTool_AngleMeasure",
|
|
59 "GuiTool_EllipseMeasure",
|
|
60 "GuiTool_LAST"
|
|
61 };
|
|
62 if (i >= GuiTool_LAST)
|
|
63 {
|
|
64 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Wrong tool index");
|
|
65 }
|
|
66 return descs[i];
|
|
67 }
|
|
68
|
|
69 Scene2D& TrackerSampleApp::GetScene()
|
|
70 {
|
|
71 return scene_;
|
|
72 }
|
|
73
|
|
74 void TrackerSampleApp::SelectNextTool()
|
|
75 {
|
|
76 currentTool_ = static_cast<GuiTool>(currentTool_ + 1);
|
|
77 if (currentTool_ == GuiTool_LAST)
|
|
78 currentTool_ = static_cast<GuiTool>(0);;
|
|
79 printf("Current tool is now: %s\n", MeasureToolToString(currentTool_));
|
|
80 }
|
|
81
|
|
82 void TrackerSampleApp::DisplayInfoText(const PointerEvent& e)
|
|
83 {
|
|
84 ScenePoint2D p = e.GetMainPosition().Apply(scene_.GetCanvasToSceneTransform());
|
|
85
|
|
86 char buf[64];
|
|
87 sprintf(buf, "(%0.02f,%0.02f)", p.GetX(), p.GetY());
|
|
88
|
|
89 if (scene_.HasLayer(INFOTEXT_LAYER_ZINDEX))
|
|
90 {
|
|
91 TextSceneLayer& layer =
|
|
92 dynamic_cast<TextSceneLayer&>(scene_.GetLayer(INFOTEXT_LAYER_ZINDEX));
|
|
93 layer.SetText(buf);
|
|
94 layer.SetPosition(p.GetX(), p.GetY());
|
|
95 }
|
|
96 else
|
|
97 {
|
|
98 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer);
|
|
99 layer->SetColor(0, 255, 0);
|
|
100 layer->SetText(buf);
|
|
101 layer->SetBorder(20);
|
|
102 layer->SetAnchor(BitmapAnchor_BottomCenter);
|
|
103 layer->SetPosition(p.GetX(), p.GetY());
|
|
104 scene_.SetLayer(INFOTEXT_LAYER_ZINDEX, layer.release());
|
|
105 }
|
|
106 }
|
|
107
|
|
108 void TrackerSampleApp::HideInfoText()
|
|
109 {
|
|
110 scene_.DeleteLayer(INFOTEXT_LAYER_ZINDEX);
|
|
111 }
|
|
112
|
|
113 void TrackerSampleApp::HandleApplicationEvent(
|
|
114 const OpenGLCompositor & compositor,
|
|
115 const SDL_Event & event)
|
|
116 {
|
|
117 if (event.type == SDL_MOUSEMOTION)
|
|
118 {
|
|
119 int scancodeCount = 0;
|
|
120 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
|
|
121
|
|
122 if (activeTracker_.get() == NULL &&
|
|
123 SDL_SCANCODE_LCTRL < scancodeCount &&
|
|
124 keyboardState[SDL_SCANCODE_LCTRL])
|
|
125 {
|
|
126 // The "left-ctrl" key is down, while no tracker is present
|
|
127 // Let's display the info text
|
|
128 PointerEvent e;
|
|
129 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
|
|
130 DisplayInfoText(e);
|
|
131 }
|
|
132 else
|
|
133 {
|
|
134 HideInfoText();
|
|
135 //LOG(TRACE) << "(event.type == SDL_MOUSEMOTION)";
|
|
136 if (activeTracker_.get() != NULL)
|
|
137 {
|
|
138 //LOG(TRACE) << "(activeTracker_.get() != NULL)";
|
|
139 PointerEvent e;
|
|
140 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
|
|
141 //LOG(TRACE) << "event.button.x = " << event.button.x << " " <<
|
|
142 // "event.button.y = " << event.button.y;
|
|
143 //LOG(TRACE) << "activeTracker_->PointerMove(e); " <<
|
|
144 // e.GetMainPosition().GetX() << " " << e.GetMainPosition().GetY();
|
|
145 activeTracker_->PointerMove(e);
|
|
146 if (!activeTracker_->IsActive())
|
|
147 activeTracker_ = NULL;
|
|
148 }
|
|
149 }
|
|
150 }
|
|
151 else if (event.type == SDL_MOUSEBUTTONUP)
|
|
152 {
|
|
153 if (activeTracker_)
|
|
154 {
|
|
155 PointerEvent e;
|
|
156 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
|
|
157 activeTracker_->PointerUp(e);
|
|
158 if (!activeTracker_->IsActive())
|
|
159 activeTracker_ = NULL;
|
|
160 }
|
|
161 }
|
|
162 else if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
163 {
|
|
164 PointerEvent e;
|
|
165 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
|
|
166 if (activeTracker_)
|
|
167 {
|
|
168 activeTracker_->PointerDown(e);
|
|
169 if (!activeTracker_->IsActive())
|
|
170 activeTracker_ = NULL;
|
|
171 }
|
|
172 else
|
|
173 {
|
|
174 // we ATTEMPT to create a tracker if need be
|
|
175 activeTracker_ = CreateSuitableTracker(event, e, compositor);
|
|
176 }
|
|
177 }
|
|
178 else if (event.type == SDL_KEYDOWN &&
|
|
179 event.key.repeat == 0 /* Ignore key bounce */)
|
|
180 {
|
|
181 switch (event.key.keysym.sym)
|
|
182 {
|
|
183 case SDLK_ESCAPE:
|
|
184 if (activeTracker_)
|
|
185 {
|
|
186 activeTracker_->Cancel();
|
|
187 if (!activeTracker_->IsActive())
|
|
188 activeTracker_ = NULL;
|
|
189 }
|
|
190 break;
|
|
191
|
|
192 case SDLK_t:
|
|
193 if (!activeTracker_)
|
|
194 SelectNextTool();
|
|
195 else
|
|
196 {
|
|
197 LOG(WARNING) << "You cannot change the active tool when an interaction"
|
|
198 " is taking place";
|
|
199 }
|
|
200 break;
|
|
201
|
|
202 case SDLK_s:
|
|
203 scene_.FitContent(compositor.GetCanvasWidth(),
|
|
204 compositor.GetCanvasHeight());
|
|
205 break;
|
|
206
|
|
207 case SDLK_c:
|
|
208 TakeScreenshot(
|
|
209 "screenshot.png",
|
|
210 compositor.GetCanvasWidth(),
|
|
211 compositor.GetCanvasHeight());
|
|
212 break;
|
|
213
|
|
214 default:
|
|
215 break;
|
|
216 }
|
|
217 }
|
|
218 }
|
|
219
|
|
220 FlexiblePointerTrackerPtr TrackerSampleApp::CreateSuitableTracker(
|
|
221 const SDL_Event & event,
|
|
222 const PointerEvent & e,
|
|
223 const OpenGLCompositor & compositor)
|
|
224 {
|
|
225 switch (event.button.button)
|
|
226 {
|
|
227 case SDL_BUTTON_MIDDLE:
|
|
228 return CreateSimpleTrackerAdapter(PointerTrackerPtr(
|
|
229 new PanSceneTracker(scene_, e)));
|
|
230
|
|
231 case SDL_BUTTON_RIGHT:
|
|
232 return CreateSimpleTrackerAdapter(PointerTrackerPtr(
|
|
233 new ZoomSceneTracker(scene_, e, compositor.GetCanvasHeight())));
|
|
234
|
|
235 case SDL_BUTTON_LEFT:
|
|
236 {
|
|
237 //LOG(TRACE) << "CreateSuitableTracker: case SDL_BUTTON_LEFT:";
|
|
238 // TODO: we need to iterate on the set of measuring tool and perform
|
|
239 // a hit test to check if a tracker needs to be created for edition.
|
|
240 // Otherwise, depending upon the active tool, we might want to create
|
|
241 // a "measuring tool creation" tracker
|
|
242
|
|
243 // TODO: if there are conflicts, we should prefer a tracker that
|
|
244 // pertains to the type of measuring tool currently selected (TBD?)
|
|
245 FlexiblePointerTrackerPtr hitTestTracker = TrackerHitTest(e);
|
|
246
|
|
247 if (hitTestTracker != NULL)
|
|
248 {
|
|
249 //LOG(TRACE) << "hitTestTracker != NULL";
|
|
250 return hitTestTracker;
|
|
251 }
|
|
252 else
|
|
253 {
|
|
254 switch (currentTool_)
|
|
255 {
|
|
256 case GuiTool_Rotate:
|
|
257 //LOG(TRACE) << "Creating RotateSceneTracker";
|
|
258 return CreateSimpleTrackerAdapter(PointerTrackerPtr(
|
|
259 new RotateSceneTracker(scene_, e)));
|
|
260 case GuiTool_LineMeasure:
|
|
261 return FlexiblePointerTrackerPtr(new CreateLineMeasureTracker(
|
|
262 scene_, undoStack_, measureTools_, e));
|
|
263 case GuiTool_Pan:
|
|
264 return CreateSimpleTrackerAdapter(PointerTrackerPtr(
|
|
265 new PanSceneTracker(scene_, e)));
|
|
266 case GuiTool_Zoom:
|
|
267 return CreateSimpleTrackerAdapter(PointerTrackerPtr(
|
|
268 new ZoomSceneTracker(scene_, e, compositor.GetCanvasHeight())));
|
|
269 //case GuiTool_AngleMeasure:
|
|
270 // return new AngleMeasureTracker(scene_, measureTools_, undoStack_, e);
|
|
271 //case GuiTool_CircleMeasure:
|
|
272 // return new CircleMeasureTracker(scene_, measureTools_, undoStack_, e);
|
|
273 //case GuiTool_EllipseMeasure:
|
|
274 // return new EllipseMeasureTracker(scene_, measureTools_, undoStack_, e);
|
|
275 case GuiTool_AngleMeasure:
|
|
276 LOG(ERROR) << "Not implemented yet!";
|
|
277 return NULL;
|
|
278 case GuiTool_CircleMeasure:
|
|
279 LOG(ERROR) << "Not implemented yet!";
|
|
280 return NULL;
|
|
281 case GuiTool_EllipseMeasure:
|
|
282 LOG(ERROR) << "Not implemented yet!";
|
|
283 return NULL;
|
|
284 default:
|
|
285 throw OrthancException(ErrorCode_InternalError, "Wrong tool!");
|
|
286 }
|
|
287 }
|
|
288 }
|
|
289 default:
|
|
290 return NULL;
|
|
291 }
|
|
292 }
|
|
293
|
|
294
|
|
295 void TrackerSampleApp::PrepareScene()
|
|
296 {
|
|
297 // Texture of 2x2 size
|
|
298 {
|
|
299 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false);
|
|
300
|
|
301 uint8_t* p = reinterpret_cast<uint8_t*>(i.GetRow(0));
|
|
302 p[0] = 255;
|
|
303 p[1] = 0;
|
|
304 p[2] = 0;
|
|
305
|
|
306 p[3] = 0;
|
|
307 p[4] = 255;
|
|
308 p[5] = 0;
|
|
309
|
|
310 p = reinterpret_cast<uint8_t*>(i.GetRow(1));
|
|
311 p[0] = 0;
|
|
312 p[1] = 0;
|
|
313 p[2] = 255;
|
|
314
|
|
315 p[3] = 255;
|
|
316 p[4] = 0;
|
|
317 p[5] = 0;
|
|
318
|
|
319 scene_.SetLayer(TEXTURE_2x2_1_ZINDEX, new ColorTextureSceneLayer(i));
|
|
320
|
|
321 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i));
|
|
322 l->SetOrigin(-3, 2);
|
|
323 l->SetPixelSpacing(1.5, 1);
|
|
324 l->SetAngle(20.0 / 180.0 * M_PI);
|
|
325 scene_.SetLayer(TEXTURE_2x2_2_ZINDEX, l.release());
|
|
326 }
|
|
327
|
|
328 // Texture of 1x1 size
|
|
329 {
|
|
330 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false);
|
|
331
|
|
332 uint8_t* p = reinterpret_cast<uint8_t*>(i.GetRow(0));
|
|
333 p[0] = 255;
|
|
334 p[1] = 0;
|
|
335 p[2] = 0;
|
|
336
|
|
337 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i));
|
|
338 l->SetOrigin(-2, 1);
|
|
339 l->SetAngle(20.0 / 180.0 * M_PI);
|
|
340 scene_.SetLayer(TEXTURE_1x1_ZINDEX, l.release());
|
|
341 }
|
|
342
|
|
343 // Some lines
|
|
344 {
|
|
345 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer);
|
|
346
|
|
347 layer->SetThickness(1);
|
|
348
|
|
349 PolylineSceneLayer::Chain chain;
|
|
350 chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5));
|
|
351 chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5));
|
|
352 chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5));
|
|
353 chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5));
|
|
354 layer->AddChain(chain, true);
|
|
355
|
|
356 chain.clear();
|
|
357 chain.push_back(ScenePoint2D(-5, -5));
|
|
358 chain.push_back(ScenePoint2D(5, -5));
|
|
359 chain.push_back(ScenePoint2D(5, 5));
|
|
360 chain.push_back(ScenePoint2D(-5, 5));
|
|
361 layer->AddChain(chain, true);
|
|
362
|
|
363 double dy = 1.01;
|
|
364 chain.clear();
|
|
365 chain.push_back(ScenePoint2D(-4, -4));
|
|
366 chain.push_back(ScenePoint2D(4, -4 + dy));
|
|
367 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy));
|
|
368 chain.push_back(ScenePoint2D(4, 2));
|
|
369 layer->AddChain(chain, false);
|
|
370
|
|
371 layer->SetColor(0, 255, 255);
|
|
372 scene_.SetLayer(LINESET_1_ZINDEX, layer.release());
|
|
373 }
|
|
374
|
|
375 // Some text
|
|
376 {
|
|
377 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer);
|
|
378 layer->SetText("Hello");
|
|
379 scene_.SetLayer(LINESET_2_ZINDEX, layer.release());
|
|
380 }
|
|
381 }
|
|
382
|
|
383
|
|
384 void TrackerSampleApp::DisableTracker()
|
|
385 {
|
|
386 if (activeTracker_)
|
|
387 {
|
|
388 activeTracker_->Cancel();
|
|
389 activeTracker_ = NULL;
|
|
390 }
|
|
391 }
|
|
392
|
|
393 void TrackerSampleApp::TakeScreenshot(const std::string& target,
|
|
394 unsigned int canvasWidth,
|
|
395 unsigned int canvasHeight)
|
|
396 {
|
|
397 // Take a screenshot, then save it as PNG file
|
|
398 CairoCompositor compositor(scene_, canvasWidth, canvasHeight);
|
|
399 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE, Orthanc::Encoding_Latin1);
|
|
400 compositor.Refresh();
|
|
401
|
|
402 Orthanc::ImageAccessor canvas;
|
|
403 compositor.GetCanvas().GetReadOnlyAccessor(canvas);
|
|
404
|
|
405 Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false);
|
|
406 Orthanc::ImageProcessing::Convert(png, canvas);
|
|
407
|
|
408 Orthanc::PngWriter writer;
|
|
409 writer.WriteToFile(target, png);
|
|
410 }
|
|
411
|
|
412
|
|
413 FlexiblePointerTrackerPtr TrackerSampleApp::TrackerHitTest(const PointerEvent & e)
|
|
414 {
|
|
415 // std::vector<MeasureToolPtr> measureTools_;
|
|
416 return nullptr;
|
|
417 }
|
|
418
|
|
419
|
|
420
|
|
421 }
|