Compare commits

...

101 Commits

Author SHA1 Message Date
Aaron Fenyes
5e117d5877 Merge branch 'main' into app-proto
Incorporate the engine prototype from pull request #13, which just got
merged into `main`.
2024-10-21 00:25:59 -07:00
Aaron Fenyes
517fd327fa Assembly: mark constraints as active or not 2024-10-16 23:22:25 -07:00
Aaron Fenyes
f1690b62e1 Move full interface prototype to top level 2024-10-14 17:08:44 -07:00
Aaron Fenyes
cca5a781c4 Remove standalone display prototype 2024-10-14 16:43:13 -07:00
Aaron Fenyes
abe231126d Display: restore intersection and cusp highlighting
This increases resource use a bit, because we now have to hold two
fragments in memory at once instead of just one. It's still much better
than holding all of the top twelve fragments, though!
2024-10-14 16:36:52 -07:00
Aaron Fenyes
ee1c691787 Display: shade fragments after depth sorting
This reduces register pressure significantly. This stepping stone commit
temporarily removes highlighting of intersections and cusps.
2024-10-14 16:04:56 -07:00
Aaron Fenyes
19907838ce Display: remove redundant depth test 2024-09-30 17:59:48 -07:00
Aaron Fenyes
e3120f7109 Display: remove unused fragment-sorting function 2024-09-30 16:48:36 -07:00
Aaron Fenyes
18ebf3be2c Display: add turntable for benchmarking
Together with 25fa108 and 4f8f360, this lets us do a benchmarking
routine for `full-interface` which is comparable to the one we've been
using for `inversive-display`.
2024-09-30 00:44:13 -07:00
Aaron Fenyes
edace8e4ea Outline: include ID and label in element diff key 2024-09-29 23:41:16 -07:00
Aaron Fenyes
70bd39b9e5 App: remove unused imports 2024-09-29 23:30:35 -07:00
Aaron Fenyes
25fa108e9b AddRemove: add low-curvature test assembly from inversive-display 2024-09-28 19:37:43 -07:00
Aaron Fenyes
7977b11caf AddRemove: switch between pre-made test assemblies 2024-09-28 18:56:33 -07:00
Aaron Fenyes
1c9fec36e5 Display: make scene change flag track element list 2024-09-28 18:51:28 -07:00
Aaron Fenyes
721a8716d4 Assembly: don't track element list when inserting
Calling `try_insert_element` or `insert_new_element` in a responsive
context shouldn't make the context track `elements_by_id`.
2024-09-28 18:49:17 -07:00
Aaron Fenyes
4f8f36053f App: use general test assembly from inversive-display
This moves us toward dropping the separate display prototype.
2024-09-28 14:18:04 -07:00
Aaron Fenyes
28b1ecb8e9 App: use element insertion method in test 2024-09-28 13:29:09 -07:00
Aaron Fenyes
b08dbd6f93 Assembly: factor out element insertion 2024-09-28 13:27:03 -07:00
Aaron Fenyes
bd0982f821 AddRemove: make a button that adds elements
In the process, switch selection storage back to `FxHashSet`, reverting
commit b3afd6f.
2024-09-27 14:33:49 -07:00
Aaron Fenyes
2444649dd1 AddRemove: underscore unused event variables 2024-09-26 19:17:57 -07:00
Aaron Fenyes
b3afd6f555 App: Store selection in BTreeSet
Since we're using `BTreeSet` for element constraint sets now, we might
as well use it for the selection set too. This removes the `rustc-hash`
dependency.
2024-09-26 19:16:41 -07:00
Aaron Fenyes
9b39fe56b8 Outline: include constraints in element diff key
This tells Sycamore that the outline view of an element should update
when the element's constraint set has changed. To make the constraint
set hashable, so we can include it in the diff key, we store it as a
`BTreeSet` instead of an `FxHashSet`.
2024-09-26 19:10:34 -07:00
Aaron Fenyes
f5486fb0dd AddRemove: make a button that adds constraints 2024-09-26 15:02:51 -07:00
Aaron Fenyes
4e3c86fb71 Ignore profiling folders 2024-09-26 13:23:56 -07:00
Aaron Fenyes
7ff1b9cb65 App: rename directory 2024-09-26 13:22:48 -07:00
Aaron Fenyes
e6281cdcc6 Display: shrink canvas to 600px
This makes profiling more comparable with `inversive-display`.
2024-09-25 14:48:58 -07:00
Aaron Fenyes
fc85d15f83 Outline: show constraint details 2024-09-23 00:39:14 -07:00
Aaron Fenyes
7709c61f71 Outline: spruce up styling
Use `details` elements to hide and show constraints.
2024-09-22 23:55:07 -07:00
Aaron Fenyes
edee153e37 App: remove unused import 2024-09-22 23:50:16 -07:00
Aaron Fenyes
4a24a01928 App: insert constraints consistently
Also, write constructors for state objects.
2024-09-22 14:40:31 -07:00
Aaron Fenyes
050e2373a6 App: store constraints
Draft listing of constraints in outline view.
2024-09-22 14:05:40 -07:00
Aaron Fenyes
147e275823 App: don't bother copying key into element
When we access an element, we always have its key, either because the
slab iterator yielded it along side the element or because we used it to
get the element from the slab.
2024-09-22 02:38:17 -07:00
Aaron Fenyes
d121385c18 App: store assembly elements in slab 2024-09-22 02:21:45 -07:00
Aaron Fenyes
78f8ef8215 Outline: switch to single selection 2024-09-19 17:53:07 -07:00
Aaron Fenyes
96f8b6b5f3 App: store selection in hash map
Switch `Assembly.elements` to a hash map too, since that's probably
closer to what we'll want in the future.
2024-09-19 16:08:55 -07:00
Aaron Fenyes
96afad0c97 Display: highlight selected elements 2024-09-16 15:46:45 -07:00
Aaron Fenyes
a60624884a App: add element selection 2024-09-16 11:29:44 -07:00
Aaron Fenyes
93190e99da Display: bring in keyboard navigation code 2024-09-15 11:54:39 -07:00
Aaron Fenyes
e2d3af2867 Display: say "assembly" instead of "construction"
Update variable names and comments in code from the display prototype.
2024-09-15 11:41:16 -07:00
Aaron Fenyes
7cb01bab82 Ray-caster: drop outdated performance comment
The size of the internal fragment arrays is what really matters, as
discussed in the "Display" page on the wiki.
2024-09-15 11:38:32 -07:00
Aaron Fenyes
f47be08d98 Display: get the assembly from the app state 2024-09-15 11:31:22 -07:00
Aaron Fenyes
cd18d594e0 Display: bring in ray-casting code 2024-09-14 11:46:24 -07:00
Aaron Fenyes
49655a8d62 Ray-caster: remove debug code
Remove GPU code and uniforms that were used as scaffolding during
initial development, but have now been replaced by CPU analogues.
2024-09-14 10:58:46 -07:00
Aaron Fenyes
959e4cc8b5 App: pass app state into outline as context 2024-09-13 15:15:55 -07:00
Aaron Fenyes
49170671b4 App: add display canvas 2024-09-13 14:53:12 -07:00
Aaron Fenyes
0c2869d3f3 Outline: improve code formatting 2024-09-13 00:43:19 -07:00
Aaron Fenyes
e6d1e0b865 Outline: encapsulate assembly data 2024-09-13 00:40:34 -07:00
Aaron Fenyes
d481181ef8 Outline: sort elements by ID 2024-09-13 00:07:49 -07:00
Aaron Fenyes
20b96a9764 Outline: switch from "Editor" to "App" 2024-09-12 22:39:21 -07:00
Aaron Fenyes
634e97b659 Outline: switch to user-facing ID 2024-09-12 22:36:54 -07:00
Aaron Fenyes
336b940471 Outline: start on editor state and outline view 2024-09-12 15:24:41 -07:00
Aaron Fenyes
d3c9a08d22 Add zoom to keyboard controls 2024-09-10 04:08:49 -07:00
Aaron Fenyes
aceac5e5c4 Add roll to keyboard controls 2024-09-10 03:14:33 -07:00
Aaron Fenyes
20d072d615 Combine key-down and key-up handlers 2024-09-10 02:29:50 -07:00
Aaron Fenyes
c67f37c934 Implement keyboard navigation 2024-09-09 19:41:15 -07:00
Aaron Fenyes
2efc08d6c0 Enable focus for tabs and display
You can now switch tabs from the keyboard using the usual radio button
interaction.
2024-09-09 02:15:04 -07:00
Aaron Fenyes
69ab888d5b Simplify control labeling 2024-09-09 00:32:29 -07:00
Aaron Fenyes
0173b63e19 Add picture plane to circles in triangle 2024-09-08 23:43:26 -07:00
Aaron Fenyes
b289d2d4c3 Distinguish odd layer counts in debug mode
The low-curvature construction admits odd layer counts.
2024-09-08 23:31:48 -07:00
Aaron Fenyes
163361184b Ray-caster: avoid roundoff error in quadratic equation 2024-09-08 23:00:28 -07:00
Aaron Fenyes
ab830b194e Use circles in triangle as low-curvature construction
In the process, add a way to build a sphere by offset and curvature.
2024-09-06 19:01:18 -07:00
Aaron Fenyes
3493a798d1 Add low-curvature construction
Also add infrastructure for switching between constructions.
2024-09-04 16:27:28 -07:00
Aaron Fenyes
121934c4c3 Encapsulate construction 2024-09-04 12:58:55 -07:00
Aaron Fenyes
a4236a34df Ray-caster: dim the interior layers of spheres
This seems to weaken the intersection disk illusion.
2024-09-02 15:15:18 -07:00
Aaron Fenyes
f148552964 Ray-caster: only draw the top few fragments
This seems to increase graphics pipe use from 50--60% to 55--65%.
2024-08-29 15:01:38 -07:00
Aaron Fenyes
6db9f5be6c Enlarge the test construction to six spheres
In debug mode, assign most of the color scale to even layer counts. Odd
layer counts are topologically prohibited, so we should rarely see bugs
severe enough to produce them.
2024-08-28 17:14:55 -07:00
Aaron Fenyes
3a721a4cc8 Ray-caster: enlarge the construction data uniforms
No noticeable effect on GPU performance.
2024-08-28 16:06:33 -07:00
Aaron Fenyes
8fde202911 Ray-caster: drop unused depth-pair array
This doesn't affect GPU performance noticeably, so benchmarks before and
after the change should be comparable.
2024-08-28 15:52:19 -07:00
Aaron Fenyes
4afc82034b Ray-caster: sort fragments while shading 2024-08-28 14:37:29 -07:00
Aaron Fenyes
e80adf831d Ray-caster: correct hit detection 2024-08-28 01:59:46 -07:00
Aaron Fenyes
3d7ee98dd6 Ray-caster: check layer count
In debug mode, show the layer count instead of the shaded image. This
reveals a bug: testing whether the hit depth is NaN doesn't actually
detect sphere misses, because `sphere_cast` returns -1 rather than NaN
to indicate a miss.
2024-08-28 01:55:46 -07:00
Aaron Fenyes
c04e29f586 Reduce the frame interval sample frequency
At the higher frequency we were using earlier, the overhead from
updating the readout contributed significantly to the frame interval.
2024-08-28 01:47:38 -07:00
Aaron Fenyes
01c2af6615 Rotate and translate construction
In the process, write code to make updates that depend on the time
between frames.
2024-08-27 18:39:58 -07:00
Aaron Fenyes
a40a110788 Ray-caster: only draw when the scene is changed
This is how I typically schedule draw calls in JavaScript applications.
The baseline CPU activity for the display prototype is now in line with
other pages (though perhaps a bit higher), and the profiler shows little
time being spent in draw calls, even when I'm continually moving a
slider. The interface feels pretty responsive overall, although the
sliders seem to be lagging a bit.
2024-08-27 13:55:08 -07:00
Aaron Fenyes
f62f44b5a7 Optimization: decouple internal and uniform SPHERE_MAX 2024-08-27 00:00:48 -07:00
Aaron Fenyes
ec48592ef1 Ray-caster: add a frame time monitor
It's time to start optimizing. Frame time is easy to measure, and we can
use it to gauge responsiveness.
2024-08-26 23:39:51 -07:00
Aaron Fenyes
5e9c5db231 Ray-caster: switch from draw effect to animation loop
This wastes a lot of CPU time, as explained on lines 253--258 of
`main.rs`, but it's better than the previous version, which could block
graphics updates system-wide for seconds on end.
2024-08-26 16:06:37 -07:00
Aaron Fenyes
bf140efaf7 Ray-caster: remove hard-coded test construction 2024-08-26 13:51:01 -07:00
Aaron Fenyes
cbec31f5df Ray-caster: pass colors in through uniforms 2024-08-26 13:41:34 -07:00
Aaron Fenyes
b9370ceb41 Ray-caster: label controls 2024-08-26 01:47:53 -07:00
Aaron Fenyes
85db7b9be0 Ray-caster: pass the sphere count as a uniform
In the process, start exploring array size limits of various kinds.
2024-08-26 00:58:20 -07:00
Aaron Fenyes
c5fe725b1b Ray-caster: automate getting uniform array locations 2024-08-25 22:22:14 -07:00
Aaron Fenyes
5bf23fa789 Ray-caster: pass spheres in through uniforms
Keep the hard-coded spheres for comparison.
2024-08-25 21:40:59 -07:00
Aaron Fenyes
206a2df480 Ray-caster: add a third test sphere
This helps confirm that the generalized depth-sorting is working.
2024-08-25 16:41:31 -07:00
Aaron Fenyes
c18cac642b Ray-caster: generalize depth sorting
Switch from a hard-coded sorting network for four fragments to an
insertion sort, which should work for any number of fragments.
2024-08-25 00:47:36 -07:00
Aaron Fenyes
8798683d25 Ray-caster: store sphere data in arrays
This is a first step toward general depth sorting.
2024-08-25 00:00:28 -07:00
Aaron Fenyes
b9587872d3 Ray-caster: don't bother clearing the screen
The ray-caster triangles cover the whole viewport, so they'll completely
overdraw the previous frame.
2024-08-24 11:31:22 -07:00
Aaron Fenyes
766d56027c Ray-caster: move shaders to separate files
This properly reflects the modularity of the code, and it simplifies
indentation and syntax highlighting.
2024-08-24 11:27:19 -07:00
Aaron Fenyes
25da6ca062 Ray-caster: adjust opacity of highlighting 2024-08-24 11:08:31 -07:00
Aaron Fenyes
e3df765f16 Ray-caster: highlight intersections and cusps 2024-08-24 01:38:06 -07:00
Aaron Fenyes
f1029b3102 Ray-caster: map output into sRGB space
Change the base color and default opacity to keep the picture looking
broadly the same.
2024-08-24 01:05:19 -07:00
Aaron Fenyes
87763fc458 Ray-caster: tidy up sphere shading 2024-08-24 00:29:11 -07:00
Aaron Fenyes
2ef0fdd3e2 Ray-cast two spheres, with hard-coded depth sorting 2024-08-23 12:56:54 -07:00
Aaron Fenyes
d2cecf69db Ray-cast a translucent sphere 2024-08-23 00:16:41 -07:00
Aaron Fenyes
c78a041dc7 Write a ray-caster for inversive spheres 2024-08-22 22:08:34 -07:00
Aaron Fenyes
f274119da6 Enable depth testing
To get the right order, flip the sign of the `z` component in the output
of the projection map.
2024-08-22 18:17:01 -07:00
Aaron Fenyes
1fbeb23194 Add rotation control
In the process, find and correct an error in the --+ vertex, which was
miswritten as ---.
2024-08-22 00:04:58 -07:00
Aaron Fenyes
80b210e667 Make the projection map a uniform 2024-08-21 23:07:14 -07:00
Aaron Fenyes
81f9b8e040 Find vertex attribute indices in advance 2024-08-21 22:36:56 -07:00
Aaron Fenyes
5885189b04 Draw a mesh in perspective, and in color 2024-08-21 17:31:17 -07:00
Aaron Fenyes
fd3cbae1b4 Get a WebGL canvas working in Sycamore 2024-08-21 13:01:33 -07:00
12 changed files with 1428 additions and 0 deletions

