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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
use crate::{Capsule, ClosestPoint, Distance, LineSegment, Plane, Ray, Sphere, Triangle};
use mini_math::Vector3;

/// Trait for determining whether two shapes intersect with one another
pub trait Intersection<Rhs> {
    /// Whether this shape intersect with the other
    fn intersects(&self, rhs: &Rhs) -> bool;
}

impl Intersection<Ray> for Sphere {
    fn intersects(&self, ray: &Ray) -> bool {
        let p = ray.closest_point(&self.center);
        self.distance(&p) < 0.0
    }
}

impl Intersection<Sphere> for Ray {
    fn intersects(&self, sphere: &Sphere) -> bool {
        sphere.intersects(self)
    }
}

impl Intersection<Capsule> for Ray {
    fn intersects(&self, rhs: &Capsule) -> bool {
        self.distance(rhs) < 0.0
    }
}

impl Intersection<Ray> for Capsule {
    fn intersects(&self, rhs: &Ray) -> bool {
        rhs.intersects(self)
    }
}

impl Intersection<Ray> for Plane {
    fn intersects(&self, ray: &Ray) -> bool {
        let t =
            -(self.d + Vector3::from(ray.origin).dot(self.normal)) / ray.direction.dot(self.normal);
        t >= 0.0
    }
}

impl Intersection<Plane> for Ray {
    fn intersects(&self, plane: &Plane) -> bool {
        plane.intersects(self)
    }
}

impl Intersection<LineSegment> for Sphere {
    fn intersects(&self, line: &LineSegment) -> bool {
        let p = line.closest_point(&self.center);
        self.distance(&p) < 0.0
    }
}

impl Intersection<Sphere> for LineSegment {
    fn intersects(&self, sphere: &Sphere) -> bool {
        sphere.intersects(self)
    }
}

impl Intersection<Sphere> for Plane {
    fn intersects(&self, sphere: &Sphere) -> bool {
        self.distance(&sphere.center).abs() <= sphere.radius
    }
}

impl Intersection<Plane> for Sphere {
    fn intersects(&self, plane: &Plane) -> bool {
        plane.intersects(self)
    }
}

impl Intersection<Sphere> for Sphere {
    fn intersects(&self, sphere: &Sphere) -> bool {
        let combined_radius = self.radius + sphere.radius;
        (self.center - sphere.center).magnitude_squared() <= combined_radius * combined_radius
    }
}

impl Intersection<Sphere> for Triangle {
    fn intersects(&self, sphere: &Sphere) -> bool {
        let plane = Plane::from(self);

        let p = plane.closest_point(&sphere.center);
        let distance_from_plane_squared = (p - sphere.center).magnitude_squared();

        if distance_from_plane_squared > sphere.radius * sphere.radius {
            return false;
        }

        let radius_on_plane = (sphere.radius * sphere.radius - distance_from_plane_squared).sqrt();
        let coordinates = self.barycentric_coordinates(p);

        coordinates.x > -radius_on_plane
            && coordinates.y > -radius_on_plane
            && coordinates.z > -radius_on_plane
    }
}

impl Intersection<Triangle> for Sphere {
    fn intersects(&self, triangle: &Triangle) -> bool {
        triangle.intersects(self)
    }
}

impl Intersection<Ray> for Triangle {
    fn intersects(&self, ray: &Ray) -> bool {
        let plane = Plane::from(self);

        let n_dot_r = plane.normal.dot(ray.direction);
        // early exit if ray parallel to plane
        if n_dot_r.abs() < std::f32::EPSILON {
            return false;
        }

        let d = plane.normal.dot(ray.origin - self.a);
        let t = -d / n_dot_r;

        // early exit if triangle entirely behind ray
        if t < 0.0 {
            return false;
        }

        let intersection_point = ray.origin + ray.direction * t;
        self.coplanar_point_inside(intersection_point)
    }
}

impl Intersection<Triangle> for Ray {
    fn intersects(&self, triangle: &Triangle) -> bool {
        triangle.intersects(self)
    }
}

impl Intersection<LineSegment> for Triangle {
    fn intersects(&self, line: &LineSegment) -> bool {
        let plane = Plane::from(self);

        let mut direction = line.end - line.start;
        let length = direction.magnitude();
        direction /= length;

        let n_dot_r = plane.normal.dot(direction);
        // early exit if line parallel to plane
        if n_dot_r.abs() < std::f32::EPSILON {
            return false;
        }

        let d = plane.normal.dot(line.start - self.a);
        let t = -d / n_dot_r;

        // early exit if triangle is entirely in fornt or behind of the line segment
        if t < 0.0 || t > length {
            return false;
        }

        let intersection_point = line.start + direction * t;
        self.coplanar_point_inside(intersection_point)
    }
}

