From 51b060712217f0d041f6ea4801aceea523a37372 Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Tue, 17 Nov 2026 05:32:11 +0100 Subject: [PATCH] :bug: Keep a mirrored shape's rotation composing its transform on a turn Under one flip a shape can store either of two turns for the same transform: the one before its flip, which flips and turns produce, or the opposite one after it, which sync may store when it orients a copy child under a flipped parent. A transform adds its turn to the stored rotation, and a flip negates it, as for the turn before the flip, so a shape storing the other one was left with a rotation its transform does not have: a child turned 45 degrees after its flip and turned again by 45 stored 90 over a plain flip. `apply-transform-generic` now first takes, for a mirrored shape with an unskewed transform, the turn before its flip that the transform gives. Found by checking the shapes of the pinned generated programs for rotations and flips that do not compose their transform (program 332: rotate, flip the parent, rotate it, update main); none is left there but the skew a layout or a scale constraint gives a turned child. Test (geom-shapes-test, fails before the fix): `a-rotation-turns-a-mirror-that-stores-the-turn-after-its-flip`. Benchmark (bench-components, alone): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 21.448 | 23.648 | 0.61 | | edit-through-a-chain-of-dependents | 68.931 | 41.076 | 43.132 | 0.6 | | library-update-into-copies | 83.083 | 54.822 | 56.18 | 0.66 | | main-edit-among-many-shapes | 4.837 | 2.275 | 2.358 | 0.47 | | main-edit-into-10-copies | 1.79 | 1.08 | 1.103 | 0.6 | | main-edit-into-100-copies | 15.415 | 10.411 | 11.35 | 0.68 | | main-edit-into-1000-copies | 153.086 | 110.32 | 118.294 | 0.72 | | variant-switch-into-copies | 32.052 | 21.161 | 23.07 | 0.66 | AI-assisted-by: claude-opus-5-5 --- .../app/common/geom/shapes/transforms.cljc | 28 ++++++++++++++++++- .../test/common_tests/geom_shapes_test.cljc | 20 +++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index 9e7a0296c8..ef2e037d58 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -377,11 +377,37 @@ (assoc :points points)))) +(defn- turn-before-flips + "`shape` with the rotation that turns it before its flips. Under one flip + a shape may store the turn it takes after them, the opposite one; a + transform adds its turn to, and a flip negates, the turn before them." + [shape] + (let [transform (:transform shape)] + (if (and (some? transform) + (not= (boolean (:flip-x shape)) (boolean (:flip-y shape)))) + (let [{:keys [a b c d]} transform + sign (if (:flip-x shape) -1 1) + turn (mth/degrees (mth/atan2 (* sign b) (* sign a))) + rotation (d/nilv (:rotation shape) 0) + same? #(mth/close? (mod (- %1 %2 -180) 360) 180 0.01) + ;; the axes of a skewed transform decompose into no turn + square? (mth/close? (/ (+ (* a c) (* b d)) + (max (* (mth/hypot a b) (mth/hypot c d)) 1e-12)) + 0 1e-6)] + (if (and square? + (neg? (- (* a d) (* b c))) + (not (same? rotation turn)) + (same? rotation (- turn))) + (assoc shape :rotation (mod turn 360)) + shape)) + shape))) + (defn- apply-transform-generic "Given a new set of points transformed, set up the rectangle so it keeps its properties. We adjust de x,y,width,height and create a custom transform" [shape transform-mtx turn] - (let [points (-> (dm/get-prop shape :points) + (let [shape (turn-before-flips shape) + points (-> (dm/get-prop shape :points) (gco/transform-points transform-mtx)) shape (adjust-shape-flips shape points diff --git a/common/test/common_tests/geom_shapes_test.cljc b/common/test/common_tests/geom_shapes_test.cljc index 5c47ce5c4d..39a255cf10 100644 --- a/common/test/common_tests/geom_shapes_test.cljc +++ b/common/test/common_tests/geom_shapes_test.cljc @@ -339,3 +339,23 @@ (let [halved (mirror (rotate shape 30) (gpt/point -1 -1))] (t/is (and (:flip-x halved) (:flip-y halved))) (t/is (composes? halved))))) + +(t/deftest a-rotation-turns-a-mirror-that-stores-the-turn-after-its-flip + ;; a mirrored shape may store the turn it takes after its flip, the + ;; opposite of the one before it; a turn keeps its rotation and flips + ;; composing its transform, before the flips + (doseq [angle [30 45 90]] + (let [base (create-test-shape :rect {:width 120 :height 60}) + ;; turned 45 degrees after a horizontal flip, storing 45 + mirrored (gmt/multiply (gmt/scale-matrix (gpt/point -1 1)) (gmt/rotate-matrix 45)) + center (gsh/shape->center base) + shape (-> base + (assoc :flip-x true :rotation 45 + :transform mirrored :transform-inverse (gmt/inverse mirrored)) + (assoc :points (mapv #(gpt/transform % (gmt/transform-in center mirrored)) + (grc/rect->points (:selrect base))))) + after (gsh/transform-shape shape (ctm/rotation-modifiers shape (gsh/shape->center shape) angle)) + scale (gpt/point (if (:flip-x after) -1 1) (if (:flip-y after) -1 1)) + implied (gmt/multiply (gmt/rotate-matrix (:rotation after)) (gmt/scale-matrix scale))] + (t/is (every? #(mth/close? (get implied %) (get (:transform after) %) 1e-9) [:a :b :c :d]) + (pr-str angle (:rotation after) (:transform after))))))