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
use fj_interop::mesh::Color;

use crate::{
    algorithms::{approx::Tolerance, reverse_face, transform::TransformObject},
    objects::{Face, Shell},
};

use super::{Path, Sweep};

impl Sweep for Face {
    type Swept = Shell;

    fn sweep(
        self,
        path: impl Into<Path>,
        tolerance: impl Into<Tolerance>,
        color: Color,
    ) -> Self::Swept {
        let path = path.into();
        let tolerance = tolerance.into();

        let mut faces = Vec::new();

        let bottom_face =
            create_bottom_face(&self, path.is_negative_direction());
        faces.push(bottom_face);

        let top_face = create_top_face(self.clone(), path);
        faces.push(top_face);

        for cycle in self.all_cycles() {
            for edge in cycle.edges() {
                let face = edge.sweep(path, tolerance, color);
                faces.push(face);
            }
        }

        Shell::new().with_faces(faces)
    }
}

fn create_bottom_face(
    face: &Face,
    is_sweep_along_negative_direction: bool,
) -> Face {
    if is_sweep_along_negative_direction {
        face.clone()
    } else {
        reverse_face(face)
    }
}

fn create_top_face(face: Face, path: Path) -> Face {
    let mut face = face.translate(path.inner());

    if path.is_negative_direction() {
        face = reverse_face(&face);
    };

    face
}