dyna3/engine-proto/ConstructionViewer.jl

223 lines
5.7 KiB
Julia
Raw Normal View History

2024-06-26 07:41:21 +00:00
module Viewer
using Blink
using Colors
2024-06-26 22:24:31 +00:00
using Printf
2024-06-26 07:41:21 +00:00
2024-07-17 21:30:43 +00:00
using Main.Engine
2024-06-26 20:15:54 +00:00
export ConstructionViewer, display!, opentools!, closetools!
2024-06-26 07:41:21 +00:00
# === Blink utilities ===
append_to_head!(w, type, content) = @js w begin
@var element = document.createElement($type)
element.appendChild(document.createTextNode($content))
document.head.appendChild(element)
end
style!(w, stylesheet) = append_to_head!(w, "style", stylesheet)
script!(w, code) = append_to_head!(w, "script", code)
# === construction viewer ===
mutable struct ConstructionViewer
win::Window
function ConstructionViewer()
# create window and open developer console
2024-06-26 22:24:31 +00:00
win = Window(Blink.Dict(:width => 620, :height => 830))
2024-06-26 07:41:21 +00:00
# set stylesheet
style!(win, """
2024-06-26 20:15:54 +00:00
body {
2024-06-26 22:24:31 +00:00
background-color: #ccc;
2024-06-26 20:15:54 +00:00
}
2024-06-26 22:24:31 +00:00
/* the maximum dimensions keep Ganja from blowing up the canvas */
#view {
display: block;
width: 600px;
height: 600px;
2024-06-26 20:15:54 +00:00
margin-top: 10px;
margin-left: 10px;
border-radius: 10px;
2024-06-26 22:24:31 +00:00
background-color: #f0f0f0;
}
#control-panel {
width: 600px;
height: 200px;
box-sizing: border-box;
padding: 5px 10px 5px 10px;
margin-top: 10px;
margin-left: 10px;
2024-06-26 22:56:51 +00:00
overflow-y: scroll;
2024-06-26 22:24:31 +00:00
border-radius: 10px;
background-color: #f0f0f0;
}
#control-panel > div {
margin-top: 5px;
2024-06-26 22:51:57 +00:00
padding: 4px;
2024-06-26 22:24:31 +00:00
border-radius: 5px;
2024-06-26 22:51:57 +00:00
border: solid;
2024-06-26 22:24:31 +00:00
font-family: monospace;
2024-06-26 07:41:21 +00:00
}
""")
2024-07-07 04:35:09 +00:00
# load Ganja.js. for an automatically updated web-hosted version, load from
#
# https://unpkg.com/ganja.js
#
# instead
loadjs!(win, "http://localhost:8000/ganja-1.0.204.js")
2024-06-26 07:41:21 +00:00
# create global functions and variables
script!(win, """
// create algebra
var CGA3 = Algebra(4, 1);
// initialize element list and palette
var elements = [];
var palette = [];
2024-06-26 22:24:31 +00:00
// declare handles for the view and its options
var view;
var viewOpt;
// declare handles for the controls
var controlPanel;
2024-06-26 22:51:57 +00:00
var visToggles;
2024-06-26 07:41:21 +00:00
// create scene function
function scene() {
commands = [];
for (let n = 0; n < elements.length; ++n) {
2024-06-26 22:51:57 +00:00
if (visToggles[n].checked) {
2024-06-26 22:24:31 +00:00
commands.push(palette[n], elements[n]);
}
2024-06-26 07:41:21 +00:00
}
return commands;
}
2024-06-26 22:24:31 +00:00
function updateView() {
requestAnimationFrame(view.update.bind(view, scene));
}
2024-06-26 07:41:21 +00:00
""")
@js win begin
2024-06-26 22:24:31 +00:00
# create view
viewOpt = Dict(
2024-06-26 20:15:54 +00:00
:conformal => true,
:gl => true,
:devicePixelRatio => window.devicePixelRatio
2024-06-26 07:41:21 +00:00
)
2024-06-26 22:24:31 +00:00
view = CGA3.graph(scene, viewOpt)
view.setAttribute(:id, "view")
view.removeAttribute(:style)
document.body.replaceChildren(view)
# create control panel
controlPanel = document.createElement(:div)
controlPanel.setAttribute(:id, "control-panel")
document.body.appendChild(controlPanel)
2024-06-26 07:41:21 +00:00
end
new(win)
end
end
2024-06-28 04:45:41 +00:00
mprod(v, w) =
v[1]*w[1] + v[2]*w[2] + v[3]*w[3] + v[4]*w[4] - v[5]*w[5]
2024-06-26 07:41:21 +00:00
function display!(viewer::ConstructionViewer, elements::Matrix)
# load elements
2024-06-28 04:45:41 +00:00
elements_full = []
2024-07-17 21:30:43 +00:00
for elt in eachcol(Engine.unmix * elements)
2024-06-28 04:45:41 +00:00
if mprod(elt, elt) < 0.5
elt_full = [0; elt; fill(0, 26)]
else
# `elt` is a spacelike vector, representing a generalized sphere, so we
# take its Hodge dual before passing it to Ganja.js. the dual represents
# the same generalized sphere, but Ganja.js only displays planes when
# they're represented by vectors in grade 4 rather than grade 1
elt_full = [fill(0, 26); -elt[5]; -elt[4]; elt[3]; -elt[2]; elt[1]; 0]
end
push!(elements_full, elt_full)
end
2024-06-26 07:41:21 +00:00
@js viewer.win elements = $elements_full.map((elt) -> @new CGA3(elt))
# generate palette. this is Gadfly's `default_discrete_colors` palette,
# available under the MIT license
palette = distinguishable_colors(
length(elements_full),
[LCHab(70, 60, 240)],
transform = c -> deuteranopic(c, 0.5),
lchoices = Float64[65, 70, 75, 80],
cchoices = Float64[0, 50, 60, 70],
hchoices = range(0, stop=330, length=24)
)
palette_packed = [RGB24(c).color for c in palette]
@js viewer.win palette = $palette_packed
2024-06-26 22:51:57 +00:00
# create visibility toggles
2024-06-26 22:24:31 +00:00
@js viewer.win begin
controlPanel.replaceChildren()
2024-06-26 22:51:57 +00:00
visToggles = []
2024-06-26 22:24:31 +00:00
end
2024-06-26 22:51:57 +00:00
for (elt, c) in zip(eachcol(elements), palette)
vec_str = join(map(t -> @sprintf("%.3f", t), elt), ", ")
color_str = "#$(hex(c))"
style_str = "background-color: $color_str; border-color: $color_str;"
2024-06-26 22:24:31 +00:00
@js viewer.win begin
2024-06-26 22:51:57 +00:00
@var toggle = document.createElement(:div)
toggle.setAttribute(:style, $style_str)
toggle.checked = true
toggle.addEventListener(
"click",
() -> begin
toggle.checked = !toggle.checked
toggle.style.backgroundColor = toggle.checked ? $color_str : "inherit";
updateView()
end
)
toggle.appendChild(document.createTextNode($vec_str))
visToggles.push(toggle);
controlPanel.appendChild(toggle);
2024-06-26 22:24:31 +00:00
end
end
2024-06-26 07:41:21 +00:00
# update view
2024-06-26 22:24:31 +00:00
@js viewer.win updateView()
2024-06-26 07:41:21 +00:00
end
2024-06-26 20:15:54 +00:00
function opentools!(viewer::ConstructionViewer)
2024-06-26 22:24:31 +00:00
size(viewer.win, 1240, 830)
2024-06-26 20:15:54 +00:00
opentools(viewer.win)
end
function closetools!(viewer::ConstructionViewer)
closetools(viewer.win)
2024-06-26 22:24:31 +00:00
size(viewer.win, 620, 830)
2024-06-26 20:15:54 +00:00
end
2024-06-26 07:41:21 +00:00
end
# ~~~ sandbox setup ~~~
elements = begin
const a = sqrt(BigFloat(3)/2)
sqrt(0.5) * BigFloat[
1 1 -1 -1 0
1 -1 1 -1 0
1 -1 -1 1 0
0.5 0.5 0.5 0.5 1+a
0.5 0.5 0.5 0.5 1-a
]
end
2024-06-26 07:41:21 +00:00
# show construction
viewer = Viewer.ConstructionViewer()
Viewer.display!(viewer, elements)