feat(model): build and read solids with the geometry kernel

Every 3-D solid OCS makes or opens now goes through cadkernel, which holds
analytic surfaces — a cylinder is a cylinder record, not a spline that looks
like one. That is what lets a solid built here be written back out as exact
ACIS instead of a triangulation, and a facetted export is a one-way door: the
next application to open the file would have no way back to the circle.

The Model tab's seven primitives, the Design-group booleans, 3DROTATE,
3DMIRROR, 3DALIGN, SLICE, SECTION, POLYSOLID and PYRAMID all come from the
kernel now, and `scene.solid_models` holds a `Body`. A mirrored solid needed
its own care before; the kernel reverses the loops on the way through, so it
comes back the right way out rather than lighting black.

EXTRUDE and REVOLVE sweep the profile `entities::curve` already defines, so a
circle, an arc-bulged polyline and a closed spline arrive as the same thing.
A circle extrudes into four cylinder patches rather than a run of chords, and
a profile turned about an axis becomes a cone, a sphere or a torus. A spline
profile has no analytic side wall and is refused rather than approximated.

ACIS goes both ways through the codec's own bridge: `lift` to read and
`append` to write. That drops the 679-line truck conversion and the planar-
only exporter, and the tessellator keeps its bespoke sampler as a fallback for
whatever the kernel cannot yet express — reported rather than passed off as
whole, since a mesh missing a wall looks finished.

truck-shapeops is no longer used and is gone from the manifest. truck-modeling
stays for now: fourteen files use it as a 3-D NURBS evaluator, which the
kernel has no answer for yet, and SWEEP and LOFT have no kernel operation
behind them either.

The two `automation` test failures are unchanged from before this and are not
caused by it.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Hakan Seven 2026-08-10 00:32:34 +03:00
commit 1f642bb40a
15 changed files with 912 additions and 1634 deletions

View file

@ -1,131 +1,32 @@
//! Export a truck B-rep `Solid` to an exact ACIS `SatDocument`.
//! Export a B-rep [`Body`] to an exact ACIS `SatDocument`.
//!
//! OCS models 3-D solids with truck (`scene.solid_models`), but EXTRUDE /
//! REVOLVE / SWEEP / LOFT / boolean results carry no ACIS geometry, so other
//! CAD applications drop them on open. Converting the truck solid to ACIS lets
//! the DWG/DXF writer store real modeler geometry.
//! A solid built by the Model tab, by EXTRUDE / REVOLVE, or by a boolean
//! carries no ACIS geometry of its own, so other CAD applications drop it on
//! open. Writing the body out as real modeller geometry is what stops that.
//!
//! This module handles the **planar** case: a solid whose faces are all planes
//! bounded by straight edges (boxes, extruded polygons, planar boolean results).
//! It walks the topology into a vertex/face-ring mesh and hands it to acadrust's
//! [`build_planar_body`], which assembles the exact B-rep. Any curved face or
//! edge makes it return `None` — those solids are left for the NURBS path.
//! # Exact, not facetted
//!
//! The kernel keeps analytic surfaces — a cylinder is a `cylinder` record and
//! a sphere a `sphere` — so what lands in the document is the shape itself
//! rather than a triangulation of it. That is the whole reason the Model tab
//! builds bodies at all: a facetted export is a one-way door, and the next
//! application to open the file would have no way back to the circle.
//!
//! The writing is [`acis::append`], which lives with the codec because it has
//! to know both sides. What is left here is the small matter of putting one
//! body into a fresh document.
use std::collections::HashMap;
use acadrust::acis::append;
use acadrust::entities::acis::SatDocument;
use acadrust::kernel::brep::Body;
use acadrust::entities::acis::{primitives::build_planar_body, SatDocument};
use truck_modeling::{Curve, Solid, Surface};
/// Signed volume × 6 of the closed mesh (positive when every face ring is wound
/// counter-clockwise as seen from outside). Used to make the export independent
/// of truck's face-orientation convention.
fn signed_volume6(vertices: &[[f64; 3]], faces: &[Vec<usize>]) -> f64 {
let mut acc = 0.0;
for f in faces {
let a = vertices[f[0]];
// Fan-triangulate the (planar, convex-or-not) ring from its first vertex.
for i in 1..f.len() - 1 {
let b = vertices[f[i]];
let c = vertices[f[i + 1]];
acc += a[0] * (b[1] * c[2] - b[2] * c[1])
- a[1] * (b[0] * c[2] - b[2] * c[0])
+ a[2] * (b[0] * c[1] - b[1] * c[0]);
}
}
acc
}
/// Build an exact ACIS `SatDocument` from a truck solid whose faces are all
/// planar and edges all straight. Returns `None` for any curved face/edge (left
/// for the NURBS export), for faces with holes, or for a degenerate body.
pub fn planar_solid_to_sat(solid: &Solid) -> Option<SatDocument> {
let mut positions: Vec<[f64; 3]> = Vec::new();
let mut vert_index: HashMap<_, usize> = HashMap::new();
let mut faces: Vec<Vec<usize>> = Vec::new();
for shell in solid.boundaries() {
for face in shell.face_iter() {
// Planar faces only — a curved surface needs the NURBS path.
if !matches!(face.surface(), Surface::Plane(_)) {
return None;
}
// Single outer loop only (no holes) in the planar path.
let boundaries = face.boundaries();
if boundaries.len() != 1 {
return None;
}
let wire = &boundaries[0];
// Every bounding edge must be a straight line.
for edge in wire.edge_iter() {
if !matches!(edge.curve(), Curve::Line(_)) {
return None;
}
}
// Ordered vertex ring of the loop.
let mut ring: Vec<usize> = Vec::new();
for v in wire.vertex_iter() {
let key = v.id();
let p = v.point();
let idx = *vert_index.entry(key).or_insert_with(|| {
positions.push([p.x, p.y, p.z]);
positions.len() - 1
});
// Drop a closing vertex that repeats the ring's start.
if ring.last() != Some(&idx) {
ring.push(idx);
}
}
if ring.len() >= 2 && ring.first() == ring.last() {
ring.pop();
}
if ring.len() < 3 {
return None;
}
faces.push(ring);
}
}
if faces.len() < 4 || positions.len() < 4 {
return None;
}
// Make the winding outward-CCW regardless of truck's convention: a
// correctly-oriented closed shell has positive signed volume; if it came out
// negative every ring is inside-out, so reverse them all.
if signed_volume6(&positions, &faces) < 0.0 {
for f in faces.iter_mut() {
f.reverse();
}
}
build_planar_body(&positions, &faces)
}
#[cfg(test)]
mod tests {
use super::*;
use truck_modeling::builder;
use truck_modeling::{Point3, Vector3};
/// A truck box (dimension-raising sweep vertex→edge→face→solid) must export
/// to a valid 6-face / 12-edge / 8-vertex ACIS body.
#[test]
fn box_exports_to_valid_planar_sat() {
let p = builder::vertex(Point3::new(0.0, 0.0, 0.0));
let e = builder::tsweep(&p, Vector3::new(2.0, 0.0, 0.0));
let f = builder::tsweep(&e, Vector3::new(0.0, 2.0, 0.0));
let solid = builder::tsweep(&f, Vector3::new(0.0, 0.0, 2.0));
let sat = planar_solid_to_sat(&solid).expect("box should export to SAT");
assert_eq!(sat.faces().len(), 6, "box has 6 planar faces");
assert_eq!(sat.edges().len(), 12, "box has 12 straight edges");
assert_eq!(sat.vertices().len(), 8, "box has 8 vertices");
assert!(
sat.validate().is_empty(),
"exported box failed ACIS validation: {:?}",
sat.validate()
);
}
/// An exact ACIS document holding `body`, or `None` when the kernel has no
/// record form for something in it — a spline curve, at present.
///
/// Refusing is the point: a document missing a face is one that parses into a
/// different solid, and nothing downstream could tell.
pub fn planar_solid_to_sat(body: &Body) -> Option<SatDocument> {
let mut document = SatDocument::new();
append(body, &mut document).ok()?;
Some(document)
}

View file

@ -0,0 +1,128 @@
//! Drawing an ACIS solid by lifting it into the geometry kernel.
//!
//! A DWG or DXF carries its solids as ACIS records — analytic surfaces and the
//! topology joining them — and the kernel holds exactly that. So the shortest
//! path from a file to a picture is to lift the document into a `Body` and ask
//! the kernel for triangles, rather than to re-derive each surface's extent by
//! sampling it.
//!
//! # Why it can still fall short
//!
//! A face on a surface the kernel does not model, a curve it has no form for,
//! a pointer graph that does not hold together: [`lift`] reports each as a
//! [`Loss`] rather than quietly dropping it. What comes back then is a body
//! with faces missing, and the mesh it makes has holes — which is why the
//! result is marked incomplete and the caller keeps its own sampler for those.
//!
//! Saying so is the point. A partial mesh that claimed to be whole would show
//! a solid with a wall missing and nothing to suggest anything was wrong.
use acadrust::acis::lift;
use acadrust::entities::acis::SatDocument;
use acadrust::kernel::brep;
use crate::scene::model::mesh_model::{MeshLodSet, MeshModel};
/// How far a triangle may sit from the surface it lies on.
const SAG: f64 = 0.05;
/// What counts as the same point when the kernel reads a body over.
const TOL: f64 = 1e-9;
/// Tessellate an ACIS document by lifting it into the kernel.
///
/// `None` when nothing in the document lifts at all. The result's `complete`
/// flag says whether every face made it; a caller with a fallback sampler
/// uses it to decide whether to run one.
pub fn tessellate_sat(
document: &SatDocument,
name: String,
color: [f32; 4],
sag: f64,
) -> Option<MeshLodSet> {
let (bodies, loss) = lift(document);
if bodies.is_empty() {
return None;
}
let sag = if sag > 0.0 { sag } else { SAG };
let mut positions: Vec<[f64; 3]> = Vec::new();
let mut normals: Vec<[f32; 3]> = Vec::new();
let mut indices: Vec<u32> = Vec::new();
// A face the kernel holds but cannot express in its surface's own
// parameters leaves a hole, the same as one that never lifted — so both
// are counted before calling the mesh whole.
let mut undrawn = 0usize;
for body in &bodies {
for face in body.face_keys() {
let Some(mesh) = brep::mesh::face(body, face, sag, TOL) else {
undrawn += 1;
continue;
};
let base = positions.len() as u32;
positions.extend_from_slice(&mesh.positions);
normals.extend(
mesh.normals
.iter()
.map(|n| [n[0] as f32, n[1] as f32, n[2] as f32]),
);
indices.extend(
mesh.triangles
.iter()
.flat_map(|t| [base + t[0] as u32, base + t[1] as u32, base + t[2] as u32]),
);
}
}
if indices.is_empty() {
return None;
}
// The renderer holds each position as a coarse float plus a fine
// correction, so a solid at survey coordinates keeps its last millimetres
// instead of losing them to f32.
let mut verts = Vec::with_capacity(positions.len());
let mut verts_low = Vec::with_capacity(positions.len());
for point in &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,
]);
}
let mut set = MeshLodSet::from_single(MeshModel {
name,
verts,
verts_low,
normals,
indices,
triangle_material_handles: Vec::new(),
triangle_colors: Vec::new(),
color,
selected: false,
});
set.complete = loss.is_empty() && undrawn == 0;
Some(set)
}
/// The edges of every body in an ACIS document, as polylines.
///
/// What draws a solid's wireframe and what a click hit-tests against. Taken
/// from the kernel's own curves rather than from the mesh, so a rim is a
/// circle sampled to tolerance instead of whatever the triangulation left
/// along it.
///
/// Not called yet: the solid tessellator keeps its own feature-edge pass,
/// which also carries isolines. Here because it is the kernel's answer to the
/// same question, and the two should converge on it.
#[allow(dead_code)]
pub fn edge_polylines(document: &SatDocument, sag: f64) -> Vec<Vec<[f64; 3]>> {
let (bodies, _) = lift(document);
let sag = if sag > 0.0 { sag } else { SAG };
bodies
.iter()
.flat_map(|body| brep::edge_polylines(body, sag))
.collect()
}

