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
use mini_math::Point;

use crate::{Distance, Line, LineSegment, Plane, Ray, Sphere, Triangle};

/// Trait for finding the closest point to another object
pub trait ClosestPoint<Other> {
    /// The closest point to another object
    fn closest_point(&self, other: &Other) -> Point;
}

impl ClosestPoint<Point> for Sphere {
    fn closest_point(&self, other: &Point) -> Point {
        self.center + (*other - self.center).normalized() * self.radius
    }
}

impl ClosestPoint<Sphere> for Sphere {
    fn closest_point(&self, other: &Sphere) -> Point {
        self.closest_point(&other.center)
    }
}

impl ClosestPoint<Point> for Line {
    fn closest_point(&self, other: &Point) -> Point {
        let dot = self.direction.dot(*other - self.point);
        self.point + self.direction * dot
    }
}

impl ClosestPoint<Line> for Line {
    fn closest_point(&self, other: &Line) -> Point {
        let w = self.point - other.point;
        let b = self.direction.dot(other.direction);
        let d = self.direction.dot(w);
        let e = other.direction.dot(w);
        let d_p = 1.0 - b * b;

        if d_p < std::f32::EPSILON {
            return self.point;
        }

        let sc = (b * e - d) / d_p;

        self.point + self.direction * sc
    }
}

impl ClosestPoint<Point> for Ray {
    fn closest_point(&self, other: &Point) -> Point {
        let dot = (*other - self.origin).dot(self.direction);

        if dot <= 0.0 {
            self.origin
        } else {
            self.origin + self.direction * dot
        }
    }
}

impl ClosestPoint<Line> for Ray {
    fn closest_point(&self, other: &Line) -> Point {
        let p = Line::new(self.origin, self.direction).closest_point(other);
        self.closest_point(&p)
    }
}

impl ClosestPoint<Ray> for Line {
    fn closest_point(&self, other: &Ray) -> Point {
        self.closest_point(&other.closest_point(self))
    }
}

impl ClosestPoint<LineSegment> for Ray {
    fn closest_point(&self, other: &LineSegment) -> Point {
        let p = Line::new(self.origin, self.direction)
            .closest_point(&Line::from_points(other.start, other.end));
        let p = other.closest_point(&p);
        self.closest_point(&p)
    }
}

impl ClosestPoint<Ray> for LineSegment {
    fn closest_point(&self, other: &Ray) -> Point {
        self.closest_point(&other.closest_point(self))
    }
}

impl ClosestPoint<Ray> for Ray {
    fn closest_point(&self, other: &Ray) -> Point {
        let p = Line::new(other.origin, other.direction)
            .closest_point(&Line::new(self.origin, self.direction));
        let p = other.closest_point(&p);
        self.closest_point(&p)
    }
}

impl ClosestPoint<Point> for LineSegment {
    fn closest_point(&self, other: &Point) -> Point {
        let mut direction = self.end - self.start;
        let length = direction.magnitude();
        direction /= length;

        let dot = (*other - self.start).dot(direction);

        if dot < 0.0 {
            self.start
        } else {
            self.start + direction * dot.min(length)
        }
    }
}

impl ClosestPoint<Line> for LineSegment {
    fn closest_point(&self, other: &Line) -> Point {
        let p = other.closest_point(&Line::from_points(self.start, self.end));
        self.closest_point(&p)
    }
}

impl ClosestPoint<LineSegment> for LineSegment {
    fn closest_point(&self, other: &LineSegment) -> Point {
        let p = Line::from_points(other.start, other.end)
            .closest_point(&Line::from_points(self.start, self.end));
        let p = other.closest_point(&p);
        self.closest_point(&p)
    }
}

impl ClosestPoint<Point> for Plane {
    fn closest_point(&self, other: &Point) -> Point {
        let distance = self.distance(other);
        *other - self.normal * distance
    }
}

