diff Framework/Scene2D/Internals/CairoInfoPanelRenderer.cpp @ 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 2d8ab34c8c91
children
line wrap: on
line diff
--- 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);