changeset 660:cb3b76d16234

Added info display map to the app + global logging function + fixed bisecting algo to display angle measure text label
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 14 May 2019 19:38:51 +0200
parents 68b5688241d5
children 970949ff868a 8b6adfb62a2f
files Samples/Common/AngleMeasureTool.cpp Samples/Common/MeasureTools.h Samples/Sdl/TrackerSample.cpp Samples/Sdl/TrackerSampleApp.cpp Samples/Sdl/TrackerSampleApp.h
diffstat 5 files changed, 116 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/Samples/Common/AngleMeasureTool.cpp	Tue May 14 19:38:15 2019 +0200
+++ b/Samples/Common/AngleMeasureTool.cpp	Tue May 14 19:38:51 2019 +0200
@@ -25,6 +25,8 @@
 
 #include <boost/math/constants/constants.hpp>
 
+extern void TrackerSample_SetInfoDisplayMessage(std::string key, std::string value);
+
 namespace OrthancStone
 {
   AngleMeasureTool::~AngleMeasureTool()
@@ -159,7 +161,7 @@
         {
           PolylineSceneLayer::Chain chain;
 
-          const double ARC_RADIUS_CANVAS_COORD = 20.0;
+          const double ARC_RADIUS_CANVAS_COORD = 30.0;
           AddShortestArc(chain, GetScene(), side1End_, center_, side2End_, 
             ARC_RADIUS_CANVAS_COORD*pixelToScene);
           polylineLayer->AddChain(chain, false);
@@ -171,23 +173,70 @@
         double p1cAngle = atan2(
           side1End_.GetY() - center_.GetY(),
           side1End_.GetX() - center_.GetX());
+
+        TrackerSample_SetInfoDisplayMessage("center_.GetX()",
+          boost::lexical_cast<std::string>(center_.GetX()));
+
+        TrackerSample_SetInfoDisplayMessage("center_.GetY()",
+          boost::lexical_cast<std::string>(center_.GetY()));
+
+        TrackerSample_SetInfoDisplayMessage("side1End_.GetX()",
+          boost::lexical_cast<std::string>(side1End_.GetX()));
+
+        TrackerSample_SetInfoDisplayMessage("side1End_.GetY()",
+          boost::lexical_cast<std::string>(side1End_.GetY()));
+
+        TrackerSample_SetInfoDisplayMessage("side2End_.GetX()",
+          boost::lexical_cast<std::string>(side2End_.GetX()));
+
+        TrackerSample_SetInfoDisplayMessage("side2End_.GetY()",
+          boost::lexical_cast<std::string>(side2End_.GetY()));
+
+        TrackerSample_SetInfoDisplayMessage("p1cAngle (deg)",
+          boost::lexical_cast<std::string>(RadiansToDegrees(p1cAngle)));
+
         double p2cAngle = atan2(
           side2End_.GetY() - center_.GetY(),
           side2End_.GetX() - center_.GetX());
+
         double delta = NormalizeAngle(p2cAngle - p1cAngle);
-        double theta = delta/2;
+        TrackerSample_SetInfoDisplayMessage("delta (deg)",
+          boost::lexical_cast<std::string>(RadiansToDegrees(delta)));
+
+        double theta = p1cAngle + delta/2;
 
-        const double TEXT_CENTER_DISTANCE_CANVAS_COORD = 40.0;
+        TrackerSample_SetInfoDisplayMessage("theta (deg)",
+          boost::lexical_cast<std::string>(RadiansToDegrees(theta)));
+
+        TrackerSample_SetInfoDisplayMessage("p2cAngle (deg)",
+          boost::lexical_cast<std::string>(RadiansToDegrees(p2cAngle)));
+
+        const double TEXT_CENTER_DISTANCE_CANVAS_COORD = 90;
 
         double offsetX = TEXT_CENTER_DISTANCE_CANVAS_COORD * cos(theta);
+        TrackerSample_SetInfoDisplayMessage("offsetX (pix)",
+          boost::lexical_cast<std::string>(offsetX));
+
         double offsetY = TEXT_CENTER_DISTANCE_CANVAS_COORD * sin(theta);
-        double pointX = center_.GetX() + offsetX;
-        double pointY = center_.GetY() + offsetY;
+        TrackerSample_SetInfoDisplayMessage("offsetY (pix)",
+          boost::lexical_cast<std::string>(offsetY));
+
+        double pointX = center_.GetX() + offsetX * pixelToScene;
+        double pointY = center_.GetY() + offsetY * pixelToScene;
+        TrackerSample_SetInfoDisplayMessage("pointX",
+          boost::lexical_cast<std::string>(pointX));
+
+        TrackerSample_SetInfoDisplayMessage("pointY",
+          boost::lexical_cast<std::string>(pointY));
 
         TextSceneLayer* textLayer = GetTextLayer();
 
         char buf[64];
         double angleDeg = RadiansToDegrees(delta);
+
+        TrackerSample_SetInfoDisplayMessage("angleDeg",
+          boost::lexical_cast<std::string>(angleDeg));
+
         sprintf(buf, "%0.02f deg", angleDeg);
         textLayer->SetText(buf);
         textLayer->SetColor(0, 223, 21);
--- a/Samples/Common/MeasureTools.h	Tue May 14 19:38:15 2019 +0200
+++ b/Samples/Common/MeasureTools.h	Tue May 14 19:38:51 2019 +0200
@@ -85,3 +85,5 @@
 }
 
 
+extern void TrackerSample_SetInfoDisplayMessage(
+  std::string key, std::string value);
--- a/Samples/Sdl/TrackerSample.cpp	Tue May 14 19:38:15 2019 +0200
+++ b/Samples/Sdl/TrackerSample.cpp	Tue May 14 19:38:51 2019 +0200
@@ -55,7 +55,16 @@
 
 
 
+boost::weak_ptr<TrackerSampleApp> g_app;
 
+void TrackerSample_SetInfoDisplayMessage(std::string key, std::string value)
+{
+  boost::shared_ptr<TrackerSampleApp> app = g_app.lock();
+  if (app)
+  {
+    app->SetInfoDisplayMessage(key, value);
+  }
+}
 
 /**
  * IMPORTANT: The full arguments to "main()" are needed for SDL on
@@ -68,13 +77,13 @@
   Orthanc::Logging::EnableInfoLevel(true);
   Orthanc::Logging::EnableTraceLevel(true);
 
-
   try
   {
     MessageBroker broker;
-    TrackerSampleApp app(broker);
-    app.PrepareScene();
-    app.Run();
+    boost::shared_ptr<TrackerSampleApp> app(new TrackerSampleApp(broker));
+    g_app = app;
+    app->PrepareScene();
+    app->Run();
   }
   catch (Orthanc::OrthancException& e)
   {
@@ -85,3 +94,5 @@
 
   return 0;
 }
+
+
--- a/Samples/Sdl/TrackerSampleApp.cpp	Tue May 14 19:38:15 2019 +0200
+++ b/Samples/Sdl/TrackerSampleApp.cpp	Tue May 14 19:38:51 2019 +0200
@@ -82,8 +82,16 @@
 
   void TrackerSampleApp::DisplayInfoText()
   {
-    char buf[256];
-    sprintf(buf, "INFO TEXT\n*** INFO TEXT ***\ntoto tata tutu titi\nprrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrout");
+    // do not try to use stuff too early!
+    if (compositor_.get() == NULL)
+      return;
+
+    std::stringstream msg;
+    for (auto kv : infoTextMap_)
+    {
+      msg << kv.first << " : " << kv.second << std::endl;
+    }
+    auto msgS = msg.str();
 
     TextSceneLayer* layerP = NULL;
     if (scene_.HasLayer(FIXED_INFOTEXT_LAYER_ZINDEX))
@@ -98,14 +106,13 @@
       layerP = layer.get();
       layer->SetColor(0, 255, 0);
       layer->SetFontIndex(1);
-      layer->SetText(buf);
       layer->SetBorder(20);
       layer->SetAnchor(BitmapAnchor_TopLeft);
       //layer->SetPosition(0,0);
       scene_.SetLayer(FIXED_INFOTEXT_LAYER_ZINDEX, layer.release());
     }
     // position the fixed info text in the upper right corner
-    layerP->SetText(buf);
+    layerP->SetText(msgS.c_str());
     double cX = compositor_->GetCanvasWidth() * (-0.5);
     double cY = compositor_->GetCanvasHeight() * (-0.5);
     scene_.GetCanvasToSceneTransform().Apply(cX,cY);
@@ -262,9 +269,7 @@
 
   void TrackerSampleApp::OnSceneTransformChanged(const Scene2D::SceneTransformChanged& message)
   {
-    // do not try to call this too early!
-    if(compositor_.get() != NULL)
-      DisplayInfoText();
+    DisplayInfoText();
   }
 
   FlexiblePointerTrackerPtr TrackerSampleApp::CreateSuitableTracker(
@@ -342,6 +347,25 @@
   }
 
 
+  TrackerSampleApp::TrackerSampleApp(MessageBroker& broker) : IObserver(broker)
+    , currentTool_(GuiTool_Rotate)
+    , scene_(broker)
+  {
+    scene_.RegisterObserverCallback(
+      new Callable<TrackerSampleApp, Scene2D::SceneTransformChanged>
+      (*this, &TrackerSampleApp::OnSceneTransformChanged));
+
+    TEXTURE_2x2_1_ZINDEX = 1;
+    TEXTURE_1x1_ZINDEX = 2;
+    TEXTURE_2x2_2_ZINDEX = 3;
+    LINESET_1_ZINDEX = 4;
+    LINESET_2_ZINDEX = 5;
+    FLOATING_INFOTEXT_LAYER_ZINDEX = 6;
+    FIXED_INFOTEXT_LAYER_ZINDEX = 7;
+
+
+  }
+
   void TrackerSampleApp::PrepareScene()
   {
     // Texture of 2x2 size
@@ -543,6 +567,14 @@
     compositor_.reset(NULL);
   }
 
-
+  void TrackerSampleApp::SetInfoDisplayMessage(
+    std::string key, std::string value)
+  {
+    if (value == "")
+      infoTextMap_.erase(key);
+    else
+      infoTextMap_[key] = value;
+    DisplayInfoText();
+  }
 
 }
--- a/Samples/Sdl/TrackerSampleApp.h	Tue May 14 19:38:15 2019 +0200
+++ b/Samples/Sdl/TrackerSampleApp.h	Tue May 14 19:38:51 2019 +0200
@@ -28,6 +28,7 @@
 
 #include <boost/make_shared.hpp>
 #include <boost/shared_ptr.hpp>
+#include <boost/enable_shared_from_this.hpp>
 
 namespace OrthancStone
 {
@@ -54,29 +55,14 @@
   class Scene2D;
 
   class TrackerSampleApp : public IObserver
+    , public boost::enable_shared_from_this<TrackerSampleApp>
   {
   public:
     // 12 because.
-    TrackerSampleApp(MessageBroker& broker)
-      : IObserver(broker)
-      , currentTool_(GuiTool_Rotate)
-      , scene_(broker)
-    {
-      scene_.RegisterObserverCallback(
-        new Callable<TrackerSampleApp, Scene2D::SceneTransformChanged>
-        (*this, &TrackerSampleApp::OnSceneTransformChanged));
-
-      TEXTURE_2x2_1_ZINDEX = 1;
-      TEXTURE_1x1_ZINDEX = 2;
-      TEXTURE_2x2_2_ZINDEX = 3;
-      LINESET_1_ZINDEX = 4;
-      LINESET_2_ZINDEX = 5;
-      FLOATING_INFOTEXT_LAYER_ZINDEX = 6;
-      FIXED_INFOTEXT_LAYER_ZINDEX = 7;
-    }
+    TrackerSampleApp(MessageBroker& broker);
     void PrepareScene();
     void Run();
-
+    void SetInfoDisplayMessage(std::string key, std::string value);
     void DisableTracker();
 
     Scene2D& GetScene();
@@ -124,6 +110,7 @@
     */
     Scene2D scene_;
 
+    std::map<std::string, std::string> infoTextMap_;
     FlexiblePointerTrackerPtr activeTracker_;
     std::vector<TrackerCommandPtr> undoStack_;