impl ClosestPoint<Point> for Triangle {
    fn closest_point(&self, other: &Point) -> Point {
        let plane = Plane::from(self);
        let q = plane.closest_point(other);

        let coordinates = self.barycentric_coordinates(q);
        if coordinates.x >= 0.0 && coordinates.y >= 0.0 && coordinates.z >= 0.0 {
            return q;
        }

        let p0 = LineSegment::new(self.a, self.b).closest_point(other);
        let p1 = LineSegment::new(self.b, self.c).closest_point(other);
        let p2 = LineSegment::new(self.c, self.a).closest_point(other);

        let d0 = (p0 - *other).magnitude_squared();
        let d1 = (p1 - *other).magnitude_squared();
        let d2 = (p2 - *other).magnitude_squared();

        if d0 < d1 && d0 < d2 {
            p0
        } else if d1 < d0 && d1 < d2 {
            p1
        } else {
            p2
        }
    }
}

#[cfg(test)]
mod tests {
    use mini_math::Vector3;

    use super::*;

    #[test]
    fn test_line_line() {
        let line = Line::from_points(Point::new(0.0, 0.0, 0.0), Point::new(0.0, 0.0, 10.0));

        let l = Line::from_points(Point::new(0.0, 0.0, 1.0), Point::new(0.0, 10.0, 10.0));
        assert_eq!(line.closest_point(&l), Point::new(0.0, 0.0, 1.0));

        let l = Line::from_points(Point::new(0.0, 5.0, 5.0), Point::new(0.0, 5.0, 15.0));
        assert_eq!(line.closest_point(&l), Point::new(0.0, 0.0, 0.0));

        let l = Line::from_points(Point::new(0.0, 5.0, 0.0), Point::new(25.0, 5.0, 0.0));
        assert_eq!(line.closest_point(&l), Point::new(0.0, 0.0, 0.0));

        let l = Line::from_points(Point::new(0.0, 5.0, 10.0), Point::new(25.0, 5.0, 10.0));
        assert_eq!(line.closest_point(&l), Point::new(0.0, 0.0, 10.0));
    }

    #[test]
    fn test_ray_point() {
        let ray = Ray::new(Point::zero(), Vector3::new(0.0, 0.0, 1.0));

        let p = Point::new(0.0, 0.0, -5.0);
        assert_eq!(ray.closest_point(&p), Point::zero());

        let p = Point::new(0.0, 5.0, 25.0);
        assert_eq!(ray.closest_point(&p), Point::new(0.0, 0.0, 25.0));
    }

    #[test]
    fn test_ray_line() {
        let ray = Ray::new(Point::zero(), Vector3::new(0.0, 0.0, 1.0));

        let l = Line::new(Point::new(0.0, 5.0, 0.0), Vector3::new(0.0, 0.0, 1.0));
        assert_eq!(ray.closest_point(&l), Point::new(0.0, 0.0, 0.0));

        let l = Line::new(Point::new(0.0, 0.0, -5.0), Vector3::new(0.0, 0.0, -1.0));
        assert_eq!(ray.closest_point(&l), Point::new(0.0, 0.0, 0.0));

        let l = Line::new(Point::new(0.0, 5.0, -5.0), Vector3::new(0.0, 1.0, 0.0));
        assert_eq!(ray.closest_point(&l), Point::new(0.0, 0.0, 0.0));

        let l = Line::new(Point::new(0.0, 5.0, 5.0), Vector3::new(0.0, 1.0, 0.0));
        assert_eq!(ray.closest_point(&l), Point::new(0.0, 0.0, 5.0));
    }

    #[test]
    fn test_plane_point() {
        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 p = Point::new(2.0, 1.0, 3.0);
        assert_eq!(plane.closest_point(&p), Point::new(2.0, 0.0, 3.0));

        let p = Point::new(-2.0, -1.0, -3.0);
        assert_eq!(plane.closest_point(&p), Point::new(-2.0, 0.0, -3.0));
    }

    #[test]
    fn test_triangle_point() {
        let triangle = Triangle::new(
            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 p = Point::new(0.0, 1.0, 0.0);
        assert_eq!(triangle.closest_point(&p), Point::new(0.0, 0.0, 0.0));

        let p = Point::new(0.0, 1.0, 2.0);
        assert_eq!(triangle.closest_point(&p), Point::new(0.0, 0.0, 1.0));

        let p = Point::new(0.0, -1.0, -2.0);
        assert_eq!(triangle.closest_point(&p), Point::new(0.0, 0.0, -1.0));
    }
}