changeset 1325:be17fed8c7c5 broker

Added flag in InfoPanelSceneLayer to apply the scene rotation to the displayed image.
author Benjamin Golinvaux <bgo@osimis.io>
date Tue, 24 Mar 2020 20:34:28 +0100
parents 4d8d642f7036
children 55166e57a77c
files Framework/Scene2D/InfoPanelSceneLayer.cpp Framework/Scene2D/InfoPanelSceneLayer.h Framework/Scene2D/Internals/CairoInfoPanelRenderer.cpp Framework/Scene2D/Internals/CairoInfoPanelRenderer.h Framework/Scene2D/Internals/OpenGLInfoPanelRenderer.cpp Framework/Scene2D/Internals/OpenGLInfoPanelRenderer.h
diffstat 6 files changed, 144 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Scene2D/InfoPanelSceneLayer.cpp	Tue Mar 24 16:24:26 2020 +0100
+++ b/Framework/Scene2D/InfoPanelSceneLayer.cpp	Tue Mar 24 20:34:28 2020 +0100
@@ -28,10 +28,12 @@
 {
   InfoPanelSceneLayer::InfoPanelSceneLayer(const Orthanc::ImageAccessor& texture,
                                            BitmapAnchor anchor,
-                                           bool isLinearInterpolation) :
+                                           bool isLinearInterpolation,
+                                           bool applySceneRotation) :
     texture_(Orthanc::Image::Clone(texture)),
     anchor_(anchor),
-    isLinearInterpolation_(isLinearInterpolation)
+    isLinearInterpolation_(isLinearInterpolation),
+    applySceneRotation_(applySceneRotation)
   {
     if (texture_->GetFormat() != Orthanc::PixelFormat_RGBA32 &&
         texture_->GetFormat() != Orthanc::PixelFormat_RGB24)
--- a/Framework/Scene2D/InfoPanelSceneLayer.h	Tue Mar 24 16:24:26 2020 +0100
+++ b/Framework/Scene2D/InfoPanelSceneLayer.h	Tue Mar 24 20:34:28 2020 +0100
@@ -37,15 +37,26 @@
     std::unique_ptr<Orthanc::ImageAccessor>  texture_;
     BitmapAnchor                           anchor_;
     bool                                   isLinearInterpolation_;
+    bool                                   applySceneRotation_;
 
   public:
+  /**
+   * If you supply `true` for `applySceneRotation`, then, in addition to 
+   * a translation to bring it at the desired anchoring location, the image
+   * will be rotated around its center by the same rotation as the scene
+   * transformation.
+   */
     InfoPanelSceneLayer(const Orthanc::ImageAccessor& texture,
                         BitmapAnchor anchor,
-                        bool isLinearInterpolation);
+                        bool isLinearInterpolation,
+                        bool applySceneRotation = false);
 
     virtual ISceneLayer* Clone() const
     {
-      return new InfoPanelSceneLayer(*texture_, anchor_, isLinearInterpolation_);
+      return new InfoPanelSceneLayer(*texture_, 
+                                     anchor_, 
+                                     isLinearInterpolation_, 
+                                     applySceneRotation_);
     }
 
     const Orthanc::ImageAccessor& GetTexture() const
@@ -58,6 +69,11 @@
       return anchor_;
     }
 
+    bool ShouldApplySceneRotation() const
+    {
+      return applySceneRotation_;
+    }
+
     bool IsLinearInterpolation() const
     {
       return isLinearInterpolation_;
--- a/Framework/Scene2D/Internals/CairoInfoPanelRenderer.cpp	Tue Mar 24 16:24:26 2020 +0100
+++ b/Framework/Scene2D/Internals/CairoInfoPanelRenderer.cpp	Tue Mar 24 20:34:28 2020 +0100
@@ -34,9 +34,9 @@
       texture_.Copy(l.GetTexture(), true);
       anchor_ = l.GetAnchor();
       isLinearInterpolation_ = l.IsLinearInterpolation();
+      applySceneRotation_ = l.ShouldApplySceneRotation();
     }
 
