From 06c8ada6f7f7be72a4c0fea90efa67c4134e8b9b Mon Sep 17 00:00:00 2001 From: "alonso.torres" Date: Wed, 18 Oct 2023 11:13:07 +0200 Subject: [PATCH] :zap: Improve performance for constraints --- .../app/common/geom/shapes/constraints.cljc | 46 +++++++++++++------ .../src/app/common/geom/shapes/modifiers.cljc | 7 ++- common/src/app/common/geom/shapes/points.cljc | 11 +++++ 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/common/src/app/common/geom/shapes/constraints.cljc b/common/src/app/common/geom/shapes/constraints.cljc index 6a9885e69..cda192609 100644 --- a/common/src/app/common/geom/shapes/constraints.cljc +++ b/common/src/app/common/geom/shapes/constraints.cljc @@ -314,21 +314,37 @@ (let [transformed-parent-bounds @transformed-parent-bounds modifiers (ctm/select-child modifiers) - transformed-child-bounds (gtr/transform-bounds child-bounds modifiers) - modifiers (normalize-modifiers constraints-h constraints-v modifiers - child-bounds transformed-child-bounds parent-bounds transformed-parent-bounds) - transformed-child-bounds (gtr/transform-bounds child-bounds modifiers) - child-points-before (gpo/parent-coords-bounds child-bounds parent-bounds) - child-points-after (gpo/parent-coords-bounds transformed-child-bounds transformed-parent-bounds) + reset-modifiers? + (and (gpo/axis-aligned? parent-bounds) + (gpo/axis-aligned? child-bounds) + (gpo/axis-aligned? transformed-parent-bounds)) - modifiers-h (constraint-modifier (constraints-h const->type+axis) :x - child-points-before parent-bounds - child-points-after transformed-parent-bounds) + modifiers + (if reset-modifiers? + (ctm/empty) + (normalize-modifiers constraints-h constraints-v modifiers + child-bounds (gtr/transform-bounds child-bounds modifiers) + parent-bounds transformed-parent-bounds)) - modifiers-v (constraint-modifier (constraints-v const->type+axis) :y - child-points-before parent-bounds - child-points-after transformed-parent-bounds)] - (-> modifiers - (ctm/add-modifiers modifiers-h) - (ctm/add-modifiers modifiers-v)))))) + transformed-child-bounds (if reset-modifiers? + child-bounds + (gtr/transform-bounds child-bounds modifiers))] + + ;; If the parent is a layout we don't need to calculate its constraints. Finish + ;; after normalize the children (to keep proper proportions) + (if (ctl/any-layout? parent) + modifiers + (let [child-points-before (gpo/parent-coords-bounds child-bounds parent-bounds) + child-points-after (gpo/parent-coords-bounds transformed-child-bounds transformed-parent-bounds) + + modifiers-h (constraint-modifier (constraints-h const->type+axis) :x + child-points-before parent-bounds + child-points-after transformed-parent-bounds) + + modifiers-v (constraint-modifier (constraints-v const->type+axis) :y + child-points-before parent-bounds + child-points-after transformed-parent-bounds)] + (-> modifiers + (ctm/add-modifiers modifiers-h) + (ctm/add-modifiers modifiers-v)))))))) diff --git a/common/src/app/common/geom/shapes/modifiers.cljc b/common/src/app/common/geom/shapes/modifiers.cljc index fb033a49b..bfcaf6519 100644 --- a/common/src/app/common/geom/shapes/modifiers.cljc +++ b/common/src/app/common/geom/shapes/modifiers.cljc @@ -100,7 +100,11 @@ [modif-tree children objects bounds parent transformed-parent-bounds ignore-constraints] (let [modifiers (dm/get-in modif-tree [(:id parent) :modifiers])] ;; Move modifiers don't need to calculate constraints - (if (ctm/only-move? modifiers) + (cond + (ctm/empty? modifiers) + modif-tree + + (ctm/only-move? modifiers) (loop [modif-tree modif-tree children (seq children)] (if-let [current (first children)] @@ -109,6 +113,7 @@ modif-tree)) ;; Check the constraints, then resize + :else (let [parent-id (:id parent) parent-bounds (gtr/transform-bounds @(get bounds parent-id) (ctm/select-parent modifiers))] (loop [modif-tree modif-tree diff --git a/common/src/app/common/geom/shapes/points.cljc b/common/src/app/common/geom/shapes/points.cljc index 3722beb9e..348472bd2 100644 --- a/common/src/app/common/geom/shapes/points.cljc +++ b/common/src/app/common/geom/shapes/points.cljc @@ -91,6 +91,17 @@ :else 0))) +(defn axis-aligned? + "Check if the points are parallel to the coordinate axis." + [[p1 p2 _ p4 :as pts]] + (and (= (count pts) 4) + (let [hv (gpt/to-vec p1 p2) + vv (gpt/to-vec p1 p4)] + (and (mth/almost-zero? (:y hv)) + (mth/almost-zero? (:x vv)) + (> (:x hv) 0) + (> (:y vv) 0))))) + (defn parent-coords-bounds [child-bounds [p1 p2 _ p4 :as parent-bounds]]