2024-08-28 20:42:52 +01:00
|
|
|
signature FILE_THREAD =
|
|
|
|
|
sig
|
2024-08-29 05:38:58 +01:00
|
|
|
val run: FileMessage.t Mailbox.mbox * InputMessage.t Mailbox.mbox -> unit
|
2024-08-28 20:42:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
structure FileThread :> FILE_THREAD =
|
|
|
|
|
struct
|
|
|
|
|
open FileMessage
|
2024-08-29 05:38:58 +01:00
|
|
|
open InputMessage
|
2024-08-28 20:42:52 +01:00
|
|
|
|
2024-08-29 09:55:08 +01:00
|
|
|
datatype parse_result = OK of AppType.triangle list | PARSE_ERROR
|
2024-08-29 04:39:23 +01:00
|
|
|
|
2024-08-30 02:22:02 +01:00
|
|
|
val structureName = "LowerCaseA"
|
2024-08-29 09:55:08 +01:00
|
|
|
val filename = "a.dsc"
|
2024-08-30 02:22:02 +01:00
|
|
|
val exportFilename = "a.sml"
|
|
|
|
|
|
|
|
|
|
datatype dir = X | Y
|
|
|
|
|
|
|
|
|
|
fun ndcToLerp (num, dir) =
|
|
|
|
|
let
|
|
|
|
|
val num = (num + 1.0) / 2.0
|
|
|
|
|
val num = Real32.toString num
|
|
|
|
|
in
|
|
|
|
|
case dir of
|
|
|
|
|
X =>
|
|
|
|
|
" ((startX * (1.0 - " ^ num ^ ")) + (endX * " ^ num ^ ")) / windowWidth"
|
|
|
|
|
| Y =>
|
|
|
|
|
" ((startY * (1.0 - " ^ num ^ ")) + (endY * " ^ num ^ ")) / windowHeight"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
fun helpExportTriangles (io, triangles) =
|
|
|
|
|
case triangles of
|
|
|
|
|
{x1, y1, x2, y2, x3, y3} :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val x1 = ndcToLerp (x1, X)
|
|
|
|
|
val x2 = ndcToLerp (x2, X)
|
|
|
|
|
val x3 = ndcToLerp (x3, X)
|
|
|
|
|
|
|
|
|
|
val y1 = ndcToLerp (y1, Y)
|
|
|
|
|
val y2 = ndcToLerp (y2, Y)
|
|
|
|
|
val y3 = ndcToLerp (y3, Y)
|
|
|
|
|
|
|
|
|
|
val line = String.concat
|
|
|
|
|
[ x1, ",\n", y1, ",\n"
|
|
|
|
|
, x2, ",\n", y2, ",\n"
|
|
|
|
|
, x3, ",\n", y3
|
|
|
|
|
, case tl of [] => "\n" | _ => ",\n"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
val _ = TextIO.output (io, line)
|
|
|
|
|
in
|
|
|
|
|
helpExportTriangles (io, tl)
|
|
|
|
|
end
|
|
|
|
|
| [] => ()
|
|
|
|
|
|
|
|
|
|
fun exportTriangles triangles =
|
|
|
|
|
let
|
|
|
|
|
val io = TextIO.openOut exportFilename
|
|
|
|
|
|
|
|
|
|
val fileStartString =
|
|
|
|
|
String.concat ["structure ", structureName, " =\nstruct\n"]
|
|
|
|
|
val _ = TextIO.output (io, fileStartString)
|
|
|
|
|
|
|
|
|
|
val functionStartString =
|
|
|
|
|
" fun lerp (startX, startY, drawWidth, drawHeight, windowWidth, windowHeight) : Real32.real vector =\n\
|
|
|
|
|
\ let\n\
|
|
|
|
|
\ val startX = Real32.fromInt startX\n\
|
|
|
|
|
\ val startY = Real32.fromInt startY\n\
|
|
|
|
|
\ val endX = startX + drawWidth\n\
|
|
|
|
|
\ val endY = startY + drawHeight\n\
|
|
|
|
|
\ in\n\
|
|
|
|
|
\ #["
|
|
|
|
|
val _ = TextIO.output (io, functionStartString)
|
|
|
|
|
|
|
|
|
|
val _ = helpExportTriangles (io, triangles)
|
|
|
|
|
|
|
|
|
|
val _ = TextIO.output (io, " ]\n end\nend")
|
|
|
|
|
val _ = TextIO.closeOut io
|
|
|
|
|
in
|
|
|
|
|
()
|
|
|
|
|
end
|
2024-08-29 04:39:23 +01:00
|
|
|
|
2024-08-29 05:21:04 +01:00
|
|
|
fun parse (io, acc) =
|
2024-08-29 04:39:23 +01:00
|
|
|
case TextIO.inputLine io of
|
|
|
|
|
SOME line =>
|
2024-08-29 05:21:04 +01:00
|
|
|
let
|
2024-08-29 09:55:08 +01:00
|
|
|
val line = ParseFile.parseLine line
|
2024-08-29 05:21:04 +01:00
|
|
|
in
|
|
|
|
|
(case line of
|
|
|
|
|
SOME tri => parse (io, tri :: acc)
|
|
|
|
|
| NONE => PARSE_ERROR)
|
|
|
|
|
end
|
2024-08-29 04:39:23 +01:00
|
|
|
| NONE => let val triangles = List.rev acc in OK triangles end
|
|
|
|
|
|
2024-08-29 05:38:58 +01:00
|
|
|
fun loadTriangles inputMailbox =
|
2024-08-29 05:21:04 +01:00
|
|
|
let
|
|
|
|
|
val io = TextIO.openIn filename
|
|
|
|
|
val triangles = parse (io, [])
|
|
|
|
|
val _ = TextIO.closeIn io
|
2024-08-29 05:38:58 +01:00
|
|
|
|
2024-08-29 09:55:08 +01:00
|
|
|
val inputMsg =
|
2024-08-29 05:38:58 +01:00
|
|
|
case triangles of
|
|
|
|
|
OK triangles => USE_TRIANGLES triangles
|
|
|
|
|
| PARSE_ERROR => TRIANGLES_LOAD_ERROR
|
2024-08-29 05:21:04 +01:00
|
|
|
in
|
2024-08-29 05:38:58 +01:00
|
|
|
Mailbox.send (inputMailbox, inputMsg)
|
2024-08-29 04:39:23 +01:00
|
|
|
end
|
|
|
|
|
|
2024-08-29 00:05:30 +01:00
|
|
|
fun helpSaveTriangles (triangles, io) =
|
|
|
|
|
case triangles of
|
|
|
|
|
{x1, y1, x2, y2, x3, y3} :: tl =>
|
|
|
|
|
let
|
|
|
|
|
val triString = String.concat
|
2024-08-29 09:55:08 +01:00
|
|
|
[ "x ", Real32.toString x1
|
|
|
|
|
, " y ", Real32.toString y1
|
2024-08-29 03:59:47 +01:00
|
|
|
|
2024-08-29 09:55:08 +01:00
|
|
|
, " x ", Real32.toString x2
|
|
|
|
|
, " y ", Real32.toString y2
|
2024-08-29 03:59:47 +01:00
|
|
|
|
2024-08-29 09:55:08 +01:00
|
|
|
, " x ", Real32.toString x3
|
|
|
|
|
, " y ", Real32.toString y3
|
2024-08-29 00:05:30 +01:00
|
|
|
, "\n"
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
val _ = TextIO.output (io, triString)
|
|
|
|
|
in
|
|
|
|
|
helpSaveTriangles (tl, io)
|
|
|
|
|
end
|
|
|
|
|
| [] => ()
|
|
|
|
|
|
|
|
|
|
fun saveTriangles triangles =
|
2024-08-29 00:43:51 +01:00
|
|
|
let
|
|
|
|
|
val io = TextIO.openOut filename
|
|
|
|
|
val _ = helpSaveTriangles (triangles, io)
|
|
|
|
|
val _ = TextIO.closeOut io
|
|
|
|
|
in
|
|
|
|
|
()
|
2024-08-29 00:05:30 +01:00
|
|
|
end
|
|
|
|
|
|
2024-08-29 05:38:58 +01:00
|
|
|
fun run (fileMailbox, inputMailbox) =
|
2024-08-28 20:42:52 +01:00
|
|
|
let
|
|
|
|
|
val _ =
|
|
|
|
|
case Mailbox.recv fileMailbox of
|
2024-08-29 00:05:30 +01:00
|
|
|
SAVE_TRIANGLES triangles => saveTriangles triangles
|
2024-08-29 05:38:58 +01:00
|
|
|
| LOAD_TRIANGLES => loadTriangles inputMailbox
|
2024-08-28 20:42:52 +01:00
|
|
|
| EXPORT_TRIANGLES triangles => ()
|
|
|
|
|
in
|
2024-08-29 05:38:58 +01:00
|
|
|
run (fileMailbox, inputMailbox)
|
2024-08-28 20:42:52 +01:00
|
|
|
end
|
|
|
|
|
end
|