diff --git a/frontend/src/arthur/domain/raster.cljs b/frontend/src/arthur/domain/raster.cljs index 288e249..fe63033 100644 --- a/frontend/src/arthur/domain/raster.cljs +++ b/frontend/src/arthur/domain/raster.cljs @@ -19,16 +19,21 @@ A raster may have an `:origin`, the stage point its first pixel is, and ops stay in stage pixels either way: the editor rasterises whatever part of the - work area is on screen, which need not be the stage." + work area is on screen, which need not be the stage. It may have a `:scale` + too, a whole number of its pixels to a stage pixel, which is how a lens sees + what is under it more finely than the stage does; see `warp!`." (:require [arthur.domain.warp :as warp])) (defn make [w h] {:w w :h h :buf (js/Uint8Array. (* w h))}) -(defn clear! [{:keys [buf cov] :as r} index] +(defn clear! + "`r` filled with `index`, which it remembers as what is under everything + drawn on it." + [{:keys [buf cov] :as r} index] (.fill buf index) (when cov (.fill cov 0)) - r) + (assoc r :bg index)) ;; --------------------------------------------------------------------------- ;; layers and knockouts @@ -97,15 +102,21 @@ `pts` may be a CLJS vector or any typed array; scanline crossings are collected into a plain JS array and sorted in place. `ink` is as `plot!`'s." ([r pts n index] (fill-poly-buf! r pts n index nil)) - ([{:keys [w h buf cov origin] :as r} pts n index ink] + ([{:keys [w h buf cov origin scale] :as r} pts n index ink] (when (>= n 3) (let [[ox oy] (or origin [0 0]) - px (fn [i] (- (if (vector? pts) (-nth pts (* 2 i)) (aget pts (* 2 i))) ox)) - py (fn [i] (- (if (vector? pts) (-nth pts (inc (* 2 i))) (aget pts (inc (* 2 i)))) oy)) + sc (or scale 1) + px (fn [i] (* sc (- (if (vector? pts) (-nth pts (* 2 i)) (aget pts (* 2 i))) ox))) + py (fn [i] (* sc (- (if (vector? pts) (-nth pts (inc (* 2 i))) (aget pts (inc (* 2 i)))) oy))) xs (array) ys (map py (range n)) y0 (max 0 (js/Math.ceil (- (reduce min ys) 0.5))) - y1 (min (dec h) (inc (js/Math.floor (- (reduce max ys) 0.5))))] + y1 (min (dec h) (inc (js/Math.floor (- (reduce max ys) 0.5)))) + ;; A shape wholly to one side of the raster has no span on any row + ;; of it, and need not be walked row by row to find that out: a lens + ;; redraws everything before it over a small box. + y1 (let [qs (map px (range n))] + (if (or (< (reduce max qs) 0) (> (reduce min qs) w)) -1 y1))] ;; `dotimes` over the span rather than a hand-rolled index: bounded ;; iteration with no accumulator is what it is for, and it compiles to the ;; same JS for-loop the recur did. @@ -163,9 +174,11 @@ clamp that would flatten the performance at the extremes." ([r cx cy rad index] (fill-disc! r cx cy rad index nil nil)) ([r cx cy rad index over] (fill-disc! r cx cy rad index over nil)) - ([{:keys [w h buf cov origin] :as r} cx cy rad index over ink] + ([{:keys [w h buf cov origin scale] :as r} cx cy rad index over ink] (let [[ox oy] (or origin [0 0]) - cx (- cx ox) cy (- cy oy) + sc (or scale 1) + cx (* sc (- cx ox)) cy (* sc (- cy oy)) + rad (* sc rad) rr (* rad rad) y0 (max 0 (js/Math.floor (- cy rad))) y1 (min (dec h) (js/Math.ceil (+ cy rad))) @@ -196,16 +209,21 @@ The top-left is rounded rather than the centre, so the block is size x size on every frame. Round the extents instead and a fractional centre gives you three pixels on one frame and four on the next, which reads as the pupil - breathing." + breathing. + + On a finer raster it is snapped to the STAGE's grid and then scaled, so a + lens shows the same block bigger, not a block of another size." ([r cx cy size index] (fill-rect! r cx cy size index nil nil)) ([r cx cy size index over] (fill-rect! r cx cy size index over nil)) - ([{:keys [w h buf cov origin] :as r} cx cy size index over ink] + ([{:keys [w h buf cov origin scale] :as r} cx cy size index over ink] (let [[ox oy] (or origin [0 0]) + sc (or scale 1) cx (- cx ox) cy (- cy oy) size (js/Math.round size)] (when (>= size 1) - (let [x0 (js/Math.round (- cx (/ size 2))) - y0 (js/Math.round (- cy (/ size 2))) + (let [x0 (* sc (js/Math.round (- cx (/ size 2)))) + y0 (* sc (js/Math.round (- cy (/ size 2)))) + size (* sc size) ya (max 0 y0) yb (min h (+ y0 size)) xa (max 0 x0) xb (min w (+ x0 size))] (dotimes [iy (- yb ya)] @@ -301,29 +319,67 @@ r) ;; One layer per depth of nesting, reused from frame to frame, as the resolver -;; reuses its point buffers. +;; reuses its point buffers — and one set per LEVEL, a lens's finer redraw of +;; what is under it being level 1, so the redraw never draws into a layer the +;; frame it is part of is still using. (defonce ^:private layers (atom {})) -(defn- layer-at [depth {:keys [w h origin]}] - (let [l (get @layers depth)] +(defn- layer-at [level depth {:keys [w h origin scale]}] + (let [l (get @layers [level depth])] (if (and l (= w (:w l)) (= h (:h l))) - (assoc l :origin origin) + (assoc l :origin origin :scale scale) (let [l (layer w h)] - (swap! layers assoc depth l) - (assoc l :origin origin))))) + (swap! layers assoc [level depth] l) + (assoc l :origin origin :scale scale))))) -;; The pixels a warp covers, and what each will show. Reused from op to op, as -;; the layers are, and grown only for a bigger raster: a warp costs what it -;; covers and allocates nothing. -(defonce ^:private scratch (volatile! {:covered (js/Int32Array. 1) :shown (js/Int32Array. 0)})) -(defonce ^:private at (js/Float64Array. 2)) +;; What a warp works with, per level, reused from op to op as the layers are +;; and grown only for a bigger raster: the pixels it covers, where each reads +;; from, and what each will show. A warp costs what it covers and allocates +;; nothing for that. +(defonce ^:private scratch (atom {})) + +(defn- scratch-at [level {:keys [w h]}] + (let [n (* w h) + sc (get @scratch level)] + (if (and sc (<= n (.-length (:shown sc)))) + sc + (let [sc {:covered (js/Int32Array. (inc n)) :src (js/Float64Array. (* 2 n)) + :shown (js/Int32Array. n) :at (js/Float64Array. 2)}] + (swap! scratch assoc level sc) + sc)))) (defn- covered-for - "The scratch a warp over raster `r` records its pixels into, emptied." - [{:keys [w h]}] - (when (< (.-length (:shown @scratch)) (* w h)) - (vreset! scratch {:covered (js/Int32Array. (inc (* w h))) :shown (js/Int32Array. (* w h))})) - (doto (:covered @scratch) (aset 0 0))) + "The scratch a warp over raster `r` at `level` records its pixels into, + emptied." + [level r] + (doto (:covered (scratch-at level r)) (aset 0 0))) + +;; How much finer a lens may see, and how many pixels its redraw may come to: +;; four stage rasters' worth, which a full-stage lens at 2x is exactly. +(def ^:private finest 4) +(def ^:private fine-budget (* 4 320 200)) + +;; The buffers a finer redraw draws into, grown and never shrunk. +(defonce ^:private fine-bufs (volatile! {:buf (js/Uint8Array. 0) :cov (js/Uint8Array. 0)})) + +(defn- fine-raster + "A raster `k` times finer than the stage over stage box `[x0 y0 x1 y1]`, for + a redraw of what is under a lens: a layer when `layer?`, otherwise filled + with `bg`, as the raster the lens is on was." + [k [x0 y0 x1 y1] layer? bg] + (let [ox (js/Math.floor x0) oy (js/Math.floor y0) + w (* k (inc (- (js/Math.ceil x1) ox))) + h (* k (inc (- (js/Math.ceil y1) oy))) + n (* w h)] + (when (< (.-length (:buf @fine-bufs)) n) + (vreset! fine-bufs {:buf (js/Uint8Array. n) :cov (js/Uint8Array. n)})) + (let [buf (.subarray (:buf @fine-bufs) 0 n) + cov (when layer? (.subarray (:cov @fine-bufs) 0 n))] + (.fill buf (or bg 0)) + (when cov (.fill cov 0)) + {:w w :h h :buf buf :cov cov :origin [ox oy] :scale k}))) + +(declare draw!) (defn- warp! "Draw each pixel the fill before this covered as what `field` says it shows, @@ -331,26 +387,60 @@ EVERY PIXEL IS READ BEFORE ANY IS WRITTEN. A pixel a lens moves is one some other pixel of the lens reads, so writing as it goes would magnify what it - had already magnified. Nothing outside what is covered is written, so a read - outside it sees the buffer as the warp found it. + had already magnified. Nothing outside what is covered is written. + + WHAT IT MAGNIFIES IS DRAWN AGAIN, FINER, not blown up. Magnifying the pixels + would show each one as a block and lose what the drawing has between them; + but what is under a lens is `under`, the ops drawn before it in this buffer, + and drawing them again `k` times finer over the box it reads from shows what + the shapes are at that size — the same picture as bending the shapes before + rasterising, at a fraction of the work. `k` is how much the field magnifies + where it magnifies most: one over the smallest step between what two + neighbouring pixels read. It is a whole number, at most `finest`, and small + enough that the redraw keeps to `fine-budget`; a warp that magnifies nothing + reads the buffer as it is. A redraw is level 1, and a lens in it reads its + own buffer rather than redrawing again. A point off the raster reads its nearest edge. In a layer what is shown includes whether anything is there: a warp moves a hole as it moves a mark." - [{:keys [w h ^js buf ^js cov origin] :as r} field] + [{:keys [w h ^js buf ^js cov origin scale bg] :as r} field level under] (let [[ox oy] (or origin [0 0]) - {:keys [^js covered ^js shown]} @scratch + sc (or scale 1) + {:keys [^js covered ^js src ^js shown ^js at]} (scratch-at level r) n (aget covered 0) - layer? (some? cov)] + layer? (some? cov) + box #js [js/Infinity js/Infinity (- js/Infinity) (- js/Infinity)] + step (volatile! js/Infinity)] ;; `js-mod` and a division rather than `rem` and `quot`, which are calls: ;; this is once per covered pixel. (dotimes [i n] (let [o (aget covered (inc i)) ix (js-mod o w)] - (field at (+ ox ix 0.5) (+ oy (/ (- o ix) w) 0.5)) - (let [x (max 0 (min (dec w) (js/Math.floor (- (aget at 0) ox)))) - y (max 0 (min (dec h) (js/Math.floor (- (aget at 1) oy)))) - s (+ (* y w) x)] - (aset shown i (if layer? (+ (aget buf s) (* 256 (aget cov s))) (aget buf s)))))) + (field at (+ ox (/ (+ ix 0.5) sc)) (+ oy (/ (+ (/ (- o ix) w) 0.5) sc))) + (let [x (aget at 0) y (aget at 1)] + (aset src (* 2 i) x) + (aset src (inc (* 2 i)) y) + (aset box 0 (min (aget box 0) x)) (aset box 1 (min (aget box 1) y)) + (aset box 2 (max (aget box 2) x)) (aset box 3 (max (aget box 3) y)) + (when (and (pos? i) (== o (inc (aget covered i)))) + (let [d (js/Math.hypot (- x (aget src (* 2 (dec i)))) (- y (aget src (dec (* 2 i)))))] + (when (< 1e-6 d @step) (vreset! step d))))))) + (let [area (* (- (aget box 2) (aget box 0) -2) (- (aget box 3) (aget box 1) -2)) + k (if (and under (pos? n) (< @step (/ 1 sc))) + (loop [k (min finest (js/Math.ceil (- (/ 1 @step) 1e-6)))] + (if (and (< 1 k) (< fine-budget (* area k k))) (recur (dec k)) k)) + 1) + ;; Where the lens reads from: the stage drawn again, finer, or the + ;; buffer as it is. + {fw :w fh :h ^js fbuf :buf ^js fcov :cov [fx fy] :origin fs :scale} + (if (< 1 k) + (draw! (fine-raster k (vec box) layer? bg) under 1) + {:w w :h h :buf buf :cov cov :origin [ox oy] :scale sc})] + (dotimes [i n] + (let [x (max 0 (min (dec fw) (js/Math.floor (* fs (- (aget src (* 2 i)) fx))))) + y (max 0 (min (dec fh) (js/Math.floor (* fs (- (aget src (inc (* 2 i))) fy))))) + s (+ (* y fw) x)] + (aset shown i (if layer? (+ (aget fbuf s) (* 256 (aget fcov s))) (aget fbuf s)))))) (dotimes [i n] (let [o (aget covered (inc i)) v (aget shown i)] @@ -372,26 +462,39 @@ `plot!`. A warp with no field — a kind there is none of — is drawn MAGENTA, as a colour the palette lacks is: loud, and not fatal." [r ops] - (reduce - (fn [stack {:keys [kind pts n color stencil cx cy size] :as op}] - (let [top (peek stack) - field (when (:warp op) (warp/field op)) - color (if (and (:warp op) (nil? field)) 255 color) - ink (if field (covered-for top) (or (:knock op) (:lut op)))] - (case kind - :begin (let [l (layer-at (count stack) r)] - (.fill (:cov l) 0) - (conj stack l)) - :end (if (< 1 (count stack)) - (let [s (pop stack)] (composite! (peek s) top) s) - stack) - (do (case kind - :poly (fill-poly-buf! top pts n color ink) - :disc (fill-disc! top cx cy (:r op) color stencil ink) - :rect (fill-rect! top cx cy size color stencil ink) - :mask (fill-mask! top (:mask op) color ink) - (throw (ex-info "draw op kind is not rasterisable" {:op (dissoc op :pts)}))) - (when field (warp! top field)) - stack)))) - [r] ops) + (draw! r ops 0)) + +(defn- draw! + "`draw-ops!` at `level`, which a lens's finer redraw of what is under it is + 1 of. Each buffer on the stack remembers where in `ops` it began, so a lens + knows which ops are under it: the ones before it in its own buffer." + [r ops level] + (let [ops (vec ops)] + (reduce + (fn [stack i] + (let [{:keys [kind pts n color stencil cx cy size] :as op} (nth ops i) + top (:r (peek stack)) + field (when (:warp op) (warp/field op)) + color (if (and (:warp op) (nil? field)) 255 color) + ink (if field (covered-for level top) (or (:knock op) (:lut op)))] + (case kind + :begin (let [l (layer-at level (count stack) r)] + (.fill (:cov l) 0) + (conj stack {:r l :from (inc i)})) + :end (if (< 1 (count stack)) + (let [s (pop stack)] (composite! (:r (peek s)) top) s) + stack) + (do (case kind + :poly (fill-poly-buf! top pts n color ink) + :disc (fill-disc! top cx cy (:r op) color stencil ink) + :rect (fill-rect! top cx cy size color stencil ink) + ;; A brush stroke's mask is a byte per stage pixel, and has + ;; nothing finer to show. + :mask (when (= 1 (or (:scale top) 1)) (fill-mask! top (:mask op) color ink)) + (throw (ex-info "draw op kind is not rasterisable" {:op (dissoc op :pts)}))) + (when field + (warp! top field level + (when (zero? level) (subvec ops (:from (peek stack)) i)))) + stack)))) + [{:r r :from 0}] (range (count ops)))) r) diff --git a/frontend/test/arthur/domain/warp_test.cljs b/frontend/test/arthur/domain/warp_test.cljs index 54f36a4..e7eabaf 100644 --- a/frontend/test/arthur/domain/warp_test.cljs +++ b/frontend/test/arthur/domain/warp_test.cljs @@ -54,6 +54,17 @@ (is (= [:lens] (pick/hit (:ops d) [12 12] [:lens])) "unless the glass is what is selected, which is how it is dragged"))) +(deftest a-lens-draws-what-is-under-it-finer-not-bigger + ;; A line half a pixel wide is one pixel on the stage, and twice the size it + ;; is one pixel still: magnifying the pixel would make it two. The lens's + ;; middle is over the line, where it magnifies twice. + (let [line [:line (sq 20.2 10 20.7 30) 4] + glass [:lens (sq 10 10 30 30) [:warp :lens]] + d (drawn (shapes wall line glass)) + across (row d 20 17 24)] + (is (= [2 2 2 2 4 2 2 2 2] (row (drawn (shapes wall line)) 20 16 25)) "one pixel without the lens") + (is (= 1 (count (filter #{4} across))) (str "and one under it: " across)))) + (deftest a-lens-bulges-and-has-no-seam ;; A round glass of radius 10 about (50, 50): magnified in the middle, as it ;; is at its rim and outside it, and squeezed in between.