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

use crate::{
    local::Local,
    objects::{Curve, Edge, GlobalVertex, Surface, Vertex, VerticesOfEdge},
};

/// API for building an [`Edge`]
pub struct EdgeBuilder;

impl EdgeBuilder {
    /// Create a circle from the given radius
    pub fn circle_from_radius(&self, radius: Scalar) -> Edge {
        let curve_local = Curve::Circle(Circle {
            center: Point::origin(),
            a: Vector::from([radius, Scalar::ZERO]),
            b: Vector::from([Scalar::ZERO, radius]),
        });
        let curve_canonical = Curve::Circle(Circle {
            center: Point::origin(),
            a: Vector::from([radius, Scalar::ZERO, Scalar::ZERO]),
            b: Vector::from([Scalar::ZERO, radius, Scalar::ZERO]),
        });

        Edge::new(
            Local::new(curve_local, curve_canonical),
            VerticesOfEdge::none(),
        )
    }

    /// Create a line segment from two points
    pub fn line_segment_from_points(
        &self,
        surface: &Surface,
        points: [impl Into<Point<2>>; 2],
    ) -> Edge {
        let points = points.map(Into::into);

        let global_vertices = points.map(|position| {
            let position = surface.point_from_surface_coords(position);
            GlobalVertex::from_position(position)
        });

        let curve_local = Curve::Line(Line::from_points(points));
        let curve_canonical = {
            let points =
                global_vertices.map(|global_vertex| global_vertex.position());
            Curve::Line(Line::from_points(points))
        };

        let vertices = {
            let [a, b] = global_vertices;
            [
                Vertex::new(Point::from([0.]), a),
                Vertex::new(Point::from([1.]), b),
            ]
        };

        Edge::new(
            Local::new(curve_local, curve_canonical),
            VerticesOfEdge::from_vertices(vertices),
        )
    }
}