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
use std::{array, collections::VecDeque};

use fj_interop::ext::ArrayExt;

use crate::{
    geometry::path::SurfacePath,
    objects::{Cycle, Surface},
    partial::{MaybeSurfacePath, Partial, PartialCycle, PartialFace},
};

use super::SurfaceBuilder;

/// Builder API for [`PartialFace`]
pub trait FaceBuilder {
    /// Add an interior cycle
    fn add_interior(&mut self) -> Partial<Cycle>;

    /// Update the face's surface as a plane
    ///
    /// The plane geometry is inferred from three of the face's vertices. Also
    /// infers any undefined `SurfaceVertex` positions.
    ///
    /// # Panics
    ///
    /// Assumes that the face exterior has exactly three vertices to use. Panics
    /// otherwise. This is a temporary limitation, not a fundamental one. It
    /// could be overcome with some more work.
    fn update_surface_as_plane(&mut self) -> Partial<Surface>;

    /// Infer any undefined curves in the face
    fn infer_curves(&mut self);
}

impl FaceBuilder for PartialFace {
    fn add_interior(&mut self) -> Partial<Cycle> {
        let cycle = Partial::from_partial(PartialCycle {
            surface: self.exterior.read().surface.clone(),
            ..Default::default()
        });
        self.interiors.push(cycle.clone());
        cycle
    }

    fn update_surface_as_plane(&mut self) -> Partial<Surface> {
        let mut exterior = self.exterior.write();
        let mut vertices = exterior
            .half_edges
            .iter()
            .map(|half_edge| {
                let [(_, surface_vertex), _] = &half_edge.read().vertices;
                let global_position = surface_vertex
                    .read()
                    .global_form
                    .read()
                    .position
                    .expect("Need global position to infer plane");

                (surface_vertex.clone(), global_position)
            })
            .collect::<VecDeque<_>>();

        let (significant_vertices, surface) = {
            let first_three_vertices = array::from_fn(|_| {
                vertices
                    .pop_front()
                    .expect("Expected at least three vertices")
            });

            let first_three_points_global =
                first_three_vertices.each_ref_ext().map(|(_, point)| *point);

            let (first_three_points_surface, surface) = exterior
                .surface
                .write()
                .update_as_plane_from_points(first_three_points_global);

            let first_three_vertices = first_three_vertices
                .zip_ext(first_three_points_surface)
                .map(|((vertex, _), point_global)| (vertex, point_global));

            (first_three_vertices, surface)
        };
        let rest_of_vertices =
            vertices.into_iter().map(|(vertex, point_global)| {
                let point_surface = surface.project_global_point(point_global);
                (vertex, point_surface)
            });

        for (mut surface_vertex, point) in
            significant_vertices.into_iter().chain(rest_of_vertices)
        {
            surface_vertex.write().position = Some(point);
        }

        exterior.surface.clone()
    }

    fn infer_curves(&mut self) {
        for half_edge in &mut self.exterior.write().half_edges {
            let mut half_edge = half_edge.write();

            let mut curve = half_edge.curve.clone();
            let mut curve = curve.write();

            if let Some(path) = &mut curve.path {
                match path {
                    MaybeSurfacePath::Defined(_) => {
                        // Path is already defined. Nothing to infer.
                    }
                    MaybeSurfacePath::UndefinedCircle { .. } => todo!(
                        "Inferring undefined circles is not supported yet"
                    ),
                    MaybeSurfacePath::UndefinedLine => {
                        let points_surface =
                            half_edge.vertices.each_ref_ext().map(|vertex| {
                                vertex.1.read().position.expect(
                                    "Can't infer curve without surface points",
                                )
                            });
                        let (line, points_curve) =
                            SurfacePath::line_from_points(points_surface);

                        *path = MaybeSurfacePath::Defined(line);
                        for (vertex, point) in half_edge
                            .vertices
                            .each_mut_ext()
                            .zip_ext(points_curve)
                        {
                            vertex.0 = Some(point);
                        }
                    }
                }
            }
        }
    }
}