use crate::{
objects::{
Curve, Cycle, Face, HalfEdge, Region, Shell, Sketch, Solid, Surface,
Vertex,
},
operations::build::{Polygon, TetrahedronShell},
storage::Handle,
Core,
};
use super::{IsInsertedNo, IsInsertedYes};
pub trait Insert: Sized {
type Inserted;
#[must_use]
fn insert(self, core: &mut Core) -> Self::Inserted;
}
macro_rules! impl_insert {
($($ty:ty, $store:ident;)*) => {
$(
impl Insert for $ty {
type Inserted = Handle<Self>;
fn insert(self, core: &mut Core) -> Self::Inserted {
let handle = core.layers.objects.$store.reserve();
let object = (handle.clone(), self).into();
core.layers.objects.insert(
object,
&core.layers.geometry,
&mut core.layers.validation,
);
handle
}
}
)*
};
}
impl_insert!(
Curve, curves;
Cycle, cycles;
Face, faces;
HalfEdge, half_edges;
Region, regions;
Shell, shells;
Sketch, sketches;
Solid, solids;
Surface, surfaces;
Vertex, vertices;
);
impl<T> Insert for Handle<T>
where
T: Insert,
{
type Inserted = Self;
fn insert(self, _: &mut Core) -> Self::Inserted {
self
}
}
impl<const D: usize> Insert for Polygon<D, IsInsertedNo> {
type Inserted = Polygon<D, IsInsertedYes>;
fn insert(self, core: &mut Core) -> Self::Inserted {
Polygon {
face: self.face.insert(core),
half_edges: self.half_edges,
vertices: self.vertices,
}
}
}
impl Insert for TetrahedronShell<IsInsertedNo> {
type Inserted = TetrahedronShell<IsInsertedYes>;
fn insert(self, core: &mut Core) -> Self::Inserted {
TetrahedronShell {
shell: self.shell.insert(core),
abc: self.abc,
bad: self.bad,
dac: self.dac,
cbd: self.cbd,
}
}
}