🐛 Store the turn a mirrored shape's transform decomposes into

When sync turns a subtree, it added the head's angle to the rotation of
every shape in it. A shape mirrored relative to the head stores its turn
the other way, so it kept the right transform and corners with a stored
rotation 90 degrees off: 225 for a transform that its vertical flip
decomposes as 315.

Each turned shape now stores its rotation plus the angle, or minus it
when only that one decomposes its transform with its flips. A copy
flipped through a turned nested head now stores 90, the turn its
transform decomposes into, where it stored 270; the test that recorded
270 checks that the oracle still accepts either sign.

Benchmark (scripts/bench-components, ms, lower 2.5% quantile):

| world                              | baseline |     now | ratio |
|------------------------------------+----------+---------+-------|
| deepest-edit-through-three-levels  |   35.215 |  21.321 |  0.61 |
| edit-through-a-chain-of-dependents |   68.931 |  40.817 |  0.59 |
| library-update-into-copies         |   83.083 |  52.024 |  0.63 |
| main-edit-among-many-shapes        |    4.837 |   2.486 |  0.51 |
| main-edit-into-10-copies           |     1.79 |   0.972 |  0.54 |
| main-edit-into-100-copies          |   15.415 |   9.993 |  0.65 |
| main-edit-into-1000-copies         |  153.086 | 102.183 |  0.67 |
| variant-switch-into-copies         |   32.052 |  23.063 |  0.72 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-03 13:10:01 +01:00
parent 7fd46d516b
commit d7ade70b57
3 changed files with 56 additions and 6 deletions

View File

@ -1966,6 +1966,28 @@
(turn shape)
shape))))
(defn- stored-turn
"The rotation that `shape`, just turned by `angle` with its subtree, stores:
`rotation` plus `angle`, or minus it when only that one decomposes its
transform with its flips. A mirror that flips after its turn turns the
other way."
[shape rotation angle]
(let [{:keys [a b c d]} (or (:transform shape) (gmt/matrix))
mirrored (not= (boolean (:flip-x shape)) (boolean (:flip-y shape)))
sign (if (:flip-x shape) -1 1)
turn (mth/degrees (mth/atan2 (* sign b) (* sign a)))
same? #(mth/close? (mod (- %1 %2 -180) 360) 180 0.01)
stores? (fn [value]
(or (same? value turn)
(and mirrored (same? value (- turn)))
(and (:flip-x shape) (:flip-y shape) (same? value (+ turn 180)))))
plus (mod (+ rotation angle) 360)
minus (mod (- rotation angle) 360)]
(if (and (= mirrored (neg? (- (* a d) (* b c))))
(not (stores? plus)) (stores? minus))
minus
plus)))
(defn orient-like
"Turn the subtree of `head` around `center` to match `target`'s matrix.
Compose flip flags with the roots, independent of rotation step size."
@ -1984,12 +2006,13 @@
flip-x? flip-y?)
(fn [shape]
(let [flip-x (cond-> (:flip-x shape) flip-x? not)
flip-y (cond-> (:flip-y shape) flip-y? not)]
(-> shape
(assoc-in [:modifiers :rotation] angle)
(gsh/apply-transform transform)
(dissoc :modifiers)
(assoc :flip-x flip-x :flip-y flip-y)))))))))
flip-y (cond-> (:flip-y shape) flip-y? not)
turned (-> shape
(assoc-in [:modifiers :rotation] angle)
(gsh/apply-transform transform)
(dissoc :modifiers)
(assoc :flip-x flip-x :flip-y flip-y))]
(assoc turned :rotation (stored-turn turned (or (:rotation shape) 0) angle)))))))))
(defn- maybe-sync
[c enabled? done? f]

View File

@ -509,3 +509,22 @@
(t/is (gpt/close? (first (:points (ths/get-shape (tm/file before) label)))
(first (:points copy)))
(str case)))))
(t/deftest a-turn-stores-a-mirrored-childs-rotation-as-its-transform-decomposes
;; a child mirrored across its diagonal, stored as a vertical flip after a
;; 270 degree turn, turned 45 degrees with its unflipped parent: only 225
;; decomposes its new transform with the flip
(let [head (cts/setup-shape {:type :frame :x 0 :y 0 :width 100 :height 100})
target (gsh/transform-shape head (ctm/rotation-modifiers head (gsh/shape->center head) 45))
square (cts/setup-shape {:type :rect :x 25 :y 25 :width 50 :height 50})
mirror (gmt/matrix 0 1 1 0 0 0)
child (assoc square
:transform mirror :transform-inverse (gmt/inverse mirror)
:rotation 270 :flip-y true
:points [(gpt/point 25 25) (gpt/point 25 75) (gpt/point 75 75) (gpt/point 75 25)])
turned ((cll/orient-like head target (gsh/shape->center head)) child)
{:keys [a b c d]} (:transform turned)]
(t/is (mth/close? 225 (:rotation turned) 0.01))
(t/is (every? true? (map #(mth/close? %1 %2 1e-6)
[a b c d]
[(- (/ (mth/sqrt 2) 2)) (/ (mth/sqrt 2) 2) (/ (mth/sqrt 2) 2) (/ (mth/sqrt 2) 2)])))))

View File

@ -1033,3 +1033,11 @@
;; later sync leaves it there
(t/is (map? (cg/run {:world [2 2 {:text true :text-styles true}]
:ops [[:style 8 9 3 1] [:resize 2 0 0 0] [:detach 3 0 0 0]]}))))
(t/deftest generated-turned-main-keeps-a-flipped-copys-rotation-consistent
;; a main turned 45 degrees turns the subtree of a flipped copy; a mirrored
;; child stores the rotation its transform and flip decompose into
(t/is (map? (cg/run {:world [2 2 {:variants true :text-styles true :variant-diff false
:rich-variants true :layout-copies true :text true}]
:ops [[:rotate 3 0 5 0] [:duplicate 0 0 0 0] [:duplicate 0 0 0 0]
[:transform 5 4 0 4] [:rotate 0 0 0 0]]}))))