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
//! Add holes to shapes

use fj_math::{Point, Scalar, Vector};

use crate::{
    objects::{Cycle, Face, HalfEdge, Region, Shell},
    services::Services,
    storage::Handle,
};

use super::{
    build::{BuildCycle, BuildHalfEdge, BuildRegion},
    insert::Insert,
    join::JoinCycle,
    sweep::{SweepCache, SweepRegion},
    update::{UpdateCycle, UpdateFace, UpdateRegion, UpdateShell},
};

/// Add a hole to a [`Shell`]
pub trait AddHole {
    /// Add a blind hole at the provided location
    fn add_blind_hole(
        &self,
        location: HoleLocation,
        radius: impl Into<Scalar>,
        path: impl Into<Vector<3>>,
        services: &mut Services,
    ) -> Self;

    /// Add a through hole between the provided locations
    fn add_through_hole(
        &self,
        locations: [HoleLocation; 2],
        radius: impl Into<Scalar>,
        services: &mut Services,
    ) -> Self;
}

impl AddHole for Shell {
    fn add_blind_hole(
        &self,
        location: HoleLocation,
        radius: impl Into<Scalar>,
        path: impl Into<Vector<3>>,
        services: &mut Services,
    ) -> Self {
        let entry = HalfEdge::circle(location.position, radius, services)
            .insert(services);
        let hole = Region::empty(services)
            .update_exterior(|_| {
                Cycle::empty()
                    .add_half_edges([entry.clone()])
                    .insert(services)
            })
            .sweep_region(
                location.face.surface(),
                path,
                &mut SweepCache::default(),
                services,
            )
            .all_faces()
            .map(|face| face.insert(services))
            .collect::<Vec<_>>();

        self.update_face(location.face, |face| {
            face.update_region(|region| {
                region
                    .add_interiors([Cycle::empty()
                        .add_joined_edges(
                            [(entry.clone(), entry.path(), entry.boundary())],
                            services,
                        )
                        .insert(services)])
                    .insert(services)
            })
            .insert(services)
        })
        .add_faces(hole)
    }

    fn add_through_hole(
        &self,
        [entry_location, exit_location]: [HoleLocation; 2],
        radius: impl Into<Scalar>,
        services: &mut Services,
    ) -> Self {
        let radius = radius.into();

        let entry = HalfEdge::circle(entry_location.position, radius, services)
            .insert(services);

        let path = {
            let point = |location: &HoleLocation| {
                location
                    .face
                    .surface()
                    .geometry()
                    .point_from_surface_coords(location.position)
            };

            let entry_point = point(&entry_location);
            let exit_point = point(&exit_location);

            exit_point - entry_point
        };

        let swept_region = Region::empty(services)
            .update_exterior(|_| {
                Cycle::empty()
                    .add_half_edges([entry.clone()])
                    .insert(services)
            })
            .sweep_region(
                entry_location.face.surface(),
                path,
                &mut SweepCache::default(),
                services,
            );

        let hole = swept_region
            .side_faces
            .into_iter()
            .map(|face| face.insert(services))
            .collect::<Vec<_>>();

        let exit = swept_region
            .top_face
            .region()
            .exterior()
            .half_edges()
            .only();

        self.update_face(entry_location.face, |face| {
            face.update_region(|region| {
                region
                    .add_interiors([Cycle::empty()
                        .add_joined_edges(
                            [(entry.clone(), entry.path(), entry.boundary())],
                            services,
                        )
                        .insert(services)])
                    .insert(services)
            })
            .insert(services)
        })
        .update_face(exit_location.face, |face| {
            face.update_region(|region| {
                region
                    .add_interiors([Cycle::empty()
                        .add_joined_edges(
                            [(exit.clone(), exit.path(), exit.boundary())],
                            services,
                        )
                        .insert(services)])
                    .insert(services)
            })
            .insert(services)
        })
        .add_faces(hole)
    }
}

/// Defines the location of a hole
pub struct HoleLocation<'r> {
    /// The face that the hole is in
    pub face: &'r Handle<Face>,

    /// The position of the hole within the face, in surface coordinates
    pub position: Point<2>,
}