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

@ -1,7 +1,17 @@
use sycamore::prelude::*;
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 */
// 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())
}
);
let lorentz_prod = create_signal(0.0);
let lorentz_prod_valid = create_signal(false);
let active = create_signal(true);
let measured = state.assembly.elements.map(
move |elts| {
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 {
subjects: subjects,
lorentz_prod: lorentz_prod,
lorentz_prod_text: create_signal(String::new()),
lorentz_prod_valid: lorentz_prod_valid,
active: active,
measured: measured,
desired: desired,
desired_text: create_signal(String::new()),
role: role,
});
state.selection.update(|sel| sel.clear());
@ -212,19 +230,19 @@ pub fn AddRemove() -> View {
&JsValue::from(cst.subjects.0),
&JsValue::from(cst.subjects.1),
&JsValue::from(":"),
&JsValue::from(cst.lorentz_prod.get_untracked())
&JsValue::from(cst.desired.get_untracked())
);
}
});
// update the realization when the constraint becomes active
// and valid, or is edited while active and valid
// update the realization when the observable becomes
// constrained, or is edited while constrained
create_effect(move || {
console::log_1(&JsValue::from(
format!("Constraint ({}, {}) updated", subjects.0, subjects.1)
));
lorentz_prod.track();
if active.get() && lorentz_prod_valid.get() {
desired.track();
if role.with(|r| matches!(r, ConstraintRole::Constrain)) {
state.assembly.realize();
}
});