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
//! Thin wrappers around `brepkit-blend` for the operations API.
use brepkit_blend::BlendResult;
use brepkit_blend::chamfer_builder::ChamferBuilder;
use brepkit_blend::fillet_builder::FilletBuilder;
use brepkit_topology::Topology;
use brepkit_topology::edge::EdgeId;
use brepkit_topology::solid::SolidId;
use crate::OperationsError;
/// Fillet edges with constant radius — the production engine.
///
/// Runs the corrected [`FilletBuilder`]: one ordered contour per G1 run,
/// canonical boundary-registry ownership, ordered-fan junction corners, and
/// closed periodic bands for circular rims. This is the engine the WASM
/// dispatch prefers; the deprecated `fillet_rolling_ball` remains callable
/// only as an explicit reference implementation.
///
/// # Errors
/// Returns `OperationsError` if radius is non-positive, edges are empty,
/// or the blend computation fails.
pub fn fillet_v2(
topo: &mut Topology,
solid: SolidId,
edges: &[EdgeId],
radius: f64,
) -> Result<BlendResult, OperationsError> {
if radius <= 0.0 {
return Err(OperationsError::InvalidInput {
reason: "radius must be positive".into(),
});
}
if edges.is_empty() {
return Err(OperationsError::InvalidInput {
reason: "no edges specified".into(),
});
}
let mut builder = FilletBuilder::new(topo, solid);
builder.add_edges(edges, radius);
Ok(builder.build()?)
}
/// Chamfer edges with two distances (v2 engine).
///
/// # Errors
/// Returns `OperationsError` if distances are non-positive, edges are empty,
/// or the blend computation fails.
pub fn chamfer_v2(
topo: &mut Topology,
solid: SolidId,
edges: &[EdgeId],
d1: f64,
d2: f64,
) -> Result<BlendResult, OperationsError> {
if d1 <= 0.0 || d2 <= 0.0 {
return Err(OperationsError::InvalidInput {
reason: "distances must be positive".into(),
});
}
if edges.is_empty() {
return Err(OperationsError::InvalidInput {
reason: "no edges specified".into(),
});
}
let mut builder = ChamferBuilder::new(topo, solid);
builder.add_edges_asymmetric(edges, d1, d2);
Ok(builder.build()?)
}
/// Chamfer edges with distance and angle (v2 engine).
///
/// # Errors
/// Returns `OperationsError` if distance is non-positive, angle is out of
/// range (0, pi/2), edges are empty, or the blend computation fails.
pub fn chamfer_distance_angle(
topo: &mut Topology,
solid: SolidId,
edges: &[EdgeId],
distance: f64,
angle: f64,
) -> Result<BlendResult, OperationsError> {
if distance <= 0.0 {
return Err(OperationsError::InvalidInput {
reason: "distance must be positive".into(),
});
}
if angle <= 0.0 || angle >= std::f64::consts::FRAC_PI_2 {
return Err(OperationsError::InvalidInput {
reason: "angle must be between 0 and \u{03c0}/2".into(),
});
}
if edges.is_empty() {
return Err(OperationsError::InvalidInput {
reason: "no edges specified".into(),
});
}
let mut builder = ChamferBuilder::new(topo, solid);
builder.add_edges_distance_angle(edges, distance, angle);
Ok(builder.build()?)
}
/// Fillet edges with per-edge radius laws (v2 walking engine).
///
/// # Errors
/// Returns `OperationsError` if `edge_laws` is empty or the blend
/// computation fails.
pub fn fillet_v2_variable(
topo: &mut Topology,
solid: SolidId,
edge_laws: Vec<(EdgeId, brepkit_blend::radius_law::RadiusLaw)>,
) -> Result<BlendResult, OperationsError> {
if edge_laws.is_empty() {
return Err(OperationsError::InvalidInput {
reason: "no edges specified".into(),
});
}
// Group tangent-continuous runs with an identical constant radius into
// one edge set. The builder still computes per-edge stripes inside a
// set, so grouping does not remove in-run junctions by itself; it keeps
// each run under one radius law and gives downstream corner handling a
// consistent picture of which junctions are true radius-change or
// angular corners.
let mut remaining: Vec<(EdgeId, brepkit_blend::radius_law::RadiusLaw)> = edge_laws;
let mut chains: Vec<(Vec<EdgeId>, brepkit_blend::radius_law::RadiusLaw)> = Vec::new();
while let Some((seed, law)) = remaining.pop() {
let seed_r = match law {
brepkit_blend::radius_law::RadiusLaw::Constant(r) => Some(r),
_ => None,
};
let mut chain = vec![seed];
if let Some(r) = seed_r {
loop {
let mut grew = false;
let mut i = 0;
while i < remaining.len() {
let brepkit_blend::radius_law::RadiusLaw::Constant(cand_r) = remaining[i].1
else {
i += 1;
continue;
};
if (cand_r - r).abs() > 1e-9 {
i += 1;
continue;
}
if chain_extends_tangently(topo, &chain, remaining[i].0) {
chain.push(remaining[i].0);
remaining.swap_remove(i);
grew = true;
} else {
i += 1;
}
}
if !grew {
break;
}
}
}
chains.push((chain, law));
}
let mut builder = FilletBuilder::new(topo, solid);
for (chain, law) in chains {
builder.add_edges_with_law(&chain, law);
}
Ok(builder.build()?)
}
/// Whether `cand` shares an endpoint with some chain edge and meets it
/// tangentially (G1) there.
fn chain_extends_tangently(topo: &Topology, chain: &[EdgeId], cand: EdgeId) -> bool {
use brepkit_math::vec::{Point3, Vec3};
let ends = |eid: EdgeId| -> Option<(Point3, Point3, brepkit_topology::edge::EdgeCurve)> {
let e = topo.edge(eid).ok()?;
Some((
topo.vertex(e.start()).ok()?.point(),
topo.vertex(e.end()).ok()?.point(),
e.curve().clone(),
))
};
let Some((cs, ce, cc)) = ends(cand) else {
return false;
};
let tangent_at = |curve: &brepkit_topology::edge::EdgeCurve,
s: Point3,
e: Point3,
at_start: bool|
-> Option<Vec3> {
curve
.tangent_with_endpoints(if at_start { 0.0 } else { 1.0 }, s, e)
.normalize()
.ok()
};
for &eid in chain {
let Some((s, e, curve)) = ends(eid) else {
continue;
};
for (cp, c_at_start) in [(cs, true), (ce, false)] {
for (p, at_start) in [(s, true), (e, false)] {
if (cp - p).length() > 1e-6 {
continue;
}
let (Some(t_chain), Some(t_cand)) = (
tangent_at(&curve, s, e, at_start),
tangent_at(&cc, cs, ce, c_at_start),
) else {
continue;
};
// Traversal directions at a shared vertex oppose when the
// curves continue smoothly through it exactly when one is
// at its start and the other at its end.
let aligned = if at_start == c_at_start {
-t_chain.dot(t_cand)
} else {
t_chain.dot(t_cand)
};
if aligned > 0.999 {
return true;
}
}
}
}
false
}