i_overlay/vector/
edge.rs

1use alloc::vec::Vec;
2use i_float::int::point::IntPoint;
3use i_shape::int::path::IntPath;
4
5pub type SideFill = u8;
6pub type VectorPath = Vec<VectorEdge>;
7pub type VectorShape = Vec<VectorPath>;
8
9pub const SUBJ_LEFT: u8 = 0b0001;
10pub const SUBJ_RIGHT: u8 = 0b0010;
11pub const CLIP_LEFT: u8 = 0b0100;
12pub const CLIP_RIGHT: u8 = 0b1000;
13
14pub trait Reverse {
15    fn reverse(self) -> Self;
16}
17
18impl Reverse for SideFill {
19    fn reverse(self) -> Self {
20        let subj_left = self & SUBJ_LEFT;
21        let subj_right = self & SUBJ_RIGHT;
22        let clip_left = self & CLIP_LEFT;
23        let clip_right = self & CLIP_RIGHT;
24
25        (subj_left << 1) | (subj_right >> 1) | (clip_left << 1) | (clip_right >> 1)
26    }
27}
28
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub struct VectorEdge {
31    pub a: IntPoint,
32    pub b: IntPoint,
33    pub fill: SideFill,
34}
35
36impl VectorEdge {
37    pub(crate) fn new(fill: SideFill, a: IntPoint, b: IntPoint) -> Self {
38        let fill = if a < b {
39            fill
40        } else {
41            fill.reverse()
42        };
43
44        Self { a, b, fill }
45    }
46}
47
48pub trait ToPath {
49    fn to_path(&self) -> IntPath;
50}
51
52impl ToPath for VectorPath {
53    fn to_path(&self) -> IntPath {
54        self.iter().map(|e| e.a).collect()
55    }
56}