forked from StudioInfinity/dyna3
Drop unused context
Using pointers to refer to elements reduces the need to drag context around.
This commit is contained in:
parent
5191534886
commit
fbd6177a07
3 changed files with 25 additions and 34 deletions
|
@ -241,7 +241,7 @@ pub fn AddRemove() -> View {
|
|||
.unwrap()
|
||||
);
|
||||
state.assembly.insert_regulator(
|
||||
Rc::new(InversiveDistanceRegulator::new(subjects, &state.assembly))
|
||||
Rc::new(InversiveDistanceRegulator::new(subjects))
|
||||
);
|
||||
state.selection.update(|sel| sel.clear());
|
||||
}
|
||||
|
|
|
@ -44,24 +44,18 @@ pub type ElementColor = [f32; 3];
|
|||
static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
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 {
|
||||
// the default identifier for an element of this type
|
||||
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;
|
||||
|
||||
// the regulators that should be created when an element of this type is
|
||||
// inserted into the given assembly with the given storage key
|
||||
/* 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 {
|
||||
// the default regulators that come with this element
|
||||
fn default_regulators(self: Rc<Self>) -> Vec<Rc<dyn Regulator>> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
|
@ -165,8 +159,8 @@ impl Element for Sphere {
|
|||
)
|
||||
}
|
||||
|
||||
fn default_regulators(self: Rc<Self>, assembly: &Assembly) -> Vec<Rc<dyn Regulator>> {
|
||||
vec![Rc::new(HalfCurvatureRegulator::new(self, assembly))]
|
||||
fn default_regulators(self: Rc<Self>) -> Vec<Rc<dyn Regulator>> {
|
||||
vec![Rc::new(HalfCurvatureRegulator::new(self))]
|
||||
}
|
||||
|
||||
fn id(&self) -> &String {
|
||||
|
@ -199,7 +193,7 @@ impl Element 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(
|
||||
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 {
|
||||
fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab<Rc<dyn Element>>) {
|
||||
fn pose(&self, problem: &mut ConstraintProblem) {
|
||||
let index = self.column_index().expect(
|
||||
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
|
||||
// to report whether the set is present. the default implementation does no
|
||||
// preconditioning
|
||||
fn try_activate(&self, _assembly: &Assembly) -> bool {
|
||||
fn try_activate(&self) -> bool {
|
||||
self.set_point().with(|set_pt| set_pt.is_present())
|
||||
}
|
||||
}
|
||||
|
@ -316,7 +310,7 @@ pub struct 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 measurement = create_memo(move || {
|
||||
representations[0].with(|rep_0|
|
||||
|
@ -347,7 +341,7 @@ impl Regulator 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| {
|
||||
if let Some(val) = set_pt.value {
|
||||
let [row, col] = self.subjects.each_ref().map(
|
||||
|
@ -368,7 +362,7 @@ pub struct 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(
|
||||
|rep| rep[Sphere::CURVATURE_COMPONENT]
|
||||
);
|
||||
|
@ -392,7 +386,7 @@ impl Regulator for HalfCurvatureRegulator {
|
|||
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) {
|
||||
Some(half_curv) => {
|
||||
self.subject.representation().update(
|
||||
|
@ -406,7 +400,7 @@ impl Regulator 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| {
|
||||
if let Some(val) = set_pt.value {
|
||||
let col = self.subject.column_index().expect(
|
||||
|
@ -468,11 +462,11 @@ impl Assembly {
|
|||
// insert the element
|
||||
let id = elt.id().clone();
|
||||
let elt_rc = Rc::new(elt);
|
||||
let key = self.elements.update(|elts| elts.insert(elt_rc.clone())); /* KLUDGE */ // reorganize to avoid cloning?
|
||||
self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, 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()));
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
|
@ -513,8 +507,7 @@ impl Assembly {
|
|||
);
|
||||
|
||||
// add the regulator to each subject's regulator list
|
||||
let subjects = regulator.subjects();
|
||||
let subject_regulators: Vec<_> = subjects.into_iter().map(
|
||||
let subject_regulators: Vec<_> = regulator.subjects().into_iter().map(
|
||||
|subj| subj.regulators()
|
||||
).collect();
|
||||
for regulators in subject_regulators {
|
||||
|
@ -531,7 +524,7 @@ impl Assembly {
|
|||
format!("Updated regulator with subjects {:?}", regulator.subjects())
|
||||
));
|
||||
|
||||
if regulator.try_activate(&self_for_effect) {
|
||||
if regulator.try_activate() {
|
||||
self_for_effect.realize();
|
||||
}
|
||||
});
|
||||
|
@ -573,11 +566,11 @@ impl Assembly {
|
|||
let problem = self.elements.with_untracked(|elts| {
|
||||
let mut problem = ConstraintProblem::new(elts.len());
|
||||
for (_, elt) in elts {
|
||||
elt.pose(&mut problem, elts);
|
||||
elt.pose(&mut problem);
|
||||
}
|
||||
self.regulators.with_untracked(|regs| {
|
||||
for (_, reg) in regs {
|
||||
reg.pose(&mut problem, elts);
|
||||
reg.pose(&mut problem);
|
||||
}
|
||||
});
|
||||
problem
|
||||
|
|
|
@ -11,7 +11,6 @@ use crate::{
|
|||
AppState,
|
||||
assembly::{
|
||||
Element,
|
||||
ElementKey,
|
||||
HalfCurvatureRegulator,
|
||||
InversiveDistanceRegulator,
|
||||
Regulator,
|
||||
|
@ -96,7 +95,6 @@ pub trait OutlineItem {
|
|||
|
||||
impl OutlineItem for InversiveDistanceRegulator {
|
||||
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 {
|
||||
self.subjects[1].label()
|
||||
} 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
|
||||
#[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 class = {
|
||||
let element_for_class = element.clone();
|
||||
|
@ -271,8 +269,8 @@ pub fn Outline() -> View {
|
|||
) {
|
||||
Keyed(
|
||||
list=element_list,
|
||||
view=|(key, elt)| view! {
|
||||
ElementOutlineItem(key=key, element=elt)
|
||||
view=|(_, elt)| view! {
|
||||
ElementOutlineItem(element=elt)
|
||||
},
|
||||
key=|(_, elt)| elt.serial()
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue