From e4f2dfaa11f9ad8e7a6ea1da57e570d15076332d Mon Sep 17 00:00:00 2001 From: Miguel de Benito Delgado Date: Wed, 21 May 2025 21:29:40 +0200 Subject: [PATCH 1/2] :sparkles: Make precision closest point computation depend on zoom --- common/src/app/common/types/path/segment.cljc | 15 +++++++-------- .../app/main/ui/workspace/shapes/path/editor.cljs | 2 +- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/common/src/app/common/types/path/segment.cljc b/common/src/app/common/types/path/segment.cljc index bf8184f03..773772afe 100644 --- a/common/src/app/common/types/path/segment.cljc +++ b/common/src/app/common/types/path/segment.cljc @@ -182,11 +182,11 @@ ;; FIXME: move to helpers?, this function need performance review, it ;; is executed so many times on path edition (defn- curve-closest-point - [position start end h1 h2] + [position start end h1 h2 precision] (let [d (memoize (fn [t] (gpt/distance position (helpers/curve-values start end h1 h2 t))))] (loop [t1 0.0 t2 1.0] - (if (<= (mth/abs (- t1 t2)) path-closest-point-accuracy) + (if (<= (mth/abs (- t1 t2)) precision) (-> (helpers/curve-values start end h1 h2 t1) ;; store the segment info (with-meta {:t t1 :from-p start :to-p end})) @@ -214,7 +214,7 @@ (double t2))))))) (defn- line-closest-point - "Point on line" + "Finds the closest point in the line segment defined by from-p and to-p" [position from-p to-p] (let [e1 (gpt/to-vec from-p to-p) @@ -274,13 +274,12 @@ (defn closest-point - "Given a path and a position" - [content position] - + "Returns the closest point in the path to the position, at a given precision" + [content position precision] (let [point+distance (fn [[cur-segment prev-segment]] (let [from-p (helpers/segment->point prev-segment) - to-p (helpers/segment->point cur-segment) + to-p (helpers/segment->point cur-segment) h1 (gpt/point (get-in cur-segment [:params :c1x]) (get-in cur-segment [:params :c1y])) h2 (gpt/point (get-in cur-segment [:params :c2x]) @@ -291,7 +290,7 @@ (line-closest-point position from-p to-p) :curve-to - (curve-closest-point position from-p to-p h1 h2) + (curve-closest-point position from-p to-p h1 h2 precision) nil)] (when point diff --git a/frontend/src/app/main/ui/workspace/shapes/path/editor.cljs b/frontend/src/app/main/ui/workspace/shapes/path/editor.cljs index 1c8e91fcb..dede7d331 100644 --- a/frontend/src/app/main/ui/workspace/shapes/path/editor.cljs +++ b/frontend/src/app/main/ui/workspace/shapes/path/editor.cljs @@ -348,7 +348,7 @@ ms/mouse-position (mf/deps base-content zoom) (fn [position] - (when-let [point (path.segment/closest-point base-content position)] + (when-let [point (path.segment/closest-point base-content position (/ 0.01 zoom))] (reset! hover-point (when (< (gpt/distance position point) (/ 10 zoom)) point))))) [:g.path-editor {:ref editor-ref} From 8f7a67400047f99c07e5e319e5720900320dd125 Mon Sep 17 00:00:00 2001 From: Miguel de Benito Delgado Date: Wed, 21 May 2025 21:32:18 +0200 Subject: [PATCH 2/2] :fire: Remove unused fn types.path.segment.path-closest-point --- common/src/app/common/types/path/segment.cljc | 38 ------------------- 1 file changed, 38 deletions(-) diff --git a/common/src/app/common/types/path/segment.cljc b/common/src/app/common/types/path/segment.cljc index 773772afe..d446495ec 100644 --- a/common/src/app/common/types/path/segment.cljc +++ b/common/src/app/common/types/path/segment.cljc @@ -235,44 +235,6 @@ from-p to-p)))) -;; FIXME: incorrect API, complete shape is not necessary here -(defn path-closest-point - "Given a path and a position" - [shape position] - - (let [point+distance - (fn [[cur-segment prev-segment]] - (let [from-p (helpers/segment->point prev-segment) - to-p (helpers/segment->point cur-segment) - h1 (gpt/point (get-in cur-segment [:params :c1x]) - (get-in cur-segment [:params :c1y])) - h2 (gpt/point (get-in cur-segment [:params :c2x]) - (get-in cur-segment [:params :c2y])) - point - (case (:command cur-segment) - :line-to - (line-closest-point position from-p to-p) - - :curve-to - (curve-closest-point position from-p to-p h1 h2) - - nil)] - (when point - [point (gpt/distance point position)]))) - - find-min-point - (fn [[min-p min-dist :as acc] [cur-p cur-dist :as cur]] - (if (and (some? acc) (or (not cur) (<= min-dist cur-dist))) - [min-p min-dist] - [cur-p cur-dist]))] - - (->> (:content shape) - (d/with-prev) - (map point+distance) - (reduce find-min-point) - (first)))) - - (defn closest-point "Returns the closest point in the path to the position, at a given precision" [content position precision]