🐛 Keep authored root orientation as a switch override

A switch places the new root in the old orientation before carrying
attributes. When rotation is already equal, the carry step emits no
write and drops its geometry group. Preserve that group for retained
authored root orientation so later sync keeps the visible result.

Extend the rotation regression to check touched and later sync: two
assertions fail before the fix. Focused checks pass 43 tests and 166
assertions, including the two previously shrunk generated regressions.

Benchmark vs Phase 1e: main 10/100/1000 0.64/0.65/0.97; levels 0.49;
chain 0.59; library 0.58; many shapes 0.89; switch 0.60. No flags.

AI-assisted-by: GPT-6
This commit is contained in:
alonso.torres 2026-10-23 19:44:53 +02:00
parent 66c254fbc6
commit 13905afc60
2 changed files with 35 additions and 27 deletions

View File

@ -361,6 +361,31 @@
(let [new-touched (ctf/get-touched-from-ref-chain-until-target-ref container libraries shape nil)]
(assoc shape :touched (disj new-touched :swap-group)))))
(defn- keep-orientation-override
"Retained orientation that differs from the new component's expected
placement needs a geometry override to survive later sync."
[changes shape baseline page ldata component-id]
(let [parent (get-in page [:objects (:parent-id shape)])
main (ctf/get-component-root ldata (ctkl/get-component ldata component-id))
main-parent (get-in (ctf/get-component-container ldata (ctkl/get-component ldata component-id))
[:objects (:parent-id main)])
parent-delta (if (:shape-ref parent) 0
(- (or (:rotation parent) 0) (or (:rotation main-parent) 0)))
rotation (mod (+ (or (:rotation baseline) 0) parent-delta) 360)
flip (fn [key]
(if (:shape-ref parent)
(boolean (get baseline key))
(odd? (count (filter identity [(get baseline key) (get parent key)
(get main-parent key)])))))
changed? (or (not (mth/close? (mod (or (:rotation shape) 0) 360) rotation 0.01))
(not= (boolean (:flip-x shape)) (flip :flip-x))
(not= (boolean (:flip-y shape)) (flip :flip-y)))]
(cond-> changes
changed?
(pcb/update-shapes [(:id shape)]
#(update % :touched (fnil conj #{}) :geometry-group)
{:ignore-touched true}))))
(defn generate-keep-touched
"This is used as part of the switch process, when you switch from
an original-shape to a new-shape. It generate changes to
@ -435,7 +460,12 @@
(keep-swapped-item changes new old ldata page libraries)
(clsw/update-attrs-on-switch changes new old new-shape original-shape old-ref container)))
changes
pairs)]
pairs)
changes (cond-> changes
(ctk/touched-group? original-shape :geometry-group)
(keep-orientation-override original-shape new-shape page
(or (get-in libraries [(:component-file new-shape) :data]) ldata)
(:component-id new-shape)))]
[changes (mapv (comp :parent-id :new) pairs)]))
(defn- shape-index
@ -445,31 +475,6 @@
(when-let [parent (get objects parent-id)]
(or (d/index-of (:shapes parent) shape-id) 0)))
(defn- keep-orientation-override
"A plain swap keeps its old orientation. When that differs from the new
component's expected placement, the swap authors a geometry override."
[changes shape baseline page ldata component-id]
(let [parent (get-in page [:objects (:parent-id shape)])
main (ctf/get-component-root ldata (ctkl/get-component ldata component-id))
main-parent (get-in (ctf/get-component-container ldata (ctkl/get-component ldata component-id))
[:objects (:parent-id main)])
parent-delta (if (:shape-ref parent) 0
(- (or (:rotation parent) 0) (or (:rotation main-parent) 0)))
rotation (mod (+ (or (:rotation baseline) 0) parent-delta) 360)
flip (fn [key]
(if (:shape-ref parent)
(boolean (get baseline key))
(odd? (count (filter identity [(get baseline key) (get parent key)
(get main-parent key)])))))
changed? (or (not (mth/close? (mod (or (:rotation shape) 0) 360) rotation 0.01))
(not= (boolean (:flip-x shape)) (flip :flip-x))
(not= (boolean (:flip-y shape)) (flip :flip-y)))]
(cond-> changes
changed?
(pcb/update-shapes [(:id shape)]
#(update % :touched (fnil conj #{}) :geometry-group)
{:ignore-touched true}))))
(defn generate-component-swap-in-place
"Swap the copy head `shape` of `page` for a copy of the component
`component-id` of library data `ldata`. The new head takes the old one's

View File

@ -642,7 +642,10 @@
copy (ths/get-shape file' :copy01)
child (ths/get-shape-by-id file' (first (:shapes copy)))]
(t/is (== 30 (mth/precision (:rotation copy) 2)))
(t/is (== 30 (mth/precision (:rotation child) 2)))))
(t/is (== 30 (mth/precision (:rotation child) 2)))
(t/is (ctk/touched-group? copy :geometry-group))
(t/is (= (orientation file' :copy01)
(orientation (propagate file' :c02) :copy01)))))
(t/deftest switch-turns-the-swap-with-the-child-of-the-new-variant
(let [file (-> (thf/sample-file :file1)