impl Intersection<Triangle> for LineSegment {
    fn intersects(&self, triangle: &Triangle) -> bool {
        triangle.intersects(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mini_math::{Point, Vector3};

    #[test]
    fn test_ray_sphere_intersects() {
        let sphere = Sphere::new(Point::new(0.0, 20.0, 0.0), 10.0);

        let ray = Ray::new(Point::new(-20.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0));
        assert!(!sphere.intersects(&ray));
        assert!(!ray.intersects(&sphere));

        let ray = Ray::new(Point::new(-20.0, 20.0, 0.0), Vector3::new(1.0, 0.0, 0.0));
        assert!(sphere.intersects(&ray));
        assert!(ray.intersects(&sphere));
    }

    #[test]
    fn test_segment_sphere_intersects() {
        let sphere = Sphere::new(Point::new(0.0, 20.0, 0.0), 10.0);

        let segment = LineSegment::new(Point::new(-20.0, 0.0, 0.0), Point::new(-10.0, 0.0, 0.0));
        assert!(!sphere.intersects(&segment));
        assert!(!segment.intersects(&sphere));

        let segment = LineSegment::new(Point::new(10.0, 0.0, 0.0), Point::new(20.0, 0.0, 0.0));
        assert!(!sphere.intersects(&segment));
        assert!(!segment.intersects(&sphere));

        let segment = LineSegment::new(Point::new(-20.0, 20.0, 0.0), Point::new(20.0, 0.0, 0.0));
        assert!(sphere.intersects(&segment));
        assert!(segment.intersects(&sphere));
    }

    #[test]
    fn test_ray_plane_intersects() {
        let plane = Plane::from_points(
            Point::new(-1.0, 0.0, -1.0),
            Point::new(1.0, 0.0, -1.0),
            Point::new(0.0, 0.0, 1.0),
        );

        let ray = Ray::new(Point::new(0.0, 1.0, 0.0), Vector3::new(0.0, 1.0, 0.0));
        assert!(!plane.intersects(&ray));
        assert!(!ray.intersects(&plane));

        let ray = Ray::new(Point::new(0.0, -1.0, 0.0), Vector3::new(0.0, 1.0, 0.0));
        assert!(plane.intersects(&ray));
        assert!(ray.intersects(&plane));
    }

    #[test]
    fn test_sphere_plane_intersects() {
        let plane = Plane::from_points(
            Point::new(-1.0, 0.0, -1.0),
            Point::new(1.0, 0.0, -1.0),
            Point::new(0.0, 0.0, 1.0),
        );

        let sphere = Sphere::new(Point::new(0.0, 10.0, 0.0), 5.0);
        assert!(!plane.intersects(&sphere));
        assert!(!sphere.intersects(&plane));

        let sphere = Sphere::new(Point::new(0.0, -10.0, 0.0), 5.0);
        assert!(!plane.intersects(&sphere));
        assert!(!sphere.intersects(&plane));

        let sphere = Sphere::new(Point::new(0.0, 2.0, 0.0), 5.0);
        assert!(plane.intersects(&sphere));
        assert!(sphere.intersects(&plane));
    }

    #[test]
    fn test_sphere_sphere_intersects() {
        let sphere1 = Sphere::new(Point::new(10.0, 0.0, 0.0), 5.0);

        let sphere2 = Sphere::new(Point::new(-10.0, 00.0, 0.0), 5.0);
        assert!(!sphere1.intersects(&sphere2));
        assert!(!sphere2.intersects(&sphere1));

        let sphere2 = Sphere::new(Point::new(0.0, 0.0, 0.0), 7.0);
        assert!(sphere1.intersects(&sphere2));
        assert!(sphere2.intersects(&sphere1));
    }

    #[test]
    fn test_triangle_sphere_intersects() {
        let triangle = Triangle::new(
            Point::new(-1.0, 0.0, 0.0),
            Point::new(1.0, 0.0, 0.0),
            Point::new(0.0, 0.0, 1.0),
        );

        // distance from plane in the positive direction
        let sphere = Sphere::new(Point::new(0.0, 1.0, 0.0), 0.5);
        assert!(!triangle.intersects(&sphere));
        assert!(!sphere.intersects(&triangle));

        // distance from plane in the negative direction
        let sphere = Sphere::new(Point::new(0.0, -1.0, 0.0), 0.5);
        assert!(!triangle.intersects(&sphere));
        assert!(!sphere.intersects(&triangle));

        // distance from the plane in the direction of each edge
        let sphere = Sphere::new(Point::new(0.0, 0.0, -3.0), 0.5);
        assert!(!triangle.intersects(&sphere));
        assert!(!sphere.intersects(&triangle));

        let sphere = Sphere::new(Point::new(3.0, 0.0, 3.0), 0.5);
        assert!(!triangle.intersects(&sphere));
        assert!(!sphere.intersects(&triangle));

        let sphere = Sphere::new(Point::new(-3.0, 0.0, 3.0), 0.5);
        assert!(!triangle.intersects(&sphere));
        assert!(!sphere.intersects(&triangle));

        // diagonally from an edge
        let sphere = Sphere::new(Point::new(0.0, 0.3, -0.3), 0.5);
        assert!(triangle.intersects(&sphere));
        assert!(sphere.intersects(&triangle));

        // in the middle of the triangle
        let sphere = Sphere::new(Point::new(0.0, 0.0, 0.0), 0.5);
        assert!(triangle.intersects(&sphere));
        assert!(sphere.intersects(&triangle));
    }

    #[test]
    fn test_triangle_ray_intersects() {
        let triangle = Triangle::new(
            Point::new(-1.0, 0.0, 0.0),
            Point::new(1.0, 0.0, 0.0),
            Point::new(0.0, 0.0, 1.0),
        );

        // parallel
        let ray = Ray::new(Point::new(0.0, 1.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
        assert!(!triangle.intersects(&ray));

        // in front
        let ray = Ray::new(Point::new(0.0, 1.0, 0.0), Vector3::new(0.0, 1.0, 0.0));
        assert!(!triangle.intersects(&ray));

        // behind
        let ray = Ray::new(Point::new(0.0, -1.0, 0.0), Vector3::new(0.0, -1.0, 0.0));
        assert!(!triangle.intersects(&ray));

        // past
        let ray = Ray::new(Point::new(3.0, 1.0, 3.0), Vector3::new(0.0, -1.0, 0.0));
        assert!(!triangle.intersects(&ray));

        // straight through
        let ray = Ray::new(Point::new(0.0, 1.0, 0.0), Vector3::new(0.0, -1.0, 0.0));
        assert!(triangle.intersects(&ray));

        // diagonally through
        let ray = Ray::new(
            Point::new(-0.5, -1.0, 0.0),
            Vector3::new(0.5, 1.0, 0.0).normalized(),
        );
        assert!(triangle.intersects(&ray));
    }

    #[test]
    fn test_triangle_ray_intersects_2() {
        let ray = Ray::new(
            Point::new(0.0, 2.0, -6.0),
            Vector3::new(0.0, 0.0, 1.0).normalized(),
        );
        let triangle = Triangle::new(
            Point::new(2.0, 0.0, -1.25),
            Point::new(0.0, 3.0, 2.5),
            Point::new(-2.0, 0.0, -1.25),
        );

        assert!(triangle.intersects(&ray));
    }

    #[test]
    fn test_triangle_line_segment_intersects() {
        let triangle = Triangle::new(
            Point::new(-1.0, 0.0, 0.0),
            Point::new(1.0, 0.0, 0.0),
            Point::new(0.0, 0.0, 1.0),
        );

        // parallel
        let line = LineSegment::new(Point::new(0.0, 1.0, 0.0), Point::new(0.0, 1.0, 1.0));
        assert!(!triangle.intersects(&line));

        // in front
        let line = LineSegment::new(Point::new(0.0, 1.0, 0.0), Point::new(0.0, 4.0, 0.0));
        assert!(!triangle.intersects(&line));

        // behind
        let line = LineSegment::new(Point::new(0.0, -8.0, 0.0), Point::new(0.0, -1.0, 0.0));
        assert!(!triangle.intersects(&line));

        // past
        let line = LineSegment::new(Point::new(3.0, 1.0, 3.0), Point::new(3.0, -1.0, 3.0));
        assert!(!triangle.intersects(&line));

        // straight through
        let line = LineSegment::new(Point::new(0.0, 1.0, 0.0), Point::new(0.0, -1.0, 0.0));
        assert!(triangle.intersects(&line));

        // diagonally through
        let line = LineSegment::new(Point::new(-0.5, -2.0, 0.0), Point::new(0.5, 2.0, 0.0));
        assert!(triangle.intersects(&line));
    }
}