Assembly: factor out element insertion

This commit is contained in:
Aaron Fenyes 2024-09-28 13:10:22 -07:00
parent bd0982f821
commit b08dbd6f93

View File

@ -39,6 +39,25 @@ impl Assembly {
} }
} }
// insert an element 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) {
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));
}
pub fn try_insert_element(&self, elt: Element) -> bool {
let can_insert = self.elements_by_id.with(
|elts_by_id| !elts_by_id.contains_key(&elt.id)
);
if can_insert {
self.insert_element_unchecked(elt);
}
can_insert
}
pub fn insert_new_element(&self) { pub fn insert_new_element(&self) {
// find the next unused identifier in the default sequence // find the next unused identifier in the default sequence
let mut id_num = 1; let mut id_num = 1;
@ -51,15 +70,15 @@ impl Assembly {
} }
// create and insert a new element // create and insert a new element
let elt = Element { self.insert_element_unchecked(
id: id.clone(), Element {
label: format!("Sphere {}", id_num), id: id,
color: [0.75_f32, 0.75_f32, 0.75_f32], label: format!("Sphere {}", id_num),
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.5, -0.5]), color: [0.75_f32, 0.75_f32, 0.75_f32],
constraints: BTreeSet::default() rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.5, -0.5]),
}; constraints: BTreeSet::default()
let key = self.elements.update(|elts| elts.insert(elt)); }
self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, key)); );
} }
pub fn insert_constraint(&self, constraint: Constraint) { pub fn insert_constraint(&self, constraint: Constraint) {