🐛 Turn before the flips when neither order is nearer the old axes

A variant switch that carries a turn onto a flipped child gives it the
turn before or after its flips, whichever lies nearer the axes the child
had (`unskewed`). For an unflipped 45-degree turn given a horizontal flip
both lie equally far, and the tie went to the order the engines'
rounding favoured: with the WASM engine the switched child stored
`flipX·R45`, the same shape turned the other way, while its rotation and
flips say `R45·flipX`.

A tie within 1e-9 now takes the turn before the flips, as the function
already intends for axes that tell neither apart.

Found by the WASM replay of the pinned generated programs (program 87, a
switch onto a flipped variant), which passes with it.

Test (comp-expected-geometry-test, fails before the fix with the WASM
engine's axes): `an-unskewed-turn-that-tells-no-flip-apart-turns-before-it`.

Benchmark (bench-components, alone):

|                             :world | :baseline |    :now |   :mean | :ratio |
|------------------------------------+-----------+---------+---------+--------|
|  deepest-edit-through-three-levels |    35.215 |  22.123 |  22.948 |   0.63 |
| edit-through-a-chain-of-dependents |    68.931 |  41.395 |  43.459 |    0.6 |
|         library-update-into-copies |    83.083 |  51.144 |  55.732 |   0.62 |
|        main-edit-among-many-shapes |     4.837 |   2.373 |   2.539 |   0.49 |
|           main-edit-into-10-copies |      1.79 |   1.047 |   1.091 |   0.58 |
|          main-edit-into-100-copies |    15.415 |  10.442 |   10.86 |   0.68 |
|         main-edit-into-1000-copies |   153.086 | 105.735 | 109.461 |   0.69 |
|         variant-switch-into-copies |    32.052 |  21.661 |  23.984 |   0.68 |

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-11-17 03:27:50 +01:00
parent 8242ddbec7
commit bce9ab99cd
2 changed files with 17 additions and 1 deletions

View File

@ -732,7 +732,7 @@
flipped (gmt/multiply flips turn)
;; axes that tell neither apart, as those of a bool fitting resets,
;; take the turn before the flips, as a flip of a turned shape stores
transform (if (>= (closeness turned) (closeness flipped)) turned flipped)]
transform (if (>= (closeness turned) (- (closeness flipped) 1e-9)) turned flipped)]
(assoc shape :transform transform :transform-inverse (gmt/inverse transform))))
(defn- layout-skewed?

View File

@ -617,3 +617,19 @@
(t/is (mth/close? 90 (:rotation turned) 0.01))
(t/is (mth/close? 20 (gpt/distance p1 p2) 1e-6))
(t/is (mth/close? 20 (gpt/distance p1 p4) 1e-6))))
(t/deftest an-unskewed-turn-that-tells-no-flip-apart-turns-before-it
;; a shape turned 45 degrees without flips, given a horizontal flip: both
;; orders of turn and flip lie as far from its axes, whatever rounding the
;; engine leaves in them, so the turn goes before the flip
(let [c (mth/cos (mth/radians 45))]
(doseq [[a b cc d] [[c c (- c) c]
[0.7071067811865475 0.7071067811865479 -0.7071067811865479 0.7071067811865479]
[0.7071067811865479 0.7071067811865475 -0.707106781186547 0.7071067811865475]
[0.7071067690849304 0.7071067690849304 -0.7071067690849304 0.7071067690849304]]]
(let [shape {:rotation 45 :flip-x true :flip-y false
:transform (gmt/matrix a b cc d 0 0)}
transform (:transform (cll/unskewed shape))]
(t/is (gmt/close? (gmt/multiply (gmt/rotate-matrix 45) (gmt/scale-matrix (gpt/point -1 1)))
transform)
(pr-str [a b cc d transform]))))))