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
use fj_math::{Point, Scalar};

use crate::{
    objects::{
        Curve, GlobalCurve, GlobalEdge, GlobalVertex, HalfEdge, Surface, Vertex,
    },
    stores::{Handle, Stores},
};

use super::MaybePartial;

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

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

    /// The global form of the [`HalfEdge`]
    ///
    /// Can be computed by [`PartialHalfEdge::build`], if not available.
    pub global_form: Option<GlobalEdge>,
}

impl PartialHalfEdge {
    /// Update the partial half-edge with the given curve
    pub fn with_curve(mut self, curve: impl Into<MaybePartial<Curve>>) -> Self {
        self.curve = Some(curve.into());
        self
    }

    /// Update the partial half-edge with the given vertices
    pub fn with_vertices(
        mut self,
        vertices: [impl Into<MaybePartial<Vertex>>; 2],
    ) -> Self {
        self.vertices = vertices.map(Into::into).map(Some);
        self
    }

    /// Update the partial half-edge, starting it from the given vertex
    pub fn with_from_vertex(
        mut self,
        vertex: impl Into<MaybePartial<Vertex>>,
    ) -> Self {
        self.vertices[0] = Some(vertex.into());
        self
    }

    /// Update the partial half-edge with the given end vertex
    pub fn with_to_vertex(
        mut self,
        vertex: impl Into<MaybePartial<Vertex>>,
    ) -> Self {
        self.vertices[1] = Some(vertex.into());
        self
    }

    /// Update the partial half-edge with the given global form
    pub fn with_global_form(mut self, global_form: GlobalEdge) -> Self {
        self.global_form = Some(global_form);
        self
    }

    /// Update partial half-edge as a circle, from the given radius
    pub fn as_circle_from_radius(
        mut self,
        surface: Surface,
        radius: impl Into<Scalar>,
    ) -> Self {
        let curve = Curve::partial()
            .with_surface(surface)
            .as_circle_from_radius(radius);

        let vertices = {
            let [a_curve, b_curve] =
                [Scalar::ZERO, Scalar::TAU].map(|coord| Point::from([coord]));

            let global_vertex = GlobalVertex::partial()
                .from_curve_and_position(curve.clone(), a_curve);

            [a_curve, b_curve].map(|point_curve| {
                Vertex::partial()
                    .with_position(point_curve)
                    .with_curve(curve.clone())
                    .with_global_form(global_vertex.clone())
            })
        };

        self.curve = Some(curve.into());
        self.vertices = vertices.map(Into::into).map(Some);

        self
    }

    /// Update partial half-edge as a line segment, from the given points
    pub fn as_line_segment_from_points(
        mut self,
        surface: Surface,
        points: [impl Into<Point<2>>; 2],
    ) -> Self {
        let curve = Curve::partial()
            .with_surface(surface)
            .as_line_from_points(points);

        let vertices = [0., 1.].map(|position| {
            Vertex::partial()
                .with_position([position])
                .with_curve(curve.clone())
        });

        self.curve = Some(curve.into());
        self.vertices = vertices.map(Into::into).map(Some);

        self
    }

    /// Build a full [`HalfEdge`] from the partial half-edge
    pub fn build(self, stores: &Stores) -> HalfEdge {
        let curve = self
            .curve
            .expect("Can't build `HalfEdge` without curve")
            .into_full(stores);
        let vertices = self.vertices.map(|vertex| {
            vertex
                .expect("Can't build `HalfEdge` without vertices")
                .into_full(stores)
        });

        let global_form = self.global_form.unwrap_or_else(|| {
            GlobalEdge::partial()
                .from_curve_and_vertices(&curve, &vertices)
                .build(stores)
        });

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

impl From<HalfEdge> for PartialHalfEdge {
    fn from(half_edge: HalfEdge) -> Self {
        Self {
            curve: Some(half_edge.curve().clone().into()),
            vertices: half_edge.vertices().clone().map(Into::into).map(Some),
            global_form: Some(half_edge.global_form().clone()),
        }
    }
}

/// A partial [`GlobalEdge`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PartialGlobalEdge {
    /// The curve that the [`GlobalEdge`] is defined in
    ///
    /// Must be provided before [`PartialGlobalEdge::build`] is called.
    pub curve: Option<Handle<GlobalCurve>>,

    /// The vertices that bound the [`GlobalEdge`] in the curve
    ///
    /// Must be provided before [`PartialGlobalEdge::build`] is called.
    pub vertices: Option<[GlobalVertex; 2]>,
}

impl PartialGlobalEdge {
    /// Update partial global edge from the given curve and vertices
    pub fn from_curve_and_vertices(
        mut self,
        curve: &Curve,
        vertices: &[Vertex; 2],
    ) -> Self {
        self.curve = Some(curve.global_form().clone());
        self.vertices =
            Some(vertices.clone().map(|vertex| *vertex.global_form()));

        self
    }

    /// Build a full [`GlobalEdge`] from the partial global edge
    pub fn build(self, _: &Stores) -> GlobalEdge {
        let curve = self
            .curve
            .expect("Can't build `GlobalEdge` without `GlobalCurve`");
        let vertices = self
            .vertices
            .expect("Can't build `GlobalEdge` without vertices");

        GlobalEdge::new(curve, vertices)
    }
}

impl From<GlobalEdge> for PartialGlobalEdge {
    fn from(global_edge: GlobalEdge) -> Self {
        Self {
            curve: Some(global_edge.curve().clone()),
            vertices: Some(*global_edge.vertices()),
        }
    }
}