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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::collections::HashSet;

use fj_math::{Point, Scalar};

use crate::{
    geometry::{Curve, Surface},
    topology::{Cycle, Edge, Face, Vertex},
};

use super::{stores::Stores, Handle};

pub trait Validate {
    fn validate(
        &self,
        min_distance: Scalar,
        stores: &Stores,
    ) -> Result<(), ValidationError>;
}

impl Validate for Point<3> {
    fn validate(&self, _: Scalar, _: &Stores) -> Result<(), ValidationError> {
        Ok(())
    }
}

impl Validate for Curve {
    fn validate(&self, _: Scalar, _: &Stores) -> Result<(), ValidationError> {
        Ok(())
    }
}

impl Validate for Surface {
    fn validate(&self, _: Scalar, _: &Stores) -> Result<(), ValidationError> {
        Ok(())
    }
}

impl Validate for Vertex {
    /// Validate the vertex
    ///
    /// # Implementation note
    ///
    /// In the future, this method is likely to validate more than it already
    /// does. See documentation of [`crate::kernel`] for some context on that.
    fn validate(
        &self,
        min_distance: Scalar,
        stores: &Stores,
    ) -> Result<(), ValidationError> {
        if !stores.points.contains(&self.point) {
            return Err(StructuralIssues::default().into());
        }
        for existing in stores.vertices.iter() {
            let distance = (existing.get().point() - self.point()).magnitude();

            if distance < min_distance {
                return Err(ValidationError::Uniqueness);
            }
        }

        Ok(())
    }
}

impl Validate for Edge {
    fn validate(
        &self,
        _: Scalar,
        stores: &Stores,
    ) -> Result<(), ValidationError> {
        let mut missing_curve = None;
        let mut missing_vertices = HashSet::new();

        if !stores.curves.contains(&self.curve) {
            missing_curve = Some(self.curve.clone());
        }
        for vertices in &self.vertices {
            for vertex in vertices {
                if !stores.vertices.contains(vertex) {
                    missing_vertices.insert(vertex.clone());
                }
            }
        }

        if missing_curve.is_some() || !missing_vertices.is_empty() {
            return Err(StructuralIssues {
                missing_curve,
                missing_vertices,
                ..StructuralIssues::default()
            }
            .into());
        }

        Ok(())
    }
}

impl Validate for Cycle {
    /// Validate the cycle
    ///
    /// # Implementation note
    ///
    /// The validation of the cycle should be extended to cover more cases:
    /// - That those edges form a cycle.
    /// - That the cycle is not self-overlapping.
    /// - That there exists no duplicate cycle, with the same edges.
    fn validate(
        &self,
        _: Scalar,
        stores: &Stores,
    ) -> Result<(), ValidationError> {
        let mut missing_edges = HashSet::new();
        for edge in &self.edges {
            if !stores.edges.contains(edge) {
                missing_edges.insert(edge.clone());
            }
        }

        if !missing_edges.is_empty() {
            return Err(StructuralIssues {
                missing_edges,
                ..StructuralIssues::default()
            }
            .into());
        }

        Ok(())
    }
}

impl Validate for Face {
    fn validate(
        &self,
        _: Scalar,
        stores: &Stores,
    ) -> Result<(), ValidationError> {
        if let Face::Face {
            surface,
            exteriors,
            interiors,
            ..
        } = self
        {
            let mut missing_surface = None;
            let mut missing_cycles = HashSet::new();

            if !stores.surfaces.contains(surface) {
                missing_surface = Some(surface.clone());
            }
            for cycle in exteriors.iter().chain(interiors) {
                if !stores.cycles.contains(cycle) {
                    missing_cycles.insert(cycle.clone());
                }
            }

            if missing_surface.is_some() || !missing_cycles.is_empty() {
                return Err(StructuralIssues {
                    missing_surface,
                    missing_cycles,
                    ..StructuralIssues::default()
                }
                .into());
            }
        }

        Ok(())
    }
}

/// Returned by the various `add_` methods of the [`Shape`] API
pub type ValidationResult<T> = Result<Handle<T>, ValidationError>;

/// An error that can occur during a validation
#[derive(Debug, thiserror::Error)]
pub enum ValidationError {
    /// Structural validation failed
    ///
    /// Structural validation verifies, that all the object that an object
    /// refers to are already part of the shape.
    #[error("Structural validation failed")]
    Structural(StructuralIssues),

    /// Uniqueness validation failed
    ///
    /// Uniqueness validation checks, that an object is unique. Uniqueness is
    /// only required for topological objects, as there's no harm in geometric
    /// objects being duplicated.
    #[error("Uniqueness validation failed")]
    #[allow(unused)]
    Uniqueness,

    /// Geometric validation failed
    ///
    /// Geometric validation checks, that various geometric constraints of an
    /// object are upheld. For example, edges or faces might not be allowed to
    /// intersect.
    #[error("Geometric validation failed")]
    #[allow(unused)]
    Geometric,
}

impl ValidationError {
    /// Indicate whether validation found a missing curve
    #[cfg(test)]
    pub fn missing_curve(&self, curve: &Handle<Curve>) -> bool {
        if let Self::Structural(StructuralIssues { missing_curve, .. }) = self {
            return missing_curve.as_ref() == Some(curve);
        }

        false
    }

    /// Indicate whether validation found a missing vertex
    #[cfg(test)]
    pub fn missing_vertex(&self, vertex: &Handle<Vertex>) -> bool {
        if let Self::Structural(StructuralIssues {
            missing_vertices, ..
        }) = self
        {
            return missing_vertices.contains(vertex);
        }

        false
    }

    /// Indicate whether validation found a missing edge
    #[cfg(test)]
    pub fn missing_edge(&self, edge: &Handle<Edge>) -> bool {
        if let Self::Structural(StructuralIssues { missing_edges, .. }) = self {
            return missing_edges.contains(edge);
        }

        false
    }

    /// Indicate whether validation found a missing surface
    #[cfg(test)]
    pub fn missing_surface(&self, surface: &Handle<Surface>) -> bool {
        if let Self::Structural(StructuralIssues {
            missing_surface, ..
        }) = self
        {
            return missing_surface.as_ref() == Some(surface);
        }

        false
    }

    /// Indicate whether validation found a missing cycle
    #[cfg(test)]
    pub fn missing_cycle(&self, cycle: &Handle<Cycle>) -> bool {
        if let Self::Structural(StructuralIssues { missing_cycles, .. }) = self
        {
            return missing_cycles.contains(cycle);
        }

        false
    }
}

impl From<StructuralIssues> for ValidationError {
    fn from(issues: StructuralIssues) -> Self {
        Self::Structural(issues)
    }
}

/// Structural issues found during validation
///
/// Used by [`ValidationError`].
#[derive(Debug, Default)]
pub struct StructuralIssues {
    /// Missing curve found in edge validation
    pub missing_curve: Option<Handle<Curve>>,

    /// Missing vertices found in edge validation
    pub missing_vertices: HashSet<Handle<Vertex>>,

    /// Missing edges found in cycle validation
    pub missing_edges: HashSet<Handle<Edge>>,

    /// Missing surface found in face validation
    pub missing_surface: Option<Handle<Surface>>,

    /// Missing cycles found in face validation
    pub missing_cycles: HashSet<Handle<Cycle>>,
}