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:
@@ -1,6 +1,7 @@
|
||||
$(SML_LIB)/basis/basis.mlb
|
||||
|
||||
(* FUNCTIONAL CORE *)
|
||||
fcore/grid.sml
|
||||
fcore/layer-tree.sml
|
||||
fcore/app-type.sml
|
||||
|
||||
|
||||
23
fcore/grid.sml
Normal file
23
fcore/grid.sml
Normal 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
|
||||
@@ -1,19 +1,11 @@
|
||||
structure LayerTree =
|
||||
struct
|
||||
type square = {r: int, g: int, b: int, a: int}
|
||||
|
||||
type grid = square vector vector
|
||||
|
||||
datatype tree =
|
||||
NODE of {key: int, value: grid, left: tree, right: tree}
|
||||
NODE of {key: int, value: Grid.t, left: tree, right: tree}
|
||||
| LEAF
|
||||
|
||||
val emptyPixel = {r = 0, g = 0, b = 0, a = 0}
|
||||
|
||||
val emptyTree = LEAF
|
||||
|
||||
fun isBlank ({a, ...}: square) = a = 0
|
||||
|
||||
fun insert (newKey, newValue, tree) =
|
||||
case tree of
|
||||
LEAF => NODE {key = newKey, value = newValue, left = LEAF, right = LEAF}
|
||||
@@ -66,7 +58,7 @@ struct
|
||||
(fn (xIdx, valueYAxis) =>
|
||||
Vector.mapi
|
||||
(fn (yIdx, valuePixel) =>
|
||||
if isBlank valuePixel then
|
||||
if Grid.isBlank valuePixel then
|
||||
let val accYAxis = Vector.sub (acc, xIdx)
|
||||
in Vector.sub (accYAxis, yIdx)
|
||||
end
|
||||
@@ -75,26 +67,13 @@ struct
|
||||
|
||||
fun makeEmptyGrid maxSide =
|
||||
Vector.tabulate (maxSide, fn _ =>
|
||||
Vector.tabulate (maxSide, fn _ => emptyPixel))
|
||||
Vector.tabulate (maxSide, fn _ => Grid.emptyPixel))
|
||||
|
||||
fun flatten (maxSide, tree) =
|
||||
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) =
|
||||
let val f = helpChangeGridSize maxSide
|
||||
let val f = Grid.changeGridSize maxSide
|
||||
in map (f, tree)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user