graph,layout: Add creation methods for new primitives and a new label

This commit is contained in:
Mikolaj Wielgus 2023-10-22 05:07:53 +00:00
parent a2531b078d
commit 0dac23f018
6 changed files with 153 additions and 59 deletions

View File

@ -85,7 +85,7 @@ impl<'a> Draw<'a> {
let net = self.layout.primitive(head.dot()).weight().net;
self.layout
.add_seg(head.dot(), into, FixedSegWeight { net, width })?;
.add_fixed_seg(head.dot(), into, FixedSegWeight { net, width })?;
Ok(())
}
@ -109,7 +109,7 @@ impl<'a> Draw<'a> {
let net = self.layout.primitive(head.dot()).weight().net;
self.layout
.add_seg(head.dot(), into, FixedSegWeight { net, width })?;
.add_fixed_seg(head.dot(), into, FixedSegWeight { net, width })?;
Ok(())
}
@ -247,7 +247,7 @@ impl<'a> Draw<'a> {
let dot = head.dot;
let bend_to = self
.layout
.add_dot(self.layout.primitive(head.dot).weight())
.add_fixed_dot(self.layout.primitive(head.dot).weight())
.map_err(|err| {
self.undo_seg(head, seg);
err
@ -257,7 +257,7 @@ impl<'a> Draw<'a> {
let bend = self
.layout
.add_bend(head.dot, bend_to, around, FixedBendWeight { net, cw })
.add_fixed_bend(head.dot, bend_to, around, FixedBendWeight { net, cw })
.map_err(|err| {
self.layout.remove(bend_to.into());
self.undo_seg(head, seg);
@ -288,7 +288,7 @@ impl<'a> Draw<'a> {
#[debug_ensures(ret.is_err() -> self.layout.node_count() == old(self.layout.node_count()))]
fn seg(&mut self, head: Head, to: Point, width: f64) -> Result<(BareHead, FixedSegIndex), ()> {
let net = self.layout.primitive(head.dot()).weight().net;
let to_index = self.layout.add_dot(FixedDotWeight {
let to_index = self.layout.add_fixed_dot(FixedDotWeight {
net,
circle: Circle {
pos: to,
@ -297,7 +297,7 @@ impl<'a> Draw<'a> {
})?;
let seg = self
.layout
.add_seg(head.dot(), to_index, FixedSegWeight { net, width })
.add_fixed_seg(head.dot(), to_index, FixedSegWeight { net, width })
.map_err(|err| {
self.layout.remove(to_index.into());
err

View File

@ -167,7 +167,8 @@ impl_type!(LooseBendWeight, LooseBend, LooseBendIndex);
#[derive(Debug, EnumAsInner, Clone, Copy, PartialEq)]
pub enum Label {
End,
FixedEnd,
LooseEnd,
Outer,
Core,
}

View File

@ -10,8 +10,8 @@ use crate::band::Band;
use crate::bow::Bow;
use crate::graph::{
BendIndex, DotIndex, FixedBendIndex, FixedBendWeight, FixedDotIndex, FixedDotWeight,
FixedSegIndex, FixedSegWeight, GenericIndex, GetNodeIndex, Index, Interior, Label,
MakePrimitive, Retag, SegIndex, Weight,
FixedSegIndex, FixedSegWeight, GenericIndex, GetNodeIndex, HalfLooseSegWeight, Index, Interior,
Label, LooseDotIndex, LooseDotWeight, MakePrimitive, Retag, SegIndex, Weight,
};
use crate::primitive::{
GenericPrimitive, GetConnectable, GetWeight, MakeShape, TaggedPrevTaggedNext,
@ -68,8 +68,18 @@ impl Layout {
}
#[debug_ensures(self.graph.node_count() == old(self.graph.node_count() + 1))]
pub fn add_dot(&mut self, weight: FixedDotWeight) -> Result<FixedDotIndex, ()> {
let dot = FixedDotIndex::new(self.graph.add_node(Weight::FixedDot(weight)));
pub fn add_fixed_dot(&mut self, weight: FixedDotWeight) -> Result<FixedDotIndex, ()> {
let dot = FixedDotIndex::new(self.graph.add_node(weight.into()));
self.insert_into_rtree(dot.into());
self.fail_and_remove_if_collides_except(dot.into(), &[])?;
Ok(dot)
}
#[debug_ensures(self.graph.node_count() == old(self.graph.node_count() + 1))]
pub fn add_loose_dot(&mut self, weight: LooseDotWeight) -> Result<LooseDotIndex, ()> {
let dot = LooseDotIndex::new(self.graph.add_node(weight.into()));
self.insert_into_rtree(dot.into());
self.fail_and_remove_if_collides_except(dot.into(), &[])?;
@ -79,18 +89,86 @@ impl Layout {
#[debug_ensures(self.graph.node_count() == old(self.graph.node_count() + 1))]
#[debug_ensures(self.graph.edge_count() == old(self.graph.edge_count() + 2))]
pub fn add_seg(
pub fn add_fixed_seg(
&mut self,
from: FixedDotIndex,
to: FixedDotIndex,
weight: FixedSegWeight,
) -> Result<FixedSegIndex, ()> {
let seg = FixedSegIndex::new(self.graph.add_node(Weight::FixedSeg(weight)));
let seg = FixedSegIndex::new(self.graph.add_node(weight.into()));
self.graph
.add_edge(from.node_index(), seg.node_index(), Label::End);
.add_edge(from.node_index(), seg.node_index(), Label::FixedEnd);
self.graph
.add_edge(seg.node_index(), to.node_index(), Label::End);
.add_edge(seg.node_index(), to.node_index(), Label::FixedEnd);
self.insert_into_rtree(seg.into());
self.fail_and_remove_if_collides_except(seg.into(), &[])?;
self.graph
.node_weight_mut(from.node_index())
.unwrap()
.as_fixed_dot_mut()
.unwrap()
.net = weight.net;
self.graph
.node_weight_mut(to.node_index())
.unwrap()
.as_fixed_dot_mut()
.unwrap()
.net = weight.net;
Ok(seg)
}
#[debug_ensures(self.graph.node_count() == old(self.graph.node_count() + 1))]
#[debug_ensures(self.graph.edge_count() == old(self.graph.edge_count() + 2))]
pub fn add_half_loose_seg(
&mut self,
from: FixedDotIndex,
to: LooseDotIndex,
weight: HalfLooseSegWeight,
) -> Result<FixedSegIndex, ()> {
let seg = FixedSegIndex::new(self.graph.add_node(weight.into()));
self.graph
.add_edge(from.node_index(), seg.node_index(), Label::FixedEnd);
self.graph
.add_edge(seg.node_index(), to.node_index(), Label::LooseEnd);
self.insert_into_rtree(seg.into());
self.fail_and_remove_if_collides_except(seg.into(), &[])?;
self.graph
.node_weight_mut(from.node_index())
.unwrap()
.as_fixed_dot_mut()
.unwrap()
.net = weight.net;
self.graph
.node_weight_mut(to.node_index())
.unwrap()
.as_fixed_dot_mut()
.unwrap()
.net = weight.net;
Ok(seg)
}
#[debug_ensures(self.graph.node_count() == old(self.graph.node_count() + 1))]
#[debug_ensures(self.graph.edge_count() == old(self.graph.edge_count() + 2))]
pub fn add_loose_seg(
&mut self,
from: LooseDotIndex,
to: LooseDotIndex,
weight: HalfLooseSegWeight,
) -> Result<FixedSegIndex, ()> {
let seg = FixedSegIndex::new(self.graph.add_node(weight.into()));
self.graph
.add_edge(from.node_index(), seg.node_index(), Label::LooseEnd);
self.graph
.add_edge(seg.node_index(), to.node_index(), Label::LooseEnd);
self.insert_into_rtree(seg.into());
self.fail_and_remove_if_collides_except(seg.into(), &[])?;
@ -113,7 +191,7 @@ impl Layout {
#[debug_ensures(ret.is_ok() -> self.graph.node_count() == old(self.graph.node_count() + 1))]
#[debug_ensures(ret.is_err() -> self.graph.node_count() == old(self.graph.node_count()))]
pub fn add_bend(
pub fn add_fixed_bend(
&mut self,
from: FixedDotIndex,
to: FixedDotIndex,
@ -123,11 +201,26 @@ impl Layout {
match around {
Index::FixedDot(core) => self.add_core_bend(from, to, core, weight),
Index::FixedBend(around) => self.add_outer_bend(from, to, around, weight),
Index::FixedSeg(..) => unreachable!(),
_ => unreachable!(),
}
}
/*#[debug_ensures(ret.is_ok() -> self.graph.node_count() == old(self.graph.node_count() + 1))]
#[debug_ensures(ret.is_err() -> self.graph.node_count() == old(self.graph.node_count()))]
pub fn add_loose_bend(
&mut self,
from: LooseDotIndex,
to: LooseDotIndex,
around: Index,
weight: FixedBendWeight,
) -> Result<FixedBendIndex, ()> {
match around {
Index::FixedDot(core) => self.add_core_bend(from, to, core, weight),
Index::FixedBend(around) => self.add_outer_bend(from, to, around, weight),
_ => unreachable!(),
}
}*/
#[debug_ensures(ret.is_ok() -> self.graph.node_count() == old(self.graph.node_count() + 1))]
#[debug_ensures(ret.is_err() -> self.graph.node_count() == old(self.graph.node_count()))]
#[debug_ensures(ret.is_ok() -> self.graph.edge_count() == old(self.graph.edge_count() + 3))]
@ -139,12 +232,12 @@ impl Layout {
core: FixedDotIndex,
weight: FixedBendWeight,
) -> Result<FixedBendIndex, ()> {
let bend = FixedBendIndex::new(self.graph.add_node(Weight::FixedBend(weight)));
let bend = FixedBendIndex::new(self.graph.add_node(weight.into()));
self.graph
.add_edge(from.node_index(), bend.node_index(), Label::End);
.add_edge(from.node_index(), bend.node_index(), Label::FixedEnd);
self.graph
.add_edge(bend.node_index(), to.node_index(), Label::End);
.add_edge(bend.node_index(), to.node_index(), Label::FixedEnd);
self.graph
.add_edge(bend.node_index(), core.node_index(), Label::Core);
@ -178,12 +271,12 @@ impl Layout {
.first()
.unwrap();
let bend = FixedBendIndex::new(self.graph.add_node(Weight::FixedBend(weight)));
let bend = FixedBendIndex::new(self.graph.add_node(weight.into()));
self.graph
.add_edge(from.node_index(), bend.node_index(), Label::End);
.add_edge(from.node_index(), bend.node_index(), Label::FixedEnd);
self.graph
.add_edge(bend.node_index(), to.node_index(), Label::End);
.add_edge(bend.node_index(), to.node_index(), Label::FixedEnd);
self.graph
.add_edge(bend.node_index(), core.node_index(), Label::Core);
self.graph

View File

@ -134,7 +134,7 @@ fn main() {
let dot1 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 1,
circle: Circle {
pos: (100.5, 400.5).into(),
@ -143,20 +143,20 @@ fn main() {
})
.unwrap();
let dot2 = router
/*let dot2 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 1,
circle: Circle {
pos: (100.5, 500.5).into(),
r: 8.0,
},
})
.unwrap();
.unwrap();*/
let dot_end = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 1,
circle: Circle {
pos: (470.5, 350.5).into(),
@ -167,7 +167,7 @@ fn main() {
let dot1_1 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (200.5, 200.5).into(),
@ -178,7 +178,7 @@ fn main() {
let dot2_1 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (200.5, 500.5).into(),
@ -187,7 +187,7 @@ fn main() {
})
.unwrap();
let _ = router.layout.add_seg(
let _ = router.layout.add_fixed_seg(
dot1_1,
dot2_1,
FixedSegWeight {
@ -198,7 +198,7 @@ fn main() {
let dot2_2 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (600.5, 500.5).into(),
@ -207,7 +207,7 @@ fn main() {
})
.unwrap();
let _ = router.layout.add_seg(
let _ = router.layout.add_fixed_seg(
dot2_1,
dot2_2,
FixedSegWeight {
@ -218,7 +218,7 @@ fn main() {
let dot3 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (400.5, 200.5).into(),
@ -229,7 +229,7 @@ fn main() {
let dot4 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (400.5, 400.5).into(),
@ -238,7 +238,7 @@ fn main() {
})
.unwrap();
let _ = router.layout.add_seg(
let _ = router.layout.add_fixed_seg(
dot3,
dot4,
FixedSegWeight {
@ -249,7 +249,7 @@ fn main() {
let dot5 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (530.5, 400.5).into(),
@ -258,7 +258,7 @@ fn main() {
})
.unwrap();
let _ = router.layout.add_seg(
let _ = router.layout.add_fixed_seg(
dot4,
dot5,
FixedSegWeight {
@ -269,7 +269,7 @@ fn main() {
let dot1_2 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (600.5, 200.5).into(),
@ -278,7 +278,7 @@ fn main() {
})
.unwrap();
let _ = router.layout.add_seg(
let _ = router.layout.add_fixed_seg(
dot3,
dot1_2,
FixedSegWeight {
@ -287,7 +287,7 @@ fn main() {
},
);
let _ = router.layout.add_seg(
let _ = router.layout.add_fixed_seg(
dot1_2,
dot2_2,
FixedSegWeight {
@ -298,7 +298,7 @@ fn main() {
let dot6 = router
.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: 2,
circle: Circle {
pos: (530.5, 300.5).into(),
@ -307,7 +307,7 @@ fn main() {
})
.unwrap();
let _ = router.layout.add_seg(
let _ = router.layout.add_fixed_seg(
dot5,
dot6,
FixedSegWeight {
@ -375,7 +375,7 @@ fn main() {
-1,
);
render_times(
/*render_times(
&mut event_pump,
&mut canvas,
RouterOrLayout::Router(&mut router),
@ -384,7 +384,7 @@ fn main() {
None,
&[],
-1,
);
);*/
render_times(
&mut event_pump,

View File

@ -85,7 +85,7 @@ impl<'a, W> GenericPrimitive<'a, W> {
self.graph
.edge_weight(self.graph.find_edge(*ni, prev_index).unwrap())
.unwrap()
.is_end()
.is_fixed_end()
})
.next()
{
@ -108,7 +108,7 @@ impl<'a, W> GenericPrimitive<'a, W> {
self.graph
.edge_weight(self.graph.find_edge(*ni, self.index.node_index()).unwrap())
.unwrap()
.is_end()
.is_fixed_end()
})
.next()
}
@ -126,7 +126,7 @@ impl<'a, W> GenericPrimitive<'a, W> {
self.graph
.edge_weight(self.graph.find_edge(prev_index, *ni).unwrap())
.unwrap()
.is_end()
.is_fixed_end()
})
.next()
{
@ -149,7 +149,7 @@ impl<'a, W> GenericPrimitive<'a, W> {
self.graph
.edge_weight(self.graph.find_edge(self.index.node_index(), *ni).unwrap())
.unwrap()
.is_end()
.is_fixed_end()
})
.next()
}
@ -203,7 +203,7 @@ impl<'a, W> Ends<FixedDotIndex, FixedDotIndex> for GenericPrimitive<'a, W> {
.0,
)
.unwrap()
.is_end()
.is_fixed_end()
})
.filter(|ni| self.graph.node_weight(*ni).unwrap().is_fixed_dot())
.map(|ni| FixedDotIndex::new(ni))
@ -259,7 +259,7 @@ impl<'a> FixedDot<'a> {
.0,
)
.unwrap()
.is_end()
.is_fixed_end()
})
.filter(|ni| self.graph.node_weight(*ni).unwrap().is_fixed_bend())
.map(|ni| FixedBendIndex::new(ni))

View File

@ -127,7 +127,7 @@ impl Router {
} else {
let from_weight = self.layout.primitive(from).weight();
self.layout
.add_dot(FixedDotWeight {
.add_fixed_dot(FixedDotWeight {
net: from_weight.net,
circle: Circle { pos: to, r: 2.0 },
})