4
app-proto/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
target
dist
profiling
Cargo.lock

42
app-proto/Cargo.toml Normal file
View File

@ -0,0 +1,42 @@
[package]
name = "sketch-outline"
version = "0.1.0"
authors = ["Aaron"]
edition = "2021"
[features]
default = ["console_error_panic_hook"]
[dependencies]
itertools = "0.13.0"
js-sys = "0.3.70"
nalgebra = "0.33.0"
rustc-hash = "2.0.0"
slab = "0.4.9"
sycamore = "0.9.0-beta.3"
# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
[dependencies.web-sys]
version = "0.3.69"
features = [
'HtmlCanvasElement',
'Performance',
'WebGl2RenderingContext',
'WebGlBuffer',
'WebGlProgram',
'WebGlShader',
'WebGlUniformLocation',
'WebGlVertexArrayObject'
]
[dev-dependencies]
wasm-bindgen-test = "0.3.34"
[profile.release]
opt-level = "s" # optimize for small code size
debug = true # include debug symbols

9
app-proto/index.html Normal file
View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Sketch outline</title>
<link data-trunk rel="css" href="main.css"/>
</head>
<body></body>
</html>

124
app-proto/main.css Normal file
View File

@ -0,0 +1,124 @@
body {
margin: 0px;
color: #fcfcfc;
background-color: #222;
}
/* sidebar */
#sidebar {
display: flex;
flex-direction: column;
float: left;
width: 450px;
height: 100vh;
margin: 0px;
padding: 0px;
border-width: 0px 1px 0px 0px;
border-style: solid;
border-color: #555;
}
/* add-remove */
#add-remove {
display: flex;
gap: 8px;
margin: 8px;
}
#add-remove > button {
width: 32px;
height: 32px;
font-size: large;
}
/* outline */
#outline {
flex-grow: 1;
margin: 0px;
padding: 0px;
overflow-y: scroll;
}
li {
user-select: none;
}
summary {
display: flex;
}
summary.selected {
color: #fff;
background-color: #444;
}
summary > div, .cst {
padding-top: 4px;
padding-bottom: 4px;
}
.elt, .cst {
display: flex;
flex-grow: 1;
padding-left: 8px;
padding-right: 8px;
}
.elt-switch {
width: 18px;
padding-left: 2px;
text-align: center;
}
details:has(li) .elt-switch::after {
content: '▸';
}
details[open]:has(li) .elt-switch::after {
content: '▾';
}
.elt-label {
flex-grow: 1;
}
.cst-label {
flex-grow: 1;
}
.elt-rep {
display: flex;
}
.elt-rep > div, .cst-rep {
padding: 2px 0px 0px 0px;
font-size: 10pt;
text-align: center;
width: 56px;
}
.cst {
font-style: italic;
}
.cst > input {
margin: 0px 8px 0px 0px;
}
/* display */
canvas {
float: left;
margin-left: 20px;
margin-top: 20px;
background-color: #020202;
border: 1px solid #555;
border-radius: 16px;
}
canvas:focus {
border-color: #aaa;
}

