From 63e3d733ba29c8053cda857ca7cd771cd10400fb Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Thu, 3 Apr 2025 14:13:45 -0700 Subject: [PATCH] Introduce a problem poser trait Elements and regulators use this common interface to write their data into the constraint problem. --- app-proto/src/assembly.rs | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index f41397b..33c8c91 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -31,6 +31,10 @@ pub type ElementColor = [f32; 3]; // each assembly has a key that identifies it within the sesssion static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0); +pub trait ProblemPoser { + fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab); +} + #[derive(Clone, PartialEq)] pub struct Element { pub id: String, @@ -123,8 +127,10 @@ impl Element { None } } - - fn write_to_problem(&self, problem: &mut ConstraintProblem) { +} + +impl ProblemPoser for Element { + fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab) { if let Some(index) = self.column_index { problem.gram.push_sym(index, index, 1.0); problem.guess.set_column(index, &self.representation.get_clone_untracked()); @@ -134,14 +140,10 @@ impl Element { } } -pub trait Regulator: OutlineItem { - // get information +pub trait Regulator: ProblemPoser + OutlineItem { fn subjects(&self) -> Vec; fn measurement(&self) -> ReadSignal; fn set_point(&self) -> Signal; - - // write problem data - fn write_to_problem(&self, problem: &mut ConstraintProblem, elts: &Slab); } pub struct ProductRegulator { @@ -162,8 +164,10 @@ impl Regulator for ProductRegulator { fn set_point(&self) -> Signal { self.set_point } - - fn write_to_problem(&self, problem: &mut ConstraintProblem, elts: &Slab) { +} + +impl ProblemPoser for ProductRegulator { + fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab) { self.set_point.with_untracked(|set_pt| { if let Some(val) = set_pt.value { let subject_column_indices = self.subjects.map( @@ -197,8 +201,10 @@ impl Regulator for HalfCurvatureRegulator { fn set_point(&self) -> Signal { self.set_point } - - fn write_to_problem(&self, problem: &mut ConstraintProblem, elts: &Slab) { +} + +impl ProblemPoser for HalfCurvatureRegulator { + fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab) { self.set_point.with_untracked(|set_pt| { if let Some(val) = set_pt.value { if let Some(col) = elts[self.subject].column_index { @@ -441,11 +447,11 @@ impl Assembly { let problem = self.elements.with_untracked(|elts| { let mut problem_to_be = ConstraintProblem::new(elts.len()); for (_, elt) in elts { - elt.write_to_problem(&mut problem_to_be); + elt.pose(&mut problem_to_be, elts); } self.regulators.with_untracked(|regs| { for (_, reg) in regs { - reg.write_to_problem(&mut problem_to_be, elts); + reg.pose(&mut problem_to_be, elts); } }); problem_to_be @@ -621,7 +627,7 @@ mod tests { "Sphere".to_string(), [1.0_f32, 1.0_f32, 1.0_f32], engine::sphere(0.0, 0.0, 0.0, 1.0) - ).write_to_problem(&mut ConstraintProblem::new(1)); + ).pose(&mut ConstraintProblem::new(1), &Slab::new()); }); } @@ -645,7 +651,7 @@ mod tests { subjects: subjects, measurement: create_memo(|| 0.0), set_point: create_signal(SpecifiedValue::try_from("0.0".to_string()).unwrap()) - }.write_to_problem(&mut ConstraintProblem::new(2), &elts); + }.pose(&mut ConstraintProblem::new(2), &elts); }); } } \ No newline at end of file