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

use crate::{
    objects::{Curve, GlobalCurve, Surface},
    path::SurfacePath,
    stores::{Handle, HandleWrapper, Stores},
};

/// A partial [`Curve`]
///
/// See [`crate::partial`] for more information.
#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PartialCurve {
    /// The path that defines the [`Curve`]
    ///
    /// Must be provided before calling [`PartialCurve::build`].
    pub path: Option<SurfacePath>,

    /// The surface that the [`Curve`] is defined in
    ///
    /// Must be provided before calling [`PartialCurve::build`].
    pub surface: Option<Handle<Surface>>,

    /// The global form of the [`Curve`]
    ///
    /// Will be computed from `path` and `surface` in [`PartialCurve::build`],
    /// if not provided.
    pub global_form: Option<HandleWrapper<GlobalCurve>>,
}

impl PartialCurve {
    /// Provide a path for the partial curve
    pub fn with_path(mut self, path: SurfacePath) -> Self {
        self.path = Some(path);
        self
    }

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

    /// Provide a global form for the partial curve
    pub fn with_global_form(
        mut self,
        global_form: impl Into<HandleWrapper<GlobalCurve>>,
    ) -> Self {
        self.global_form = Some(global_form.into());
        self
    }

    /// Update partial curve to represent the u-axis
    pub fn as_u_axis(self) -> Self {
        let a = Point::origin();
        let b = a + Vector::unit_u();

        self.as_line_from_points([a, b])
    }

    /// Update partial curve to represent the v-axis
    pub fn as_v_axis(self) -> Self {
        let a = Point::origin();
        let b = a + Vector::unit_v();

        self.as_line_from_points([a, b])
    }

    /// Update partial curve as a circle, from the provided radius
    pub fn as_circle_from_radius(self, radius: impl Into<Scalar>) -> Self {
        self.with_path(SurfacePath::circle_from_radius(radius))
    }

    /// Update partial curve as a line, from the provided points
    pub fn as_line_from_points(self, points: [impl Into<Point<2>>; 2]) -> Self {
        self.with_path(SurfacePath::line_from_points(points))
    }

    /// Build a full [`Curve`] from the partial curve
    pub fn build(self, stores: &Stores) -> Curve {
        let path = self.path.expect("Can't build `Curve` without path");
        let surface =
            self.surface.expect("Can't build `Curve` without surface");

        let global_form = self
            .global_form
            .unwrap_or_else(|| GlobalCurve::new(stores).into());

        Curve::new(surface, path, global_form)
    }
}

impl From<&Curve> for PartialCurve {
    fn from(curve: &Curve) -> Self {
        Self {
            path: Some(curve.path()),
            surface: Some(curve.surface().clone()),
            global_form: Some(curve.global_form().clone().into()),
        }
    }
}