242
app-proto/src/add_remove.rs Normal file
View File

@ -0,0 +1,242 @@
use std::collections::BTreeSet; /* DEBUG */
use sycamore::prelude::*;
use web_sys::{console, wasm_bindgen::JsValue};
use crate::{engine, AppState, assembly::{Assembly, Constraint, Element}};
/* DEBUG */
fn load_gen_assemb(assembly: &Assembly) {
let _ = assembly.try_insert_element(
Element {
id: String::from("gemini_a"),
label: String::from("Castor"),
color: [1.00_f32, 0.25_f32, 0.00_f32],
rep: engine::sphere(0.5, 0.5, 0.0, 1.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: String::from("gemini_b"),
label: String::from("Pollux"),
color: [0.00_f32, 0.25_f32, 1.00_f32],
rep: engine::sphere(-0.5, -0.5, 0.0, 1.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: String::from("ursa_major"),
label: String::from("Ursa major"),
color: [0.25_f32, 0.00_f32, 1.00_f32],
rep: engine::sphere(-0.5, 0.5, 0.0, 0.75),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: String::from("ursa_minor"),
label: String::from("Ursa minor"),
color: [0.25_f32, 1.00_f32, 0.00_f32],
rep: engine::sphere(0.5, -0.5, 0.0, 0.5),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: String::from("moon_deimos"),
label: String::from("Deimos"),
color: [0.75_f32, 0.75_f32, 0.00_f32],
rep: engine::sphere(0.0, 0.15, 1.0, 0.25),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: String::from("moon_phobos"),
label: String::from("Phobos"),
color: [0.00_f32, 0.75_f32, 0.50_f32],
rep: engine::sphere(0.0, -0.15, -1.0, 0.25),
constraints: BTreeSet::default()
}
);
assembly.insert_constraint(
Constraint {
args: (
assembly.elements_by_id.with_untracked(|elts_by_id| elts_by_id["gemini_a"]),
assembly.elements_by_id.with_untracked(|elts_by_id| elts_by_id["gemini_b"])
),
rep: 0.5,
active: create_signal(true)
}
);
}
/* DEBUG */
fn load_low_curv_assemb(assembly: &Assembly) {
let a = 0.75_f64.sqrt();
let _ = assembly.try_insert_element(
Element {
id: "central".to_string(),
label: "Central".to_string(),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: engine::sphere(0.0, 0.0, 0.0, 1.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: "assemb_plane".to_string(),
label: "Assembly plane".to_string(),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: engine::sphere_with_offset(0.0, 0.0, 1.0, 0.0, 0.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: "side1".to_string(),
label: "Side 1".to_string(),
color: [1.00_f32, 0.00_f32, 0.25_f32],
rep: engine::sphere_with_offset(1.0, 0.0, 0.0, 1.0, 0.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: "side2".to_string(),
label: "Side 2".to_string(),
color: [0.25_f32, 1.00_f32, 0.00_f32],
rep: engine::sphere_with_offset(-0.5, a, 0.0, 1.0, 0.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: "side3".to_string(),
label: "Side 3".to_string(),
color: [0.00_f32, 0.25_f32, 1.00_f32],
rep: engine::sphere_with_offset(-0.5, -a, 0.0, 1.0, 0.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: "corner1".to_string(),
label: "Corner 1".to_string(),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: engine::sphere(-4.0/3.0, 0.0, 0.0, 1.0/3.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: "corner2".to_string(),
label: "Corner 2".to_string(),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: engine::sphere(2.0/3.0, -4.0/3.0 * a, 0.0, 1.0/3.0),
constraints: BTreeSet::default()
}
);
let _ = assembly.try_insert_element(
Element {
id: String::from("corner3"),
label: String::from("Corner 3"),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: engine::sphere(2.0/3.0, 4.0/3.0 * a, 0.0, 1.0/3.0),
constraints: BTreeSet::default()
}
);
}
#[component]
pub fn AddRemove() -> View {
/* DEBUG */
let assembly_name = create_signal("general".to_string());
create_effect(move || {
// get name of chosen assembly
let name = assembly_name.get_clone();
console::log_1(
&JsValue::from(format!("Showing assembly \"{}\"", name.clone()))
);
batch(|| {
let state = use_context::<AppState>();
let assembly = &state.assembly;
// clear state
assembly.elements.update(|elts| elts.clear());
assembly.elements_by_id.update(|elts_by_id| elts_by_id.clear());
state.selection.update(|sel| sel.clear());
// load assembly
match name.as_str() {
"general" => load_gen_assemb(assembly),
"low-curv" => load_low_curv_assemb(assembly),
_ => ()
};
});
});
view! {
div(id="add-remove") {
button(
on:click=|_| {
let state = use_context::<AppState>();
state.assembly.insert_new_element();
/* DEBUG */
// print updated list of elements by identifier
console::log_1(&JsValue::from("elements by identifier:"));
for (id, key) in state.assembly.elements_by_id.get_clone().iter() {
console::log_3(
&JsValue::from(" "),
&JsValue::from(id),
&JsValue::from(*key)
);
}
}
) { "+" }
button(
disabled={
let state = use_context::<AppState>();
state.selection.with(|sel| sel.len() != 2)
},
on:click=|_| {
let state = use_context::<AppState>();
let args = state.selection.with(
|sel| {
let arg_vec: Vec<_> = sel.into_iter().collect();
(arg_vec[0].clone(), arg_vec[1].clone())
}
);
state.assembly.insert_constraint(Constraint {
args: args,
rep: 0.0,
active: create_signal(true)
});
state.selection.update(|sel| sel.clear());
/* DEBUG */
// print updated constraint list
console::log_1(&JsValue::from("constraints:"));
state.assembly.constraints.with(|csts| {
for (_, cst) in csts.into_iter() {
console::log_5(
&JsValue::from(" "),
&JsValue::from(cst.args.0),
&JsValue::from(cst.args.1),
&JsValue::from(":"),
&JsValue::from(cst.rep)
);
}
});
}
) { "🔗" }
select(bind:value=assembly_name) { /* DEBUG */
option(value="general") { "General" }
option(value="low-curv") { "Low-curvature" }
}
}
}
}

93
app-proto/src/assembly.rs Normal file
View File

@ -0,0 +1,93 @@
use nalgebra::DVector;
use rustc_hash::FxHashMap;
use slab::Slab;
use std::collections::BTreeSet;
use sycamore::prelude::*;
#[derive(Clone, PartialEq)]
pub struct Element {
pub id: String,
pub label: String,
pub color: [f32; 3],
pub rep: DVector<f64>,
pub constraints: BTreeSet<usize>
}
#[derive(Clone)]
pub struct Constraint {
pub args: (usize, usize),
pub rep: f64,
pub active: Signal<bool>
}
// a complete, view-independent description of an assembly
#[derive(Clone)]
pub struct Assembly {
// elements and constraints
pub elements: Signal<Slab<Element>>,
pub constraints: Signal<Slab<Constraint>>,
// indexing
pub elements_by_id: Signal<FxHashMap<String, usize>>
}
impl Assembly {
pub fn new() -> Assembly {
Assembly {
elements: create_signal(Slab::new()),
constraints: create_signal(Slab::new()),
elements_by_id: create_signal(FxHashMap::default())
}
}
// insert an element into the assembly without checking whether we already
// have an element with the same identifier. any element that does have the
// same identifier will get kicked out of the `elements_by_id` index
fn insert_element_unchecked(&self, elt: Element) {
let id = elt.id.clone();
let key = self.elements.update(|elts| elts.insert(elt));
self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, key));
}
pub fn try_insert_element(&self, elt: Element) -> bool {
let can_insert = self.elements_by_id.with_untracked(
|elts_by_id| !elts_by_id.contains_key(&elt.id)
);
if can_insert {
self.insert_element_unchecked(elt);
}
can_insert
}
pub fn insert_new_element(&self) {
// find the next unused identifier in the default sequence
let mut id_num = 1;
let mut id = format!("sphere{}", id_num);
while self.elements_by_id.with_untracked(
|elts_by_id| elts_by_id.contains_key(&id)
) {
id_num += 1;
id = format!("sphere{}", id_num);
}
// create and insert a new element
self.insert_element_unchecked(
Element {
id: id,
label: format!("Sphere {}", id_num),
color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.5, -0.5]),
constraints: BTreeSet::default()
}
);
}
pub fn insert_constraint(&self, constraint: Constraint) {
let args = constraint.args;
let key = self.constraints.update(|csts| csts.insert(constraint));
self.elements.update(|elts| {
elts[args.0].constraints.insert(key);
elts[args.1].constraints.insert(key);
})
}
}

443
app-proto/src/display.rs Normal file
View File

@ -0,0 +1,443 @@
use core::array;
use nalgebra::{DMatrix, Rotation3, Vector3};
use sycamore::{prelude::*, motion::create_raf};
use web_sys::{
console,
window,
KeyboardEvent,
WebGl2RenderingContext,
WebGlProgram,
WebGlShader,
WebGlUniformLocation,
wasm_bindgen::{JsCast, JsValue}
};
use crate::AppState;
fn compile_shader(
context: &WebGl2RenderingContext,
shader_type: u32,
source: &str,
) -> WebGlShader {
let shader = context.create_shader(shader_type).unwrap();
context.shader_source(&shader, source);
context.compile_shader(&shader);
shader
}
fn get_uniform_array_locations<const N: usize>(
context: &WebGl2RenderingContext,
program: &WebGlProgram,
var_name: &str,
member_name_opt: Option<&str>
) -> [Option<WebGlUniformLocation>; N] {
array::from_fn(|n| {
let name = match member_name_opt {
Some(member_name) => format!("{var_name}[{n}].{member_name}"),
None => format!("{var_name}[{n}]")
};
context.get_uniform_location(&program, name.as_str())
})
}
// load the given data into the vertex input of the given name
fn bind_vertex_attrib(
context: &WebGl2RenderingContext,
index: u32,
size: i32,
data: &[f32]
) {
// create a data buffer and bind it to ARRAY_BUFFER
let buffer = context.create_buffer().unwrap();
context.bind_buffer(WebGl2RenderingContext::ARRAY_BUFFER, Some(&buffer));
// load the given data into the buffer. the function `Float32Array::view`
// creates a raw view into our module's `WebAssembly.Memory` buffer.
// allocating more memory will change the buffer, invalidating the view.
// that means we have to make sure we don't allocate any memory until the
// view is dropped
unsafe {
context.buffer_data_with_array_buffer_view(
WebGl2RenderingContext::ARRAY_BUFFER,
&js_sys::Float32Array::view(&data),
WebGl2RenderingContext::STATIC_DRAW,
);
}
// allow the target attribute to be used
context.enable_vertex_attrib_array(index);
// take whatever's bound to ARRAY_BUFFER---here, the data buffer created
// above---and bind it to the target attribute
//
// https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/vertexAttribPointer
//
context.vertex_attrib_pointer_with_i32(
index,
size,
WebGl2RenderingContext::FLOAT,
false, // don't normalize
0, // zero stride
0, // zero offset
);
}
#[component]
pub fn Display() -> View {
let state = use_context::<AppState>();
// canvas
let display = create_node_ref();
// navigation
let pitch_up = create_signal(0.0);
let pitch_down = create_signal(0.0);
let yaw_right = create_signal(0.0);
let yaw_left = create_signal(0.0);
let roll_ccw = create_signal(0.0);
let roll_cw = create_signal(0.0);
let zoom_in = create_signal(0.0);
let zoom_out = create_signal(0.0);
let turntable = create_signal(false); /* BENCHMARKING */
// change listener
let scene_changed = create_signal(true);
create_effect(move || {
state.assembly.elements.track();
state.selection.track();
scene_changed.set(true);
});
/* INSTRUMENTS */
const SAMPLE_PERIOD: i32 = 60;
let mut last_sample_time = 0.0;
let mut frames_since_last_sample = 0;
let mean_frame_interval = create_signal(0.0);
on_mount(move || {
// timing
let mut last_time = 0.0;
// viewpoint
const ROT_SPEED: f64 = 0.4; // in radians per second
const ZOOM_SPEED: f64 = 0.15; // multiplicative rate per second
const TURNTABLE_SPEED: f64 = 0.1; /* BENCHMARKING */
let mut orientation = DMatrix::<f64>::identity(5, 5);
let mut rotation = DMatrix::<f64>::identity(5, 5);
let mut location_z: f64 = 5.0;
// display parameters
const OPACITY: f32 = 0.5; /* SCAFFOLDING */
const HIGHLIGHT: f32 = 0.2; /* SCAFFOLDING */
const LAYER_THRESHOLD: i32 = 0; /* DEBUG */
const DEBUG_MODE: i32 = 0; /* DEBUG */
/* INSTRUMENTS */
let performance = window().unwrap().performance().unwrap();
// get the display canvas
let canvas = display.get().unchecked_into::<web_sys::HtmlCanvasElement>();
let ctx = canvas
.get_context("webgl2")
.unwrap()
.unwrap()
.dyn_into::<WebGl2RenderingContext>()
.unwrap();
// compile and attach the vertex and fragment shaders
let vertex_shader = compile_shader(
&ctx,
WebGl2RenderingContext::VERTEX_SHADER,
include_str!("identity.vert"),
);
let fragment_shader = compile_shader(
&ctx,
WebGl2RenderingContext::FRAGMENT_SHADER,
include_str!("inversive.frag"),
);
let program = ctx.create_program().unwrap();
ctx.attach_shader(&program, &vertex_shader);
ctx.attach_shader(&program, &fragment_shader);
ctx.link_program(&program);
let link_status = ctx
.get_program_parameter(&program, WebGl2RenderingContext::LINK_STATUS)
.as_bool()
.unwrap();
let link_msg = if link_status {
"Linked successfully"
} else {
"Linking failed"
};
console::log_1(&JsValue::from(link_msg));
ctx.use_program(Some(&program));
/* DEBUG */
// print the maximum number of vectors that can be passed as
// uniforms to a fragment shader. the OpenGL ES 3.0 standard
// requires this maximum to be at least 224, as discussed in the
// documentation of the GL_MAX_FRAGMENT_UNIFORM_VECTORS parameter
// here:
//
// https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glGet.xhtml
//
// there are also other size limits. for example, on Aaron's
// machine, the the length of a float or genType array seems to be
// capped at 1024 elements
console::log_2(
&ctx.get_parameter(WebGl2RenderingContext::MAX_FRAGMENT_UNIFORM_VECTORS).unwrap(),
&JsValue::from("uniform vectors available")
);
// find indices of vertex attributes and uniforms
const SPHERE_MAX: usize = 200;
let position_index = ctx.get_attrib_location(&program, "position") as u32;
let sphere_cnt_loc = ctx.get_uniform_location(&program, "sphere_cnt");
let sphere_sp_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", Some("sp")
);
let sphere_lt_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "sphere_list", Some("lt")
);
let color_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "color_list", None
);
let highlight_locs = get_uniform_array_locations::<SPHERE_MAX>(
&ctx, &program, "highlight_list", None
);
let resolution_loc = ctx.get_uniform_location(&program, "resolution");
let shortdim_loc = ctx.get_uniform_location(&program, "shortdim");
let opacity_loc = ctx.get_uniform_location(&program, "opacity");
let layer_threshold_loc = ctx.get_uniform_location(&program, "layer_threshold");
let debug_mode_loc = ctx.get_uniform_location(&program, "debug_mode");
// create a vertex array and bind it to the graphics context
let vertex_array = ctx.create_vertex_array().unwrap();
ctx.bind_vertex_array(Some(&vertex_array));
// set the vertex positions
const VERTEX_CNT: usize = 6;
let positions: [f32; 3*VERTEX_CNT] = [
// northwest triangle
-1.0, -1.0, 0.0,
-1.0, 1.0, 0.0,
1.0, 1.0, 0.0,
// southeast triangle
-1.0, -1.0, 0.0,
1.0, 1.0, 0.0,
1.0, -1.0, 0.0
];
bind_vertex_attrib(&ctx, position_index, 3, &positions);
// set up a repainting routine
let (_, start_animation_loop, _) = create_raf(move || {
// get the time step
let time = performance.now();
let time_step = 0.001*(time - last_time);
last_time = time;
// get the navigation state
let pitch_up_val = pitch_up.get();
let pitch_down_val = pitch_down.get();
let yaw_right_val = yaw_right.get();
let yaw_left_val = yaw_left.get();
let roll_ccw_val = roll_ccw.get();
let roll_cw_val = roll_cw.get();
let zoom_in_val = zoom_in.get();
let zoom_out_val = zoom_out.get();
let turntable_val = turntable.get(); /* BENCHMARKING */
// update the assembly's orientation
let ang_vel = {
let pitch = pitch_up_val - pitch_down_val;
let yaw = yaw_right_val - yaw_left_val;
let roll = roll_ccw_val - roll_cw_val;
if pitch != 0.0 || yaw != 0.0 || roll != 0.0 {
ROT_SPEED * Vector3::new(-pitch, yaw, roll).normalize()
} else {
Vector3::zeros()
}
} /* BENCHMARKING */ + if turntable_val {
Vector3::new(0.0, TURNTABLE_SPEED, 0.0)
} else {
Vector3::zeros()
};
let mut rotation_sp = rotation.fixed_view_mut::<3, 3>(0, 0);
rotation_sp.copy_from(
Rotation3::from_scaled_axis(time_step * ang_vel).matrix()
);
orientation = &rotation * &orientation;
// update the assembly's location
let zoom = zoom_out_val - zoom_in_val;
location_z *= (time_step * ZOOM_SPEED * zoom).exp();
if scene_changed.get() {
/* INSTRUMENTS */
// measure mean frame interval
frames_since_last_sample += 1;
if frames_since_last_sample >= SAMPLE_PERIOD {
mean_frame_interval.set((time - last_sample_time) / (SAMPLE_PERIOD as f64));
last_sample_time = time;
frames_since_last_sample = 0;
}
// find the map from assembly space to world space
let location = {
let u = -location_z;
DMatrix::from_column_slice(5, 5, &[
1.0, 0.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0, u,
0.0, 0.0, 2.0*u, 1.0, u*u,
0.0, 0.0, 0.0, 0.0, 1.0
])
};
let assembly_to_world = &location * &orientation;
// get the assembly
let elements = state.assembly.elements.get_clone();
let element_iter = (&elements).into_iter();
let reps_world: Vec<_> = element_iter.clone().map(|(_, elt)| &assembly_to_world * &elt.rep).collect();
let colors: Vec<_> = element_iter.clone().map(|(key, elt)|
if state.selection.with(|sel| sel.contains(&key)) {
elt.color.map(|ch| 0.2 + 0.8*ch)
} else {
elt.color
}
).collect();
let highlights: Vec<_> = element_iter.map(|(key, _)|
if state.selection.with(|sel| sel.contains(&key)) {
1.0_f32
} else {
HIGHLIGHT
}
).collect();
// set the resolution
let width = canvas.width() as f32;
let height = canvas.height() as f32;
ctx.uniform2f(resolution_loc.as_ref(), width, height);
ctx.uniform1f(shortdim_loc.as_ref(), width.min(height));
// pass the assembly
ctx.uniform1i(sphere_cnt_loc.as_ref(), elements.len() as i32);
for n in 0..reps_world.len() {
let v = &reps_world[n];
ctx.uniform3f(
sphere_sp_locs[n].as_ref(),
v[0] as f32, v[1] as f32, v[2] as f32
);
ctx.uniform2f(
sphere_lt_locs[n].as_ref(),
v[3] as f32, v[4] as f32
);
ctx.uniform3fv_with_f32_array(
color_locs[n].as_ref(),
&colors[n]
);
ctx.uniform1f(
highlight_locs[n].as_ref(),
highlights[n]
);
}
// pass the display parameters
ctx.uniform1f(opacity_loc.as_ref(), OPACITY);
ctx.uniform1i(layer_threshold_loc.as_ref(), LAYER_THRESHOLD);
ctx.uniform1i(debug_mode_loc.as_ref(), DEBUG_MODE);
// draw the scene
ctx.draw_arrays(WebGl2RenderingContext::TRIANGLES, 0, VERTEX_CNT as i32);
// clear the scene change flag
scene_changed.set(
pitch_up_val != 0.0
|| pitch_down_val != 0.0
|| yaw_left_val != 0.0
|| yaw_right_val != 0.0
|| roll_cw_val != 0.0
|| roll_ccw_val != 0.0
|| zoom_in_val != 0.0
|| zoom_out_val != 0.0
|| turntable_val /* BENCHMARKING */
);
} else {
frames_since_last_sample = 0;
mean_frame_interval.set(-1.0);
}
});
start_animation_loop();
});
let set_nav_signal = move |event: KeyboardEvent, value: f64| {
let mut navigating = true;
let shift = event.shift_key();
match event.key().as_str() {
"ArrowUp" if shift => zoom_in.set(value),
"ArrowDown" if shift => zoom_out.set(value),
"ArrowUp" => pitch_up.set(value),
"ArrowDown" => pitch_down.set(value),
"ArrowRight" if shift => roll_cw.set(value),
"ArrowLeft" if shift => roll_ccw.set(value),
"ArrowRight" => yaw_right.set(value),
"ArrowLeft" => yaw_left.set(value),
_ => navigating = false
};
if navigating {
scene_changed.set(true);
event.prevent_default();
}
};
view! {
/* TO DO */
// switch back to integer-valued parameters when that becomes possible
// again
canvas(
ref=display,
width="600",
height="600",
tabindex="0",
on:keydown=move |event: KeyboardEvent| {
if event.key() == "Shift" {
roll_cw.set(yaw_right.get());
roll_ccw.set(yaw_left.get());
zoom_in.set(pitch_up.get());
zoom_out.set(pitch_down.get());
yaw_right.set(0.0);
yaw_left.set(0.0);
pitch_up.set(0.0);
pitch_down.set(0.0);
} else {
if event.key() == "Enter" { /* BENCHMARKING */
turntable.set_fn(|turn| !turn);
scene_changed.set(true);
}
set_nav_signal(event, 1.0);
}
},
on:keyup=move |event: KeyboardEvent| {
if event.key() == "Shift" {
yaw_right.set(roll_cw.get());
yaw_left.set(roll_ccw.get());
pitch_up.set(zoom_in.get());
pitch_down.set(zoom_out.get());
roll_cw.set(0.0);
roll_ccw.set(0.0);
zoom_in.set(0.0);
zoom_out.set(0.0);
} else {
set_nav_signal(event, 0.0);
}
},
on:blur=move |_| {
pitch_up.set(0.0);
pitch_down.set(0.0);
yaw_right.set(0.0);
yaw_left.set(0.0);
roll_ccw.set(0.0);
roll_cw.set(0.0);
}
)
}
}

27
app-proto/src/engine.rs Normal file
View File

@ -0,0 +1,27 @@
use nalgebra::DVector;
// the sphere with the given center and radius, with inward-pointing normals
pub fn sphere(center_x: f64, center_y: f64, center_z: f64, radius: f64) -> DVector<f64> {
let center_norm_sq = center_x * center_x + center_y * center_y + center_z * center_z;
DVector::from_column_slice(&[
center_x / radius,
center_y / radius,
center_z / radius,
0.5 / radius,
0.5 * (center_norm_sq / radius - radius)
])
}
// the sphere of curvature `curv` whose closest point to the origin has position
// `off * dir` and normal `dir`, where `dir` is a unit vector. setting the
// curvature to zero gives a plane
pub fn sphere_with_offset(dir_x: f64, dir_y: f64, dir_z: f64, off: f64, curv: f64) -> DVector<f64> {
let norm_sp = 1.0 + off * curv;
DVector::from_column_slice(&[
norm_sp * dir_x,
norm_sp * dir_y,
norm_sp * dir_z,
0.5 * curv,
off * (1.0 + 0.5 * off * curv)
])
}

View File

@ -0,0 +1,7 @@
#version 300 es
in vec4 position;
void main() {
gl_Position = position;
}

View File

@ -0,0 +1,234 @@
#version 300 es
precision highp float;
out vec4 outColor;
// --- inversive geometry ---
struct vecInv {
vec3 sp;
vec2 lt;
};
// --- uniforms ---
// assembly
const int SPHERE_MAX = 200;
uniform int sphere_cnt;
uniform vecInv sphere_list[SPHERE_MAX];
uniform vec3 color_list[SPHERE_MAX];
uniform float highlight_list[SPHERE_MAX];
// view
uniform vec2 resolution;
uniform float shortdim;
// controls
uniform float opacity;
uniform int layer_threshold;
uniform bool debug_mode;
// light and camera
const float focal_slope = 0.3;
const vec3 light_dir = normalize(vec3(2., 2., 1.));
const float ixn_threshold = 0.005;
const float INTERIOR_DIMMING = 0.7;
// --- sRGB ---
// map colors from RGB space to sRGB space, as specified in the sRGB standard
// (IEC 61966-2-1:1999)
//
// https://www.color.org/sRGB.pdf
// https://www.color.org/chardata/rgb/srgb.xalter
//
// in RGB space, color value is proportional to light intensity, so linear
// color-vector interpolation corresponds to physical light mixing. in sRGB
// space, the color encoding used by many monitors, we use more of the value
// interval to represent low intensities, and less of the interval to represent
// high intensities. this improves color quantization
float sRGB(float t) {
if (t <= 0.0031308) {
return 12.92*t;
} else {
return 1.055*pow(t, 5./12.) - 0.055;
}
}
vec3 sRGB(vec3 color) {
return vec3(sRGB(color.r), sRGB(color.g), sRGB(color.b));
}
// --- shading ---
struct Fragment {
vec3 pt;
vec3 normal;
vec4 color;
};
Fragment sphere_shading(vecInv v, vec3 pt, vec3 base_color) {
// the expression for normal needs to be checked. it's supposed to give the
// negative gradient of the lorentz product between the impact point vector
// and the sphere vector with respect to the coordinates of the impact
// point. i calculated it in my head and decided that the result looked good
// enough for now
vec3 normal = normalize(-v.sp + 2.*v.lt.s*pt);
float incidence = dot(normal, light_dir);
float illum = mix(0.4, 1.0, max(incidence, 0.0));
return Fragment(pt, normal, vec4(illum * base_color, opacity));
}
float intersection_dist(Fragment a, Fragment b) {
float intersection_sin = length(cross(a.normal, b.normal));
vec3 disp = a.pt - b.pt;
return max(
abs(dot(a.normal, disp)),
abs(dot(b.normal, disp))
) / intersection_sin;
}
// --- ray-casting ---
struct TaggedDepth {
float depth;
float dimming;
int id;
};
// if `a/b` is less than this threshold, we approximate `a*u^2 + b*u + c` by
// the linear function `b*u + c`
const float DEG_THRESHOLD = 1e-9;
// the depths, represented as multiples of `dir`, where the line generated by
// `dir` hits the sphere represented by `v`. if both depths are positive, the
// smaller one is returned in the first component. if only one depth is
// positive, it could be returned in either component
vec2 sphere_cast(vecInv v, vec3 dir) {
float a = -v.lt.s * dot(dir, dir);
float b = dot(v.sp, dir);
float c = -v.lt.t;
float adjust = 4.*a*c/(b*b);
if (adjust < 1.) {
// as long as `b` is non-zero, the linear approximation of
//
// a*u^2 + b*u + c
//
// at `u = 0` will reach zero at a finite depth `u_lin`. the root of the
// quadratic adjacent to `u_lin` is stored in `lin_root`. if both roots
// have the same sign, `lin_root` will be the one closer to `u = 0`
float square_rect_ratio = 1. + sqrt(1. - adjust);
float lin_root = -(2.*c)/b / square_rect_ratio;
if (abs(a) > DEG_THRESHOLD * abs(b)) {
return vec2(lin_root, -b/(2.*a) * square_rect_ratio);
} else {
return vec2(lin_root, -1.);
}
} else {
// the line through `dir` misses the sphere completely
return vec2(-1., -1.);
}
}
void main() {
vec2 scr = (2.*gl_FragCoord.xy - resolution) / shortdim;
vec3 dir = vec3(focal_slope * scr, -1.);
// cast rays through the spheres
const int LAYER_MAX = 12;
TaggedDepth top_hits [LAYER_MAX];
int layer_cnt = 0;
for (int id = 0; id < sphere_cnt; ++id) {
// find out where the ray hits the sphere
vec2 hit_depths = sphere_cast(sphere_list[id], dir);
// insertion-sort the points we hit into the hit list
float dimming = 1.;
for (int side = 0; side < 2; ++side) {
float depth = hit_depths[side];
if (depth > 0.) {
for (int layer = layer_cnt; layer >= 0; --layer) {
if (layer < 1 || top_hits[layer-1].depth <= depth) {
// we're not as close to the screen as the hit before
// the empty slot, so insert here
if (layer < LAYER_MAX) {
top_hits[layer] = TaggedDepth(depth, dimming, id);
}
break;
} else {
// we're closer to the screen than the hit before the
// empty slot, so move that hit into the empty slot
top_hits[layer] = top_hits[layer-1];
}
}
layer_cnt = min(layer_cnt + 1, LAYER_MAX);
dimming = INTERIOR_DIMMING;
}
}
}
/* DEBUG */
// in debug mode, show the layer count instead of the shaded image
if (debug_mode) {
// at the bottom of the screen, show the color scale instead of the
// layer count
if (gl_FragCoord.y < 10.) layer_cnt = int(16. * gl_FragCoord.x / resolution.x);
// convert number to color
ivec3 bits = layer_cnt / ivec3(1, 2, 4);
vec3 color = mod(vec3(bits), 2.);
if (layer_cnt % 16 >= 8) {
color = mix(color, vec3(0.5), 0.5);
}
outColor = vec4(color, 1.);
return;
}
// composite the sphere fragments
vec3 color = vec3(0.);
int layer = layer_cnt - 1;
TaggedDepth hit = top_hits[layer];
Fragment frag_next = sphere_shading(
sphere_list[hit.id],
hit.depth * dir,
hit.dimming * color_list[hit.id]
);
float highlight_next = highlight_list[hit.id];
--layer;
for (; layer >= layer_threshold; --layer) {
// load the current fragment
Fragment frag = frag_next;
float highlight = highlight_next;
// shade the next fragment
hit = top_hits[layer];
frag_next = sphere_shading(
sphere_list[hit.id],
hit.depth * dir,
hit.dimming * color_list[hit.id]
);
highlight_next = highlight_list[hit.id];
// highlight intersections
float ixn_dist = intersection_dist(frag, frag_next);
float max_highlight = max(highlight, highlight_next);
float ixn_highlight = 0.5 * max_highlight * (1. - smoothstep(2./3.*ixn_threshold, 1.5*ixn_threshold, ixn_dist));
frag.color = mix(frag.color, vec4(1.), ixn_highlight);
frag_next.color = mix(frag_next.color, vec4(1.), ixn_highlight);
// highlight cusps
float cusp_cos = abs(dot(dir, frag.normal));
float cusp_threshold = 2.*sqrt(ixn_threshold * sphere_list[hit.id].lt.s);
float cusp_highlight = highlight * (1. - smoothstep(2./3.*cusp_threshold, 1.5*cusp_threshold, cusp_cos));
frag.color = mix(frag.color, vec4(1.), cusp_highlight);
// composite the current fragment
color = mix(color, frag.color.rgb, frag.color.a);
}
color = mix(color, frag_next.color.rgb, frag_next.color.a);
outColor = vec4(sRGB(color), 1.);
}

42
app-proto/src/main.rs Normal file
View File

@ -0,0 +1,42 @@
mod add_remove;
mod assembly;
mod display;
mod engine;
mod outline;
use rustc_hash::FxHashSet;
use sycamore::prelude::*;
use add_remove::AddRemove;
use assembly::Assembly;
use display::Display;
use outline::Outline;
#[derive(Clone)]
struct AppState {
assembly: Assembly,
selection: Signal<FxHashSet<usize>>
}
impl AppState {
fn new() -> AppState {
AppState {
assembly: Assembly::new(),
selection: create_signal(FxHashSet::default())
}
}
}
fn main() {
sycamore::render(|| {
provide_context(AppState::new());
view! {
div(id="sidebar") {
AddRemove {}
Outline {}
}
Display {}
}
});
}

161
app-proto/src/outline.rs Normal file
View File

@ -0,0 +1,161 @@
use itertools::Itertools;
use sycamore::{prelude::*, web::tags::div};
use web_sys::{Element, KeyboardEvent, MouseEvent, wasm_bindgen::JsCast};
use crate::AppState;
// this component lists the elements of the assembly, showing the constraints
// on each element as a collapsible sub-list. its implementation is based on
// Kate Morley's HTML + CSS tree views:
//
// https://iamkate.com/code/tree-views/
//
#[component]
pub fn Outline() -> View {
// sort the elements alphabetically by ID
let elements_sorted = create_memo(|| {
let state = use_context::<AppState>();
state.assembly.elements
.get_clone()
.into_iter()
.sorted_by_key(|(_, elt)| elt.id.clone())
.collect()
});
view! {
ul(
id="outline",
on:click={
let state = use_context::<AppState>();
move |_| state.selection.update(|sel| sel.clear())
}
) {
Keyed(
list=elements_sorted,
view=|(key, elt)| {
let state = use_context::<AppState>();
let class = create_memo({
move || {
if state.selection.with(|sel| sel.contains(&key)) {
"selected"
} else {
""
}
}
});
let label = elt.label.clone();
let rep_components = elt.rep.iter().map(|u| {
let u_coord = u.to_string().replace("-", "\u{2212}");
View::from(div().children(u_coord))
}).collect::<Vec<_>>();
let constrained = elt.constraints.len() > 0;
let details_node = create_node_ref();
view! {
/* [TO DO] switch to integer-valued parameters whenever
that becomes possible again */
li {
details(ref=details_node) {
summary(
class=class.get(),
on:keydown={
move |event: KeyboardEvent| {
match event.key().as_str() {
"Enter" => {
if event.shift_key() {
state.selection.update(|sel| {
if !sel.remove(&key) {
sel.insert(key);
}
});
} else {
state.selection.update(|sel| {
sel.clear();
sel.insert(key);
});
}
event.prevent_default();
},
"ArrowRight" if constrained => {
let _ = details_node
.get()
.unchecked_into::<Element>()
.set_attribute("open", "");
},
"ArrowLeft" => {
let _ = details_node
.get()
.unchecked_into::<Element>()
.remove_attribute("open");
},
_ => ()
}
}
}
) {
div(
class="elt-switch",
on:click=|event: MouseEvent| event.stop_propagation()
)
div(
class="elt",
on:click={
move |event: MouseEvent| {
if event.shift_key() {
state.selection.update(|sel| {
if !sel.remove(&key) {
sel.insert(key);
}
});
} else {
state.selection.update(|sel| {
sel.clear();
sel.insert(key);
});
}
event.stop_propagation();
event.prevent_default();
}
}
) {
div(class="elt-label") { (label) }
div(class="elt-rep") { (rep_components) }
}
}
ul(class="constraints") {
Keyed(
list=elt.constraints.into_iter().collect::<Vec<_>>(),
view=move |c_key: usize| {
let c_state = use_context::<AppState>();
let assembly = &c_state.assembly;
let cst = assembly.constraints.with(|csts| csts[c_key].clone());
let other_arg = if cst.args.0 == key {
cst.args.1
} else {
cst.args.0
};
let other_arg_label = assembly.elements.with(|elts| elts[other_arg].label.clone());
view! {
li(class="cst") {
input(r#type="checkbox", bind:checked=cst.active)
div(class="cst-label") { (other_arg_label) }
div(class="cst-rep") { (cst.rep) }
}
}
},
key=|c_key| c_key.clone()
)
}
}
}
}
},
key=|(key, elt)| (
key.clone(),
elt.id.clone(),
elt.label.clone(),
elt.constraints.clone()
)
)
}
}
}