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

use crate::{
    builder::GlobalVertexBuilder,
    objects::{Curve, GlobalVertex, Objects, Surface, SurfaceVertex, Vertex},
    partial::{util::merge_options, MaybePartial},
    storage::Handle,
    validate::ValidationError,
};

/// A partial [`Vertex`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default)]
pub struct PartialVertex {
    position: Option<Point<1>>,
    curve: MaybePartial<Curve>,
    surface_form: MaybePartial<SurfaceVertex>,
}

impl PartialVertex {
    /// Access the position of the [`Vertex`] on the curve
    pub fn position(&self) -> Option<Point<1>> {
        self.position
    }

    /// Access the curve that the [`Vertex`] is defined in
    pub fn curve(&self) -> MaybePartial<Curve> {
        self.curve.clone()
    }

    /// Access the surface form of the [`Vertex`]
    pub fn surface_form(&self) -> MaybePartial<SurfaceVertex> {
        self.surface_form.clone()
    }

    /// Provide a position for the partial vertex
    pub fn with_position(
        mut self,
        position: Option<impl Into<Point<1>>>,
    ) -> Self {
        if let Some(position) = position {
            self.position = Some(position.into());
        }
        self
    }

    /// Provide a curve for the partial vertex
    pub fn with_curve(mut self, curve: impl Into<MaybePartial<Curve>>) -> Self {
        self.curve = curve.into();
        self
    }

    /// Provide a surface form for the partial vertex
    pub fn with_surface_form(
        mut self,
        surface_form: impl Into<MaybePartial<SurfaceVertex>>,
    ) -> Self {
        self.surface_form = surface_form.into();
        self
    }

    /// Merge this partial object with another
    pub fn merge_with(self, other: Self) -> Self {
        Self {
            position: merge_options(self.position, other.position),
            curve: self.curve.merge_with(other.curve),
            surface_form: self.surface_form.merge_with(other.surface_form),
        }
    }

    /// Build a full [`Vertex`] from the partial vertex
    ///
    /// # Panics
    ///
    /// Panics, if position has not been provided.
    ///
    /// Panics, if curve has not been provided.
    pub fn build(
        self,
        objects: &Objects,
    ) -> Result<Handle<Vertex>, ValidationError> {
        let position = self
            .position
            .expect("Cant' build `Vertex` without position");
        let curve = self.curve.into_full(objects)?;

        let surface_form = self
            .surface_form
            .update_partial(|partial| {
                let position = partial.position.unwrap_or_else(|| {
                    curve.path().point_from_path_coords(position)
                });

                partial
                    .with_position(Some(position))
                    .with_surface(Some(curve.surface().clone()))
            })
            .into_full(objects)?;

        Ok(objects.vertices.insert(Vertex::new(
            position,
            curve,
            surface_form,
        ))?)
    }
}

impl From<&Vertex> for PartialVertex {
    fn from(vertex: &Vertex) -> Self {
        Self {
            position: Some(vertex.position()),
            curve: vertex.curve().clone().into(),
            surface_form: vertex.surface_form().clone().into(),
        }
    }
}

/// A partial [`SurfaceVertex`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PartialSurfaceVertex {
    position: Option<Point<2>>,
    surface: Option<Handle<Surface>>,
    global_form: MaybePartial<GlobalVertex>,
}

impl PartialSurfaceVertex {
    /// Access the position of the [`SurfaceVertex`]
    pub fn position(&self) -> Option<Point<2>> {
        self.position
    }

    /// Access the surface that the [`SurfaceVertex`] is defined in
    pub fn surface(&self) -> Option<Handle<Surface>> {
        self.surface.clone()
    }

    /// Access the global form of the [`SurfaceVertex`]
    pub fn global_form(&self) -> MaybePartial<GlobalVertex> {
        self.global_form.clone()
    }

    /// Provide a position for the partial surface vertex
    pub fn with_position(
        mut self,
        position: Option<impl Into<Point<2>>>,
    ) -> Self {
        if let Some(position) = position {
            self.position = Some(position.into());
        }
        self
    }

    /// Provide a surface for the partial surface vertex
    pub fn with_surface(mut self, surface: Option<Handle<Surface>>) -> Self {
        if let Some(surface) = surface {
            self.surface = Some(surface);
        }
        self
    }

    /// Provide a global form for the partial surface vertex
    pub fn with_global_form(
        mut self,
        global_form: Option<impl Into<MaybePartial<GlobalVertex>>>,
    ) -> Self {
        if let Some(global_form) = global_form {
            self.global_form = global_form.into();
        }
        self
    }

    /// Merge this partial object with another
    pub fn merge_with(self, other: Self) -> Self {
        Self {
            position: merge_options(self.position, other.position),
            surface: merge_options(self.surface, other.surface),
            global_form: self.global_form.merge_with(other.global_form),
        }
    }

    /// Build a full [`SurfaceVertex`] from the partial surface vertex
    pub fn build(
        self,
        objects: &Objects,
    ) -> Result<Handle<SurfaceVertex>, ValidationError> {
        let position = self
            .position
            .expect("Can't build `SurfaceVertex` without position");
        let surface = self
            .surface
            .expect("Can't build `SurfaceVertex` without `Surface`");

        let global_form = self
            .global_form
            .update_partial(|global_form| {
                global_form.update_from_surface_and_position(&surface, position)
            })
            .into_full(objects)?;

        Ok(objects.surface_vertices.insert(SurfaceVertex::new(
            position,
            surface,
            global_form,
        ))?)
    }
}

impl From<&SurfaceVertex> for PartialSurfaceVertex {
    fn from(surface_vertex: &SurfaceVertex) -> Self {
        Self {
            position: Some(surface_vertex.position()),
            surface: Some(surface_vertex.surface().clone()),
            global_form: (surface_vertex.global_form().clone()).into(),
        }
    }
}

/// A partial [`GlobalVertex`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PartialGlobalVertex {
    position: Option<Point<3>>,
}

impl PartialGlobalVertex {
    /// Access the position of the [`GlobalVertex`]
    pub fn position(&self) -> Option<Point<3>> {
        self.position
    }

    /// Provide a position for the partial global vertex
    pub fn with_position(
        mut self,
        position: Option<impl Into<Point<3>>>,
    ) -> Self {
        if let Some(position) = position {
            self.position = Some(position.into());
        }
        self
    }

    /// Merge this partial object with another
    pub fn merge_with(self, other: Self) -> Self {
        Self {
            position: merge_options(self.position, other.position),
        }
    }

    /// Build a full [`GlobalVertex`] from the partial global vertex
    pub fn build(
        self,
        objects: &Objects,
    ) -> Result<Handle<GlobalVertex>, ValidationError> {
        let position = self
            .position
            .expect("Can't build a `GlobalVertex` without a position");

        Ok(objects
            .global_vertices
            .insert(GlobalVertex::from_position(position))?)
    }
}

impl From<&GlobalVertex> for PartialGlobalVertex {
    fn from(global_vertex: &GlobalVertex) -> Self {
        Self {
            position: Some(global_vertex.position()),
        }
    }
}