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

use crate::{
    objects::HalfEdge,
    partial::{Partial, PartialCycle, PartialFace},
};

use super::CycleBuilder;

/// Builder API for [`PartialFace`]
pub trait FaceBuilder {
    /// Update the face exterior as a polygon from the provided points
    fn update_exterior_as_polygon_from_points(
        &mut self,
        points: impl IntoIterator<Item = impl Into<Point<2>>>,
    ) -> Vec<Partial<HalfEdge>>;

    /// Update the face exterior as a polygon
    fn update_exterior_as_polygon(&mut self);

    /// Update the face exterior as a triangle, from 3D points
    ///
    /// Uses the three points to infer a plane that is used as the surface.
    ///
    /// # Implementation Note
    ///
    /// This method is probably just temporary, and will be generalized into a
    /// "update as polygon from global points" method sooner or later. For now,
    /// I didn't want to deal with the question of how to infer the surface, and
    /// how to handle points that don't fit that surface.
    fn update_exterior_as_triangle_from_global_points(
        &mut self,
        points: [impl Into<Point<3>>; 3],
    ) -> [Partial<HalfEdge>; 3];

    /// Add an interior polygon, from the provided points
    fn add_interior_polygon_from_points(
        &mut self,
        points: impl IntoIterator<Item = impl Into<Point<2>>>,
    );
}

impl FaceBuilder for PartialFace {
    fn update_exterior_as_polygon_from_points(
        &mut self,
        points: impl IntoIterator<Item = impl Into<Point<2>>>,
    ) -> Vec<Partial<HalfEdge>> {
        self.exterior.write().update_as_polygon_from_points(points)
    }

    fn update_exterior_as_polygon(&mut self) {
        self.exterior.write().update_as_polygon()
    }

    fn update_exterior_as_triangle_from_global_points(
        &mut self,
        points_global: [impl Into<Point<3>>; 3],
    ) -> [Partial<HalfEdge>; 3] {
        self.exterior
            .write()
            .update_as_triangle_from_global_points(points_global)
    }

    fn add_interior_polygon_from_points(
        &mut self,
        points: impl IntoIterator<Item = impl Into<Point<2>>>,
    ) {
        let mut cycle = PartialCycle {
            surface: self.exterior.read().surface.clone(),
            ..Default::default()
        };
        cycle.update_as_polygon_from_points(points);

        self.interiors.push(Partial::from_partial(cycle));
    }
}