001 (ns walkmap.svg
002 "Utility functions for writing stereolithography (STL) files (and possibly,
003 later, other geometry files of interest to us) as scalable vector graphics
004 (SVG)."
005 (:require [clojure.string :as s]
006 [taoensso.timbre :as l :refer [info error spy]]
007 [walkmap.polygon :refer [polygon?]]
008 [walkmap.vertex :refer [vertex?]]))
009
010 (defn- facet->svg-poly
011 [facet]
012 [:polygon
013 {:points (s/join " " (map #(str (:x %) "," (:y %)) (:vertices facet)))}])
014
015 (defn stl->svg
016 "Convert this in-memory `stl` structure, as read by `decode-binary-stl`, into
017 an in-memory hiccup representation of SVG structure, and return it."
018 [stl]
019 (let [minx (reduce
020 min
021 (map
022 #(reduce min (map :x (:vertices %)))
023 (:facets stl)))
024 maxx (reduce
025 max
026 (map
027 #(reduce max (map :x (:vertices %)))
028 (:facets stl)))
029 miny (reduce
030 min
031 (map
032 #(reduce min (map :y (:vertices %)))
033 (:facets stl)))
034 maxy (reduce
035 max
036 (map
037 #(reduce max (map :y (:vertices %)))
038 (:facets stl)))]
039 [:svg
040 {:xmlns "http://www.w3.org/2000/svg"
041 :version "1.2"
042 :width (- maxx minx)
043 :height (- maxy miny)
044 :viewBox (s/join " " (map str [minx miny maxx maxy]))}
045 (vec
046 (cons
047 :g
048 (map
049 facet->svg-poly
050 (:facets stl))))]))