feat(geometry): add interface to apply edits

This commit is contained in:
Mikolaj Wielgus 2024-11-28 05:15:39 +01:00
parent f17c8ce756
commit a838310ecb
3 changed files with 148 additions and 20 deletions

View File

@ -124,6 +124,15 @@ impl<
GenericIndex::<W>::new(self.graph.add_node(GenericNode::Primitive(weight.into())))
}
pub(super) fn add_dot_at_index<W: AccessDotWeight<PW>>(
&mut self,
dot: GenericIndex<W>,
weight: W,
) {
self.graph
.update_node(dot.petgraph_index(), GenericNode::Primitive(weight.into()));
}
pub fn add_seg<W: AccessSegWeight<PW>>(
&mut self,
from: DI,
@ -136,6 +145,18 @@ impl<
seg
}
pub(super) fn add_seg_at_index<W: AccessSegWeight<PW>>(
&mut self,
seg: GenericIndex<W>,
from: DI,
to: DI,
weight: W,
) {
self.graph
.update_node(seg.petgraph_index(), GenericNode::Primitive(weight.into()));
self.init_seg_joints(seg, from, to);
}
fn init_seg_joints<W: AccessSegWeight<PW>>(&mut self, seg: GenericIndex<W>, from: DI, to: DI) {
self.graph.update_edge(
from.petgraph_index(),
@ -162,6 +183,26 @@ impl<
bend
}
pub(super) fn add_bend_at_index<W: AccessBendWeight<PW>>(
&mut self,
bend: GenericIndex<W>,
from: DI,
to: DI,
core: DI,
weight: W,
) {
self.graph
.update_node(bend.petgraph_index(), GenericNode::Primitive(weight.into()));
self.init_bend_joints_and_core(bend, from, to, core);
}
pub(super) fn add_compound_at_index(&mut self, compound: GenericIndex<CW>, weight: CW) {
self.graph.update_node(
compound.petgraph_index(),
GenericNode::Compound(weight.into()),
);
}
fn init_bend_joints_and_core<W: AccessBendWeight<PW>>(
&mut self,
bend: GenericIndex<W>,

View File

@ -333,6 +333,58 @@ impl<
}
}
pub fn apply_edit(&mut self, edit: GeometryEdit<PW, DW, SW, BW, CW, PI, DI, SI, BI>) {
for (compound, ..) in &edit.compounds {
self.geometry_with_rtree.remove_compound(*compound);
}
for (bend, ..) in &edit.bends {
self.geometry_with_rtree.remove_bend(*bend);
}
for (seg, ..) in &edit.segs {
self.geometry_with_rtree.remove_seg(*seg);
}
for (dot, ..) in &edit.dots {
self.geometry_with_rtree.remove_dot(*dot);
}
for (dot, (.., maybe_weight)) in &edit.dots {
if let Some(weight) = maybe_weight {
self.geometry_with_rtree.add_dot_at_index(*dot, *weight);
}
}
for (seg, (.., maybe_data)) in &edit.segs {
if let Some(((from, to), weight)) = maybe_data {
self.geometry_with_rtree
.add_seg_at_index(*seg, *from, *to, *weight);
}
}
for (bend, (.., maybe_data)) in &edit.bends {
if let Some(((from, to, core), weight)) = maybe_data {
self.geometry_with_rtree
.add_bend_at_index(*bend, *from, *to, *core, *weight);
}
}
for (compound, (.., maybe_data)) in &edit.compounds {
if let Some((members, weight)) = maybe_data {
self.geometry_with_rtree
.add_compound_at_index(*compound, *weight);
for member in members {
self.geometry_with_rtree.add_to_compound(
GenericIndex::<PW>::new(member.petgraph_index()),
*compound,
);
}
}
}
}
pub fn compound_weight(&self, compound: GenericIndex<CW>) -> CW {
self.geometry_with_rtree.compound_weight(compound)
}

View File

@ -79,16 +79,22 @@ impl<
GenericIndex<W>: Into<PI>,
{
let dot = self.geometry.add_dot(weight);
self.init_dot_bbox(dot, weight);
self.init_dot_bbox(dot.into().try_into().unwrap_or_else(|_| unreachable!()));
dot
}
fn init_dot_bbox<W: AccessDotWeight<PW> + GetLayer>(&mut self, dot: GenericIndex<W>, weight: W)
where
GenericIndex<W>: Into<PI>,
{
self.rtree
.insert(self.make_dot_bbox(dot.into().try_into().unwrap_or_else(|_| unreachable!())));
pub(super) fn add_dot_at_index<W: AccessDotWeight<PW> + GetLayer>(
&mut self,
dot: DI,
weight: W,
) {
self.geometry
.add_dot_at_index(GenericIndex::<W>::new(dot.petgraph_index()), weight);
self.init_dot_bbox(dot);
}
fn init_dot_bbox(&mut self, dot: DI) {
self.rtree.insert(self.make_dot_bbox(dot));
}
pub fn add_seg<W: AccessSegWeight<PW> + GetLayer>(
@ -101,16 +107,28 @@ impl<
GenericIndex<W>: Into<PI>,
{
let seg = self.geometry.add_seg(from, to, weight);
self.init_seg_bbox(seg, weight);
self.init_seg_bbox(seg.into().try_into().unwrap_or_else(|_| unreachable!()));
seg
}
fn init_seg_bbox<W: AccessSegWeight<PW> + GetLayer>(&mut self, seg: GenericIndex<W>, weight: W)
where
GenericIndex<W>: Into<PI>,
{
self.rtree
.insert(self.make_seg_bbox(seg.into().try_into().unwrap_or_else(|_| unreachable!())));
pub(super) fn add_seg_at_index<W: AccessSegWeight<PW> + GetLayer>(
&mut self,
seg: SI,
from: DI,
to: DI,
weight: W,
) {
self.geometry.add_seg_at_index(
GenericIndex::<W>::new(seg.petgraph_index()),
from,
to,
weight,
);
self.init_seg_bbox(seg);
}
fn init_seg_bbox(&mut self, seg: SI) {
self.rtree.insert(self.make_seg_bbox(seg));
}
pub fn add_bend<W: AccessBendWeight<PW> + GetLayer>(
@ -124,17 +142,34 @@ impl<
GenericIndex<W>: Into<PI>,
{
let bend = self.geometry.add_bend(from, to, core, weight);
self.init_bend_bbox(bend, weight);
self.init_bend_bbox(bend.into().try_into().unwrap_or_else(|_| unreachable!()));
bend
}
fn init_bend_bbox<W: AccessBendWeight<PW> + GetLayer>(
pub(super) fn add_bend_at_index<W: AccessBendWeight<PW> + GetLayer>(
&mut self,
bend: GenericIndex<W>,
bend: BI,
from: DI,
to: DI,
core: DI,
weight: W,
) where
GenericIndex<W>: Into<PI>,
{
) {
self.geometry.add_bend_at_index(
GenericIndex::<W>::new(bend.petgraph_index()),
from,
to,
core,
weight,
);
self.init_bend_bbox(bend);
}
pub(super) fn add_compound_at_index(&mut self, compound: GenericIndex<CW>, weight: CW) {
self.geometry
.add_compound_at_index(GenericIndex::<CW>::new(compound.petgraph_index()), weight);
}
fn init_bend_bbox(&mut self, bend: BI) {
self.rtree
.insert(self.make_bend_bbox(bend.into().try_into().unwrap_or_else(|_| unreachable!())));
}