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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use crate::*;
use core::cmp::Ordering;
use core::convert::TryFrom;
use primitive_from::PrimitiveFrom;

///Convenience function to create a ray.
#[must_use]
pub fn ray<N>(point: Vec2<N>, dir: Vec2<N>) -> Ray<N> {
    Ray { point, dir }
}

///A Ray.
#[derive(Debug, Copy, Clone)]
#[must_use]
pub struct Ray<N> {
    pub point: Vec2<N>,
    pub dir: Vec2<N>,
}

impl<B: Copy> Ray<B> {
    #[inline(always)]
    pub fn inner_as<A: PrimitiveFrom<B>>(&self) -> Ray<A> {
        Ray {
            point: self.point.inner_as(),
            dir: self.dir.inner_as(),
        }
    }
}

impl<N: Copy + core::ops::Add<Output = N> + core::ops::Mul<Output = N>> Ray<N> {
    #[inline(always)]
    pub fn point_at_tval(&self, tval: N) -> Vec2<N> {
        self.point + self.dir * tval
    }
}
impl<N> Ray<N> {
    #[inline(always)]
    pub fn inner_into<B: From<N>>(self) -> Ray<B> {
        let point = self.point.inner_into();
        let dir = self.dir.inner_into();
        Ray { point, dir }
    }
    #[inline(always)]
    pub fn inner_try_into<B: TryFrom<N>>(self) -> Result<Ray<B>, B::Error> {
        let point = self.point.inner_try_into();
        let dir = self.dir.inner_try_into();
        match (point, dir) {
            (Ok(point), Ok(dir)) => Ok(Ray { point, dir }),
            (Err(e), Ok(_)) => Err(e),
            (Ok(_), Err(e)) => Err(e),
            (Err(e), Err(_)) => Err(e),
        }
    }
}

impl<N: PartialOrd + Copy> Ray<N> {
    #[inline(always)]
    pub fn range_side(&self, axis: impl Axis, range: &Range<N>) -> Ordering {
        let v = if axis.is_xaxis() {
            self.point.x
        } else {
            self.point.y
        };

        range.contains_ext(v)
    }
}

///Describes if a ray hit a rectangle.
#[derive(Copy, Clone, Debug)]
#[must_use]
pub enum CastResult<N> {
    Hit(N),
    NoHit,
}

impl<N> CastResult<N> {
    #[inline(always)]
    pub fn map<X>(self, mut func: impl FnMut(N) -> X) -> CastResult<X> {
        match self {
            CastResult::Hit(a) => CastResult::Hit(func(a)),
            CastResult::NoHit => CastResult::NoHit,
        }
    }

    #[inline(always)]
    pub fn unwrap(self) -> N {
        match self {
            CastResult::Hit(a) => a,
            CastResult::NoHit => panic!("unwrapped a NoHit in CastResult"),
        }
    }
}

use roots;
use roots::*;
impl<N: num_traits::Float + roots::FloatType> Ray<N> {
    ///Checks if a ray intersects a circle.
    pub fn cast_to_circle(&self, center: Vec2<N>, radius: N) -> CastResult<N> {
        //https://math.stackexchange.com/questions/311921/get-location-of-vector-circle-intersection
        //circle
        //(x-center.x)^2+(y-center.y)^2=r2
        //ray
        //x(t)=ray.dir.x*t+ray.point.x
        //y(t)=ray.dir.y*t+ray.point.y
        //
        //solve for t.
        //
        //
        //we get:
        //
        //𝑎𝑡^2+𝑏𝑡+𝑐=0
        //
        //
        //
        //
        let ray = self;
        let zz = <N as FloatType>::zero();
        let two = <N as FloatType>::one()+<N as FloatType>::one();

        let a = ray.dir.x.powi(2) + ray.dir.y.powi(2);
        let b =
            two * ray.dir.x * (ray.point.x - center.x) + two * ray.dir.y * (ray.point.y - center.y);
        let c =
            (ray.point.x - center.x).powi(2) + (ray.point.y - center.y).powi(2) - radius.powi(2);

        match find_roots_quadratic(a, b, c) {
            Roots::No(_) => CastResult::NoHit,
            Roots::One([a]) => {
                if a < zz {
                    CastResult::NoHit
                } else {
                    CastResult::Hit(a)
                }
            }
            Roots::Two([a, b]) => {
                let (closer, further) = if a < b { (a, b) } else { (b, a) };

                if closer < zz && further < zz {
                    CastResult::NoHit
                } else if closer < zz && further > zz {
                    CastResult::Hit(<N as FloatType>::zero())
                } else {
                    CastResult::Hit(closer)
                }
            }
            _ => unreachable!(),
        }
    }
}

