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
//! Circle primitive

use cgmath::{BaseFloat, Point2, Vector2};
use cgmath::prelude::*;

use {Aabb2, Ray2};
use prelude::*;

/// Circle primitive
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Circle<S> {
    /// Radius of the circle
    pub radius: S,
}

impl<S> Circle<S> {
    /// Create a new circle primitive
    pub fn new(radius: S) -> Self {
        Self { radius }
    }
}

impl<S> Primitive for Circle<S>
where
    S: BaseFloat,
{
    type Point = Point2<S>;

    fn support_point<T>(&self, direction: &Vector2<S>, transform: &T) -> Point2<S>
    where
        T: Transform<Point2<S>>,
    {
        let direction = transform.inverse_transform_vector(*direction).unwrap();
        transform.transform_point(Point2::from_vec(direction.normalize_to(self.radius)))
    }
}

impl<S> ComputeBound<Aabb2<S>> for Circle<S>
where
    S: BaseFloat,
{
    fn compute_bound(&self) -> Aabb2<S> {
        Aabb2::new(
            Point2::new(-self.radius, -self.radius),
            Point2::new(self.radius, self.radius),
        )
    }
}

impl<S> Discrete<Ray2<S>> for Circle<S>
where
    S: BaseFloat,
{
    fn intersects(&self, r: &Ray2<S>) -> bool {
        let s = self;
        let l = Vector2::new(-r.origin.x, -r.origin.y);
        let tca = l.dot(r.direction);
        if tca < S::zero() {
            return false;
        }
        let d2 = l.dot(l) - tca * tca;
        d2 <= s.radius * s.radius
    }
}

impl<S> Continuous<Ray2<S>> for Circle<S>
where
    S: BaseFloat,
{
    type Result = Point2<S>;

    fn intersection(&self, r: &Ray2<S>) -> Option<Point2<S>> {
        let s = self;

        let l = Vector2::new(-r.origin.x, -r.origin.y);
        let tca = l.dot(r.direction);
        if tca < S::zero() {
            return None;
        }
        let d2 = l.dot(l) - tca * tca;
        if d2 > s.radius * s.radius {
            return None;
        }
        let thc = (s.radius * s.radius - d2).sqrt();
        Some(r.origin + r.direction * (tca - thc))
    }
}

#[cfg(test)]
mod tests {
    use std;

    use Ray2;
    use cgmath::{Basis2, Decomposed, Point2, Rad, Rotation2, Vector2};
    use prelude::*;

    use super::*;

    // circle
    #[test]
    fn test_circle_far_1() {
        test_circle(1., 0., 10., 0., 0.);
    }

    #[test]
    fn test_circle_far_2() {
        test_circle(1., 1., 7.0710677, 7.0710677, 0.);
    }

    #[test]
    fn test_circle_far_3() {
        test_circle(1., 0., 10., 0., -std::f32::consts::PI / 4.);
    }

    #[test]
    fn test_circle_far_4() {
        let circle = Circle::new(10.);
        let direction = Vector2::new(1., 0.);
        let transform = transform(0., 10., 0.);
        let point = circle.support_point(&direction, &transform);
        assert_eq!(Point2::new(10., 10.), point);
    }

    #[test]
    fn test_circle_bound() {
        let circle = Circle::new(10.);
        assert_eq!(bound(-10., -10., 10., 10.), circle.compute_bound())
    }

    #[test]
    fn test_circle_ray_discrete() {
        let circle = Circle::new(10.);
        let ray = Ray2::new(Point2::new(25., 0.), Vector2::new(-1., 0.));
        assert!(circle.intersects(&ray));
        let ray = Ray2::new(Point2::new(25., -11.), Vector2::new(-1., 0.));
        assert!(!circle.intersects(&ray));
    }

    #[test]
    fn test_circle_ray_discrete_transformed() {
        let circle = Circle::new(10.);
        let ray = Ray2::new(Point2::new(25., 0.), Vector2::new(-1., 0.));
        let t = transform(0., 0., 0.);
        assert!(circle.intersects_transformed(&ray, &t));
        let t = transform(0., 11., 0.);
        assert!(!circle.intersects_transformed(&ray, &t));
    }

    #[test]
    fn test_circle_ray_continuous() {
        let circle = Circle::new(10.);
        let ray = Ray2::new(Point2::new(25., 0.), Vector2::new(-1., 0.));
        assert_eq!(Some(Point2::new(10., 0.)), circle.intersection(&ray));
        let ray = Ray2::new(Point2::new(25., -11.), Vector2::new(-1., 0.));
        assert_eq!(None, circle.intersection(&ray));
    }

    #[test]
    fn test_circle_ray_continuous_transformed() {
        let circle = Circle::new(10.);
        let ray = Ray2::new(Point2::new(25., 0.), Vector2::new(-1., 0.));
        let t = transform(0., 0., 0.);
        assert_eq!(
            Some(Point2::new(10., 0.)),
            circle.intersection_transformed(&ray, &t)
        );
        let t = transform(0., 11., 0.);
        assert_eq!(None, circle.intersection_transformed(&ray, &t));
    }

    fn test_circle(dx: f32, dy: f32, px: f32, py: f32, rot: f32) {
        let circle = Circle::new(10.);
        let direction = Vector2::new(dx, dy);
        let transform = transform(0., 0., rot);
        let point = circle.support_point(&direction, &transform);
        assert_ulps_eq!(px, point.x);
        assert_ulps_eq!(py, point.y);
    }

    // util
    fn transform(dx: f32, dy: f32, rot: f32) -> Decomposed<Vector2<f32>, Basis2<f32>> {
        Decomposed {
            scale: 1.,
            rot: Rotation2::from_angle(Rad(rot)),
            disp: Vector2::new(dx, dy),
        }
    }

    fn bound(min_x: f32, min_y: f32, max_x: f32, max_y: f32) -> Aabb2<f32> {
        Aabb2::new(Point2::new(min_x, min_y), Point2::new(max_x, max_y))
    }
}