Generalize constraints to observables

This commit is contained in:
Aaron Fenyes 2025-01-25 13:00:18 -08:00
parent 46324fecc6
commit fb8e391587
4 changed files with 77 additions and 35 deletions

View file

@ -131,10 +131,6 @@ details[open]:has(li) .element-switch::after {
color: var(--text-invalid); color: var(--text-invalid);
} }
.constraint > input[type=checkbox] {
margin: 0px 8px 0px 0px;
}
.constraint > input[type=text] { .constraint > input[type=text] {
color: inherit; color: inherit;
background-color: inherit; background-color: inherit;
@ -154,6 +150,10 @@ details[open]:has(li) .element-switch::after {
font-style: normal; font-style: normal;
} }
.constrained > .status::after, details:has(.constrained):not([open]) .status::after {
content: '🔗';
}
.invalid > .status::after, details:has(.invalid):not([open]) .status::after { .invalid > .status::after, details:has(.invalid):not([open]) .status::after {
content: '⚠'; content: '⚠';
color: var(--text-invalid); color: var(--text-invalid);

View file

@ -1,7 +1,17 @@
use sycamore::prelude::*; use sycamore::prelude::*;
use web_sys::{console, wasm_bindgen::JsValue}; use web_sys::{console, wasm_bindgen::JsValue};
use crate::{engine, AppState, assembly::{Assembly, Constraint, Element}}; use crate::{
engine,
AppState,
assembly::{
Assembly,
Constraint,
ConstraintRole,
Element
},
engine::Q
};
/* DEBUG */ /* DEBUG */
// load an example assembly for testing. this code will be removed once we've // load an example assembly for testing. this code will be removed once we've
@ -190,15 +200,23 @@ pub fn AddRemove() -> View {
(subject_vec[0].clone(), subject_vec[1].clone()) (subject_vec[0].clone(), subject_vec[1].clone())
} }
); );
let lorentz_prod = create_signal(0.0); let measured = state.assembly.elements.map(
let lorentz_prod_valid = create_signal(false); move |elts| {
let active = create_signal(true); let reps = (
elts[subjects.0].representation.get_clone(),
elts[subjects.1].representation.get_clone()
);
reps.0.dot(&(&*Q * reps.1))
}
);
let desired = create_signal(0.0);
let role = create_signal(ConstraintRole::Measure);
state.assembly.insert_constraint(Constraint { state.assembly.insert_constraint(Constraint {
subjects: subjects, subjects: subjects,
lorentz_prod: lorentz_prod, measured: measured,
lorentz_prod_text: create_signal(String::new()), desired: desired,
lorentz_prod_valid: lorentz_prod_valid, desired_text: create_signal(String::new()),
active: active, role: role,
}); });
state.selection.update(|sel| sel.clear()); state.selection.update(|sel| sel.clear());
@ -212,19 +230,19 @@ pub fn AddRemove() -> View {
&JsValue::from(cst.subjects.0), &JsValue::from(cst.subjects.0),
&JsValue::from(cst.subjects.1), &JsValue::from(cst.subjects.1),
&JsValue::from(":"), &JsValue::from(":"),
&JsValue::from(cst.lorentz_prod.get_untracked()) &JsValue::from(cst.desired.get_untracked())
); );
} }
}); });
// update the realization when the constraint becomes active // update the realization when the observable becomes
// and valid, or is edited while active and valid // constrained, or is edited while constrained
create_effect(move || { create_effect(move || {
console::log_1(&JsValue::from( console::log_1(&JsValue::from(
format!("Constraint ({}, {}) updated", subjects.0, subjects.1) format!("Constraint ({}, {}) updated", subjects.0, subjects.1)
)); ));
lorentz_prod.track(); desired.track();
if active.get() && lorentz_prod_valid.get() { if role.with(|r| matches!(r, ConstraintRole::Constrain)) {
state.assembly.realize(); state.assembly.realize();
} }
}); });

View file

