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
use fj_math::{Line, Plane, Point, Scalar};

use crate::{
    geometry::path::{GlobalPath, SurfacePath},
    insert::Insert,
    objects::{Curve, GlobalCurve, Objects, Surface},
    services::Service,
    storage::Handle,
};

/// The intersection between two surfaces
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct SurfaceSurfaceIntersection {
    /// The intersection curves
    pub intersection_curves: [Handle<Curve>; 2],
}

impl SurfaceSurfaceIntersection {
    /// Compute the intersection between two surfaces
    pub fn compute(
        surfaces: [Handle<Surface>; 2],
        objects: &mut Service<Objects>,
    ) -> Option<Self> {
        // Algorithm from Real-Time Collision Detection by Christer Ericson. See
        // section 5.4.4, Intersection of Two Planes.
        //
        // Adaptations were made to get the intersection curves in local
        // coordinates for each surface.

        let surfaces_and_planes = surfaces.map(|surface| {
            let plane = plane_from_surface(&surface);
            (surface, plane)
        });
        let [a, b] = surfaces_and_planes.clone().map(|(_, plane)| plane);

        let (a_distance, a_normal) = a.constant_normal_form();
        let (b_distance, b_normal) = b.constant_normal_form();

        let direction = a_normal.cross(&b_normal);

        let denom = direction.dot(&direction);
        if denom == Scalar::ZERO {
            // Comparing `denom` against zero looks fishy. It's probably better
            // to compare it against an epsilon value, but I don't know how
            // large that epsilon should be.
            //
            // I'll just leave it like that, until we had the opportunity to
            // collect some experience with this code.
            // - @hannobraun
            return None;
        }

        let origin = (b_normal * a_distance - a_normal * b_distance)
            .cross(&direction)
            / denom;
        let origin = Point { coords: origin };

        let line = Line::from_origin_and_direction(origin, direction);

        let curves = surfaces_and_planes.map(|(surface, plane)| {
            let path = SurfacePath::Line(plane.project_line(&line));
            let global_form = GlobalCurve.insert(objects);

            Curve::new(surface, path, global_form).insert(objects)
        });

        Some(Self {
            intersection_curves: curves,
        })
    }
}

fn plane_from_surface(surface: &Surface) -> Plane {
    let (line, path) = {
        let line = match surface.geometry().u {
            GlobalPath::Line(line) => line,
            _ => todo!("Only plane-plane intersection is currently supported."),
        };

        (line, surface.geometry().v)
    };

    Plane::from_parametric(line.origin(), line.direction(), path)
}

#[cfg(test)]
mod tests {
    use fj_math::Transform;
    use pretty_assertions::assert_eq;

    use crate::{
        algorithms::transform::TransformObject,
        builder::CurveBuilder,
        insert::Insert,
        partial::{Partial, PartialCurve, PartialObject},
        services::Services,
    };

    use super::SurfaceSurfaceIntersection;

    #[test]
    fn plane_plane() {
        let mut services = Services::new();

        let xy = services.objects.surfaces.xy_plane();
        let xz = services.objects.surfaces.xz_plane();

        // Coincident and parallel planes don't have an intersection curve.
        assert_eq!(
            SurfaceSurfaceIntersection::compute(
                [
                    xy.clone(),
                    xy.clone().transform(
                        &Transform::translation([0., 0., 1.],),
                        &mut services.objects
                    )
                ],
                &mut services.objects
            ),
            None,
        );

        let mut expected_xy = PartialCurve {
            surface: Partial::from(xy.clone()),
            ..Default::default()
        };
        expected_xy.update_as_u_axis();
        let expected_xy = expected_xy
            .build(&mut services.objects)
            .insert(&mut services.objects);
        let mut expected_xz = PartialCurve {
            surface: Partial::from(xz.clone()),
            ..Default::default()
        };
        expected_xz.update_as_u_axis();
        let expected_xz = expected_xz
            .build(&mut services.objects)
            .insert(&mut services.objects);

        assert_eq!(
            SurfaceSurfaceIntersection::compute(
                [xy, xz],
                &mut services.objects
            ),
            Some(SurfaceSurfaceIntersection {
                intersection_curves: [expected_xy, expected_xz],
            })
        );
    }
}