From 81e423fcbe9ea13c67e720192e63cc7cfc99ecd2 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Tue, 8 Apr 2025 18:49:48 -0700 Subject: [PATCH] Give every sphere a curvature regulator In the process, fix a reactivity bug by removing unintended signal tracking from `insert_regulator`. --- app-proto/src/add_remove.rs | 28 ++++++++++++++-------------- app-proto/src/assembly.rs | 36 ++++++++++++++++++++---------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/app-proto/src/add_remove.rs b/app-proto/src/add_remove.rs index f44f5c0..573bd36 100644 --- a/app-proto/src/add_remove.rs +++ b/app-proto/src/add_remove.rs @@ -11,7 +11,7 @@ use crate::{ // load an example assembly for testing. this code will be removed once we've // built a more formal test assembly system fn load_gen_assemb(assembly: &Assembly) { - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( String::from("gemini_a"), String::from("Castor"), @@ -19,7 +19,7 @@ fn load_gen_assemb(assembly: &Assembly) { engine::sphere(0.5, 0.5, 0.0, 1.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( String::from("gemini_b"), String::from("Pollux"), @@ -27,7 +27,7 @@ fn load_gen_assemb(assembly: &Assembly) { engine::sphere(-0.5, -0.5, 0.0, 1.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( String::from("ursa_major"), String::from("Ursa major"), @@ -35,7 +35,7 @@ fn load_gen_assemb(assembly: &Assembly) { engine::sphere(-0.5, 0.5, 0.0, 0.75) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( String::from("ursa_minor"), String::from("Ursa minor"), @@ -43,7 +43,7 @@ fn load_gen_assemb(assembly: &Assembly) { engine::sphere(0.5, -0.5, 0.0, 0.5) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( String::from("moon_deimos"), String::from("Deimos"), @@ -51,7 +51,7 @@ fn load_gen_assemb(assembly: &Assembly) { engine::sphere(0.0, 0.15, 1.0, 0.25) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( String::from("moon_phobos"), String::from("Phobos"), @@ -66,7 +66,7 @@ fn load_gen_assemb(assembly: &Assembly) { // built a more formal test assembly system fn load_low_curv_assemb(assembly: &Assembly) { let a = 0.75_f64.sqrt(); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( "central".to_string(), "Central".to_string(), @@ -74,7 +74,7 @@ fn load_low_curv_assemb(assembly: &Assembly) { engine::sphere(0.0, 0.0, 0.0, 1.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( "assemb_plane".to_string(), "Assembly plane".to_string(), @@ -82,7 +82,7 @@ fn load_low_curv_assemb(assembly: &Assembly) { engine::sphere_with_offset(0.0, 0.0, 1.0, 0.0, 0.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( "side1".to_string(), "Side 1".to_string(), @@ -90,7 +90,7 @@ fn load_low_curv_assemb(assembly: &Assembly) { engine::sphere_with_offset(1.0, 0.0, 0.0, 1.0, 0.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( "side2".to_string(), "Side 2".to_string(), @@ -98,7 +98,7 @@ fn load_low_curv_assemb(assembly: &Assembly) { engine::sphere_with_offset(-0.5, a, 0.0, 1.0, 0.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( "side3".to_string(), "Side 3".to_string(), @@ -106,7 +106,7 @@ fn load_low_curv_assemb(assembly: &Assembly) { engine::sphere_with_offset(-0.5, -a, 0.0, 1.0, 0.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( "corner1".to_string(), "Corner 1".to_string(), @@ -114,7 +114,7 @@ fn load_low_curv_assemb(assembly: &Assembly) { engine::sphere(-4.0/3.0, 0.0, 0.0, 1.0/3.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( "corner2".to_string(), "Corner 2".to_string(), @@ -122,7 +122,7 @@ fn load_low_curv_assemb(assembly: &Assembly) { engine::sphere(2.0/3.0, -4.0/3.0 * a, 0.0, 1.0/3.0) ) ); - let _ = assembly.try_insert_element( + let _ = assembly.try_insert_sphere( Element::new( String::from("corner3"), String::from("Corner 3"), diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 33c8c91..387b567 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -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 { + pub fn try_insert_sphere(&self, elt: Element) -> Option { 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) { @@ -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(); } }); }