Files
OneCAD-Draft/src/scene/convert/acad_to_truck.rs
T
Hakan SevenandClaude Opus 4.8 3f060a570a fix(pick): hit-test lineweight width and thickness walls
Selection tested a flat 8 px radius around each wire's centreline and
nothing else, so two things the user can see were not pickable.

A wire renders as a band `line_weight_px` wide, so pick now widens to that
rendered half-width via `pick_tolerance_px`, mirroring the wire shader's
`select(0.5, half_width, lwdisplay_enable)`. Behaviour is unchanged for
every standard weight (the widest, 2.11 mm, renders 7.97 px half-width —
just under the 8 px threshold); this keeps the two sides from drifting
apart if the display boost in `lineweight_to_px` ever changes.

Entities extruded by a DXF thickness draw their wall as four edges with
nothing in between, so the interior was a hole the cursor fell through.
WireModel gains `pick_tris`: triangles hit-testing treats as solid and the
renderer never uploads. Kept apart from `fill_tris` because that channel
reaches the GPU, and a wall that rendered shaded would change how every
thickness drawing looks. Ranked below `fill_tris` — those are drawn, these
are not, so where they overlap the drawn face is what the user means.

Walls are dropped when a wire is projected into a floating viewport:
`points` are reprojected and clipped into paper coords there, so cloned
model-space triangles would hit-test at a model-scale offset. Extruded
entities stay selectable by their edges, which do get projected.

The wall loop runs on every hover that misses everything else, so it
rejects on the wire's AABB first — an extruded circle is ~128 triangles
and a drawing full of thickness would otherwise project every wall on
each mouse move.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 14:58:55 +03:00

114 lines
4.5 KiB
Rust

// acadrust -> truck topology conversion layer.
use acadrust::{CadDocument, EntityType};
use truck_modeling::{Edge, Solid, Vertex, Wire};
use crate::entities::traits::EntityTypeOps;
use crate::scene::model::wire_model::{SnapHint, TangentGeom};
/// One group of glyph strokes with its world-space origin stored in f64.
/// Strokes are in glyph-local space (origin = [0,0]) so that the large
/// world offset can be subtracted with f64 precision in tessellate.rs.
///
/// `color`, when set, overrides the entity colour for just this group — used
/// by MTEXT inline `\C` / `\c` per-run colour. Strokes sharing the same
/// (color, None) override are merged into one WireModel downstream; runs with
/// distinct colours emit their own WireModel.
pub struct TextStroke {
pub strokes: Vec<Vec<[f32; 2]>>,
pub origin: [f64; 2],
pub color: Option<[f32; 3]>,
pub fill_tris: Vec<[f32; 2]>,
/// Layout inputs to rebuild this run as per-glyph SDF quads (see
/// `scene::text::glyph_quads`). `Some` on runs wired for the SDF text
/// renderer; `None` leaves the run to the stroke path only. Heights are
/// raw (pre annotation-scale), matching `strokes` — the SDF collector
/// applies annotation scale the same way `tessellate` does for strokes.
pub run: Option<GlyphRun>,
}
/// Per-run text-layout inputs needed to reproduce a run as SDF glyph quads.
#[derive(Clone, Debug)]
pub struct GlyphRun {
pub text: String,
pub font: String,
/// Raw height in drawing units (annotation scale applied later).
pub height: f32,
pub rotation: f32,
pub width_factor: f32,
pub oblique: f32,
pub tracking: f32,
/// Bold run — the SDF glyph bakes with a wider pen (thicker strokes).
pub bold: bool,
}
#[allow(dead_code)]
pub enum TruckObject {
Point(Vertex),
Curve(Edge),
Contour(Wire),
Text(Vec<TextStroke>),
/// Pre-computed NaN-separated 3-D point list (leader lines, arrowheads, etc.).
/// Points are stored in WCS as **f64** so the large world_offset can be
/// subtracted in full precision in tessellate.rs before the f32 cast.
/// Casting WCS coordinates to f32 in the entity converters used to wreck
/// rotated sub-glyph precision on drawings far from origin.
Lines(Vec<[f64; 3]>),
/// Like Lines but linetype pattern restarts at each NaN-separated segment (plinegen=false).
SegmentedLines(Vec<[f64; 3]>),
Volume(Solid),
}
pub struct TruckEntity {
pub object: TruckObject,
pub snap_pts: Vec<(glam::DVec3, SnapHint)>,
pub tangent_geoms: Vec<TangentGeom>,
/// Polyline vertex positions in WCS f64; converted to offset-relative f32
/// at the wire-model boundary.
pub key_vertices: Vec<[f64; 3]>,
/// Pre-triangulated fill geometry: flat list of WCS f64 vertices, 3 per
/// triangle. Non-empty for mesh-like entities (PolyfaceMesh, PolygonMesh)
/// that need solid fill.
pub fill_tris: Vec<[f64; 3]>,
/// Pre-triangulated pick-only geometry: flat list of WCS f64 vertices, 3
/// per triangle. Non-empty for entities carrying a DXF thickness (code 39)
/// — the swept wall between each base segment and its extruded copy. Kept
/// apart from `fill_tris` because only that one reaches the GPU; see
/// [`WireModel::pick_tris`](crate::scene::model::wire_model::WireModel::pick_tris).
pub pick_tris: Vec<[f64; 3]>,
}
pub fn convert(entity: &EntityType, document: &CadDocument) -> Option<TruckEntity> {
entity.to_truck_entity(document)
}
/// Triangulate the wall swept by extruding `base` along `extrusion` — the
/// geometry a DXF thickness (code 39) adds to an entity. Two triangles per
/// base segment, in `TruckEntity::pick_tris` order (flat WCS f64, 3 per tri).
///
/// `base` is a point chain in the same form a wire uses, so a NaN entry breaks
/// the sweep exactly as it breaks a segment chain — a polyline with disjoint
/// runs extrudes into one wall per run, not a wall bridging the gap.
pub fn extrusion_wall_tris(base: &[[f64; 3]], extrusion: [f64; 3]) -> Vec<[f64; 3]> {
let top = |p: [f64; 3]| {
[
p[0] + extrusion[0],
p[1] + extrusion[1],
p[2] + extrusion[2],
]
};
let mut out = Vec::new();
let mut prev: Option<[f64; 3]> = None;
for &p in base {
if p[0].is_nan() {
prev = None;
continue;
}
if let Some(a) = prev {
out.extend_from_slice(&[a, p, top(p), a, top(p), top(a)]);
}
prev = Some(p);
}
out
}