comparison Samples/Sdl/TrackerSample.cpp @ 644:f939f449482c

Ongoing tracker work.
author Benjamin Golinvaux <bgo@osimis.io>
date Fri, 10 May 2019 16:15:55 +0200
parents 500c3f70b6c2
children 1e9ed656318e
comparison
equal deleted inserted replaced
634:6a144a45b2d8 644:f939f449482c
16 * 16 *
17 * You should have received a copy of the GNU Affero General Public License 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/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 #include "TrackerSampleApp.h"
21 22
22 // From Stone 23 // From Stone
23 #include "../../Applications/Sdl/SdlOpenGLWindow.h" 24 #include "../../Applications/Sdl/SdlOpenGLWindow.h"
24 #include "../../Framework/Scene2D/CairoCompositor.h" 25 #include "../../Framework/Scene2D/CairoCompositor.h"
25 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h" 26 #include "../../Framework/Scene2D/ColorTextureSceneLayer.h"
26 #include "../../Framework/Scene2D/OpenGLCompositor.h" 27 #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" 28 #include "../../Framework/StoneInitialization.h"
32 29
33 // From Orthanc framework
34 #include <Core/Logging.h> 30 #include <Core/Logging.h>
35 #include <Core/OrthancException.h> 31 #include <Core/OrthancException.h>
36 #include <Core/Images/Image.h> 32
37 #include <Core/Images/ImageProcessing.h>
38 #include <Core/Images/PngWriter.h>
39 33
40 #include <boost/shared_ptr.hpp> 34 #include <boost/shared_ptr.hpp>
41 #include <boost/weak_ptr.hpp> 35 #include <boost/weak_ptr.hpp>
42 36
43 #include <SDL.h> 37 #include <SDL.h>
44 #include <stdio.h> 38 #include <stdio.h>
45
46
47 // to be moved into Stone
48 #include "../Common/MeasureTrackers.h"
49 #include "../Common/MeasureCommands.h"
50 39
51 /* 40 /*
52 TODO: 41 TODO:
53 42
54 - to decouple the trackers from the sample, we need to supply them with 43 - to decouple the trackers from the sample, we need to supply them with
60 */ 49 */
61 50
62 51
63 using namespace Orthanc; 52 using namespace Orthanc;
64 using namespace OrthancStone; 53 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 54
429 55
430 static void GLAPIENTRY 56 static void GLAPIENTRY
431 OpenGLMessageCallback(GLenum source, 57 OpenGLMessageCallback(GLenum source,
432 GLenum type, 58 GLenum type,
444 } 70 }
445 } 71 }
446 72
447 bool g_stopApplication = false; 73 bool g_stopApplication = false;
448 74
449 void TrackerSampleApp::Run() 75 void Run(TrackerSampleApp* app)
450 { 76 {
451 SdlOpenGLWindow window("Hello", 1024, 768); 77 SdlOpenGLWindow window("Hello", 1024, 768);
452 78
453 scene_.FitContent(window.GetCanvasWidth(), window.GetCanvasHeight()); 79 app->GetScene().FitContent(window.GetCanvasWidth(), window.GetCanvasHeight());
454 80
455 glEnable(GL_DEBUG_OUTPUT); 81 glEnable(GL_DEBUG_OUTPUT);
456 glDebugMessageCallback(OpenGLMessageCallback, 0); 82 glDebugMessageCallback(OpenGLMessageCallback, 0);
457 83
458 OpenGLCompositor compositor(window, scene_); 84 OpenGLCompositor compositor(window, app->GetScene());
459 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT, 85 compositor.SetFont(0, Orthanc::EmbeddedResources::UBUNTU_FONT,
460 FONT_SIZE, Orthanc::Encoding_Latin1); 86 FONT_SIZE, Orthanc::Encoding_Latin1);
461 87
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) 88 while (!g_stopApplication)
467 { 89 {
468 compositor.Refresh(); 90 compositor.Refresh();
469 91
470 SDL_Event event; 92 SDL_Event event;
473 if (event.type == SDL_QUIT) 95 if (event.type == SDL_QUIT)
474 { 96 {
475 g_stopApplication = true; 97 g_stopApplication = true;
476 break; 98 break;
477 } 99 }
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 && 100 else if (event.type == SDL_WINDOWEVENT &&
498 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) 101 event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
499 { 102 {
500 tracker.reset(NULL); 103 app->DisableTracker(); // was: tracker.reset(NULL);
501 compositor.UpdateSize(); 104 compositor.UpdateSize();
502 } 105 }
503 else if (event.type == SDL_KEYDOWN && 106 else if (event.type == SDL_KEYDOWN &&
504 event.key.repeat == 0 /* Ignore key bounce */) 107 event.key.repeat == 0 /* Ignore key bounce */)
505 { 108 {
510 break; 113 break;
511 114
512 case SDLK_q: 115 case SDLK_q:
513 g_stopApplication = true; 116 g_stopApplication = true;
514 break; 117 break;
515
516 case SDLK_t:
517 SelectNextTool();
518 break;
519
520 default: 118 default:
521 break; 119 break;
522 } 120 }
523 } 121 }
524 HandleApplicationEvent(compositor, event, tracker); 122 app->HandleApplicationEvent(compositor, event);
525 } 123 }
526 SDL_Delay(1); 124 SDL_Delay(1);
527 } 125 }
528 } 126 }
529 127
535 **/ 133 **/
536 int main(int argc, char* argv[]) 134 int main(int argc, char* argv[])
537 { 135 {
538 StoneInitialize(); 136 StoneInitialize();
539 Orthanc::Logging::EnableInfoLevel(true); 137 Orthanc::Logging::EnableInfoLevel(true);
138 Orthanc::Logging::EnableTraceLevel(true);
139
540 140
541 try 141 try
542 { 142 {
543 TrackerSampleApp app; 143 TrackerSampleApp app;
544 app.PrepareScene(); 144 app.PrepareScene();
545 app.Run(); 145 Run(&app);
546 } 146 }
547 catch (Orthanc::OrthancException& e) 147 catch (Orthanc::OrthancException& e)
548 { 148 {
549 LOG(ERROR) << "EXCEPTION: " << e.What(); 149 LOG(ERROR) << "EXCEPTION: " << e.What();
550 } 150 }