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

use crate::{
    builder::GlobalVertexBuilder,
    objects::{Curve, GlobalVertex, Objects, Surface, SurfaceVertex, Vertex},
    partial::{MaybePartial, MergeWith, Replace},
    services::Service,
    storage::Handle,
};

/// A partial [`Vertex`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default)]
pub struct PartialVertex {
    /// The position of the [`Vertex`]
    pub position: Option<Point<1>>,

    /// The curve that the [`Vertex`] is defined in
    pub curve: MaybePartial<Curve>,

    /// The surface form of the [`Vertex`]
    pub surface_form: MaybePartial<SurfaceVertex>,
}

impl PartialVertex {
    /// 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: &mut Service<Objects>) -> Vertex {
        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(|mut partial| {
                let position = partial.position.unwrap_or_else(|| {
                    curve.path().point_from_path_coords(position)
                });

                partial.position = Some(position);
                partial.surface = Some(curve.surface().clone());

                partial
            })
            .into_full(objects);

        Vertex::new(position, curve, surface_form)
    }
}

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

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

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

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)]
pub struct PartialSurfaceVertex {
    /// The position of the [`SurfaceVertex`]
    pub position: Option<Point<2>>,

    /// The surface that the [`SurfaceVertex`] is defined in
    pub surface: Option<Handle<Surface>>,

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

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

        if self.global_form.position().is_none() {
            self.global_form = self.global_form.merge_with(
                PartialGlobalVertex::from_surface_and_position(
                    &surface.geometry(),
                    position,
                ),
            )
        }
        let global_form = self.global_form.into_full(objects);

        SurfaceVertex::new(position, surface, global_form)
    }
}

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

        Self {
            position: self.position.merge_with(other.position),
            surface: self.surface.merge_with(other.surface),
            global_form: self.global_form.merge_with(other.global_form),
        }
    }
}

impl Replace<Surface> for PartialSurfaceVertex {
    fn replace(&mut self, surface: Handle<Surface>) -> &mut Self {
        self.surface = Some(surface);
        self
    }
}

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 {
    /// The position of the [`GlobalVertex`]
    pub position: Option<Point<3>>,
}

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

        GlobalVertex::new(position)
    }
}

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

        Self {
            position: self.position.merge_with(other.position),
        }
    }
}

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