1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
use super::ray::*;
use super::path::*;
use super::to_curves::*;
use super::graph_path::*;
use super::super::curve::*;
use super::super::normal::*;
use super::super::super::geo::*;

use smallvec::*;

///
/// Represents a curve that can be represented either forwards or backwards
///
#[derive(Clone)]
pub (crate) enum ReversableCurve<Curve> {
    Forward(Curve),
    Reversed(Curve)
}

impl<Curve: BezierCurve> Geo for ReversableCurve<Curve> {
    type Point=Curve::Point;
}

impl<Curve: BezierCurve> BezierCurve for ReversableCurve<Curve> {
    #[inline]
    fn start_point(&self) -> Curve::Point { 
        match self {
            ReversableCurve::Forward(curve)     => curve.start_point(),
            ReversableCurve::Reversed(curve)    => curve.end_point()
        }
    }

    #[inline]
    fn end_point(&self) -> Curve::Point { 
        match self {
            ReversableCurve::Forward(curve)     => curve.end_point(),
            ReversableCurve::Reversed(curve)    => curve.start_point()
        }
    }

    #[inline]
    fn control_points(&self) -> (Curve::Point, Curve::Point) {
        match self {
            ReversableCurve::Forward(curve)     => curve.control_points(),
            ReversableCurve::Reversed(curve)    => {
                let (cp1, cp2) = curve.control_points();
                (cp2, cp1)
            }
        }
    }
}

impl<Curve: BezierCurve> RayPath for Vec<Curve> 
where Curve::Point: Coordinate2D {
    type Curve = ReversableCurve<Curve>;
    type Point = Curve::Point;

    #[inline] fn num_points(&self) -> usize {
        self.len()
    }

    #[inline] fn num_edges(&self, _point_idx: usize) -> usize {
        1
    }

    #[inline] fn reverse_edges_for_point(&self, point_idx: usize) -> SmallVec<[GraphEdgeRef; 8]> {
        if point_idx == 0 {
            smallvec![GraphEdgeRef { start_idx: self.len()-1, edge_idx: 0, reverse: true }]
        } else {
            smallvec![GraphEdgeRef { start_idx: point_idx-1, edge_idx: 0, reverse: true }]
        }
    }

    #[inline] fn edges_for_point(&self, point_idx: usize) -> SmallVec<[GraphEdgeRef; 8]> {
        smallvec![GraphEdgeRef { start_idx: point_idx, edge_idx: 0, reverse: false }]
    }

    #[inline] fn get_edge(&self, edge: GraphEdgeRef) -> Self::Curve {
        if edge.reverse {
            ReversableCurve::Reversed(self[edge.start_idx].clone())
        } else {
            ReversableCurve::Forward(self[edge.start_idx].clone())
        }
    }

    #[inline] fn get_next_edge(&self, edge: GraphEdgeRef) -> (GraphEdgeRef, Self::Curve) {
        let next_ref = GraphEdgeRef { start_idx: self.edge_end_point_idx(edge), edge_idx: 0, reverse: edge.reverse };
        (next_ref, self.get_edge(next_ref))
    }

    #[inline] fn point_position(&self, point: usize) -> Self::Point {
        self[point].start_point()
    }

    #[inline] fn edge_start_point_idx(&self, edge: GraphEdgeRef) -> usize {
        if edge.reverse {
            unimplemented!()
        } else {
            edge.start_idx
        }
    }

    #[inline] fn edge_end_point_idx(&self, edge: GraphEdgeRef) -> usize {
        if edge.reverse {
            unimplemented!()
        } else {
            if edge.start_idx+1 == self.len() {
                0
            } else {
                edge.start_idx+1
            }
        }
    }

    #[inline] fn edge_following_edge_idx(&self, _edge: GraphEdgeRef) -> usize {
        0
    }
}

///
/// Returns true if a particular point is within a bezier path
/// 
pub fn path_contains_point<P: BezierPath>(path: &P, point: &P::Point) -> bool
where P::Point: Coordinate2D {
    // We want to cast a ray from the outer edge of the bounds to our point
    let (min_bounds, max_bounds) = path.bounding_box();

    if min_bounds.x() > point.x() || max_bounds.x() < point.x() || min_bounds.y() > point.y() || max_bounds.y() < point.y() {
        // Point is outside the bounds of the path
        false
    } else {
        // Ray is from the top of the bounds to our point
        let ray             = (max_bounds + P::Point::from_components(&[0.01, 0.01]), *point);
        let ray_direction   = ray.1 - ray.0;

        // Call through to ray_collisions to get the collisions
        let curves          = path_to_curves::<_, Curve<_>>(path).collect::<Vec<_>>();
        let collisions      = ray_collisions(&curves, &ray);

        // The total of all of the ray directions
        let mut total_direction     = 0;

        for (collision, curve_t, line_t, _pos) in collisions {
            // Stop once the ray reaches the desired point
            if line_t > 1.0 { break; }

            // Curve this collision was is just the start index of the edge
            let curve_idx   = collision.edge().start_idx;

            // Use the normal at this point to determine the direction relative to the ray
            let normal      = curves[curve_idx].normal_at_pos(curve_t);
            let direction   = ray_direction.dot(&normal).signum() as i32;

            // Add to the total direction
            total_direction += direction;
        }

        // Point is inside the path if the ray crosses more lines facing in a particular direction
        total_direction != 0
    }
}