a bit of refactoring for quad tree's 'toTriangles' function

This commit is contained in:
2025-07-06 13:26:33 +01:00
parent 1ce3a36db4
commit 833005703b

View File

@@ -213,7 +213,7 @@ struct
NODE {tl = tl, bl = bl, tr = tr, br = br}
end
fun foldWithDuplicates (f, tree, acc) =
fun foldWithDuplicates (f, tree, acc) =
case tree of
LEAF item => f (item, acc)
| NODE {tl, tr, bl, br} =>
@@ -226,34 +226,37 @@ struct
end
fun insertItemIntoTree (item, acc) =
if #data item = 0 then
if #data item = 0 then
(* ignore specific data by not inserting it into tree.
* May later functorise this quad tree,
* and allow different types of data
* to be ignored/stored in #data field. *)
acc
else
else
BinTree.insert (item, acc)
fun toList qtree =
let
val tree = foldWithDuplicates (insertItemIntoTree, qtree, BinTree.empty)
in
BinTree.toList (tree, [])
let val tree = foldWithDuplicates (insertItemIntoTree, qtree, BinTree.empty)
in BinTree.toList (tree, [])
end
fun toTriangles (windowWidth, windowHeight, squares, acc) =
case squares of
{x, y, ex, ey, data = _} :: tl =>
let
val startX = Ndc.fromPixelX (x, windowWidth, windowHeight)
val endX = Ndc.fromPixelX (ex, windowWidth, windowHeight)
val startY = Ndc.fromPixelY (y, windowWidth, windowHeight)
val endY = Ndc.fromPixelY (ey, windowWidth, windowHeight)
local
fun loop (windowWidth, windowHeight, squares, acc) =
case squares of
{x, y, ex, ey, data = _} :: tl =>
let
val startX = Ndc.fromPixelX (x, windowWidth, windowHeight)
val endX = Ndc.fromPixelX (ex, windowWidth, windowHeight)
val startY = Ndc.fromPixelY (y, windowWidth, windowHeight)
val endY = Ndc.fromPixelY (ey, windowWidth, windowHeight)
val acc = Ndc.ltrbToVertex (startX, startY, endX, endY) :: acc
in
toTriangles (windowWidth, windowHeight, tl, acc)
end
| [] => Vector.concat acc
val acc = Ndc.ltrbToVertex (startX, startY, endX, endY) :: acc
in
loop (windowWidth, windowHeight, tl, acc)
end
| [] => Vector.concat acc
in
fun toTriangles (windowWidth, windowHeight, squares) =
loop (windowWidth, windowHeight, squares, [])
end
end