From 69fe8bc9b5adc993b70000ca39234d9d358b7ab9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Moya?= Date: Wed, 9 Jun 2021 08:41:41 +0200 Subject: [PATCH] :recycle: Add some small performance refactors --- common/src/app/common/geom/matrix.cljc | 16 ++++++++++++ common/src/app/common/geom/shapes/rect.cljc | 5 ---- .../app/common/geom/shapes/transforms.cljc | 26 +++++++++---------- 3 files changed, 29 insertions(+), 18 deletions(-) diff --git a/common/src/app/common/geom/matrix.cljc b/common/src/app/common/geom/matrix.cljc index fc2513f4a8..761e641ff0 100644 --- a/common/src/app/common/geom/matrix.cljc +++ b/common/src/app/common/geom/matrix.cljc @@ -48,6 +48,22 @@ ([m1 m2 & others] (reduce multiply (multiply m1 m2) others))) +(defn add-translate + "Given two TRANSLATE matrixes (only e and f have significative + values), combine them. Quicker than multiplying them, for this + precise case." + ([{m1a :a m1b :b m1c :c m1d :d m1e :e m1f :f} + {m2a :a m2b :b m2c :c m2d :d m2e :e m2f :f}] + (Matrix. + 1 + 0 + 0 + 1 + (+ m1e m2e) + (+ m1f m2f))) + ([m1 m2 & others] + (reduce add-translate (add-translate m1 m2) others))) + (defn substract [{m1a :a m1b :b m1c :c m1d :d m1e :e m1f :f} {m2a :a m2b :b m2c :c m2d :d m2e :e m2f :f}] diff --git a/common/src/app/common/geom/shapes/rect.cljc b/common/src/app/common/geom/shapes/rect.cljc index 853f7d7f48..91e7d18a9a 100644 --- a/common/src/app/common/geom/shapes/rect.cljc +++ b/common/src/app/common/geom/shapes/rect.cljc @@ -9,11 +9,6 @@ [app.common.geom.point :as gpt] [app.common.geom.shapes.common :as gco])) -(defn left-of [rect] (:x rect)) -(defn right-of [rect] (+ (:x rect) (:width rect))) -(defn top-of [rect] (:y rect)) -(defn bottom-of [rect] (+ (:y rect) (:height rect))) - (defn rect->points [{:keys [x y width height]}] ;; (assert (number? x)) ;; (assert (number? y)) diff --git a/common/src/app/common/geom/shapes/transforms.cljc b/common/src/app/common/geom/shapes/transforms.cljc index 09dfb3f8b5..cddd6e4af3 100644 --- a/common/src/app/common/geom/shapes/transforms.cljc +++ b/common/src/app/common/geom/shapes/transforms.cljc @@ -374,31 +374,31 @@ orig-h (when origin (cond - (mth/close? (:x origin) (gpr/left-of parent-rect)) :left - (mth/close? (:x origin) (gpr/right-of parent-rect)) :right + (mth/close? (:x origin) (:x1 parent-rect)) :left + (mth/close? (:x origin) (:x2 parent-rect)) :right :else :middle)) orig-v (when origin (cond - (mth/close? (:y origin) (gpr/top-of parent-rect)) :top - (mth/close? (:y origin) (gpr/bottom-of parent-rect)) :bottom + (mth/close? (:y origin) (:y1 parent-rect)) :top + (mth/close? (:y origin) (:y2 parent-rect)) :bottom :else :middle)) delta-h (when orig-h (cond (= orig-h :left) - (- (gpr/right-of transformed-parent-rect) (gpr/right-of parent-rect)) + (- (:x2 transformed-parent-rect) (:x2 parent-rect)) (= orig-h :right) - (- (gpr/left-of transformed-parent-rect) (gpr/left-of parent-rect)) + (- (:x1 transformed-parent-rect) (:x1 parent-rect)) :else 0)) delta-v (when orig-v (cond (= orig-v :top) - (- (gpr/bottom-of transformed-parent-rect) (gpr/bottom-of parent-rect)) + (- (:y2 transformed-parent-rect) (:y2 parent-rect)) (= orig-v :bottom) - (- (gpr/top-of transformed-parent-rect) (gpr/top-of parent-rect)) + (- (:y1 transformed-parent-rect) (:y1 parent-rect)) :else 0)) @@ -418,13 +418,13 @@ :leftright (cond (= orig-h :left) - {:resize-origin (gpt/point (gpr/left-of child-rect) (gpr/top-of child-rect)) + {:resize-origin (gpt/point (:x1 child-rect) (:y1 child-rect)) :resize-vector (gpt/point (/ (+ (:width child-rect) delta-h) (:width child-rect)) 1)} (= orig-h :right) - {:resize-origin (gpt/point (gpr/right-of child-rect) (gpr/top-of child-rect)) + {:resize-origin (gpt/point (:x2 child-rect) (:y1 child-rect)) :resize-vector (gpt/point (/ (- (:width child-rect) delta-h) (:width child-rect)) 1)} @@ -455,13 +455,13 @@ :topbottom (cond (= orig-v :top) - {:resize-origin (gpt/point (gpr/left-of child-rect) (gpr/top-of child-rect)) + {:resize-origin (gpt/point (:x1 child-rect) (:y1 child-rect)) :resize-vector (gpt/point 1 (/ (+ (:height child-rect) delta-v) (:height child-rect)))} (= orig-v :bottom) - {:resize-origin (gpt/point (gpr/left-of child-rect) (gpr/bottom-of child-rect)) + {:resize-origin (gpt/point (:x1 child-rect) (:y2 child-rect)) :resize-vector (gpt/point 1 (/ (- (:height child-rect) delta-v) (:height child-rect)))} @@ -493,7 +493,7 @@ (:displacement parent-modifiers) (update :displacement #(if (nil? %) (:displacement parent-modifiers) - (gmt/multiply % (:displacement parent-modifiers))))))) + (gmt/add-translate % (:displacement parent-modifiers))))))) (defn update-group-viewbox "Updates the viewbox for groups imported from SVG's"