use rustc_hash::FxHashMap as HashMap;
use super::geometry::{self, Curve, Frame, Surface};
use super::reader::{Node, XtFile};
use crate::{ImportedColor, NurbsCurve, NurbsSurface, Vec3};
use super::geometry::MM_PER_METRE;
fn real(value: f64) -> String {
let text = format!("{value:E}");
if text.contains('.') {
text
} else {
text.replacen('E', ".E", 1)
}
}
pub struct BodyStep {
pub step: String,
pub name: Option<String>,
pub color: Option<ImportedColor>,
pub fitted_curves: usize,
pub unverified_curves: usize,
}
fn attributes<'f>(file: &'f XtFile, node: &Node) -> Vec<(&'f str, Option<&'f Node>)> {
let mut out = Vec::new();
let mut at = node.ptr("attributes_features");
let mut guard = 0;
while let Some(attribute) = file.node(at) {
guard += 1;
if guard > 10_000 {
break;
}
if attribute.type_name() == "ATTRIBUTE" {
let name = file
.follow(attribute, "definition")
.and_then(|d| file.follow(d, "identifier"))
.and_then(|id| id.text("string"))
.unwrap_or("");
let first = attribute.ints("fields").first().and_then(|&i| u32::try_from(i).ok()).and_then(|i| file.node(i));
out.push((name, first));
}
at = attribute.ptr("next");
}
out
}
fn color_of(file: &XtFile, node: &Node, definition: &str) -> Option<ImportedColor> {
attributes(file, node).into_iter().find(|(name, _)| *name == definition).and_then(|(_, field)| {
let rgb = field?.reals("values");
(rgb.len() >= 3 && rgb[..3].iter().all(|c| c.is_finite() && (0.0..=1.0).contains(c)))
.then(|| ImportedColor { r: rgb[0], g: rgb[1], b: rgb[2] })
})
}
pub fn name_of(file: &XtFile, node: &Node) -> Option<String> {
for (name, field) in attributes(file, node) {
let Some(field) = field else { continue };
let text = match name {
"SDL/TYSA_NAME" => field.text("values").map(str::to_string),
"SDL/TYSA_UNAME" => {
let units: Vec<u16> = field.ints("values").into_iter().map(|c| c as u16).collect();
String::from_utf16(&units).ok()
}
_ => None,
};
if let Some(text) = text.map(|t| t.trim_end_matches('\0').trim().to_string()).filter(|t| !t.is_empty()) {
return Some(text);
}
}
None
}
pub(super) fn runs_forward(file: &XtFile, node: &Node) -> bool {
let mut forward = node.char("sense") != Some(b'-');
let mut at = node;
for _ in 0..8 {
if at.type_name() != "TRIMMED_CURVE" {
break;
}
let Some(basis) = file.follow(at, "basis_curve") else { break };
if basis.char("sense") == Some(b'-') {
forward = !forward;
}
at = basis;
}
forward
}
pub(super) fn chain<'f>(file: &'f XtFile, first: u32, next: &'static str) -> impl Iterator<Item = &'f Node> + 'f {
let mut at = first;
let mut seen = rustc_hash::FxHashSet::default();
std::iter::from_fn(move || {
let node = file.node(at)?;
if !seen.insert(at) {
return None;
}
at = node.ptr(next);
Some(node)
})
}
struct EmittedSurface {
id: usize,
flipped: bool,
}
struct Translator<'a> {
file: &'a XtFile,
text: String,
next_id: usize,
vertices: HashMap<u32, usize>,
edges: HashMap<u32, usize>,
surfaces: HashMap<u32, usize>,
emitted: Vec<EmittedSurface>,
fitted_curves: usize,
unverified_curves: usize,
edge_ends: Option<(Vec3, Vec3)>,
edge_end_tolerances: Vec<(Vec3, f64)>,
anchor: Option<(Vec3, Vec3)>,
spines: HashMap<u32, Result<Curve, String>>,
read_blend_sp: bool,
}
pub fn body_to_step(file: &XtFile, body: &Node, tag: usize) -> Result<BodyStep, String> {
let mut t = Translator {
file,
text: String::new(),
next_id: 1,
vertices: HashMap::default(),
edges: HashMap::default(),
surfaces: HashMap::default(),
emitted: Vec::new(),
fitted_curves: 0,
unverified_curves: 0,
edge_ends: None,
edge_end_tolerances: Vec::new(),
anchor: None,
spines: HashMap::default(),
read_blend_sp: false,
};
t.body(body)?;
let loosest = file
.nodes()
.iter()
.filter(|n| matches!(n.type_name(), "EDGE" | "VERTEX"))
.filter_map(|n| n.real("tolerance"))
.chain(body.real("res_linear"))
.filter(|t| t.is_finite() && *t > 0.0)
.fold(0.0f64, f64::max);
if loosest > 0.0 {
let unit = t.add("(LENGTH_UNIT()NAMED_UNIT(*)SI_UNIT(.MILLI.,.METRE.))".into());
t.add(format!(
"UNCERTAINTY_MEASURE_WITH_UNIT(LENGTH_MEASURE({}),#{unit},'distance_accuracy_value','Parasolid tolerance')",
real(loosest * MM_PER_METRE)
));
}
let document =format!("ISO-10303-21;\nHEADER;\nENDSEC;\nDATA;\n{}ENDSEC;\nEND-ISO-10303-21;\n", t.text);
if let Ok(path) = std::env::var("BREP_DEBUG_XT_STEP") {
let _ = std::fs::write(format!("{path}.{tag}"), &document);
}
Ok(BodyStep {
step: document,
name: name_of(file, body),
color: color_of(file, body, "SDL/TYSA_COLOUR_2").or_else(|| color_of(file, body, "SDL/TYSA_COLOUR")),
fitted_curves: t.fitted_curves,
unverified_curves: t.unverified_curves,
})
}
impl<'a> Translator<'a> {
fn add(&mut self, body: String) -> usize {
let id = self.next_id;
self.next_id += 1;
self.text.push_str(&format!("#{id}={body};\n"));
id
}
fn point_id(&mut self, p: Vec3) -> usize {
self.add(format!("CARTESIAN_POINT('',({},{},{}))", real(p.x), real(p.y), real(p.z)))
}
fn direction_id(&mut self, d: Vec3) -> usize {
self.add(format!("DIRECTION('',({},{},{}))", real(d.x), real(d.y), real(d.z)))
}
fn placement_id(&mut self, origin: Vec3, z: Vec3, x: Vec3) -> usize {
let origin = self.point_id(origin);
let z = self.direction_id(z);
let x = self.direction_id(x);
self.add(format!("AXIS2_PLACEMENT_3D('',#{origin},#{z},#{x})"))
}
fn node(&self, index: u32, what: &str) -> Result<&'a Node, String> {
self.file.node(index).ok_or_else(|| format!("{what} {index} is missing"))
}
fn body(&mut self, body: &Node) -> Result<(), String> {
let body_type = body.int("body_type").unwrap_or(0);
let regions: Vec<&Node> = chain(self.file, body.ptr("region"), "next").collect();
match body_type {
1 => {
let mut solids = Vec::new();
for region in regions.iter().filter(|r| r.char("type") == Some(b'S')) {
let shells: Vec<&Node> = chain(self.file, region.ptr("shell"), "next").collect();
if shells.is_empty() {
continue;
}
let mut emitted = Vec::new();
for shell in &shells {
let faces: Vec<&Node> = chain(self.file, shell.ptr("face"), "next").collect();
if faces.is_empty() {
return Err(format!("solid shell {} has no faces", shell.index));
}
let mut ids = Vec::with_capacity(faces.len());
for face in faces {
ids.push(self.face(face)?);
}
let bounds = self.shell_box(shell);
emitted.push((ids, bounds));
}
let holds = |outer: &([f64; 3], [f64; 3]), inner: &([f64; 3], [f64; 3])| {
let slack = 1e-6;
(0..3).all(|k| outer.0[k] <= inner.0[k] + slack && inner.1[k] <= outer.1[k] + slack)
};
let diagonal = |b: &([f64; 3], [f64; 3])| (0..3).map(|k| (b.1[k] - b.0[k]).max(0.0).powi(2)).sum::<f64>();
let outer = (0..emitted.len())
.find(|&i| (0..emitted.len()).all(|j| j == i || holds(&emitted[i].1, &emitted[j].1)))
.or_else(|| {
(0..emitted.len()).max_by(|&a, &b| {
diagonal(&emitted[a].1).partial_cmp(&diagonal(&emitted[b].1)).unwrap_or(std::cmp::Ordering::Equal)
})
})
.unwrap_or(0);
let mut shell_ids = Vec::new();
for (ids, _) in &emitted {
let list: Vec<String> = ids.iter().map(|id| format!("#{id}")).collect();
shell_ids.push(self.add(format!("CLOSED_SHELL('',({}))", list.join(","))));
}
let solid = if emitted.len() == 1 {
self.add(format!("MANIFOLD_SOLID_BREP('',#{})", shell_ids[0]))
} else {
let voids: Vec<String> = (0..shell_ids.len())
.filter(|&i| i != outer)
.map(|i| format!("#{}", self.add(format!("ORIENTED_CLOSED_SHELL('',*,#{},.F.)", shell_ids[i]))))
.collect();
self.add(format!("BREP_WITH_VOIDS('',#{},({}))", shell_ids[outer], voids.join(",")))
};
solids.push(solid);
}
if solids.is_empty() {
return Err("the solid body has no solid region".into());
}
self.representation("ADVANCED_BREP_SHAPE_REPRESENTATION", &solids);
}
3 => {
let mut shells = Vec::new();
let mut seen = rustc_hash::FxHashSet::default();
for region in ®ions {
for shell in chain(self.file, region.ptr("shell"), "next") {
let mut ids = Vec::new();
let back = chain(self.file, shell.ptr("face"), "next");
let front: Vec<&Node> = chain(self.file, shell.ptr("front_face"), "next_front").collect();
for face in back.chain(front) {
if seen.insert(face.index) {
ids.push(self.face(face)?);
}
}
if ids.is_empty() {
continue;
}
let list: Vec<String> = ids.iter().map(|id| format!("#{id}")).collect();
shells.push(self.add(format!("OPEN_SHELL('',({}))", list.join(","))));
}
}
if shells.is_empty() {
return Err("the sheet body has no faces".into());
}
let list: Vec<String> = shells.iter().map(|id| format!("#{id}")).collect();
let model = self.add(format!("SHELL_BASED_SURFACE_MODEL('',({}))", list.join(",")));
self.representation("MANIFOLD_SURFACE_SHAPE_REPRESENTATION", &[model]);
}
2 => {
let mut sets = Vec::new();
for region in ®ions {
for shell in chain(self.file, region.ptr("shell"), "next") {
let mut ids = Vec::new();
for edge in chain(self.file, shell.ptr("edge"), "next") {
ids.push(self.edge(edge.index)?);
}
if ids.is_empty() {
continue;
}
let list: Vec<String> = ids.iter().map(|id| format!("#{id}")).collect();
sets.push(self.add(format!("CONNECTED_EDGE_SET('',({}))", list.join(","))));
}
}
if sets.is_empty() {
return Err("the wire body has no edges".into());
}
let list: Vec<String> = sets.iter().map(|id| format!("#{id}")).collect();
let model = self.add(format!("EDGE_BASED_WIREFRAME_MODEL('',({}))", list.join(",")));
self.representation("EDGE_BASED_WIREFRAME_SHAPE_REPRESENTATION", &[model]);
}
6 => return Err("the body is a general (non-manifold) body, which is not read yet".into()),
other => return Err(format!("the body has type {other}, which is not a Parasolid body type")),
}
Ok(())
}
fn representation(&mut self, kind: &str, items: &[usize]) {
let list: Vec<String> = items.iter().map(|id| format!("#{id}")).collect();
self.add(format!("{kind}('',({}),$)", list.join(",")));
}
fn shell_box(&self, shell: &Node) -> ([f64; 3], [f64; 3]) {
let mut lo = [f64::INFINITY; 3];
let mut hi = [f64::NEG_INFINITY; 3];
let mut include = |p: Vec3| {
for (k, c) in [p.x, p.y, p.z].into_iter().enumerate() {
lo[k] = lo[k].min(c);
hi[k] = hi[k].max(c);
}
};
for face in chain(self.file, shell.ptr("face"), "next") {
let edgeless = chain(self.file, face.ptr("loop"), "next").all(|lp| self.fins(lp).iter().all(|fin| fin.ptr("edge") == 0));
if edgeless {
let surface = self.file.follow(face, "surface").and_then(|s| geometry::surface(self.file, s, 0).ok());
let reach = match surface {
Some(Surface::Sphere { frame, radius }) => Some((frame.origin, radius)),
Some(Surface::Torus { frame, major, minor }) => Some((frame.origin, major + minor)),
_ => None,
};
if let Some((centre, r)) = reach {
include(centre.sub(Vec3::new(r, r, r)));
include(centre.add(Vec3::new(r, r, r)));
}
}
for lp in chain(self.file, face.ptr("loop"), "next") {
for fin in self.fins(lp) {
if let Ok(p) = self.vertex_location(fin.ptr("vertex")) {
include(p);
}
if let Some(edge) = self.file.follow(fin, "edge") {
for p in self.edge_samples(edge) {
include(p);
}
}
}
}
}
(lo, hi)
}
fn edge_samples(&self, edge: &Node) -> Vec<Vec3> {
let Some(curve) = self.file.follow(edge, "curve").and_then(|c| geometry::curve(self.file, c).ok()) else {
return Vec::new();
};
let ends = edge_fins(self.file, edge).ok().and_then(|(positive, negative)| {
Some((self.vertex_location(negative.ptr("vertex")).ok()?, self.vertex_location(positive.ptr("vertex")).ok()?))
});
let forward = self.file.follow(edge, "curve").map_or(true, |c| runs_forward(self.file, c));
let range = match (&curve, ends) {
(Curve::Line { .. }, _) => return Vec::new(),
(Curve::Circle { frame, .. } | Curve::Ellipse { frame, .. }, Some((start, end))) => {
let (major, minor) = match curve {
Curve::Ellipse { major, minor, .. } => (major, minor),
_ => (1.0, 1.0),
};
let angle = |p: Vec3| {
let d = p.sub(frame.origin);
(d.dot(frame.y()) / minor).atan2(d.dot(frame.x) / major)
};
let (a, b) = (angle(start), angle(end));
let sweep = if forward { (b - a).rem_euclid(std::f64::consts::TAU) } else { -(a - b).rem_euclid(std::f64::consts::TAU) };
let sweep = if sweep == 0.0 { std::f64::consts::TAU } else { sweep };
[a, a + sweep]
}
(Curve::Nurbs(nurbs), Some((start, end))) => {
let at = |p: Vec3| crate::project_point_to_curve(nurbs, p).map(|f| f.u).ok();
match (at(start), at(end)) {
(Some(a), Some(b)) if a != b => [a, b],
_ => match curve.natural_range() {
Some(r) => r,
None => return Vec::new(),
},
}
}
(_, None) => match curve.natural_range() {
Some(r) => r,
None => return Vec::new(),
},
};
(0..=16).filter_map(|k| curve.point(range[0] + (range[1] - range[0]) * k as f64 / 16.0).ok()).collect()
}
fn fins(&self, lp: &Node) -> Vec<&'a Node> {
let start = lp.ptr("halfedge");
let mut out = Vec::new();
let mut at = start;
while let Some(fin) = self.file.node(at) {
out.push(fin);
at = fin.ptr("forward");
if at == start || out.len() > 1_000_000 {
break;
}
}
out
}
fn face(&mut self, face: &Node) -> Result<usize, String> {
let face_id = face.int("node_id").unwrap_or(face.index as i64);
let surface_index = face.ptr("surface");
let surface_node = self.node(surface_index, "the surface of a face").map_err(|e| format!("face {face_id}: {e}"))?;
let emitted = if surface_node.type_name() == "BLENDED_EDGE" {
self.blend_surface(face, surface_node)
} else {
self.surface(surface_node)
}
.map_err(|e| format!("face {face_id}: {e}"))?;
let flipped = self.emitted[emitted].flipped;
let step_surface = self.emitted[emitted].id;
let agrees = (face.char("sense") != Some(b'-')) == (surface_node.char("sense") != Some(b'-'));
let same_sense = agrees != flipped;
let mut bounds = Vec::new();
for lp in chain(self.file, face.ptr("loop"), "next") {
let fins = self.fins(lp);
let loop_id = if fins.len() == 1 && fins[0].ptr("edge") == 0 {
let vertex = self.vertex(fins[0].ptr("vertex")).map_err(|e| format!("face {face_id}: {e}"))?;
self.add(format!("VERTEX_LOOP('',#{vertex})"))
} else {
let mut members = Vec::with_capacity(fins.len());
for fin in fins {
let edge = self.edge(fin.ptr("edge")).map_err(|e| format!("face {face_id}: {e}"))?;
let sense = if fin.char("sense") == Some(b'-') { ".F." } else { ".T." };
members.push(format!("#{}", self.add(format!("ORIENTED_EDGE('',*,*,#{edge},{sense})"))));
}
self.add(format!("EDGE_LOOP('',({}))", members.join(",")))
};
bounds.push(format!("#{}", self.add(format!("FACE_BOUND('',#{loop_id},.T.)"))));
}
if bounds.is_empty() {
let surface = geometry::surface(self.file, surface_node, 0).map_err(|e| format!("face {face_id}: {e}"))?;
let loop_id = self.whole_surface_loop(&surface, same_sense).map_err(|e| format!("face {face_id}: {e}"))?;
bounds.push(format!("#{}", self.add(format!("FACE_OUTER_BOUND('',#{loop_id},.T.)"))));
}
let id = self.add(format!(
"ADVANCED_FACE('',({}),#{step_surface},{})",
bounds.join(","),
if same_sense { ".T." } else { ".F." }
));
if let Some(color) = color_of(self.file, face, "SDL/TYSA_COLOUR") {
self.face_style(id, color);
}
Ok(id)
}
fn face_boundary_points(&self, face: &Node) -> Vec<Vec3> {
let mut points = Vec::new();
for lp in chain(self.file, face.ptr("loop"), "next") {
for fin in self.fins(lp) {
if let Ok(p) = self.vertex_location(fin.ptr("vertex")) {
points.push(p);
}
let Some(edge) = self.file.follow(fin, "edge") else { continue };
points.extend(self.edge_samples(edge));
let mut curve = self.file.follow(edge, "curve");
for _ in 0..8 {
match curve {
Some(c) if c.type_name() == "TRIMMED_CURVE" => curve = self.file.follow(c, "basis_curve"),
_ => break,
}
}
if let Some(c) = curve.filter(|c| c.type_name() == "INTERSECTION") {
points.extend(self.intersection_points(c).unwrap_or_default());
}
}
}
points
}
fn blend_supports(&mut self, node: &Node, depth: usize) -> Result<([Support; 2], f64), String> {
let file = self.file;
let id = node.int("node_id").unwrap_or(node.index as i64);
if depth > 4 {
return Err(format!("blend {id}: blends rest on blends more than 4 deep"));
}
let cliff = match node.char("blend_type") {
Some(b'R') => false,
Some(b'E') => true,
_ => return Err(format!("blend {id} has a blend type that is neither rolling-ball nor cliff-edge")),
};
let radius = node.reals("range").iter().map(|r| r.abs()).fold(0.0f64, f64::max) * MM_PER_METRE;
if !(radius > 0.0 && radius.is_finite()) {
return Err(format!("blend {id} has no radius"));
}
let nodes: Vec<&Node> = node.ints("surface").into_iter().filter_map(|i| u32::try_from(i).ok()).filter_map(|i| file.node(i)).collect();
let [first, second]: [&Node; 2] = nodes.try_into().map_err(|_| format!("blend {id} does not have two supporting surfaces"))?;
let mut read = |s: &Node| -> Result<Support, String> {
if s.type_name() != "BLENDED_EDGE" {
return Ok(Support::Surface(geometry::surface(file, s, 0)?));
}
let support_id = s.int("node_id").unwrap_or(s.index as i64);
if cliff && s.reals("range").iter().all(|r| *r == 0.0) {
let edge = file.follow(s, "spine").ok_or_else(|| format!("blend {id}'s cliff edge has no curve"))?;
return Ok(Support::Cliff(self.spine_curve(edge).map_err(|e| format!("blend {id}: cliff edge: {e}"))?));
}
let (inner, tube) = self.blend_supports(s, depth + 1).map_err(|e| format!("blend {id}: supporting blend {support_id}: {e}"))?;
let spine = file.follow(s, "spine").ok_or_else(|| format!("blend {id}'s supporting blend {support_id} has no spine"))?;
let spine = self.spine_curve(spine).map_err(|e| format!("blend {id}: supporting blend {support_id}: spine: {e}"))?;
let inner = Some(Box::new((inner, tube)));
Ok(Support::Pipe { spine, radius: tube, inner })
};
let supports = [read(first)?, read(second)?];
if cliff && !supports.iter().any(|s| matches!(s, Support::Cliff(_))) {
return Err(format!("blend {id} is a cliff-edge blend with no cliff edge among its supports"));
}
Ok((supports, radius))
}
fn blend_surface(&mut self, face: &Node, node: &Node) -> Result<usize, String> {
let file = self.file;
let id = node.int("node_id").unwrap_or(node.index as i64);
let (mut supports, radius) = self.blend_supports(node, 0)?;
let spine_node = file.follow(node, "spine").ok_or_else(|| format!("blend {id} has no spine"))?;
if spine_node.char("sense") == Some(b'-') {
supports.swap(0, 1);
}
let spine = self.spine_curve(spine_node).map_err(|e| format!("blend {id}: spine: {e}"))?;
let points = self.face_boundary_points(face);
let section = |t: f64| blend_section(&supports, radius, &spine, t);
let pieces = |t0: f64, t1: f64| -> Option<NurbsSurface> {
let breaks = support_knot_crossings(&supports, §ion, t0, t1);
fit_blend_pieces(§ion, t0, t1, &breaks).ok()
};
let fit = |extend: bool| -> Result<(NurbsSurface, [f64; 2]), String> {
let [t0, t1] = spine_range(&spine, &points, 1.0 / 50.0, extend).ok_or_else(|| "the face does not lie along its spine".to_string())?;
let surface = match fit_blend(§ion, t0, t1) {
Ok(surface) => surface,
Err(e) => match pieces(t0, t1) {
Some(surface) => surface,
None => {
let [a, b] = spine_range(&spine, &points, 0.0, extend).ok_or_else(|| e.clone())?;
pieces(a, b).ok_or(e)?
}
},
};
Ok((surface, [t0, t1]))
};
let (surface, [t0, t1]) = match fit(false) {
Ok(fitted) => fitted,
Err(e) => fit(true).map_err(|_| format!("blend {id}: {e}"))?,
};
self.fitted_curves += 1;
let step = self.emit_nurbs_surface(&surface);
let flipped = [0.5, 0.3, 0.7, 0.1, 0.9]
.into_iter()
.find_map(|f| {
let t = t0 + (t1 - t0) * f;
let away = surface.evaluate(t, 0.5).ok()?.sub(geometry::extended_point(&spine, t).ok()?);
Some(surface.normal(t, 0.5).ok()?.dot(away) < 0.0)
})
.ok_or_else(|| format!("blend {id}: no regular point to orient it by"))?;
self.emitted.push(EmittedSurface { id: step, flipped });
Ok(self.emitted.len() - 1)
}
fn spine_curve(&mut self, node: &Node) -> Result<Curve, String> {
if let Some(curve) = self.spines.get(&node.index) {
return curve.clone();
}
let curve = self.spine_curve_uncached(node);
self.spines.insert(node.index, curve.clone());
curve
}
fn spine_curve_uncached(&mut self, node: &Node) -> Result<Curve, String> {
let mut node = node;
for _ in 0..8 {
if node.type_name() != "TRIMMED_CURVE" {
break;
}
node = self.file.follow(node, "basis_curve").ok_or("a trimmed curve has no basis curve")?;
}
if node.type_name() == "INTERSECTION" {
let chart = uncrowd_chart_ends(self.intersection_points(node)?, [RESOLUTION_MM; 2]);
let crossing = self.intersection_surfaces(node).map(|[a, b]| Crossing::Surfaces(a, b));
let stations = ChartStations::new(crossing.as_ref(), &chart);
let (fitted, fell_back) = stations.fit()?;
if fell_back || stations.refined(2).len() == chart.len() {
self.unverified_curves += 1;
}
return Ok(Curve::Nurbs(fitted));
}
geometry::curve(self.file, node)
}
fn whole_surface_loop(&mut self, surface: &Surface, same_sense: bool) -> Result<usize, String> {
let mut members: Vec<(usize, bool)> = match surface {
Surface::Sphere { frame, radius } => {
let south = self.point_id(frame.origin.sub(frame.z.scale(*radius)));
let south = self.add(format!("VERTEX_POINT('',#{south})"));
let north = self.point_id(frame.origin.add(frame.z.scale(*radius)));
let north = self.add(format!("VERTEX_POINT('',#{north})"));
let meridian = Curve::Circle {
frame: Frame { origin: frame.origin, z: frame.y().scale(-1.0), x: frame.x },
radius: *radius,
};
let curve = self.emit_curve(&meridian)?;
let seam = self.add(format!("EDGE_CURVE('',#{south},#{north},#{curve},.T.)"));
vec![(seam, true), (seam, false)]
}
Surface::Torus { major, .. } if *major < 0.0 => {
return Err("a whole lemon torus face (no edges) is not read yet".into());
}
Surface::Torus { frame, major, minor } => {
let corner = self.point_id(frame.origin.add(frame.x.scale(major + minor)));
let corner = self.add(format!("VERTEX_POINT('',#{corner})"));
let around = Curve::Circle { frame: *frame, radius: major + minor };
let tube = Curve::Circle {
frame: Frame { origin: frame.origin.add(frame.x.scale(*major)), z: frame.y().scale(-1.0), x: frame.x },
radius: *minor,
};
let around = self.emit_curve(&around)?;
let around = self.add(format!("EDGE_CURVE('',#{corner},#{corner},#{around},.T.)"));
let tube = self.emit_curve(&tube)?;
let tube = self.add(format!("EDGE_CURVE('',#{corner},#{corner},#{tube},.T.)"));
vec![(around, true), (tube, true), (around, false), (tube, false)]
}
_ => return Err("a face with no loop is not a whole sphere or torus, which is not read yet".into()),
};
if !same_sense {
members.reverse();
for member in &mut members {
member.1 = !member.1;
}
}
let oriented: Vec<String> = members
.into_iter()
.map(|(edge, forward)| format!("#{}", self.add(format!("ORIENTED_EDGE('',*,*,#{edge},{})", if forward { ".T." } else { ".F." }))))
.collect();
Ok(self.add(format!("EDGE_LOOP('',({}))", oriented.join(","))))
}
fn face_style(&mut self, face: usize, color: ImportedColor) {
let rgb = self.add(format!("COLOUR_RGB('',{},{},{})", real(color.r), real(color.g), real(color.b)));
let fill_colour = self.add(format!("FILL_AREA_STYLE_COLOUR('',#{rgb})"));
let fill = self.add(format!("FILL_AREA_STYLE('',(#{fill_colour}))"));
let area = self.add(format!("SURFACE_STYLE_FILL_AREA(#{fill})"));
let side = self.add(format!("SURFACE_SIDE_STYLE('',(#{area}))"));
let usage = self.add(format!("SURFACE_STYLE_USAGE(.BOTH.,#{side})"));
let assignment = self.add(format!("PRESENTATION_STYLE_ASSIGNMENT((#{usage}))"));
self.add(format!("STYLED_ITEM('',(#{assignment}),#{face})"));
}
fn vertex_location(&self, index: u32) -> Result<Vec3, String> {
let vertex = self.node(index, "vertex")?;
let point = self.file.follow(vertex, "point").ok_or_else(|| format!("vertex {index} has no point"))?;
point.vector("pvec").map(geometry::position).ok_or_else(|| format!("vertex {index}'s point has no position"))
}
fn vertex(&mut self, index: u32) -> Result<usize, String> {
if let Some(&id) = self.vertices.get(&index) {
return Ok(id);
}
let location = self.vertex_location(index)?;
let point = self.point_id(location);
let id = self.add(format!("VERTEX_POINT('',#{point})"));
self.vertices.insert(index, id);
Ok(id)
}
fn edge(&mut self, index: u32) -> Result<usize, String> {
if let Some(&id) = self.edges.get(&index) {
return Ok(id);
}
let edge = self.node(index, "edge")?;
let edge_id = edge.int("node_id").unwrap_or(index as i64);
let (positive, negative) = edge_fins(self.file, edge).map_err(|e| format!("edge {edge_id} {e}"))?;
let (end_vertex, start_vertex) = (positive.ptr("vertex"), negative.ptr("vertex"));
self.edge_ends = self.vertex_location(start_vertex).ok().zip(self.vertex_location(end_vertex).ok());
self.edge_end_tolerances = [start_vertex, end_vertex]
.into_iter()
.filter_map(|v| {
let at = self.vertex_location(v).ok()?;
let tolerance = self.file.node(v)?.real("tolerance").filter(|t| t.is_finite() && *t > 0.0).unwrap_or(0.0);
Some((at, tolerance * MM_PER_METRE))
})
.collect();
let written = self.edge_curve(edge, positive);
(self.edge_ends, self.anchor) = (None, None);
self.edge_end_tolerances.clear();
let (curve, same_sense, closed_point) = written.map_err(|e| format!("edge {edge_id}: {e}"))?;
let (start, end) = if start_vertex == 0 || end_vertex == 0 {
let p = closed_point.ok_or_else(|| format!("ring edge {edge_id}'s curve has no closing point"))?;
let point = self.point_id(p);
let v = self.add(format!("VERTEX_POINT('',#{point})"));
(v, v)
} else {
(self.vertex(start_vertex)?, self.vertex(end_vertex)?)
};
let id = self.add(format!(
"EDGE_CURVE('',#{start},#{end},#{curve},{})",
if same_sense { ".T." } else { ".F." }
));
self.edges.insert(index, id);
Ok(id)
}
fn edge_curve(&mut self, edge: &Node, positive: &Node) -> Result<(usize, bool, Option<Vec3>), String> {
if let Some(curve) = self.file.follow(edge, "curve") {
let sense = runs_forward(self.file, curve);
self.anchor = self.edge_ends.map(|(start, end)| if sense { (start, end) } else { (end, start) });
let (id, closing) = match self.curve(curve) {
Ok(read) => read,
Err(e) if !self.read_blend_sp => {
if let Some((blend, uv)) = self.blend_under(curve) {
if let Some(Ok(section)) = self.blend_cross_section(blend, &uv).transpose() {
return Ok(section);
}
}
self.read_blend_sp = true;
let again = self.curve(curve);
self.read_blend_sp = false;
again.map_err(|_| e)?
}
Err(e) => return Err(e),
};
return Ok((id, sense, closing));
}
let mut at = positive.index;
let mut first_error = None;
let mut on_blend = None;
for _ in 0..64 {
let fin = self.node(at, "fin")?;
if let Some(curve) = self.file.follow(fin, "curve") {
let fin_sense = fin.char("sense") != Some(b'-');
let curve_sense = runs_forward(self.file, curve);
self.anchor = self.edge_ends.map(|(start, end)| if fin_sense == curve_sense { (start, end) } else { (end, start) });
match self.curve(curve) {
Ok((id, closing)) => return Ok((id, fin_sense == curve_sense, closing)),
Err(e) => {
if on_blend.is_none() {
on_blend = self.blend_under(curve);
}
first_error.get_or_insert(e);
}
}
}
at = fin.ptr("other");
if at == positive.index || at == 0 {
break;
}
}
if let Some((blend, uv)) = on_blend {
if let Some(result) = self.blend_cross_section(blend, &uv).transpose() {
return result;
}
}
if !self.read_blend_sp {
self.read_blend_sp = true;
let again = self.edge_curve(edge, positive);
self.read_blend_sp = false;
if again.is_ok() {
return again;
}
}
Err(first_error.unwrap_or_else(|| "the edge has no curve and none of its fins has one".into()))
}
fn blend_under(&self, curve: &Node) -> Option<(&'a Node, NurbsCurve)> {
let mut node = self.file.node(curve.index)?;
for _ in 0..8 {
if node.type_name() != "TRIMMED_CURVE" {
break;
}
node = self.file.follow(node, "basis_curve")?;
}
if node.type_name() != "SP_CURVE" {
return None;
}
let blend = self.file.follow(node, "surface").filter(|s| s.type_name() == "BLENDED_EDGE")?;
let uv = geometry::parameter_curve(self.file, self.file.follow(node, "b_curve")?).ok()?;
Some((blend, uv))
}
fn blend_cross_section(&mut self, blend: &Node, uv: &NurbsCurve) -> Result<Option<(usize, bool, Option<Vec3>)>, String> {
let plain: Vec<(f64, f64)> = uv.control_points.iter().map(|c| (c.x / c.w, c.y / c.w)).collect();
let (Some(first), Some(last)) = (plain.first(), plain.last()) else { return Ok(None) };
let (u_low, u_high) = plain.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), p| (a.min(p.0), b.max(p.0)));
let spans = ((first.1 - last.1).abs() - 1.0).abs() < 1e-6;
if !(u_high - u_low <= 1e-6 * (1.0 + u_low.abs()) && spans) {
return Ok(None);
}
let Some((start, end)) = self.edge_ends else { return Ok(None) };
let id = blend.int("node_id").unwrap_or(blend.index as i64);
let radius = blend.reals("range").iter().map(|r| r.abs()).fold(0.0f64, f64::max) * MM_PER_METRE;
let spine_node = self.file.follow(blend, "spine").ok_or_else(|| format!("blend {id} has no spine"))?;
let spine = self.spine_curve(spine_node).map_err(|e| format!("blend {id}: spine: {e}"))?;
let miss = |c: Vec3| (start.sub(c).length() - radius).hypot(end.sub(c).length() - radius);
let t = geometry::nearest_parameter(&spine, [true, true], miss)
.ok_or_else(|| format!("blend {id}: no spine point for a cross-section edge"))?;
let centre = geometry::extended_point(&spine, t)?;
if miss(centre) > 1e-3 {
return Err(format!(
"blend {id}: a cross-section edge's vertices stand {:.3e} mm off the radius from the spine",
miss(centre)
));
}
let x = start.sub(centre).normalized()?;
let z = x.cross(end.sub(centre)).normalized()?;
let circle = Curve::Circle { frame: Frame { origin: centre, z, x }, radius };
Ok(Some((self.emit_curve(&circle)?, true, None)))
}
fn curve(&mut self, node: &Node) -> Result<(usize, Option<Vec3>), String> {
self.curve_at(node, 0, None)
}
fn curve_at(&mut self, node: &Node, depth: usize, trim: Option<[f64; 2]>) -> Result<(usize, Option<Vec3>), String> {
if depth > 8 {
return Err("trimmed curves nest more than 8 deep".into());
}
match node.type_name() {
"LINE" | "CIRCLE" | "ELLIPSE" | "B_CURVE" => {
let curve = geometry::curve(self.file, node)?;
let closing = curve.natural_range().map(|r| curve.point(r[0])).transpose()?;
Ok((self.emit_curve(&curve)?, closing))
}
"TRIMMED_CURVE" => {
let basis = self.file.follow(node, "basis_curve").ok_or("a trimmed curve has no basis curve")?;
let closing = node.vector("point_1").map(geometry::position);
let trim = match (node.real("parm_1"), node.real("parm_2")) {
(Some(a), Some(b)) => Some([a.min(b), a.max(b)]),
_ => None,
};
let (id, basis_closing) = self.curve_at(basis, depth + 1, trim)?;
Ok((id, closing.or(basis_closing)))
}
"INTERSECTION" => {
let mut chart = self.intersection_points(node)?;
if let Some((begin, finish)) = self.anchor.take() {
let closed = chart.len() > 3 && chart[0].sub(chart[chart.len() - 1]).length() <= 1e-6;
let longest = chart.windows(2).map(|w| w[1].sub(w[0]).length()).fold(0.0f64, f64::max);
if closed && begin.sub(finish).length() < longest {
if let Some(crossing) = self.intersection_crossing(node) {
chart = ChartStations::new(Some(&crossing), &chart).refined(16);
}
}
chart = reseat_closed_chart(chart, begin, finish);
chart = cut_open_chart(chart, begin, finish);
}
let tolerance_at = |p: Option<&Vec3>| {
p.and_then(|p| self.edge_end_tolerances.iter().find(|(at, _)| at.sub(*p).length() <= 1e-9))
.map_or(RESOLUTION_MM, |(_, t)| t.max(RESOLUTION_MM))
};
let tolerances = [tolerance_at(chart.first()), tolerance_at(chart.last())];
let chart = uncrowd_chart_ends(chart, tolerances);
let crossing = self.intersection_crossing(node);
let stations = ChartStations::new(crossing.as_ref(), &chart);
let (fitted, fell_back) = stations.fit()
.map_err(|e| format!("intersection curve {}: {e}", node.int("node_id").unwrap_or(node.index as i64)))?;
if fell_back || stations.refined(2).len() == chart.len() {
self.unverified_curves += 1;
}
self.fitted_curves += 1;
Ok((self.emit_curve(&Curve::Nurbs(fitted))?, chart.first().copied()))
}
"SP_CURVE" if self.read_blend_sp && self.file.follow(node, "surface").is_some_and(|s| s.type_name() == "BLENDED_EDGE") => {
let blend = self.file.follow(node, "surface").ok_or("an SP-curve has no surface")?;
let blend_id = blend.int("node_id").unwrap_or(blend.index as i64);
let (mut supports, radius) = self.blend_supports(blend, 0)?;
let spine_node = self.file.follow(blend, "spine").ok_or_else(|| format!("blend {blend_id} has no spine"))?;
let mut basis = spine_node;
for _ in 0..8 {
match self.file.follow(basis, "basis_curve") {
Some(b) if basis.type_name() == "TRIMMED_CURVE" => basis = b,
_ => break,
}
}
if basis.type_name() == "INTERSECTION" {
return Err(format!("an SP-curve on blend {blend_id}, whose spine is an intersection curve, is not read yet"));
}
if spine_node.char("sense") == Some(b'-') {
supports.swap(0, 1);
}
let spine = self.spine_curve(spine_node).map_err(|e| format!("blend {blend_id}: spine: {e}"))?;
let uv = geometry::parameter_curve(self.file, self.file.follow(node, "b_curve").ok_or("an SP-curve has no 2D curve")?)?;
let point = |u: f64, v: f64| -> Result<Vec3, String> {
let centre = ball_centre(&supports, radius, &spine, u)?;
Ok(section_at_angle(&blend_section(&supports, radius, &spine, u)?, centre, v))
};
let [t0, t1] = uv.domain()?;
let open = uv.evaluate(t0)?.sub(uv.evaluate(t1)?).length() > 1e-9;
let range = trim
.filter(|_| open)
.map(|[a, b]| [a.max(t0), b.min(t1)])
.filter(|[a, b]| b - a > 1e-9 * (t1 - t0))
.unwrap_or([t0, t1]);
let trimmed = range != [t0, t1];
let fitted = fit_sp_curve_by(&point, &uv, range)
.map_err(|e| format!("SP-curve {} on blend {blend_id}: {e}", node.int("node_id").unwrap_or(node.index as i64)))?;
let fitted = match self.edge_ends {
Some(ends) if trimmed => snap_ends(fitted, ends),
_ => fitted,
};
self.fitted_curves += 1;
let start = uv.evaluate(range[0])?;
let closing = point(start.x, start.y).ok();
Ok((self.emit_curve(&Curve::Nurbs(fitted))?, closing))
}
"SP_CURVE" => {
let (surface, uv) = self.sp_curve_parts(node)?;
let [t0, t1] = uv.domain()?;
let open = uv.evaluate(t0)?.sub(uv.evaluate(t1)?).length() > 1e-9;
let range = trim
.filter(|_| open)
.map(|[a, b]| [a.max(t0), b.min(t1)])
.filter(|[a, b]| b - a > 1e-9 * (t1 - t0))
.unwrap_or([t0, t1]);
let trimmed = range != [t0, t1];
if let Some(exact) = exact_sp_curve(&surface, &uv) {
let closing = sp_curve_points(&surface, &uv, 1, uv.domain()?)?.first().copied();
let exact = match (exact, self.edge_ends) {
(Curve::Nurbs(nurbs), Some(ends)) if trimmed => {
Curve::Nurbs(snap_ends(crate::trimmed_curve::trimmed_curve(&nurbs, range[0], range[1])?, ends))
}
(exact, _) => exact,
};
return Ok((self.emit_curve(&exact)?, closing));
}
let fitted = fit_sp_curve(&surface, &uv, range)
.map_err(|e| format!("SP-curve {}: {e}", node.int("node_id").unwrap_or(node.index as i64)))?;
let fitted = match self.edge_ends {
Some(ends) if trimmed => snap_ends(fitted, ends),
_ => fitted,
};
self.fitted_curves += 1;
let closing = sp_curve_points(&surface, &uv, 1, range)?.first().copied();
Ok((self.emit_curve(&Curve::Nurbs(fitted))?, closing))
}
other => Err(format!(
"curve {} is a {other}, which is not read yet",
node.int("node_id").unwrap_or(node.index as i64)
)),
}
}
fn intersection_points(&self, node: &Node) -> Result<Vec<Vec3>, String> {
let chart = self.file.follow(node, "chart").ok_or("an intersection curve has no chart")?;
let mut points: Vec<Vec3> = chart
.vectors("hvec")
.into_iter()
.map(|p| p.map(geometry::position).ok_or("an intersection chart has an unset point"))
.collect::<Result<_, _>>()?;
if points.len() < 2 {
return Err("an intersection chart has fewer than two points".into());
}
let terminator = |field: &str| {
self.file
.follow(node, field)
.filter(|limit| limit.char("type") == Some(b'T'))
.and_then(|limit| limit.vectors("hvec").first().copied().flatten())
.map(geometry::position)
};
if let Some(p) = terminator("start") {
if p.sub(points[0]).length() > 0.0 {
points.insert(0, p);
}
}
if let Some(p) = terminator("end") {
if p.sub(points[points.len() - 1]).length() > 0.0 {
points.push(p);
}
}
Ok(points)
}
fn intersection_surfaces(&self, node: &Node) -> Option<[Surface; 2]> {
let surfaces: Vec<Surface> = node
.ints("surface")
.into_iter()
.filter_map(|i| u32::try_from(i).ok())
.filter_map(|i| self.file.node(i))
.filter_map(|s| geometry::surface(self.file, s, 0).ok())
.collect();
surfaces.try_into().ok()
}
fn intersection_crossing(&mut self, node: &Node) -> Option<Crossing> {
if let Some([a, b]) = self.intersection_surfaces(node) {
return Some(Crossing::Surfaces(a, b));
}
let file = self.file;
let nodes: Vec<&Node> = node.ints("surface").into_iter().filter_map(|i| u32::try_from(i).ok()).filter_map(|i| file.node(i)).collect();
let [first, second]: [&Node; 2] = nodes.try_into().ok()?;
let (bound, other) = match (first.type_name(), second.type_name()) {
("BLEND_BOUND", _) => (first, second),
(_, "BLEND_BOUND") => (second, first),
_ => return None,
};
let blend = file.follow(bound, "blend").filter(|b| b.type_name() == "BLENDED_EDGE" && b.char("blend_type") == Some(b'R'))?;
let supports = blend.ints("surface");
if supports.len() != 2 || supports.iter().filter(|&&s| s == i64::from(other.index)).count() != 1 {
return None;
}
let support = geometry::surface(file, other, 0).ok()?;
let radius = blend.reals("range").iter().map(|r| r.abs()).fold(0.0f64, f64::max) * MM_PER_METRE;
if !(radius > 0.0 && radius.is_finite()) {
return None;
}
let spine = self.spine_curve(file.follow(blend, "spine")?).ok()?;
Some(Crossing::Contact { spine, support, radius })
}
fn sp_curve_parts(&self, node: &Node) -> Result<(Surface, NurbsCurve), String> {
let surface_node = self.file.follow(node, "surface").ok_or("an SP-curve has no surface")?;
let surface = geometry::surface(self.file, surface_node, 0)?;
let b_curve = self.file.follow(node, "b_curve").ok_or("an SP-curve has no 2D curve")?;
Ok((surface, geometry::parameter_curve(self.file, b_curve)?))
}
fn emit_curve(&mut self, curve: &Curve) -> Result<usize, String> {
Ok(match curve {
Curve::Line { point, direction } => {
let p = self.point_id(*point);
let d = self.direction_id(*direction);
let v = self.add(format!("VECTOR('',#{d},1.)"));
self.add(format!("LINE('',#{p},#{v})"))
}
Curve::Circle { frame, radius } => {
let a = self.placement_id(frame.origin, frame.z, frame.x);
self.add(format!("CIRCLE('',#{a},{})", real(*radius)))
}
Curve::Ellipse { frame, major, minor } => {
let a = self.placement_id(frame.origin, frame.z, frame.x);
self.add(format!("ELLIPSE('',#{a},{},{})", real(*major), real(*minor)))
}
Curve::Nurbs(curve) => self.emit_nurbs_curve(curve),
})
}
fn emit_nurbs_curve(&mut self, curve: &NurbsCurve) -> usize {
let points: Vec<String> = curve
.control_points
.iter()
.map(|c| format!("#{}", self.point_id(Vec3::new(c.x / c.w, c.y / c.w, c.z / c.w))))
.collect();
let (mults, values) = knot_runs(&curve.knots);
let rational = curve.control_points.iter().any(|c| (c.w - 1.0).abs() > 1e-15);
let degree = curve.degree;
if rational {
let weights: Vec<String> = curve.control_points.iter().map(|c| real(c.w)).collect();
self.add(format!(
"(BOUNDED_CURVE()B_SPLINE_CURVE({degree},({}),.UNSPECIFIED.,.F.,.F.)B_SPLINE_CURVE_WITH_KNOTS(({mults}),({values}),.UNSPECIFIED.)CURVE()GEOMETRIC_REPRESENTATION_ITEM()RATIONAL_B_SPLINE_CURVE(({}))REPRESENTATION_ITEM(''))",
points.join(","),
weights.join(",")
))
} else {
self.add(format!(
"B_SPLINE_CURVE_WITH_KNOTS('',{degree},({}),.UNSPECIFIED.,.F.,.F.,({mults}),({values}),.UNSPECIFIED.)",
points.join(",")
))
}
}
fn surface(&mut self, node: &Node) -> Result<usize, String> {
if let Some(&i) = self.surfaces.get(&node.index) {
return Ok(i);
}
let surface = geometry::surface(self.file, node, 0)?;
let revolved = match self.spun_skew_line(node, &surface)? {
Some(hyperboloid) => Some(hyperboloid),
None => spun_conic_to_axis(node, &surface)?,
};
let (id, flipped) = match revolved {
Some(revolved) => (self.emit_nurbs_surface(&revolved), false),
None => self.emit_surface(&surface)?,
};
self.emitted.push(EmittedSurface { id, flipped });
let i = self.emitted.len() - 1;
self.surfaces.insert(node.index, i);
Ok(i)
}
fn spun_skew_line(&self, node: &Node, surface: &Surface) -> Result<Option<NurbsSurface>, String> {
let Surface::Spun { profile: profile @ Curve::Line { point, direction }, axis_point, axis } = surface else { return Ok(None) };
if !matches!(spun_analytic(profile, *axis_point, *axis), Ok(None)) {
return Ok(None);
}
let axis = axis.normalized()?;
let rate = direction.dot(axis) * MM_PER_METRE;
if rate.abs() < 1e-9 {
return Err("a line spun about an axis square to it and skew to it is not read yet".into());
}
let at = |p: Vec3| p.sub(*point).dot(axis) / rate;
let (mut lo, mut hi) = (f64::INFINITY, f64::NEG_INFINITY);
let mut take = |t: f64| {
if t.is_finite() {
(lo, hi) = (lo.min(t), hi.max(t));
}
};
let file = self.file;
for face in file.nodes().iter().filter(|n| n.type_name() == "FACE" && n.ptr("surface") == node.index) {
for lp in chain(file, face.ptr("loop"), "next") {
for fin in self.fins(lp) {
if let Ok(p) = self.vertex_location(fin.ptr("vertex")) {
take(at(p));
}
let mut curves: Vec<&Node> = Vec::new();
if let Some(edge) = file.follow(fin, "edge") {
curves.extend(file.follow(edge, "curve"));
}
curves.extend(file.follow(fin, "curve"));
for mut c in curves {
for _ in 0..8 {
match file.follow(c, "basis_curve") {
Some(basis) if c.type_name() == "TRIMMED_CURVE" => c = basis,
_ => break,
}
}
match c.type_name() {
"SP_CURVE" if c.ptr("surface") == node.index => {
if let Ok(uv) = file.follow(c, "b_curve").ok_or(()).and_then(|b| geometry::parameter_curve(file, b).map_err(|_| ())) {
for q in &uv.control_points {
take(q.x / q.w);
}
}
}
"INTERSECTION" => {
for p in self.intersection_points(c).unwrap_or_default() {
take(at(p));
}
}
_ => match geometry::curve(file, c) {
Ok(Curve::Nurbs(n)) => {
for q in &n.control_points {
take(at(Vec3::new(q.x / q.w, q.y / q.w, q.z / q.w)));
}
}
Ok(Curve::Circle { frame, radius }) => {
let reach = radius * frame.z.cross(axis).length();
take(at(frame.origin.add(axis.scale(reach))));
take(at(frame.origin.sub(axis.scale(reach))));
}
Ok(Curve::Ellipse { frame, major, minor }) => {
let reach = (major * frame.x.dot(axis)).hypot(minor * frame.y().dot(axis));
take(at(frame.origin.add(axis.scale(reach))));
take(at(frame.origin.sub(axis.scale(reach))));
}
_ => {}
},
}
}
}
}
}
if !(hi > lo) {
return Err("a line spun about an axis skew to it bounds no face".into());
}
let pad = 0.05 * (hi - lo);
let (lo, hi) = (lo - pad, hi + pad);
let ends = [profile.point(lo)?, profile.point(hi)?];
let net = ends.iter().map(|p| crate::Vec4 { x: p.x, y: p.y, z: p.z, w: 1.0 }).collect();
let segment = NurbsCurve::new(1, vec![0.0, 0.0, 1.0, 1.0], net)?;
Ok(Some(revolve(&segment, *axis_point, axis)?))
}
fn emit_surface(&mut self, surface: &Surface) -> Result<(usize, bool), String> {
Ok(match surface {
Surface::Plane(f) => {
let a = self.placement_id(f.origin, f.z, f.x);
(self.add(format!("PLANE('',#{a})")), false)
}
Surface::Cylinder { frame, radius } => {
let a = self.placement_id(frame.origin, frame.z, frame.x);
(self.add(format!("CYLINDRICAL_SURFACE('',#{a},{})", real(*radius))), false)
}
Surface::Cone { frame, radius, sin, cos } => {
let a = self.placement_id(frame.origin, frame.z, frame.x);
let angle = sin.atan2(*cos);
(self.add(format!("CONICAL_SURFACE('',#{a},{},{})", real(*radius), real(angle))), false)
}
Surface::Sphere { frame, radius } => {
let a = self.placement_id(frame.origin, frame.z, frame.x);
(self.add(format!("SPHERICAL_SURFACE('',#{a},{})", real(*radius))), false)
}
Surface::Torus { frame, major, minor } if *major < 0.0 => {
let Surface::Spun { profile: Curve::Nurbs(arc), axis_point, axis } = geometry::lemon(*frame, *major, *minor)? else {
return Err("a lemon torus's arc is not a B-curve".into());
};
(self.emit_nurbs_surface(&revolve(&arc, axis_point, axis)?), false)
}
Surface::Torus { frame, major, minor } => {
let a = self.placement_id(frame.origin, frame.z, frame.x);
(
self.add(format!(
"TOROIDAL_SURFACE('',#{a},{},{})",
real(*major),
real(*minor)
)),
false,
)
}
Surface::Nurbs(nurbs) => (self.emit_nurbs_surface(nurbs), false),
Surface::Offset { base, base_sense, distance } => match offset_analytic(base, *base_sense, *distance) {
Ok(offset) => self.emit_surface(&offset)?,
Err(_) if !base.is_analytic() => {
let (basis, flipped) = self.emit_surface(base)?;
let along = if *base_sense != flipped { *distance } else { -*distance };
(self.add(format!("OFFSET_SURFACE('',#{basis},{},.F.)", real(along))), flipped)
}
Err(error) => return Err(error),
},
Surface::Spun { profile, axis_point, axis } => {
if let Some((analytic, sample)) = spun_analytic(profile, *axis_point, *axis).map_err(|e| format!("spun profile: {e}"))? {
let (id, flipped) = self.emit_surface(&analytic)?;
let spun_normal = surface.natural_normal(sample, 0.0).map_err(|e| format!("spun normal at {sample}: {e}"))?;
let point = surface.point(sample, 0.0)?;
let outward = analytic_outward(&analytic, point).map_err(|e| format!("analytic normal at {point:?} of {analytic:?}: {e}"))?;
let agrees = outward.dot(spun_normal) > 0.0;
return Ok((id, flipped == agrees));
}
if profile_meets_axis(profile, *axis_point, *axis) {
let Curve::Nurbs(nurbs) = profile else {
return Err("a spun surface whose profile meets its axis is not read yet".into());
};
return Ok((self.emit_nurbs_surface(&revolve(nurbs, *axis_point, *axis)?), false));
}
if matches!(profile, Curve::Line { .. }) {
return Err("a line spun about an axis skew to it (a hyperboloid) is not read yet".into());
}
let curve = self.emit_curve(profile)?;
let point = self.point_id(*axis_point);
let direction = self.direction_id(*axis);
let placement = self.add(format!("AXIS1_PLACEMENT('',#{point},#{direction})"));
(self.add(format!("SURFACE_OF_REVOLUTION('',#{curve},#{placement})")), true)
}
Surface::Swept { section: Curve::Line { point, direction: along }, direction } => {
let normal = along.cross(*direction).normalized().map_err(|_| "a line swept along itself")?;
self.emit_surface(&Surface::Plane(Frame { origin: *point, z: normal, x: *along }))?
}
Surface::Swept { section, direction } => {
let curve = self.emit_curve(section)?;
let direction = self.direction_id(*direction);
let vector = self.add(format!("VECTOR('',#{direction},1.)"));
(self.add(format!("SURFACE_OF_LINEAR_EXTRUSION('',#{curve},#{vector})")), false)
}
})
}
fn emit_nurbs_surface(&mut self, surface: &NurbsSurface) -> usize {
let mut rows = Vec::with_capacity(surface.control_points.len());
let mut weight_rows = Vec::with_capacity(surface.control_points.len());
let mut rational = false;
for row in &surface.control_points {
let mut ids = Vec::with_capacity(row.len());
let mut weights = Vec::with_capacity(row.len());
for c in row {
ids.push(format!("#{}", self.point_id(Vec3::new(c.x / c.w, c.y / c.w, c.z / c.w))));
weights.push(real(c.w));
rational |= (c.w - 1.0).abs() > 1e-15;
}
rows.push(format!("({})", ids.join(",")));
weight_rows.push(format!("({})", weights.join(",")));
}
let (mults_u, values_u) = knot_runs(&surface.knots_u);
let (mults_v, values_v) = knot_runs(&surface.knots_v);
let (du, dv) = (surface.degree_u, surface.degree_v);
if rational {
self.add(format!(
"(BOUNDED_SURFACE()B_SPLINE_SURFACE({du},{dv},({}),.UNSPECIFIED.,.F.,.F.,.F.)B_SPLINE_SURFACE_WITH_KNOTS(({mults_u}),({mults_v}),({values_u}),({values_v}),.UNSPECIFIED.)GEOMETRIC_REPRESENTATION_ITEM()RATIONAL_B_SPLINE_SURFACE(({}))REPRESENTATION_ITEM('')SURFACE())",
rows.join(","),
weight_rows.join(",")
))
} else {
self.add(format!(
"B_SPLINE_SURFACE_WITH_KNOTS('',{du},{dv},({}),.UNSPECIFIED.,.F.,.F.,.F.,({mults_u}),({mults_v}),({values_u}),({values_v}),.UNSPECIFIED.)",
rows.join(",")
))
}
}
}
fn spun_analytic(profile: &Curve, axis_point: Vec3, axis: Vec3) -> Result<Option<(Surface, f64)>, String> {
let foot = |p: Vec3| axis_point.add(axis.scale(p.sub(axis_point).dot(axis)));
let tiny = 1e-9;
Ok(match profile {
Curve::Line { point, direction } => {
if point.sub(axis_point).dot(direction.cross(axis)).abs() > tiny {
return Ok(None);
}
let t = if point.sub(foot(*point)).length() > tiny { 0.0 } else { 1.0 };
let p = point.add(direction.scale(t * MM_PER_METRE));
let centre = foot(p);
let radial = p.sub(centre);
let radius = radial.length();
if radius <= tiny {
return Ok(None);
}
let x = radial.scale(1.0 / radius);
let along = direction.dot(axis);
let across = direction.dot(x);
let surface = if across.abs() <= tiny {
Surface::Cylinder { frame: Frame { origin: centre, z: axis, x }, radius }
} else if along.abs() <= tiny {
Surface::Plane(Frame { origin: centre, z: axis, x })
} else {
let z = if along * across > 0.0 { axis } else { axis.scale(-1.0) };
let (sin, cos) = (across.abs(), along.abs());
Surface::Cone { frame: Frame { origin: centre, z, x }, radius, sin, cos }
};
Some((surface, t))
}
Curve::Circle { frame, radius } => {
if frame.z.dot(axis).abs() > tiny || axis_point.sub(frame.origin).dot(frame.z).abs() > tiny * (1.0 + radius) {
return Ok(None);
}
let centre = foot(frame.origin);
let offset = frame.origin.sub(centre);
let major = offset.length();
let outward = if major > tiny { offset.scale(1.0 / major) } else { frame.z.cross(axis).normalized()? };
let outward = outward.sub(frame.z.scale(outward.dot(frame.z))).normalized()?;
let sample = outward.dot(frame.y()).atan2(outward.dot(frame.x));
if major <= tiny * (1.0 + radius) {
let x = axis.perpendicular()?;
Some((Surface::Sphere { frame: Frame { origin: centre, z: axis, x }, radius: *radius }, sample))
} else if major > *radius {
Some((Surface::Torus { frame: Frame { origin: centre, z: axis, x: outward }, major, minor: *radius }, sample))
} else {
return Err("a spun circle that crosses its axis (a lemon or apple torus) has no STEP surface".into());
}
}
_ => None,
})
}
fn analytic_outward(surface: &Surface, p: Vec3) -> Result<Vec3, String> {
let from_axis = |frame: &Frame| {
let d = p.sub(frame.origin);
d.sub(frame.z.scale(d.dot(frame.z)))
};
match surface {
Surface::Plane(f) => Ok(f.z),
Surface::Cylinder { frame, .. } => from_axis(frame).normalized(),
Surface::Cone { frame, sin, cos, .. } => {
let radial = from_axis(frame).normalized()?;
Ok(radial.scale(*cos).sub(frame.z.scale(*sin)))
}
Surface::Sphere { frame, .. } => p.sub(frame.origin).normalized(),
Surface::Torus { frame, major, .. } => {
let radial = from_axis(frame).normalized()?;
p.sub(frame.origin.add(radial.scale(*major))).normalized()
}
_ => Err("not an analytic surface".into()),
}
}
pub(super) fn revolve(profile: &NurbsCurve, axis_point: Vec3, axis: Vec3) -> Result<NurbsSurface, String> {
let axis = axis.normalized()?;
let half = std::f64::consts::FRAC_1_SQRT_2;
let net = profile
.control_points
.iter()
.map(|c| {
let p = Vec3::new(c.x / c.w, c.y / c.w, c.z / c.w);
let foot = axis_point.add(axis.scale(p.sub(axis_point).dot(axis)));
let radial = p.sub(foot);
let turned = axis.cross(radial);
(0..9)
.map(|j| {
let angle = std::f64::consts::FRAC_PI_4 * j as f64;
let (stretch, weight) = if j % 2 == 0 { (1.0, 1.0) } else { (std::f64::consts::SQRT_2, half) };
let q = foot.add(radial.scale(stretch * angle.cos())).add(turned.scale(stretch * angle.sin()));
let w = c.w * weight;
crate::Vec4 { x: q.x * w, y: q.y * w, z: q.z * w, w }
})
.collect()
})
.collect();
let around = vec![0.0, 0.0, 0.0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1.0, 1.0, 1.0];
NurbsSurface::new(profile.degree, 2, profile.knots.clone(), around, net)
}
pub(super) fn spun_conic_to_axis(node: &Node, surface: &Surface) -> Result<Option<NurbsSurface>, String> {
let Surface::Spun { profile, axis_point, axis } = surface else { return Ok(None) };
let (frame, major, minor) = match profile {
Curve::Circle { frame, radius } => (frame, *radius, *radius),
Curve::Ellipse { frame, major, minor } => (frame, *major, *minor),
_ => return Ok(None),
};
if !conic_reaches_axis(frame, major, minor, *axis_point, *axis) || matches!(spun_analytic(profile, *axis_point, *axis), Ok(Some(_))) {
return Ok(None);
}
let (Some(a), Some(b)) = (node.real("start_param"), node.real("end_param")) else { return Ok(None) };
spun_conic_surface(frame, major, minor, [a, b], *axis_point, *axis).map(Some)
}
pub(super) fn conic_reaches_axis(frame: &Frame, major: f64, minor: f64, axis_point: Vec3, axis: Vec3) -> bool {
let Ok(axis) = axis.normalized() else { return false };
let scale = 1.0 + major.abs().max(minor.abs());
if frame.z.dot(axis).abs() > 1e-9 || axis_point.sub(frame.origin).dot(frame.z).abs() > 1e-9 * scale {
return false;
}
let Ok(n) = frame.z.cross(axis).normalized() else { return false };
let d0 = frame.origin.sub(axis_point).dot(n);
let amplitude = (major * n.dot(frame.x)).hypot(minor * n.dot(frame.y()));
d0.abs() <= amplitude + 1e-9 * scale
}
pub(super) fn spun_conic_surface(frame: &Frame, major: f64, minor: f64, [a, b]: [f64; 2], axis_point: Vec3, axis: Vec3) -> Result<NurbsSurface, String> {
if !(b > a && b - a <= std::f64::consts::TAU + 1e-9) {
return Err(format!("a spun profile's range [{a}, {b}] is not a stretch of one turn"));
}
let pieces = ((b - a) / std::f64::consts::FRAC_PI_2).ceil().max(1.0) as usize;
let on = |t: f64| frame.origin.add(frame.x.scale(major * t.cos())).add(frame.y().scale(minor * t.sin()));
let mut net = Vec::new();
let mut knots = vec![0.0; 3];
for k in 0..pieces {
let (t0, t1) = (a + (b - a) * k as f64 / pieces as f64, a + (b - a) * (k + 1) as f64 / pieces as f64);
let (half, middle) = ((t1 - t0) / 2.0, (t0 + t1) / 2.0);
let w = half.cos();
let apex = frame.origin.add(frame.x.scale(major * middle.cos() / w)).add(frame.y().scale(minor * middle.sin() / w));
if k == 0 {
let p = on(t0);
net.push(crate::Vec4 { x: p.x, y: p.y, z: p.z, w: 1.0 });
}
let p = on(t1);
net.push(crate::Vec4 { x: apex.x * w, y: apex.y * w, z: apex.z * w, w });
net.push(crate::Vec4 { x: p.x, y: p.y, z: p.z, w: 1.0 });
knots.extend([(k + 1) as f64, (k + 1) as f64]);
}
knots.push(pieces as f64);
let arc = NurbsCurve::new(2, knots, net)?;
revolve(&arc, axis_point, axis)
}
fn profile_meets_axis(profile: &Curve, axis_point: Vec3, axis: Vec3) -> bool {
let Some([a, b]) = profile.natural_range() else { return false };
(0..=256).filter_map(|k| profile.point(a + (b - a) * k as f64 / 256.0).ok()).any(|p| {
let d = p.sub(axis_point);
d.sub(axis.scale(d.dot(axis))).length() < 1e-3
})
}
fn offset_analytic(base: &Surface, base_sense: bool, distance: f64) -> Result<Surface, String> {
let d = if base_sense { distance } else { -distance };
let positive = |r: f64, what: &str| {
if r > 0.0 {
Ok(r)
} else {
Err(format!("an offset {what} collapses (radius {r})"))
}
};
Ok(match base {
Surface::Plane(f) => Surface::Plane(Frame { origin: f.origin.add(f.z.scale(d)), ..*f }),
Surface::Cylinder { frame, radius } => Surface::Cylinder { frame: *frame, radius: positive(radius + d, "cylinder")? },
Surface::Sphere { frame, radius } => Surface::Sphere { frame: *frame, radius: positive(radius + d, "sphere")? },
Surface::Torus { frame, major, minor } => {
Surface::Torus { frame: *frame, major: *major, minor: positive(minor + d, "torus")? }
}
Surface::Cone { frame, radius, sin, cos } => {
let r = radius + d / cos;
if r < 0.0 {
return Err(format!("an offset cone passes its apex (radius {r})"));
}
Surface::Cone { frame: *frame, radius: r, sin: *sin, cos: *cos }
}
_ => return Err("an offset of a non-analytic surface is not read yet".into()),
})
}
pub(super) fn cut_open_chart(chart: Vec<Vec3>, begin: Vec3, finish: Vec3) -> Vec<Vec3> {
let n = chart.len();
if n < 2 || chart[0].sub(chart[n - 1]).length() <= 1e-6 {
return chart;
}
let place = |p: Vec3| -> (f64, f64) {
let mut best = (f64::INFINITY, 0.0);
for i in 0..n - 1 {
let (a, b) = (chart[i], chart[i + 1]);
let ab = b.sub(a);
let t = (p.sub(a).dot(ab) / ab.dot(ab).max(1e-300)).clamp(0.0, 1.0);
let d = p.sub(a.add(ab.scale(t))).length();
if d < best.0 {
best = (d, i as f64 + t);
}
}
best
};
let ((db, sb), (df, sf)) = (place(begin), place(finish));
if db > 1.0 || df > 1.0 || (sb - sf).abs() < 1e-12 {
return chart;
}
let ((lo, first), (hi, last)) = if sb <= sf { ((sb, begin), (sf, finish)) } else { ((sf, finish), (sb, begin)) };
let mut out = vec![first];
for (j, p) in chart.iter().enumerate() {
let s = j as f64;
if s > lo && s < hi && p.sub(first).length() > 1e-9 && p.sub(last).length() > 1e-9 {
out.push(*p);
}
}
out.push(last);
out
}
pub(super) fn reseat_closed_chart(chart: Vec<Vec3>, begin: Vec3, finish: Vec3) -> Vec<Vec3> {
let n = chart.len();
if n < 4 || chart[0].sub(chart[n - 1]).length() > 1e-6 {
return chart;
}
let ring = &chart[..n - 1];
let m = ring.len();
let position = |p: Vec3| -> f64 {
let mut best = (f64::INFINITY, 0.0);
for i in 0..m {
let (a, b) = (ring[i], ring[(i + 1) % m]);
let ab = b.sub(a);
let t = (p.sub(a).dot(ab) / ab.dot(ab).max(1e-300)).clamp(0.0, 1.0);
let d = p.sub(a.add(ab.scale(t))).length();
if d < best.0 {
best = (d, i as f64 + t);
}
}
best.1
};
let at = |s: f64| {
let i = (s.floor() as usize) % m;
ring[i].add(ring[(i + 1) % m].sub(ring[i]).scale(s - s.floor()))
};
let rotated = |seam: Vec3, from: usize| {
let mut out = vec![seam];
out.extend((0..m).map(|i| ring[(from + i) % m]).filter(|p| p.sub(seam).length() > 1e-9));
out.push(seam);
out
};
let (start, end) = (position(begin), position(finish));
let gap = (start - end).rem_euclid(m as f64);
if begin.sub(finish).length() <= 1e-9 || gap <= 1e-12 {
return rotated(begin, (start.floor() as usize + 1) % m);
}
let middle = end + 0.5 * gap;
let inside = (1..=m)
.map(|k| end.floor() + k as f64)
.filter(|&s| s > end && s < end + gap)
.min_by(|a, b| (a - middle).abs().partial_cmp(&(b - middle).abs()).unwrap_or(std::cmp::Ordering::Equal));
match inside {
Some(s) => {
let k = (s as usize) % m;
rotated(ring[k], k)
}
None => rotated(at(middle), (middle.floor() as usize + 1) % m),
}
}
pub(super) enum Crossing {
Surfaces(Surface, Surface),
Contact { spine: Curve, support: Surface, radius: f64 },
}
impl Crossing {
fn solve(&self, station: Vec3, direction: Vec3, reach: f64) -> Option<Vec3> {
match self {
Crossing::Surfaces(first, second) => solve_on_both(first, second, station, direction, reach),
Crossing::Contact { spine, support, radius } => {
let t = match spine {
Curve::Nurbs(curve) => crate::project_point_to_curve(curve, station).ok()?.u,
_ => geometry::nearest_parameter(spine, [true, true], |q| q.sub(station).length())?,
};
let centre = geometry::extended_point(spine, t).ok()?;
let (distance, gradient) = support.distance(centre)?;
if (distance.abs() - radius).abs() > 0.05 * radius + 1e-6 {
return None;
}
let p = centre.sub(gradient.scale(distance));
(p.sub(station).length() <= reach).then_some(p)
}
}
}
}
const RESOLUTION_MM: f64 = 1e-5;
pub(super) fn uncrowd_chart_ends(chart: Vec<Vec3>, [at_first, at_last]: [f64; 2]) -> Vec<Vec3> {
let n = chart.len();
if n < 3 || chart[0].sub(chart[n - 1]).length() <= 1e-6 {
return chart;
}
let (first, last) = (chart[0], chart[n - 1]);
let mut out = Vec::with_capacity(n);
out.push(first);
out.extend(chart[1..n - 1].iter().copied().filter(|p| p.sub(first).length() > at_first && p.sub(last).length() > at_last));
out.push(last);
out
}
fn solve_on_both(first: &Surface, second: &Surface, station: Vec3, direction: Vec3, reach: f64) -> Option<Vec3> {
let mut x = station;
for _ in 0..40 {
if x.sub(station).length() > reach {
return None;
}
let (f1, g1) = first.distance(x)?;
let (f2, g2) = second.distance(x)?;
let f3 = x.sub(station).dot(direction);
let step = solve3([g1, g2, direction], [-f1, -f2, -f3])?;
x = x.add(step);
if step.length() < 1e-11 {
let (f1, _) = first.distance(x)?;
let (f2, _) = second.distance(x)?;
return (f1.abs() < 1e-7 && f2.abs() < 1e-7).then_some(x);
}
}
None
}
fn solve3(a: [Vec3; 3], b: [f64; 3]) -> Option<Vec3> {
let det = a[0].dot(a[1].cross(a[2]));
if det.abs() < 1e-9 {
return None;
}
let x = a[1].cross(a[2]).scale(b[0]).add(a[2].cross(a[0]).scale(b[1])).add(a[0].cross(a[1]).scale(b[2]));
Some(x.scale(1.0 / det))
}
const FIT_TOLERANCE: f64 = 1e-5;
const FIT_REFUSAL: f64 = 1e-3;
const FIT_DOUBLINGS: usize = 7;
const MAX_FIT_STATIONS: usize = 4096;
pub(super) fn fit_by_pieces(
pieces: usize,
points: impl Fn(usize, usize) -> Result<Vec<Vec3>, String>,
last: Vec3,
start: usize,
) -> Result<NurbsCurve, String> {
if pieces == 0 {
return Err("the curve has no points".into());
}
let most = start << FIT_DOUBLINGS;
let mut samples = vec![start; pieces];
let mut misses = vec![f64::INFINITY; pieces];
let mut cache: HashMap<(usize, usize), Vec<Vec3>> = HashMap::default();
let sampled = |i: usize, s: usize, cache: &mut HashMap<(usize, usize), Vec<Vec3>>| -> Result<Vec<Vec3>, String> {
if let Some(found) = cache.get(&(i, s)) {
return Ok(found.clone());
}
let found = points(i, s)?;
cache.insert((i, s), found.clone());
Ok(found)
};
let mut best: Option<(NurbsCurve, f64)> = None;
loop {
let mut run = Vec::new();
for (i, &s) in samples.iter().enumerate() {
run.extend(sampled(i, s, &mut cache)?);
}
run.push(last);
let fit = fit_through(&run)?;
let mut worst = 0.0f64;
let mut missing = Vec::new();
let mut doubling = Vec::new();
for (i, &s) in samples.iter().enumerate() {
let check = sampled(i, 2 * s, &mut cache)?;
let own = sampled(i, s, &mut cache)?;
if check.len() == own.len() && check.iter().zip(&own).all(|(a, b)| a.sub(*b).length() == 0.0) {
misses[i] = 0.0;
continue;
}
let miss = check
.iter()
.map(|p| crate::project_point_to_curve(&fit, *p).map(|f| f.distance).unwrap_or(f64::INFINITY))
.fold(0.0f64, f64::max);
worst = worst.max(miss);
if miss > FIT_TOLERANCE {
missing.push(i);
if miss > 10.0 * FIT_TOLERANCE || !(misses[i] <= 2.0 * miss) {
doubling.push(i);
}
}
misses[i] = miss;
}
if missing.is_empty() {
return Ok(fit);
}
if best.as_ref().map_or(true, |(_, b)| worst < *b) {
best = Some((fit, worst));
}
let total: usize = samples.iter().sum();
if doubling.is_empty()
|| doubling.iter().any(|&i| samples[i] >= most)
|| 2 * (total + doubling.iter().map(|&i| samples[i]).sum::<usize>()) > MAX_FIT_STATIONS
{
break;
}
for i in doubling {
samples[i] *= 2;
}
}
match best {
Some((fit, worst)) if worst <= FIT_REFUSAL => Ok(fit),
Some((_, worst)) => Err(format!("no cubic through its points comes within {FIT_REFUSAL} mm of it (closest {worst:.2e} mm)")),
None => Err("the curve has no points".into()),
}
}
struct ChartStations<'a> {
crossing: Option<&'a Crossing>,
chart: &'a [Vec3],
memo: Option<std::cell::RefCell<HashMap<(usize, u64), Option<Vec3>>>>,
}
impl<'a> ChartStations<'a> {
fn new(crossing: Option<&'a Crossing>, chart: &'a [Vec3]) -> Self {
let memo = crate::step_import::import_lean().then(|| std::cell::RefCell::new(HashMap::default()));
Self { crossing, chart, memo }
}
fn station(&self, crossing: &Crossing, i: usize, fraction: f64, direction: Vec3) -> Option<Vec3> {
let (a, b) = (self.chart[i], self.chart[i + 1]);
let chord = b.sub(a);
let solve = || crossing.solve(a.add(chord.scale(fraction)), direction, chord.length());
let Some(memo) = &self.memo else { return solve() };
if let Some(found) = memo.borrow().get(&(i, fraction.to_bits())) {
return *found;
}
let found = solve();
memo.borrow_mut().insert((i, fraction.to_bits()), found);
found
}
fn chord(&self, i: usize, splits: usize) -> Vec<Vec3> {
let (a, b) = (self.chart[i], self.chart[i + 1]);
let mut out = vec![a];
let Some(crossing) = self.crossing else { return out };
let Ok(direction) = b.sub(a).normalized() else { return out };
for k in 1..splits {
if let Some(p) = self.station(crossing, i, k as f64 / splits as f64, direction) {
out.push(p);
}
}
out
}
fn refined(&self, splits: usize) -> Vec<Vec3> {
let chart = self.chart;
if self.crossing.is_none() {
return chart.to_vec();
}
let mut out = Vec::with_capacity(chart.len() * splits);
for i in 0..chart.len() - 1 {
out.extend(self.chord(i, splits));
}
out.push(chart[chart.len() - 1]);
out
}
fn fit(&self) -> Result<(NurbsCurve, bool), String> {
let chart = self.chart;
let fit = |stations: &ChartStations| fit_by_pieces(chart.len() - 1, |i, s| Ok(stations.chord(i, s)), chart[chart.len() - 1], 8);
match (fit(self), self.crossing) {
(Err(_), Some(Crossing::Contact { .. })) => Ok((fit(&ChartStations { crossing: None, chart, memo: None })?, true)),
(result, _) => Ok((result?, false)),
}
}
}
fn fit_sp_curve(surface: &Surface, uv: &NurbsCurve, range: [f64; 2]) -> Result<NurbsCurve, String> {
fit_sp_curve_by(&|u, v| surface.point(u, v), uv, range)
}
fn fit_sp_curve_by(point: &impl Fn(f64, f64) -> Result<Vec3, String>, uv: &NurbsCurve, [t0, t1]: [f64; 2]) -> Result<NurbsCurve, String> {
let mut breaks = vec![t0];
breaks.extend(uv.knots.iter().copied().filter(|&k| k > t0 && k < t1));
breaks.push(t1);
breaks.dedup();
let at = |t: f64| -> Result<Vec3, String> {
let p = uv.evaluate(t)?;
point(p.x, p.y)
};
fit_by_pieces(
breaks.len() - 1,
|i, s| (0..s).map(|k| at(breaks[i] + (breaks[i + 1] - breaks[i]) * k as f64 / s as f64)).collect(),
at(t1)?,
8,
)
}
fn sp_curve_points(surface: &Surface, uv: &NurbsCurve, per_span: usize, [t0, t1]: [f64; 2]) -> Result<Vec<Vec3>, String> {
let mut breaks = vec![t0];
breaks.extend(uv.knots.iter().copied().filter(|&k| k > t0 && k < t1));
breaks.push(t1);
breaks.dedup();
let mut params = Vec::new();
for pair in breaks.windows(2).filter(|w| w[1] > w[0]) {
for k in 0..per_span {
params.push(pair[0] + (pair[1] - pair[0]) * k as f64 / per_span as f64);
}
}
params.push(t1);
params
.into_iter()
.map(|t| {
let p = uv.evaluate(t)?;
surface.point(p.x, p.y)
})
.collect()
}
fn exact_sp_curve(surface: &Surface, uv: &NurbsCurve) -> Option<Curve> {
const M: f64 = MM_PER_METRE;
if let Surface::Plane(f) = surface {
let points = uv
.control_points
.iter()
.map(|c| {
let p = f.origin.scale(c.w).add(f.x.scale(c.x * M)).add(f.y().scale(c.y * M));
crate::Vec4 { x: p.x, y: p.y, z: p.z, w: c.w }
})
.collect();
return NurbsCurve::new(uv.degree, uv.knots.clone(), points).ok().map(Curve::Nurbs);
}
if !surface.is_analytic() {
return None;
}
let plain: Vec<(f64, f64)> = uv.control_points.iter().map(|c| (c.x / c.w, c.y / c.w)).collect();
let (first, last) = (*plain.first()?, *plain.last()?);
let flat = |pick: fn(&(f64, f64)) -> f64| plain.iter().all(|p| (pick(p) - pick(&first)).abs() <= 1e-12 * (1.0 + pick(&first).abs()));
let (constant_u, constant_v) = (flat(|p| p.0), flat(|p| p.1));
let (along, forward) = match (constant_u, constant_v) {
(true, false) => (false, last.1 > first.1),
(false, true) => (true, last.0 > first.0),
_ => return None,
};
let (u0, v0) = first;
let about_axis = |frame: &Frame| -> Option<Curve> {
let p = surface.point(0.0, v0).ok()?;
let height = p.sub(frame.origin).dot(frame.z);
let centre = frame.origin.add(frame.z.scale(height));
let radius = p.sub(centre).length();
let z = if forward { frame.z } else { frame.z.scale(-1.0) };
(radius > 1e-9).then(|| Curve::Circle { frame: Frame { origin: centre, z, x: frame.x }, radius })
};
let in_half_plane = |frame: &Frame, centre: Vec3, radius: f64| -> Option<Curve> {
let radial = frame.x.scale(u0.cos()).add(frame.y().scale(u0.sin()));
let normal = radial.cross(frame.z);
let normal = if forward { normal } else { normal.scale(-1.0) };
(radius > 1e-9).then(|| Curve::Circle { frame: Frame { origin: centre, z: normal, x: radial }, radius })
};
match (surface, along) {
(Surface::Cylinder { .. } | Surface::Cone { .. }, false) => {
let a = surface.point(u0, v0).ok()?;
let b = surface.point(u0, v0 + 1.0).ok()?;
let direction = b.sub(a).normalized().ok()?;
let direction = if forward { direction } else { direction.scale(-1.0) };
Some(Curve::Line { point: a, direction })
}
(Surface::Cylinder { frame, .. } | Surface::Cone { frame, .. } | Surface::Sphere { frame, .. } | Surface::Torus { frame, .. }, true) => {
about_axis(frame)
}
(Surface::Sphere { frame, radius }, false) => in_half_plane(frame, frame.origin, *radius),
(Surface::Torus { frame, major, minor }, false) => {
let radial = frame.x.scale(u0.cos()).add(frame.y().scale(u0.sin()));
in_half_plane(frame, frame.origin.add(radial.scale(*major)), *minor)
}
_ => None,
}
}
fn snap_ends(mut curve: NurbsCurve, (start, end): (Vec3, Vec3)) -> NurbsCurve {
let n = curve.control_points.len();
if n < 2 {
return curve;
}
let point = |c: &crate::Vec4| Vec3::new(c.x / c.w, c.y / c.w, c.z / c.w);
let (first, last) = (point(&curve.control_points[0]), point(&curve.control_points[n - 1]));
let (a, b) = if first.sub(start).length() + last.sub(end).length() <= first.sub(end).length() + last.sub(start).length() {
(start, end)
} else {
(end, start)
};
if first.sub(a).length() > 0.1 || last.sub(b).length() > 0.1 {
return curve;
}
for (k, p) in [(0, a), (n - 1, b)] {
let w = curve.control_points[k].w;
curve.control_points[k] = crate::Vec4 { x: p.x * w, y: p.y * w, z: p.z * w, w };
}
curve
}
pub(super) fn edge_fins<'f>(file: &'f XtFile, edge: &Node) -> Result<(&'f Node, &'f Node), &'static str> {
let first = file.follow(edge, "halfedge").ok_or("has no fin")?;
let other = file.follow(first, "other").ok_or("has one fin")?;
Ok(if first.char("sense") == Some(b'-') && other.char("sense") != Some(b'-') { (other, first) } else { (first, other) })
}
pub(super) fn spine_range(spine: &Curve, points: &[Vec3], margin: f64, extend: bool) -> Option<[f64; 2]> {
let params: Vec<f64> = match spine {
Curve::Line { point, direction } => points.iter().map(|p| p.sub(*point).dot(*direction) / MM_PER_METRE).collect(),
Curve::Circle { frame, .. } | Curve::Ellipse { frame, .. } => {
let angle = |p: &Vec3| match spine {
Curve::Ellipse { .. } => geometry::nearest_parameter(spine, [false, false], |q| q.sub(*p).length()).unwrap_or(f64::NAN),
_ => {
let d = p.sub(frame.origin);
d.dot(frame.y()).atan2(d.dot(frame.x))
}
};
let mut angles: Vec<f64> = points.iter().map(|p| angle(p).rem_euclid(std::f64::consts::TAU)).filter(|a| a.is_finite()).collect();
angles.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
let first = *angles.first()?;
let (mut gap, mut after) = (first + std::f64::consts::TAU - angles[angles.len() - 1], first);
for pair in angles.windows(2) {
if pair[1] - pair[0] > gap {
(gap, after) = (pair[1] - pair[0], pair[1]);
}
}
if gap < std::f64::consts::FRAC_PI_4 {
return Some([0.0, std::f64::consts::TAU]);
}
angles.iter().map(|a| after + (a - after).rem_euclid(std::f64::consts::TAU)).collect()
}
Curve::Nurbs(curve) => {
let feet: Vec<f64> = points.iter().filter_map(|p| crate::project_point_to_curve(curve, *p).ok().map(|f| f.u)).collect();
if let Some([a, b]) = geometry::closed_range(spine) {
let period = b - a;
let mut along: Vec<f64> = feet.iter().map(|t| a + (t - a).rem_euclid(period)).collect();
along.sort_by(|x, y| x.total_cmp(y));
let first = *along.first()?;
let (mut gap, mut after) = (first + period - along[along.len() - 1], first);
for pair in along.windows(2) {
if pair[1] - pair[0] > gap {
(gap, after) = (pair[1] - pair[0], pair[1]);
}
}
if gap < period / 8.0 {
return Some([a, b]);
}
let span = period - gap;
let pad = (span * margin).min(0.5 * gap);
return Some([after - pad, after + span + pad]);
}
let [a, b] = curve.domain().ok()?;
if !extend {
return range_of(&feet, margin, [a, b]);
}
points
.iter()
.zip(&feet)
.map(|(p, &foot)| {
if foot > a && foot < b {
return foot;
}
geometry::nearest_parameter(spine, [true, true], |q| q.sub(*p).length()).unwrap_or(foot)
})
.collect()
}
};
let (lo, hi) = params.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(a, b), &x| (a.min(x), b.max(x)));
if !(hi > lo) {
return None;
}
let margin = (hi - lo) * margin;
let (mut lo, mut hi) = (lo - margin, hi + margin);
if let Curve::Nurbs(curve) = spine {
let [a, b] = curve.domain().ok()?;
let (first, last) = params.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(x, y), &t| (x.min(t), y.max(t)));
(lo, hi) = (lo.max(a.min(first)), hi.min(b.max(last)));
}
Some([lo, hi])
}
fn range_of(params: &[f64], margin: f64, [a, b]: [f64; 2]) -> Option<[f64; 2]> {
let (lo, hi) = params.iter().fold((f64::INFINITY, f64::NEG_INFINITY), |(x, y), &t| (x.min(t), y.max(t)));
if !(hi > lo) {
return None;
}
let pad = (hi - lo) * margin;
Some([(lo - pad).max(a), (hi + pad).min(b)])
}
pub(super) fn section_at_angle(s: &[crate::Vec4; 3], centre: Vec3, v: f64) -> Vec3 {
let at = |c: &crate::Vec4| Vec3::new(c.x / c.w, c.y / c.w, c.z / c.w);
let (a, b) = (at(&s[0]).sub(centre), at(&s[2]).sub(centre));
let angle = (a.dot(b) / (a.length() * b.length()).max(1e-300)).clamp(-1.0, 1.0).acos();
if !(angle > 1e-9) {
return at(&s[0]);
}
let half = 0.5 * angle;
let w = 0.5 + 0.5 * ((v * angle - half) / 2.0).tan() / (half / 2.0).tan();
let basis = [(1.0 - w) * (1.0 - w), 2.0 * w * (1.0 - w), w * w];
let (mut x, mut y, mut z, mut h) = (0.0, 0.0, 0.0, 0.0);
for (k, c) in basis.iter().zip(s) {
(x, y, z, h) = (x + k * c.x, y + k * c.y, z + k * c.z, h + k * c.w);
}
Vec3::new(x / h, y / h, z / h)
}
pub(super) enum Support {
Surface(Surface),
Cliff(Curve),
Pipe { spine: Curve, radius: f64, inner: Option<Box<([Support; 2], f64)>> },
}
fn nearest_on(curve: &Curve, x: Vec3) -> Option<(f64, Vec3)> {
if let Curve::Line { point, direction } = curve {
let d = direction.normalized().ok()?;
let s = x.sub(*point).dot(d);
return Some((s / (direction.length() * MM_PER_METRE), point.add(d.scale(s))));
}
let k = geometry::nearest_parameter(curve, [true, true], |q| q.sub(x).length())?;
Some((k, geometry::extended_point(curve, k).ok()?))
}
fn pipe_axis_point(spine: &Curve, inner: &Option<Box<([Support; 2], f64)>>, x: Vec3) -> Option<Vec3> {
let (mut k, near) = nearest_on(spine, x)?;
let Some(inner) = inner else { return Some(near) };
let centre = |k: f64| ball_centre(&inner.0, inner.1, spine, k).ok();
let h = match spine.natural_range() {
Some([a, b]) if b > a => 1e-6 * (b - a),
_ => 1e-6 / MM_PER_METRE,
};
for _ in 0..12 {
let c = centre(k)?;
let slope = centre(k + h)?.sub(centre(k - h)?).scale(0.5 / h);
let speed = slope.dot(slope);
if !(speed > 0.0) {
return Some(c);
}
let step = x.sub(c).dot(slope) / speed;
k += step;
if step.abs() <= 1e-13 * (1.0 + k.abs()) {
break;
}
}
centre(k)
}
fn support_distance(s: &Support, x: Vec3) -> Option<(f64, Vec3)> {
match s {
Support::Surface(surface) => surface.distance(x),
Support::Pipe { spine, radius, inner } => {
let out = x.sub(pipe_axis_point(spine, inner, x)?);
Some((out.length() - radius, out.normalized().ok()?))
}
Support::Cliff(edge) => {
let out = x.sub(nearest_on(edge, x)?.1);
Some((out.length(), out.normalized().ok()?))
}
}
}
fn ball_centre(supports: &[Support; 2], radius: f64, spine: &Curve, t: f64) -> Result<Vec3, String> {
let mut centre = geometry::extended_point(spine, t)?;
let beyond = matches!(spine, Curve::Nurbs(_))
&& geometry::closed_range(spine).is_none()
&& spine.natural_range().is_some_and(|[a, b]| t < a || t > b);
if supports.iter().all(|s| matches!(s, Support::Surface(_))) && !beyond {
return Ok(centre);
}
let Some(tangent) = geometry::curve_tangent(spine, t) else { return Ok(centre) };
for _ in 0..8 {
let (Some((d0, g0)), Some((d1, g1))) = (support_distance(&supports[0], centre), support_distance(&supports[1], centre)) else {
return Ok(centre);
};
let (f0, f1) = (d0 - radius * d0.signum(), d1 - radius * d1.signum());
if f0.abs().max(f1.abs()) < 1e-10 * (1.0 + radius) {
break;
}
let across = |g: Vec3| g.sub(tangent.scale(g.dot(tangent)));
let (u, v) = (across(g0), across(g1));
let (a, b, c, d) = (g0.dot(u), g0.dot(v), g1.dot(u), g1.dot(v));
let det = a * d - b * c;
if det.abs() < 1e-12 {
break;
}
let alpha = (-f0 * d + f1 * b) / det;
let beta = (-a * f1 + c * f0) / det;
centre = centre.add(u.scale(alpha)).add(v.scale(beta));
}
Ok(centre)
}
pub(super) fn blend_section(supports: &[Support; 2], radius: f64, spine: &Curve, t: f64) -> Result<[crate::Vec4; 3], String> {
let centre = ball_centre(supports, radius, spine, t)?;
let foot = |s: &Support| -> Result<Vec3, String> {
match s {
Support::Surface(s) => {
let (distance, gradient) = s.distance(centre).ok_or("a supporting surface has no distance there")?;
if (distance.abs() - radius).abs() > 0.05 * radius + 1e-6 {
return Err(format!("the spine stands {:.3e} mm from a support, not the radius {radius:.3e} mm", distance.abs()));
}
Ok(centre.sub(gradient.scale(distance)))
}
Support::Pipe { spine: axis, radius: tube, inner } => {
let near = pipe_axis_point(axis, inner, centre).ok_or("the supporting blend's spine does not reach the section")?;
let out = centre.sub(near);
let distance = (out.length() - tube).abs();
if (distance - radius).abs() > 0.05 * radius + 1e-6 {
return Err(format!("the spine stands {distance:.3e} mm from a supporting blend, not the radius {radius:.3e} mm"));
}
Ok(near.add(out.normalized()?.scale(*tube)))
}
Support::Cliff(edge) => {
let tangent = geometry::curve_tangent(spine, t).ok_or("the spine has no tangent there")?;
let miss = |q: Vec3| q.sub(centre).dot(tangent).hypot(q.sub(centre).length() - radius);
let q = if let Curve::Line { point, direction } = edge {
let along = direction.dot(tangent);
if along.abs() < 1e-12 {
return Err("the cliff edge runs along the section's plane".into());
}
point.add(direction.scale(centre.sub(*point).dot(tangent) / along))
} else {
let k = geometry::nearest_parameter(edge, [true, true], miss).ok_or("the cliff edge does not reach the section")?;
geometry::extended_point(edge, k)?
};
if miss(q) > 0.01 * radius {
return Err(format!("the cliff edge passes {:.3e} mm off the section's circle", miss(q)));
}
Ok(q)
}
}
};
let (a, b) = (foot(&supports[0])?.sub(centre), foot(&supports[1])?.sub(centre));
let (la, lb) = (a.length(), b.length());
let cos = (a.dot(b) / (la * lb)).clamp(-1.0, 1.0);
let angle = cos.acos();
let at = |p: Vec3, w: f64| crate::Vec4 { x: p.x * w, y: p.y * w, z: p.z * w, w };
if angle <= 1e-9 {
let foot = centre.add(a);
return Ok([at(foot, 1.0), at(foot, 1.0), at(foot, 1.0)]);
}
if angle >= 170f64.to_radians() {
return Err(format!("a cross-section subtends {:.1} degrees", angle.to_degrees()));
}
let half = angle / 2.0;
let bisector = a.scale(1.0 / la).add(b.scale(1.0 / lb)).normalized()?;
let apex = centre.add(bisector.scale(0.5 * (la + lb) / half.cos()));
let w = half.cos();
Ok([at(centre.add(a), 1.0), at(apex, w), at(centre.add(b), 1.0)])
}
pub(super) fn fit_blend(section: impl Fn(f64) -> Result<[crate::Vec4; 3], String>, t0: f64, t1: f64) -> Result<NurbsSurface, String> {
fit_blend_split(§ion, t0, t1, 0)
}
fn fit_blend_split(section: &impl Fn(f64) -> Result<[crate::Vec4; 3], String>, t0: f64, t1: f64, depth: usize) -> Result<NurbsSurface, String> {
let (surface, worst, worst_t) = fit_blend_once(section, t0, t1)?;
if worst <= FIT_REFUSAL {
return Ok(surface);
}
let refusal = || format!("no surface through its sections comes within {FIT_REFUSAL} mm of it (closest {worst:.2e} mm)");
if depth >= 4 {
return Err(refusal());
}
let Some(kink) = section_kink(section, t0, t1, worst_t) else { return Err(refusal()) };
if !(kink > t0 + 1e-6 * (t1 - t0) && kink < t1 - 1e-6 * (t1 - t0)) {
return Err(refusal());
}
let left = fit_blend_split(section, t0, kink, depth + 1)?;
let right = fit_blend_split(section, kink, t1, depth + 1)?;
join_c0_in_u(&left, &right).ok_or_else(refusal)
}
fn section_kink(section: &impl Fn(f64) -> Result<[crate::Vec4; 3], String>, t0: f64, t1: f64, around: f64) -> Option<f64> {
let point = |t: f64| -> Option<[Vec3; 3]> {
let s = section(t).ok()?;
Some([0, 1, 2].map(|j| Vec3::new(s[j].x / s[j].w, s[j].y / s[j].w, s[j].z / s[j].w)))
};
let jump = |t: f64, h: f64| -> f64 {
let (Some(a), Some(b), Some(c)) = (point(t - h), point(t), point(t + h)) else { return 0.0 };
(0..3).map(|j| c[j].sub(b[j]).sub(b[j].sub(a[j])).length() / h).fold(0.0, f64::max)
};
let (mut lo, mut hi) = ((around - 0.02 * (t1 - t0)).max(t0), (around + 0.02 * (t1 - t0)).min(t1));
let mut best = around;
for _ in 0..8 {
let h = (hi - lo) / 128.0;
best = (0..=64).map(|k| lo + (hi - lo) * k as f64 / 64.0).max_by(|a, b| jump(*a, h).total_cmp(&jump(*b, h)))?;
let width = (hi - lo) / 16.0;
(lo, hi) = ((best - width).max(t0), (best + width).min(t1));
}
(jump(best, 1e-9 * (t1 - t0)) > 0.0).then_some(best)
}
fn join_c0_in_u(left: &NurbsSurface, right: &NurbsSurface) -> Option<NurbsSurface> {
if left.degree_u != right.degree_u || left.degree_v != right.degree_v || left.knots_v != right.knots_v {
return None;
}
let p = left.degree_u;
let mut knots = left.knots_u[..left.knots_u.len() - 1].to_vec();
knots.extend_from_slice(&right.knots_u[p + 1..]);
let mut net = left.control_points.clone();
net.extend(right.control_points.iter().skip(1).cloned());
NurbsSurface::new(p, left.degree_v, knots, left.knots_v.clone(), net).ok()
}
pub(super) fn support_knot_crossings(supports: &[Support; 2], section: &impl Fn(f64) -> Result<[crate::Vec4; 3], String>, t0: f64, t1: f64) -> Vec<f64> {
let mut out = Vec::new();
for (j, support) in supports.iter().enumerate() {
if let Support::Surface(Surface::Swept { section: profile @ Curve::Nurbs(curve), direction }) = support {
let Ok(along) = direction.normalized() else { continue };
let lines = c1_lines(&curve.knots, curve.degree);
if lines.is_empty() {
continue;
}
let end = if j == 0 { 0 } else { 2 };
let foot = |t: f64| -> Option<f64> {
let s = section(t).ok()?;
let p = Vec3::new(s[end].x / s[end].w, s[end].y / s[end].w, s[end].z / s[end].w);
geometry::nearest_parameter(profile, [false, false], |q| {
let d = q.sub(p);
d.sub(along.scale(d.dot(along))).length()
})
};
crossings_of(&foot, &lines, t0, t1, &mut out);
continue;
}
let Support::Surface(Surface::Nurbs(surface)) = support else { continue };
let lines_uv = [c1_lines(&surface.knots_u, surface.degree_u), c1_lines(&surface.knots_v, surface.degree_v)];
if lines_uv.iter().all(|l| l.is_empty()) {
continue;
}
let end = if j == 0 { 0 } else { 2 };
let foot = |t: f64| -> Option<[f64; 2]> {
let s = section(t).ok()?;
let p = Vec3::new(s[end].x / s[end].w, s[end].y / s[end].w, s[end].z / s[end].w);
let f = crate::project_point_to_surface(surface, p).ok()?;
Some([f.u, f.v])
};
let stations = 256;
let mut prev: Option<(f64, [f64; 2])> = None;
for k in 0..=stations {
let t = t0 + (t1 - t0) * k as f64 / stations as f64;
let Some(here) = foot(t) else { prev = None; continue };
if let Some((tp, there)) = prev {
for axis in 0..2 {
for &line in &lines_uv[axis] {
if (here[axis] - line).abs() <= 1e-12 * (1.0 + line.abs()) {
out.push(t);
continue;
}
if (there[axis] - line) * (here[axis] - line) < 0.0 {
let (mut a, mut b) = (tp, t);
for _ in 0..60 {
let m = 0.5 * (a + b);
match foot(m) {
Some(f) if (there[axis] - line) * (f[axis] - line) > 0.0 => a = m,
Some(_) => b = m,
None => break,
}
}
out.push(0.5 * (a + b));
}
}
}
}
prev = Some((t, here));
}
}
let span = t1 - t0;
out.retain(|&t| t > t0 + 1e-6 * span && t < t1 - 1e-6 * span);
out.sort_by(|a, b| a.total_cmp(b));
out.dedup_by(|a, b| (*a - *b).abs() <= 1e-9 * span);
out
}
pub(super) fn c1_lines(knots: &[f64], degree: usize) -> Vec<f64> {
let mut values: Vec<(f64, usize)> = Vec::new();
for &k in knots {
match values.last_mut() {
Some((v, n)) if *v == k => *n += 1,
_ => values.push((k, 1)),
}
}
let (Some(&first), Some(&last)) = (knots.first(), knots.last()) else { return Vec::new() };
values.into_iter().filter(|&(v, n)| n + 1 >= degree && n <= degree && v > first && v < last).map(|(v, _)| v).collect()
}
pub(super) fn crossings_of(foot: &impl Fn(f64) -> Option<f64>, lines: &[f64], t0: f64, t1: f64, out: &mut Vec<f64>) {
let stations = 256;
let mut prev: Option<(f64, f64)> = None;
for k in 0..=stations {
let t = t0 + (t1 - t0) * k as f64 / stations as f64;
let Some(here) = foot(t) else { prev = None; continue };
if let Some((tp, there)) = prev {
for &line in lines {
if (here - line).abs() <= 1e-12 * (1.0 + line.abs()) {
out.push(t);
continue;
}
if (there - line) * (here - line) < 0.0 {
let (mut a, mut b) = (tp, t);
for _ in 0..60 {
let m = 0.5 * (a + b);
match foot(m) {
Some(f) if (there - line) * (f - line) > 0.0 => a = m,
Some(_) => b = m,
None => break,
}
}
out.push(0.5 * (a + b));
}
}
}
prev = Some((t, here));
}
}
fn fit_blend_pieces(section: &impl Fn(f64) -> Result<[crate::Vec4; 3], String>, t0: f64, t1: f64, breaks: &[f64]) -> Result<NurbsSurface, String> {
let mut ends = vec![t0];
ends.extend_from_slice(breaks);
ends.push(t1);
let mut joined: Option<NurbsSurface> = None;
for pair in ends.windows(2) {
let piece = fit_blend_split(section, pair[0], pair[1], 0)?;
joined = Some(match joined {
None => piece,
Some(left) => join_c0_in_u(&left, &piece).ok_or("the pieces do not join")?,
});
}
joined.ok_or_else(|| "the blend has no pieces".to_string())
}
fn fit_blend_once(section: &impl Fn(f64) -> Result<[crate::Vec4; 3], String>, t0: f64, t1: f64) -> Result<(NurbsSurface, f64, f64), String> {
let rational_point = |c: &[crate::Vec4; 3], v: f64| {
let basis = [(1.0 - v) * (1.0 - v), 2.0 * v * (1.0 - v), v * v];
let (mut x, mut y, mut z, mut w) = (0.0, 0.0, 0.0, 0.0);
for (b, p) in basis.iter().zip(c) {
(x, y, z, w) = (x + b * p.x, y + b * p.y, z + b * p.z, w + b * p.w);
}
Vec3::new(x / w, y / w, z / w)
};
const MAX_STATIONS: usize = 1024;
let mut best: Option<(NurbsSurface, f64, f64)> = None;
let mut params: Vec<f64> = (0..=8).map(|k| t0 + (t1 - t0) * k as f64 / 8.0).collect();
let mut sections: Vec<[crate::Vec4; 3]> = params.iter().map(|&t| section(t)).collect::<Result<_, _>>()?;
let mut stalled = 0;
let mut kept: Vec<Option<[crate::Vec4; 3]>> = Vec::new();
loop {
let degree = (params.len() - 1).min(3);
let mut columns = Vec::with_capacity(3);
let mut knots = Vec::new();
for j in 0..3 {
let xyz: Vec<Vec3> = sections.iter().map(|s| Vec3::new(s[j].x, s[j].y, s[j].z)).collect();
let weights: Vec<Vec3> = sections.iter().map(|s| Vec3::new(s[j].w, 0.0, 0.0)).collect();
let xyz = crate::interpolate_curve(&xyz, degree, ¶ms)?;
let weights = crate::interpolate_curve(&weights, degree, ¶ms)?;
knots = xyz.knots.clone();
let column: Vec<crate::Vec4> = xyz
.control_points
.iter()
.zip(&weights.control_points)
.map(|(p, w)| crate::Vec4 { x: p.x / p.w, y: p.y / p.w, z: p.z / p.w, w: w.x / w.w })
.collect();
columns.push(column);
}
let net: Vec<Vec<crate::Vec4>> = (0..columns[0].len()).map(|i| columns.iter().map(|c| c[i]).collect()).collect();
let surface = NurbsSurface::new(degree, 2, knots, vec![0.0, 0.0, 0.0, 1.0, 1.0, 1.0], net)?;
let mut misses = Vec::with_capacity(params.len() - 1);
for (k, pair) in params.windows(2).enumerate() {
let t = 0.5 * (pair[0] + pair[1]);
let exact = match kept.get(k) {
Some(&Some(exact)) => exact,
_ => section(t)?,
};
let mut miss = 0.0f64;
for v in [0.0, 0.25, 0.5, 0.75, 1.0] {
miss = miss.max(surface.evaluate(t, v)?.sub(rational_point(&exact, v)).length());
}
misses.push((t, exact, miss));
}
let (worst_t, worst) = misses.iter().fold((t0, 0.0f64), |(wt, w), m| if m.2 > w { (m.0, m.2) } else { (wt, w) });
if worst <= FIT_TOLERANCE {
return Ok((surface, worst, worst_t));
}
stalled = if best.as_ref().is_some_and(|(_, b, _)| worst > 0.5 * b) { stalled + 1 } else { 0 };
if best.as_ref().map_or(true, |(_, b, _)| worst < *b) {
best = Some((surface, worst, worst_t));
}
let missing = misses.iter().filter(|m| m.2 > FIT_TOLERANCE).count();
if stalled >= 3 || params.len() + missing > MAX_STATIONS + 1 {
break;
}
let (mut next_params, mut next_sections) = (Vec::with_capacity(params.len() + missing), Vec::with_capacity(params.len() + missing));
let mut next_kept = Vec::with_capacity(params.len() + missing);
for (k, (t, exact, miss)) in misses.into_iter().enumerate() {
next_params.push(params[k]);
next_sections.push(sections[k]);
if miss > FIT_TOLERANCE {
next_params.push(t);
next_sections.push(exact);
next_kept.extend([None, None]);
} else {
next_kept.push(Some(exact));
}
}
kept = next_kept;
next_params.push(params[params.len() - 1]);
next_sections.push(sections[sections.len() - 1]);
(params, sections) = (next_params, next_sections);
}
best.ok_or_else(|| "the blend has no sections".to_string())
}
fn fit_through(points: &[Vec3]) -> Result<NurbsCurve, String> {
let mut stations = Vec::with_capacity(points.len());
for p in points {
if stations.last().map_or(true, |q: &Vec3| p.sub(*q).length() > 1e-12) {
stations.push(*p);
}
}
if stations.len() < 2 {
return Err("a fitted curve has fewer than two distinct points".into());
}
let mut params = vec![0.0];
for w in stations.windows(2) {
params.push(params[params.len() - 1] + w[1].sub(w[0]).length());
}
let degree = (stations.len() - 1).min(3);
crate::interpolate_curve(&stations, degree, ¶ms)
}
fn knot_runs(knots: &[f64]) -> (String, String) {
let mut mults = Vec::new();
let mut values = Vec::new();
for &k in knots {
if values.last() == Some(&k) {
*mults.last_mut().expect("paired with values") += 1;
} else {
values.push(k);
mults.push(1usize);
}
}
(
mults.iter().map(|m| m.to_string()).collect::<Vec<_>>().join(","),
values.iter().map(|v| real(*v)).collect::<Vec<_>>().join(","),
)
}