fix: solve concentric geometry in kernel

This commit is contained in:
Hakan Seven committed 2026-09-17 17:39:01 +03:00
1 parent 16c417f81d
commit 80f2d5fd72
11 files changed
+165 -39

No files matched your search

+1 -1
View File
@@ -1496,7 +1496,7 @@ impl OpenCADStudio {
}
}
"NCONSTRAINT" | "GCCONCENTRIC" => {
"GCCONCENTRIC" => {
use crate::command::CmdResult;
use crate::modules::parametric::ConcentricConstraintCommand;
+1 -1
View File
@@ -231,7 +231,7 @@ fn command_category(name: &str) -> &'static str {
"VCONSTRAINT",
"ECONSTRAINT",
"TCONSTRAINT",
"NCONSTRAINT",
"GCCONCENTRIC",
"NRCONSTRAINT",
"LCONSTRAINT",
"DELCONSTRAINT",
-1
View File
@@ -2784,7 +2784,6 @@ mod constraint_registry_tests {
"GCPERPENDICULAR",
"ECONSTRAINT",
"TCONSTRAINT",
"NCONSTRAINT",
"GCCONCENTRIC",
"NRCONSTRAINT",
"LCONSTRAINT",
+2 -2
View File
@@ -246,14 +246,14 @@ fn apply_geom_prop(arc: &mut Arc, field: &str, value: &str) {
"end_angle" => arc.end_angle = v.to_radians(),
"normal_x" | "normal_y" | "normal_z" => {
let center = arc.center_wcs();
let mut normal = glam::DVec3::new(arc.normal.x, arc.normal.y, arc.normal.z);
let mut normal = cadkernel::space::Vec3::new(arc.normal.x, arc.normal.y, arc.normal.z);
match field {
"normal_x" => normal.x = v,
"normal_y" => normal.y = v,
"normal_z" => normal.z = v,
_ => {}
}
if let Some(normal) = normal.try_normalize() {
if let Some(normal) = normal.normalize() {
arc.normal = acadrust::types::Vector3::new(normal.x, normal.y, normal.z);
let (x, y, z) = crate::scene::view::transform::wcs_point_to_ocs(
(center.x, center.y, center.z),
+19 -2
View File
@@ -204,14 +204,18 @@ fn apply_geom_prop(circle: &mut Circle, field: &str, value: &str) {
"area" if v > 0.0 => circle.radius = (v / PI).sqrt(),
"normal_x" | "normal_y" | "normal_z" => {
let center = circle.center_wcs();
let mut normal = glam::DVec3::new(circle.normal.x, circle.normal.y, circle.normal.z);
let mut normal = cadkernel::space::Vec3::new(
circle.normal.x,
circle.normal.y,
circle.normal.z,
);
match field {
"normal_x" => normal.x = v,
"normal_y" => normal.y = v,
"normal_z" => normal.z = v,
_ => {}
}
if let Some(normal) = normal.try_normalize() {
if let Some(normal) = normal.normalize() {
circle.normal = acadrust::types::Vector3::new(normal.x, normal.y, normal.z);
let (x, y, z) = crate::scene::view::transform::wcs_point_to_ocs(
(center.x, center.y, center.z),
@@ -376,6 +380,19 @@ impl crate::entities::traits::MassPropsCalc for Circle {
mod tests {
use super::*;
#[test]
fn editing_normal_preserves_world_center() {
let mut circle = Circle::from_coords(2.0, 3.0, 4.0, 5.0);
let center = circle.center_wcs();
apply_geom_prop(&mut circle, "normal_x", "1");
assert!((circle.center_wcs() - center).length() < 1.0e-9);
let normal_length =
(circle.normal.x.powi(2) + circle.normal.y.powi(2) + circle.normal.z.powi(2)).sqrt();
assert!((normal_length - 1.0).abs() < 1.0e-12);
}
#[test]
fn quadrant_grips_offer_interactive_radius() {
use crate::entities::traits::Grippable;
+28 -27
View File
@@ -221,36 +221,24 @@ fn apply_geom_prop(ell: &mut Ellipse, field: &str, value: &str) {
"start_angle" => ell.start_parameter = v.to_radians(),
"end_angle" => ell.end_parameter = v.to_radians(),
"normal_x" | "normal_y" | "normal_z" => {
let mut normal = glam::DVec3::new(ell.normal.x, ell.normal.y, ell.normal.z);
let mut normal = [ell.normal.x, ell.normal.y, ell.normal.z];
match field {
"normal_x" => normal.x = v,
"normal_y" => normal.y = v,
"normal_z" => normal.z = v,
"normal_x" => normal[0] = v,
"normal_y" => normal[1] = v,
"normal_z" => normal[2] = v,
_ => {}
}
if let Some(normal) = normal.try_normalize() {
let current = glam::DVec3::new(
ell.major_axis.x,
ell.major_axis.y,
ell.major_axis.z,
);
let length = current.length();
let projected = current - normal * current.dot(normal);
let major = projected.try_normalize().or_else(|| {
let helper = if normal.x.abs() < 0.8 {
glam::DVec3::X
} else {
glam::DVec3::Y
};
normal.cross(helper).try_normalize()
});
if let Some(major) = major {
ell.normal = acadrust::types::Vector3::new(normal.x, normal.y, normal.z);
let major = major * length;
ell.major_axis =
acadrust::types::Vector3::new(major.x, major.y, major.z);
}
}
let Some(normal) = cadkernel::space::Vec3::from(normal).normalize() else {
return;
};
let Some(major) = cadkernel::space::reorient_axis_to_plane(
[ell.major_axis.x, ell.major_axis.y, ell.major_axis.z],
normal.to_array(),
) else {
return;
};
ell.normal = acadrust::types::Vector3::new(normal.x, normal.y, normal.z);
ell.major_axis = acadrust::types::Vector3::new(major[0], major[1], major[2]);
}
_ => {}
}
@@ -456,6 +444,19 @@ mod grip_tests {
(v.major_axis.x * v.major_axis.x + v.major_axis.y * v.major_axis.y).sqrt()
}
#[test]
fn editing_normal_reorients_major_axis_in_kernel() {
let mut e = ell(10.0, 0.5);
apply_geom_prop(&mut e, "normal_x", "1");
let normal = DVec3::new(e.normal.x, e.normal.y, e.normal.z);
let major = DVec3::new(e.major_axis.x, e.major_axis.y, e.major_axis.z);
assert!((normal.length() - 1.0).abs() < 1.0e-12);
assert!((major.length() - 10.0).abs() < 1.0e-9);
assert!(normal.dot(major).abs() < 1.0e-9);
}
#[test]
fn minor_grip_below_major_updates_ratio_only() {
let mut e = ell(10.0, 0.5);
+27
View File
@@ -156,3 +156,30 @@ impl CadCommand for ConcentricConstraintCommand {
CmdResult::Cancel
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn polyline_pick_keeps_the_selected_arc_center() {
let handle = Handle::new(13);
let mut polyline = acadrust::entities::LwPolyline::new();
polyline.vertices = vec![
acadrust::entities::LwVertex::from_coords(0.0, 0.0),
acadrust::entities::LwVertex::with_bulge(
acadrust::types::Vector2::new(5.0, 0.0),
1.0,
),
acadrust::entities::LwVertex::from_coords(10.0, 0.0),
];
let reference = ConcentricConstraintCommand::picked_reference(
&EntityType::LwPolyline(polyline),
handle,
DVec3::new(8.0, 2.0, 0.0),
)
.expect("arc segment");
assert_eq!(reference, ParametricRef::segment_center(handle, 1));
}
}
-1
View File
@@ -167,7 +167,6 @@ inventory::submit!(crate::command::CommandRegistration {
"GCPERPENDICULAR",
"ECONSTRAINT",
"TCONSTRAINT",
"NCONSTRAINT",
"GCCONCENTRIC",
"NRCONSTRAINT",
"LCONSTRAINT",
+83
View File
@@ -3520,6 +3520,17 @@ mod tests {
use acadrust::entities::EntityType;
use acadrust::types::Vector3;
fn spatial_circle(center: Vector3, radius: f64) -> acadrust::entities::Circle {
let normal = Vector3::UNIT_Y;
let (x, y, z) = crate::scene::view::transform::wcs_point_to_ocs(
(center.x, center.y, center.z),
(normal.x, normal.y, normal.z),
);
let mut circle = acadrust::entities::Circle::from_coords(x, y, z, radius);
circle.normal = normal;
circle
}
#[test]
fn polyline_constraint_order_follows_stored_vertices_for_open_and_closed_polylines() {
use acadrust::entities::{LwPolyline, LwVertex, Polyline2D, Vertex2D};
@@ -4278,4 +4289,76 @@ mod tests {
< 1.0e-9);
assert!(jet.tangent[1].abs() < 1.0e-9 && jet.tangent[2].abs() < 1.0e-9);
}
#[test]
fn spatial_concentric_initial_solve_keeps_the_first_circle_fixed() {
let mut scene = Scene::new();
let first = scene.add_entity(EntityType::Circle(spatial_circle(Vector3::ZERO, 2.0)));
let second = scene.add_entity(EntityType::Circle(spatial_circle(
Vector3::new(5.0, 0.0, 3.0),
4.0,
)));
let first_ref = ParametricRef::center(first);
let second_ref = ParametricRef::center(second);
scene
.parametric_constraint_set_mut(ParametricScope::ModelSpace)
.add(
ConstraintKind::Concentric,
vec![first_ref, second_ref],
None,
);
scene.bump_entities_with_initial_parametric_policy(
&[
(first, super::ChangeKind::Modified),
(second, super::ChangeKind::Modified),
],
&[first_ref],
true,
);
let EntityType::Circle(first_circle) = scene.document.get_entity(first).unwrap() else {
panic!("expected circle");
};
let EntityType::Circle(second_circle) = scene.document.get_entity(second).unwrap() else {
panic!("expected circle");
};
assert!(first_circle.center_wcs().length() < 1.0e-9);
assert!(second_circle.center_wcs().length() < 1.0e-9);
assert!((second_circle.radius - 4.0).abs() < 1.0e-12);
}
#[test]
fn spatial_concentric_chain_follows_the_driven_end() {
let mut scene = Scene::new();
let handles = [1.0, 2.0, 3.0].map(|radius| {
scene.add_entity(EntityType::Circle(spatial_circle(Vector3::ZERO, radius)))
});
let set = scene.parametric_constraint_set_mut(ParametricScope::ModelSpace);
set.add(
ConstraintKind::Concentric,
vec![ParametricRef::center(handles[0]), ParametricRef::center(handles[1])],
None,
);
set.add(
ConstraintKind::Concentric,
vec![ParametricRef::center(handles[1]), ParametricRef::center(handles[2])],
None,
);
let driven_center = Vector3::new(8.0, 0.0, -3.0);
*scene.document.get_entity_mut(handles[2]).unwrap() =
EntityType::Circle(spatial_circle(driven_center, 3.0));
scene.bump_entities_with_parametric_driven(
&[(handles[2], super::ChangeKind::Modified)],
&[ParametricRef::whole(handles[2])],
);
for handle in handles {
let EntityType::Circle(circle) = scene.document.get_entity(handle).unwrap() else {
panic!("expected circle");
};
assert!((circle.center_wcs() - driven_center).length() < 1.0e-9);
}
}
}