Drop unused context

Using pointers to refer to elements reduces the need to drag context
around.
This commit is contained in:
Aaron Fenyes 2025-05-04 01:19:00 -07:00
parent 5191534886
commit fbd6177a07
3 changed files with 25 additions and 34 deletions

View file

@ -241,7 +241,7 @@ pub fn AddRemove() -> View {
.unwrap() .unwrap()
); );
state.assembly.insert_regulator( state.assembly.insert_regulator(
Rc::new(InversiveDistanceRegulator::new(subjects, &state.assembly)) Rc::new(InversiveDistanceRegulator::new(subjects))
); );
state.selection.update(|sel| sel.clear()); state.selection.update(|sel| sel.clear());
} }

View file

@ -44,24 +44,18 @@ pub type ElementColor = [f32; 3];
static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0); static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0);
pub trait ProblemPoser { pub trait ProblemPoser {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Rc<dyn Element>>); fn pose(&self, problem: &mut ConstraintProblem);
} }
pub trait Element: ProblemPoser + DisplayItem { pub trait Element: ProblemPoser + DisplayItem {
// the default identifier for an element of this type // the default identifier for an element of this type
fn default_id() -> String where Self: Sized; fn default_id() -> String where Self: Sized;
// create the default example of an element of this type // the default example of an element of this type
fn default(id: String, id_num: u64) -> Self where Self: Sized; fn default(id: String, id_num: u64) -> Self where Self: Sized;
// the regulators that should be created when an element of this type is // the default regulators that come with this element
// inserted into the given assembly with the given storage key fn default_regulators(self: Rc<Self>) -> Vec<Rc<dyn Regulator>> {
/* KLUDGE */
// this organization made sense when regulators identified their subjects by
// storage key, so the element has to be inserted before its regulators
// could be created. now that regulators identify their subjects by pointer,
// we should consider refactoring
fn default_regulators(self: Rc<Self>, _assembly: &Assembly) -> Vec<Rc<dyn Regulator>> where Self: Sized {
Vec::new() Vec::new()
} }
@ -165,8 +159,8 @@ impl Element for Sphere {
) )
} }
fn default_regulators(self: Rc<Self>, assembly: &Assembly) -> Vec<Rc<dyn Regulator>> { fn default_regulators(self: Rc<Self>) -> Vec<Rc<dyn Regulator>> {
vec![Rc::new(HalfCurvatureRegulator::new(self, assembly))] vec![Rc::new(HalfCurvatureRegulator::new(self))]
} }
fn id(&self) -> &String { fn id(&self) -> &String {
@ -199,7 +193,7 @@ impl Element for Sphere {
} }
impl ProblemPoser for Sphere { impl ProblemPoser for Sphere {
fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab<Rc<dyn Element>>) { fn pose(&self, problem: &mut ConstraintProblem) {
let index = self.column_index().expect( let index = self.column_index().expect(
format!("Sphere \"{}\" should be indexed before writing problem data", self.id).as_str() format!("Sphere \"{}\" should be indexed before writing problem data", self.id).as_str()
); );
@ -283,7 +277,7 @@ impl Element for Point {
} }
impl ProblemPoser for Point { impl ProblemPoser for Point {
fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab<Rc<dyn Element>>) { fn pose(&self, problem: &mut ConstraintProblem) {
let index = self.column_index().expect( let index = self.column_index().expect(
format!("Point \"{}\" should be indexed before writing problem data", self.id).as_str() format!("Point \"{}\" should be indexed before writing problem data", self.id).as_str()
); );
@ -304,7 +298,7 @@ pub trait Regulator: ProblemPoser + OutlineItem {
// preconditioning when the set point is present, and use its return value // preconditioning when the set point is present, and use its return value
// to report whether the set is present. the default implementation does no // to report whether the set is present. the default implementation does no
// preconditioning // preconditioning
fn try_activate(&self, _assembly: &Assembly) -> bool { fn try_activate(&self) -> bool {
self.set_point().with(|set_pt| set_pt.is_present()) self.set_point().with(|set_pt| set_pt.is_present())
} }
} }
@ -316,7 +310,7 @@ pub struct InversiveDistanceRegulator {
} }
impl InversiveDistanceRegulator { impl InversiveDistanceRegulator {
pub fn new(subjects: [Rc<dyn Element>; 2], assembly: &Assembly) -> InversiveDistanceRegulator { pub fn new(subjects: [Rc<dyn Element>; 2]) -> InversiveDistanceRegulator {
let representations = subjects.each_ref().map(|subj| subj.representation()); let representations = subjects.each_ref().map(|subj| subj.representation());
let measurement = create_memo(move || { let measurement = create_memo(move || {
representations[0].with(|rep_0| representations[0].with(|rep_0|
@ -347,7 +341,7 @@ impl Regulator for InversiveDistanceRegulator {
} }
impl ProblemPoser for InversiveDistanceRegulator { impl ProblemPoser for InversiveDistanceRegulator {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Rc<dyn Element>>) { fn pose(&self, problem: &mut ConstraintProblem) {
self.set_point.with_untracked(|set_pt| { self.set_point.with_untracked(|set_pt| {
if let Some(val) = set_pt.value { if let Some(val) = set_pt.value {
let [row, col] = self.subjects.each_ref().map( let [row, col] = self.subjects.each_ref().map(
@ -368,7 +362,7 @@ pub struct HalfCurvatureRegulator {
} }
impl HalfCurvatureRegulator { impl HalfCurvatureRegulator {
pub fn new(subject: Rc<dyn Element>, assembly: &Assembly) -> HalfCurvatureRegulator { pub fn new(subject: Rc<dyn Element>) -> HalfCurvatureRegulator {
let measurement = subject.representation().map( let measurement = subject.representation().map(
|rep| rep[Sphere::CURVATURE_COMPONENT] |rep| rep[Sphere::CURVATURE_COMPONENT]
); );
@ -392,7 +386,7 @@ impl Regulator for HalfCurvatureRegulator {
self.set_point self.set_point
} }
fn try_activate(&self, assembly: &Assembly) -> bool { fn try_activate(&self) -> bool {
match self.set_point.with(|set_pt| set_pt.value) { match self.set_point.with(|set_pt| set_pt.value) {
Some(half_curv) => { Some(half_curv) => {
self.subject.representation().update( self.subject.representation().update(
@ -406,7 +400,7 @@ impl Regulator for HalfCurvatureRegulator {
} }
impl ProblemPoser for HalfCurvatureRegulator { impl ProblemPoser for HalfCurvatureRegulator {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Rc<dyn Element>>) { fn pose(&self, problem: &mut ConstraintProblem) {
self.set_point.with_untracked(|set_pt| { self.set_point.with_untracked(|set_pt| {
if let Some(val) = set_pt.value { if let Some(val) = set_pt.value {
let col = self.subject.column_index().expect( let col = self.subject.column_index().expect(
@ -468,11 +462,11 @@ impl Assembly {
// insert the element // insert the element
let id = elt.id().clone(); let id = elt.id().clone();
let elt_rc = Rc::new(elt); let elt_rc = Rc::new(elt);
let key = self.elements.update(|elts| elts.insert(elt_rc.clone())); /* KLUDGE */ // reorganize to avoid cloning? let key = self.elements.update(|elts| elts.insert(elt_rc.clone()));
self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, elt_rc.clone())); /* KLUDGE */ // reorganize to avoid cloning? self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, elt_rc.clone()));
// create and insert the element's default regulators // create and insert the element's default regulators
for reg in elt_rc.default_regulators(&self) { for reg in elt_rc.default_regulators() {
self.insert_regulator(reg); self.insert_regulator(reg);
} }
@ -513,8 +507,7 @@ impl Assembly {
); );
// add the regulator to each subject's regulator list // add the regulator to each subject's regulator list
let subjects = regulator.subjects(); let subject_regulators: Vec<_> = regulator.subjects().into_iter().map(
let subject_regulators: Vec<_> = subjects.into_iter().map(
|subj| subj.regulators() |subj| subj.regulators()
).collect(); ).collect();
for regulators in subject_regulators { for regulators in subject_regulators {
@ -531,7 +524,7 @@ impl Assembly {
format!("Updated regulator with subjects {:?}", regulator.subjects()) format!("Updated regulator with subjects {:?}", regulator.subjects())
)); ));
if regulator.try_activate(&self_for_effect) { if regulator.try_activate() {
self_for_effect.realize(); self_for_effect.realize();
} }
}); });
@ -573,11 +566,11 @@ impl Assembly {
let problem = self.elements.with_untracked(|elts| { let problem = self.elements.with_untracked(|elts| {
let mut problem = ConstraintProblem::new(elts.len()); let mut problem = ConstraintProblem::new(elts.len());
for (_, elt) in elts { for (_, elt) in elts {
elt.pose(&mut problem, elts); elt.pose(&mut problem);
} }
self.regulators.with_untracked(|regs| { self.regulators.with_untracked(|regs| {
for (_, reg) in regs { for (_, reg) in regs {
reg.pose(&mut problem, elts); reg.pose(&mut problem);
} }
}); });
problem problem

View file

@ -11,7 +11,6 @@ use crate::{
AppState, AppState,
assembly::{ assembly::{
Element, Element,
ElementKey,
HalfCurvatureRegulator, HalfCurvatureRegulator,
InversiveDistanceRegulator, InversiveDistanceRegulator,
Regulator, Regulator,
@ -96,7 +95,6 @@ pub trait OutlineItem {
impl OutlineItem for InversiveDistanceRegulator { impl OutlineItem for InversiveDistanceRegulator {
fn outline_item(self: Rc<Self>, element: Rc<dyn Element>) -> View { fn outline_item(self: Rc<Self>, element: Rc<dyn Element>) -> View {
let state = use_context::<AppState>();
let other_subject_label = if self.subjects[0] == element { let other_subject_label = if self.subjects[0] == element {
self.subjects[1].label() self.subjects[1].label()
} else { } else {
@ -138,7 +136,7 @@ fn RegulatorOutlineItem(regulator_key: RegulatorKey, element: Rc<dyn Element>) -
// a list item that shows an element in an outline view of an assembly // a list item that shows an element in an outline view of an assembly
#[component(inline_props)] #[component(inline_props)]
fn ElementOutlineItem(key: ElementKey, element: Rc<dyn Element>) -> View { fn ElementOutlineItem(element: Rc<dyn Element>) -> View {
let state = use_context::<AppState>(); let state = use_context::<AppState>();
let class = { let class = {
let element_for_class = element.clone(); let element_for_class = element.clone();
@ -271,8 +269,8 @@ pub fn Outline() -> View {
) { ) {
Keyed( Keyed(
list=element_list, list=element_list,
view=|(key, elt)| view! { view=|(_, elt)| view! {
ElementOutlineItem(key=key, element=elt) ElementOutlineItem(element=elt)
}, },
key=|(_, elt)| elt.serial() key=|(_, elt)| elt.serial()
) )