Give each regulator a constructor

The assembly shouldn't have to know how to construct regulators.
This commit is contained in:
Aaron Fenyes 2025-04-17 14:10:07 -07:00
parent 8f8e806d12
commit 52d99755f9
2 changed files with 46 additions and 37 deletions

View file

@ -4,7 +4,7 @@ use web_sys::{console, wasm_bindgen::JsValue};
use crate::{
engine,
AppState,
assembly::{Assembly, Element}
assembly::{Assembly, Element, ProductRegulator}
};
/* DEBUG */
@ -189,7 +189,9 @@ pub fn AddRemove() -> View {
.try_into()
.unwrap()
);
state.assembly.insert_new_product_regulator(subjects);
state.assembly.insert_regulator(
ProductRegulator::new(subjects, &state.assembly)
);
state.selection.update(|sel| sel.clear());
}
) { "🔗" }

View file

@ -155,6 +155,29 @@ pub struct ProductRegulator {
pub set_point: Signal<SpecifiedValue>
}
impl ProductRegulator {
pub fn new(subjects: [ElementKey; 2], assembly: &Assembly) -> ProductRegulator {
let measurement = assembly.elements.map(
move |elts| {
let representations = subjects.map(|subj| elts[subj].representation);
representations[0].with(|rep_0|
representations[1].with(|rep_1|
rep_0.dot(&(&*Q * rep_1))
)
)
}
);
let set_point = create_signal(SpecifiedValue::from_empty_spec());
ProductRegulator {
subjects: subjects,
measurement: measurement,
set_point: set_point
}
}
}
impl Regulator for ProductRegulator {
fn subjects(&self) -> Vec<ElementKey> {
self.subjects.into()
@ -192,6 +215,23 @@ pub struct HalfCurvatureRegulator {
pub set_point: Signal<SpecifiedValue>
}
impl HalfCurvatureRegulator {
pub fn new(subject: ElementKey, assembly: &Assembly) -> HalfCurvatureRegulator {
const CURVATURE_COMPONENT: usize = 3;
let measurement = assembly.elements.map(
move |elts| elts[subject].representation.with(|rep| rep[CURVATURE_COMPONENT])
);
let set_point = create_signal(SpecifiedValue::from_empty_spec());
HalfCurvatureRegulator {
subject: subject,
measurement: measurement,
set_point: set_point
}
}
}
impl Regulator for HalfCurvatureRegulator {
fn subjects(&self) -> Vec<ElementKey> {
vec![self.subject]
@ -285,7 +325,7 @@ impl Assembly {
self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, key));
// regulate the sphere's curvature
self.insert_new_half_curvature_regulator(key);
self.insert_regulator(HalfCurvatureRegulator::new(key, &self));
key
}
@ -323,7 +363,7 @@ impl Assembly {
);
}
fn insert_regulator<T: Regulator + 'static>(&self, regulator: T) {
pub fn insert_regulator<T: Regulator + 'static>(&self, regulator: T) {
// add the regulator to the assembly's regulator list
let regulator_rc = Rc::new(regulator);
let key = self.regulators.update(
@ -377,39 +417,6 @@ impl Assembly {
});
}
pub fn insert_new_product_regulator(&self, subjects: [ElementKey; 2]) {
// create and insert a new product regulator
let measurement = self.elements.map(
move |elts| {
let representations = subjects.map(|subj| elts[subj].representation);
representations[0].with(|rep_0|
representations[1].with(|rep_1|
rep_0.dot(&(&*Q * rep_1))
)
)
}
);
let set_point = create_signal(SpecifiedValue::from_empty_spec());
self.insert_regulator(ProductRegulator {
subjects: subjects,
measurement: measurement,
set_point: set_point
});
}
pub fn insert_new_half_curvature_regulator(&self, subject: ElementKey) {
// create and insert a new half-curvature regulator
let measurement = self.elements.map(
move |elts| elts[subject].representation.with(|rep| rep[3])
);
let set_point = create_signal(SpecifiedValue::from_empty_spec());
self.insert_regulator(HalfCurvatureRegulator {
subject: subject,
measurement: measurement,
set_point: set_point
});
}
// --- realization ---
pub fn realize(&self) {