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

use crate::objects::{Edge, Vertex, VerticesOfEdge};

use super::{Approx, Local};

impl Approx for Edge {
    type Approximation = Vec<Local<Point<1>>>;

    fn approx(&self, tolerance: super::Tolerance) -> Self::Approximation {
        let mut points = self.curve().approx(tolerance);
        approx_edge(*self.vertices(), &mut points);

        points
    }
}

pub fn approx_edge(
    vertices: VerticesOfEdge<Vertex>,
    points: &mut Vec<Local<Point<1>>>,
) {
    // Insert the exact vertices of this edge into the approximation. This means
    // we don't rely on the curve approximation to deliver accurate
    // representations of these vertices, which they might not be able to do.
    //
    // If we used inaccurate representations of those vertices here, then that
    // would lead to bugs in the approximation, as points that should refer to
    // the same vertex would be understood to refer to very close, but distinct
    // vertices.
    let vertices = vertices.convert(|vertex| {
        Local::new(vertex.position(), vertex.global().position())
    });
    if let Some([a, b]) = vertices {
        points.insert(0, a);
        points.push(b);
    }

    if vertices.is_none() {
        // The edge has no vertices, which means it connects to itself. We need
        // to reflect that in the approximation.

        if let Some(&point) = points.first() {
            points.push(point);
        }
    }
}

#[cfg(test)]
mod test {
    use fj_math::Point;

    use crate::{
        algorithms::approx::Local,
        objects::{GlobalVertex, Vertex, VerticesOfEdge},
    };

    #[test]
    fn approx_edge() {
        let a = Point::from([1., 2., 3.]);
        let b = Point::from([2., 3., 5.]);
        let c = Point::from([3., 5., 8.]);
        let d = Point::from([5., 8., 13.]);

        let v1 = GlobalVertex::from_position(a);
        let v2 = GlobalVertex::from_position(d);

        let vertices = VerticesOfEdge::from_vertices([
            Vertex::new(Point::from([0.]), v1),
            Vertex::new(Point::from([1.]), v2),
        ]);

        let a = Local::new([0.0], a);
        let b = Local::new([0.25], b);
        let c = Local::new([0.75], c);
        let d = Local::new([1.0], d);

        // Regular edge
        let mut points = vec![b, c];
        super::approx_edge(vertices, &mut points);
        assert_eq!(points, vec![a, b, c, d]);

        // Continuous edge
        let mut points = vec![b, c];
        super::approx_edge(VerticesOfEdge::none(), &mut points);
        assert_eq!(points, vec![b, c, b]);
    }
}