107 lines
5.5 KiB
Clojure
107 lines
5.5 KiB
Clojure
(ns arthur.domain.outline-test
|
||
(:require [cljs.test :refer [deftest is]]
|
||
[arthur.domain.outline :as outline]
|
||
[arthur.domain.raster :as r]))
|
||
|
||
(defn- covered [{:keys [buf]}] (count (filter #(= 1 %) (array-seq buf))))
|
||
|
||
(deftest a-traced-ring-fills-back-to-exactly-the-mask
|
||
(let [m (outline/stamp! (outline/mask 40 30) [8 8] [30 20] 6)
|
||
[ring & more] (outline/rings m)
|
||
back (r/fill-poly-buf! (r/make 40 30) ring (quot (count ring) 2) 1)]
|
||
(is (empty? more) "one stroke is one piece")
|
||
(is (= (vec (array-seq (:buf m))) (vec (array-seq (:buf back)))))))
|
||
|
||
(deftest a-single-pixel-is-a-square
|
||
(let [m (outline/mask 5 5)]
|
||
(aset (:buf m) 12 1)
|
||
(is (= [[2 2 3 2 3 3 2 3]] (outline/rings m 0)))))
|
||
|
||
(deftest pieces-are-traced-apart-biggest-first
|
||
(let [m (-> (outline/mask 40 20)
|
||
(outline/stamp! [5 10] [5 10] 4)
|
||
(outline/stamp! [20 10] [34 10] 6))]
|
||
(is (= 2 (count (outline/rings m))))
|
||
(is (< 8 (covered m)))
|
||
(is (< 15 (apply min (take-nth 2 (first (outline/rings m))))) "the long one first")))
|
||
|
||
(deftest a-fit-keeps-the-corners-and-straightens-the-stairs
|
||
(let [block (let [m (outline/mask 60 60)]
|
||
(doseq [y (range 10 40) x (range 10 50)] (aset (:buf m) (+ x (* y 60)) 1))
|
||
(first (outline/rings m)))
|
||
stairs (let [m (outline/mask 60 60)]
|
||
(doseq [y (range 5 55) x (range 5 (inc y))] (aset (:buf m) (+ x (* y 60)) 1))
|
||
(first (outline/rings m)))]
|
||
(is (= 8 (count (outline/fit block 1))) "a rectangle is its four corners")
|
||
(is (= 6 (count (outline/fit stairs 1))) "a pixel staircase is one diagonal: a triangle")
|
||
(is (< 6 (count (outline/fit stairs 0.25))) "unless asked to fit closer than the steps")))
|
||
|
||
(deftest a-loop-keeps-its-hole-in-one-ring
|
||
;; A ring of brush round a gap: one piece, one hole, and the one polygon it
|
||
;; makes fills back to the ring and leaves the gap empty.
|
||
(let [m (reduce (fn [m a]
|
||
(let [p #(vector (+ 30 (* 15 (js/Math.cos %))) (+ 30 (* 15 (js/Math.sin %))))]
|
||
(outline/stamp! m (p a) (p (+ a 0.3)) 5)))
|
||
(outline/mask 60 60) (range 0 6.6 0.3))
|
||
[piece & more] (outline/pieces m)
|
||
fill (fn [ring] (:buf (r/fill-poly-buf! (r/make 60 60) ring (quot (count ring) 2) 1)))
|
||
middle (+ 30 (* 30 60))]
|
||
(is (empty? more))
|
||
(is (= 1 (count (:holes piece))))
|
||
(is (= (vec (array-seq (:buf m))) (vec (array-seq (fill (outline/polygon piece 0.01)))))
|
||
"unsimplified, the bridged ring is the stroke exactly")
|
||
(is (zero? (aget (fill (outline/polygon piece 0.01)) middle)) "the middle is a hole")
|
||
(is (zero? (aget (fill (outline/polygon piece 8)) middle)) "and simplified, still a hole")))
|
||
|
||
(deftest overlapping-strokes-leave-no-slivers-and-nothing-crosses
|
||
;; A scribble back and forth over itself: the gaps between passes are not
|
||
;; holes anyone meant, and the one ring it makes fills back to the scribble
|
||
;; without a cut through it.
|
||
(let [m (reduce (fn [m i] (outline/stamp! m [10 (+ 10 (* 3 i))] [70 (+ 12 (* 3 i))] 5))
|
||
(outline/mask 80 60) (range 10))
|
||
[piece] (outline/pieces m)
|
||
ring (outline/polygon piece 1)
|
||
back (:buf (r/fill-poly-buf! (r/make 80 60) ring (quot (count ring) 2) 1))
|
||
miss (count (filter true? (map not= (array-seq (:buf m)) (array-seq back))))]
|
||
(is (< miss 80) (str miss " pixels differ — within a pixel of the edge, not a cut"))
|
||
(is (every? #(< (count %) 40) (outline/rings-of piece 1)) "and every ring is simple")))
|
||
|
||
(deftest a-gap-open-to-the-outside-is-not-a-hole
|
||
(let [m (-> (outline/mask 40 40)
|
||
(outline/stamp! [5 5] [30 5] 4)
|
||
(outline/stamp! [30 5] [30 30] 4)
|
||
(outline/stamp! [30 30] [5 30] 4))]
|
||
(is (= [] (:holes (first (outline/pieces m)))))))
|
||
|
||
(deftest a-small-hole-is-a-few-points-not-its-staircase
|
||
(let [m (outline/mask 60 60)]
|
||
(doseq [y (range 10 50) x (range 10 50)
|
||
:when (not (and (< 25 x 34) (< 25 y 34)))]
|
||
(aset (:buf m) (+ x (* y 60)) 1))
|
||
(let [[outer hole] (outline/rings-of (first (outline/pieces m)) 1)]
|
||
(is (= 8 (count outer)))
|
||
(is (= 8 (count hole)) "an 8×8 hole is its four corners"))))
|
||
|
||
(deftest a-hole-whose-fit-would-cross-the-outside-is-clipped-to-it
|
||
;; A wedge-shaped gap reaching to within a pixel of the outside: fitted on its
|
||
;; own, its edge can cross the outside's. It stays a hole, inside the body.
|
||
(let [m (outline/mask 60 60)]
|
||
(doseq [y (range 10 50) x (range 10 50)
|
||
:when (not (and (< 12 x 47) (< 12 y (- 47 (quot (- x 12) 3)))))]
|
||
(aset (:buf m) (+ x (* y 60)) 1))
|
||
(let [rs (outline/rings-of (first (outline/pieces m)) 3)
|
||
ring (outline/join rs)
|
||
back (:buf (r/fill-poly-buf! (r/make 60 60) ring (quot (count ring) 2) 1))]
|
||
(is (< 1 (count rs)) "the hole is kept")
|
||
(is (zero? (aget back (+ 25 (* 20 60)))) "and is a hole")
|
||
(is (every? #(< (count %) 30) rs) "and simple")
|
||
(is (zero? (aget back (+ 5 (* 5 60)))) "and nothing outside the body is filled"))))
|
||
|
||
(deftest a-brush-mask-can-live-outside-the-export-rectangle
|
||
(let [m (assoc (outline/mask 60 50) :origin [-20 -20])
|
||
normal (outline/stamp! (outline/mask 60 50) [10 12] [30 24] 6)
|
||
outside (outline/stamp! m [-10 -8] [10 4] 6)
|
||
shift (fn [ring] (mapv #(- % 20) ring))]
|
||
(is (= (vec (array-seq (:buf normal))) (vec (array-seq (:buf outside)))))
|
||
(is (= (mapv shift (outline/rings normal)) (outline/rings outside)))
|
||
(is (neg? (apply min (first (outline/rings outside)))))))
|