Files
OneCAD-Draft/src/modules/draw/modify/stretch.rs
T
Hakan SevenandClaude Opus 4.8 57710eec2b fix(precision): keep typed coordinates exact in f64 (#311)
Typed values were quantized to the f32 grid before reaching the
entity (e.g. a length of 2000.8 committed as 2000.8020), worsening
with magnitude. The interaction point layer was all glam::Vec3 (f32).

Two-layer fix:

1. Coordinate backbone -> f64. last_point, last_cursor_world,
   dyn_anchor, dyn_ref become DVec3; UcsXform transforms, parse_coord,
   the ucs helper fns, dyn_resolve_point, the OTRACK typed-distance
   paths, command-line coordinate entry and the coordinate readout all
   compute in f64. Screen/GPU/alignment stays f32 (snap engine, render
   geometry, projection, UCS-icon/viewcube drawing) with casts only at
   those boundaries.

2. Per-command committed storage -> DVec3. on_point already received a
   DVec3, but many command impls narrowed it back to f32 (let pt =
   pt.as_vec3(); Vec<Vec3> fields) and re-widened at build, quantizing
   the committed coordinate. Migrated PLINE, SPLINE, MLINE, LENGTHEN,
   INSERT, 3D primitives, every dimension/leader/mleader, ray, revcloud,
   wipeout, attdef, table, tolerance, array center, mview, plot_window,
   plus the QDIM_PLACE handler and qdim_collect_points. Preview,
   hit-test and rubber-band paths keep f32 at the boundary.

Hatch manual boundaries are left for a follow-up: HatchModel.boundary
is structurally Arc<Vec<[f32;2]>> and needs a model-level widen.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-11 21:26:40 +03:00

183 lines
6.2 KiB
Rust

// Stretch tool — ribbon definition + interactive command.
//
// Command: STRETCH (SS)
// Workflow:
// 1. Pick first corner of the crossing window (right-to-left = crossing).
// 2. Pick second corner.
// 3. Pick base point.
// 4. Pick new point → stretches only vertices inside the crossing window.
//
// Entity behaviour:
// Line : move start if inside, move end if inside, move both if both inside.
// LwPolyline : move each vertex independently.
// Polyline/P2D: move each vertex independently.
// Arc / Circle: move the whole entity if its center is inside the window.
// Insert : move the whole entity if its insertion point is inside.
// All others : move the whole entity if any point is inside.
use acadrust::Handle;
use glam::DVec3;
use crate::command::{CadCommand, CmdResult};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::model::wire_model::WireModel;
// ── Ribbon definition ──────────────────────────────────────────────────────
pub fn tool() -> ToolDef {
ToolDef {
id: "STRETCH",
label: "Stretch",
icon: IconKind::Svg(include_bytes!("../../../../assets/icons/stretch.svg")),
event: ModuleEvent::Command("STRETCH".to_string()),
}
}
// ── Command implementation ─────────────────────────────────────────────────
enum Step {
/// Waiting for the first crossing-window corner.
WindowCorner1,
/// Waiting for the second corner; `c1` is the first corner.
WindowCorner2(DVec3),
/// Crossing window defined; waiting for base point.
Base { win_min: DVec3, win_max: DVec3 },
/// Waiting for target point.
Target {
win_min: DVec3,
win_max: DVec3,
base: DVec3,
},
}
pub struct StretchCommand {
handles: Vec<Handle>,
wire_models: Vec<WireModel>,
step: Step,
}
impl StretchCommand {
pub fn new(handles: Vec<Handle>, wire_models: Vec<WireModel>) -> Self {
Self {
handles,
wire_models,
step: Step::WindowCorner1,
}
}
}
impl CadCommand for StretchCommand {
fn name(&self) -> &'static str {
"STRETCH"
}
fn prompt(&self) -> String {
match &self.step {
Step::WindowCorner1 => format!(
"STRETCH Specify first corner of crossing window [{} objects]:",
self.handles.len()
),
Step::WindowCorner2(_) => "STRETCH Specify opposite corner:".into(),
Step::Base { .. } => "STRETCH Specify base point:".into(),
Step::Target { base, .. } => format!(
"STRETCH Specify new point [base {:.3},{:.3}]:",
base.x, base.z
),
}
}
fn on_point(&mut self, pt: DVec3) -> CmdResult {
match &self.step {
Step::WindowCorner1 => {
self.step = Step::WindowCorner2(pt);
CmdResult::NeedPoint
}
Step::WindowCorner2(c1) => {
let win_min = c1.min(pt);
let win_max = c1.max(pt);
self.step = Step::Base { win_min, win_max };
CmdResult::NeedPoint
}
Step::Base { win_min, win_max } => {
let (wmin, wmax) = (*win_min, *win_max);
self.step = Step::Target {
win_min: wmin,
win_max: wmax,
base: pt,
};
CmdResult::NeedPoint
}
Step::Target {
win_min,
win_max,
base,
} => {
let delta = pt - *base;
CmdResult::StretchEntities {
handles: self.handles.clone(),
win_min: *win_min,
win_max: *win_max,
delta,
}
}
}
}
fn on_enter(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn on_escape(&mut self) -> CmdResult {
CmdResult::Cancel
}
fn window_corner_pick(&self) -> bool {
// The two crossing-window corners are free points; Ortho/Polar must not
// pin the opposite corner to an axis or the window becomes a line (#291).
matches!(self.step, Step::WindowCorner1 | Step::WindowCorner2(_))
}
fn window_first_corner(&self) -> Option<DVec3> {
// Expose the first corner so the host draws a filled crossing marquee to
// the cursor, matching a normal box selection instead of a bare outline.
match &self.step {
Step::WindowCorner2(c1) => Some(*c1),
_ => None,
}
}
fn on_preview_wires(&mut self, pt: DVec3) -> Vec<WireModel> {
match &self.step {
// The crossing-window rectangle is drawn as a filled selection
// marquee by the host (via window_first_corner) so it matches a
// normal box selection — nothing to draw here. (#291)
Step::WindowCorner2(_) => vec![],
Step::Target {
win_min,
win_max,
base,
} => {
let delta = pt - *base;
// Live ghost: vertices inside the crossing window follow the
// cursor, the rest stay anchored. This is the preview/GPU path,
// so downcast to f32 only at the WireModel boundary.
let mut out: Vec<WireModel> = self
.wire_models
.iter()
.map(|w| w.stretched((*win_min).as_vec3(), (*win_max).as_vec3(), delta.as_vec3()))
.collect();
out.push(WireModel::solid(
"rubber_band".into(),
vec![
[base.x as f32, base.y as f32, base.z as f32],
[pt.x as f32, pt.y as f32, pt.z as f32],
],
WireModel::CYAN,
false,
));
out
}
_ => vec![],
}
}
}