Replace AnyMap in Stores

Using this approach, accessing a store required borrowing all of
`Stores`. This caused problems, as multiple stores couldn't get bored
separately. Or when using iterators and closures.
This commit is contained in:
Hanno Braun 2025-01-23 21:49:52 +01:00
parent 1fa9e15acd
commit 3e1afe5f88
3 changed files with 17 additions and 15 deletions

View File

@ -14,7 +14,7 @@ pub fn model() -> AnyOp {
let sketch = let sketch =
Sketch::from([[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]]); Sketch::from([[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]]);
let surface = stores.get().insert(Plane { let surface = stores.surfaces.insert(Plane {
origin: Point::from([0., 0., 0.5]), origin: Point::from([0., 0., 0.5]),
coords: Bivector { coords: Bivector {
a: Vector::from([1., 0., 0.]), a: Vector::from([1., 0., 0.]),
@ -22,9 +22,11 @@ pub fn model() -> AnyOp {
}, },
}); });
sketch.to_face(surface, stores.get()) sketch.to_face(surface, &mut stores.vertices)
}; };
let bottom = top.flip(stores.get()).translate([0., 0., -1.], &mut stores); let bottom = top
.flip(&mut stores.surfaces)
.translate([0., 0., -1.], &mut stores);
let [a, b, c, d] = bottom.vertices().collect_array().unwrap(); let [a, b, c, d] = bottom.vertices().collect_array().unwrap();
let [e, f, g, h] = top.vertices().collect_array().unwrap(); let [e, f, g, h] = top.vertices().collect_array().unwrap();
@ -32,7 +34,7 @@ pub fn model() -> AnyOp {
let [left, right, front, back] = let [left, right, front, back] =
[[a, e, h, d], [b, c, g, f], [a, b, f, e], [c, d, h, g]].map( [[a, e, h, d], [b, c, g, f], [a, b, f, e], [c, d, h, g]].map(
|[q, r, s, t]| { |[q, r, s, t]| {
let surface = stores.get().insert(Plane::from_points( let surface = stores.surfaces.insert(Plane::from_points(
[q, r, s].map(|vertex| vertex.point), [q, r, s].map(|vertex| vertex.point),
)); ));
Face::new(surface, [q, r, s, t].map(|vertex| vertex.clone())) Face::new(surface, [q, r, s, t].map(|vertex| vertex.clone()))
@ -41,7 +43,7 @@ pub fn model() -> AnyOp {
let solid = Solid::new( let solid = Solid::new(
[bottom, top, left, right, front, back] [bottom, top, left, right, front, back]
.map(|face| stores.get().insert(face)), .map(|face| stores.faces.insert(face)),
); );
AnyOp::new(solid) AnyOp::new(solid)

View File

@ -1,22 +1,22 @@
use std::marker::PhantomData; use std::marker::PhantomData;
use anymap3::AnyMap; use crate::{
geometry::Handle,
use crate::geometry::Handle; math::Plane,
topology::{Face, Vertex},
};
#[derive(Default)] #[derive(Default)]
pub struct Stores { pub struct Stores {
inner: AnyMap, pub faces: Store<Face>,
pub surfaces: Store<Plane>,
pub vertices: Store<Vertex>,
} }
impl Stores { impl Stores {
pub fn new() -> Self { pub fn new() -> Self {
Self::default() Self::default()
} }
pub fn get<T: 'static>(&mut self) -> &mut Store<T> {
self.inner.entry::<Store<T>>().or_default()
}
} }
pub struct Store<T> { pub struct Store<T> {

View File

@ -44,11 +44,11 @@ impl Face {
let offset = offset.into(); let offset = offset.into();
Self { Self {
surface: stores.get().insert(self.surface.translate(offset)), surface: stores.surfaces.insert(self.surface.translate(offset)),
vertices: self vertices: self
.vertices .vertices
.iter() .iter()
.map(|vertex| stores.get().insert(vertex.translate(offset))) .map(|vertex| stores.vertices.insert(vertex.translate(offset)))
.collect(), .collect(),
} }
} }