use std::mem::{self, swap}; use petgraph::stable_graph::StableDiGraph; use petgraph::Direction::{Incoming, Outgoing}; use crate::graph::{ BendIndex, BendWeight, DotIndex, DotWeight, Index, Label, Path, SegIndex, SegWeight, Tag, TaggedIndex, TaggedWeight, }; use crate::math; use crate::shape::{BendShape, DotShape, SegShape, Shape}; #[derive(Debug)] 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 { match self.tagged_weight() { TaggedWeight::Dot(dot) => Shape::Dot(DotShape { c: dot.circle }), TaggedWeight::Seg(seg) => { let ends = self.ends(); Shape::Seg(SegShape { from: self.primitive(ends[0]).weight().circle.pos, to: self.primitive(ends[1]).weight().circle.pos, width: seg.width, }) } TaggedWeight::Bend(bend) => { let ends = self.ends(); let mut bend_shape = BendShape { from: self.primitive(ends[0]).weight().circle.pos, to: self.primitive(ends[1]).weight().circle.pos, center: self.primitive(self.core().unwrap()).weight().circle.pos, width: self.primitive(ends[0]).weight().circle.r * 2.0, }; if bend.cw { swap(&mut bend_shape.from, &mut bend_shape.to); } Shape::Bend(bend_shape) } } } pub fn next(&self) -> Option { self.graph .neighbors_directed(self.index.index, Outgoing) .filter(|ni| { self.graph .edge_weight(self.graph.find_edge(self.index.index, *ni).unwrap()) .unwrap() .is_end() }) .map(|ni| Index::