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
use fj_interop::mesh::Color;

use crate::{
    objects::{Cycle, Face, Objects, Surface},
    partial::{MaybePartial, MergeWith, Mergeable},
    services::Service,
    storage::Handle,
};

/// A partial [`Face`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default)]
pub struct PartialFace {
    exterior: MaybePartial<Cycle>,
    interiors: Vec<MaybePartial<Cycle>>,
    color: Option<Color>,
}

impl PartialFace {
    /// Access th surface that the [`Face`] is defined in
    pub fn surface(&self) -> Option<Handle<Surface>> {
        self.exterior.surface()
    }

    /// Access the [`Face`]'s exterior cycle
    pub fn exterior(&self) -> MaybePartial<Cycle> {
        self.exterior.clone()
    }

    /// Access the [`Face`]'s interior cycles
    pub fn interiors(&self) -> impl Iterator<Item = MaybePartial<Cycle>> + '_ {
        self.interiors.iter().cloned()
    }

    /// Access the color of the [`Face`]
    pub fn color(&self) -> Option<Color> {
        self.color
    }

    /// Build the [`Face`] with the provided exterior
    pub fn with_exterior(
        mut self,
        exterior: impl Into<MaybePartial<Cycle>>,
    ) -> Self {
        self.exterior = exterior.into();
        self
    }

    /// Build the [`Face`] with the provided interior polygons
    pub fn with_interiors(
        mut self,
        interiors: impl IntoIterator<Item = impl Into<MaybePartial<Cycle>>>,
    ) -> Self {
        let interiors = interiors.into_iter().map(Into::into);
        self.interiors.extend(interiors);
        self
    }

    /// Build the [`Face`] with the provided color
    pub fn with_color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    /// Construct a polygon from a list of points
    pub fn build(self, objects: &mut Service<Objects>) -> Face {
        let exterior = self.exterior.into_full(objects);
        let interiors = self
            .interiors
            .into_iter()
            .map(|cycle| cycle.into_full(objects))
            .collect::<Vec<_>>();
        let color = self.color.unwrap_or_default();

        Face::new(exterior, interiors, color)
    }
}

impl MergeWith for PartialFace {
    fn merge_with(self, other: impl Into<Self>) -> Self {
        let other = other.into();

        Self {
            exterior: self.exterior.merge_with(other.exterior),
            interiors: Mergeable(self.interiors)
                .merge_with(Mergeable(other.interiors))
                .0,
            color: self.color.merge_with(other.color),
        }
    }
}

impl From<&Face> for PartialFace {
    fn from(face: &Face) -> Self {
        Self {
            exterior: face.exterior().clone().into(),
            interiors: face.interiors().cloned().map(Into::into).collect(),
            color: Some(face.color()),
        }
    }
}