001  (ns walkmap.core
002    "At this stage, primarily utility functions dealing with stereolithography
003    (STL) files. Not a stable API yet!"
004    (:require [clojure.java.io :as io :refer [file output-stream input-stream]]
005              [clojure.string :as s]
006              [hiccup.core :refer [html]]
007              [me.raynes.fs :as fs]
008              [taoensso.timbre :as l :refer [info error spy]]
009              [walkmap.stl :refer [decode-binary-stl]]
010              [walkmap.svg :refer [stl->svg]]))
011  
012  (def ^:dynamic *sea-level*
013    "The sea level on heightmaps we're currently handling. If characters are to
014    be able to swin in the sea, we must model the sea bottom, so we need
015    heightmaps which cover at least the continental shelf. However, the sea
016    bottom is not walkable territory and can be culled from walkmaps.
017  
018    **Note** must be a floating point number. `(= 0 0.0)` returns `false`!"
019    0.0)
020  
021  (defn ocean?
022    "Of a `facet`, is the altitude of every vertice equal to `*sea-level*`?"
023    [facet]
024    (every?
025      #(= % *sea-level*)
026      (map :z (:vertices facet))))
027  
028  (defn cull-ocean-facets
029    "Ye cannae walk on water. Remove all facets from this `stl` structure which
030    are at sea level."
031    [stl]
032    (assoc stl :facets (remove ocean? (:facets stl))))
033  
034  (defn binary-stl-file->svg
035    "Given only an `in-filename`, parse the indicated file, expected to be
036    binary STL, and return an equivalent SVG structure. Given both `in-filename`
037    and `out-filename`, as side-effect write the SVG to the indicated output file."
038    ([in-filename]
039     (stl->svg (cull-ocean-facets (decode-binary-stl in-filename))))
040    ([in-filename out-filename]
041     (let [s (binary-stl-file->svg in-filename)]
042       (spit out-filename (html s))
043       s)))
044