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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//! What the turned-stock treatments share — a toroidal fillet and a conical chamfer are both
//! proved by the coaxial external cylinders and the faces they meet (`quiddity.fillets`,
//! `quiddity.chamfers`); countersinks read cone rims the same way. The turned-profile key
//! (`quiddity.turned.profile_key_from_bands`) under which steps and grooves publish their
//! body/profile join lives here too.
use std::cmp::Ordering;
use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use super::Context;
use super::body::BodyKey;
use super::cylinders::{CylinderEvidence, coaxial_axis_lines};
use super::placed::{Grid, Placed};
use super::policy::{self, AXIS_ALIGNED_COS};
use crate::kernel::brep::Part;
use crate::kernel::geom::{self, Bounds, Curve, Surface, V3};
use crate::kernel::py;
/// Coaxial analytic axes may differ by modelling noise only (a fraction of the diameter).
const COAXIAL_FRAC: f64 = 1e-4;
internal_entry! {
/// The run's external cylinders by face.
pub fn external_cylinders<'c>(ctx: &'c Context<'_>) -> BTreeMap<usize, &'c CylinderEvidence> {
ctx.cylinders()
.iter()
.filter(|c| c.external)
.map(|c| (c.face, c))
.collect()
}
}
internal_entry! {
/// The external cylinders among *neighbours* that revolve about the line (*origin*, *axis*).
pub fn coaxial_cylinders<'c>(
neighbours: &[usize],
external: &BTreeMap<usize, &'c CylinderEvidence>,
origin: V3,
axis: V3,
) -> Vec<&'c CylinderEvidence> {
neighbours
.iter()
.filter_map(|n| external.get(n).copied())
.filter(|c| {
coaxial_axis_lines(
origin,
axis,
c.axis_point,
c.direction,
policy::length_tol(c.diameter, COAXIAL_FRAC),
)
})
.collect()
}
}
internal_entry! {
/// Whether the coaxial cylinders include two different bands (a shoulder between diameters).
pub fn bridges_two_bands(coaxial: &[&CylinderEvidence]) -> bool {
coaxial.iter().any(|c| c.diameter != coaxial[0].diameter)
}
}
internal_entry! {
/// The planar faces among *neighbours* square to *axis* (a shoulder or end face).
pub fn transverse_planes(part: &Part, neighbours: &[usize], axis: V3) -> Vec<usize> {
neighbours
.iter()
.copied()
.filter(|&n| match part.faces[n].surface {
Surface::Plane { frame } => geom::dot(frame.z, axis).abs() > AXIS_ALIGNED_COS,
_ => false,
})
.collect()
}
}
/// A circular edge's radius and centre.
fn circle(part: &Part, edge: usize) -> Option<(f64, V3)> {
match part.edges[edge].curve {
Curve::Circle { frame, radius } => Some((radius, frame.origin)),
_ => None,
}
}
/// A circular rim: its radius and centre.
pub type Rim = (f64, V3);
internal_entry! {
/// `(minor, major, included angle°)` of a cone face: its smallest and largest circular rims and
/// the full cone angle to 2 dp (`cone_rims`).
pub fn cone_rims(part: &Part, face: usize) -> Option<(Rim, Rim, f64)> {
let Surface::Cone { semi_angle, .. } = part.faces[face].surface else {
return None;
};
let mut circles: Vec<(f64, V3)> = part
.face_edges(face)
.into_iter()
.filter_map(|e| circle(part, e))
.collect();
if circles.len() < 2 {
return None;
}
circles.sort_by(|a, b| py::order(a.0, b.0));
let included = py::round_to(2.0 * semi_angle.to_degrees().abs(), 2);
Some((circles[0], circles[circles.len() - 1], included))
}
}
/// The body-local membership of one turned profile (`TurnedProfileKey`): the axis letter, the
/// commonest band axis point with its axial coordinate zeroed, the body's box and its key.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TurnedProfileKey {
pub axis: char,
pub axis_origin: Placed<V3>,
pub body_bounds: Placed<[f64; 6]>,
pub body_key: Option<BodyKey>,
}
impl TurnedProfileKey {
/// Python's `_order_key` comparison.
pub fn order(&self, other: &Self) -> Ordering {
self.axis
.cmp(&other.axis)
.then_with(|| py::tuple_order(&self.axis_origin[..], &other.axis_origin[..]))
.then_with(|| py::tuple_order(&self.body_bounds[..], &other.body_bounds[..]))
.then(self.body_key.is_some().cmp(&other.body_key.is_some()))
.then_with(|| {
py::tuple_order(
self.body_key.as_ref().map_or(&[][..], |k| k.as_slice()),
other.body_key.as_ref().map_or(&[][..], |k| k.as_slice()),
)
})
}
}
internal_entry! {
/// The axis letter of an axis index.
pub fn axis_letter(axis: usize) -> char {
['x', 'y', 'z'][axis]
}
}
internal_entry! {
/// `profile_key_from_bands`: the profile one body's coaxial bands prove. *bounds* is the body's
/// box (the solid's, or the part's when it has none).
pub fn profile_key_from_bands(
axis: usize,
bands: &[&CylinderEvidence],
bounds: &Bounds,
body_key: Option<BodyKey>,
) -> TurnedProfileKey {
// Coaxial supports describe one transverse origin. Average their raw representatives
// before publication, instead of selecting a modal rounded coordinate lexicographically.
// Translation, axis interchange and reversal commute with this affine construction.
assert!(!bands.is_empty(), "a profile has bands");
let origin = [0, 1, 2].map(|i| {
if i == axis {
0.0
} else {
py::fsum(bands.iter().map(|b| b.axis_point[i])) / bands.len() as f64
}
});
TurnedProfileKey {
axis: axis_letter(axis),
axis_origin: Placed::new(origin, Grid::Decimals(8)),
body_bounds: Placed::new(
[
bounds.min[0],
bounds.max[0],
bounds.min[1],
bounds.max[1],
bounds.min[2],
bounds.max[2],
],
Grid::Decimals(8),
),
body_key,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn equally_supported_profile_origins_follow_motion_and_band_permutation() {
// Analytically coaxial bands can carry transverse modelling noise. Both rounded
// representatives occur once: the lower coordinate must not win the mode tie.
for [w, l, axis] in [[0, 1, 2], [1, 2, 0], [2, 0, 1]] {
for sign in [-1., 1.] {
for shift in [0., 123.456] {
let point = |noise: f64| {
let mut p = [0.; 3];
p[w] = shift + sign * (1. + noise);
p[l] = shift - sign * (2. - noise);
p[axis] = shift;
p
};
let mut direction = [0.; 3];
direction[axis] = sign;
let mut bands: Vec<_> = [-2e-8, 2e-8]
.into_iter()
.enumerate()
.map(|(face, noise)| CylinderEvidence {
face,
solid: 0,
diameter: 10.,
axis,
u_extent: std::f64::consts::TAU,
axis_point: point(noise),
direction,
s_lo: 0.,
s_hi: 10.,
external: true,
})
.collect();
let bounds = Bounds {
min: [shift - 10.; 3],
max: [shift + 10.; 3],
};
let mut expected = point(0.).map(|v| py::round_to(v, 8));
expected[axis] = 0.;
for _ in 0..2 {
let refs: Vec<_> = bands.iter().collect();
let key = profile_key_from_bands(axis, &refs, &bounds, None);
assert_eq!(*key.axis_origin, expected);
bands.reverse();
}
}
}
}
}
}