feat(ucs): integrate coordinate systems

Route coordinate input, geometry creation, transforms, properties,\nviews, and viewport persistence through a shared working plane.\n\nRefs #484
This commit is contained in:
Hakan Seven committed 2026-08-04 23:17:54 +03:00
1 parent b1d55c6a7e
commit 657d023fb7
61 files changed
+2218 -747

No files matched your search

+26 -16
View File
@@ -14,10 +14,10 @@
// 1. Center point → 2. Item count (text) → 3. Total angle in degrees (text)
use acadrust::Handle;
use glam::{DVec3, Mat4};
use glam::DVec3;
use crate::t;
use crate::command::{CadCommand, CmdResult, EntityTransform};
use crate::command::{CadCommand, CmdResult, EntityTransform, WorkingPlane};
use crate::modules::draw::defaults;
use crate::modules::IconKind;
use crate::scene::model::wire_model::WireModel;
@@ -63,7 +63,7 @@ pub struct ArrayRectCommand {
default_cols: u32,
default_row_sp: f64,
default_col_sp: f64,
ucs: Mat4,
plane: WorkingPlane,
}
impl ArrayRectCommand {
@@ -76,18 +76,17 @@ impl ArrayRectCommand {
default_cols: defaults::get_array_cols() as u32,
default_row_sp: defaults::get_array_row_sp(),
default_col_sp: defaults::get_array_col_sp(),
ucs: Mat4::IDENTITY,
plane: WorkingPlane::default(),
}
}
/// Row/column offsets run along the active UCS axes (`ucs` rotates the
/// world-frame grid step), identity = world.
/// Row/column offsets run along the active coordinate axes.
fn build_transforms(
rows: u32,
cols: u32,
row_sp: f64,
col_sp: f64,
ucs: Mat4,
plane: WorkingPlane,
) -> Vec<EntityTransform> {
let mut t = Vec::new();
for r in 0..rows {
@@ -95,9 +94,11 @@ impl ArrayRectCommand {
if r == 0 && c == 0 {
continue;
}
t.push(EntityTransform::Translate(ucs.as_dmat4().transform_vector3(
glam::DVec3::new(col_sp * c as f64, row_sp * r as f64, 0.0),
)));
t.push(EntityTransform::Translate(plane.vector_to_world(DVec3::new(
col_sp * c as f64,
row_sp * r as f64,
0.0,
))));
}
}
t
@@ -109,8 +110,8 @@ impl CadCommand for ArrayRectCommand {
"ARRAYRECT"
}
fn set_ucs(&mut self, ucs: Mat4) {
self.ucs = ucs;
fn set_working_plane(&mut self, plane: WorkingPlane) {
self.plane = plane;
}
fn prompt(&self) -> String {
@@ -194,7 +195,7 @@ impl CadCommand for ArrayRectCommand {
};
Some(CmdResult::BatchCopy(
self.handles.clone(),
Self::build_transforms(rows, cols, row_sp, col_sp, self.ucs),
Self::build_transforms(rows, cols, row_sp, col_sp, self.plane),
))
}
}
@@ -219,7 +220,7 @@ impl CadCommand for ArrayRectCommand {
}
RectStep::ColSp { rows, cols, row_sp } => (rows, cols, row_sp, self.default_col_sp),
};
Self::build_transforms(rows, cols, row_sp, col_sp, self.ucs)
Self::build_transforms(rows, cols, row_sp, col_sp, self.plane)
.iter()
.flat_map(|t| {
if let EntityTransform::Translate(delta) = t {
@@ -262,6 +263,7 @@ pub struct ArrayPolarCommand {
step: PolarStep,
default_count: u32,
default_angle: f64,
plane: WorkingPlane,
}
impl ArrayPolarCommand {
@@ -272,11 +274,16 @@ impl ArrayPolarCommand {
step: PolarStep::Center,
default_count: defaults::get_array_p_count() as u32,
default_angle: defaults::get_array_p_angle(),
plane: WorkingPlane::default(),
}
}
}
impl CadCommand for ArrayPolarCommand {
fn set_working_plane(&mut self, plane: WorkingPlane) {
self.plane = plane;
}
fn name(&self) -> &'static str {
"ARRAYPOLAR"
}
@@ -342,6 +349,7 @@ impl CadCommand for ArrayPolarCommand {
let transforms = (1..count)
.map(|n| EntityTransform::Rotate {
center,
axis: self.plane.z,
angle_rad: step_rad * n as f64,
})
.collect();
@@ -363,12 +371,13 @@ impl CadCommand for ArrayPolarCommand {
}
};
let step_rad = total_deg.to_radians() / count as f64;
let axis = self.plane.z.as_vec3();
let mut out: Vec<WireModel> = (1..count)
.flat_map(|n| {
let angle_rad = (step_rad * n as f64) as f32;
self.wire_models
.iter()
.map(move |w| w.rotated(center, angle_rad))
.map(move |wire| wire.rotated_about_axis(center, axis, angle_rad))
})
.collect();
// Rubber-band from center to cursor while picking the center point.
@@ -675,6 +684,7 @@ impl ArrayPathCommand {
let center = Self::rigid_center(p0, p, dth);
EntityTransform::Rotate {
center,
axis: DVec3::Z,
angle_rad: dth,
}
}
@@ -814,7 +824,7 @@ impl CadCommand for ArrayPathCommand {
.iter()
.map(|w| w.translated(delta.as_vec3()))
.collect::<Vec<_>>(),
EntityTransform::Rotate { center, angle_rad } => self
EntityTransform::Rotate { center, angle_rad, .. } => self
.wire_models
.iter()
.map(|w| w.rotated(center.as_vec3(), *angle_rad as f32))
+31 -7
View File
@@ -12,7 +12,7 @@ use acadrust::Handle;
use glam::DVec3;
use crate::t;
use crate::command::{CadCommand, CmdResult, EntityTransform};
use crate::command::{CadCommand, CmdResult, EntityTransform, WorkingPlane};
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::model::wire_model::WireModel;
@@ -41,6 +41,7 @@ pub struct MirrorCommand {
text_ghosts: Vec<(WireModel, DVec3)>,
mirror_text: bool,
step: Step,
plane: WorkingPlane,
}
impl MirrorCommand {
@@ -56,11 +57,16 @@ impl MirrorCommand {
text_ghosts,
mirror_text,
step: Step::P1,
plane: WorkingPlane::default(),
}
}
}
impl CadCommand for MirrorCommand {
fn set_working_plane(&mut self, plane: WorkingPlane) {
self.plane = plane;
}
fn name(&self) -> &'static str {
"MIRROR"
}
@@ -147,18 +153,32 @@ impl CadCommand for MirrorCommand {
let mut out: Vec<WireModel> = self
.wire_models
.iter()
.map(|w| w.mirrored(p1.as_vec3(), p2.as_vec3()))
.map(|w| {
w.mirrored_in_plane(
p1.as_vec3(),
p2.as_vec3(),
self.plane.z.as_vec3(),
)
})
.collect();
// Text ghosts honour MIRRTEXT: on → true glyph mirror (full reflection);
// off → keep glyphs readable, relocate to the mirror-symmetric position
// by reflecting the box centre and translating.
for (w, center) in &self.text_ghosts {
if self.mirror_text {
out.push(w.mirrored(p1.as_vec3(), p2.as_vec3()));
out.push(w.mirrored_in_plane(
p1.as_vec3(),
p2.as_vec3(),
self.plane.z.as_vec3(),
));
} else {
let (mut cx, mut cy) = (center.x, center.y);
crate::scene::view::transform::reflect_xy_point(&mut cx, &mut cy, p1, p2);
let delta = glam::Vec3::new((cx - center.x) as f32, (cy - center.y) as f32, 0.0);
let reflected = crate::scene::view::transform::reflected_point(
*center,
p1,
p2,
self.plane.z,
);
let delta = (reflected - *center).as_vec3();
out.push(w.translated(delta));
}
}
@@ -180,7 +200,11 @@ impl MirrorCommand {
/// Commit the mirror. `erase` true flips the originals in place; false
/// keeps them and adds a mirrored copy. Either way the command ends.
fn finish(&self, p1: DVec3, p2: DVec3, erase: bool) -> CmdResult {
let xform = EntityTransform::Mirror { p1, p2 };
let xform = EntityTransform::Mirror {
p1,
p2,
working_normal: self.plane.z,
};
if erase {
CmdResult::TransformSelected(self.handles.clone(), xform)
} else {
+32 -24
View File
@@ -13,7 +13,7 @@ use acadrust::Handle;
use glam::DVec3;
use crate::t;
use crate::command::{CadCommand, CmdResult, DynField, EntityTransform};
use crate::command::{CadCommand, CmdResult, DynField, EntityTransform, WorkingPlane};
use crate::modules::draw::defaults;
use crate::modules::{IconKind, ModuleEvent, ToolDef};
use crate::scene::model::wire_model::WireModel;
@@ -44,6 +44,7 @@ pub struct RotateCommand {
wire_models: Vec<WireModel>,
step: Step,
default_angle: f64, // degrees
plane: WorkingPlane,
}
impl RotateCommand {
@@ -53,6 +54,7 @@ impl RotateCommand {
wire_models,
step: Step::Center,
default_angle: defaults::get_rotate_angle(),
plane: WorkingPlane::default(),
}
}
@@ -60,12 +62,20 @@ impl RotateCommand {
defaults::set_rotate_angle(angle_rad.to_degrees());
CmdResult::TransformSelected(
self.handles.clone(),
EntityTransform::Rotate { center, angle_rad },
EntityTransform::Rotate {
center,
axis: self.plane.z,
angle_rad,
},
)
}
}
impl CadCommand for RotateCommand {
fn set_working_plane(&mut self, plane: WorkingPlane) {
self.plane = plane;
}
fn name(&self) -> &'static str {
"ROTATE"
}
@@ -110,11 +120,9 @@ impl CadCommand for RotateCommand {
}
Step::Angle { center } => {
let center = *center;
let direction = pt - center;
if direction.x.hypot(direction.y) <= f64::EPSILON {
let Some(angle_rad) = self.plane.angle(center, pt) else {
return CmdResult::NeedPoint;
}
let angle_rad = direction.y.atan2(direction.x);
};
self.commit(center, angle_rad)
}
Step::RefFirst { center } => {
@@ -123,22 +131,18 @@ impl CadCommand for RotateCommand {
CmdResult::NeedPoint
}
Step::RefSecond { center, first } => {
let direction = pt - *first;
if direction.x.hypot(direction.y) <= f64::EPSILON {
return CmdResult::NeedPoint;
}
let center = *center;
let ref_angle = direction.y.atan2(direction.x);
let Some(ref_angle) = self.plane.angle(*first, pt) else {
return CmdResult::NeedPoint;
};
self.step = Step::RefNew { center, ref_angle };
CmdResult::NeedPoint
}
Step::RefNew { center, ref_angle } => {
let center = *center;
let direction = pt - center;
if direction.x.hypot(direction.y) <= f64::EPSILON {
let Some(new_angle) = self.plane.angle(center, pt) else {
return CmdResult::NeedPoint;
}
let new_angle = direction.y.atan2(direction.x);
};
self.commit(center, new_angle - *ref_angle)
}
}
@@ -192,11 +196,10 @@ impl CadCommand for RotateCommand {
fn on_preview_wires(&mut self, pt: DVec3) -> Vec<WireModel> {
let (center, angle_rad) = match &self.step {
Step::Angle { center } => {
let direction = pt - *center;
if direction.x.hypot(direction.y) <= f64::EPSILON {
let Some(angle) = self.plane.angle(*center, pt) else {
return vec![];
}
(*center, direction.y.atan2(direction.x))
};
(*center, angle)
}
Step::RefSecond { first, .. } => {
return vec![WireModel::solid(
@@ -210,11 +213,10 @@ impl CadCommand for RotateCommand {
)];
}
Step::RefNew { center, ref_angle } => {
let direction = pt - *center;
if direction.x.hypot(direction.y) <= f64::EPSILON {
let Some(angle) = self.plane.angle(*center, pt) else {
return vec![];
}
(*center, direction.y.atan2(direction.x) - *ref_angle)
};
(*center, angle - *ref_angle)
}
_ => return vec![],
};
@@ -222,7 +224,13 @@ impl CadCommand for RotateCommand {
// drawn by the dynamic-input overlay (polar guide), not here.
self.wire_models
.iter()
.map(|w| w.rotated(center.as_vec3(), angle_rad as f32))
.map(|w| {
w.rotated_about_axis(
center.as_vec3(),
self.plane.z.as_vec3(),
angle_rad as f32,
)
})
.collect()
}