-    
     void CairoInfoPanelRenderer::Render(const AffineTransform2D& transform,
                                         unsigned int canvasWidth,
                                         unsigned int canvasHeight)
@@ -47,14 +47,71 @@
         canvasWidth, canvasHeight);
 
       cairo_t* cr = target_.GetCairoContext();
-
       cairo_save(cr);
 
-      cairo_matrix_t t;
-      cairo_matrix_init_identity(&t);
-      cairo_matrix_translate(&t, dx, dy);
-      cairo_transform(cr, &t);
+      if (applySceneRotation_)
+      {
+        // the transformation is as follows:
+        // - originally, the image is aligned so that its top left corner
+        // is at 0,0
+        // - first, we translate the image by -w/2,-h/2 
+        // - then we rotate it, so that the next rotation will make the  
+        //   image rotate around its center.
+        // - then, we translate the image by +w/2,+h/2 to put it
+        //   back in place
+        // - the fourth and last transform is the one that brings the 
+        //   image to its desired anchored location.
+
+        int32_t halfWidth =
+          static_cast<int32_t>(0.5 * texture_.GetWidth());
+
+        int32_t halfHeight =
+          static_cast<int32_t>(0.5 * texture_.GetHeight());
+
+        AffineTransform2D translation1 =
+          AffineTransform2D::CreateOffset(-halfWidth, -halfHeight);
+
+        const Matrix& sceneTransformM = transform.GetHomogeneousMatrix();
+        Matrix r;
+        Matrix q;
+        LinearAlgebra::RQDecomposition3x3(r, q, sceneTransformM);
+
+        // first, put the scene rotation in a cairo matrix
+        cairo_matrix_t m;
+        cairo_matrix_init(
+          &m, q(0, 0), q(1, 0), q(0, 1), q(1, 1), q(0, 2), q(1, 2));
 
+        // now let's build the transform piece by piece
+        // first translation (directly written in `transform`)
+        cairo_matrix_t transform;
+        cairo_matrix_init_identity(&transform);
+        cairo_matrix_translate(&transform, -halfWidth, -halfHeight);
+
+        // then the rotation
+        cairo_matrix_multiply(&transform, &transform, &m);
+
+        // then the second translation
+        {
+          cairo_matrix_t translation2;
+          cairo_matrix_init_translate(&translation2, halfWidth, halfHeight);
+          cairo_matrix_multiply(&transform, &transform, &m);
+        }
+
+        // then the last translation
+        {
+          cairo_matrix_t translation3;
+          cairo_matrix_init_translate(&translation3, dx, dy);
+          cairo_matrix_multiply(&transform, &transform, &translation3);
+        }
+        cairo_transform(cr, &transform);
+      } 
+      else
+      {
+        cairo_matrix_t t;
+        cairo_matrix_init_identity(&t);
+        cairo_matrix_translate(&t, dx, dy);
+        cairo_transform(cr, &t);
+      }
       cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
       cairo_set_source_surface(cr, texture_.GetObject(), 0, 0);
 
--- a/Framework/Scene2D/Internals/CairoInfoPanelRenderer.h	Tue Mar 24 16:24:26 2020 +0100
+++ b/Framework/Scene2D/Internals/CairoInfoPanelRenderer.h	Tue Mar 24 20:34:28 2020 +0100
@@ -36,11 +36,15 @@
       CairoSurface           texture_;
       BitmapAnchor           anchor_;
       bool                   isLinearInterpolation_;
+      bool                   applySceneRotation_;
 
     public:
       CairoInfoPanelRenderer(ICairoContextProvider& target,
                              const ISceneLayer& layer) :
-        target_(target)
+        target_(target),
+        anchor_(BitmapAnchor_TopLeft),
+        applySceneRotation_(false)
+
       {
         Update(layer);
       }
