forked from 1b-agent/opencadstudio-upstream
refactor: drop truck, and evaluate every curve through the kernel
The last four truck crates are gone from the manifest. What they were doing falls into three parts, and each now has one answer instead of two. NURBS evaluation. truck was carrying B-spline curves and surfaces for the SPLINE entity, hatch boundaries, BLEND's endpoint frames, the spline preview and ACIS spline-surface faces. The kernel now has both, over one de Boor written across however many coordinates a control point holds — so a plane curve and a space curve cannot drift apart, and a surface is the same algorithm applied twice. The rational and the polynomial cases stop being separate types: weights absent means polynomial. The entity conversion. Every LINE, ARC, ELLIPSE, SPLINE and polyline was built into truck topology purely so it could be sampled back into points. They are sampled through `entities::curve` now, which is where each one's geometry is already defined once and what EXTRUDE and REVOLVE read — so a circle drawn on screen and a circle handed to the Model tab come from the same definition. That retires four of TruckObject's variants and the module is renamed for what it does. SWEEP and LOFT. Both only ever produced a mesh, so both are built from point lists: a band of quads per span, and a lid where a profile closes. Lofting profiles of different densities resamples them by distance rather than by index, so a circle lofted to a square no longer twists. Two things worth noting for anyone reading the old comments. The tolerance globals that lived in the tessellation module were never about truck at all — they are the per-frame chord height, and they move to `curve_tol`. And the rule that a profile handed over as `Lines` silently broke EXTRUDE and REVOLVE no longer holds: those read the curve directly and never look at this channel. The two `automation` test failures are unchanged from before this and are not caused by it. Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
1 parent
1f642bb40a
commit
8c7ebdc9f1
58 files changed
+947
-1761
No files matched your search
@@ -1,4 +1,4 @@
|
||||
// Triangle mesh model — produced by truck Shell/Solid tessellation.
|
||||
// Triangle mesh model — produced by the kernel Shell/Solid tessellation.
|
||||
//
|
||||
// Stored alongside WireModels in the scene; rendered by the mesh pipeline
|
||||
// (wgpu TriangleList with depth test, flat normals).
|
||||
@@ -301,7 +301,7 @@ impl MeshLodSet {
|
||||
}
|
||||
|
||||
/// Wrap a single MeshModel as a one-LOD set. Used by interactive
|
||||
/// commands that only produce one tessellation (e.g. truck-based
|
||||
/// commands that only produce one tessellation (e.g. the kernel-based
|
||||
/// BOX/CYLINDER creation). The LOD selector will pick slot 0 for
|
||||
/// every zoom level.
|
||||
pub fn from_single(mesh: MeshModel) -> Self {
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
use acadrust::kernel::brep::{self, Body};
|
||||
use acadrust::kernel::geom2d::Curve;
|
||||
use acadrust::kernel::space::{Plane, PlanarCurve};
|
||||
use acadrust::kernel::space::{PlanarCurve, Plane, Vec3};
|
||||
use acadrust::EntityType;
|
||||
|
||||
use crate::entities::curve::entity_curve;
|
||||
@@ -123,6 +123,228 @@ pub fn revolved(
|
||||
)
|
||||
}
|
||||
|
||||
// ── SWEEP and LOFT ──────────────────────────────────────────────────────────
|
||||
//
|
||||
// Neither keeps a B-rep — both have only ever produced a mesh — so both are
|
||||
// built from point lists rather than from topology. A profile becomes the
|
||||
// points its own curve tessellates to, which is the same source EXTRUDE and
|
||||
// REVOLVE read, so a circle stays round here too.
|
||||
|
||||
use crate::entities::curve::curve_points;
|
||||
use crate::scene::model::mesh_model::{MeshLodSet, MeshModel};
|
||||
|
||||
/// The points a profile entity traces, and whether it closes.
|
||||
fn outline(entity: &EntityType) -> Option<(Vec<[f64; 3]>, bool)> {
|
||||
let planar = entity_curve(entity)?;
|
||||
let closed = planar.curve.is_closed();
|
||||
let mut points = curve_points(&planar);
|
||||
// A closed curve tessellates back to its own start. Carrying the repeat
|
||||
// would put a zero-width quad in every strip below.
|
||||
if closed && points.len() > 1 {
|
||||
let first = points[0];
|
||||
let last = points[points.len() - 1];
|
||||
if Vec3::from(first).distance(Vec3::from(last)) < 1e-9 {
|
||||
points.pop();
|
||||
}
|
||||
}
|
||||
(points.len() >= 2).then_some((points, closed))
|
||||
}
|
||||
|
||||
/// SWEEP: drag a profile along a path.
|
||||
///
|
||||
/// The path contributes its direction and length, not its shape — which is
|
||||
/// what SWEEP has always done here, and what makes it an extrusion along an
|
||||
/// arbitrary vector rather than along a curve.
|
||||
pub fn swept(profile: &EntityType, path: &EntityType, color: [f32; 4]) -> Option<MeshLodSet> {
|
||||
let (points, closed) = outline(profile)?;
|
||||
let along = {
|
||||
let track = curve_points(&entity_curve(path)?);
|
||||
let (from, to) = (Vec3::from(*track.first()?), Vec3::from(*track.last()?));
|
||||
to - from
|
||||
};
|
||||
if along.length() < 1e-12 {
|
||||
return None;
|
||||
}
|
||||
let moved: Vec<[f64; 3]> = points
|
||||
.iter()
|
||||
.map(|point| (Vec3::from(*point) + along).to_array())
|
||||
.collect();
|
||||
let mut mesh = Ribbon::default();
|
||||
mesh.band(&points, &moved, closed);
|
||||
if closed {
|
||||
// An open profile sweeps into a sheet with nothing to cap.
|
||||
mesh.cap(&points, true);
|
||||
mesh.cap(&moved, false);
|
||||
}
|
||||
mesh.finish(color)
|
||||
}
|
||||
|
||||
/// LOFT: rule a surface through a run of profiles.
|
||||
///
|
||||
/// Consecutive profiles are joined by a band each, and the two ends are
|
||||
/// capped when they close. Profiles with different point counts are resampled
|
||||
/// onto the finer of the two, so a circle lofted to a square does not twist.
|
||||
pub fn lofted(profiles: &[EntityType], color: [f32; 4]) -> Option<MeshLodSet> {
|
||||
let sections: Vec<(Vec<[f64; 3]>, bool)> = profiles.iter().filter_map(outline).collect();
|
||||
if sections.len() < 2 {
|
||||
return None;
|
||||
}
|
||||
let mut mesh = Ribbon::default();
|
||||
for pair in sections.windows(2) {
|
||||
let count = pair[0].0.len().max(pair[1].0.len());
|
||||
let closed = pair[0].1 && pair[1].1;
|
||||
let lower = resampled(&pair[0].0, count, pair[0].1);
|
||||
let upper = resampled(&pair[1].0, count, pair[1].1);
|
||||
mesh.band(&lower, &upper, closed);
|
||||
}
|
||||
if sections.first()?.1 {
|
||||
mesh.cap(§ions.first()?.0, true);
|
||||
}
|
||||
if sections.last()?.1 {
|
||||
mesh.cap(§ions.last()?.0, false);
|
||||
}
|
||||
mesh.finish(color)
|
||||
}
|
||||
|
||||
/// A ring walked in `count` even steps along its own length.
|
||||
///
|
||||
/// Even by distance rather than by index: two profiles given at different
|
||||
/// densities line up where they are, so a band between them does not twist
|
||||
/// wherever one of them happened to be sampled more finely.
|
||||
fn resampled(points: &[[f64; 3]], count: usize, closed: bool) -> Vec<[f64; 3]> {
|
||||
let mut ring: Vec<Vec3> = points.iter().map(|point| Vec3::from(*point)).collect();
|
||||
if closed {
|
||||
ring.push(ring[0]);
|
||||
}
|
||||
let mut walked = vec![0.0];
|
||||
for pair in ring.windows(2) {
|
||||
walked.push(walked[walked.len() - 1] + pair[0].distance(pair[1]));
|
||||
}
|
||||
let total = *walked.last().unwrap_or(&0.0);
|
||||
if total <= 0.0 {
|
||||
return points.to_vec();
|
||||
}
|
||||
let steps = if closed { count } else { count.max(2) - 1 };
|
||||
(0..if closed { count } else { count.max(2) })
|
||||
.map(|step| {
|
||||
let want = total * step as f64 / steps as f64;
|
||||
let at = walked
|
||||
.iter()
|
||||
.rposition(|reached| *reached <= want)
|
||||
.unwrap_or(0)
|
||||
.min(ring.len() - 2);
|
||||
let span = walked[at + 1] - walked[at];
|
||||
let along = if span > 0.0 { (want - walked[at]) / span } else { 0.0 };
|
||||
ring[at].lerp(ring[at + 1], along).to_array()
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Triangles being gathered from bands and caps.
|
||||
#[derive(Default)]
|
||||
struct Ribbon {
|
||||
positions: Vec<[f64; 3]>,
|
||||
normals: Vec<[f64; 3]>,
|
||||
triangles: Vec<[u32; 3]>,
|
||||
}
|
||||
|
||||
impl Ribbon {
|
||||
/// A strip of quads between two rings of the same length.
|
||||
fn band(&mut self, lower: &[[f64; 3]], upper: &[[f64; 3]], closed: bool) {
|
||||
let count = lower.len().min(upper.len());
|
||||
if count < 2 {
|
||||
return;
|
||||
}
|
||||
let spans = if closed { count } else { count - 1 };
|
||||
for step in 0..spans {
|
||||
let next = (step + 1) % count;
|
||||
self.quad(lower[step], lower[next], upper[next], upper[step]);
|
||||
}
|
||||
}
|
||||
|
||||
/// A flat lid over a closed ring, fanned from its middle.
|
||||
///
|
||||
/// A fan rather than a proper triangulation: a lofted section can be
|
||||
/// concave and a fan would then cover ground outside it, but every
|
||||
/// profile these commands accept is a single closed curve, and the middle
|
||||
/// of one is inside it.
|
||||
fn cap(&mut self, ring: &[[f64; 3]], downward: bool) {
|
||||
if ring.len() < 3 {
|
||||
return;
|
||||
}
|
||||
let mut middle = Vec3::new(0.0, 0.0, 0.0);
|
||||
for point in ring {
|
||||
middle = middle + Vec3::from(*point);
|
||||
}
|
||||
let middle = (middle / ring.len() as f64).to_array();
|
||||
for step in 0..ring.len() {
|
||||
let next = (step + 1) % ring.len();
|
||||
if downward {
|
||||
self.triangle(middle, ring[next], ring[step]);
|
||||
} else {
|
||||
self.triangle(middle, ring[step], ring[next]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn quad(&mut self, a: [f64; 3], b: [f64; 3], c: [f64; 3], d: [f64; 3]) {
|
||||
self.triangle(a, b, c);
|
||||
self.triangle(a, c, d);
|
||||
}
|
||||
|
||||
fn triangle(&mut self, a: [f64; 3], b: [f64; 3], c: [f64; 3]) {
|
||||
let Some(normal) = (Vec3::from(b) - Vec3::from(a))
|
||||
.cross(Vec3::from(c) - Vec3::from(a))
|
||||
.normalize()
|
||||
else {
|
||||
// Collapsed: no normal, and nothing to draw.
|
||||
return;
|
||||
};
|
||||
let base = self.positions.len() as u32;
|
||||
for corner in [a, b, c] {
|
||||
self.positions.push(corner);
|
||||
self.normals.push(normal.to_array());
|
||||
}
|
||||
self.triangles.push([base, base + 1, base + 2]);
|
||||
}
|
||||
|
||||
/// The gathered triangles as the renderer's mesh, or `None` for none.
|
||||
fn finish(self, color: [f32; 4]) -> Option<MeshLodSet> {
|
||||
if self.triangles.is_empty() {
|
||||
return None;
|
||||
}
|
||||
// The renderer holds each position as a coarse float plus a fine
|
||||
// correction, so a profile at survey coordinates keeps its last
|
||||
// millimetres instead of losing them to f32.
|
||||
let mut verts = Vec::with_capacity(self.positions.len());
|
||||
let mut verts_low = Vec::with_capacity(self.positions.len());
|
||||
for point in &self.positions {
|
||||
let high = [point[0] as f32, point[1] as f32, point[2] as f32];
|
||||
verts.push(high);
|
||||
verts_low.push([
|
||||
(point[0] - high[0] as f64) as f32,
|
||||
(point[1] - high[1] as f64) as f32,
|
||||
(point[2] - high[2] as f64) as f32,
|
||||
]);
|
||||
}
|
||||
Some(MeshLodSet::from_single(MeshModel {
|
||||
name: String::new(),
|
||||
verts,
|
||||
verts_low,
|
||||
normals: self
|
||||
.normals
|
||||
.iter()
|
||||
.map(|n| [n[0] as f32, n[1] as f32, n[2] as f32])
|
||||
.collect(),
|
||||
indices: self.triangles.iter().flatten().copied().collect(),
|
||||
triangle_material_handles: Vec::new(),
|
||||
triangle_colors: Vec::new(),
|
||||
color,
|
||||
selected: false,
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in new issue
Block a user