//TODO make a float specific one

impl<N: num_traits::Num + num_traits::Signed + PartialOrd + Copy + Ord + core::fmt::Debug> Ray<N> {
    ///Returns if a ray intersects a box.
    pub fn cast_to_rect(&self, rect: &Rect<N>) -> CastResult<N> {
        let ray = self;

        //Find the corner that the ray will hit one of its sides with.
        let next_grid_pos = {
            vec2(
                if ray.dir.x < N::zero() {
                    rect.x.end
                } else if ray.dir.x > N::zero() {
                    rect.x.start
                } else {
                    if rect.x.contains(ray.point.x) {
                        let diff = rect.y.difference_to_point(ray.point.y);
                        match diff {
                            Some(diff) => {
                                if diff.signum() == -ray.dir.y.signum() {
                                    return CastResult::Hit(diff.abs());
                                } else {
                                    return CastResult::NoHit;
                                }
                            }
                            None => return CastResult::Hit(N::zero()),
                        }
                    } else {
                        return CastResult::NoHit;
                    }
                },
                if ray.dir.y < N::zero() {
                    rect.y.end
                } else if ray.dir.y > N::zero() {
                    rect.y.start
                } else {
                    if rect.y.contains(ray.point.y) {
                        let diff = rect.x.difference_to_point(ray.point.x);
                        match diff {
                            Some(diff) => {
                                if diff.signum() == -ray.dir.x.signum() {
                                    return CastResult::Hit(diff.abs());
                                } else {
                                    return CastResult::NoHit;
                                }
                            }
                            None => return CastResult::Hit(N::zero()),
                        }
                    } else {
                        return CastResult::NoHit;
                    }
                },
            )
        };

        //Compute the tval of hitting both the x and y axis.
        let tvalx = (next_grid_pos.x - ray.point.x) / ray.dir.x;
        let tvaly = (next_grid_pos.y - ray.point.y) / ray.dir.y;

        fn as_positive<N: PartialOrd + num_traits::Signed>(a: N) -> Option<N> {
            if a > N::zero() {
                Some(a)
            } else {
                None
            }
        }

        match (as_positive(tvalx), as_positive(tvaly)) {
            (Some(x), Some(y)) => {
                let x = if rect.y.contains(ray.point_at_tval(x).y) {
                    Some(x)
                } else {
                    None
                };

                let y = if rect.x.contains(ray.point_at_tval(y).x) {
                    Some(y)
                } else {
                    None
                };

                match (x, y) {
                    (Some(x), Some(y)) => CastResult::Hit(x.min(y)),
                    (Some(x), None) => CastResult::Hit(x),
                    (None, Some(y)) => CastResult::Hit(y),
                    (None, None) => CastResult::NoHit,
                }
            }
            (Some(x), None) => {
                if rect.y.contains(ray.point_at_tval(x).y) {
                    CastResult::Hit(x)
                } else {
                    CastResult::NoHit
                }
            }
            (None, Some(y)) => {
                if rect.x.contains(ray.point_at_tval(y).x) {
                    CastResult::Hit(y)
                } else {
                    CastResult::NoHit
                }
            }
            (None, None) => {
                if rect.contains_point(ray.point) {
                    CastResult::Hit(N::zero())
                } else {
                    CastResult::NoHit
                }
            }
        }
    }
}