--- a/Framework/Scene2D/Internals/OpenGLInfoPanelRenderer.cpp	Tue Mar 24 16:24:26 2020 +0100
+++ b/Framework/Scene2D/Internals/OpenGLInfoPanelRenderer.cpp	Tue Mar 24 20:34:28 2020 +0100
@@ -32,6 +32,7 @@
         context_.MakeCurrent();
         texture_.reset(new OpenGL::OpenGLTexture(context_));
         texture_->Load(layer.GetTexture(), layer.IsLinearInterpolation());
+        applySceneRotation_ = layer.ShouldApplySceneRotation();
         anchor_ = layer.GetAnchor();
       }
     }
@@ -41,11 +42,11 @@
                                                      const InfoPanelSceneLayer& layer) :
       context_(context),
       program_(program),
-      anchor_(BitmapAnchor_TopLeft)
+      anchor_(BitmapAnchor_TopLeft),
+      applySceneRotation_(false)
     {
       LoadTexture(layer);
     }
-
     
     void OpenGLInfoPanelRenderer::Render(const AffineTransform2D& transform,
                                          unsigned int canvasWidth,
@@ -60,7 +61,56 @@
 
         // The position of this type of layer is layer: Ignore the
         // "transform" coming from the scene
-        program_.Apply(*texture_, AffineTransform2D::CreateOffset(dx, dy), true);
+        AffineTransform2D actualTransform = 
+          AffineTransform2D::CreateOffset(dx, dy);
+
+        if (applySceneRotation_)
+        {
+          // the transformation is as follows:
+          // - originally, the image is aligned so that its top left corner
+          // is at 0,0
+          // - first, we translate the image by -w/2,-h/2 
+          // - then we rotate it, so that the next rotation will make the  
+          //   image rotate around its center.
+          // - then, we translate the image by +w/2,+h/2 to put it
+          //   back in place
+          // - the fourth and last transform is the one that brings the 
+          //   image to its desired anchored location.
+
+          int32_t halfWidth = 
+            static_cast<int32_t>(0.5 * texture_->GetWidth());
+
+          int32_t halfHeight=
+            static_cast<int32_t>(0.5 * texture_->GetHeight());
+
+          AffineTransform2D translation1 =
+            AffineTransform2D::CreateOffset(-halfWidth, -halfHeight);
+
+          const Matrix& sceneTransformM = transform.GetHomogeneousMatrix();
+          Matrix r;
+          Matrix q;
+          LinearAlgebra::RQDecomposition3x3(r, q, sceneTransformM);
+
+          // counterintuitively, q is the rotation and r is the upper
+          // triangular
+          AffineTransform2D rotation(q);
+
+          AffineTransform2D translation2 =
+            AffineTransform2D::CreateOffset(halfWidth, halfHeight);
+
+          // please note that the last argument is the 1st applied 
+          // transformation (rationale: if arguments are a, b and c, then
+          // the resulting matrix is a*b*c:
+          // x2 = (a*b*c)*x1 = (a*(b*(c*x1))) (you can see that the result
+          // of c*x1 is transformed by b, and the result of b*c*x1 is trans-
+          // formed by a)
+          actualTransform = AffineTransform2D::Combine(actualTransform,
+                                                       translation2,
+                                                       rotation,
+                                                       translation1);
+        }
+
+        program_.Apply(*texture_, actualTransform, true);
       }
     }
   }
--- a/Framework/Scene2D/Internals/OpenGLInfoPanelRenderer.h	Tue Mar 24 16:24:26 2020 +0100
+++ b/Framework/Scene2D/Internals/OpenGLInfoPanelRenderer.h	Tue Mar 24 20:34:28 2020 +0100
@@ -36,6 +36,7 @@
       OpenGLColorTextureProgram&            program_;
       std::unique_ptr<OpenGL::OpenGLTexture>  texture_;
       BitmapAnchor                          anchor_;
+      bool                                  applySceneRotation_;
 
       void LoadTexture(const InfoPanelSceneLayer& layer);