diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index a783341f0e..2f08cf61a5 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -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))) diff --git a/common/src/app/common/types/modifiers.cljc b/common/src/app/common/types/modifiers.cljc index fb16abf80c..0cde420de6 100644 --- a/common/src/app/common/types/modifiers.cljc +++ b/common/src/app/common/types/modifiers.cljc @@ -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) diff --git a/common/test/common_tests/geom_shapes_test.cljc b/common/test/common_tests/geom_shapes_test.cljc index 5eb5ace5a6..5c47ce5c4d 100644 --- a/common/test/common_tests/geom_shapes_test.cljc +++ b/common/test/common_tests/geom_shapes_test.cljc @@ -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)))))