mirror of
https://github.com/penpot/penpot.git
synced 2026-11-22 18:30:34 +00:00
Under one flip a shape can store either of two turns for the same transform: the one before its flip, which flips and turns produce, or the opposite one after it, which sync may store when it orients a copy child under a flipped parent. A transform adds its turn to the stored rotation, and a flip negates it, as for the turn before the flip, so a shape storing the other one was left with a rotation its transform does not have: a child turned 45 degrees after its flip and turned again by 45 stored 90 over a plain flip. `apply-transform-generic` now first takes, for a mirrored shape with an unskewed transform, the turn before its flip that the transform gives. Found by checking the shapes of the pinned generated programs for rotations and flips that do not compose their transform (program 332: rotate, flip the parent, rotate it, update main); none is left there but the skew a layout or a scale constraint gives a turned child. Test (geom-shapes-test, fails before the fix): `a-rotation-turns-a-mirror-that-stores-the-turn-after-its-flip`. Benchmark (bench-components, alone): | :world | :baseline | :now | :mean | :ratio | |------------------------------------+-----------+--------+---------+--------| | deepest-edit-through-three-levels | 35.215 | 21.448 | 23.648 | 0.61 | | edit-through-a-chain-of-dependents | 68.931 | 41.076 | 43.132 | 0.6 | | library-update-into-copies | 83.083 | 54.822 | 56.18 | 0.66 | | main-edit-among-many-shapes | 4.837 | 2.275 | 2.358 | 0.47 | | main-edit-into-10-copies | 1.79 | 1.08 | 1.103 | 0.6 | | main-edit-into-100-copies | 15.415 | 10.411 | 11.35 | 0.68 | | main-edit-into-1000-copies | 153.086 | 110.32 | 118.294 | 0.72 | | variant-switch-into-copies | 32.052 | 21.161 | 23.07 | 0.66 | AI-assisted-by: claude-opus-5-5
362 lines
16 KiB
Clojure
362 lines
16 KiB
Clojure
;; This Source Code Form is subject to the terms of the Mozilla Public
|
|
;; License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
;;
|
|
;; Copyright (c) KALEIDOS SUBSIDIARY SL
|
|
|
|
(ns common-tests.geom-shapes-test
|
|
(:require
|
|
[app.common.geom.matrix :as gmt]
|
|
[app.common.geom.point :as gpt]
|
|
[app.common.geom.rect :as grc]
|
|
[app.common.geom.shapes :as gsh]
|
|
[app.common.geom.shapes.intersect :as gsin]
|
|
[app.common.geom.shapes.transforms :as gsht]
|
|
[app.common.math :as mth :refer [close?]]
|
|
[app.common.types.modifiers :as ctm]
|
|
[app.common.types.path :as path]
|
|
[app.common.types.shape :as cts]
|
|
[clojure.test :as t]))
|
|
|
|
(def default-path
|
|
[{:command :move-to :params {:x 0 :y 0}}
|
|
{:command :line-to :params {:x 20 :y 20}}
|
|
{:command :line-to :params {:x 30 :y 30}}
|
|
{:command :curve-to :params {:x 40 :y 40 :c1x 35 :c1y 35 :c2x 45 :c2y 45}}
|
|
{:command :close-path}])
|
|
|
|
(defn create-test-shape
|
|
([type] (create-test-shape type {}))
|
|
([type params]
|
|
(if (= type :path)
|
|
(cts/setup-shape
|
|
(into {:type :path
|
|
:content (path/content (:content params default-path))}
|
|
params))
|
|
(cts/setup-shape
|
|
(into {:type type
|
|
:width 20
|
|
:height 20}
|
|
params)))))
|
|
|
|
(t/deftest regrouping-preserves-the-representation-of-unflipped-axes
|
|
(doseq [horizontal [{} {:flip-x nil} {:flip-x false}]
|
|
vertical [{} {:flip-y nil} {:flip-y false}]]
|
|
(let [group (merge (dissoc (create-test-shape :group) :flip-x :flip-y) horizontal vertical)
|
|
child (create-test-shape :rect)
|
|
after (gsh/update-group-selrect group [child])]
|
|
(t/is (= (select-keys group [:flip-x :flip-y]) (select-keys after [:flip-x :flip-y])))
|
|
(t/is (gsh/close-attrs? :points (:points child) (:points after) 1e-10))
|
|
(t/is (= (select-keys after [:flip-x :flip-y])
|
|
(select-keys (gsh/update-group-selrect after [child]) [:flip-x :flip-y]))))))
|
|
|
|
(t/deftest regrouping-still-recalculates-stale-flip-flags
|
|
(let [group (create-test-shape :group {:flip-x true :flip-y true})
|
|
child (create-test-shape :rect)
|
|
after (gsh/update-group-selrect group [child])]
|
|
(t/is (false? (:flip-x after)))
|
|
(t/is (false? (:flip-y after)))
|
|
(t/is (gsh/close-attrs? :points (:points child) (:points after) 1e-10))))
|
|
|
|
(t/deftest transform-shapes
|
|
(t/testing "Shape without modifiers should stay the same"
|
|
(t/are [type]
|
|
(let [shape-before (create-test-shape type)
|
|
shape-after (gsh/transform-shape shape-before)]
|
|
(= shape-before shape-after))
|
|
|
|
:rect :path))
|
|
|
|
(t/testing "Transform shape with translation modifiers"
|
|
(doseq [type [:rect :path]]
|
|
(let [modifiers (ctm/move-modifiers (gpt/point 10 -10))
|
|
shape-before (create-test-shape type {:modifiers modifiers})
|
|
shape-after (gsh/transform-shape shape-before)]
|
|
|
|
(t/is (not= shape-before shape-after))
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :x])
|
|
(- 10 (get-in shape-after [:selrect :x]))))
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :y])
|
|
(+ 10 (get-in shape-after [:selrect :y]))))
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :width])
|
|
(get-in shape-after [:selrect :width])))
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :height])
|
|
(get-in shape-after [:selrect :height]))))))
|
|
|
|
(t/testing "Transform with empty translation"
|
|
(t/are [type]
|
|
(let [modifiers {:displacement (gmt/matrix)}
|
|
shape-before (create-test-shape type {:modifiers modifiers})
|
|
shape-after (gsh/transform-shape shape-before)]
|
|
(t/are [prop]
|
|
(t/is (close? (get-in shape-before [:selrect prop])
|
|
(get-in shape-after [:selrect prop])))
|
|
:x :y :width :height :x1 :y1 :x2 :y2))
|
|
:rect :path))
|
|
|
|
(t/testing "Transform shape with resize modifiers"
|
|
(t/are [type]
|
|
(let [modifiers (ctm/resize-modifiers (gpt/point 2 2) (gpt/point 0 0))
|
|
shape-before (create-test-shape type {:modifiers modifiers})
|
|
shape-after (gsh/transform-shape shape-before)]
|
|
(t/is (not= shape-before shape-after))
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :x])
|
|
(get-in shape-after [:selrect :x])))
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :y])
|
|
(get-in shape-after [:selrect :y])))
|
|
|
|
(t/is (close? (* 2 (get-in shape-before [:selrect :width]))
|
|
(get-in shape-after [:selrect :width])))
|
|
|
|
(t/is (close? (* 2 (get-in shape-before [:selrect :height]))
|
|
(get-in shape-after [:selrect :height]))))
|
|
:rect :path))
|
|
|
|
(t/testing "Transform with empty resize"
|
|
(t/are [type]
|
|
(let [modifiers (ctm/resize-modifiers (gpt/point 1 1) (gpt/point 0 0))
|
|
shape-before (create-test-shape type {:modifiers modifiers})
|
|
shape-after (gsh/transform-shape shape-before)]
|
|
(t/are [prop]
|
|
(t/is (close? (get-in shape-before [:selrect prop])
|
|
(get-in shape-after [:selrect prop])))
|
|
:x :y :width :height :x1 :y1 :x2 :y2))
|
|
:rect :path))
|
|
|
|
(t/testing "Transform with resize=0"
|
|
(let [modifiers (ctm/resize-modifiers (gpt/point 0 0) (gpt/point 0 0))
|
|
shape-before (create-test-shape :rect {:modifiers modifiers})
|
|
shape-after (gsh/transform-shape shape-before)]
|
|
(t/is (close? 0.01 (get-in shape-after [:selrect :width])))
|
|
(t/is (close? 0.01 (get-in shape-after [:selrect :height])))))
|
|
|
|
(t/testing "Transform shape with rotation modifiers"
|
|
(t/are [type]
|
|
(let [shape-before (create-test-shape type)
|
|
modifiers (ctm/rotation-modifiers shape-before (gsh/shape->center shape-before) 30)
|
|
shape-before (assoc shape-before :modifiers modifiers)
|
|
shape-after (gsh/transform-shape shape-before)]
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :x])
|
|
(get-in shape-after [:selrect :x])))
|
|
|
|
(t/is (close? (get-in shape-before [:selrect :y])
|
|
(get-in shape-after [:selrect :y])))
|
|
|
|
(t/is (= (count (:points shape-before)) (count (:points shape-after))))
|
|
|
|
(for [idx (range 0 (count (:point shape-before)))]
|
|
(do (t/is (not (close? (get-in shape-before [:points idx :x])
|
|
(get-in shape-after [:points idx :x]))))
|
|
(t/is (not (close? (get-in shape-before [:points idx :y])
|
|
(get-in shape-after [:points idx :y])))))))
|
|
:rect :path))
|
|
|
|
(t/testing "Transform shape with rotation = 0 should leave equal selrect"
|
|
(t/are [type]
|
|
(let [shape-before (create-test-shape type)
|
|
modifiers (ctm/rotation-modifiers shape-before (gsh/shape->center shape-before) 0)
|
|
shape-after (gsh/transform-shape (assoc shape-before :modifiers modifiers))]
|
|
(t/are [prop]
|
|
(t/is (close? (get-in shape-before [:selrect prop])
|
|
(get-in shape-after [:selrect prop])))
|
|
:x :y :width :height :x1 :y1 :x2 :y2))
|
|
:rect :path))
|
|
|
|
(t/testing "Transform shape with invalid selrect fails gracefully"
|
|
(t/are [type selrect]
|
|
(let [modifiers (ctm/move-modifiers 0 0)
|
|
shape-before (create-test-shape type {:selrect selrect})
|
|
shape-after (gsh/transform-shape shape-before modifiers)]
|
|
|
|
(t/is (grc/close-rect? (:selrect shape-before)
|
|
(:selrect shape-after))))
|
|
|
|
:rect (grc/make-rect 0 0 ##Inf ##Inf)
|
|
:path (grc/make-rect 0 0 ##Inf ##Inf))))
|
|
|
|
(t/deftest points-to-selrect
|
|
(let [points [(gpt/point 0.5 0.5)
|
|
(gpt/point -1 -2)
|
|
(gpt/point 20 65.2)
|
|
(gpt/point 12 -10)]
|
|
result (grc/points->rect points)
|
|
expect {:x -1.0, :y -10.0, :width 21.0, :height 75.2}]
|
|
|
|
(t/is (= (:x expect) (:x result)))
|
|
(t/is (= (:y expect) (:y result)))
|
|
(t/is (= (:width expect) (:width result)))
|
|
(t/is (= (:height expect) (:height result)))))
|
|
|
|
(def g45 (mth/radians 45))
|
|
|
|
(t/deftest points-transform-matrix
|
|
(t/testing "Transform matrix"
|
|
(t/are [selrect points expected]
|
|
(let [result (gsht/transform-points-matrix selrect points)]
|
|
(t/is (gmt/close? expected result)))
|
|
|
|
;; No transformation
|
|
(grc/make-rect 0 0 10 10)
|
|
(-> (grc/make-rect 0 0 10 10)
|
|
(grc/rect->points))
|
|
(gmt/matrix)
|
|
|
|
;; Displacement
|
|
(grc/make-rect 0 0 10 10)
|
|
(-> (grc/make-rect 20 20 10 10)
|
|
(grc/rect->points))
|
|
(gmt/matrix 1 0 0 1 20 20)
|
|
|
|
;; Resize
|
|
(grc/make-rect 0 0 10 10)
|
|
(-> (grc/make-rect 0 0 20 40)
|
|
(grc/rect->points))
|
|
(gmt/matrix 2 0 0 4 0 0)
|
|
|
|
;; Displacement+Resize
|
|
(grc/make-rect 0 0 10 10)
|
|
(-> (grc/make-rect 10 10 20 40)
|
|
(grc/rect->points))
|
|
(gmt/matrix 2 0 0 4 10 10)
|
|
|
|
;; Rotation
|
|
(grc/make-rect 0 0 10 10)
|
|
(-> (grc/make-rect 0 0 10 10)
|
|
(grc/rect->points)
|
|
(gsh/transform-points (gmt/rotate-matrix 45)))
|
|
(gmt/matrix (mth/cos g45) (mth/sin g45) (- (mth/sin g45)) (mth/cos g45) 0 0)
|
|
|
|
;; Rotation + Resize
|
|
(grc/make-rect 0 0 10 10)
|
|
(-> (grc/make-rect 0 0 20 40)
|
|
(grc/rect->points)
|
|
(gsh/transform-points (gmt/rotate-matrix 45)))
|
|
(gmt/matrix (* (mth/cos g45) 2) (* (mth/sin g45) 2) (* (- (mth/sin g45)) 4) (* (mth/cos g45) 4) 0 0))))
|
|
|
|
|
|
(t/deftest shape-has-point-pred
|
|
(let [{:keys [points] :as shape} (create-test-shape :rect {})
|
|
point1 (first points)
|
|
point2 (update point1 :x - 100)]
|
|
(t/is (true? (gsin/fast-has-point? shape point1)))
|
|
(t/is (true? (gsin/slow-has-point? shape point1)))
|
|
(t/is (false? (gsin/fast-has-point? shape point2)))
|
|
(t/is (false? (gsin/fast-has-point? shape point2)))))
|
|
|
|
;; ---- adjust-shape-flips (via apply-transform / transform-shape) ----
|
|
|
|
(t/deftest flip-x-only-toggles-flip-x-and-negates-rotation
|
|
(t/testing "Flipping only X axis toggles flip-x and negates rotation"
|
|
;; Build a rect with a known rotation, then apply a scale(-1, 1)
|
|
;; from the left edge to simulate an X-axis flip.
|
|
(let [shape (create-test-shape :rect {:rotation 30})
|
|
;; Flip horizontally about x=0 (left edge of shape)
|
|
origin (gpt/point (get-in shape [:selrect :x]) (get-in shape [:selrect :y]))
|
|
mods (ctm/resize-modifiers (gpt/point -1 1) origin)
|
|
result (gsh/transform-shape shape mods)]
|
|
;; flip-x should have been toggled (from nil/false to true)
|
|
(t/is (true? (:flip-x result)))
|
|
;; flip-y should NOT be set
|
|
(t/is (not (true? (:flip-y result))))
|
|
;; rotation is negated then normalised into [0,360): -30 mod 360 = 330
|
|
(t/is (mth/close? 330 (:rotation result))))))
|
|
|
|
(t/deftest flip-y-only-toggles-flip-y-and-negates-rotation
|
|
(t/testing "Flipping only Y axis toggles flip-y and negates rotation"
|
|
(let [shape (create-test-shape :rect {:rotation 45})
|
|
origin (gpt/point (get-in shape [:selrect :x]) (get-in shape [:selrect :y]))
|
|
mods (ctm/resize-modifiers (gpt/point 1 -1) origin)
|
|
result (gsh/transform-shape shape mods)]
|
|
(t/is (not (true? (:flip-x result))))
|
|
(t/is (true? (:flip-y result)))
|
|
;; -45 mod 360 = 315
|
|
(t/is (mth/close? 315 (:rotation result))))))
|
|
|
|
(t/deftest flip-both-axes-toggles-both-flags-but-preserves-rotation
|
|
(t/testing "Flipping both axes toggles flip-x and flip-y, but does NOT negate rotation"
|
|
;; Two simultaneous axis flips = 180° rotation, so stored rotation is unchanged.
|
|
(let [shape (create-test-shape :rect {:rotation 30})
|
|
origin (gpt/point (get-in shape [:selrect :x]) (get-in shape [:selrect :y]))
|
|
mods (ctm/resize-modifiers (gpt/point -1 -1) origin)
|
|
result (gsh/transform-shape shape mods)]
|
|
(t/is (true? (:flip-x result)))
|
|
(t/is (true? (:flip-y result)))
|
|
;; rotation must not be negated when both axes are flipped
|
|
(t/is (mth/close? 30 (:rotation result))))))
|
|
|
|
(t/deftest a-right-angle-turn-does-not-invent-a-flip-from-roundoff
|
|
(doseq [size [0.01 100 1000000]]
|
|
(let [shape (create-test-shape :rect {:width size :height size :rotation 90})
|
|
shape (assoc shape :points [(gpt/point size 0) (gpt/point size size)
|
|
(gpt/point 0 size) (gpt/point 0 (* size 1e-14))])
|
|
turn (gmt/transform-in (gpt/point (/ size 2) (/ size 2)) (gmt/rotate-matrix 90))
|
|
after (gsh/apply-transform (assoc-in shape [:modifiers :rotation] 90) turn)]
|
|
(t/is (not (:flip-x after)))
|
|
(t/is (not (:flip-y after)))
|
|
(t/is (mth/close? 180 (:rotation after))))))
|
|
|
|
(t/deftest a-reflection-keeps-flags-and-rotation-consistent-with-the-transform
|
|
;; At odd multiples of 45 degrees a reflection turns both edges by a
|
|
;; right angle, so neither edge reverses.
|
|
(doseq [angle [30 45 135 225 315]
|
|
axis [(gpt/point -1 1) (gpt/point 1 -1)]]
|
|
(let [shape (create-test-shape :rect {:width 120 :height 140})
|
|
rotated (gsh/transform-shape shape (ctm/rotation-modifiers shape (gsh/shape->center shape) angle))
|
|
after (gsh/transform-shape rotated (ctm/resize-modifiers axis (gsh/shape->center rotated)))
|
|
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 (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)))))
|
|
|
|
(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))))))
|