Replace duplicate fn get-point with segment->point

This commit is contained in:
Andrey Antukh 2025-04-09 08:49:09 +02:00
parent f3c3f3e2d8
commit 1d0020f6e6
7 changed files with 46 additions and 49 deletions

View file

@ -14,12 +14,21 @@
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.math :as mth] [app.common.math :as mth]
[app.common.svg :as csvg] [app.common.svg :as csvg]
[app.common.types.path.segment :as path.segm] [app.common.types.path.helpers :as path.helpers]
[app.common.types.path.segment :as path.segment]
[cuerdas.core :as str])) [cuerdas.core :as str]))
(def commands-regex #"(?i)[mzlhvcsqta][^mzlhvcsqta]*") (def commands-regex #"(?i)[mzlhvcsqta][^mzlhvcsqta]*")
(def regex #"[+-]?(\d+(\.\d+)?|\.\d+)(e[+-]?\d+)?") (def regex #"[+-]?(\d+(\.\d+)?|\.\d+)(e[+-]?\d+)?")
(defn- get-point
"Get a point for a segment"
[prev-pos {:keys [relative params] :as segment}]
(let [{:keys [x y] :or {x (:x prev-pos) y (:y prev-pos)}} params]
(if relative
(-> prev-pos (update :x + x) (update :y + y))
(path.helpers/segment->point segment))))
(defn extract-params (defn extract-params
[data pattern] [data pattern]
(loop [result [] (loop [result []
@ -184,7 +193,7 @@
(defn smooth->curve (defn smooth->curve
[{:keys [params]} pos handler] [{:keys [params]} pos handler]
(let [{c1x :x c1y :y} (path.segm/calculate-opposite-handler pos handler)] (let [{c1x :x c1y :y} (path.segment/calculate-opposite-handler pos handler)]
{:c1x c1x {:c1x c1x
:c1y c1y :c1y c1y
:c2x (:cx params) :c2x (:cx params)
@ -412,7 +421,7 @@
(= :smooth-quadratic-bezier-curve-to (:command command)) (= :smooth-quadratic-bezier-curve-to (:command command))
(-> (assoc :command :curve-to) (-> (assoc :command :curve-to)
(update :params merge (quadratic->curve prev-pos (gpt/point params) (path.segm/calculate-opposite-handler prev-pos prev-qc))))) (update :params merge (quadratic->curve prev-pos (gpt/point params) (path.segment/calculate-opposite-handler prev-pos prev-qc)))))
result (if (= :elliptical-arc (:command command)) result (if (= :elliptical-arc (:command command))
(into result (arc->beziers prev-pos command)) (into result (arc->beziers prev-pos command))
@ -435,13 +444,13 @@
(gpt/point (get-in orig-command [:params :cx]) (get-in orig-command [:params :cy])) (gpt/point (get-in orig-command [:params :cx]) (get-in orig-command [:params :cy]))
:smooth-quadratic-bezier-curve-to :smooth-quadratic-bezier-curve-to
(path.segm/calculate-opposite-handler prev-pos prev-qc) (path.segment/calculate-opposite-handler prev-pos prev-qc)
(gpt/point (get-in orig-command [:params :x]) (get-in orig-command [:params :y]))) (gpt/point (get-in orig-command [:params :x]) (get-in orig-command [:params :y])))
next-pos (if (= :close-path (:command command)) next-pos (if (= :close-path (:command command))
prev-start prev-start
(path.segm/get-point prev-pos command)) (get-point prev-pos command))
next-start (if (= :move-to (:command command)) next-pos prev-start)] next-start (if (= :move-to (:command command)) next-pos prev-start)]

View file

@ -18,19 +18,6 @@
#?(:clj (set! *warn-on-reflection* true)) #?(:clj (set! *warn-on-reflection* true))
(defn get-point
"Get a point for a segment"
([prev-pos {:keys [relative params] :as segment}]
(let [{:keys [x y] :or {x (:x prev-pos) y (:y prev-pos)}} params]
(if relative
(-> prev-pos (update :x + x) (update :y + y))
(get-point segment))))
([segment]
(when segment
(let [{:keys [x y]} (:params segment)]
(gpt/point x y)))))
(defn update-handler (defn update-handler
[command prefix point] [command prefix point]
(let [[cox coy] (if (= prefix :c1) [:c1x :c1y] [:c2x :c2y])] (let [[cox coy] (if (= prefix :c1) [:c1x :c1y] [:c2x :c2y])]
@ -57,8 +44,8 @@
(d/enumerate) (d/enumerate)
(mapcat (fn [[index [cur-cmd pre-cmd]]] (mapcat (fn [[index [cur-cmd pre-cmd]]]
(if (and pre-cmd (= :curve-to (:command cur-cmd))) (if (and pre-cmd (= :curve-to (:command cur-cmd)))
(let [cur-pos (get-point cur-cmd) (let [cur-pos (helpers/segment->point cur-cmd)
pre-pos (get-point pre-cmd)] pre-pos (helpers/segment->point pre-cmd)]
(-> [[pre-pos [index :c1]] (-> [[pre-pos [index :c1]]
[cur-pos [index :c2]]])) [cur-pos [index :c2]]]))
[]))) [])))
@ -69,7 +56,7 @@
(defn point-indices (defn point-indices
[content point] [content point]
(->> (d/enumerate content) (->> (d/enumerate content)
(filter (fn [[_ cmd]] (= point (get-point cmd)))) (filter (fn [[_ cmd]] (= point (helpers/segment->point cmd))))
(mapv (fn [[index _]] index)))) (mapv (fn [[index _]] index))))
(defn handler-indices (defn handler-indices
@ -79,8 +66,8 @@
(d/enumerate) (d/enumerate)
(mapcat (fn [[index [cur-cmd pre-cmd]]] (mapcat (fn [[index [cur-cmd pre-cmd]]]
(if (and (some? pre-cmd) (= :curve-to (:command cur-cmd))) (if (and (some? pre-cmd) (= :curve-to (:command cur-cmd)))
(let [cur-pos (get-point cur-cmd) (let [cur-pos (helpers/segment->point cur-cmd)
pre-pos (get-point pre-cmd)] pre-pos (helpers/segment->point pre-cmd)]
(cond-> [] (cond-> []
(= pre-pos point) (conj [index :c1]) (= pre-pos point) (conj [index :c1])
(= cur-pos point) (conj [index :c2]))) (= cur-pos point) (conj [index :c2])))
@ -91,8 +78,8 @@
[content index prefix] [content index prefix]
(let [point (if (= prefix :c2) (let [point (if (= prefix :c2)
(get-point (nth content index)) (helpers/segment->point (nth content index))
(get-point (nth content (dec index)))) (helpers/segment->point (nth content (dec index))))
point->handlers (content->handlers content) point->handlers (content->handlers content)
@ -113,7 +100,7 @@
"Returns the commands involving a point with its indices" "Returns the commands involving a point with its indices"
[content point] [content point]
(->> (d/enumerate content) (->> (d/enumerate content)
(filterv (fn [[_ cmd]] (= (get-point cmd) point))))) (filterv (fn [[_ cmd]] (= (helpers/segment->point cmd) point)))))
;; FIXME: candidate to be optimized with native data type operation ;; FIXME: candidate to be optimized with native data type operation
(defn handler->point (defn handler->point
@ -135,8 +122,8 @@
(defn handler->node (defn handler->node
[content index prefix] [content index prefix]
(if (= prefix :c1) (if (= prefix :c1)
(get-point (nth content (dec index))) (helpers/segment->point (nth content (dec index)))
(get-point (nth content index)))) (helpers/segment->point (nth content index))))
(defn calculate-opposite-handler (defn calculate-opposite-handler
"Given a point and its handler, gives the symmetric handler" "Given a point and its handler, gives the symmetric handler"
@ -370,8 +357,8 @@
process-command process-command
(fn [content [index [command prev]]] (fn [content [index [command prev]]]
(let [cur-point (get-point command) (let [cur-point (helpers/segment->point command)
pre-point (get-point prev) pre-point (helpers/segment->point prev)
handler-c1 (get-handler command :c1) handler-c1 (get-handler command :c1)
handler-c2 (get-handler command :c2)] handler-c2 (get-handler command :c2)]
(if (and (= :curve-to (:command command)) (if (and (= :curve-to (:command command))
@ -403,7 +390,7 @@
(defn- line->curve (defn- line->curve
[from-p segment] [from-p segment]
(let [to-p (get-point segment) (let [to-p (helpers/segment->point segment)
v (gpt/to-vec from-p to-p) v (gpt/to-vec from-p to-p)
d (gpt/distance from-p to-p) d (gpt/distance from-p to-p)
@ -459,10 +446,10 @@
{:index index {:index index
:prev-i (when (some? prev) prev-i) :prev-i (when (some? prev) prev-i)
:prev-c prev :prev-c prev
:prev-p (get-point prev) :prev-p (helpers/segment->point prev)
:next-i (when (some? next) next-i) :next-i (when (some? next) next-i)
:next-c next :next-c next
:next-p (get-point next) :next-p (helpers/segment->point next)
:segment segment})) :segment segment}))
indices) indices)
@ -541,7 +528,7 @@
;; Close-path makes a segment from the last point to the initial path point ;; Close-path makes a segment from the last point to the initial path point
cur-point (if close-path? cur-point (if close-path?
start-point start-point
(get-point cur-cmd)) (helpers/segment->point cur-cmd))
;; If there is a move-to we don't have a segment ;; If there is a move-to we don't have a segment
prev-point (if move-to? prev-point (if move-to?
@ -646,10 +633,10 @@
subpath (peek result) subpath (peek result)
point (get-point cur-cmd) point (helpers/segment->point cur-cmd)
old-prev-point (get-point prev-cmd) old-prev-point (helpers/segment->point prev-cmd)
new-prev-point (get-point (peek subpath)) new-prev-point (helpers/segment->point (peek subpath))
remove? (contains? points point) remove? (contains? points point)
@ -724,8 +711,8 @@
(flatten) (flatten)
(into [])) (into []))
(let [prev-point (get-point prev-cmd) (let [prev-point (helpers/segment->point prev-cmd)
cur-point (get-point cur-cmd) cur-point (helpers/segment->point cur-cmd)
cur-cmd (cond-> cur-cmd cur-cmd (cond-> cur-cmd
(and (contains? points prev-point) (and (contains? points prev-point)
@ -803,7 +790,7 @@
[content point->merge-point] [content point->merge-point]
(let [replace-command (let [replace-command
(fn [cmd] (fn [cmd]
(let [point (get-point cmd)] (let [point (helpers/segment->point cmd)]
(if (contains? point->merge-point point) (if (contains? point->merge-point point)
(let [merge-point (get point->merge-point point)] (let [merge-point (get point->merge-point point)]
(-> cmd (update :params assoc :x (:x merge-point) :y (:y merge-point)))) (-> cmd (update :params assoc :x (:x merge-point) :y (:y merge-point))))

View file

@ -216,7 +216,7 @@
(butlast content) (butlast content)
content) content)
command (first content) command (first content)
to-p (path.helpers/command->point command) to-p (path.helpers/segment->point command)
[from-p move-p command-pts] [from-p move-p command-pts]
(case (:command command) (case (:command command)
@ -224,8 +224,8 @@
:close-path [move-p move-p (when move-p [move-p])] :close-path [move-p move-p (when move-p [move-p])]
:line-to [to-p move-p (when (and from-p to-p) [from-p to-p])] :line-to [to-p move-p (when (and from-p to-p) [from-p to-p])]
:curve-to [to-p move-p :curve-to [to-p move-p
(let [c1 (path.helpers/command->point command :c1) (let [c1 (path.helpers/segment->point command :c1)
c2 (path.helpers/command->point command :c2) c2 (path.helpers/segment->point command :c2)
curve [from-p to-p c1 c2]] curve [from-p to-p c1 c2]]
(when (and from-p to-p c1 c2) (when (and from-p to-p c1 c2)
(into [from-p to-p] (into [from-p to-p]

View file

@ -76,7 +76,7 @@
index (or index (count content)) index (or index (count content))
prefix (or prefix :c1) prefix (or prefix :c1)
position (or position (path.segment/get-point (nth content (dec index)))) position (or position (path.helpers/segment->point (nth content (dec index))))
old-handler (path.segment/handler->point content index prefix) old-handler (path.segment/handler->point content index prefix)

View file

@ -260,7 +260,7 @@
content (st/get-path state :content) content (st/get-path state :content)
points (path.segment/get-points content) points (path.segment/get-points content)
point (-> content (nth (if (= prefix :c1) (dec index) index)) (path.segment/get-point)) point (-> content (nth (if (= prefix :c1) (dec index) index)) (path.helpers/segment->point))
handler (-> content (nth index) (path.segment/get-handler prefix)) handler (-> content (nth index) (path.segment/get-handler prefix))
[op-idx op-prefix] (path.segment/opposite-index content index prefix) [op-idx op-prefix] (path.segment/opposite-index content index prefix)

View file

@ -10,6 +10,7 @@
[app.common.data.macros :as dm] [app.common.data.macros :as dm]
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.types.path :as path] [app.common.types.path :as path]
[app.common.types.path.helpers :as path.helpers]
[app.common.types.path.segment :as path.segment] [app.common.types.path.segment :as path.segment]
[app.main.data.workspace.path :as drp] [app.main.data.workspace.path :as drp]
[app.main.snap :as snap] [app.main.snap :as snap]
@ -299,7 +300,7 @@
(mf/with-memo [content-points] (mf/with-memo [content-points]
(into #{} content-points)) (into #{} content-points))
last-p (->> content last path.segment/get-point) last-p (->> content last path.helpers/segment->point)
handlers handlers
(mf/with-memo [content] (mf/with-memo [content]
@ -422,7 +423,7 @@
(let [[snap-selected snap-points] (let [[snap-selected snap-points]
(cond (cond
(some? drag-handler) [#{drag-handler} points] (some? drag-handler) [#{drag-handler} points]
(some? preview) [#{(path.segment/get-point preview)} points] (some? preview) [#{(path.helpers/segment->point preview)} points]
(some? moving-handler) [#{moving-handler} points] (some? moving-handler) [#{moving-handler} points]
:else :else
[(->> selected-points (map base->point) (into #{})) [(->> selected-points (map base->point) (into #{}))

View file

@ -11,7 +11,7 @@
WARNING: Pending to be removed from codebase once completly unused" WARNING: Pending to be removed from codebase once completly unused"
(:require (:require
[app.common.geom.point :as gpt] [app.common.geom.point :as gpt]
[app.common.types.path.segment :as path.segm] [app.common.types.path.helpers :refer [segment->point]]
[app.util.array :as arr])) [app.util.array :as arr]))
(defn pt= (defn pt=
@ -122,7 +122,7 @@
(try (try
(let [result (make-array (count content))] (let [result (make-array (count content))]
(reduce (fn [last-move current] (reduce (fn [last-move current]
(let [point (path.segm/get-point current) (let [point (segment->point current)
current-move? (= :move-to (:command current)) current-move? (= :move-to (:command current))
last-move (if current-move? point last-move)] last-move (if current-move? point last-move)]