Improve performance for constraints

This commit is contained in:
alonso.torres 2023-10-18 11:13:07 +02:00 committed by Andrey Antukh
parent 16694f005d
commit 06c8ada6f7
3 changed files with 48 additions and 16 deletions

View file

@ -314,21 +314,37 @@
(let [transformed-parent-bounds @transformed-parent-bounds (let [transformed-parent-bounds @transformed-parent-bounds
modifiers (ctm/select-child modifiers) 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) reset-modifiers?
child-points-after (gpo/parent-coords-bounds transformed-child-bounds transformed-parent-bounds) (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 modifiers
child-points-before parent-bounds (if reset-modifiers?
child-points-after transformed-parent-bounds) (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 transformed-child-bounds (if reset-modifiers?
child-points-before parent-bounds child-bounds
child-points-after transformed-parent-bounds)] (gtr/transform-bounds child-bounds modifiers))]
(-> modifiers
(ctm/add-modifiers modifiers-h) ;; If the parent is a layout we don't need to calculate its constraints. Finish
(ctm/add-modifiers modifiers-v)))))) ;; 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))))))))

View file

@ -100,7 +100,11 @@
[modif-tree children objects bounds parent transformed-parent-bounds ignore-constraints] [modif-tree children objects bounds parent transformed-parent-bounds ignore-constraints]
(let [modifiers (dm/get-in modif-tree [(:id parent) :modifiers])] (let [modifiers (dm/get-in modif-tree [(:id parent) :modifiers])]
;; Move modifiers don't need to calculate constraints ;; 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 (loop [modif-tree modif-tree
children (seq children)] children (seq children)]
(if-let [current (first children)] (if-let [current (first children)]
@ -109,6 +113,7 @@
modif-tree)) modif-tree))
;; Check the constraints, then resize ;; Check the constraints, then resize
:else
(let [parent-id (:id parent) (let [parent-id (:id parent)
parent-bounds (gtr/transform-bounds @(get bounds parent-id) (ctm/select-parent modifiers))] parent-bounds (gtr/transform-bounds @(get bounds parent-id) (ctm/select-parent modifiers))]
(loop [modif-tree modif-tree (loop [modif-tree modif-tree

View file

@ -91,6 +91,17 @@
:else :else
0))) 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 (defn parent-coords-bounds
[child-bounds [p1 p2 _ p4 :as parent-bounds]] [child-bounds [p1 p2 _ p4 :as parent-bounds]]