@ -111,13 +111,19 @@ impl Element {
} }
} }
pub enum ConstraintRole {
Measure,
Constrain,
Invalid
}
#[derive(Clone)] #[derive(Clone)]
pub struct Constraint { pub struct Constraint {
pub subjects: (ElementKey, ElementKey), pub subjects: (ElementKey, ElementKey),
pub lorentz_prod: Signal<f64>, pub measured: ReadSignal<f64>,
pub lorentz_prod_text: Signal<String>, pub desired: Signal<f64>,
pub lorentz_prod_valid: Signal<bool>, pub desired_text: Signal<String>,
pub active: Signal<bool> pub role: Signal<ConstraintRole>
} }
// the velocity is expressed in uniform coordinates // the velocity is expressed in uniform coordinates
@ -230,11 +236,11 @@ impl Assembly {
let mut gram_to_be = PartialMatrix::new(); let mut gram_to_be = PartialMatrix::new();
self.constraints.with_untracked(|csts| { self.constraints.with_untracked(|csts| {
for (_, cst) in csts { for (_, cst) in csts {
if cst.active.get_untracked() && cst.lorentz_prod_valid.get_untracked() { if cst.role.with_untracked(|role| matches!(role, ConstraintRole::Constrain)) {
let subjects = cst.subjects; let subjects = cst.subjects;
let row = elts[subjects.0].column_index.unwrap(); let row = elts[subjects.0].column_index.unwrap();
let col = elts[subjects.1].column_index.unwrap(); let col = elts[subjects.1].column_index.unwrap();
gram_to_be.push_sym(row, col, cst.lorentz_prod.get_untracked()); gram_to_be.push_sym(row, col, cst.desired.get_untracked());
} }
} }
}); });

View file

@ -8,7 +8,16 @@ use web_sys::{
wasm_bindgen::JsCast wasm_bindgen::JsCast
}; };
use crate::{AppState, assembly, assembly::{Constraint, ConstraintKey, ElementKey}}; use crate::{
AppState,
assembly,
assembly::{
Constraint,
ConstraintKey,
ConstraintRole::*,
ElementKey
}
};
// an editable view of the Lorentz product representing a constraint // an editable view of the Lorentz product representing a constraint
#[component(inline_props)] #[component(inline_props)]
@ -16,16 +25,22 @@ fn LorentzProductInput(constraint: Constraint) -> View {
view! { view! {
input( input(
r#type="text", r#type="text",
bind:value=constraint.lorentz_prod_text, placeholder=constraint.measured.with(|result| result.to_string()),
bind:value=constraint.desired_text,
on:change=move |event: Event| { on:change=move |event: Event| {
let target: HtmlInputElement = event.target().unwrap().unchecked_into(); let target: HtmlInputElement = event.target().unwrap().unchecked_into();
match target.value().parse::<f64>() { let value = target.value();
Ok(lorentz_prod) => batch(|| { if value.is_empty() {
constraint.lorentz_prod.set(lorentz_prod); constraint.role.set(Measure);
constraint.lorentz_prod_valid.set(true); } else {
}), match target.value().parse::<f64>() {
Err(_) => constraint.lorentz_prod_valid.set(false) Ok(desired) => batch(|| {
}; constraint.desired.set(desired);
constraint.role.set(Constrain);
}),
Err(_) => constraint.role.set(Invalid)
};
}
} }
) )
} }
@ -43,12 +58,15 @@ fn ConstraintOutlineItem(constraint_key: ConstraintKey, element_key: ElementKey)
constraint.subjects.0 constraint.subjects.0
}; };
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone()); let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
let class = constraint.lorentz_prod_valid.map( let class = constraint.role.map(
|&lorentz_prod_valid| if lorentz_prod_valid { "constraint" } else { "constraint invalid" } |role| match role {
Measure => "constraint",
Constrain => "constraint constrained",
Invalid => "constraint invalid"
}
); );
view! { view! {
li(class=class.get()) { li(class=class.get()) {
input(r#type="checkbox", bind:checked=constraint.active)
div(class="constraint-label") { (other_subject_label) } div(class="constraint-label") { (other_subject_label) }
LorentzProductInput(constraint=constraint) LorentzProductInput(constraint=constraint)
div(class="status") div(class="status")