Skip to content

Commit

Permalink
Consolidate Stores in single Stores struct
Browse files Browse the repository at this point in the history
  • Loading branch information
hannobraun committed Jan 13, 2025
1 parent d3b6c37 commit 8ed94a5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
19 changes: 8 additions & 11 deletions experiments/2024-12-09/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ use itertools::Itertools;
use crate::{
geometry::{Shape, Sketch, Triangle},
math::{Bivector, Plane, Point, Vector},
storage::Store,
topology::{Face, Vertex},
storage::Stores,
topology::Face,
};

pub fn model(shape: &mut Shape) {
let mut faces = Store::<Face>::new();
let mut surfaces = Store::<Plane>::new();
let mut vertices = Store::<Vertex>::new();
let mut triangles = Store::<Triangle>::new();
let mut stores = Stores::new();

let bottom = surfaces.insert(Plane {
let bottom = stores.get().insert(Plane {
origin: Point::from([0., 0., -0.5]),
coords: Bivector {
a: Vector::from([1., 0., 0.]),
b: Vector::from([0., -1., 0.]),
},
});
let top = surfaces.insert(Plane {
let top = stores.get().insert(Plane {
origin: Point::from([0., 0., 0.5]),
coords: Bivector {
a: Vector::from([1., 0., 0.]),
Expand All @@ -32,10 +29,10 @@ pub fn model(shape: &mut Shape) {
Sketch::from([[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5]]);

let [bottom, top] =
[bottom, top].map(|plane| Face::new(&sketch, plane, &mut vertices));
[bottom, top].map(|plane| Face::new(&sketch, plane, stores.get()));

let (bottom, top) = shape
.extend_with(&mut faces)
.extend_with(stores.get::<Face>())
.add(bottom)
.add(top)
.get_added();
Expand All @@ -47,7 +44,7 @@ pub fn model(shape: &mut Shape) {
[a, b, c, d, e, f, g, h].map(|vertex| vertex.point);

shape
.extend_with(&mut triangles)
.extend_with(stores.get::<Triangle>())
.add([d, e, h]) // left
.add([d, h, a])
.add([c, b, g]) // right
Expand Down
17 changes: 17 additions & 0 deletions experiments/2024-12-09/src/storage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
use std::marker::PhantomData;

use anymap3::AnyMap;

use crate::geometry::Handle;

#[derive(Default)]
pub struct Stores {
inner: AnyMap,
}

impl Stores {
pub fn new() -> Self {
Self::default()
}

pub fn get<T: 'static>(&mut self) -> &mut Store<T> {
self.inner.entry::<Store<T>>().or_default()
}
}

pub struct Store<T> {
_t: PhantomData<T>,
}
Expand Down

0 comments on commit 8ed94a5

Please sign in to comment.