Give every sphere a curvature regulator
All checks were successful
/ test (pull_request) Successful in 2m24s
All checks were successful
/ test (pull_request) Successful in 2m24s
In the process, fix a reactivity bug by removing unintended signal tracking from `insert_regulator`.
This commit is contained in:
parent
63e3d733ba
commit
81e423fcbe
2 changed files with 34 additions and 30 deletions
|
@ -261,28 +261,33 @@ impl Assembly {
|
|||
|
||||
// --- inserting elements and regulators ---
|
||||
|
||||
// insert an element into the assembly without checking whether we already
|
||||
// insert a sphere 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) -> ElementKey {
|
||||
fn insert_sphere_unchecked(&self, elt: Element) -> ElementKey {
|
||||
// insert the sphere
|
||||
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));
|
||||
|
||||
// regulate the sphere's curvature
|
||||
self.insert_new_half_curvature_regulator(key);
|
||||
|
||||
key
|
||||
}
|
||||
|
||||
pub fn try_insert_element(&self, elt: Element) -> Option<ElementKey> {
|
||||
pub fn try_insert_sphere(&self, elt: Element) -> Option<ElementKey> {
|
||||
let can_insert = self.elements_by_id.with_untracked(
|
||||
|elts_by_id| !elts_by_id.contains_key(&elt.id)
|
||||
);
|
||||
if can_insert {
|
||||
Some(self.insert_element_unchecked(elt))
|
||||
Some(self.insert_sphere_unchecked(elt))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_new_sphere(self) {
|
||||
pub fn insert_new_sphere(&self) {
|
||||
// find the next unused identifier in the default sequence
|
||||
let mut id_num = 1;
|
||||
let mut id = format!("sphere{}", id_num);
|
||||
|
@ -294,7 +299,7 @@ impl Assembly {
|
|||
}
|
||||
|
||||
// create and insert a sphere
|
||||
let key = self.insert_element_unchecked(
|
||||
let _ = self.insert_sphere_unchecked(
|
||||
Element::new(
|
||||
id,
|
||||
format!("Sphere {}", id_num),
|
||||
|
@ -302,9 +307,6 @@ impl Assembly {
|
|||
sphere(0.0, 0.0, 0.0, 1.0)
|
||||
)
|
||||
);
|
||||
|
||||
// create and insert a curvature regulator
|
||||
self.insert_new_half_curvature_regulator(key);
|
||||
}
|
||||
|
||||
fn insert_regulator(&self, regulator: Rc<dyn Regulator>) {
|
||||
|
@ -312,7 +314,7 @@ impl Assembly {
|
|||
let key = self.regulators.update(
|
||||
|regs| regs.insert(regulator)
|
||||
);
|
||||
let subject_regulators: Vec<_> = self.elements.with(
|
||||
let subject_regulators: Vec<_> = self.elements.with_untracked(
|
||||
|elts| subjects.into_iter().map(
|
||||
|subj| elts[subj].regulators
|
||||
).collect()
|
||||
|
@ -324,7 +326,7 @@ impl Assembly {
|
|||
/* DEBUG */
|
||||
// print an updated list of regulators
|
||||
console::log_1(&JsValue::from("Regulators:"));
|
||||
self.regulators.with(|regs| {
|
||||
self.regulators.with_untracked(|regs| {
|
||||
for (_, reg) in regs.into_iter() {
|
||||
console::log_1(&JsValue::from(format!(
|
||||
" {:?}: {}",
|
||||
|
@ -344,7 +346,7 @@ impl Assembly {
|
|||
});
|
||||
}
|
||||
|
||||
pub fn insert_new_product_regulator(self, subjects: [ElementKey; 2]) {
|
||||
pub fn insert_new_product_regulator(&self, subjects: [ElementKey; 2]) {
|
||||
// create and insert a new product regulator
|
||||
let measurement = self.elements.map(
|
||||
move |elts| {
|
||||
|
@ -365,17 +367,18 @@ impl Assembly {
|
|||
|
||||
// update the realization when the regulator becomes a constraint, or is
|
||||
// edited while acting as a constraint
|
||||
let self_for_effect = self.clone();
|
||||
create_effect(move || {
|
||||
console::log_1(&JsValue::from(
|
||||
format!("Updated regulator with subjects {:?}", subjects)
|
||||
));
|
||||
if set_point.with(|set_pt| set_pt.is_present()) {
|
||||
self.realize();
|
||||
self_for_effect.realize();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn insert_new_half_curvature_regulator(self, subject: ElementKey) {
|
||||
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])
|
||||
|
@ -389,12 +392,13 @@ impl Assembly {
|
|||
|
||||
// update the realization when the regulator becomes a constraint, or is
|
||||
// edited while acting as a constraint
|
||||
let self_for_effect = self.clone();
|
||||
create_effect(move || {
|
||||
console::log_1(&JsValue::from(
|
||||
format!("Updated regulator with subjects [{}]", subject)
|
||||
));
|
||||
if let Some(half_curv) = set_point.with(|set_pt| set_pt.value) {
|
||||
let representation = self.elements.with(
|
||||
let representation = self_for_effect.elements.with_untracked(
|
||||
|elts| elts[subject].representation
|
||||
);
|
||||
representation.update(|rep| {
|
||||
|
@ -428,7 +432,7 @@ impl Assembly {
|
|||
)
|
||||
));
|
||||
});
|
||||
self.realize();
|
||||
self_for_effect.realize();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue