/// Convert the given laser frame vector segments to a graph of points.
Convert the given laser frame vector segments to a graph of points.
Convert the given laser frame vector segments to a graph of points .
pub fn segments_to_point_graph<I>(segments: I) -> PointGraph
where
    I: IntoIterator<Item = Segment>,
{
    // A hashable version of a `Point`, used for removing point duplicates during graph generation.
    #[derive(Eq, Hash, PartialEq)]
    struct HashPoint {
        pos: [i32; 2],
        rgb: [u32; 3],
    }
    struct Node {
        ix: NodeIndex,
        weight: u32,
    }
    impl From<Point> for HashPoint {
        fn from(p: Point) -> Self {
            let [px, py] = p.position;
            let [pr, pg, pb] = p.color;
            let x = (px * std::i16::MAX as f32) as i32;
            let y = (py * std::i16::MAX as f32) as i32;
            let r = (pr * std::u16::MAX as f32) as u32;
            let g = (pg * std::u16::MAX as f32) as u32;
            let b = (pb * std::u16::MAX as f32) as u32;
            let pos = [x, y];
            let rgb = [r, g, b];
            HashPoint { pos, rgb }
        }
    }
    let mut g = PointGraph::default();
    let mut pt_to_id = HashMap::new();
    // Build the graph.
    for seg in segments {
        match seg.kind {
            SegmentKind::Blank => (),
            SegmentKind::Lit => {
                let ha = HashPoint::from(seg.start);
                let hb = HashPoint::from(seg.end);
                let na = {
                    let n = pt_to_id.entry(ha).or_insert_with(|| {
                        let ix = g.add_node(seg.start);
                        let weight = seg.start.weight;
                        Node { ix, weight }
                    });
                    n.weight = std::cmp::max(n.weight, seg.start.weight);
                    n.ix
                };
                let nb = {
                    let n = pt_to_id.entry(hb).or_insert_with(|| {
                        let ix = g.add_node(seg.end);
                        let weight = seg.end.weight;
                        Node { ix, weight }
                    });
                    n.weight = std::cmp::max(n.weight, seg.end.weight);
                    n.ix
                };
                if na == nb {
                    continue;
                }
                if g.find_edge(na, nb).is_none() {
                    g.add_edge(na, nb, ());
                }
            }
        }
    }
    g
}
pub fn segments_to_point_graph < I > ( segments : I ) -> PointGraph where I : IntoIterator < Item = Segment > , { // A hashable version of a `Point`, used for removing point duplicates during graph generation. # [ derive ( Eq Hash PartialEq ) ] struct HashPoint { pos : [ i32 ; 2 ] , rgb : [ u32 ; 3 ] , } struct Node { ix : NodeIndex , weight : u32 , } impl From < Point > for HashPoint { fn from ( p : Point ) -> Self { let [ px , py ] = p . position ; let [ pr , pg , pb ] = p . color ; let x = ( px * std :: i16 :: MAX as f32 ) as i32 ; let y = ( py * std :: i16 :: MAX as f32 ) as i32 ; let r = ( pr * std :: u16 :: MAX as f32 ) as u32 ; let g = ( pg * std :: u16 :: MAX as f32 ) as u32 ; let b = ( pb * std :: u16 :: MAX as f32 ) as u32 ; let pos = [ x , y ] ; let rgb = [ r , g , b ] ; HashPoint { pos , rgb } } } let mut g = PointGraph :: default ( ) ; let mut pt_to_id = HashMap :: new ( ) ; // Build the graph. for seg in segments { match seg . kind { SegmentKind :: Blank => ( ) , SegmentKind :: Lit => { let ha = HashPoint :: from ( seg . start ) ; let hb = HashPoint :: from ( seg . end ) ; let na = { let n = pt_to_id . entry ( ha ) . or_insert_with ( | | { let ix = g . add_node ( seg . start ) ; let weight = seg . start . weight ; Node { ix , weight } } ) ; n . weight = std :: cmp :: max ( n . weight , seg . start . weight ) ; n . ix } ; let nb = { let n = pt_to_id . entry ( hb ) . or_insert_with ( | | { let ix = g . add_node ( seg . end ) ; let weight = seg . end . weight ; Node { ix , weight } } ) ; n . weight = std :: cmp :: max ( n . weight , seg . end . weight ) ; n . ix } ; if na == nb { continue ; } if g . find_edge ( na , nb ) . is_none ( ) { g . add_edge ( na , nb , ( ) ) ; } } } } g }
Convert the given laser frame vector segments to a graph of points.
Convert the given laser frame vector segments to a graph of points .
segments
I