comparison Samples/Sdl/TrackerSample.cpp @ 632:500c3f70b6c2

- Added a ClearAllChains method to PolylineSceneLayer --> revision must change when calling it ==> BumpRevision has been added to base class - Added some docs = Added GetMinDepth + GetMaxDepth to Scene2D (to alleviate the need for app- specific "Z depth registry" : clients may simply add a new layer on top or at the bottom of the existing layer set. - Added the line tracker measurement tools, commands and trackers. Generic base classes + Line measure - started work on the line measure handles
author Benjamin Golinvaux <bgo@osimis.io>
date Thu, 09 May 2019 10:41:31 +0200
parents
children 104c379f3f1b f939f449482c
comparison
equal deleted inserted replaced
618:0925b27e8750 632:500c3f70b6c2
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 // From Stone
23 #include "../../Applications/Sdl/SdlOpenGLWindow.h"
24 #include "../../Framework/Scene2D/CairoCompositor.h"
25 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h"
26 #include "../../Framework/Scene2D/OpenGLCompositor.h"
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/StoneInitialization.h"
32
33 // From Orthanc framework
34 #include <Core/Logging.h>
35 #include <Core/OrthancException.h>
36 #include <Core/Images/Image.h>
37 #include <Core/Images/ImageProcessing.h>
38 #include <Core/Images/PngWriter.h>
39
40 #include <boost/shared_ptr.hpp>
41 #include <boost/weak_ptr.hpp>
42
43 #include <SDL.h>
44 #include <stdio.h>
45
46
47 // to be moved into Stone
48 #include "../Common/MeasureTrackers.h"
49 #include "../Common/MeasureCommands.h"
50
51 /*
52 TODO:
53
54 - to decouple the trackers from the sample, we need to supply them with
55 the scene rather than the app
56
57 - in order to do that, we need a GetNextFreeZIndex function (or something
58 along those lines) in the scene object
59
60 */
61
62
63 using namespace Orthanc;
64 using namespace OrthancStone;
65
66 namespace OrthancStone
67 {
68 enum GuiTool
69 {
70 GuiTool_Rotate = 0,
71 GuiTool_Pan,
72 GuiTool_Zoom,
73 GuiTool_LineMeasure,
74 GuiTool_CircleMeasure,
75 GuiTool_AngleMeasure,
76 GuiTool_EllipseMeasure,
77 GuiTool_LAST
78 };
79
80 const char* MeasureToolToString(size_t i)
81 {
82 static const char* descs[] = {
83 "GuiTool_Rotate",
84 "GuiTool_Pan",
85 "GuiTool_Zoom",
86 "GuiTool_LineMeasure",
87 "GuiTool_CircleMeasure",
88 "GuiTool_AngleMeasure",
89 "GuiTool_EllipseMeasure",
90 "GuiTool_LAST"
91 };
92 if (i >= GuiTool_LAST)
93 {
94 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Wrong tool index");
95 }
96 return descs[i];
97 }
98 }
99
100 class TrackerSampleApp
101 {
102 public:
103 // 12 because.
104 TrackerSampleApp() : currentTool_(GuiTool_Rotate)
105 {
106 TEXTURE_2x2_1_ZINDEX = 1;
107 TEXTURE_1x1_ZINDEX = 2;
108 TEXTURE_2x2_2_ZINDEX = 3;
109 LINESET_1_ZINDEX = 4;
110 LINESET_2_ZINDEX = 5;
111 INFOTEXT_LAYER_ZINDEX = 6;
112 }
113 void PrepareScene();
114 void Run();
115
116 private:
117 Scene2D& GetScene()
118 {
119 return scene_;
120 }
121
122 void SelectNextTool()
123 {
124 currentTool_ = static_cast<GuiTool>(currentTool_ + 1);
125 if (currentTool_ == GuiTool_LAST)
126 currentTool_ = static_cast<GuiTool>(0);;
127 printf("Current tool is now: %s\n", MeasureToolToString(currentTool_));
128 }
129
130 void HandleApplicationEvent(
131 const OpenGLCompositor& compositor,
132 const SDL_Event& event,
133 std::auto_ptr<IPointerTracker>& activeTracker);
134
135 IPointerTracker* TrackerSampleApp::TrackerHitTest(const PointerEvent& e);
136
137 IPointerTracker* CreateSuitableTracker(
138 const SDL_Event& event,
139 const PointerEvent& e,
140 const OpenGLCompositor& compositor);
141
142 void TakeScreenshot(
143 const std::string& target,
144 unsigned int canvasWidth,
145 unsigned int canvasHeight);
146
147 /**
148 This adds the command at the top of the undo stack
149 */
150 void Commit(TrackerCommandPtr cmd);
151 void Undo();
152 void Redo();
153
154 private:
155 static const unsigned int FONT_SIZE = 32;
156
157 std::vector<TrackerCommandPtr> undoStack_;
158
159 // we store the measure tools here so that they don't get deleted
160 std::vector<MeasureToolPtr> measureTools_;
161
162 //static const int LAYER_POSITION = 150;
163 #if 0
164 int TEXTURE_2x2_1_ZINDEX = 12;
165 int TEXTURE_1x1_ZINDEX = 13;
166 int TEXTURE_2x2_2_ZINDEX = 14;
167 int LINESET_1_ZINDEX = 50;
168 int LINESET_2_ZINDEX = 100;
169 int INFOTEXT_LAYER_ZINDEX = 150;
170 #else
171 int TEXTURE_2x2_1_ZINDEX;
172 int TEXTURE_1x1_ZINDEX;
173 int TEXTURE_2x2_2_ZINDEX;
174 int LINESET_1_ZINDEX;
175 int LINESET_2_ZINDEX;
176 int INFOTEXT_LAYER_ZINDEX;
177 #endif
178 Scene2D scene_;
179 GuiTool currentTool_;
180 };
181
182
183 void TrackerSampleApp::PrepareScene()
184 {
185 // Texture of 2x2 size
186 {
187 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 2, 2, false);
188
189 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0));
190 p[0] = 255;
191 p[1] = 0;
192 p[2] = 0;
193
194 p[3] = 0;
195 p[4] = 255;
196 p[5] = 0;
197
198 p = reinterpret_cast<uint8_t*>(i.GetRow(1));
199 p[0] = 0;
200 p[1] = 0;
201 p[2] = 255;
202
203 p[3] = 255;
204 p[4] = 0;
205 p[5] = 0;
206
207 scene_.SetLayer(TEXTURE_2x2_1_ZINDEX, new ColorTextureSceneLayer(i));
208
209 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i));
210 l->SetOrigin(-3, 2);
211 l->SetPixelSpacing(1.5, 1);
212 l->SetAngle(20.0 / 180.0 * M_PI);
213 scene_.SetLayer(TEXTURE_2x2_2_ZINDEX, l.release());
214 }
215
216 // Texture of 1x1 size
217 {
218 Orthanc::Image i(Orthanc::PixelFormat_RGB24, 1, 1, false);
219
220 uint8_t *p = reinterpret_cast<uint8_t*>(i.GetRow(0));
221 p[0] = 255;
222 p[1] = 0;
223 p[2] = 0;
224
225 std::auto_ptr<ColorTextureSceneLayer> l(new ColorTextureSceneLayer(i));
226 l->SetOrigin(-2, 1);
227 l->SetAngle(20.0 / 180.0 * M_PI);
228 scene_.SetLayer(TEXTURE_1x1_ZINDEX, l.release());
229 }
230
231 // Some lines
232 {
233 std::auto_ptr<PolylineSceneLayer> layer(new PolylineSceneLayer);
234
235 layer->SetThickness(1);
236
237 PolylineSceneLayer::Chain chain;
238 chain.push_back(ScenePoint2D(0 - 0.5, 0 - 0.5));
239 chain.push_back(ScenePoint2D(0 - 0.5, 2 - 0.5));
240 chain.push_back(ScenePoint2D(2 - 0.5, 2 - 0.5));
241 chain.push_back(ScenePoint2D(2 - 0.5, 0 - 0.5));
242 layer->AddChain(chain, true);
243
244 chain.clear();
245 chain.push_back(ScenePoint2D(-5, -5));
246 chain.push_back(ScenePoint2D(5, -5));
247 chain.push_back(ScenePoint2D(5, 5));
248 chain.push_back(ScenePoint2D(-5, 5));
249 layer->AddChain(chain, true);
250
251 double dy = 1.01;
252 chain.clear();
253 chain.push_back(ScenePoint2D(-4, -4));
254 chain.push_back(ScenePoint2D(4, -4 + dy));
255 chain.push_back(ScenePoint2D(-4, -4 + 2.0 * dy));
256 chain.push_back(ScenePoint2D(4, 2));
257 layer->AddChain(chain, false);
258
259 layer->SetColor(0, 255, 255);
260 scene_.SetLayer(LINESET_1_ZINDEX, layer.release());
261 }
262
263 // Some text
264 {
265 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer);
266 layer->SetText("Hello");
267 scene_.SetLayer(LINESET_2_ZINDEX, layer.release());
268 }
269 }
270
271
272 void TrackerSampleApp::TakeScreenshot(const std::string& target,
273 unsigned int canvasWidth,
274 unsigned int canvasHeight)
275 {
276 // Take a screenshot, then save it as PNG file
277 CairoCompositor compositor(scene_, canvasWidth, canvasHeight);
278 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, FONT_SIZE, Orthanc::Encoding_Latin1);
279 compositor.Refresh();
280
281 Orthanc::ImageAccessor canvas;
282 compositor.GetCanvas().GetReadOnlyAccessor(canvas);
283
284 Orthanc::Image png(Orthanc::PixelFormat_RGB24, canvas.GetWidth(), canvas.GetHeight(), false);
285 Orthanc::ImageProcessing::Convert(png, canvas);
286
287 Orthanc::PngWriter writer;
288 writer.WriteToFile(target, png);
289 }
290
291
292 IPointerTracker* TrackerSampleApp::TrackerHitTest(const PointerEvent& e)
293 {
294 // std::vector<MeasureToolPtr> measureTools_;
295 return nullptr;
296 }
297
298 IPointerTracker* TrackerSampleApp::CreateSuitableTracker(
299 const SDL_Event& event,
300 const PointerEvent& e,
301 const OpenGLCompositor& compositor)
302 {
303 switch (event.button.button)
304 {
305 case SDL_BUTTON_MIDDLE:
306 return new PanSceneTracker(scene_, e);
307
308 case SDL_BUTTON_RIGHT:
309 return new ZoomSceneTracker(
310 scene_, e, compositor.GetCanvasHeight());
311
312 case SDL_BUTTON_LEFT:
313 {
314 // TODO: we need to iterate on the set of measuring tool and perform
315 // a hit test to check if a tracker needs to be created for edition.
316 // Otherwise, depending upon the active tool, we might want to create
317 // a "measuring tool creation" tracker
318
319 // TODO: if there are conflicts, we should prefer a tracker that
320 // pertains to the type of measuring tool currently selected (TBD?)
321 IPointerTracker* hitTestTracker = TrackerHitTest(e);
322
323 if (hitTestTracker != NULL)
324 {
325 return hitTestTracker;
326 }
327 else
328 {
329 switch (currentTool_)
330 {
331 case GuiTool_Rotate:
332 return new RotateSceneTracker(scene_, e);
333 case GuiTool_LineMeasure:
334 return new CreateLineMeasureTracker(
335 scene_, undoStack_, measureTools_, e);
336 //case GuiTool_AngleMeasure:
337 // return new AngleMeasureTracker(scene_, measureTools_, undoStack_, e);
338 //case GuiTool_CircleMeasure:
339 // return new CircleMeasureTracker(scene_, measureTools_, undoStack_, e);
340 //case GuiTool_EllipseMeasure:
341 // return new EllipseMeasureTracker(scene_, measureTools_, undoStack_, e);
342 default:
343 throw OrthancException(ErrorCode_InternalError, "Wrong tool!");
344 }
345 }
346 }
347 default:
348 return NULL;
349 }
350 }
351
352 void TrackerSampleApp::HandleApplicationEvent(
353 const OpenGLCompositor& compositor,
354 const SDL_Event& event,
355 std::auto_ptr<IPointerTracker>& activeTracker)
356 {
357 if (event.type == SDL_MOUSEMOTION)
358 {
359 int scancodeCount = 0;
360 const uint8_t* keyboardState = SDL_GetKeyboardState(&scancodeCount);
361
362 if (activeTracker.get() == NULL &&
363 SDL_SCANCODE_LCTRL < scancodeCount &&
364 keyboardState[SDL_SCANCODE_LCTRL])
365 {
366 // The "left-ctrl" key is down, while no tracker is present
367
368 PointerEvent e;
369 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
370
371 ScenePoint2D p = e.GetMainPosition().Apply(scene_.GetCanvasToSceneTransform());
372
373 char buf[64];
374 sprintf(buf, "(%0.02f,%0.02f)", p.GetX(), p.GetY());
375
376 if (scene_.HasLayer(INFOTEXT_LAYER_ZINDEX))
377 {
378 TextSceneLayer& layer =
379 dynamic_cast<TextSceneLayer&>(scene_.GetLayer(INFOTEXT_LAYER_ZINDEX));
380 layer.SetText(buf);
381 layer.SetPosition(p.GetX(), p.GetY());
382 }
383 else
384 {
385 std::auto_ptr<TextSceneLayer> layer(new TextSceneLayer);
386 layer->SetColor(0, 255, 0);
387 layer->SetText(buf);
388 layer->SetBorder(20);
389 layer->SetAnchor(BitmapAnchor_BottomCenter);
390 layer->SetPosition(p.GetX(), p.GetY());
391 scene_.SetLayer(INFOTEXT_LAYER_ZINDEX, layer.release());
392 }
393 }
394 else
395 {
396 scene_.DeleteLayer(INFOTEXT_LAYER_ZINDEX);
397 }
398 }
399 else if (event.type == SDL_MOUSEBUTTONDOWN)
400 {
401 PointerEvent e;
402 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
403
404 activeTracker.reset(CreateSuitableTracker(event, e, compositor));
405 }
406 else if (event.type == SDL_KEYDOWN &&
407 event.key.repeat == 0 /* Ignore key bounce */)
408 {
409 switch (event.key.keysym.sym)
410 {
411 case SDLK_s:
412 scene_.FitContent(compositor.GetCanvasWidth(),
413 compositor.GetCanvasHeight());
414 break;
415
416 case SDLK_c:
417 TakeScreenshot(
418 "screenshot.png",
419 compositor.GetCanvasWidth(),
420 compositor.GetCanvasHeight());
421 break;
422
423 default:
424 break;
425 }
426 }
427 }
428
429
430 static void GLAPIENTRY
431 OpenGLMessageCallback(GLenum source,
432 GLenum type,
433 GLuint id,
434 GLenum severity,
435 GLsizei length,
436 const GLchar* message,
437 const void* userParam)
438 {
439 if (severity != GL_DEBUG_SEVERITY_NOTIFICATION)
440 {
441 fprintf(stderr, "GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
442 (type == GL_DEBUG_TYPE_ERROR ? "** GL ERROR **" : ""),
443 type, severity, message);
444 }
445 }
446
447 bool g_stopApplication = false;
448
449 void TrackerSampleApp::Run()
450 {
451 SdlOpenGLWindow window("Hello", 1024, 768);
452
453 scene_.FitContent(window.GetCanvasWidth(), window.GetCanvasHeight());
454
455 glEnable(GL_DEBUG_OUTPUT);
456 glDebugMessageCallback(OpenGLMessageCallback, 0);
457
458 OpenGLCompositor compositor(window, scene_);
459 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
460 FONT_SIZE, Orthanc::Encoding_Latin1);
461
462 // this will either be empty or contain the current tracker, if any
463 std::auto_ptr<IPointerTracker> tracker;
464
465
466 while (!g_stopApplication)
467 {
468 compositor.Refresh();
469
470 SDL_Event event;
471 while (!g_stopApplication && SDL_PollEvent(&event))
472 {
473 if (event.type == SDL_QUIT)
474 {
475 g_stopApplication = true;
476 break;
477 }
478 else if (event.type == SDL_MOUSEMOTION)
479 {
480 if (tracker.get() != NULL)
481 {
482 PointerEvent e;
483 e.AddPosition(compositor.GetPixelCenterCoordinates(event.button.x, event.button.y));
484 LOG(TRACE) << "event.button.x = " << event.button.x << " " <<
485 "event.button.y = " << event.button.y;
486 tracker->Update(e);
487 }
488 }
489 else if (event.type == SDL_MOUSEBUTTONUP)
490 {
491 if (tracker.get() != NULL)
492 {
493 tracker->Release();
494 tracker.reset(NULL);
495 }
496 }
497 else if (event.type == SDL_WINDOWEVENT &&
498 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
499 {
500 tracker.reset(NULL);
501 compositor.UpdateSize();
502 }
503 else if (event.type == SDL_KEYDOWN &&
504 event.key.repeat == 0 /* Ignore key bounce */)
505 {
506 switch (event.key.keysym.sym)
507 {
508 case SDLK_f:
509 window.GetWindow().ToggleMaximize();
510 break;
511
512 case SDLK_q:
513 g_stopApplication = true;
514 break;
515
516 case SDLK_t:
517 SelectNextTool();
518 break;
519
520 default:
521 break;
522 }
523 }
524 HandleApplicationEvent(compositor, event, tracker);
525 }
526 SDL_Delay(1);
527 }
528 }
529
530
531 /**
532 * IMPORTANT: The full arguments to "main()" are needed for SDL on
533 * Windows. Otherwise, one gets the linking error "undefined reference
534 * to `SDL_main'". https://wiki.libsdl.org/FAQWindows
535 **/
536 int main(int argc, char* argv[])
537 {
538 StoneInitialize();
539 Orthanc::Logging::EnableInfoLevel(true);
540
541 try
542 {
543 TrackerSampleApp app;
544 app.PrepareScene();
545 app.Run();
546 }
547 catch (Orthanc::OrthancException& e)
548 {
549 LOG(ERROR) << "EXCEPTION: " << e.What();
550 }
551
552 StoneFinalize();
553
554 return 0;
555 }