0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-02-12 18:18:24 -05:00

Performance improvements

This commit is contained in:
alonso.torres 2022-11-28 13:05:54 +01:00
parent 04243be4a5
commit 600f9ef071
2 changed files with 96 additions and 33 deletions

View file

@ -7,6 +7,7 @@
(ns app.common.types.modifiers (ns app.common.types.modifiers
(:refer-clojure :exclude [empty empty?]) (:refer-clojure :exclude [empty empty?])
(:require (:require
[app.common.perf :as perf]
[app.common.data :as d] [app.common.data :as d]
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.geom.matrix :as gmt] [app.common.geom.matrix :as gmt]
@ -217,15 +218,44 @@
:property property :property property
:value value}))) :value value})))
(defn- merge-geometry
[geometry other]
(cond
(c/empty? geometry)
other
(c/empty? other)
geometry
:else
(loop [result geometry
modifiers (seq other)]
(if (c/empty? modifiers)
result
(let [current (first modifiers)
result
(cond
(= :move (:type current))
(maybe-add-move result current)
(= :resize (:type current))
(maybe-add-resize result current)
:else
(conj result current))]
(recur result (rest modifiers)))))))
(defn add-modifiers (defn add-modifiers
[modifiers new-modifiers] [modifiers new-modifiers]
(cond-> modifiers (cond-> modifiers
(some? (:geometry-child new-modifiers)) (some? (:geometry-child new-modifiers))
(update :geometry-child #(d/concat-vec [] % (:geometry-child new-modifiers))) (update :geometry-child merge-geometry (:geometry-child new-modifiers))
(some? (:geometry-parent new-modifiers)) (some? (:geometry-parent new-modifiers))
(update :geometry-parent #(d/concat-vec [] % (:geometry-parent new-modifiers))) (update :geometry-parent merge-geometry (:geometry-parent new-modifiers))
(some? (:structure-parent new-modifiers)) (some? (:structure-parent new-modifiers))
(update :structure-parent #(d/concat-vec [] % (:structure-parent new-modifiers))) (update :structure-parent #(d/concat-vec [] % (:structure-parent new-modifiers)))
@ -426,39 +456,46 @@
(defn modifiers->transform (defn modifiers->transform
"Given a set of modifiers returns its transformation matrix" "Given a set of modifiers returns its transformation matrix"
[modifiers] [modifiers]
(letfn [(apply-modifier [matrix {:keys [type vector rotation center origin transform transform-inverse] :as modifier}]
(case type
:move
(gmt/multiply (gmt/translate-matrix vector) matrix)
:resize (let [modifiers
(let [origin (cond-> origin (if (d/not-empty? (:geometry-parent modifiers))
(or (some? transform-inverse)(some? transform)) (concat (:geometry-parent modifiers) (:geometry-child modifiers))
(gpt/transform transform-inverse))] (:geometry-child modifiers))]
(gmt/multiply
(-> (gmt/matrix)
(cond-> (some? transform)
(gmt/multiply transform))
(gmt/translate origin)
(gmt/scale vector)
(gmt/translate (gpt/negate origin))
(cond-> (some? transform-inverse)
(gmt/multiply transform-inverse)))
matrix))
:rotation (when (d/not-empty? modifiers)
(gmt/multiply (loop [matrix (gmt/matrix)
(-> (gmt/matrix) modifiers (seq modifiers)]
(gmt/translate center) (if (c/empty? modifiers)
(gmt/multiply (gmt/rotate-matrix rotation)) matrix
(gmt/translate (gpt/negate center))) (let [{:keys [type vector rotation center origin transform transform-inverse] :as modifier} (first modifiers)
matrix)))] matrix
(let [modifiers (if (d/not-empty? (:geometry-parent modifiers)) (case type
(d/concat-vec (:geometry-parent modifiers) (:geometry-child modifiers)) :move
(:geometry-child modifiers))] (gmt/multiply (gmt/translate-matrix vector) matrix)
(when (d/not-empty? modifiers)
(->> modifiers :resize
(reduce apply-modifier (gmt/matrix))))))) (let [origin (cond-> origin
(or (some? transform-inverse)(some? transform))
(gpt/transform transform-inverse))]
(gmt/multiply
(-> (gmt/matrix)
(cond-> (some? transform)
(gmt/multiply transform))
(gmt/translate origin)
(gmt/scale vector)
(gmt/translate (gpt/negate origin))
(cond-> (some? transform-inverse)
(gmt/multiply transform-inverse)))
matrix))
:rotation
(gmt/multiply
(-> (gmt/matrix)
(gmt/translate center)
(gmt/multiply (gmt/rotate-matrix rotation))
(gmt/translate (gpt/negate center)))
matrix))]
(recur matrix (rest modifiers))))))))
(defn apply-structure-modifiers (defn apply-structure-modifiers
"Apply structure changes to a shape" "Apply structure changes to a shape"

View file

@ -0,0 +1,26 @@
;; 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 INC
(ns common-tests.types-modifiers-test
(:require
[clojure.test :as t]
[app.common.geom.matrix :as gmt]
[app.common.geom.point :as gpt]
[app.common.types.modifiers :as ctm]))
(t/deftest test-modifiers->transform
(let [modifiers
(-> (ctm/empty)
(ctm/move (gpt/point 100 200))
(ctm/resize (gpt/point 100 200) (gpt/point 2.0 0.5))
(ctm/move (gpt/point -100 -200))
(ctm/resize (gpt/point 100 200) (gpt/point 2.0 0.5))
(ctm/rotation (gpt/point 0 0) -100)
(ctm/resize (gpt/point 100 200) (gpt/point 2.0 0.5)))
transform (ctm/modifiers->transform modifiers)]
(t/is (not (gmt/close? (gmt/matrix) transform)))))