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
use fj_interop::ext::ArrayExt;

use crate::{
    builder::GlobalEdgeBuilder,
    objects::{
        Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Objects,
        Surface, Vertex,
    },
    partial::{MaybePartial, MergeWith, PartialCurve, PartialVertex, Replace},
    storage::Handle,
    validate::ValidationError,
};

/// A partial [`HalfEdge`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default)]
pub struct PartialHalfEdge {
    /// The curve that the [`HalfEdge`] is defined in
    pub curve: MaybePartial<Curve>,

    /// The vertices that bound the [`HalfEdge`] in the curve
    pub vertices: [MaybePartial<Vertex>; 2],

    /// The global form of the [`HalfEdge`]
    pub global_form: MaybePartial<GlobalEdge>,
}

impl PartialHalfEdge {
    /// Build a full [`HalfEdge`] from the partial half-edge
    pub fn build(
        mut self,
        objects: &Objects,
    ) -> Result<HalfEdge, ValidationError> {
        let global_curve = self
            .curve
            .global_form()
            .merge_with(self.global_form.curve());

        let curve = {
            self.curve = self.curve.merge_with(PartialCurve {
                global_form: global_curve,
                ..Default::default()
            });

            self.curve.into_full(objects)?
        };
        let vertices = self.vertices.try_map_ext(|vertex| {
            vertex
                .merge_with(PartialVertex {
                    curve: curve.clone().into(),
                    ..Default::default()
                })
                .into_full(objects)
        })?;

        let global_form = self
            .global_form
            .update_partial(|partial| {
                partial.update_from_curve_and_vertices(&curve, &vertices)
            })
            .into_full(objects)?;

        Ok(HalfEdge::new(vertices, global_form))
    }
}

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

        Self {
            curve: self.curve.merge_with(other.curve),
            vertices: self.vertices.merge_with(other.vertices),
            global_form: self.global_form.merge_with(other.global_form),
        }
    }
}

impl Replace<Surface> for PartialHalfEdge {
    fn replace(&mut self, surface: Handle<Surface>) -> &mut Self {
        self.curve.replace(surface.clone());

        for vertex in &mut self.vertices {
            vertex.replace(surface.clone());
        }

        self
    }
}

impl From<&HalfEdge> for PartialHalfEdge {
    fn from(half_edge: &HalfEdge) -> Self {
        let [back_vertex, front_vertex] =
            half_edge.vertices().clone().map(Into::into);

        Self {
            curve: half_edge.curve().clone().into(),
            vertices: [back_vertex, front_vertex],
            global_form: half_edge.global_form().clone().into(),
        }
    }
}

/// A partial [`GlobalEdge`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default)]
pub struct PartialGlobalEdge {
    /// The curve that the [`GlobalEdge`] is defined in
    pub curve: MaybePartial<GlobalCurve>,

    /// The vertices that bound the [`GlobalEdge`] in the curve
    pub vertices: [MaybePartial<GlobalVertex>; 2],
}

impl PartialGlobalEdge {
    /// Build a full [`GlobalEdge`] from the partial global edge
    pub fn build(
        self,
        objects: &Objects,
    ) -> Result<GlobalEdge, ValidationError> {
        let curve = self.curve.into_full(objects)?;
        let vertices = self
            .vertices
            .try_map_ext(|global_vertex| global_vertex.into_full(objects))?;

        Ok(GlobalEdge::new(curve, vertices))
    }
}

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

        Self {
            curve: self.curve.merge_with(other.curve),
            vertices: self.vertices.merge_with(other.vertices),
        }
    }
}

impl From<&GlobalEdge> for PartialGlobalEdge {
    fn from(global_edge: &GlobalEdge) -> Self {
        Self {
            curve: global_edge.curve().clone().into(),
            vertices: global_edge
                .vertices()
                .access_in_normalized_order()
                .map(Into::into),
        }
    }
}