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))))))