Merge pull request #1288 from ramox81/feat/auto-constraint-complete

# Conflicts:
#	src/ui/overlay.rs
This commit is contained in:
Hakan Seven committed 2026-09-15 14:41:43 +03:00
commit cb4c4cef65
3 files changed
+264 -29

No files matched your search

+17 -3
View File
@@ -813,7 +813,14 @@ bg={bg_ms:.1}ms n={view_count}"
.active_cmd
.as_ref()
.is_some_and(|cmd| !cmd.needs_entity_pick() && !cmd.is_selection_gathering());
let constraint_glyphs: Vec<(iced::Point, [f32; 2], String, bool, bool)> = if is_paper {
let constraint_glyphs: Vec<(
iced::Point,
[f32; 2],
String,
bool,
bool,
Vec<iced::Point>,
)> = if is_paper {
Vec::new()
} else {
let scope = tab.current_parametric_scope();
@@ -825,9 +832,16 @@ bg={bg_ms:.1}ms n={view_count}"
self.constraint_bar_display,
)
.into_iter()
.map(|(id, point, direction, label, is_conflicting)| {
.map(|(id, point, direction, label, is_conflicting, hover_points)| {
let selected = tab.scene.selected_constraint == Some(id);
(point, direction, label, is_conflicting, selected)
(
point,
direction,
label,
is_conflicting,
selected,
hover_points,
)
})
.collect()
};
+173 -5
View File
@@ -606,6 +606,121 @@ pub(crate) fn glyph_placement(
}
}
/// World-space locations that explain what a hovered constraint acts on.
/// Point constraints expose their referenced point directly; curve relations
/// expose the contact or intersection that makes the relation visible.
fn constraint_segment_endpoints(
document: &acadrust::CadDocument,
reference: ParametricRef,
) -> Option<[Vector3; 2]> {
let segment = reference.segment_index()?;
let entity = document.get_entity(reference.entity)?;
let points = super::dimension_assoc::source_points(entity);
let closed = match entity {
acadrust::EntityType::LwPolyline(polyline) => polyline.is_closed,
acadrust::EntityType::Polyline2D(polyline) => polyline.is_closed(),
_ => return None,
};
let first = *points.get(segment)?;
let second = if segment + 1 < points.len() {
points[segment + 1]
} else if closed {
*points.first()?
} else {
return None;
};
Some([first, second])
}
fn constraint_reference_curve_xy(
document: &acadrust::CadDocument,
reference: ParametricRef,
) -> Option<cadkernel::geom2d::Curve> {
let curve = crate::entities::curve::entity_curve_xy(document.get_entity(reference.entity)?)?;
reference
.segment_index()
.map(|index| curve.segments().into_iter().nth(index))
.unwrap_or(Some(curve))
}
pub(crate) fn constraint_hover_points(
document: &acadrust::CadDocument,
constraint: &ParametricConstraint,
) -> Vec<Vector3> {
let mut points = Vec::new();
let push_unique = |points: &mut Vec<Vector3>, point: Vector3| {
if point.x.is_finite()
&& point.y.is_finite()
&& point.z.is_finite()
&& !points
.iter()
.any(|existing| (*existing - point).length_squared() <= 1.0e-12)
{
points.push(point);
}
};
for reference in &constraint.refs {
let Some(marker) = reference.marker else {
continue;
};
let Some(entity) = document.get_entity(reference.entity) else {
continue;
};
if let Some(point) = resolve_point(entity, marker) {
push_unique(&mut points, point);
}
}
if matches!(
constraint.kind,
ConstraintKind::Horizontal | ConstraintKind::Vertical
) {
for reference in &constraint.refs {
let Some(entity) = document.get_entity(reference.entity) else {
continue;
};
if let Some(endpoints) = constraint_segment_endpoints(document, *reference) {
for point in endpoints {
push_unique(&mut points, point);
}
} else if matches!(entity, acadrust::EntityType::Line(_)) {
for point in super::dimension_assoc::source_points(entity) {
push_unique(&mut points, point);
}
}
}
}
if matches!(
constraint.kind,
ConstraintKind::Perpendicular | ConstraintKind::Tangent
) {
if let [first, second, ..] = constraint.refs.as_slice() {
if let (Some(first_curve), Some(second_curve)) = (
constraint_reference_curve_xy(document, *first),
constraint_reference_curve_xy(document, *second),
) {
let elevation = glyph_placement(document, constraint)
.map(|(anchor, _)| anchor.z)
.unwrap_or(0.0);
for crossing in cadkernel::geom2d::intersect(
&first_curve,
&second_curve,
cadkernel::geom2d::Tolerance::default(),
) {
push_unique(
&mut points,
Vector3::new(crossing.point[0], crossing.point[1], elevation),
);
}
}
}
}
points
}
impl super::Scene {
pub fn is_parametric_constraint_visible(
&self,
@@ -964,8 +1079,9 @@ impl super::Scene {
}
/// Screen-projected parametric-constraint glyph placements for `scope`:
/// `(id, anchor, outward_screen_direction, label, is_conflicting)` for
/// every enabled, visible constraint whose glyph projects on-screen.
/// `(id, anchor, outward_screen_direction, label, is_conflicting,
/// hover_points)` for every enabled, visible constraint whose glyph
/// projects on-screen.
/// `vp_size` is the full canvas size (as `SelectionState::vp_size`
/// reports it), matching what `viewport_edit_frame`/
/// `active_model_tile_bounds` expect. Mirrors the projection
@@ -980,7 +1096,14 @@ impl super::Scene {
vp_size: (f32, f32),
show_values: bool,
display_mode: i16,
) -> Vec<(ConstraintId, iced::Point, [f32; 2], String, bool)> {
) -> Vec<(
ConstraintId,
iced::Point,
[f32; 2],
String,
bool,
Vec<iced::Point>,
)> {
let Some(set) = self.parametric_constraint_set(scope) else {
return Vec::new();
};
@@ -1034,7 +1157,30 @@ impl super::Scene {
} else {
c.kind.glyph_symbol().to_string()
};
point.x.is_finite().then_some((c.id, point, direction.to_array(), label, is_conflicting))
let hover_points = constraint_hover_points(&self.document, c)
.into_iter()
.filter_map(|hover_point| {
let projected = crate::scene::pick::grip::project_rte(
glam::DVec3::new(hover_point.x, hover_point.y, hover_point.z),
view_rot,
eye,
bounds,
)?;
let point = iced::Point::new(
bounds.x + projected.x,
bounds.y + projected.y,
);
(point.x.is_finite() && point.y.is_finite()).then_some(point)
})
.collect();
point.x.is_finite().then_some((
c.id,
point,
direction.to_array(),
label,
is_conflicting,
hover_points,
))
})
.collect()
}
@@ -1060,7 +1206,7 @@ impl super::Scene {
);
let glyphs: Vec<(iced::Point, [f32; 2], String, bool)> = placements
.iter()
.map(|(_, point, direction, label, is_conflicting)| {
.map(|(_, point, direction, label, is_conflicting, _)| {
(*point, *direction, label.clone(), *is_conflicting)
})
.collect();
@@ -1546,4 +1692,26 @@ mod tests {
assert!(!scene.should_display_parametric_constraint(scope, id, true, 2));
}
#[test]
fn hover_geometry_keeps_a_bulged_polyline_segment_curved() {
let mut document = acadrust::CadDocument::new();
let mut polyline = acadrust::entities::LwPolyline::new();
polyline.common.handle = h(1);
polyline.vertices = vec![
acadrust::entities::LwVertex::with_bulge(
acadrust::types::Vector2::new(0.0, 0.0),
1.0,
),
acadrust::entities::LwVertex::from_coords(10.0, 0.0),
];
document
.add_entity(acadrust::EntityType::LwPolyline(polyline))
.unwrap();
assert!(matches!(
constraint_reference_curve_xy(&document, ParametricRef::segment(h(1), 0)),
Some(cadkernel::geom2d::Curve::Arc(_))
));
}
}
+74 -21
View File
@@ -27,6 +27,7 @@ const CONSTRAINT_GLYPH_PAD_X: f32 = 7.0;
const CONSTRAINT_GLYPH_PAD_Y: f32 = 4.0;
const CONSTRAINT_GLYPH_GAP: f32 = 6.0;
const CONSTRAINT_GLYPH_ROW_GAP: f32 = 4.0;
const CONSTRAINT_HOVER_MARKER_RADIUS: f32 = 7.0;
fn constraint_glyph_size(label: &str) -> Size {
let w = label.chars().count() as f32 * CONSTRAINT_GLYPH_SIZE * 0.62
@@ -35,6 +36,22 @@ fn constraint_glyph_size(label: &str) -> Size {
Size::new(w, h)
}
fn draw_tangent_constraint_glyph(
frame: &mut canvas::Frame,
center: Point,
color: Color,
) {
let radius = 4.5;
let circle_center = Point::new(center.x - 0.3, center.y + 2.75);
let diagonal = radius * std::f32::consts::FRAC_1_SQRT_2;
let contact = Point::new(circle_center.x - diagonal, circle_center.y - diagonal);
let tangent_end = Point::new(contact.x + 8.0, contact.y - 8.0);
let stroke = canvas::Stroke::default().with_color(color).with_width(1.35);
frame.stroke(&canvas::Path::circle(circle_center, radius), stroke.clone());
frame.stroke(&canvas::Path::line(contact, tangent_end), stroke);
}
fn constraint_glyph_box(
anchor: Point,
outward: [f32; 2],
@@ -749,7 +766,7 @@ pub fn selection_overlay<'a>(
crosshair_bg: [f32; 4],
crosshair: CrosshairOptions,
selection_visual: SelectionVisualOptions,
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool)>,
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool, Vec<Point>)>,
) -> Element<'a, Message> {
canvas(SelectionCanvas {
selection,
@@ -833,9 +850,9 @@ struct SelectionCanvas {
crosshair: CrosshairOptions,
selection_visual: SelectionVisualOptions,
/// Constraint glyph anchor, outward screen direction, label, conflict
/// state, and whether the pill itself is the current click-to-select
/// target (`Scene::selected_constraint`) — for the current scope.
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool)>,
/// state, whether the pill itself is the current click-to-select target,
/// and the points to mark while the pill is hovered.
constraint_glyphs: Vec<(Point, [f32; 2], String, bool, bool, Vec<Point>)>,
}
fn draw_grip_marker(
@@ -1021,7 +1038,7 @@ impl canvas::Program<Message> for SelectionCanvas {
let glyphs: Vec<_> = self
.constraint_glyphs
.iter()
.map(|(anchor, outward, label, conflict, _)| {
.map(|(anchor, outward, label, conflict, _, _)| {
(*anchor, *outward, label.clone(), *conflict)
})
.collect();
@@ -1747,15 +1764,18 @@ impl canvas::Program<Message> for SelectionCanvas {
if !self.constraint_glyphs.is_empty() {
// `constraint_glyph_offsets` only needs the anchor/outward/label/
// conflict quadruple it was written against; project away the
// trailing `is_selected` flag rather than widen its signature.
// trailing display fields rather than widen its signature.
let glyphs_for_offsets: Vec<(Point, [f32; 2], String, bool)> = self
.constraint_glyphs
.iter()
.map(|(anchor, outward, label, is_conflicting, _)| {
.map(|(anchor, outward, label, is_conflicting, _, _)| {
(*anchor, *outward, label.clone(), *is_conflicting)
})
.collect();
let offsets = constraint_glyph_offsets(&glyphs_for_offsets);
let hovered = cursor
.position_in(bounds)
.and_then(|point| constraint_glyph_hit_test(&glyphs_for_offsets, point));
let normal_bg = theme.palette().primary.base.color;
let normal_fg = theme.palette().primary.base.text;
// A redundant or conflicting constraint gets the danger palette
@@ -1765,7 +1785,7 @@ impl canvas::Program<Message> for SelectionCanvas {
let conflict_bg = theme.palette().danger.base.color;
let conflict_fg = theme.palette().danger.base.text;
let selected_ring = theme.palette().primary.strong.color;
for ((anchor, outward, label, is_conflicting, is_selected), tangent_offset) in
for ((anchor, outward, label, is_conflicting, is_selected, _), tangent_offset) in
self.constraint_glyphs.iter().zip(offsets)
{
if !anchor.x.is_finite() || !anchor.y.is_finite() {
@@ -1786,19 +1806,52 @@ impl canvas::Program<Message> for SelectionCanvas {
canvas::Stroke::default().with_color(selected_ring).with_width(2.0),
);
}
frame.fill_text(canvas::Text {
content: label.clone(),
position: Point::new(
top_left.x + size.width * 0.5,
top_left.y + size.height * 0.5,
),
color: fg,
size: iced::Pixels(CONSTRAINT_GLYPH_SIZE),
align_x: iced::alignment::Horizontal::Center.into(),
align_y: iced::alignment::Vertical::Center,
shaping: iced::advanced::text::Shaping::Advanced,
..Default::default()
});
let glyph_center = Point::new(
top_left.x + size.width * 0.5,
top_left.y + size.height * 0.5,
);
if label == "T" {
draw_tangent_constraint_glyph(&mut frame, glyph_center, fg);
} else {
frame.fill_text(canvas::Text {
content: label.clone(),
position: glyph_center,
color: fg,
size: iced::Pixels(CONSTRAINT_GLYPH_SIZE),
align_x: iced::alignment::Horizontal::Center.into(),
align_y: iced::alignment::Vertical::Center,
shaping: iced::advanced::text::Shaping::Advanced,
..Default::default()
});
}
}
if let Some(index) = hovered {
let red = Color::from_rgb(1.0, 0.0, 0.0);
let stroke = canvas::Stroke::default().with_color(red).with_width(1.5);
for point in &self.constraint_glyphs[index].5 {
let first = canvas::Path::line(
Point::new(
point.x - CONSTRAINT_HOVER_MARKER_RADIUS,
point.y - CONSTRAINT_HOVER_MARKER_RADIUS,
),
Point::new(
point.x + CONSTRAINT_HOVER_MARKER_RADIUS,
point.y + CONSTRAINT_HOVER_MARKER_RADIUS,
),
);
let second = canvas::Path::line(
Point::new(
point.x - CONSTRAINT_HOVER_MARKER_RADIUS,
point.y + CONSTRAINT_HOVER_MARKER_RADIUS,
),
Point::new(
point.x + CONSTRAINT_HOVER_MARKER_RADIUS,
point.y - CONSTRAINT_HOVER_MARKER_RADIUS,
),
);
frame.stroke(&first, stroke.clone());
frame.stroke(&second, stroke.clone());
}
}
}
// Small cross at each acquired tracking point.