primitive: Move `.weight()` method to a new `GetWeight` trait

This commit is contained in:
Mikolaj Wielgus 2023-10-20 01:08:58 +00:00
parent e271c82b7e
commit 4d20362e00
6 changed files with 22 additions and 9 deletions

View File

@ -7,6 +7,7 @@ use crate::{
guide::Guide, guide::Guide,
layout::Layout, layout::Layout,
math::Circle, math::Circle,
primitive::GetWeight,
rules::{Conditions, Rules}, rules::{Conditions, Rules},
segbend::Segbend, segbend::Segbend,
}; };

View File

@ -5,7 +5,7 @@ use crate::{
graph::{BendIndex, DotIndex}, graph::{BendIndex, DotIndex},
layout::Layout, layout::Layout,
math::{self, Circle}, math::{self, Circle},
primitive::MakeShape, primitive::{GetWeight, MakeShape},
rules::{Conditions, Rules}, rules::{Conditions, Rules},
}; };

View File

@ -12,7 +12,7 @@ use crate::graph::{
BendIndex, BendWeight, DotIndex, DotWeight, GenericIndex, GetNodeIndex, Index, Interior, Label, BendIndex, BendWeight, DotIndex, DotWeight, GenericIndex, GetNodeIndex, Index, Interior, Label,
Retag, SegIndex, SegWeight, Weight, Retag, SegIndex, SegWeight, Weight,
}; };
use crate::primitive::{GenericPrimitive, MakeShape}; use crate::primitive::{GenericPrimitive, GetWeight, MakeShape};
use crate::segbend::Segbend; use crate::segbend::Segbend;
use crate::shape::{Shape, ShapeTrait}; use crate::shape::{Shape, ShapeTrait};

View File

@ -11,6 +11,11 @@ use crate::graph::{
use crate::math::{self, Circle}; use crate::math::{self, Circle};
use crate::shape::{BendShape, DotShape, SegShape, Shape, ShapeTrait}; use crate::shape::{BendShape, DotShape, SegShape, Shape, ShapeTrait};
#[enum_dispatch]
pub trait GetWeight<W> {
fn weight(&self) -> W;
}
#[enum_dispatch] #[enum_dispatch]
pub trait MakeShape { pub trait MakeShape {
fn shape(&self) -> Shape; fn shape(&self) -> Shape;
@ -248,8 +253,10 @@ impl<'a> Dot<'a> {
.filter(|bend| self.primitive(*bend).inner().is_none()) .filter(|bend| self.primitive(*bend).inner().is_none())
.next() .next()
} }
}
pub fn weight(&self) -> DotWeight { impl<'a> GetWeight<DotWeight> for Dot<'a> {
fn weight(&self) -> DotWeight {
self.tagged_weight().into_dot().unwrap() self.tagged_weight().into_dot().unwrap()
} }
} }
@ -272,8 +279,10 @@ impl<'a> Seg<'a> {
pub fn prev(&self) -> Option<DotIndex> { pub fn prev(&self) -> Option<DotIndex> {
self.prev_node().map(|ni| DotIndex::new(ni)) self.prev_node().map(|ni| DotIndex::new(ni))
} }
}
pub fn weight(&self) -> SegWeight { impl<'a> GetWeight<SegWeight> for Seg<'a> {
fn weight(&self) -> SegWeight {
self.tagged_weight().into_seg().unwrap() self.tagged_weight().into_seg().unwrap()
} }
} }
@ -334,10 +343,6 @@ impl<'a> Bend<'a> {
self.prev_node().map(|ni| DotIndex::new(ni)) self.prev_node().map(|ni| DotIndex::new(ni))
} }
pub fn weight(&self) -> BendWeight {
self.tagged_weight().into_bend().unwrap()
}
fn inner_radius(&self) -> f64 { fn inner_radius(&self) -> f64 {
let mut r = 0.0; let mut r = 0.0;
let mut layer = BendIndex::new(self.index.node_index()); let mut layer = BendIndex::new(self.index.node_index());
@ -368,6 +373,12 @@ impl<'a> Bend<'a> {
} }
} }
impl<'a> GetWeight<BendWeight> for Bend<'a> {
fn weight(&self) -> BendWeight {
self.tagged_weight().into_bend().unwrap()
}
}
impl<'a> MakeShape for Bend<'a> { impl<'a> MakeShape for Bend<'a> {
fn shape(&self) -> Shape { fn shape(&self) -> Shape {
let ends = self.ends(); let ends = self.ends();

View File

@ -8,6 +8,7 @@ use crate::layout::Layout;
use crate::math::Circle; use crate::math::Circle;
use crate::mesh::{Mesh, MeshEdgeReference, VertexIndex}; use crate::mesh::{Mesh, MeshEdgeReference, VertexIndex};
use crate::primitive::GetWeight;
use crate::rules::Rules; use crate::rules::Rules;
use crate::tracer::{Trace, Tracer}; use crate::tracer::{Trace, Tracer};

View File

@ -6,7 +6,7 @@ use crate::{
graph::{BendIndex, DotIndex, Ends}, graph::{BendIndex, DotIndex, Ends},
layout::Layout, layout::Layout,
mesh::{Mesh, VertexIndex}, mesh::{Mesh, VertexIndex},
primitive::MakeShape, primitive::{GetWeight, MakeShape},
rules::Rules, rules::Rules,
}; };