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_interop::ext::ArrayExt;
use iter_fixed::IntoIteratorFixed;

use crate::{
    objects::{Curve, Face, Objects},
    storage::Handle,
    validate::ValidationError,
};

use super::{CurveFaceIntersection, SurfaceSurfaceIntersection};

/// An intersection between two faces
#[derive(Clone, Debug, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct FaceFaceIntersection {
    /// The intersection curves
    ///
    /// These curves correspond to the input faces, each being the local
    /// representation of the intersection on the respective face's surface.
    ///
    /// They both represent the same global curve.
    pub intersection_curves: [Handle<Curve>; 2],

    /// The interval of this intersection, in curve coordinates
    ///
    /// These curve coordinates apply to both intersection curves equally.
    pub intersection_intervals: CurveFaceIntersection,
}

impl FaceFaceIntersection {
    /// Compute the intersections between two faces
    pub fn compute(
        faces: [&Face; 2],
        objects: &Objects,
    ) -> Result<Option<Self>, ValidationError> {
        let surfaces = faces.map(|face| face.surface().clone());

        let intersection_curves =
            match SurfaceSurfaceIntersection::compute(surfaces, objects)? {
                Some(intersection) => intersection.intersection_curves,
                None => return Ok(None),
            };

        let curve_face_intersections = intersection_curves
            .each_ref_ext()
            .into_iter_fixed()
            .zip(faces)
            .map(|(curve, face)| CurveFaceIntersection::compute(curve, face))
            .collect::<[_; 2]>();

        let intersection_intervals = {
            let [a, b] = curve_face_intersections;
            a.merge(&b)
        };

        if intersection_intervals.is_empty() {
            return Ok(None);
        }

        Ok(Some(Self {
            intersection_curves,
            intersection_intervals,
        }))
    }
}

#[cfg(test)]
mod tests {
    use fj_interop::ext::ArrayExt;
    use pretty_assertions::assert_eq;

    use crate::{
        algorithms::intersect::CurveFaceIntersection,
        builder::{CurveBuilder, FaceBuilder},
        insert::Insert,
        objects::{Face, Objects},
        partial::{HasPartial, PartialCurve},
        validate::ValidationError,
    };

    use super::FaceFaceIntersection;

    #[test]
    fn compute_no_intersection() -> anyhow::Result<()> {
        let objects = Objects::new();

        #[rustfmt::skip]
        let points = [
            [1., 1.],
            [2., 1.],
            [2., 2.],
            [1., 2.],
        ];
        let [a, b] = [objects.surfaces.xy_plane(), objects.surfaces.xz_plane()]
            .try_map_ext(|surface| {
                Face::partial()
                    .with_surface(surface)
                    .with_exterior_polygon_from_points(points)
                    .build(&objects)
            })?;

        let intersection = FaceFaceIntersection::compute([&a, &b], &objects)?;

        assert!(intersection.is_none());

        Ok(())
    }

    #[test]
    fn compute_one_intersection() -> anyhow::Result<()> {
        let objects = Objects::new();

        #[rustfmt::skip]
        let points = [
            [-1., -1.],
            [ 1., -1.],
            [ 1.,  1.],
            [-1.,  1.],
        ];
        let surfaces =
            [objects.surfaces.xy_plane(), objects.surfaces.xz_plane()];
        let [a, b] = surfaces.clone().try_map_ext(|surface| {
            Face::partial()
                .with_surface(surface)
                .with_exterior_polygon_from_points(points)
                .build(&objects)
        })?;

        let intersection = FaceFaceIntersection::compute([&a, &b], &objects)?;

        let expected_curves =
            surfaces.try_map_ext(|surface| -> Result<_, ValidationError> {
                let mut curve = PartialCurve {
                    surface: Some(surface),
                    ..Default::default()
                };
                curve.update_as_line_from_points([[0., 0.], [1., 0.]]);
                Ok(curve.build(&objects)?.insert(&objects)?)
            })?;
        let expected_intervals =
            CurveFaceIntersection::from_intervals([[[-1.], [1.]]]);
        assert_eq!(
            intersection,
            Some(FaceFaceIntersection {
                intersection_curves: expected_curves,
                intersection_intervals: expected_intervals
            })
        );
        Ok(())
    }
}