diff --git a/src/layout.rs b/src/layout.rs index 6aacf83..7755a9b 100644 --- a/src/layout.rs +++ b/src/layout.rs @@ -22,14 +22,14 @@ use crate::connectivity::{ use crate::graph::{GenericIndex, GetNodeIndex}; use crate::guide::Guide; use crate::layout::bend::BendIndex; -use crate::layout::geometry::{BendWeight, DotWeight, Geometry, SegWeight}; +use crate::layout::dot::DotWeight; +use crate::layout::geometry::{BendWeightTrait, DotWeightTrait, Geometry, SegWeightTrait}; use crate::layout::seg::{SegIndex, SeqLooseSegWeight}; use crate::layout::{ bend::{FixedBendIndex, LooseBendIndex, LooseBendWeight}, dot::{DotIndex, FixedDotIndex, FixedDotWeight, LooseDotIndex, LooseDotWeight}, geometry::{ - GeometryGraph, GeometryIndex, GeometryLabel, GeometryWeight, GetComponentIndex, - MakePrimitive, Retag, + GeometryIndex, GeometryLabel, GeometryWeight, GetComponentIndex, MakePrimitive, Retag, }, seg::{FixedSegIndex, FixedSegWeight, LoneLooseSegIndex, LoneLooseSegWeight, SeqLooseSegIndex}, }; @@ -75,7 +75,7 @@ pub struct AlreadyConnected(pub i64, pub GeometryIndex); pub struct Layout { rtree: RTree, connectivity: ConnectivityGraph, - geometry: Geometry, + geometry: Geometry, } #[debug_invariant(self.geometry.graph().node_count() == self.rtree.size())] @@ -224,7 +224,7 @@ impl Layout { #[debug_ensures(ret.is_ok() -> self.geometry.graph().node_count() == old(self.geometry.graph().node_count() + 1))] #[debug_ensures(ret.is_err() -> self.geometry.graph().node_count() == old(self.geometry.graph().node_count()))] - fn add_dot_infringably( + fn add_dot_infringably>( &mut self, weight: W, infringables: &[GeometryIndex], @@ -604,7 +604,7 @@ impl Layout { #[debug_ensures(ret.is_ok() -> self.geometry.graph().edge_count() == old(self.geometry.graph().edge_count() + 2))] #[debug_ensures(ret.is_err() -> self.geometry.graph().node_count() == old(self.geometry.graph().node_count()))] #[debug_ensures(ret.is_err() -> self.geometry.graph().edge_count() == old(self.geometry.graph().edge_count()))] - fn add_seg_infringably( + fn add_seg_infringably>( &mut self, from: DotIndex, to: DotIndex, @@ -681,7 +681,7 @@ impl Layout { #[debug_ensures(ret.is_err() -> self.geometry.graph().node_count() == old(self.geometry.graph().node_count()))] #[debug_ensures(ret.is_ok() -> self.geometry.graph().edge_count() == old(self.geometry.graph().edge_count() + 3))] #[debug_ensures(ret.is_err() -> self.geometry.graph().edge_count() == old(self.geometry.graph().edge_count()))] - fn add_core_bend_infringably( + fn add_core_bend_infringably>( &mut self, from: DotIndex, to: DotIndex, @@ -703,7 +703,7 @@ impl Layout { #[debug_ensures(ret.is_err() -> self.geometry.graph().node_count() == old(self.geometry.graph().node_count()))] #[debug_ensures(ret.is_ok() -> self.geometry.graph().edge_count() == old(self.geometry.graph().edge_count() + 4))] #[debug_ensures(ret.is_err() -> self.geometry.graph().edge_count() == old(self.geometry.graph().edge_count()))] - fn add_outer_bend_infringably( + fn add_outer_bend_infringably>( &mut self, from: LooseDotIndex, to: LooseDotIndex, @@ -953,7 +953,7 @@ impl Layout { #[debug_ensures(self.geometry.graph().node_count() == old(self.geometry.graph().node_count()))] #[debug_ensures(self.geometry.graph().edge_count() == old(self.geometry.graph().edge_count()))] - pub fn geometry(&self) -> &Geometry { + pub fn geometry(&self) -> &Geometry { &self.geometry } diff --git a/src/layout/bend.rs b/src/layout/bend.rs index 26ac986..27ae154 100644 --- a/src/layout/bend.rs +++ b/src/layout/bend.rs @@ -8,7 +8,7 @@ use crate::{ }; use super::geometry::{ - BendWeight, GeometryIndex, GeometryWeight, GetBandIndex, GetComponentIndex, + BendWeightTrait, GeometryIndex, GeometryWeight, GetBandIndex, GetComponentIndex, GetComponentIndexMut, GetOffset, GetWidth, MakePrimitive, Retag, }; use petgraph::stable_graph::NodeIndex; @@ -37,7 +37,7 @@ pub struct FixedBendWeight { } impl_fixed_weight!(FixedBendWeight, FixedBend, FixedBendIndex); -impl BendWeight for FixedBendWeight {} +impl BendWeightTrait for FixedBendWeight {} impl GetWidth for FixedBendWeight { fn width(&self) -> f64 { @@ -59,4 +59,4 @@ impl GetOffset for LooseBendWeight { } impl_loose_weight!(LooseBendWeight, LooseBend, LooseBendIndex); -impl BendWeight for LooseBendWeight {} +impl BendWeightTrait for LooseBendWeight {} diff --git a/src/layout/dot.rs b/src/layout/dot.rs index 4841198..062fd39 100644 --- a/src/layout/dot.rs +++ b/src/layout/dot.rs @@ -9,7 +9,7 @@ use crate::{ }; use super::geometry::{ - DotWeight, GeometryIndex, GeometryWeight, GetBandIndex, GetComponentIndex, + DotWeightTrait, GeometryIndex, GeometryWeight, GetBandIndex, GetComponentIndex, GetComponentIndexMut, GetWidth, MakePrimitive, Retag, }; use petgraph::stable_graph::NodeIndex; @@ -24,12 +24,42 @@ pub enum DotIndex { impl From for GeometryIndex { fn from(dot: DotIndex) -> Self { match dot { - DotIndex::Fixed(fixed) => GeometryIndex::FixedDot(fixed), - DotIndex::Loose(loose) => GeometryIndex::LooseDot(loose), + DotIndex::Fixed(index) => GeometryIndex::FixedDot(index), + DotIndex::Loose(index) => GeometryIndex::LooseDot(index), } } } +#[enum_dispatch(GetWidth)] +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum DotWeight { + Fixed(FixedDotWeight), + Loose(LooseDotWeight), +} + +impl From for GeometryWeight { + fn from(dot: DotWeight) -> Self { + match dot { + DotWeight::Fixed(weight) => GeometryWeight::FixedDot(weight), + DotWeight::Loose(weight) => GeometryWeight::LooseDot(weight), + } + } +} + +impl TryFrom for DotWeight { + type Error = (); // TODO. + + fn try_from(weight: GeometryWeight) -> Result { + match weight { + GeometryWeight::FixedDot(weight) => Ok(DotWeight::Fixed(weight)), + GeometryWeight::LooseDot(weight) => Ok(DotWeight::Loose(weight)), + _ => unreachable!(), + } + } +} + +impl DotWeightTrait for DotWeight {} + #[derive(Debug, Clone, Copy, PartialEq)] pub struct FixedDotWeight { pub component: ComponentIndex, @@ -37,7 +67,7 @@ pub struct FixedDotWeight { } impl_fixed_weight!(FixedDotWeight, FixedDot, FixedDotIndex); -impl DotWeight for FixedDotWeight {} +impl DotWeightTrait for FixedDotWeight {} impl GetWidth for FixedDotWeight { fn width(&self) -> f64 { @@ -52,7 +82,7 @@ pub struct LooseDotWeight { } impl_loose_weight!(LooseDotWeight, LooseDot, LooseDotIndex); -impl DotWeight for LooseDotWeight {} +impl DotWeightTrait for LooseDotWeight {} impl GetWidth for LooseDotWeight { fn width(&self) -> f64 { diff --git a/src/layout/geometry.rs b/src/layout/geometry.rs index 8d8b588..bb3d435 100644 --- a/src/layout/geometry.rs +++ b/src/layout/geometry.rs @@ -44,6 +44,7 @@ pub trait GetWidth { fn width(&self) -> f64; } +#[enum_dispatch] pub trait GetOffset { fn offset(&self) -> f64; } @@ -96,8 +97,6 @@ macro_rules! impl_loose_weight { }; } -pub type GeometryGraph = StableDiGraph; - #[enum_dispatch(Retag)] #[derive(Debug, Clone, Copy, PartialEq)] pub enum GeometryWeight { @@ -134,33 +133,55 @@ pub trait MakePrimitive { fn primitive<'a>(&self, layout: &'a Layout) -> Primitive<'a>; } -pub trait DotWeight: GetWidth + Into + Copy {} -pub trait SegWeight: Into + Copy {} -pub trait BendWeight: Into + Copy {} +pub trait DotWeightTrait: GetWidth + Into + Copy {} +pub trait SegWeightTrait: Into + Copy {} +pub trait BendWeightTrait: Into + Copy {} #[derive(Debug)] -pub struct Geometry { - pub graph: GeometryGraph, +pub struct Geometry< + GW: TryInto, + DW: DotWeightTrait, + DI: GetNodeIndex, + SI: GetNodeIndex, + BI: GetNodeIndex, +> { + pub graph: StableDiGraph, + weight_marker: PhantomData, + dot_weight_marker: PhantomData, dot_index_marker: PhantomData, seg_index_marker: PhantomData, bend_index_marker: PhantomData, } -impl Geometry { +impl< + GW: TryInto, + DW: DotWeightTrait + Copy, + DI: GetNodeIndex, + SI: GetNodeIndex, + BI: GetNodeIndex, + > Geometry +{ pub fn new() -> Self { Self { graph: StableDiGraph::default(), + weight_marker: PhantomData, + dot_weight_marker: PhantomData, dot_index_marker: PhantomData, seg_index_marker: PhantomData, bend_index_marker: PhantomData, } } - pub fn add_dot(&mut self, weight: W) -> GenericIndex { + pub fn add_dot>(&mut self, weight: W) -> GenericIndex { GenericIndex::::new(self.graph.add_node(weight.into())) } - pub fn add_seg(&mut self, from: DI, to: DI, weight: W) -> GenericIndex { + pub fn add_seg>( + &mut self, + from: DI, + to: DI, + weight: W, + ) -> GenericIndex { let seg = GenericIndex::::new(self.graph.add_node(weight.into())); self.graph @@ -171,7 +192,7 @@ impl Geometry seg } - pub fn add_bend( + pub fn add_bend>( &mut self, from: DI, to: DI, @@ -193,7 +214,7 @@ impl Geometry bend } - pub fn graph(&self) -> &GeometryGraph { + pub fn graph(&self) -> &StableDiGraph { &self.graph } } diff --git a/src/layout/seg.rs b/src/layout/seg.rs index dd14d22..8b371b0 100644 --- a/src/layout/seg.rs +++ b/src/layout/seg.rs @@ -9,7 +9,7 @@ use crate::{ use super::geometry::{ GeometryIndex, GeometryWeight, GetBandIndex, GetComponentIndex, GetComponentIndexMut, GetWidth, - MakePrimitive, Retag, SegWeight, + MakePrimitive, Retag, SegWeightTrait, }; use petgraph::stable_graph::NodeIndex; @@ -38,7 +38,7 @@ pub struct FixedSegWeight { } impl_fixed_weight!(FixedSegWeight, FixedSeg, FixedSegIndex); -impl SegWeight for FixedSegWeight {} +impl SegWeightTrait for FixedSegWeight {} impl GetWidth for FixedSegWeight { fn width(&self) -> f64 { @@ -52,7 +52,7 @@ pub struct LoneLooseSegWeight { } impl_loose_weight!(LoneLooseSegWeight, LoneLooseSeg, LoneLooseSegIndex); -impl SegWeight for LoneLooseSegWeight {} +impl SegWeightTrait for LoneLooseSegWeight {} #[derive(Debug, Clone, Copy, PartialEq)] pub struct SeqLooseSegWeight { @@ -60,4 +60,4 @@ pub struct SeqLooseSegWeight { } impl_loose_weight!(SeqLooseSegWeight, SeqLooseSeg, SeqLooseSegIndex); -impl SegWeight for SeqLooseSegWeight {} +impl SegWeightTrait for SeqLooseSegWeight {}