View file

@ -1,679 +0,0 @@
// ACIS B-rep → truck B-rep → mesh.
//
// Rebuilds every ACIS face as a truck topological `Face` and lets truck's
// meshing kernel triangulate it, routing loaded 3DSOLID / BODY / REGION /
// SURFACE geometry through the same NURBS/B-rep kernel the Model tab builds on
// instead of the bespoke per-surface sampler in `solid3d_tess`.
//
// plane-surface → planar face from the sampled boundary loop
// cone-surface → partial revolutions via rsweep; closed revolutions use
// the parametric sampler
// sphere-surface → bespoke sampler, clipped to the boundary window (truck
// builds a full ball and cannot trim it to a partial face)
// torus-surface → bespoke sampler, clipped to the revolution arc between
// the tube's end caps (truck builds a full closed ring)
// spline-surface → truck BSplineSurface, grid-sampled (see spline_tess)
//
// Each face is meshed independently and its triangles are oriented outward
// using an analytic per-surface normal — truck's own face orientation is not
// consistent across independently built faces, so normals/winding are derived
// from geometry instead. Face coverage is recorded on `MeshLodSet`; partial
// shells remain displayable but cannot masquerade as complete solid topology.
use truck_meshalgo::tessellation::{MeshableShape, MeshedShape};
use truck_modeling::{builder, Face, InnerSpace, Point3, Rad, Shell, Vector3, Wire};
use truck_polymesh::PolygonMesh;
use acadrust::entities::acis::types::Sense;
use acadrust::entities::acis::{
SatConeSurface, SatDocument, SatFace, SatPlaneSurface, SatSphereSurface, SatTorusSurface,
};
use crate::scene::convert::solid3d_tess::{
body_transform, collect_face_loops, cone_axis_span, cone_radius, finalize_mesh,
tess_cone_face, tess_plane_face, tess_sphere_face, tess_torus_face, LodConfig,
BOUNDARY_CHORD_FRAC, TRUCK_CHORD_FRAC,
};
use crate::scene::model::mesh_model::MeshLodSet;
use rustc_hash::FxHashMap;
/// Slightly over 2π so revolution builders close the loop.
const FULL: f64 = std::f64::consts::TAU + 0.2;
/// Triangle mesh chord tolerance (world units) for planar faces, where the
/// surface itself adds no curvature.
const MESH_TOL: f64 = 0.01;
/// Analytic outward-normal rule for a face, used to orient triangles and
/// supply smooth per-vertex normals (truck's face orientation is unreliable
/// for independently built faces).
enum Outward {
/// Constant normal (planar face).
Const([f64; 3]),
/// Cone/cylinder: radial away from the axis, tilted by the half-angle.
Cone {
center: [f64; 3],
axis: [f64; 3],
sin: f64,
cos: f64,
},
}
impl Outward {
fn at(&self, p: [f64; 3]) -> [f64; 3] {
match self {
Outward::Const(n) => *n,
Outward::Cone {
center,
axis,
sin,
cos,
} => {
let d = vsub(p, *center);
let h = vdot(d, *axis);
let radial = vnorm(vsub(d, vscale(*axis, h)));
vnorm(vsub(vscale(radial, *cos), vscale(*axis, *sin)))
}
}
}
}
/// Tessellate an ACIS SAT document by rebuilding it as truck faces.
///
/// Returns `None` when no face could be rebuilt (caller should fall back to
/// the bespoke sampler).
pub fn tessellate_sat_truck(
sat: &SatDocument,
name: String,
color: [f32; 4],
_facet_res: f64,
) -> Option<MeshLodSet> {
// Vertices accumulate in f64 world/local space; `finalize_mesh` splits them
// into the double-single (high, low) pair once, so a solid at UTM scale
// keeps full precision instead of quantizing to the f32 grid.
let mut verts: Vec<[f64; 3]> = Vec::new();
let mut normals: Vec<[f32; 3]> = Vec::new();
let mut indices: Vec<u32> = Vec::new();
let face_materials: FxHashMap<i32, acadrust::Handle> = sat
.records
.iter()
.filter(|record| record.entity_type == "material-adesk-attrib")
.filter_map(|record| {
let owner = record.token_pointer(2)?.0;
let handle = record.token(3)?.as_integer()?;
(owner >= 0 && handle > 0)
.then(|| (owner, acadrust::Handle::new(handle as u64)))
})
.collect();
let face_colors: FxHashMap<i32, [f32; 4]> = sat
.records
.iter()
.filter(|record| record.entity_type == "color-adesk-attrib")
.filter_map(|record| {
let owner = record.token_pointer(2)?.0;
let value = record.token(3)?.as_integer()?;
let face_color = if (1..=255).contains(&value) {
acadrust::Color::from_index(value as i16)
} else if value > 257 {
acadrust::Color::from_true_color_value(value as i32)
} else {
return None;
};
Some((
owner,
{
let mut rgba =
crate::scene::convert::tess_util::aci_to_rgba(&face_color);
rgba[3] = color[3];
rgba
},
))
})
.collect();
let mut triangle_material_handles: Vec<Option<acadrust::Handle>> = Vec::new();
let mut triangle_colors: Vec<Option<[f32; 4]>> = Vec::new();
let face_record_indices: Vec<i32> = sat
.records
.iter()
.filter(|record| record.is_a("face"))
.map(|record| record.index)
.collect();
let mut complete = true;
for (face, face_record_index) in sat.faces().into_iter().zip(face_record_indices) {
let Some(surf_rec) = sat.resolve(face.surface()) else {
complete = false;
continue;
};
let before = indices.len();
let mut appended = false;
if let Some((faces, outward, tol)) = build_face_group(sat, &face, surf_rec) {
if !faces.is_empty() {
let shell: Shell = faces.into();
let poly = shell.triangulation(tol).to_polygon();
if !poly.tri_faces().is_empty() && cone_sweep_covered(sat, &face, surf_rec, &poly)
{
append_group(&mut verts, &mut normals, &mut indices, &poly, &outward);
appended = true;
}
}
}
// Truck's rsweep/triangulation degenerates to zero triangles on some
// short-arc cones (a curved wall face whose profile passes through the
// local origin), leaving a see-through gap. Fall back to the bespoke
// parametric sampler for just that face — it writes into the same
// buffers, so the shared finalize below still applies uniformly.
if !appended {
bespoke_face(sat, &face, surf_rec, &mut verts, &mut normals, &mut indices);
appended = indices.len() > before;
}
if !appended {
complete = false;
} else {
let material = face_materials.get(&face_record_index).copied();
triangle_material_handles.extend(
std::iter::repeat(material).take((indices.len() - before) / 3),
);
let face_color = face_colors.get(&face_record_index).copied();
triangle_colors.extend(
std::iter::repeat(face_color).take((indices.len() - before) / 3),
);
}
}
if indices.is_empty() {
return None;
}
let mesh = finalize_mesh(
name,
verts,
normals,
indices,
triangle_material_handles,
triangle_colors,
color,
body_transform(sat),
);
let mut set = MeshLodSet::from_lods(vec![mesh]);
set.complete = complete;
Some(set)
}
/// Fill one face with the bespoke parametric sampler (body-local verts into the
/// shared mesh buffers) when truck produced no triangles for it. Mirrors the
/// surface dispatch of the standalone sampler.
fn bespoke_face(
sat: &SatDocument,
face: &SatFace,
surf_rec: &acadrust::entities::acis::SatRecord,
v: &mut Vec<[f64; 3]>,
n: &mut Vec<[f32; 3]>,
i: &mut Vec<u32>,
) {
match surf_rec.entity_type.as_str() {
"plane-surface" => {
if let Some(p) = SatPlaneSurface::from_record(surf_rec) {
tess_plane_face(sat, face, &p, LodConfig::HIGH.chord_frac, v, n, i);
}
}
"cone-surface" => {
if let Some(c) = SatConeSurface::from_record(surf_rec) {
tess_cone_face(sat, face, &c, LodConfig::HIGH, v, n, i);
}
}
"sphere-surface" => {
if let Some(s) = SatSphereSurface::from_record(surf_rec) {
tess_sphere_face(sat, face, &s, LodConfig::HIGH, v, n, i);
}
}
"torus-surface" => {
if let Some(t) = SatTorusSurface::from_record(surf_rec) {
tess_torus_face(sat, face, &t, LodConfig::HIGH, v, n, i);
}
}
"spline-surface" | "meshsurf-surface" | "bs3-surface" => {
crate::scene::convert::spline_tess::tess_spline_face(
sat,
face,
LodConfig::HIGH,
v,
n,
i,
);
}
_ => {}
}
}
/// Build the truck face(s) + outward rule for one analytic ACIS face.
fn build_face_group(
sat: &SatDocument,
face: &SatFace,
surf_rec: &acadrust::entities::acis::SatRecord,
) -> Option<(Vec<Face>, Outward, f64)> {
// Chord tolerance scaled to a curved surface's radius, so the facet count
// is radius-independent (matching the circle/arc wire tessellation) rather
// than exploding on large radii.
let curve_tol = |radius: f64| (radius.abs() * TRUCK_CHORD_FRAC).max(1e-6);
match surf_rec.entity_type.as_str() {
"plane-surface" => {
let plane = SatPlaneSurface::from_record(surf_rec)?;
let f = plane_face(sat, face)?;
let (nx, ny, nz) = plane.normal();
let n = if matches!(face.sense(), Sense::Reversed) {
[-nx, -ny, -nz]
} else {
[nx, ny, nz]
};
Some((vec![f], Outward::Const(vnorm(n)), MESH_TOL))
}
"cone-surface" => {
let cone = SatConeSurface::from_record(surf_rec)?;
let tol = curve_tol(cone_radius(&cone));
let (faces, out) = cone_faces(sat, face, &cone)?;
Some((faces, out, tol))
}
// Truck builds a sphere as a full revolution and cannot trim it to the
// face's boundary, so a partial sphere renders as a whole ball. Route
// sphere faces to the bespoke sampler instead — it meshes only the
// boundary's parametric window (see `tess_sphere_face`).
"sphere-surface" => None,
// Like a sphere, truck builds a torus as a full revolution and cannot
// trim it to the face's boundary, so an open "C" tube renders as a whole
// closed ring. Route torus faces to the bespoke sampler, which meshes
// only the revolution arc between the tube's end caps (see
// `tess_torus_face` / `torus_phi_range`).
"torus-surface" => None,
_ => None,
}
}
// ── Planar face ────────────────────────────────────────────────────────────
/// Build a planar truck face from a face's sampled boundary loop. Curved
/// boundary edges (circles) are sampled into line segments, which keeps the
/// wire planar so `try_attach_plane` can fit the plane.
fn plane_face(sat: &SatDocument, face: &SatFace) -> Option<Face> {
// A pierced face (e.g. a wall with a window opening) has one outer boundary
// loop plus an inner loop per hole. `try_attach_plane` fits the plane from
// the first wire and cuts the rest as holes, letting truck's mesher trim
// them. ACIS records no outer-vs-hole flag — the kernel classifies loops at
// runtime from geometry — so we derive the outer boundary here. (#123)
let loops = collect_face_loops(sat, face, BOUNDARY_CHORD_FRAC);
if loops.is_empty() {
return None;
}
// The outer boundary is the loop that encloses the rest — i.e. the one with
// the largest area. ACIS does not guarantee it comes first in the face's
// loop list (a pierced wall lists its window holes before the wall edge),
// so pick it by area rather than trusting index 0. (#123)
let outer_idx = (0..loops.len())
.max_by(|&a, &b| {
vlen(loop_normal(&loops[a]))
.partial_cmp(&vlen(loop_normal(&loops[b])))
.unwrap_or(std::cmp::Ordering::Equal)
})
.unwrap();
if loops[outer_idx].len() < 3 {
return None;
}
// `try_attach_plane` fits its plane normal from the first (outer) wire, so
// feeding the outer in its sampled order makes truck read it as CCW. truck
// then cuts an inner wire as a hole only when it winds the *opposite* way,
// so reverse any hole loop whose area normal agrees with the outer's. The
// outer normal has the largest magnitude (largest area), keeping the sign
// test robust against sampling noise on small holes. (#123)
let outer_n = loop_normal(&loops[outer_idx]);
let mut wires: Vec<Wire> = Vec::new();
wires.push(polygon_wire(&loops[outer_idx], false)?);
for (i, lp) in loops.iter().enumerate() {
if i == outer_idx {
continue;
}
let same = vdot(loop_normal(lp), outer_n) > 0.0;
if let Some(w) = polygon_wire(lp, same) {
wires.push(w);
}
}
builder::try_attach_plane(&wires).ok()
}
fn polygon_wire(pts: &[[f64; 3]], reverse: bool) -> Option<Wire> {
if pts.len() < 3 {
return None;
}
let verts: Vec<_> = if reverse {
pts.iter()
.rev()
.map(|p| builder::vertex(Point3::new(p[0], p[1], p[2])))
.collect()
} else {
pts.iter()
.map(|p| builder::vertex(Point3::new(p[0], p[1], p[2])))
.collect()
};
let n = verts.len();
let edges: Vec<_> = (0..n)
.map(|i| builder::line(&verts[i], &verts[(i + 1) % n]))
.collect();
Some(edges.into())
}
/// Newell area-weighted normal of a closed 3-D polygon (orientation only).
fn loop_normal(pts: &[[f64; 3]]) -> [f64; 3] {
let mut n = [0.0f64; 3];
let m = pts.len();
for i in 0..m {
let a = pts[i];
let b = pts[(i + 1) % m];
n[0] += (a[1] - b[1]) * (a[2] + b[2]);
n[1] += (a[2] - b[2]) * (a[0] + b[0]);
n[2] += (a[0] - b[0]) * (a[1] + b[1]);
}
n
}
// ── Cone / cylinder face ─────────────────────────────────────────────────────
/// Build a pierced cone/cylinder lateral surface as revolution faces. A
/// single-loop face uses the parametric sampler, avoiding partial revolution
/// segments that can disappear when the profile crosses a coordinate plane.
fn cone_faces(
sat: &SatDocument,
face: &SatFace,
cone: &SatConeSurface,
) -> Option<(Vec<Face>, Outward)> {
let (cx, cy, cz) = cone.center();
let (ax, ay, az) = cone.axis();
let (ux, uy, uz) = cone.major_axis();
let radius = cone_radius(cone);
let sin = cone.sin_half_angle();
let cos = cone.cos_half_angle();
let axis = norm(Vector3::new(ax, ay, az));
let udir = norm(Vector3::new(ux, uy, uz));
// v = axis × u completes the right-handed frame; angles increase from u
// toward v (CCW about the axis), matching `builder::rsweep`.
let vdir = Vector3::new(
axis.y * udir.z - axis.z * udir.y,
axis.z * udir.x - axis.x * udir.z,
axis.x * udir.y - axis.y * udir.x,
);
let center = Point3::new(cx, cy, cz);
let (hmin, hmax) =
cone_axis_span(sat, face, cone, [axis.x, axis.y, axis.z], [cx, cy, cz])?;
let r_at = |h: f64| {
if cos.abs() > 1e-9 {
radius + h * sin / cos
} else {
radius
}
};
let loops = collect_face_loops(sat, face, BOUNDARY_CHORD_FRAC);
if loops.len() == 1 {
return None;
}
let arcs: Vec<(f64, f64)> = loops
.iter()
.map(|lp| cone_boundary_arc(lp, [cx, cy, cz], axis, udir, vdir))
.collect();
let outer_idx = arcs
.iter()
.enumerate()
.max_by(|(_, a), (_, b)| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal))
.map(|(i, _)| i)?;
let (theta0, sweep) = arcs[outer_idx];
let closed = sweep >= std::f64::consts::TAU;
let hole_indices: Vec<usize> = arcs
.iter()
.enumerate()
.filter_map(|(i, arc)| (i != outer_idx && arc.1 < std::f64::consts::TAU).then_some(i))
.collect();
if closed && hole_indices.is_empty() {
return None;
}
// Radial direction at the arc's start angle (u rotated by theta0 about axis).
let rad0 = udir * theta0.cos() + vdir * theta0.sin();
let p0 = center + rad0 * r_at(hmin) + axis * hmin;
let p1 = center + rad0 * r_at(hmax) + axis * hmax;
let profile = builder::line(&builder::vertex(p0), &builder::vertex(p1));
let shell: Shell = builder::rsweep(&profile, center, axis, Rad(sweep));
let mut faces: Vec<Face> = shell.face_iter().cloned().collect();
if closed {
let lateral = faces.first_mut()?;
for index in hole_indices {
let wire = polygon_wire(&loops[index], false)?;
lateral.try_add_boundary(wire).ok()?;
}
}
let out = Outward::Cone {
center: [cx, cy, cz],
axis: [axis.x, axis.y, axis.z],
sin,
cos,
};
Some((faces, out))
}
/// Whether a triangulated cone face actually spans the revolution its boundary
/// claims.
///
/// `rsweep` returns a full turn as several segment faces (120° apiece), and a
/// hole only ever lands on one of them: a window wider than a segment, or a
/// segment truck degenerates on, silently costs that whole third of the tube.
/// The face still reports triangles, so nothing downstream notices — the wall is
/// simply missing there and the solid reads as open. Measuring the meshed
/// coverage against the boundary catches every such loss, whatever caused it,
/// and hands the face to the parametric sampler instead.
///
/// Non-cone faces and genuinely partial cones pass straight through.
fn cone_sweep_covered(
sat: &SatDocument,
face: &SatFace,
surf_rec: &acadrust::entities::acis::SatRecord,
poly: &PolygonMesh,
) -> bool {
use std::f64::consts::TAU;
/// A meshed full revolution may skip at most this much before we call it
/// torn — comfortably wider than one chord step, far under a 120° segment.
const MAX_GAP: f64 = std::f64::consts::FRAC_PI_4;
let Some(cone) = SatConeSurface::from_record(surf_rec) else {
return true;
};
let (cx, cy, cz) = cone.center();
let (ax, ay, az) = cone.axis();
let (ux, uy, uz) = cone.major_axis();
let axis = norm(Vector3::new(ax, ay, az));
let udir = norm(Vector3::new(ux, uy, uz));
let vdir = Vector3::new(
axis.y * udir.z - axis.z * udir.y,
axis.z * udir.x - axis.x * udir.z,
axis.x * udir.y - axis.y * udir.x,
);
// Only a face whose boundary closes the revolution owes us a full sweep.
let loops = collect_face_loops(sat, face, BOUNDARY_CHORD_FRAC);
let closed = loops
.iter()
.map(|lp| cone_boundary_arc(lp, [cx, cy, cz], axis, udir, vdir).1)
.fold(0.0f64, f64::max)
>= TAU;
if !closed {
return true;
}
let mut angles: Vec<f64> = Vec::new();
for p in poly.positions() {
let d = vsub([p.x, p.y, p.z], [cx, cy, cz]);
let ru = vdot(d, [udir.x, udir.y, udir.z]);
let rv = vdot(d, [vdir.x, vdir.y, vdir.z]);
if ru.hypot(rv) > 1e-9 {
angles.push(rv.atan2(ru).rem_euclid(TAU));
}
}
if angles.len() < 3 {
return false;
}
angles.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
let mut widest = 0.0f64;
for i in 0..angles.len() {
let next = if i + 1 < angles.len() {
angles[i + 1]
} else {
angles[0] + TAU
};
widest = widest.max(next - angles[i]);
}
widest <= MAX_GAP
}
/// Smallest angular arc `(theta_start, sweep)` about the cone axis enclosing the
/// face's boundary points, robust to the ±π seam. Returns `(0.0, FULL)` when the
/// points wrap the whole revolution (a closed rim) or the boundary is unusable,
/// so a real cylinder/cone still sweeps a complete (overlapped) circle.
fn cone_boundary_arc(
poly: &[[f64; 3]],
center: [f64; 3],
_axis: Vector3,
udir: Vector3,
vdir: Vector3,
) -> (f64, f64) {
use std::f64::consts::TAU;
let mut angles: Vec<f64> = Vec::new();
for p in poly {
let d = [p[0] - center[0], p[1] - center[1], p[2] - center[2]];
let ru = d[0] * udir.x + d[1] * udir.y + d[2] * udir.z;
let rv = d[0] * vdir.x + d[1] * vdir.y + d[2] * vdir.z;
if ru.hypot(rv) > 1e-9 {
angles.push(rv.atan2(ru));
}
}
if angles.len() < 2 {
return (0.0, FULL);
}
angles.sort_by(|a, b| a.partial_cmp(b).unwrap());
// Largest angular gap between consecutive samples (wrapping past the seam);
// the enclosing arc is its complement.
let mut max_gap = 0.0;
let mut gap_at = angles.len() - 1;
for i in 0..angles.len() {
let a = angles[i];
let b = if i + 1 < angles.len() {
angles[i + 1]
} else {
angles[0] + TAU
};
let g = b - a;
if g > max_gap {
max_gap = g;
gap_at = i;
}
}
let span = TAU - max_gap;
// Nearly a full revolution → treat as a closed rim.
if span >= TAU * 0.98 - 1e-9 || span < 1e-6 {
return (0.0, FULL);
}
// Start at the sample just after the largest gap, sweep CCW by `span`.
let start = angles[(gap_at + 1) % angles.len()];
(start, span)
}
// ── Mesh append with analytic outward normals ────────────────────────────────
/// Append one face's triangulation to `mesh`, computing smooth per-vertex
/// normals from the outward rule and flipping any triangle whose winding
/// disagrees with that outward direction.
fn append_group(
verts: &mut Vec<[f64; 3]>,
normals: &mut Vec<[f32; 3]>,
out_indices: &mut Vec<u32>,
poly: &PolygonMesh,
outward: &Outward,
) {
let positions = poly.positions();
let base = verts.len() as u32;
for p in positions {
let pos = [p.x, p.y, p.z];
verts.push(pos);
let n = outward.at(pos);
normals.push([n[0] as f32, n[1] as f32, n[2] as f32]);
}
for tri in poly.tri_faces() {
let (i0, i1, i2) = (tri[0].pos, tri[1].pos, tri[2].pos);
let a = pt(positions[i0]);
let b = pt(positions[i1]);
let c = pt(positions[i2]);
let gn = vcross(vsub(b, a), vsub(c, a));
let cen = [
(a[0] + b[0] + c[0]) / 3.0,
(a[1] + b[1] + c[1]) / 3.0,
(a[2] + b[2] + c[2]) / 3.0,
];
let out = outward.at(cen);
let (j0, j1, j2) = (base + i0 as u32, base + i1 as u32, base + i2 as u32);
if vdot(gn, out) < 0.0 {
out_indices.extend_from_slice(&[j0, j2, j1]);
} else {
out_indices.extend_from_slice(&[j0, j1, j2]);
}
}
}
#[inline]
fn pt(p: Point3) -> [f64; 3] {
[p.x, p.y, p.z]
}
// ── Vector helpers (cgmath Vector3) ──────────────────────────────────────────
#[inline]
fn norm(v: Vector3) -> Vector3 {
let len = v.magnitude();
if len < 1e-12 {
Vector3::unit_z()
} else {
v / len
}
}
// ── Vector helpers ([f64; 3]) ────────────────────────────────────────────────
#[inline]
fn vsub(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
[a[0] - b[0], a[1] - b[1], a[2] - b[2]]
}
#[inline]
fn vscale(a: [f64; 3], s: f64) -> [f64; 3] {
[a[0] * s, a[1] * s, a[2] * s]
}
#[inline]
fn vdot(a: [f64; 3], b: [f64; 3]) -> f64 {
a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}
#[inline]
fn vcross(a: [f64; 3], b: [f64; 3]) -> [f64; 3] {
[
a[1] * b[2] - a[2] * b[1],
a[2] * b[0] - a[0] * b[2],
a[0] * b[1] - a[1] * b[0],
]
}
#[inline]
fn vlen(a: [f64; 3]) -> f64 {
vdot(a, a).sqrt()
}
#[inline]
fn vnorm(a: [f64; 3]) -> [f64; 3] {
let l = vlen(a);
if l < 1e-12 {
[0.0, 0.0, 1.0]
} else {
[a[0] / l, a[1] / l, a[2] / l]
}
}

