use std::mem::swap; use petgraph::Direction::{Outgoing, Incoming}; use petgraph::stable_graph::StableDiGraph; use crate::graph::{Set, DotIndex, SegIndex, BendIndex, TaggedIndex, Tag, Index, DotWeight, SegWeight, BendWeight, TaggedWeight, Label}; use crate::shape::Shape; pub struct Primitive<'a, Weight> { pub index: Index, graph: &'a StableDiGraph, } impl<'a, Weight> Primitive<'a, Weight> { pub fn new(index: Index, graph: &'a StableDiGraph) -> Self { Self {index, graph} } pub fn shape(&self) -> Shape { let ends = self.ends(); match self.tagged_weight() { TaggedWeight::Dot(dot) => Shape { width: dot.circle.r * 2.0, from: dot.circle.pos, to: dot.circle.pos, center: None, }, TaggedWeight::Seg(seg) => { Shape { width: seg.width, from: self.primitive(ends[0]).weight().circle.pos, to: self.primitive(ends[1]).weight().circle.pos, center: None, } } TaggedWeight::Bend(bend) => { let mut shape = Shape { width: self.primitive(ends[0]).weight().circle.r * 2.0, from: self.primitive(ends[0]).weight().circle.pos, to: self.primitive(ends[1]).weight().circle.pos, center: Some(self.primitive(self.core().unwrap()).weight().circle.pos), }; if bend.cw { swap(&mut shape.from, &mut shape.to); } shape } } } pub fn next(&self) -> Option { self.graph.neighbors_directed(self.index.index, Outgoing) .filter(|ni| self.graph.edge_weight(self.graph.find_edge(*ni, self.index.index).unwrap()).unwrap().is_end()) .map(|ni| Index::