use std::collections::{BTreeSet, HashMap};
use super::{Ball, Picked, SIDES, run::Beyond};
use crate::{
boolean::{
csi::{curve_distance, signed_distance},
same_domain, unify,
},
check,
geom::{Curve, Surface},
math::{Point3, Vec3},
tol,
topo::{Brep, CoedgeId, CoedgeSpec, EdgeId, FaceId, FaceName, PCurve, VertexId},
};
#[derive(Clone, Copy, Debug, PartialEq)]
enum Role {
Blend,
Other,
Side(usize),
Cap,
}
const PIECE: &str = "#piece";
pub(super) fn marked(mut tool: Brep, i: usize) -> Brep {
for f in tool.face_ids().collect::<Vec<_>>() {
let mut names = tool.face(f).names.clone();
let mark = if names.iter().any(|n| n.face == "blend") {
format!("{i}")
} else if let Some(k) = SIDES
.iter()
.position(|s| names.iter().any(|n| n.face == *s))
{
format!("{i}_{k}")
} else {
continue;
};
names.insert(FaceName::new(PIECE, mark));
tool.set_face_names(f, names);
}
tool
}
fn role(tool: &Brep, f: FaceId, i: usize) -> Role {
let names = &tool.face(f).names;
let marked = |m: String| names.iter().any(|n| n.feature == PIECE && n.face == m);
if names.iter().any(|n| n.face == "blend") {
if marked(format!("{i}")) {
Role::Blend
} else {
Role::Other
}
} else if let Some(k) = (0..SIDES.len()).find(|k| marked(format!("{i}_{k}"))) {
Role::Side(k)
} else {
Role::Cap
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Use {
Contact(usize),
Cross,
Inner,
Pole,
}
struct Parts {
blends: Vec<FaceId>,
uses: Vec<(EdgeId, Use)>,
}
fn parts(tool: &Brep, i: usize) -> Option<Parts> {
let mut faces_of: HashMap<EdgeId, Vec<FaceId>> = HashMap::new();
for c in tool.coedge_ids() {
faces_of
.entry(tool.coedge(c).edge)
.or_default()
.push(tool.coedge_face(c));
}
let role = |f: FaceId| role(tool, f, i);
let blends: Vec<FaceId> = tool
.face_ids()
.filter(|&f| role(f) == Role::Blend)
.collect();
let mut uses: Vec<(EdgeId, Use)> = Vec::new();
for &f in &blends {
for &lp in &tool.face(f).loops {
for &c in &tool.lp(lp).coedges {
let e = tool.coedge(c).edge;
if uses.iter().any(|u| u.0 == e) {
continue;
}
if tool.edge(e).curve.is_none() {
uses.push((e, Use::Pole));
continue;
}
let roles: Vec<Role> = faces_of[&e].iter().map(|&g| role(g)).collect();
let u = match roles.as_slice() {
[Role::Blend, Role::Blend | Role::Other] | [Role::Other, Role::Blend] => {
Use::Inner
}
[Role::Blend, Role::Side(k)] | [Role::Side(k), Role::Blend] => Use::Contact(*k),
[Role::Blend, Role::Cap] | [Role::Cap, Role::Blend] => Use::Cross,
_ => return None,
};
uses.push((e, u));
}
}
}
(!blends.is_empty()).then_some(Parts { blends, uses })
}
#[derive(Clone, Copy)]
struct Mapped {
edge: EdgeId,
flipped: bool,
own: bool,
}
#[allow(clippy::too_many_arguments)]
pub(super) fn sewn(
body: &Brep,
picked: &[Picked],
tools: &[Brep],
beyond: &[[Beyond; 2]],
convex: &[bool],
names: &[BTreeSet<FaceName>],
balls: &[(Vec<usize>, Ball)],
feature: &str,
) -> Result<Brep, String> {
if body.has_tolerances() {
return Err("a body with local tolerances".into());
}
let parts: Vec<Parts> = tools
.iter()
.enumerate()
.map(|(i, t)| parts(t, i))
.collect::<Option<_>>()
.ok_or("a tool whose blend faces meet it other than at its sides and ends")?;
let picked_of: HashMap<EdgeId, usize> = picked
.iter()
.enumerate()
.map(|(i, pk)| (pk.edge, i))
.collect();
let mut gone: BTreeSet<VertexId> = BTreeSet::new();
let mut near: HashMap<VertexId, BTreeSet<usize>> = HashMap::new();
for (i, pk) in picked.iter().enumerate() {
let e = body.edge(pk.edge);
for v in [e.start, e.end] {
gone.insert(v);
near.entry(v).or_default().insert(i);
}
}
let mut dropped: BTreeSet<EdgeId> = BTreeSet::new();
for (i, b) in beyond.iter().enumerate() {
for bk in b {
for &w in &bk.gone {
gone.insert(w);
near.entry(w).or_default().insert(i);
}
dropped.extend(bk.dropped.iter().copied());
}
}
let mut out = Brep::new();
let mut vmap: Vec<Option<VertexId>> = vec![None; body.vertex_ids().count()];
for v in body.vertex_ids() {
if !gone.contains(&v) {
vmap[v.index()] = Some(out.add_vertex(body.vertex(v).point));
}
}
let mut pool: Vec<VertexId> = Vec::new();
let mut tvmap: Vec<HashMap<VertexId, VertexId>> = vec![HashMap::new(); tools.len()];
for (i, tool) in tools.iter().enumerate() {
for &(e, _) in &parts[i].uses {
let edge = tool.edge(e);
for v in [edge.start, edge.end] {
if tvmap[i].contains_key(&v) {
continue;
}
let p = tool.vertex(v).point;
let id = match pool
.iter()
.find(|&&w| out.vertex(w).point.dist(p) <= tol::LINEAR)
{
Some(&w) => w,
None => {
let w = out.add_vertex(p);
pool.push(w);
w
}
};
tvmap[i].insert(v, id);
}
}
}
let points: Vec<BTreeSet<VertexId>> = tvmap
.iter()
.map(|m| m.values().copied().collect())
.collect();
for &w in &pool {
let p = out.vertex(w).point;
if vmap
.iter()
.flatten()
.any(|&v| out.vertex(v).point.dist(p) <= tol::LINEAR)
{
return Err("a blend that ends on a vertex of the body".into());
}
}
let mut ranges: Vec<(f64, f64)> = body.edge_ids().map(|e| body.edge(e).range).collect();
let mut ends: Vec<[Option<VertexId>; 2]> = body
.edge_ids()
.map(|e| {
let x = body.edge(e);
[vmap[x.start.index()], vmap[x.end.index()]]
})
.collect();
for e in body.edge_ids() {
if picked_of.contains_key(&e) || dropped.contains(&e) {
continue;
}
let edge = body.edge(e);
let at = [gone.contains(&edge.start), gone.contains(&edge.end)];
if !at[0] && !at[1] {
continue;
}
let Some(curve) = &edge.curve else {
return Err("a degenerate edge at a blend's end".into());
};
if edge.start == edge.end {
return Err("a closed edge through a blend's end".into());
}
for (k, &cut) in at.iter().enumerate() {
if !cut {
continue;
}
let v = if k == 0 { edge.start } else { edge.end };
let corner = body.vertex(v).point;
let candidates: BTreeSet<VertexId> = near[&v]
.iter()
.flat_map(|&i| points[i].iter().copied())
.collect();
let on = |range: (f64, f64)| -> Vec<(VertexId, f64)> {
candidates
.iter()
.filter_map(|&w| {
let p = out.vertex(w).point;
let (d, t) = curve_distance(curve, range, p);
(d <= tol::LINEAR && p.dist(corner) > tol::LINEAR).then_some((w, t))
})
.collect()
};
let mut hits = on(edge.range);
let span = edge.range.1 - edge.range.0;
let more = match curve {
Curve::Line(_) => Some(1e3 * span.max(1.0)),
Curve::Circle(_) => Some(std::f64::consts::TAU - span),
_ => None,
};
if let Some(more) = more.filter(|_| hits.is_empty()) {
let past = if k == 0 {
(edge.range.0 - more, edge.range.0)
} else {
(edge.range.1, edge.range.1 + more)
};
hits = on(past);
}
let Some(&(w, t)) = hits.first() else {
return Err(format!(
"a {} edge at a blend's end at ({:.3}, {:.3}, {:.3}) that no contact curve \
meets",
curve.kind(),
corner.x,
corner.y,
corner.z
));
};
if hits.iter().any(|h| h.0 != w) {
return Err("an edge at a blend's end that two contact curves meet".into());
}
ends[e.index()][k] = Some(w);
if k == 0 {
ranges[e.index()].0 = t;
} else {
ranges[e.index()].1 = t;
}
}
let (t0, t1) = ranges[e.index()];
let speed = curve.d1(0.5 * (t0 + t1)).1.norm().max(1e-300);
if (t1 - t0) * speed <= tol::LINEAR {
return Err("an edge a blend cuts back past its other end".into());
}
}
let mut emap: Vec<Option<EdgeId>> = vec![None; body.edge_ids().count()];
for e in body.edge_ids() {
if picked_of.contains_key(&e) || dropped.contains(&e) {
continue;
}
let edge = body.edge(e);
let [Some(s), Some(t)] = ends[e.index()] else {
unreachable!("every end of a kept edge is kept or cut back");
};
emap[e.index()] = Some(match &edge.curve {
Some(c) => out.add_edge(c.clone(), ranges[e.index()], s, t),
None => out.add_degenerate_edge(s, edge.range),
});
}
let mut tmap: Vec<HashMap<EdgeId, Mapped>> = vec![HashMap::new(); tools.len()];
let mut crosses: Vec<(EdgeId, Point3)> = Vec::new();
let mut creases: Vec<(EdgeId, Point3)> = Vec::new();
for (i, tool) in tools.iter().enumerate() {
for &(e, u) in &parts[i].uses {
let edge = tool.edge(e);
let (s, t) = (tvmap[i][&edge.start], tvmap[i][&edge.end]);
let Some(curve) = edge.curve.as_ref() else {
let x = out.add_degenerate_edge(s, edge.range);
tmap[i].insert(
e,
Mapped {
edge: x,
flipped: false,
own: true,
},
);
continue;
};
let mid = curve.eval(0.5 * (edge.range.0 + edge.range.1));
let shared = match u {
Use::Cross => Some(&crosses),
Use::Inner => Some(&creases),
_ => None,
};
if let Some(list) = shared.filter(|_| s != t) {
let same = list.iter().find_map(|&(x, m)| {
let xe = out.edge(x);
let ends_match =
(xe.start == s && xe.end == t) || (xe.start == t && xe.end == s);
(ends_match && m.dist(mid) <= tol::LINEAR).then_some((x, xe.start != s))
});
if let Some((x, flipped)) = same {
tmap[i].insert(
e,
Mapped {
edge: x,
flipped,
own: false,
},
);
continue;
}
}
let x = out.add_edge(curve.clone(), edge.range, s, t);
match u {
Use::Cross => crosses.push((x, mid)),
Use::Inner => creases.push((x, mid)),
_ => {}
}
tmap[i].insert(
e,
Mapped {
edge: x,
flipped: false,
own: true,
},
);
}
}
let cross_edges: Vec<EdgeId> = crosses.iter().map(|c| c.0).collect();
let mut fmap: Vec<FaceId> = Vec::with_capacity(body.face_count());
let mut sides: Vec<Option<usize>> = Vec::new();
for f in body.face_ids() {
let face = body.face(f);
let nf = out.add_face(face.surface.clone(), face.reversed, face.names.clone());
fmap.push(nf);
let mut touched = false;
let mut loops = Vec::with_capacity(face.loops.len());
for &lp in &face.loops {
let (specs, changed) = rebuild_loop(
body,
lp,
&Rebuild {
picked,
picked_of: &picked_of,
parts: &parts,
tools,
tmap: &tmap,
emap: &emap,
ranges: &ranges,
cross_edges: &cross_edges,
dropped: &dropped,
},
&out,
)?;
touched |= changed;
loops.push(specs);
}
for specs in loops {
out.add_loop(nf, specs);
}
sides.push((!touched).then_some(0));
}
let mut blend_faces: Vec<(usize, FaceId)> = Vec::new();
for (i, tool) in tools.iter().enumerate() {
for &f in &parts[i].blends {
let face = tool.face(f);
let nf = out.add_face(
face.surface.clone(),
face.reversed != convex[i],
names[i].iter().cloned(),
);
for &lp in &face.loops {
let mut specs = Vec::new();
for &c in &tool.lp(lp).coedges {
let co = tool.coedge(c);
let m = tmap[i][&co.edge];
let params = check::coedge_params(tool, c, 1);
let pcurve = |at_end: bool| -> PCurve {
if m.own && matches!(co.pcurve, PCurve::Affine { .. }) {
return co.pcurve.clone();
}
let uvs = tool.coedge_uvs(c, ¶ms);
PCurve::Projected {
seed: if at_end { uvs[1] } else { uvs[0] },
}
};
specs.push(if convex[i] {
CoedgeSpec {
edge: m.edge,
reversed: !(co.reversed != m.flipped),
pcurve: pcurve(true),
}
} else {
CoedgeSpec {
edge: m.edge,
reversed: co.reversed != m.flipped,
pcurve: pcurve(false),
}
});
}
if convex[i] {
specs.reverse();
}
out.add_loop(nf, specs);
}
sides.push(None);
blend_faces.push((i, nf));
}
}
for (at, ball) in balls {
let f = corner_face(&mut out, ball, at, &tmap, tools, &parts, feature)?;
sides.push(None);
blend_faces.push((at[0], f));
}
for s in body.solid_ids() {
let mut shells = Vec::new();
for &sh in &body.solid(s).shells {
let mut faces: Vec<FaceId> = body
.shell(sh)
.faces
.iter()
.map(|f| fmap[f.index()])
.collect();
let members: BTreeSet<FaceId> = body.shell(sh).faces.iter().copied().collect();
faces.extend(
blend_faces
.iter()
.filter(|(i, _)| members.contains(&picked[*i].faces[0]))
.map(|&(_, f)| f),
);
shells.push(out.add_shell(faces));
}
out.add_solid_of(shells, body.solid(s).outer);
}
out.pair_radial();
let issues = check::check_cut(&out, &sides);
if !issues.is_empty() {
let first: Vec<String> = issues.iter().take(3).map(|i| i.to_string()).collect();
return Err(format!("the sewn solid is invalid: {}", first.join("; ")));
}
if mergeable(&out, &sides) {
return unify(&out).map_err(|e| format!("merging the sewn faces: {e}"));
}
Ok(out)
}
fn corner_face(
out: &mut Brep,
ball: &Ball,
at: &[usize],
tmap: &[HashMap<EdgeId, Mapped>],
tools: &[Brep],
parts: &[Parts],
feature: &str,
) -> Result<FaceId, String> {
let sphere = ball.surface().map_err(|e| e.to_string())?;
let on_ball = |p: Point3| (p.dist(ball.centre) - ball.radius).abs() <= tol::LINEAR;
let mut arcs: Vec<EdgeId> = Vec::new();
for &i in at {
let tool = &tools[i];
let mut ends = parts[i].uses.iter().filter(|u| {
let e = tool.edge(u.0);
u.1 == Use::Cross
&& on_ball(tool.vertex(e.start).point)
&& on_ball(tool.vertex(e.end).point)
});
let (Some(&(e, _)), None) = (ends.next(), ends.next()) else {
return Err("a corner piece without one end section on its sphere".into());
};
arcs.push(tmap[i][&e].edge);
}
let mut lp: Vec<(EdgeId, bool)> = vec![(arcs[0], false)];
let mut left: Vec<EdgeId> = arcs[1..].to_vec();
while !left.is_empty() {
let (e, rev) = *lp.last().expect("started with one");
let x = out.edge(e);
let tip = if rev { x.start } else { x.end };
let k = left
.iter()
.position(|&a| out.edge(a).start == tip || out.edge(a).end == tip)
.ok_or("a corner whose end sections do not close")?;
let a = left.remove(k);
lp.push((a, out.edge(a).end == tip));
}
let start = |&(e, rev): &(EdgeId, bool)| {
let x = out.edge(e);
out.vertex(if rev { x.end } else { x.start }).point
};
let corners: Vec<Point3> = lp.iter().map(start).collect();
let n: Vec3 = ball.normals[0] + ball.normals[1] + ball.normals[2];
let turn = (corners[1] - corners[0])
.cross(corners[2] - corners[1])
.dot(n);
if turn < 0.0 {
lp.reverse();
for c in &mut lp {
c.1 = !c.1;
}
}
let surface = Surface::Sphere(sphere);
let hint = surface.project(ball.centre + n.unit_or_zero() * ball.radius);
let specs = lp
.iter()
.map(|c| CoedgeSpec {
edge: c.0,
reversed: c.1,
pcurve: PCurve::Projected {
seed: surface.project_near(start(c), hint),
},
})
.collect();
let f = out.add_face(surface, false, [FaceName::new(feature, "corner")]);
out.add_loop(f, specs);
Ok(f)
}
struct Rebuild<'a> {
picked: &'a [Picked],
picked_of: &'a HashMap<EdgeId, usize>,
parts: &'a [Parts],
tools: &'a [Brep],
tmap: &'a [HashMap<EdgeId, Mapped>],
emap: &'a [Option<EdgeId>],
ranges: &'a [(f64, f64)],
cross_edges: &'a [EdgeId],
dropped: &'a BTreeSet<EdgeId>,
}
fn spec_ends(out: &Brep, s: &CoedgeSpec) -> (VertexId, VertexId) {
let e = out.edge(s.edge);
if s.reversed {
(e.end, e.start)
} else {
(e.start, e.end)
}
}
fn rebuild_loop(
body: &Brep,
lp: crate::topo::LoopId,
r: &Rebuild,
out: &Brep,
) -> Result<(Vec<CoedgeSpec>, bool), String> {
let face = body.face(body.lp(lp).face);
let surface = &face.surface;
let mut changed = false;
let mut specs: Vec<(CoedgeSpec, CoedgeId)> = Vec::new();
for &c in &body.lp(lp).coedges {
let co = body.coedge(c);
let edge = body.edge(co.edge);
let (t_start, _) = if co.reversed {
(edge.range.1, edge.range.0)
} else {
edge.range
};
if let Some(&i) = r.picked_of.get(&co.edge) {
changed = true;
let k = r.picked[i]
.faces
.iter()
.position(|&g| g == body.lp(lp).face)
.ok_or("a picked edge on a face it does not join")?;
let contacts: Vec<EdgeId> = r.parts[i]
.uses
.iter()
.filter(|u| u.1 == Use::Contact(k))
.map(|u| u.0)
.collect();
let ce = match contacts.as_slice() {
[ce] => ce,
[] if r.parts[i].uses.iter().any(|u| u.1 == Use::Pole) => continue,
_ => {
return Err("a piece with its contact on a face in other than one edge".into());
}
};
let tool = &r.tools[i];
let tc = tool.edge(*ce);
let tcurve = tc.curve.as_ref().expect("checked in parts");
let curve = edge.curve.as_ref().expect("a picked edge has a curve");
let tm = 0.5 * (edge.range.0 + edge.range.1);
let (p, d) = curve.d1(tm);
let d = if co.reversed { -d } else { d };
let (_, s) = curve_distance(tcurve, tc.range, p);
let reversed = tcurve.d1(s).1.dot(d) < 0.0;
let m = r.tmap[i][ce];
let spec_edge = out.edge(m.edge);
let start = if reversed {
spec_edge.end
} else {
spec_edge.start
};
let hint = body.coedge_uvs(c, &[t_start])[0];
let seed = surface.project_near(out.vertex(start).point, hint);
specs.push((
CoedgeSpec {
edge: m.edge,
reversed,
pcurve: PCurve::Projected { seed },
},
c,
));
continue;
}
if r.dropped.contains(&co.edge) {
changed = true;
continue;
}
let ne = r.emap[co.edge.index()].expect("every unpicked edge is kept");
let range = r.ranges[co.edge.index()];
let new_start = if co.reversed { range.1 } else { range.0 };
let pcurve = match &co.pcurve {
PCurve::Projected { .. } if new_start != t_start => {
changed = true;
PCurve::Projected {
seed: body.coedge_uvs(c, &[t_start, new_start])[1],
}
}
other => {
changed |= range != edge.range;
other.clone()
}
};
specs.push((
CoedgeSpec {
edge: ne,
reversed: co.reversed,
pcurve,
},
c,
));
}
if !changed {
return Ok((specs.into_iter().map(|s| s.0).collect(), false));
}
let n = specs.len();
let mut joined: Vec<CoedgeSpec> = Vec::with_capacity(n + 2);
for i in 0..n {
let (a, ca) = &specs[i];
joined.push(a.clone());
let (b, _) = &specs[(i + 1) % n];
let (_, end) = spec_ends(out, a);
let (start, _) = spec_ends(out, b);
if end == start {
continue;
}
let chain = across(out, surface, r.cross_edges, end, start)
.ok_or("a corner of a face that no one end section closes")?;
let old = body.coedge(*ca);
let range = body.edge(old.edge).range;
let t_end = if old.reversed { range.0 } else { range.1 };
let hint = body.coedge_uvs(*ca, &[t_end])[0];
for (x, reversed) in chain {
let from = if reversed {
out.edge(x).end
} else {
out.edge(x).start
};
let seed = surface.project_near(out.vertex(from).point, hint);
joined.push(CoedgeSpec {
edge: x,
reversed,
pcurve: PCurve::Projected { seed },
});
}
}
Ok((joined, true))
}
fn across(
out: &Brep,
surface: &Surface,
cross_edges: &[EdgeId],
from: VertexId,
to: VertexId,
) -> Option<Vec<(EdgeId, bool)>> {
let joins = |x: EdgeId, at: VertexId| {
let xe = out.edge(x);
if xe.start == at {
Some((x, false))
} else if xe.end == at {
Some((x, true))
} else {
None
}
};
let direct: Vec<(EdgeId, bool)> = cross_edges
.iter()
.filter_map(|&x| joins(x, from))
.filter(|&(x, rev)| {
let xe = out.edge(x);
(if rev { xe.start } else { xe.end }) == to
})
.collect();
match direct.as_slice() {
[one] => return Some(vec![*one]),
[] => {}
_ => return None,
}
let on_face = |x: EdgeId| {
let xe = out.edge(x);
let Some(c) = xe.curve.as_ref() else {
return false;
};
(0..=4).all(|i| {
let t = xe.range.0 + (xe.range.1 - xe.range.0) * i as f64 / 4.0;
signed_distance(surface, c.eval(t)).abs() <= tol::LINEAR
})
};
let mut chain: Vec<(EdgeId, bool)> = Vec::new();
let mut at = from;
for _ in 0..4 {
let next: Vec<(EdgeId, bool)> = cross_edges
.iter()
.filter(|&&x| !chain.iter().any(|c| c.0 == x) && on_face(x))
.filter_map(|&x| joins(x, at))
.collect();
let [(x, rev)] = next.as_slice() else {
return None;
};
chain.push((*x, *rev));
let xe = out.edge(*x);
at = if *rev { xe.start } else { xe.end };
if at == to {
return Some(chain);
}
}
None
}
fn mergeable(b: &Brep, sides: &[Option<usize>]) -> bool {
let mut faces_of: HashMap<EdgeId, Vec<FaceId>> = HashMap::new();
for c in b.coedge_ids() {
faces_of
.entry(b.coedge(c).edge)
.or_default()
.push(b.coedge_face(c));
}
faces_of.values().any(|fs| {
fs.len() == 2
&& fs[0] != fs[1]
&& (sides[fs[0].index()].is_none() || sides[fs[1].index()].is_none())
&& same_domain(&b.face(fs[0]).surface, &b.face(fs[1]).surface)
})
}
#[cfg(test)]
mod tests {
use std::f64::consts::{FRAC_PI_2, PI, SQRT_2};
use super::super::{FORCE, Method, chamfer, fillet};
use crate::{
Error, Result, check,
math::{Frame, Vec2, Vec3},
measure,
profile::{NamedSegment, Profile, ProfileLoop, Segment},
sweep::{extrude, revolve},
topo::Brep,
};
fn up() -> Frame {
Frame::new(Vec3::ZERO, Vec3::Z, Vec3::X).unwrap()
}
fn prism(profile: &Profile, h: f64, id: &str) -> Brep {
extrude(profile, &up(), h, id).unwrap()
}
fn block(w: f64, d: f64, h: f64) -> Brep {
prism(&Profile::rect(Vec2::new(w / 2.0, d / 2.0), w, d), h, "b")
}
fn with(method: Method, make: &dyn Fn() -> Result<Brep>) -> Result<Brep> {
FORCE.set(Some(method));
let out = make();
FORCE.set(None);
out
}
fn sewn_as_tools(make: &dyn Fn() -> Result<Brep>, volume: f64, area: f64) -> Brep {
let sewn = with(Method::Sewn, make).unwrap();
let tools = with(Method::Tools, make).unwrap();
assert!(check::check(&sewn).is_empty(), "{:?}", check::check(&sewn));
let (sa, sv) = measure::area_and_volume(&sewn);
let (ta, tv) = measure::area_and_volume(&tools);
assert!(
(sv - tv).abs() <= 1e-9 * tv,
"volume {sv} vs the tools' {tv}"
);
assert!((sa - ta).abs() <= 1e-9 * ta, "area {sa} vs the tools' {ta}");
assert!(
(sv - volume).abs() <= 1e-9 * volume,
"volume {sv} vs {volume}"
);
assert!((sa - area).abs() <= 1e-9 * area, "area {sa} vs {area}");
assert_eq!(sewn.name_counts(), tools.name_counts());
assert_eq!(sewn.face_count(), tools.face_count());
sewn
}
fn simpson_on(f: &dyn Fn(f64) -> f64, a: f64, b: f64) -> f64 {
let n = 2000;
let h = (b - a) / n as f64;
let inner: f64 = (1..n)
.map(|i| f(a + i as f64 * h) * if i % 2 == 1 { 4.0 } else { 2.0 })
.sum();
(f(a) + inner + f(b)) * h / 3.0
}
fn simpson(f: &dyn Fn(f64) -> f64, b: f64) -> f64 {
simpson_on(f, 0.0, b)
}
fn named(b: &Brep, name: &str) -> usize {
b.name_counts().get(name).copied().unwrap_or(0)
}
#[test]
fn a_straight_round_between_square_ends_is_sewn() {
let spandrel = 25.0 * (1.0 - PI / 4.0);
let b = sewn_as_tools(
&|| fillet(&block(40.0, 30.0, 20.0), &[("b.end", "b.v_min")], 5.0, "f"),
24000.0 - 40.0 * spandrel,
5200.0 - 400.0 + FRAC_PI_2 * 5.0 * 40.0 - 2.0 * spandrel,
);
assert_eq!(b.face_count(), 7);
assert_eq!(named(&b, "f.round_0"), 1);
}
#[test]
fn a_concave_round_adds_material_between_square_ends() {
let pts = [
(0.0, 0.0),
(40.0, 0.0),
(40.0, 10.0),
(10.0, 10.0),
(10.0, 30.0),
(0.0, 30.0),
]
.map(|(x, y)| Vec2::new(x, y));
let l = prism(
&Profile::polygon(&pts, &["a", "b", "c", "d", "e", "f"]),
20.0,
"l",
);
let spandrel = 4.0 * (1.0 - PI / 4.0);
let b = sewn_as_tools(
&|| fillet(&l, &[("l.c", "l.d")], 2.0, "f"),
600.0 * 20.0 + 20.0 * spandrel,
1200.0 + 140.0 * 20.0 - 80.0 + FRAC_PI_2 * 2.0 * 20.0 + 2.0 * spandrel,
);
assert_eq!(named(&b, "f.round_0"), 1);
}
#[test]
fn a_round_on_a_whole_circle_is_a_torus_with_its_own_seam() {
let (rr, r, h) = (10.0, 2.0, 10.0);
let disc = prism(&Profile::circle(Vec2::ZERO, 2.0 * rr), h, "d");
let spandrel = r * r * (1.0 - PI / 4.0);
let off = r * (10.0 - 3.0 * PI) / (12.0 - 3.0 * PI);
let arc = FRAC_PI_2 * r * 2.0 * PI * (rr - r * (1.0 - 2.0 / PI));
let b = sewn_as_tools(
&|| fillet(&disc, &[("d.end", "d.side")], r, "f"),
PI * rr * rr * h - spandrel * 2.0 * PI * (rr - off),
PI * rr * rr + PI * (rr - r).powi(2) + 2.0 * PI * rr * (h - r) + arc,
);
assert_eq!(b.face_count(), 4);
}
#[test]
fn mitred_chamfers_share_their_end_on_the_plane_between_them() {
let d = 2.0;
let b = sewn_as_tools(
&|| {
chamfer(
&block(40.0, 30.0, 20.0),
&[("b.end", "b.v_min"), ("b.end", "b.u_max")],
d,
"c",
)
},
24000.0 - d * d / 2.0 * (70.0 - 2.0 * d / 3.0),
1200.0
+ 38.0 * 28.0
+ 70.0 * 18.0
+ (600.0 + 800.0 - d * d)
+ d * SQRT_2 * ((80.0 - d) + (60.0 - d)) / 2.0,
);
assert_eq!(named(&b, "c.bevel_0"), 1);
assert_eq!(named(&b, "c.bevel_1"), 1);
}
#[test]
fn three_rounds_close_with_a_sewn_sphere_octant() {
let pairs = [
("b.end", "b.u_max"),
("b.end", "b.v_max"),
("b.u_max", "b.v_max"),
];
let spandrel = 16.0 * (1.0 - PI / 4.0);
let b = sewn_as_tools(
&|| fillet(&block(20.0, 20.0, 20.0), &pairs, 4.0, "f"),
8000.0 - 3.0 * 16.0 * spandrel - (64.0 - PI * 64.0 / 6.0),
2400.0 - 3.0 * 4.0 * 16.0 * 2.0 - 3.0 * 16.0 - 3.0 * spandrel
+ 3.0 * FRAC_PI_2 * 4.0 * 16.0
+ PI * 16.0 / 2.0,
);
assert_eq!(named(&b, "f.corner"), 1);
}
fn shoelace(p: &[Vec2]) -> f64 {
(0..p.len())
.map(|i| p[i].cross(p[(i + 1) % p.len()]))
.sum::<f64>()
/ 2.0
}
#[test]
fn three_rounds_at_a_sharp_corner_close_with_a_sphere_triangle() {
let (r, h, side, alpha) = (2.0_f64, 10.0, 20.0, PI / 3.0);
let t = r / (alpha / 2.0).tan();
let b_pt = Vec2::new(side * alpha.cos(), side * alpha.sin());
let d_pt = Vec2::new(side, side * (alpha / 2.0).tan());
let kite = [Vec2::ZERO, Vec2::new(side, 0.0), d_pt, b_pt];
let body = prism(&Profile::polygon(&kite, &["va", "ad", "db", "bv"]), h, "k");
let pairs = [("k.va", "k.bv"), ("k.end", "k.va"), ("k.end", "k.bv")];
let base = shoelace(&kite);
let s_v = r * t - r * r * (PI - alpha) / 2.0;
let s_q = r * r * (1.0 - PI / 4.0);
let omega = PI - alpha;
let cell = t * r * r - omega * r.powi(3) / 3.0;
let along_bd = (d_pt - b_pt) * (1.0 / d_pt.dist(b_pt));
let top = shoelace(&[
Vec2::new(t, r),
Vec2::new(side, r),
d_pt,
b_pt + along_bd * r,
]);
sewn_as_tools(
&|| fillet(&body, &pairs, r, "f"),
base * h - s_v * (h - r) - 2.0 * s_q * (side - t) - cell,
(base - s_v)
+ top
+ 2.0 * (side - t) * (h - r)
+ 2.0 * (d_pt.dist(b_pt) * h - s_q)
+ (PI - alpha) * r * (h - r)
+ 2.0 * FRAC_PI_2 * r * (side - t)
+ omega * r * r,
);
}
#[test]
fn a_round_where_a_hemisphere_meets_its_base_is_a_torus() {
let (rr, r) = (10.0_f64, 2.0_f64);
let c = ((rr - r).powi(2) - r * r).sqrt();
let th = (r / c).atan();
let s = th.sin();
let sphere_arc = rr.powi(3) / 2.0 * (s - s.powi(3) / 3.0);
let round_arc = -r / 2.0
* (c * c * (s + 1.0)
+ 2.0 * c * r * ((th + FRAC_PI_2) / 2.0 + (2.0 * th).sin() / 4.0)
+ r * r * ((s - s.powi(3) / 3.0) + 2.0 / 3.0));
let quarter = Profile {
loops: vec![ProfileLoop {
segments: vec![
NamedSegment::new(
Segment::Line {
a: Vec2::ZERO,
b: Vec2::new(rr, 0.0),
},
"base",
),
NamedSegment::new(
Segment::Arc {
center: Vec2::ZERO,
radius: rr,
start: 0.0,
sweep: FRAC_PI_2,
},
"side",
),
NamedSegment::new(
Segment::Line {
a: Vec2::new(0.0, rr),
b: Vec2::ZERO,
},
"axis",
),
],
}],
};
let frame = Frame::new(Vec3::ZERO, -Vec3::Y, Vec3::X).unwrap();
let dome = revolve(&quarter, &frame, Vec3::ZERO, Vec3::Z, 2.0 * PI, "h").unwrap();
let torus = 2.0 * PI * r * (c * (th + FRAC_PI_2) + r * (s + 1.0));
let b = sewn_as_tools(
&|| fillet(&dome, &[("h.side", "h.base")], r, "f"),
2.0 / 3.0 * PI * rr.powi(3) - 2.0 * PI * (sphere_arc + round_arc),
3.0 * PI * rr * rr - 2.0 * PI * rr * rr * s - PI * (rr * rr - c * c) + torus,
);
assert_eq!(b.face_count(), 3);
}
#[test]
fn a_round_runs_past_a_chamfers_short_edge_onto_the_top() {
let body = chamfer(&block(40.0, 30.0, 20.0), &[("b.end", "b.v_min")], 1.0, "c").unwrap();
let g = |phi: f64| 2.0 - 2.0 * phi.sin();
let dw = |phi: f64| 2.0 * phi.sin();
let w = |phi: f64| 2.0 - 2.0 * phi.cos();
let (mid, end) = (PI / 3.0, FRAC_PI_2);
let spandrel = 4.0 * (1.0 - PI / 4.0);
let under_bevel = simpson_on(&|p| g(p) * dw(p), 0.0, mid);
let under_top = simpson_on(&|p| g(p) * dw(p), mid, end);
let rise = simpson_on(&|p| w(p) * g(p) * dw(p), 0.0, mid) + under_top;
let round = 2.0 * (19.0 * end + simpson_on(&w, 0.0, mid) + (end - mid));
sewn_as_tools(
&|| fillet(&body, &[("b.u_max", "b.v_min")], 2.0, "f"),
24000.0 - 20.0 - 19.0 * spandrel - rise,
5200.0 - 80.0 + 40.0 * SQRT_2
- 1.0
- under_top
- SQRT_2 * under_bevel
- 39.5
- 38.0
- spandrel
+ round,
);
}
#[test]
fn a_chamfer_into_an_inner_corner_opens_the_wall_past_it() {
let pts = [
(0.0, 0.0),
(40.0, 0.0),
(40.0, 10.0),
(10.0, 10.0),
(10.0, 30.0),
(0.0, 30.0),
]
.map(|(x, y)| Vec2::new(x, y));
let l = prism(
&Profile::polygon(&pts, &["a", "b", "c", "d", "e", "f"]),
20.0,
"l",
);
sewn_as_tools(
&|| chamfer(&l, &[("l.end", "l.c")], 1.0, "c"),
12000.0 - 15.0,
4000.0 - 60.0 + 30.0 * SQRT_2,
);
}
fn teardrop(r: f64) -> Profile {
let c = Vec2::new(r * SQRT_2, 0.0);
let t = r / SQRT_2;
let (t0, t1) = (Vec2::new(t, -t), Vec2::new(t, t));
let seg = |segment, name: &str| NamedSegment::new(segment, name);
Profile {
loops: vec![ProfileLoop {
segments: vec![
seg(
Segment::Line {
a: Vec2::ZERO,
b: t0,
},
"s0",
),
seg(
Segment::Arc {
center: c,
radius: r,
start: -0.75 * PI,
sweep: 1.5 * PI,
},
"arc",
),
seg(
Segment::Line {
a: t1,
b: Vec2::ZERO,
},
"s1",
),
],
}],
}
}
#[test]
fn a_chain_that_closes_on_itself_at_a_mitre() {
let (r, h, d) = (10.0, 5.0, 2.0);
let body = prism(&teardrop(r), h, "t");
let shape = 1.0 + 0.75 * PI;
let along = 2.0 + 1.5 * PI;
let b = sewn_as_tools(
&|| chamfer(&body, &[("t.end", "t.arc")], d, "c"),
r * r * shape * h - d * d / 2.0 * (r - d / 3.0) * along,
(r * r + (r - d).powi(2)) * shape
+ r * along * (h - d)
+ d * SQRT_2 * (r - d / 2.0) * along,
);
assert_eq!(named(&b, "c.bevel_0"), 3);
}
#[test]
fn a_round_chain_that_closes_on_itself_at_a_mitre() {
let (rr, h, r) = (10.0, 5.0, 2.0);
let body = prism(&teardrop(rr), h, "t");
let shape = 1.0 + 0.75 * PI;
let along = 2.0 + 1.5 * PI;
let inset = r * (10.0 - 3.0 * PI) / (12.0 - 3.0 * PI);
let b = sewn_as_tools(
&|| fillet(&body, &[("t.end", "t.arc")], r, "f"),
rr * rr * shape * h - r * r * (1.0 - PI / 4.0) * (rr - inset) * along,
(rr * rr + (rr - r).powi(2)) * shape
+ rr * along * (h - r)
+ FRAC_PI_2 * r * (rr - r * (1.0 - 2.0 / PI)) * along,
);
assert_eq!(named(&b, "f.round_0"), 3);
}
fn sewn_refused(make: &dyn Fn() -> Result<Brep>, why: &str, volume: f64, faces: usize) {
match with(Method::Sewn, make) {
Err(Error::Unsupported(m)) => assert!(m.contains(why), "{m}"),
other => panic!("sewn: {:?}", other.map(|b| b.face_count())),
}
let b = make().unwrap();
assert!(check::check(&b).is_empty(), "{:?}", check::check(&b));
let v = measure::volume(&b);
assert!(
(v - volume).abs() <= 1e-9 * volume,
"volume {v} vs {volume}"
);
assert_eq!(b.face_count(), faces);
}
#[test]
fn a_hole_wholly_inside_the_removed_wedge_goes_with_it() {
let frame = Frame::new(Vec3::new(-1.0, 1.2, 18.8), Vec3::X, Vec3::Y).unwrap();
let hole = extrude(&Profile::circle(Vec2::ZERO, 1.6), &frame, 42.0, "h").unwrap();
let body = crate::boolean::difference(&block(40.0, 30.0, 20.0), &hole).unwrap();
let stray = "lies outside the region its other loops bound";
sewn_refused(
&|| chamfer(&body, &[("b.end", "b.v_min")], 4.0, "c"),
stray,
24000.0 - 40.0 * 8.0,
7,
);
sewn_refused(
&|| fillet(&body, &[("b.end", "b.v_min")], 8.0, "f"),
stray,
24000.0 - 40.0 * 64.0 * (1.0 - PI / 4.0),
7,
);
}
#[test]
fn a_void_wholly_inside_the_removed_wedge_goes_with_it() {
let at = Frame::new(Vec3::new(0.0, 0.0, 18.5), Vec3::Z, Vec3::X).unwrap();
let void = extrude(
&Profile::rect(Vec2::new(20.0, 1.0), 1.0, 1.0),
&at,
1.0,
"v",
)
.unwrap();
let body = crate::boolean::difference(&block(40.0, 30.0, 20.0), &void).unwrap();
let stray = "lies outside its outer shell";
sewn_refused(
&|| chamfer(&body, &[("b.end", "b.v_min")], 4.0, "c"),
stray,
24000.0 - 40.0 * 8.0,
7,
);
sewn_refused(
&|| fillet(&body, &[("b.end", "b.v_min")], 8.0, "f"),
stray,
24000.0 - 40.0 * 64.0 * (1.0 - PI / 4.0),
7,
);
}
#[test]
fn rounds_that_cross_at_a_crease_are_sewn_along_it() {
let half = Profile {
loops: vec![ProfileLoop {
segments: vec![
NamedSegment::new(
Segment::Line {
a: Vec2::new(-10.0, 0.0),
b: Vec2::new(10.0, 0.0),
},
"flat",
),
NamedSegment::new(
Segment::Arc {
center: Vec2::ZERO,
radius: 10.0,
start: 0.0,
sweep: PI,
},
"round",
),
],
}],
};
let body = prism(&half, 5.0, "h");
let pairs = [("h.end", "h.flat"), ("h.end", "h.round")];
let make = || fillet(&body, &pairs, 1.0, "f");
let seg = |rho: f64, d: f64| rho * rho * (d / rho).acos() - d * (rho * rho - d * d).sqrt();
let delta = |phi: f64| 1.0 - phi.sin();
let rho = |phi: f64| 10.0 - delta(phi);
let cut = simpson(
&|phi| (50.0 * PI - seg(rho(phi), delta(phi))) * phi.sin(),
FRAC_PI_2,
);
let straight = simpson(
&|phi| 2.0 * (rho(phi).powi(2) - delta(phi).powi(2)).sqrt(),
FRAC_PI_2,
);
let ring = simpson(
&|phi| (PI - 2.0 * (delta(phi) / rho(phi)).asin()) * rho(phi),
FRAC_PI_2,
);
let b = sewn_as_tools(
&make,
250.0 * PI - cut,
seg(9.0, 1.0) + 50.0 * PI + 80.0 + 40.0 * PI + straight + ring,
);
assert_eq!(b.face_count(), 6);
}
}