add grid module, and change some functions in layer-tree.sml to use it instead of having grid-logic in there

This commit is contained in:
2025-08-09 08:43:51 +01:00
parent 6cd2b85d8b
commit e634bb25d7
4 changed files with 28 additions and 25 deletions

BIN
dotscape

Binary file not shown.

View File

@@ -1,6 +1,7 @@
$(SML_LIB)/basis/basis.mlb $(SML_LIB)/basis/basis.mlb
(* FUNCTIONAL CORE *) (* FUNCTIONAL CORE *)
fcore/grid.sml
fcore/layer-tree.sml fcore/layer-tree.sml
fcore/app-type.sml fcore/app-type.sml

23
fcore/grid.sml Normal file
View File

@@ -0,0 +1,23 @@
structure Grid =
struct
type square = {r: int, g: int, b: int, a: int}
type t = square vector vector
val emptyPixel = {r = 0, g = 0, b = 0, a = 0}
fun isBlank ({a, ...}: square) = a = 0
fun changeGridSize maxSide grid =
Vector.tabulate (maxSide, fn i =>
if i < Vector.length grid then
let
val yAxis = Vector.sub (grid, i)
in
Vector.tabulate (maxSide, fn ii =>
if ii < Vector.length yAxis then Vector.sub (yAxis, ii)
else emptyPixel)
end
else
Vector.tabulate (maxSide, fn _ => emptyPixel))
end

View File

@@ -1,19 +1,11 @@
structure LayerTree = structure LayerTree =
struct struct
type square = {r: int, g: int, b: int, a: int}
type grid = square vector vector
datatype tree = datatype tree =
NODE of {key: int, value: grid, left: tree, right: tree} NODE of {key: int, value: Grid.t, left: tree, right: tree}
| LEAF | LEAF
val emptyPixel = {r = 0, g = 0, b = 0, a = 0}
val emptyTree = LEAF val emptyTree = LEAF
fun isBlank ({a, ...}: square) = a = 0
fun insert (newKey, newValue, tree) = fun insert (newKey, newValue, tree) =
case tree of case tree of
LEAF => NODE {key = newKey, value = newValue, left = LEAF, right = LEAF} LEAF => NODE {key = newKey, value = newValue, left = LEAF, right = LEAF}
@@ -66,7 +58,7 @@ struct
(fn (xIdx, valueYAxis) => (fn (xIdx, valueYAxis) =>
Vector.mapi Vector.mapi
(fn (yIdx, valuePixel) => (fn (yIdx, valuePixel) =>
if isBlank valuePixel then if Grid.isBlank valuePixel then
let val accYAxis = Vector.sub (acc, xIdx) let val accYAxis = Vector.sub (acc, xIdx)
in Vector.sub (accYAxis, yIdx) in Vector.sub (accYAxis, yIdx)
end end
@@ -75,26 +67,13 @@ struct
fun makeEmptyGrid maxSide = fun makeEmptyGrid maxSide =
Vector.tabulate (maxSide, fn _ => Vector.tabulate (maxSide, fn _ =>
Vector.tabulate (maxSide, fn _ => emptyPixel)) Vector.tabulate (maxSide, fn _ => Grid.emptyPixel))
fun flatten (maxSide, tree) = fun flatten (maxSide, tree) =
foldl (helpFlatten, tree, makeEmptyGrid maxSide) foldl (helpFlatten, tree, makeEmptyGrid maxSide)
fun helpChangeGridSize maxSide squares =
Vector.tabulate (maxSide, fn i =>
if i < Vector.length squares then
let
val yAxis = Vector.sub (squares, i)
in
Vector.tabulate (maxSide, fn ii =>
if ii < Vector.length yAxis then Vector.sub (yAxis, ii)
else emptyPixel)
end
else
Vector.tabulate (maxSide, fn _ => emptyPixel))
fun changeGridSize (maxSide, tree) = fun changeGridSize (maxSide, tree) =
let val f = helpChangeGridSize maxSide let val f = Grid.changeGridSize maxSide
in map (f, tree) in map (f, tree)
end end
end end