Rename the Element structure to Sphere

This makes way for an `Element` trait. Some `Sphere` variables, like the
arguments of the sphere insertion methods, have been renamed to show
that they refer specifically to spheres. Others, like the argument of
`ElementOutlineItem`, have kept their general names, because I expect
them to become `Element` trait objects.
This commit is contained in:
Aaron Fenyes 2025-04-23 01:01:59 -07:00
parent 68abc2ad44
commit a1e23543cb
4 changed files with 41 additions and 41 deletions

View file

@ -33,11 +33,11 @@ pub type ElementColor = [f32; 3];
static NEXT_ELEMENT_SERIAL: AtomicU64 = AtomicU64::new(0);
pub trait ProblemPoser {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Element>);
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Sphere>);
}
#[derive(Clone, PartialEq)]
pub struct Element {
pub struct Sphere {
pub id: String,
pub label: String,
pub color: ElementColor,
@ -57,7 +57,7 @@ pub struct Element {
column_index: Option<usize>
}
impl Element {
impl Sphere {
const CURVATURE_COMPONENT: usize = 3;
pub fn new(
@ -65,7 +65,7 @@ impl Element {
label: String,
color: ElementColor,
representation: DVector<f64>
) -> Element {
) -> Sphere {
// take the next serial number, panicking if that was the last number we
// had left. the technique we use to panic on overflow is taken from
// _Rust Atomics and Locks_, by Mara Bos
@ -77,7 +77,7 @@ impl Element {
|serial| serial.checked_add(1)
).expect("Out of serial numbers for elements");
Element {
Sphere {
id: id,
label: label,
color: color,
@ -132,10 +132,10 @@ impl Element {
}
}
impl ProblemPoser for Element {
fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab<Element>) {
impl ProblemPoser for Sphere {
fn pose(&self, problem: &mut ConstraintProblem, _elts: &Slab<Sphere>) {
let index = self.column_index.expect(
format!("Element \"{}\" should be indexed before writing problem data", self.id).as_str()
format!("Sphere \"{}\" should be indexed before writing problem data", self.id).as_str()
);
problem.gram.push_sym(index, index, 1.0);
problem.guess.set_column(index, &self.representation.get_clone_untracked());
@ -198,7 +198,7 @@ impl Regulator for InversiveDistanceRegulator {
}
impl ProblemPoser for InversiveDistanceRegulator {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Element>) {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Sphere>) {
self.set_point.with_untracked(|set_pt| {
if let Some(val) = set_pt.value {
let [row, col] = self.subjects.map(
@ -222,7 +222,7 @@ impl HalfCurvatureRegulator {
pub fn new(subject: ElementKey, assembly: &Assembly) -> HalfCurvatureRegulator {
let measurement = assembly.elements.map(
move |elts| elts[subject].representation.with(
|rep| rep[Element::CURVATURE_COMPONENT]
|rep| rep[Sphere::CURVATURE_COMPONENT]
)
);
@ -262,13 +262,13 @@ impl Regulator for HalfCurvatureRegulator {
}
impl ProblemPoser for HalfCurvatureRegulator {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Element>) {
fn pose(&self, problem: &mut ConstraintProblem, elts: &Slab<Sphere>) {
self.set_point.with_untracked(|set_pt| {
if let Some(val) = set_pt.value {
let col = elts[self.subject].column_index.expect(
"Subject should be indexed before half-curvature regulator writes problem data"
);
problem.frozen.push(Element::CURVATURE_COMPONENT, col, val);
problem.frozen.push(Sphere::CURVATURE_COMPONENT, col, val);
}
});
}
@ -286,7 +286,7 @@ type AssemblyMotion<'a> = Vec<ElementMotion<'a>>;
#[derive(Clone)]
pub struct Assembly {
// elements and regulators
pub elements: Signal<Slab<Element>>,
pub elements: Signal<Slab<Sphere>>,
pub regulators: Signal<Slab<Rc<dyn Regulator>>>,
// solution variety tangent space. the basis vectors are stored in
@ -320,10 +320,10 @@ impl Assembly {
// 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_sphere_unchecked(&self, elt: Element) -> ElementKey {
fn insert_sphere_unchecked(&self, sphere: Sphere) -> ElementKey {
// insert the sphere
let id = elt.id.clone();
let key = self.elements.update(|elts| elts.insert(elt));
let id = sphere.id.clone();
let key = self.elements.update(|elts| elts.insert(sphere));
self.elements_by_id.update(|elts_by_id| elts_by_id.insert(id, key));
// regulate the sphere's curvature
@ -332,12 +332,12 @@ impl Assembly {
key
}
pub fn try_insert_sphere(&self, elt: Element) -> Option<ElementKey> {
pub fn try_insert_sphere(&self, sphere: Sphere) -> Option<ElementKey> {
let can_insert = self.elements_by_id.with_untracked(
|elts_by_id| !elts_by_id.contains_key(&elt.id)
|elts_by_id| !elts_by_id.contains_key(&sphere.id)
);
if can_insert {
Some(self.insert_sphere_unchecked(elt))
Some(self.insert_sphere_unchecked(sphere))
} else {
None
}
@ -356,7 +356,7 @@ impl Assembly {
// create and insert a sphere
let _ = self.insert_sphere_unchecked(
Element::new(
Sphere::new(
id,
format!("Sphere {}", id_num),
[0.75_f32, 0.75_f32, 0.75_f32],
@ -607,10 +607,10 @@ mod tests {
use super::*;
#[test]
#[should_panic(expected = "Element \"sphere\" should be indexed before writing problem data")]
#[should_panic(expected = "Sphere \"sphere\" should be indexed before writing problem data")]
fn unindexed_element_test() {
let _ = create_root(|| {
Element::new(
Sphere::new(
"sphere".to_string(),
"Sphere".to_string(),
[1.0_f32, 1.0_f32, 1.0_f32],
@ -626,7 +626,7 @@ mod tests {
let mut elts = Slab::new();
let subjects = [0, 1].map(|k| {
elts.insert(
Element::new(
Sphere::new(
format!("sphere{k}"),
format!("Sphere {k}"),
[1.0_f32, 1.0_f32, 1.0_f32],