🐛 Turn a shape without flipping it on a large rotation

A transform reads its flip flags from the shape's edges: when both edges
reverse, both flags flip. A rotation by more than 90 degrees reverses both
edges too, and its structure modifier then adds the angle to the rotation,
so the half turn was counted twice. A rect rotated 135 or 180 degrees showed
as flipped on both axes, and flip-y, rotate 180 degrees, flip-x left an
unrotated shape stored with rotation 180 and no flags.

Pass the turn a transform's structure modifiers add into the flip
adjustment. Without a reflection, both flags flip only for a half turn the
rotation does not count, as a resize by -1 on both axes.

A fourth re-run of the seed-20261101 library dependency property found this;
it also fails on earlier commits. The regression fails before the fix
(135 and 180 degrees set both flags; the sequence ends inconsistent) and
passes after it; a -1 resize on both axes still flips both. Full common
passes lint, JVM, JavaScript and formatting: JVM 2081 tests / 33910
assertions; JavaScript 2017 / 32557. All six component properties pass 40
programs each at seed 712 in both runtimes.

Compare lower-quantile milliseconds with the saved baseline; keep that
baseline unchanged. The machine/JVM warning makes ratios rough. No world
is flagged.

World                      Baseline       Now    Ratio
Main into 10 copies           1.790     0.985     0.55
Main into 100 copies         15.415    10.150     0.66
Main into 1000 copies       153.086   104.892     0.69
Three nesting levels         35.215    19.738     0.56
Dependency chain             68.931    48.772     0.71
Library update               83.083    49.020     0.59
Many plain shapes             4.837     2.576     0.53
Variant switch               32.052    20.357     0.64

AI-assisted-by: claude-opus-5-5
This commit is contained in:
alonso.torres 2026-10-31 16:48:35 +01:00
parent c3356b5325
commit 50dc444e71
3 changed files with 62 additions and 17 deletions

View File

@ -284,8 +284,9 @@
(defn- adjust-shape-flips
"After some transformations the flip-x/flip-y flags can change we need
to check this before adjusting the selrect"
[shape points]
to check this before adjusting the selrect. `turn` is the rotation in
degrees the transform adds to the shape's own."
[shape points turn]
(let [points' (dm/get-prop shape :points)
p0' (nth points' 0)
p0 (nth points 0)
@ -317,15 +318,20 @@
[flip-x? flip-y?]
(cond
(not area?) [flip-x? flip-y?]
(not mirror?) (if (and flip-x? flip-y?) [true true] [false false])
;; Without a reflection both flags flip only for a half turn the
;; rotation does not count, as a resize by -1 on both axes.
(not mirror?)
(let [edge-turn (mth/degrees (mth/atan2 (- (* (:x xv1) (:y xv2)) (* (:y xv1) (:x xv2)))
(gpt/dot xv1 xv2)))
half? (< (abs (- (mod (- edge-turn turn) 360) 180)) 1)]
[half? half?])
(not= flip-x? flip-y?) [flip-x? flip-y?]
;; A mirror axis at 45 degrees to the edges turns both by a right
;; angle. Flip the axis that keeps the final turn along the new x
;; edge: flipping y keeps the x edge's direction, flipping x
;; reverses it.
:else
(let [turn (mth/radians (- (d/nilv (dm/get-in shape [:modifiers :rotation]) 0)
(d/nilv (:rotation shape) 0)))
(let [turn (mth/radians (- turn (d/nilv (:rotation shape) 0)))
sign (if (:flip-x shape) -1 1)
along (* sign (+ (* (mth/cos turn) (:x xv2)) (* (mth/sin turn) (:y xv2))))]
[(neg? along) (not (neg? along))]))]
@ -369,11 +375,12 @@
(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]
[shape transform-mtx turn]
(let [points (-> (dm/get-prop shape :points)
(gco/transform-points transform-mtx))
shape (adjust-shape-flips shape points)
shape (adjust-shape-flips shape points
(+ turn (d/nilv (dm/get-in shape [:modifiers :rotation]) 0)))
center (gco/points->center points)
selrect (calculate-selrect points center)
@ -406,17 +413,21 @@
(defn apply-transform
"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]
(cond
(nil? transform-mtx)
shape
its properties. We adjust de x,y,width,height and create a custom transform.
`turn` is the rotation in degrees that the caller adds to the shape after
the transform."
([shape transform-mtx]
(apply-transform shape transform-mtx 0))
([shape transform-mtx turn]
(cond
(nil? transform-mtx)
shape
^boolean (gmt/move? transform-mtx)
(apply-transform-move shape transform-mtx)
^boolean (gmt/move? transform-mtx)
(apply-transform-move shape transform-mtx)
:else
(apply-transform-generic shape transform-mtx)))
:else
(apply-transform-generic shape transform-mtx turn))))
(defn- update-group-viewbox
"Updates the viewbox for groups imported from SVG's"
@ -536,7 +547,7 @@
(cond-> shape
(and (some? transform)
(not (cfh/root? shape)))
(apply-transform transform)
(apply-transform transform (ctm/structure-rotation modifiers))
(ctm/has-structure? modifiers)
(ctm/apply-structure-modifiers modifiers)))

View File

@ -530,6 +530,16 @@
(or (d/not-empty? (dm/get-prop modifiers :geometry-parent))
(d/not-empty? (dm/get-prop modifiers :geometry-child))))
(defn structure-rotation
"The degrees the structure operations of `modifiers` add to a shape's
rotation."
[modifiers]
(transduce (comp (filter #(= :rotation (dm/get-prop % :type)))
(map #(dm/get-prop % :value)))
+ 0
(concat (dm/get-prop modifiers :structure-parent)
(dm/get-prop modifiers :structure-child))))
(defn has-structure?
[{:keys [structure-parent structure-child]}]
(or (d/not-empty? structure-parent)

View File

@ -315,3 +315,27 @@
(t/is (not= (boolean (:flip-x after)) (boolean (:flip-y after))) (str angle " " axis))
(t/is (every? #(mth/close? (get implied %) (get (:transform after) %) 1e-9) [:a :b :c :d])
(str angle " " axis)))))
(t/deftest a-rotation-turns-a-shape-without-flipping-it
(let [shape (create-test-shape :rect {:width 120 :height 60})
rotate (fn [shape angle]
(gsh/transform-shape shape (ctm/rotation-modifiers shape (gsh/shape->center shape) angle)))
mirror (fn [shape axis]
(gsh/transform-shape shape (ctm/resize-modifiers axis (gsh/shape->center shape))))
composes? (fn [shape]
(let [scale (gpt/point (if (:flip-x shape) -1 1) (if (:flip-y shape) -1 1))
implied (gmt/multiply (gmt/rotate-matrix (:rotation shape)) (gmt/scale-matrix scale))]
(every? #(mth/close? (get implied %) (get (:transform shape) %) 1e-9) [:a :b :c :d])))]
(doseq [angle [135 180 270]]
(let [after (rotate shape angle)]
(t/is (not (:flip-x after)) (str angle))
(t/is (not (:flip-y after)) (str angle))
(t/is (mth/close? angle (:rotation after)) (str angle))
(t/is (composes? after) (str angle))))
(let [turned (-> shape (mirror (gpt/point 1 -1)) (rotate 180))
back (mirror turned (gpt/point -1 1))]
(t/is (composes? turned))
(t/is (composes? back)))
(let [halved (mirror (rotate shape 30) (gpt/point -1 -1))]
(t/is (and (:flip-x halved) (:flip-y halved)))
(t/is (composes? halved)))))