View file

@ -1,23 +1,7 @@
pub mod acad_to_truck;
#[cfg(feature = "solid3d")]
pub mod acis_export;
#[cfg(feature = "solid3d")]
pub mod acis_to_truck;
/// Without `solid3d` (e.g. wasm) ACIS/SAT meshing is unavailable; the entry
/// point stays so callers compile, returning no mesh.
#[cfg(not(feature = "solid3d"))]
pub mod acis_to_truck {
use crate::scene::model::mesh_model::MeshLodSet;
use acadrust::entities::acis::SatDocument;
pub fn tessellate_sat_truck(
_sat: &SatDocument,
_name: String,
_color: [f32; 4],
_facet_res: f64,
) -> Option<MeshLodSet> {
None
}
}
pub mod acis_kernel;
pub mod dgn_linestyle;
pub mod truck_tess;
pub mod tessellate;

View file

@ -46,7 +46,6 @@ pub(crate) const EDGE_CHORD_FRAC: f64 = 0.002;
/// a hexagonal prism whose flats sink far inside the true radius, tearing the
/// wall away from the planar faces and caps that meet it.
#[cfg(feature = "solid3d")]
pub(crate) const TRUCK_CHORD_FRAC: f64 = 0.005;
/// Boundary-loop sampling for parameter-range classification (which arc of a
/// sphere/torus a face covers): a fine fraction so the classification is
/// accurate; the points are not rendered.
@ -276,9 +275,10 @@ fn tessellate_sat(
))
}
/// Tessellate an ACIS document, preferring the truck B-rep kernel and falling
/// back to the bespoke per-surface sampler when truck can't rebuild the shell
/// (e.g. an unhandled surface type).
/// Tessellate an ACIS document, preferring the geometry kernel and falling
/// back to the bespoke per-surface sampler when the kernel cannot rebuild the
/// whole shell (a surface kind it does not model, a face whose boundary it
/// cannot express in that surface's own parameters).
fn tessellate_acis(
sat: &SatDocument,
name: String,
@ -286,7 +286,7 @@ fn tessellate_acis(
facet_res: f64,
isolines: usize,
) -> Option<MeshLodSet> {
let truck = crate::scene::convert::acis_to_truck::tessellate_sat_truck(
let truck = crate::scene::convert::acis_kernel::tessellate_sat(
sat,
name.clone(),
color,
@ -2049,7 +2049,7 @@ pub(crate) fn tess_plane_face(
}
// Fan triangulation from vertex 0 (outer loop only; holes are handled by
// the truck B-rep path in `acis_to_truck`).
// the kernel B-rep path in `acis_kernel`).
let n = poly.len() as u32;
for i in 1..(n - 1) {
indices.extend_from_slice(&[base, base + i, base + i + 1]);

View file

@ -1779,11 +1779,12 @@ pub struct Scene {
/// owning block emits a transformed instance so a block placed at an
/// INSERT scale renders at the right size. (#123)
pub block_meshes: HashMap<Handle, MeshLodSet>,
/// Live truck B-reps for solids created this session by the Model tab,
/// keyed by entity handle. Backs the Design-group boolean tools (a solid
/// must be here to be combined). Not persisted — rebuilt only by creating
/// or combining primitives in-session.
pub solid_models: HashMap<Handle, truck_modeling::Solid>,
/// Live B-reps for solids created this session by the Model tab, keyed by
/// entity handle. Backs the Design-group boolean tools (a solid must be
/// here to be combined) and the exact-geometry save path, which writes
/// each one back out as ACIS rather than as facets. Not persisted —
/// rebuilt only by creating or combining primitives in-session.
pub solid_models: HashMap<Handle, acadrust::kernel::brep::Body>,
/// GPU render data for raster images (RasterImage entities), keyed by handle.
pub images: HashMap<Handle, ImageModel>,
/// The viewport that is currently "entered" (MSPACE mode).
@ -2719,11 +2720,12 @@ impl Scene {
}
/// Re-evaluate every cached mesh's color through `render_style` so a
/// Register a Model-tab solid: cache its truck B-rep (for boolean ops) and
/// tessellate it into the shaded mesh pipeline under `handle`. The solid is
/// in the same offset-relative frame the mesh pipeline uses, so the mesh is
/// stored as-is (Model-tab geometry is authored at world_offset 0).
pub fn register_solid_model(&mut self, handle: Handle, solid: truck_modeling::Solid) {
/// Register a Model-tab solid: cache its B-rep (for boolean ops and the
/// exact-geometry save path) and tessellate it into the shaded mesh
/// pipeline under `handle`. The body is in the same offset-relative frame
/// the mesh pipeline uses, so the mesh is stored as-is (Model-tab geometry
/// is authored at world_offset 0).
pub fn register_solid_model(&mut self, handle: Handle, solid: acadrust::kernel::brep::Body) {
let entity = self.document.get_entity(handle);
let color = entity
.map(|e| self.render_style(e).0)

View file

@ -8,5 +8,6 @@ pub mod ole_pres;
pub mod pdf_raster;
pub mod mesh_model;
pub mod solid_model;
pub mod sweep_model;
pub mod visual_style_model;
pub mod object;

View file

@ -1,95 +1,201 @@
// truck B-rep construction for the Model tab's primitives, plus tessellation
// into the renderer's `MeshLodSet`. Each builder follows truck's own example
// recipes (tsweep / rsweep / cone / try_attach_plane), oriented Z-up with the
// footprint on the z = `base_z` plane to match acadrust's `acis::primitives`.
// B-rep construction for the Model tab's primitives, plus tessellation into
// the renderer's `MeshLodSet`.
//
// The resulting truck `Solid` is cached per entity handle on the Scene so the
// Design-group boolean tools can run truck-shapeops on it.
// The bodies come from the geometry kernel, which builds each primitive the
// way ACIS records it: an analytic surface with singular vertices where the
// surface has them, rather than a mesh or a spline that happens to look like
// one. That is what lets a solid built here be written back out as exact
// geometry instead of a facetted approximation — see `acis_bridge`.
//
// Everything is oriented Z-up with the footprint on the z = base plane, to
// match acadrust's `acis::primitives`.
//
// The resulting `Body` is cached per entity handle on the Scene so the
// Design-group boolean tools can run on it.
use truck_modeling::{builder, Point3, Rad, Solid, Vector3, Wire};
use acadrust::kernel::brep::{self, Body};
use crate::scene::model::mesh_model::{MeshLodSet, MeshModel};
const FULL: f64 = 7.0; // > 2π → builder closes the revolution
/// How far a triangle may sit from the surface it lies on. A drawing is
/// measured in millimetres, and a twentieth of one is past what a screen
/// resolves at any sane zoom.
const SAG: f64 = 0.05;
/// What counts as the same point when the kernel checks a body over.
const TOL: f64 = 1e-9;
/// Axis-aligned box from its center and full extents.
pub fn box_solid(center: [f64; 3], length: f64, width: f64, height: f64) -> Solid {
let min = Point3::new(
center[0] - length / 2.0,
center[1] - width / 2.0,
center[2] - height / 2.0,
);
let v = builder::vertex(min);
let e = builder::tsweep(&v, Vector3::new(length, 0.0, 0.0));
let f = builder::tsweep(&e, Vector3::new(0.0, width, 0.0));
builder::tsweep(&f, Vector3::new(0.0, 0.0, height))
pub fn box_solid(center: [f64; 3], length: f64, width: f64, height: f64) -> Option<Body> {
brep::make::cuboid(
[
center[0] - length / 2.0,
center[1] - width / 2.0,
center[2] - height / 2.0,
],
[length, width, height],
)
}
/// Right triangular prism (wedge): right-triangle cross-section in XZ,
/// extruded along Y. `origin` is the min corner, ramp rising in +X/+Z.
pub fn wedge_solid(origin: [f64; 3], length: f64, width: f64, height: f64) -> Solid {
let o = Point3::new(origin[0], origin[1], origin[2]);
let a = builder::vertex(o);
let b = builder::vertex(Point3::new(o.x + length, o.y, o.z));
let c = builder::vertex(Point3::new(o.x, o.y, o.z + height));
let wire: Wire = vec![
builder::line(&a, &b),
builder::line(&b, &c),
builder::line(&c, &a),
]
.into();
let face = builder::try_attach_plane(&[wire]).expect("wedge profile");
builder::tsweep(&face, Vector3::new(0.0, width, 0.0))
pub fn wedge_solid(origin: [f64; 3], length: f64, width: f64, height: f64) -> Option<Body> {
brep::make::wedge(origin, length, width, height)
}
/// Solid cylinder: disk on the z = base plane, extruded up by `height`.
pub fn cylinder_solid(center: [f64; 3], radius: f64, height: f64) -> Solid {
let v = builder::vertex(Point3::new(center[0] + radius, center[1], center[2]));
let circle = builder::rsweep(
&v,
Point3::new(center[0], center[1], center[2]),
Vector3::unit_z(),
Rad(FULL),
);
let disk = builder::try_attach_plane(&[circle]).expect("cylinder cap");
builder::tsweep(&disk, Vector3::new(0.0, 0.0, height))
/// Solid cylinder standing on the z = base plane.
pub fn cylinder_solid(center: [f64; 3], radius: f64, height: f64) -> Option<Body> {
brep::make::cylinder(center, radius, height)
}
/// Solid cone: profile (apex → rim → base-center) revolved about the axis.
pub fn cone_solid(center: [f64; 3], radius: f64, height: f64) -> Solid {
let cx = center[0];
let cy = center[1];
let cz = center[2];
let apex = builder::vertex(Point3::new(cx, cy, cz + height));
let rim = builder::vertex(Point3::new(cx + radius, cy, cz));
let base = builder::vertex(Point3::new(cx, cy, cz));
let wire: Wire = vec![builder::line(&apex, &rim), builder::line(&rim, &base)].into();
let shell = builder::cone(&wire, Vector3::unit_z(), Rad(FULL));
Solid::new(vec![shell])
/// Solid cone standing on the z = base plane, apex `height` above it.
pub fn cone_solid(center: [f64; 3], radius: f64, height: f64) -> Option<Body> {
brep::make::cone(center, radius, height)
}
/// Solid sphere: meridian semicircle revolved about the polar (Z) axis.
pub fn sphere_solid(center: [f64; 3], radius: f64) -> Solid {
let c = Point3::new(center[0], center[1], center[2]);
let top = builder::vertex(Point3::new(c.x, c.y, c.z + radius));
// Rotate the top point about the X axis by π → meridian from +Z to −Z.
let meridian: Wire = builder::rsweep(&top, c, Vector3::unit_x(), Rad(std::f64::consts::PI));
let shell = builder::cone(&meridian, Vector3::unit_z(), Rad(FULL));
Solid::new(vec![shell])
/// Solid sphere about `center`.
pub fn sphere_solid(center: [f64; 3], radius: f64) -> Option<Body> {
brep::make::sphere(center, radius)
}
/// Solid torus in the z = base plane (tube revolved about the Z axis).
pub fn torus_solid(center: [f64; 3], major: f64, minor: f64) -> Solid {
let c = Point3::new(center[0], center[1], center[2]);
let v = builder::vertex(Point3::new(c.x + major, c.y, c.z + minor));
let tube = builder::rsweep(
&v,
Point3::new(c.x + major, c.y, c.z),
Vector3::unit_y(),
Rad(FULL),
);
let shell = builder::rsweep(&tube, c, Vector3::unit_z(), Rad(FULL));
Solid::new(vec![shell])
pub fn torus_solid(center: [f64; 3], major: f64, minor: f64) -> Option<Body> {
brep::make::torus(center, major, minor)
}
/// Solid pyramid on a regular polygon of `sides` corners.
pub fn pyramid_solid(center: [f64; 3], radius: f64, height: f64, sides: usize) -> Option<Body> {
brep::make::pyramid(center, radius, height, sides)
}
// ── Placement ───────────────────────────────────────────────────────────────
/// Moves a body by a rigid transform, given as three axes and an origin.
///
/// The Model tab builds every primitive in its own upright frame and then
/// puts it on the working plane, which is the only reason this exists. A
/// body carries analytic surfaces, so moving it moves their frames rather
/// than any points.
pub fn placed(
body: &Body,
x: [f64; 3],
y: [f64; 3],
z: [f64; 3],
origin: [f64; 3],
) -> Option<Body> {
brep::transform(
body,
&brep::Placement {
x_axis: x,
y_axis: y,
z_axis: z,
origin,
},
)
}
/// Turns a body about one of the world axes, through the point `about`.
pub fn turned(body: &Body, axis: usize, angle: f64, about: [f64; 3]) -> Option<Body> {
let (sin, cos) = angle.sin_cos();
// The rotation's columns, written out per axis rather than assembled from
// a general formula: three cases are shorter than the axis-angle one and
// there is nothing to get subtly wrong in them.
let (x, y, z) = match axis {
0 => ([1.0, 0.0, 0.0], [0.0, cos, sin], [0.0, -sin, cos]),
1 => ([cos, 0.0, -sin], [0.0, 1.0, 0.0], [sin, 0.0, cos]),
_ => ([cos, sin, 0.0], [-sin, cos, 0.0], [0.0, 0.0, 1.0]),
};
placed(body, x, y, z, about_origin(x, y, z, about))
}
/// Reflects a body in the plane across one of the world axes, through `about`.
///
/// The kernel puts the mirrored solid back the right way out; a reflection
/// left alone lights black.
pub fn mirrored(body: &Body, axis: usize, about: [f64; 3]) -> Option<Body> {
let mut columns = [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]];
columns[axis][axis] = -1.0;
let [x, y, z] = columns;
placed(body, x, y, z, about_origin(x, y, z, about))
}
/// Moves a body by a transform given as a column-major 4×4, which is what a
/// frame-to-frame solve produces.
pub fn by_matrix(body: &Body, matrix: [f64; 16]) -> Option<Body> {
placed(
body,
[matrix[0], matrix[1], matrix[2]],
[matrix[4], matrix[5], matrix[6]],
[matrix[8], matrix[9], matrix[10]],
[matrix[12], matrix[13], matrix[14]],
)
}
/// Where a transform's origin has to sit for it to act about `about` rather
/// than about the world origin: `about − M·about`.
fn about_origin(x: [f64; 3], y: [f64; 3], z: [f64; 3], about: [f64; 3]) -> [f64; 3] {
let mut origin = about;
for axis in 0..3 {
origin[axis] -= x[axis] * about[0] + y[axis] * about[1] + z[axis] * about[2];
}
origin
}
/// The box a body occupies, from its mesh.
pub fn extent(body: &Body) -> Option<([f64; 3], [f64; 3])> {
let mesh = brep::mesh::body(body, SAG, TOL);
if mesh.positions.is_empty() {
return None;
}
let mut low = [f64::INFINITY; 3];
let mut high = [f64::NEG_INFINITY; 3];
for point in &mesh.positions {
for axis in 0..3 {
low[axis] = low[axis].min(point[axis]);
high[axis] = high[axis].max(point[axis]);
}
}
Some((low, high))
}
/// Where an axis-aligned plane cuts a body, as line segments.
///
/// Taken off the mesh rather than the surfaces: a section of a cone by a
/// slanted plane is a conic, of a torus a quartic, and the answer wanted here
/// is a set of Line entities either way. Each triangle the plane crosses
/// contributes the one segment where it does.
pub fn section(body: &Body, axis: usize, value: f64) -> Vec<([f64; 3], [f64; 3])> {
let mesh = brep::mesh::body(body, SAG, TOL);
let mut out = Vec::new();
for triangle in &mesh.triangles {
let corners: Vec<[f64; 3]> = triangle.iter().map(|i| mesh.positions[*i]).collect();
// Where each edge of the triangle meets the plane. A triangle with a
// corner exactly on it contributes that corner twice, which collapses
// to nothing and is dropped below.
let mut hits: Vec<[f64; 3]> = Vec::new();
for step in 0..3 {
let (from, to) = (corners[step], corners[(step + 1) % 3]);
let (a, b) = (from[axis] - value, to[axis] - value);
if (a > 0.0) == (b > 0.0) || a == b {
continue;
}
let along = a / (a - b);
hits.push([
from[0] + (to[0] - from[0]) * along,
from[1] + (to[1] - from[1]) * along,
from[2] + (to[2] - from[2]) * along,
]);
}
if hits.len() == 2 {
let span = (hits[0][0] - hits[1][0]).abs()
+ (hits[0][1] - hits[1][1]).abs()
+ (hits[0][2] - hits[1][2]).abs();
if span > 1e-9 {
out.push((hits[0], hits[1]));
}
}
}
out
}
// ── Edge extraction (pick geometry + wireframe overlay) ─────────────────────
@ -98,35 +204,22 @@ pub fn torus_solid(center: [f64; 3], major: f64, minor: f64) -> Solid {
/// `Solid3D`/result entity so it is click-pickable (the renderer's wire
/// fallback draws these as a wireframe over the shaded mesh, and hit-testing
/// uses their points).
pub fn edge_wires(solid: &Solid) -> Vec<acadrust::entities::Wire> {
use crate::scene::convert::truck_tess::{tessellate_edge, TruckTessResult};
pub fn edge_wires(body: &Body) -> Vec<acadrust::entities::Wire> {
use acadrust::types::Vector3;
let mut wires = Vec::new();
for shell in solid.boundaries() {
for edge in shell.edge_iter() {
if let TruckTessResult::Lines(pts, pts_low) = tessellate_edge(&edge) {
if pts.len() < 2 {
continue;
}
let pts3: Vec<Vector3> = pts
.iter()
.zip(pts_low.iter())
.map(|(p, l)| {
Vector3::new(
p[0] as f64 + l[0] as f64,
p[1] as f64 + l[1] as f64,
p[2] as f64 + l[2] as f64,
)
})
.collect();
wires.push(acadrust::entities::Wire::from_points(pts3));
}
}
}
wires
brep::edge_polylines(body, SAG)
.into_iter()
.map(|points| {
acadrust::entities::Wire::from_points(
points
.into_iter()
.map(|p| Vector3::new(p[0], p[1], p[2]))
.collect(),
)
})
.collect()
}
// ── Boolean operations (truck-shapeops) ─────────────────────────────────────
// ── Boolean operations ──────────────────────────────────────────────────────
/// Which CSG to apply. Mirrors `model::boolean_cmd::BoolOp` but kept local so
/// this scene module has no dependency on the UI module.
@ -137,66 +230,124 @@ pub enum Bool {
Intersect,
}
#[cfg(feature = "solid3d")]
const BOOL_TOL: f64 = 0.05;
/// Combine two solids. `Subtract` removes `b` from `a`. Returns `None` when the
/// operation fails (e.g. the solids don't actually overlap).
#[cfg(feature = "solid3d")]
pub fn boolean(op: Bool, a: &Solid, b: &Solid) -> Option<Solid> {
match op {
Bool::Union => truck_shapeops::or(a, b, BOOL_TOL),
Bool::Intersect => truck_shapeops::and(a, b, BOOL_TOL),
Bool::Subtract => {
let mut bn = b.clone();
bn.not();
truck_shapeops::and(a, &bn, BOOL_TOL)
}
}
}
/// Without `solid3d` (e.g. wasm) there is no boolean kernel.
#[cfg(not(feature = "solid3d"))]
pub fn boolean(_op: Bool, _a: &Solid, _b: &Solid) -> Option<Solid> {
None
/// Combine two solids. `Subtract` removes `b` from `a`.
///
/// `None` when the kernel refuses — a face pair it has no closed form for, a
/// cut it cannot make. It refuses rather than returning a solid with a wall
/// missing, and passing that on unchanged is the point: a half-done boolean
/// looks finished.
pub fn boolean(op: Bool, a: &Body, b: &Body) -> Option<Body> {
let how = match op {
Bool::Union => brep::Operation::Union,
Bool::Subtract => brep::Operation::Difference,
Bool::Intersect => brep::Operation::Intersection,
};
brep::combine(a.clone(), b.clone(), how, TOL).ok()
}
// ── Tessellation ────────────────────────────────────────────────────────────
/// Tessellate a truck `Solid` into a single-LOD `MeshLodSet` (world-space,
/// before world_offset is applied by the caller).
pub fn mesh_from_solid(solid: &Solid, color: [f32; 4]) -> Option<MeshLodSet> {
use crate::scene::convert::truck_tess::{tessellate_solid, TruckTessResult};
match tessellate_solid(solid) {
TruckTessResult::Mesh {
verts,
verts_low,
normals,
indices,
} if !indices.is_empty() => {
let mesh = MeshModel {
name: String::new(),
verts,
verts_low,
normals,
indices,
triangle_material_handles: Vec::new(),
triangle_colors: Vec::new(),
color,
selected: false,
};
Some(MeshLodSet::from_single(mesh))
}
_ => None,
/// Tessellate a `Body` into a single-LOD `MeshLodSet` (world-space, before
/// world_offset is applied by the caller).
pub fn mesh_from_solid(body: &Body, color: [f32; 4]) -> Option<MeshLodSet> {
let mesh = brep::mesh::body(body, SAG, TOL);
if mesh.is_empty() {
return None;
}
// The renderer holds each position as a coarse float plus a fine
// correction, so a survey coordinate keeps its last millimetres instead
// of losing them to f32.
let mut verts = Vec::with_capacity(mesh.positions.len());
let mut verts_low = Vec::with_capacity(mesh.positions.len());
for point in &mesh.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,
]);
}
let normals = mesh
.normals
.iter()
.map(|n| [n[0] as f32, n[1] as f32, n[2] as f32])
.collect();
let indices = mesh
.triangles
.iter()
.flat_map(|t| [t[0] as u32, t[1] as u32, t[2] as u32])
.collect();
Some(MeshLodSet::from_single(MeshModel {
name: String::new(),
verts,
verts_low,
normals,
indices,
triangle_material_handles: Vec::new(),
triangle_colors: Vec::new(),
color,
selected: false,
}))
}
/// The middle of a body, for a caller needing a point to turn or scale about.
///
/// Read off the mesh rather than `body_bounds`, which refuses a face that
/// wraps a closed surface — a sphere is one such face and has no box at all.
pub fn centre(body: &Body) -> Option<[f64; 3]> {
let mesh = brep::mesh::body(body, SAG, TOL);
if mesh.positions.is_empty() {
return None;
}
let mut low = [f64::INFINITY; 3];
let mut high = [f64::NEG_INFINITY; 3];
for point in &mesh.positions {
for axis in 0..3 {
low[axis] = low[axis].min(point[axis]);
high[axis] = high[axis].max(point[axis]);
}
}
Some([
(low[0] + high[0]) * 0.5,
(low[1] + high[1]) * 0.5,
(low[2] + high[2]) * 0.5,
])
}
/// How much a body encloses, from its mesh.
///
/// The divergence theorem over triangles wound outwards, which is what makes
/// it a check rather than only a measurement: a solid built inside out
/// reports a negative volume rather than a plausible one, and one missing a
/// face reports far too little. Nothing in the app measures volume yet, so it
/// exists to test with.
#[cfg(test)]
pub fn volume(body: &Body) -> f64 {
use acadrust::kernel::space::Vec3;
let mesh = brep::mesh::body(body, SAG, TOL);
let Some(middle) = centre(body) else {
return 0.0;
};
// About the body's own middle: at survey coordinates the tetrahedra
// reaching back to the origin are enormous and nearly cancel, and a
// cubic millimetre read off a sum of billions is noise.
let middle = Vec3::from(middle);
mesh.triangles
.iter()
.map(|triangle| {
let at = |index: usize| Vec3::from(mesh.positions[triangle[index]]) - middle;
at(0).cross(at(1)).dot(at(2)) / 6.0
})
.sum()
}
#[cfg(test)]
mod tests {
use super::*;
fn tri_count(s: &Solid) -> usize {
mesh_from_solid(s, [0.7, 0.7, 0.7, 1.0])
fn tri_count(body: &Body) -> usize {
mesh_from_solid(body, [0.7, 0.7, 0.7, 1.0])
.map(|m| m.lods[0].indices.len() / 3)
.unwrap_or(0)
}
@ -204,18 +355,44 @@ mod tests {
#[test]
fn all_primitives_triangulate() {
let c = [0.0, 0.0, 0.0];
assert!(tri_count(&box_solid(c, 10.0, 10.0, 10.0)) >= 12, "box");
assert!(tri_count(&wedge_solid(c, 10.0, 10.0, 10.0)) >= 6, "wedge");
assert!(tri_count(&cylinder_solid(c, 5.0, 12.0)) > 20, "cylinder");
assert!(tri_count(&cone_solid(c, 5.0, 12.0)) > 10, "cone");
assert!(tri_count(&sphere_solid(c, 5.0)) > 50, "sphere");
assert!(tri_count(&torus_solid(c, 8.0, 2.0)) > 50, "torus");
assert!(tri_count(&box_solid(c, 10.0, 10.0, 10.0).unwrap()) >= 12, "box");
assert!(tri_count(&wedge_solid(c, 10.0, 10.0, 10.0).unwrap()) >= 6, "wedge");
assert!(tri_count(&cylinder_solid(c, 5.0, 12.0).unwrap()) > 20, "cylinder");
assert!(tri_count(&cone_solid(c, 5.0, 12.0).unwrap()) > 10, "cone");
assert!(tri_count(&sphere_solid(c, 5.0).unwrap()) > 50, "sphere");
assert!(tri_count(&torus_solid(c, 8.0, 2.0).unwrap()) > 50, "torus");
assert!(tri_count(&pyramid_solid(c, 5.0, 9.0, 6).unwrap()) >= 8, "pyramid");
}
#[test]
fn every_primitive_is_the_size_it_was_asked_for() {
// Triangle counts say a mesh exists; the volume says it is the right
// shape and the right way out. A face left out reads far too small
// and one wound inwards reads negative, and neither shows up in a
// count.
use std::f64::consts::PI;
let c = [0.0, 0.0, 0.0];
let cases: [(Body, f64); 5] = [
(box_solid(c, 10.0, 4.0, 6.0).unwrap(), 240.0),
(cylinder_solid(c, 5.0, 12.0).unwrap(), PI * 25.0 * 12.0),
(cone_solid(c, 5.0, 12.0).unwrap(), PI * 25.0 * 12.0 / 3.0),
(sphere_solid(c, 5.0).unwrap(), 4.0 / 3.0 * PI * 125.0),
(torus_solid(c, 8.0, 2.0).unwrap(), 2.0 * PI * PI * 8.0 * 4.0),
];
for (body, expected) in cases {
let got = volume(&body);
assert!(got > 0.0, "wound inwards: {got}");
// Facets are chords and lie inside, so a mesh reads short and
// never over.
assert!(got > 0.94 * expected, "{got} vs {expected}");
assert!(got <= expected * 1.000_001, "{got} vs {expected}");
}
}
#[test]
fn booleans_produce_solids() {
let a = box_solid([0.0, 0.0, 0.0], 10.0, 10.0, 10.0);
let b = box_solid([5.0, 5.0, 5.0], 10.0, 10.0, 10.0);
let a = box_solid([0.0, 0.0, 0.0], 10.0, 10.0, 10.0).unwrap();
let b = box_solid([5.0, 5.0, 5.0], 10.0, 10.0, 10.0).unwrap();
for (op, label) in [
(Bool::Union, "union"),
(Bool::Subtract, "subtract"),
@ -223,13 +400,35 @@ mod tests {
] {
let r = boolean(op, &a, &b);
let n = r.as_ref().map(tri_count).unwrap_or(0);
eprintln!("{label}: tris={n}");
assert!(r.is_some() && n > 0, "{label} produced nothing");
}
}
#[test]
fn box_exposes_edges() {
assert!(edge_wires(&box_solid([0.0, 0.0, 0.0], 10.0, 10.0, 10.0)).len() >= 12);
assert!(edge_wires(&box_solid([0.0, 0.0, 0.0], 10.0, 10.0, 10.0).unwrap()).len() >= 12);
}
#[test]
fn placing_a_body_moves_it_without_changing_its_size() {
let body = box_solid([0.0, 0.0, 0.0], 10.0, 4.0, 6.0).unwrap();
// A quarter turn about Z, then five along X.
let moved = placed(
&body,
[0.0, 1.0, 0.0],
[-1.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
[5.0, 0.0, 0.0],
)
.expect("a turned box");
assert!((volume(&moved) - 240.0).abs() < 1e-6, "{}", volume(&moved));
// Centred on the origin to begin with, so the turn leaves it there
// and the move puts it five along x.
let middle = centre(&moved).unwrap();
assert!((middle[0] - 5.0).abs() < 1e-9, "{middle:?}");
// And ten along x really did become ten along y.
let (low, high) = extent(&moved).unwrap();
assert!((high[1] - low[1] - 10.0).abs() < 1e-9, "{low:?} {high:?}");
assert!((high[0] - low[0] - 4.0).abs() < 1e-9, "{low:?} {high:?}");
}
}

View file

@ -0,0 +1,203 @@
// EXTRUDE / REVOLVE: turning a drawn profile into a solid.
//
// The profile comes from `entities::curve`, which is where every entity's
// geometry is already defined once, so a circle, an arc-bulged polyline and a
// closed spline all arrive as the same thing: a plane plus a chain of curves
// in that plane's coordinates. The kernel sweeps that chain into analytic
// surfaces — a straight run into a plane, an arc into a cylinder, a profile
// turned about an axis into a cone, a sphere or a torus — so the result can
// be written back out as ACIS rather than as facets.
//
// A spline profile has no analytic side wall, so it is refused rather than
// approximated into a surface nobody asked for. That is the kernel's answer
// and this module passes it on.
use acadrust::kernel::brep::{self, Body};
use acadrust::kernel::geom2d::Curve;
use acadrust::kernel::space::{Plane, PlanarCurve};
use acadrust::EntityType;
use crate::entities::curve::entity_curve;
/// A drawn profile, as the kernel wants it: the plane it lies in and the
/// chain of pieces closing a loop in that plane.
pub struct Profile {
pub plane: Plane,
pub pieces: Vec<Curve>,
}
/// The closed profile an entity describes, or `None` when it does not
/// describe one.
///
/// A single closed curve — a circle, an ellipse, a closed polyline — is what
/// a sweep needs. The kernel wants it as a chain of at least three pieces,
/// which a polyline already is and which a circle is not, so a round profile
/// is handed over as the arcs it is made of rather than as one curve with
/// nowhere for the sweep to start.
pub fn profile_of(entity: &EntityType) -> Option<Profile> {
let planar: PlanarCurve = entity_curve(entity)?;
if !planar.curve.is_closed() {
return None;
}
let pieces = match &planar.curve {
// A polyline is already a chain. Its own segments carry the bulges,
// so an arc stays an arc rather than becoming a run of chords.
Curve::Polyline(_) => planar.curve.segments(),
// Everything else closed on itself is cut into quarters, which is
// both the fewest pieces a chain may have and the fewest that leave
// each one unambiguous about which way round it goes.
Curve::Circle(circle) => quarters(circle.centre, circle.radius),
other => split_evenly(other, 4),
};
(pieces.len() >= 3).then_some(Profile {
plane: planar.plane,
pieces,
})
}
/// A circle as four arcs.
fn quarters(centre: [f64; 2], radius: f64) -> Vec<Curve> {
use acadrust::kernel::geom2d::Arc;
use std::f64::consts::FRAC_PI_2;
(0..4)
.map(|quarter| {
let start = FRAC_PI_2 * quarter as f64;
Curve::Arc(Arc {
centre,
radius,
start_angle: start,
end_angle: start + FRAC_PI_2,
})
})
.collect()
}
/// Any other closed curve as `count` straight pieces between points on it.
///
/// The honest fallback: an ellipse or a spline has no analytic sweep, so the
/// kernel would refuse the exact form anyway. Chords at least say plainly
/// what they are.
fn split_evenly(curve: &Curve, count: usize) -> Vec<Curve> {
use acadrust::kernel::geom2d::Line;
(0..count)
.map(|step| Curve::Line(Line {
start: curve.point_at(step as f64 / count as f64),
end: curve.point_at((step + 1) as f64 / count as f64),
}))
.collect()
}
/// EXTRUDE: drag the profile `height` along its own plane's normal.
///
/// `None` for a profile that does not close, encloses nothing, or holds a
/// piece with no analytic side wall.
pub fn extruded(entity: &EntityType, height: f64) -> Option<Body> {
let profile = profile_of(entity)?;
let normal = profile.plane.normal()?;
brep::extrude(
profile.plane,
&profile.pieces,
[normal[0] * height, normal[1] * height, normal[2] * height],
)
}
/// REVOLVE: turn the profile about the axis from `from` to `to` by `angle`
/// radians.
///
/// The axis has to lie in the profile's plane — a profile and an axis that do
/// not share one sweep into surfaces with no analytic form, and the kernel
/// refuses rather than approximating them.
pub fn revolved(
entity: &EntityType,
from: [f64; 3],
to: [f64; 3],
angle: f64,
) -> Option<Body> {
let profile = profile_of(entity)?;
brep::revolve(
profile.plane,
&profile.pieces,
from,
[to[0] - from[0], to[1] - from[1], to[2] - from[2]],
angle,
)
}
#[cfg(test)]
mod tests {
use super::*;
use acadrust::entities::{Circle, LwPolyline, LwVertex};
use acadrust::types::{Vector2, Vector3};
fn square(size: f64) -> EntityType {
let mut polyline = LwPolyline::new();
for corner in [[0.0, 0.0], [size, 0.0], [size, size], [0.0, size]] {
polyline.add_vertex(LwVertex::new(Vector2::new(corner[0], corner[1])));
}
polyline.is_closed = true;
EntityType::LwPolyline(polyline)
}
fn volume(body: &Body) -> f64 {
crate::scene::model::solid_model::volume(body)
}
#[test]
fn a_closed_polyline_extrudes_into_a_prism() {
let solid = extruded(&square(10.0), 4.0).expect("a prism");
assert!((volume(&solid) - 400.0).abs() < 1e-6, "{}", volume(&solid));
}
#[test]
fn a_circle_extrudes_into_a_cylinder() {
// Cut into quarter arcs on the way, so the wall is four cylinder
// patches rather than a run of flat chords — the volume says which.
let mut circle = Circle::new();
circle.center = Vector3::new(0.0, 0.0, 0.0);
circle.radius = 5.0;
let solid = extruded(&EntityType::Circle(circle), 3.0).expect("a cylinder");
let expected = std::f64::consts::PI * 25.0 * 3.0;
let got = volume(&solid);
assert!(got > 0.98 * expected, "{got} vs {expected}");
assert!(got <= expected * 1.000_001, "{got} vs {expected}");
}
#[test]
fn a_square_revolved_about_its_own_edge_is_a_tube() {
// The axis runs up the square's left side, so what it sweeps is a
// solid cylinder of radius ten and height ten.
let solid = revolved(
&square(10.0),
[0.0, 0.0, 0.0],
[0.0, 10.0, 0.0],
std::f64::consts::TAU,
);
// The profile is in the XY plane and the axis lies in it, so this is
// a revolution about the y axis: radius ten, height ten.
let solid = solid.expect("a cylinder");
let expected = std::f64::consts::PI * 100.0 * 10.0;
let got = volume(&solid);
assert!(got > 0.98 * expected, "{got} vs {expected}");
}
#[test]
fn an_open_profile_is_refused() {
let mut polyline = LwPolyline::new();
for corner in [[0.0, 0.0], [10.0, 0.0], [10.0, 10.0]] {
polyline.add_vertex(LwVertex::new(Vector2::new(corner[0], corner[1])));
}
polyline.is_closed = false;
assert!(extruded(&EntityType::LwPolyline(polyline), 4.0).is_none());
}
#[test]
fn an_axis_off_the_profiles_plane_is_refused() {
assert!(revolved(
&square(10.0),
[0.0, 0.0, 0.0],
[0.0, 1.0, 1.0],
std::f64::consts::TAU
)
.is_none());
}
}