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
use fj_math::Winding;

use crate::{
    objects::{Cycle, Face, Surface},
    storage::Handle,
};

use super::{Validate, ValidationConfig};

impl Validate for Face {
    type Error = FaceValidationError;

    fn validate_with_config(
        &self,
        _: &ValidationConfig,
    ) -> Result<(), Self::Error> {
        FaceValidationError::check_surface_identity(self)?;
        FaceValidationError::check_interior_winding(self)?;
        Ok(())
    }
}

/// [`Face`] validation error
#[derive(Clone, Debug, thiserror::Error)]
pub enum FaceValidationError {
    /// [`Surface`] of an interior [`Cycle`] doesn't match [`Face`]'s `Surface`
    #[error(
        "`Surface` of an interior `Cycle` doesn't match `Face`'s `Surface`\n\
        - `Surface` of the `Face`: {surface:#?}\n\
        - Invalid interior `Cycle`: {interior:#?}\n\
        - `Face`: {face:#?}"
    )]
    SurfaceMismatch {
        /// The surface of the [`Face`]
        surface: Handle<Surface>,

        /// The invalid interior cycle of the [`Face`]
        interior: Handle<Cycle>,

        /// The face
        face: Face,
    },

    /// Interior of [`Face`] has invalid winding; must be opposite of exterior
    #[error(
        "Interior of `Face` has invalid winding; must be opposite of exterior\n\
        - Winding of exterior cycle: {exterior_winding:#?}\n\
        - Winding of interior cycle: {interior_winding:#?}\n\
        - `Face`: {face:#?}"
    )]
    InvalidInteriorWinding {
        /// The winding of the [`Face`]'s exterior cycle
        exterior_winding: Winding,

        /// The winding of the invalid interior cycle
        interior_winding: Winding,

        /// The face
        face: Face,
    },
}

impl FaceValidationError {
    fn check_surface_identity(face: &Face) -> Result<(), Self> {
        let surface = face.surface();

        for interior in face.interiors() {
            if surface.id() != interior.surface().id() {
                return Err(Self::SurfaceMismatch {
                    surface: surface.clone(),
                    interior: interior.clone(),
                    face: face.clone(),
                });
            }
        }

        Ok(())
    }

    fn check_interior_winding(face: &Face) -> Result<(), Self> {
        let exterior_winding = face.exterior().winding();

        for interior in face.interiors() {
            let interior_winding = interior.winding();

            if exterior_winding == interior_winding {
                return Err(Self::InvalidInteriorWinding {
                    exterior_winding,
                    interior_winding,
                    face: face.clone(),
                });
            }
            assert_ne!(
                exterior_winding,
                interior.winding(),
                "Interior cycles must have opposite winding of exterior cycle"
            );
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        algorithms::reverse::Reverse,
        builder::{CycleBuilder, FaceBuilder},
        insert::Insert,
        objects::Face,
        partial::{PartialCycle, PartialFace, PartialObject},
        services::Services,
        validate::Validate,
    };

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

        let surface = services.objects.surfaces.xy_plane();

        let valid = {
            let mut face = PartialFace::default();
            face.update_exterior_as_polygon(
                surface.clone(),
                [[0., 0.], [3., 0.], [0., 3.]],
            );
            face.add_interior_polygon(surface, [[1., 1.], [1., 2.], [2., 1.]]);

            face.build(&mut services.objects)
        };
        let invalid = {
            let mut cycle = PartialCycle::default();
            cycle.update_as_polygon_from_points(
                services.objects.surfaces.xz_plane(),
                [[1., 1.], [1., 2.], [2., 1.]],
            );
            let cycle = cycle
                .build(&mut services.objects)
                .insert(&mut services.objects);

            let interiors = [cycle];
            Face::new(valid.exterior().clone(), interiors, valid.color())
        };

        assert!(valid.validate().is_ok());
        assert!(invalid.validate().is_err());
    }

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

        let surface = services.objects.surfaces.xy_plane();

        let valid = {
            let mut face = PartialFace::default();
            face.update_exterior_as_polygon(
                surface.clone(),
                [[0., 0.], [3., 0.], [0., 3.]],
            );
            face.add_interior_polygon(surface, [[1., 1.], [1., 2.], [2., 1.]]);
            face.build(&mut services.objects)
        };
        let invalid = {
            let interiors = valid
                .interiors()
                .cloned()
                .map(|cycle| cycle.reverse(&mut services.objects))
                .collect::<Vec<_>>();

            Face::new(valid.exterior().clone(), interiors, valid.color())
        };

        assert!(valid.validate().is_ok());
        assert!(invalid.validate().is_err());
    }
}