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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//! Intersection between a ray and a line segment in 2D

use fj_math::Segment;

use super::{HorizontalRayToTheRight, Intersect};

impl Intersect for (&HorizontalRayToTheRight<2>, &Segment<2>) {
    type Intersection = RaySegmentIntersection;

    fn intersect(self) -> Option<Self::Intersection> {
        let (ray, segment) = self;

        let [a, b] = segment.points();
        let [lower, upper] = if a.v <= b.v { [a, b] } else { [b, a] };
        let [left, right] = if a.u <= b.u { [a, b] } else { [b, a] };

        if ray.origin.v > upper.v {
            // ray is above segment
            return None;
        }
        if ray.origin.v < lower.v {
            // ray is below segment
            return None;
        }

        if ray.origin.v == lower.v && lower.v == upper.v {
            // ray and segment are parallel and at same height

            if ray.origin.u > right.u {
                return None;
            }

            if ray.origin.u == a.u {
                return Some(RaySegmentIntersection::RayStartsOnOnFirstVertex);
            }
            if ray.origin.u == b.u {
                return Some(RaySegmentIntersection::RayStartsOnSecondVertex);
            }
            if ray.origin.u > left.u && ray.origin.u < right.u {
                return Some(RaySegmentIntersection::RayStartsOnSegment);
            }

            return Some(RaySegmentIntersection::RayHitsSegmentAndAreParallel);
        }

        let pa = [lower.u.into(), lower.v.into()];
        let pb = [upper.u.into(), upper.v.into()];
        let pc = [ray.origin.u.into(), ray.origin.v.into()];

        let orient2d = robust_predicates::orient2d(&pa, &pb, &pc);

        if orient2d == 0. {
            // ray starts on the line

            if ray.origin.v == a.v {
                return Some(RaySegmentIntersection::RayStartsOnOnFirstVertex);
            }
            if ray.origin.v == b.v {
                return Some(RaySegmentIntersection::RayStartsOnSecondVertex);
            }

            return Some(RaySegmentIntersection::RayStartsOnSegment);
        }

        if orient2d > 0. {
            // ray starts left of the line

            if ray.origin.v == upper.v {
                return Some(RaySegmentIntersection::RayHitsUpperVertex);
            }
            if ray.origin.v == lower.v {
                return Some(RaySegmentIntersection::RayHitsLowerVertex);
            }

            return Some(RaySegmentIntersection::RayHitsSegment);
        }

        None
    }
}

/// An intersection between a ray and a line segment
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum RaySegmentIntersection {
    /// The ray hit the segment itself
    RayHitsSegment,

    /// The ray hit the lower vertex of the segment
    RayHitsLowerVertex,

    /// The ray hit the upper vertex of the segment
    RayHitsUpperVertex,

    /// The ray hit the whole segment, as it is parallel to the ray
    RayHitsSegmentAndAreParallel,

    /// The ray starts on the segment
    RayStartsOnSegment,

    /// The ray starts on the first vertex of the segment
    RayStartsOnOnFirstVertex,

    /// The ray starts on the second vertex of the segment
    RayStartsOnSecondVertex,
}

#[cfg(test)]
mod tests {
    use fj_math::Segment;

    use crate::algorithms::intersect::Intersect;

    use super::{HorizontalRayToTheRight, RaySegmentIntersection};

    #[test]
    fn ray_is_left_of_segment() {
        let ray = HorizontalRayToTheRight::from([0., 2.]);

        let below = Segment::from([[1., 0.], [1., 1.]]);
        let above = Segment::from([[1., 3.], [1., 4.]]);
        let same_level = Segment::from([[1., 1.], [1., 3.]]);

        assert!((&ray, &below).intersect().is_none());
        assert!((&ray, &above).intersect().is_none());
        assert!(matches!(
            (&ray, &same_level).intersect(),
            Some(RaySegmentIntersection::RayHitsSegment)
        ));
    }

    #[test]
    fn ray_is_right_of_segment() {
        let ray = HorizontalRayToTheRight::from([1., 2.]);

        let same_level = Segment::from([[0., 1.], [0., 3.]]);
        assert!((&ray, &same_level).intersect().is_none());
    }

    #[test]
    fn ray_overlaps_with_segment_along_x_axis() {
        let ray = HorizontalRayToTheRight::from([1., 1.]);

        let no_hit = Segment::from([[0., 0.], [2., 3.]]);

        let hit_segment = Segment::from([[0., 0.], [3., 2.]]);
        let hit_upper = Segment::from([[0., 0.], [2., 1.]]);
        let hit_lower = Segment::from([[0., 2.], [2., 1.]]);

        assert!((&ray, &no_hit).intersect().is_none());
        assert!(matches!(
            (&ray, &hit_segment).intersect(),
            Some(RaySegmentIntersection::RayHitsSegment)
        ));
        assert!(matches!(
            (&ray, &hit_upper).intersect(),
            Some(RaySegmentIntersection::RayHitsUpperVertex),
        ));
        assert!(matches!(
            (&ray, &hit_lower).intersect(),
            Some(RaySegmentIntersection::RayHitsLowerVertex),
        ));
    }

    #[test]
    fn ray_starts_on_segment() {
        let ray = HorizontalRayToTheRight::from([1., 1.]);

        let hit_segment = Segment::from([[0., 0.], [2., 2.]]);
        let hit_upper = Segment::from([[0., 0.], [1., 1.]]);
        let hit_lower = Segment::from([[1., 1.], [2., 2.]]);

        assert!(matches!(
            (&ray, &hit_segment).intersect(),
            Some(RaySegmentIntersection::RayStartsOnSegment)
        ));
        assert!(matches!(
            (&ray, &hit_upper).intersect(),
            Some(RaySegmentIntersection::RayStartsOnSecondVertex),
        ));
        assert!(matches!(
            (&ray, &hit_lower).intersect(),
            Some(RaySegmentIntersection::RayStartsOnOnFirstVertex),
        ));
    }

    #[test]
    fn ray_and_segment_are_parallel_and_on_same_level() {
        let ray = HorizontalRayToTheRight::from([2., 0.]);

        let left = Segment::from([[0., 0.], [1., 0.]]);
        let right = Segment::from([[3., 0.], [4., 0.]]);

        assert!((&ray, &left).intersect().is_none());
        assert!(matches!(
            (&ray, &right).intersect(),
            Some(RaySegmentIntersection::RayHitsSegmentAndAreParallel)
        ));
    }

    #[test]
    fn ray_starts_on_parallel_segment() {
        let ray = HorizontalRayToTheRight::from([2., 0.]);

        let left = Segment::from([[0., 0.], [2., 0.]]);
        let overlapping = Segment::from([[1., 0.], [3., 0.]]);
        let right = Segment::from([[2., 0.], [4., 0.]]);

        assert!(matches!(
            (&ray, &left).intersect(),
            Some(RaySegmentIntersection::RayStartsOnSecondVertex)
        ));
        assert!(matches!(
            (&ray, &overlapping).intersect(),
            Some(RaySegmentIntersection::RayStartsOnSegment),
        ));
        assert!(matches!(
            (&ray, &right).intersect(),
            Some(RaySegmentIntersection::RayStartsOnOnFirstVertex),
        ));
    }
}