001 (ns walkmap.path
002 "Essentially the specification for things we shall consider to be path."
003 (:require [walkmap.polygon :refer [polygon?]]
004 [walkmap.vertex :refer [vertex?]]))
005
006 (defn path?
007 "True if `o` satisfies the conditions for a path. A path shall be a map
008 having the key `:nodes`, whose value shall be a sequence of vertices as
009 defined in `walkmap.vertex`."
010 [o]
011 (let
012 [v (:nodes o)]
013 (and
014 (seq? v)
015 (> (count v) 2)
016 (every? vertex? v)
017 (or (nil? (:kind o)) (= (:kind o) :path)))))
018
019 (defn make-path
020 [nodes]
021 (if
022 (every? vertex? nodes)
023 {:nodes nodes :id (keyword (gensym "path")) :kind :path}
024 (throw (Exception. "Each item on path must be a vertex."))))
025
026 (defn polygon->path
027 "If `o` is a polygon, return an equivalent path. What's different about
028 a path is that in polygons there is an implicit edge between the first
029 vertex and the last. In paths, there isn't, so we need to add that
030 edge explicitly.
031
032 If `o` is not a polygon, will throw an exception."
033 [o]
034 (if
035 (polygon? o)
036 (assoc (dissoc o :vertices) :kind :path :nodes (concat (:vertices o) (list (first (:vertices o)))))
037 (throw (Exception. "Not a polygon!"))))
038