add functionality to change grid size for layer tree
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
structure LayerTree =
|
structure LayerTree =
|
||||||
struct
|
struct
|
||||||
type square = {r: int, g: int, b: int, a: int}
|
type square = {r: int, g: int, b: int, a: int}
|
||||||
|
val emptyPixel = {r = 0, g = 0, b = 0, a = 0}
|
||||||
|
|
||||||
type grid = square vector vector
|
type grid = square vector vector
|
||||||
|
|
||||||
fun isBlank ({a, ...}: square) = a = 0
|
fun isBlank ({a, ...}: square) = a = 0
|
||||||
@@ -32,6 +34,29 @@ struct
|
|||||||
else if searchKey > key then get (searchKey, right)
|
else if searchKey > key then get (searchKey, right)
|
||||||
else SOME value
|
else SOME value
|
||||||
|
|
||||||
|
fun foldl (f, tree, acc) =
|
||||||
|
case tree of
|
||||||
|
LEAF => acc
|
||||||
|
| NODE {value, left, right, ...} =>
|
||||||
|
let
|
||||||
|
val acc = foldl (f, left, acc)
|
||||||
|
val acc = f (value, acc)
|
||||||
|
in
|
||||||
|
foldl (f, right, acc)
|
||||||
|
end
|
||||||
|
|
||||||
|
fun map (f, tree) =
|
||||||
|
case tree of
|
||||||
|
LEAF => LEAF
|
||||||
|
| NODE {key, value, left, right} =>
|
||||||
|
let
|
||||||
|
val left = map (f, left)
|
||||||
|
val right = map (f, right)
|
||||||
|
val newValue = f value
|
||||||
|
in
|
||||||
|
NODE {key = key, value = value, left = left, right = right}
|
||||||
|
end
|
||||||
|
|
||||||
(* copies non-blank pixels in value vector into acc *)
|
(* copies non-blank pixels in value vector into acc *)
|
||||||
fun helpFlatten (value, acc) =
|
fun helpFlatten (value, acc) =
|
||||||
Vector.mapi
|
Vector.mapi
|
||||||
@@ -45,14 +70,28 @@ struct
|
|||||||
else
|
else
|
||||||
valuePixel) valueYAxis) value
|
valuePixel) valueYAxis) value
|
||||||
|
|
||||||
fun flatten (tree, acc) =
|
fun makeEmptyGrid maxSide =
|
||||||
case tree of
|
Vector.tabulate (maxSide, fn _ =>
|
||||||
LEAF => acc
|
Vector.tabulate (maxSide, fn _ => emptyPixel))
|
||||||
| NODE {value, left, right, ...} =>
|
|
||||||
|
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
|
let
|
||||||
val acc = flatten (left, acc)
|
val yAxis = Vector.sub (squares, i)
|
||||||
val acc = helpFlatten (value, acc)
|
|
||||||
in
|
in
|
||||||
flatten (right, acc)
|
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
|
||||||
|
in map (f, tree)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user