forked from 1b-agent/opencadstudio-upstream
Merge current main with division marker support
This commit is contained in:
commit
f076db6d53
160 files changed
+16859
-16176
No files matched your search
+19
-39
@@ -182,18 +182,14 @@ fn boundary_centroid(h: &Hatch) -> Option<(f64, f64)> {
|
||||
(n > 0.0).then(|| (sx / n, sy / n))
|
||||
}
|
||||
|
||||
/// Scale the stored, final hatch-pattern geometry around the pattern origin.
|
||||
/// Scale a catalog pattern about its intrinsic coordinate origin.
|
||||
/// Pattern lines loaded from DXF/DWG are rendered prebaked, so their metadata
|
||||
/// scale is not applied again by the renderer.
|
||||
pub(crate) fn scale_pattern_geometry(
|
||||
pattern: &mut acadrust::entities::HatchPattern,
|
||||
factor: f64,
|
||||
) {
|
||||
let (origin_x, origin_y) = pattern
|
||||
.lines
|
||||
.first()
|
||||
.map(|line| (line.base_point.x, line.base_point.y))
|
||||
.unwrap_or((0.0, 0.0));
|
||||
let (origin_x, origin_y) = (0.0, 0.0);
|
||||
for line in pattern.lines.iter_mut() {
|
||||
line.base_point.x = origin_x + (line.base_point.x - origin_x) * factor;
|
||||
line.base_point.y = origin_y + (line.base_point.y - origin_y) * factor;
|
||||
@@ -205,18 +201,14 @@ pub(crate) fn scale_pattern_geometry(
|
||||
}
|
||||
}
|
||||
|
||||
/// Rotate the stored, final hatch-pattern geometry around the pattern origin.
|
||||
/// Rotate a catalog pattern about its intrinsic coordinate origin.
|
||||
/// This keeps the line angle, base point and world-space offset in sync.
|
||||
pub(crate) fn rotate_pattern_geometry(
|
||||
pattern: &mut acadrust::entities::HatchPattern,
|
||||
angle: f64,
|
||||
) {
|
||||
let (sin, cos) = angle.sin_cos();
|
||||
let (origin_x, origin_y) = pattern
|
||||
.lines
|
||||
.first()
|
||||
.map(|line| (line.base_point.x, line.base_point.y))
|
||||
.unwrap_or((0.0, 0.0));
|
||||
let (origin_x, origin_y) = (0.0, 0.0);
|
||||
for line in pattern.lines.iter_mut() {
|
||||
line.angle += angle;
|
||||
let (x, y) = (
|
||||
@@ -613,7 +605,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
|
||||
_ => h.pattern_type,
|
||||
};
|
||||
if requested != h.pattern_type {
|
||||
let old_origin = h.pattern.lines.first().map(|line| line.base_point);
|
||||
let old_origin = h.pattern_origin();
|
||||
h.pattern_type = requested;
|
||||
h.is_solid = false;
|
||||
match requested {
|
||||
@@ -629,15 +621,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
|
||||
crate::scene::model::hatch_patterns::build_dxf_pattern(entry);
|
||||
scale_pattern_geometry(&mut pattern, h.pattern_scale);
|
||||
rotate_pattern_geometry(&mut pattern, h.pattern_angle);
|
||||
if let (Some(old), Some(new)) =
|
||||
(old_origin, pattern.lines.first().map(|line| line.base_point))
|
||||
{
|
||||
translate_pattern_geometry(
|
||||
&mut pattern,
|
||||
old.x - new.x,
|
||||
old.y - new.y,
|
||||
);
|
||||
}
|
||||
translate_pattern_geometry(&mut pattern, old_origin.x, old_origin.y);
|
||||
h.pattern = pattern;
|
||||
}
|
||||
}
|
||||
@@ -695,7 +679,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
|
||||
"pattern_angle" => {
|
||||
let angle = v.to_radians();
|
||||
let delta = angle - h.pattern_angle;
|
||||
rotate_pattern_geometry(&mut h.pattern, delta);
|
||||
h.rotate_pattern_about_origin(delta);
|
||||
h.pattern_angle = angle;
|
||||
}
|
||||
// The stored pattern lines are the FINAL rendered geometry (offsets,
|
||||
@@ -706,7 +690,7 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
|
||||
let old = h.pattern_scale;
|
||||
if old > 1e-12 {
|
||||
let k = v / old;
|
||||
scale_pattern_geometry(&mut h.pattern, k);
|
||||
h.scale_pattern_about_origin(k);
|
||||
}
|
||||
h.pattern_scale = v;
|
||||
}
|
||||
@@ -724,19 +708,17 @@ fn apply_geom_prop(h: &mut Hatch, field: &str, value: &str) {
|
||||
}
|
||||
// Origin rows are relative offsets and return to zero after commit.
|
||||
"origin_x" => {
|
||||
for line in h.pattern.lines.iter_mut() {
|
||||
line.base_point.x += v;
|
||||
}
|
||||
let origin = h.pattern_origin();
|
||||
h.set_pattern_origin(acadrust::types::Vector2::new(origin.x + v, origin.y));
|
||||
}
|
||||
"origin_y" => {
|
||||
for line in h.pattern.lines.iter_mut() {
|
||||
line.base_point.y += v;
|
||||
}
|
||||
let origin = h.pattern_origin();
|
||||
h.set_pattern_origin(acadrust::types::Vector2::new(origin.x, origin.y + v));
|
||||
}
|
||||
"iso_pen_width" if v > 0.0 => {
|
||||
let old = h.pattern_scale;
|
||||
if old > 1e-12 {
|
||||
scale_pattern_geometry(&mut h.pattern, v / old);
|
||||
h.scale_pattern_about_origin(v / old);
|
||||
}
|
||||
h.pattern_scale = v;
|
||||
}
|
||||
@@ -876,10 +858,8 @@ impl Grippable for Hatch {
|
||||
GripApply::Absolute(point) => (point.x - gx, point.y - gy),
|
||||
GripApply::Translate(delta) => (delta.x, delta.y),
|
||||
};
|
||||
for line in self.pattern.lines.iter_mut() {
|
||||
line.base_point.x += dx;
|
||||
line.base_point.y += dy;
|
||||
}
|
||||
let origin = self.pattern_origin();
|
||||
self.set_pattern_origin(acadrust::types::Vector2::new(origin.x + dx, origin.y + dy));
|
||||
return;
|
||||
}
|
||||
id += 1;
|
||||
@@ -1005,11 +985,11 @@ impl Grippable for Hatch {
|
||||
fn apply_grip_menu(&mut self, grip_id: usize, action: crate::scene::model::object::GripMenuAction) {
|
||||
use crate::scene::model::object::GripMenuAction as A;
|
||||
if grip_id == 0 && matches!(action, A::OriginPoint) {
|
||||
if let (Some((gx, gy)), Some((ox, oy))) = (
|
||||
if let (Some((gx, gy)), Some((_ox, _oy))) = (
|
||||
boundary_centroid(self),
|
||||
self.pattern.lines.first().map(|line| (line.base_point.x, line.base_point.y)),
|
||||
) {
|
||||
translate_pattern_geometry(&mut self.pattern, gx - ox, gy - oy);
|
||||
self.set_pattern_origin(acadrust::types::Vector2::new(gx, gy));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1038,14 +1018,14 @@ impl Grippable for Hatch {
|
||||
A::HatchAngle => {
|
||||
let angle = value.to_radians();
|
||||
let delta = angle - self.pattern_angle;
|
||||
rotate_pattern_geometry(&mut self.pattern, delta);
|
||||
self.rotate_pattern_about_origin(delta);
|
||||
self.pattern_angle = angle;
|
||||
}
|
||||
A::HatchScale => {
|
||||
if value > 0.0 {
|
||||
if self.pattern_scale > 1e-12 {
|
||||
let factor = value / self.pattern_scale;
|
||||
scale_pattern_geometry(&mut self.pattern, factor);
|
||||
self.scale_pattern_about_origin(factor);
|
||||
}
|
||||
self.pattern_scale = value;
|
||||
}
|
||||
|
||||
+164
-17
@@ -53,9 +53,16 @@ fn spline_curve(spline: &Spline) -> Option<NurbsCurve3> {
|
||||
fn axis_frame(helix: &Helix) -> Option<(Vec3, Vec3, Vec3)> {
|
||||
let base = point(helix.axis_base_point);
|
||||
let axis = point(helix.axis_vector).normalize()?;
|
||||
let radial = point(helix.start_point) - base;
|
||||
let start_direction = (radial - axis * radial.dot(axis)).normalize()?;
|
||||
Some((base, axis, start_direction))
|
||||
let tangent = spline_curve(&helix.spline)
|
||||
.map(|curve| Vec3::from(curve.tangent_at(0.0)))
|
||||
.and_then(Vec3::normalize)
|
||||
.or_else(|| perpendicular(axis))?;
|
||||
let (axis, radial, _) = HelixCurve::frame_from_points(base.to_array(),point(helix.axis_vector).to_array(),point(helix.start_point).to_array(),tangent.to_array())?;
|
||||
Some((base,Vec3::from(axis),Vec3::from(radial)))
|
||||
}
|
||||
|
||||
fn base_radius(helix: &Helix) -> Option<f64> {
|
||||
radius_from_axis(helix, point(helix.start_point))
|
||||
}
|
||||
|
||||
fn radius_from_axis(helix: &Helix, value: Vec3) -> Option<f64> {
|
||||
@@ -65,6 +72,7 @@ fn radius_from_axis(helix: &Helix, value: Vec3) -> Option<f64> {
|
||||
}
|
||||
|
||||
fn top_radius(helix: &Helix) -> f64 {
|
||||
if helix.turns == 0.0 { return helix.radius; }
|
||||
spline_curve(&helix.spline)
|
||||
.map(|curve| Vec3::from(curve.point_at(1.0)))
|
||||
.and_then(|endpoint| radius_from_axis(helix, endpoint))
|
||||
@@ -77,7 +85,7 @@ fn kernel_curve(helix: &Helix, top_radius: f64) -> Option<HelixCurve> {
|
||||
base_center: base.to_array(),
|
||||
axis_direction: axis.to_array(),
|
||||
start_direction: start_direction.to_array(),
|
||||
base_radius: helix.radius,
|
||||
base_radius: base_radius(helix)?,
|
||||
top_radius,
|
||||
height: helix.turns * helix.turn_height,
|
||||
turns: helix.turns,
|
||||
@@ -89,6 +97,36 @@ fn kernel_curve(helix: &Helix, top_radius: f64) -> Option<HelixCurve> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Reverse generating parameters together with the exact stored spline.
|
||||
pub(crate) fn reversed(helix: &Helix) -> Option<Helix> {
|
||||
let original_curve = spline_curve(&helix.spline)?;
|
||||
let parameters = kernel_curve(helix, top_radius(helix))?.reversed()?;
|
||||
let mut result = helix.clone();
|
||||
result.axis_base_point = vector(Vec3::from(parameters.base_center));
|
||||
result.axis_vector = vector(Vec3::from(parameters.axis_direction));
|
||||
result.start_point = vector(Vec3::from(original_curve.point_at(1.0)));
|
||||
result.radius = parameters.top_radius;
|
||||
// Reverse the evaluated curve itself instead of fitting it again.
|
||||
let reversed_curve = original_curve.reversed()?;
|
||||
result.spline.control_points = reversed_curve
|
||||
.control_points()
|
||||
.iter()
|
||||
.map(|point| Vector3::new(point[0], point[1], point[2]))
|
||||
.collect();
|
||||
result.spline.fit_points.reverse();
|
||||
result.spline.weights = if helix.spline.weights.is_empty() {
|
||||
Vec::new()
|
||||
} else {
|
||||
reversed_curve.weights().to_vec()
|
||||
};
|
||||
result.spline.knots = reversed_curve.knots().to_vec();
|
||||
let begin = helix.spline.begin_tangent;
|
||||
let end = helix.spline.end_tangent;
|
||||
result.spline.begin_tangent = Vector3::new(-end.x, -end.y, -end.z);
|
||||
result.spline.end_tangent = Vector3::new(-begin.x, -begin.y, -begin.z);
|
||||
Some(result)
|
||||
}
|
||||
|
||||
fn projected_direction(direction: Vec3, axis: Vec3) -> Option<Vec3> {
|
||||
(direction - axis * direction.dot(axis)).normalize()
|
||||
}
|
||||
@@ -105,12 +143,18 @@ fn perpendicular(axis: Vec3) -> Option<Vec3> {
|
||||
}
|
||||
|
||||
fn rebuild(helix: &mut Helix, top_radius: f64) -> bool {
|
||||
let Some(curve) = kernel_curve(helix, top_radius) else {
|
||||
rebuild_with_radial(helix, top_radius, None)
|
||||
}
|
||||
|
||||
fn rebuild_with_radial(helix: &mut Helix, top_radius: f64, radial: Option<Vec3>) -> bool {
|
||||
let Some(mut curve) = kernel_curve(helix, top_radius) else {
|
||||
return false;
|
||||
};
|
||||
if let Some(radial) = radial { curve.start_direction = radial.to_array(); }
|
||||
let Some(nurbs) = curve.nurbs() else {
|
||||
return false;
|
||||
};
|
||||
helix.radius = top_radius;
|
||||
helix.spline.degree = nurbs.degree() as i32;
|
||||
helix.spline.knots = nurbs.knots().to_vec();
|
||||
helix.spline.control_points = nurbs
|
||||
@@ -127,7 +171,7 @@ fn rebuild(helix: &mut Helix, top_radius: f64) -> bool {
|
||||
helix.spline.flags.closed = false;
|
||||
helix.spline.flags.periodic = false;
|
||||
helix.spline.flags.rational = nurbs.is_rational();
|
||||
helix.spline.flags.planar = false;
|
||||
helix.spline.flags.planar = curve.height == 0.0;
|
||||
helix.spline.flags.linear = false;
|
||||
true
|
||||
}
|
||||
@@ -194,7 +238,7 @@ fn properties(helix: &Helix) -> Vec<PropSection> {
|
||||
edit(t!("Height").as_ref(), "height", height),
|
||||
edit(t!("Turns").as_ref(), "turns", helix.turns),
|
||||
edit(t!("Turn height").as_ref(), "turn_height", helix.turn_height),
|
||||
edit(t!("Base radius").as_ref(), "base_radius", helix.radius),
|
||||
edit(t!("Base radius").as_ref(), "base_radius", base_radius(helix).unwrap_or(0.0)),
|
||||
edit(t!("Top radius").as_ref(), "top_radius", top_radius),
|
||||
choice(t!("Twist").as_ref(), "twist", &twist, &twist_options),
|
||||
ro(t!("Turn slope").as_ref(), "turn_slope", format_angle(turn_slope)),
|
||||
@@ -249,10 +293,15 @@ fn apply_geom_prop(helix: &mut Helix, field: &str, value: &str) {
|
||||
helix.axis_base_point = vector(base);
|
||||
helix.start_point = vector(point(helix.start_point) + delta);
|
||||
}
|
||||
"height" if number.abs() > EPSILON => {
|
||||
"height" => {
|
||||
if helix.turns == 0.0 && number != 0.0 {
|
||||
return;
|
||||
}
|
||||
if number < 0.0 { helix.axis_vector = vector(-point(helix.axis_vector)); }
|
||||
let number = number.abs();
|
||||
if helix.constraint == HelixConstraint::TurnHeight && helix.turn_height.abs() > EPSILON {
|
||||
let turns = number / helix.turn_height;
|
||||
if turns <= EPSILON {
|
||||
if turns < 0.0 {
|
||||
return;
|
||||
}
|
||||
helix.turns = turns;
|
||||
@@ -260,16 +309,18 @@ fn apply_geom_prop(helix: &mut Helix, field: &str, value: &str) {
|
||||
helix.turn_height = number / helix.turns.max(EPSILON);
|
||||
}
|
||||
}
|
||||
"turns" if number > EPSILON => {
|
||||
if helix.constraint == HelixConstraint::TurnHeight {
|
||||
"turns" if number >= 0.0 => {
|
||||
if number == 0.0 { helix.turns = 0.0; }
|
||||
else if helix.constraint == HelixConstraint::TurnHeight {
|
||||
helix.turns = number;
|
||||
} else {
|
||||
helix.turns = number;
|
||||
helix.turn_height = old_height / number;
|
||||
}
|
||||
}
|
||||
"turn_height" if number.abs() > EPSILON => {
|
||||
if helix.constraint == HelixConstraint::Turns {
|
||||
"turn_height" if number >= 0.0 => {
|
||||
if number == 0.0 { helix.turn_height = 0.0; }
|
||||
else if helix.constraint == HelixConstraint::Turns {
|
||||
helix.turn_height = number;
|
||||
} else {
|
||||
let turns = old_height / number;
|
||||
@@ -280,12 +331,13 @@ fn apply_geom_prop(helix: &mut Helix, field: &str, value: &str) {
|
||||
helix.turns = turns;
|
||||
}
|
||||
}
|
||||
"base_radius" if number > EPSILON => {
|
||||
"base_radius" if number >= 0.0 => {
|
||||
let Some((base, _, start_direction)) = axis_frame(helix) else {
|
||||
return;
|
||||
};
|
||||
helix.radius = number;
|
||||
helix.start_point = vector(base + start_direction * number);
|
||||
if !rebuild_with_radial(helix, top, Some(start_direction)) { *helix = original; }
|
||||
return;
|
||||
}
|
||||
"top_radius" if number >= 0.0 => {
|
||||
if !rebuild(helix, number) {
|
||||
@@ -303,6 +355,7 @@ fn apply_geom_prop(helix: &mut Helix, field: &str, value: &str) {
|
||||
fn apply_grip(helix: &mut Helix, grip_id: usize, apply: GripApply) {
|
||||
let original = helix.clone();
|
||||
let top = top_radius(helix);
|
||||
let Some(base_radius) = base_radius(helix) else { return; };
|
||||
let Some((base, axis, start_direction)) = axis_frame(helix) else {
|
||||
return;
|
||||
};
|
||||
@@ -334,7 +387,6 @@ fn apply_grip(helix: &mut Helix, grip_id: usize, apply: GripApply) {
|
||||
if radius <= EPSILON {
|
||||
return;
|
||||
}
|
||||
helix.radius = radius;
|
||||
helix.start_point = vector(base + radial);
|
||||
}
|
||||
(2, GripApply::Absolute(position)) => {
|
||||
@@ -362,7 +414,7 @@ fn apply_grip(helix: &mut Helix, grip_id: usize, apply: GripApply) {
|
||||
*helix = original;
|
||||
return;
|
||||
};
|
||||
helix.start_point = vector(base + start_direction * helix.radius);
|
||||
helix.start_point = vector(base + start_direction * base_radius);
|
||||
}
|
||||
(3, GripApply::Absolute(position)) => {
|
||||
let position = kernel(position);
|
||||
@@ -455,3 +507,98 @@ impl Transformable for Helix {
|
||||
apply_transform(self, transform);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn zero_base_point_helix() -> Helix {
|
||||
let curve = HelixCurve {
|
||||
base_center: [1.0, 2.0, 3.0],
|
||||
axis_direction: [0.0, 0.0, 1.0],
|
||||
start_direction: [1.0, 0.0, 0.0],
|
||||
base_radius: 0.0,
|
||||
top_radius: 2.0,
|
||||
height: 0.0,
|
||||
turns: 0.0,
|
||||
direction: HelixDirection::CounterClockwise,
|
||||
};
|
||||
let nurbs = curve.nurbs().unwrap();
|
||||
let mut helix = Helix::new();
|
||||
helix.axis_base_point = Vector3::new(1.0, 2.0, 3.0);
|
||||
helix.axis_vector = Vector3::UNIT_Z;
|
||||
helix.start_point = helix.axis_base_point;
|
||||
helix.radius = 2.0;
|
||||
helix.turns = 0.0;
|
||||
helix.turn_height = 0.0;
|
||||
helix.spline.degree = nurbs.degree() as i32;
|
||||
helix.spline.knots = nurbs.knots().to_vec();
|
||||
helix.spline.control_points = nurbs
|
||||
.control_points()
|
||||
.iter()
|
||||
.map(|point| Vector3::new(point[0], point[1], point[2]))
|
||||
.collect();
|
||||
helix
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_base_point_locus_retains_an_editable_frame() {
|
||||
let mut helix = zero_base_point_helix();
|
||||
assert_eq!(base_radius(&helix), Some(0.0));
|
||||
assert_eq!(top_radius(&helix), 2.0);
|
||||
|
||||
let original = helix.clone();
|
||||
apply_geom_prop(&mut helix, "height", "5");
|
||||
assert_eq!(helix, original);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reversing_twice_restores_a_conical_helix_and_radius_roles() {
|
||||
let curve = HelixCurve {
|
||||
base_center: [1.0, 2.0, 3.0],
|
||||
axis_direction: [0.0, 1.0, 1.0],
|
||||
start_direction: [1.0, 0.0, 0.0],
|
||||
base_radius: 1.0,
|
||||
top_radius: 2.5,
|
||||
height: 4.0,
|
||||
turns: 1.25,
|
||||
direction: HelixDirection::Clockwise,
|
||||
};
|
||||
let nurbs = curve.nurbs().expect("valid helix");
|
||||
let mut helix = Helix::new();
|
||||
helix.axis_base_point = Vector3::new(1.0, 2.0, 3.0);
|
||||
let axis = Vec3::from(curve.axis_direction).normalize().unwrap();
|
||||
helix.axis_vector = vector(axis);
|
||||
helix.start_point = Vector3::new(2.0, 2.0, 3.0);
|
||||
helix.radius = curve.top_radius;
|
||||
helix.turns = curve.turns;
|
||||
helix.turn_height = curve.height / curve.turns;
|
||||
helix.handedness = false;
|
||||
helix.spline.degree = nurbs.degree() as i32;
|
||||
helix.spline.control_points = nurbs
|
||||
.control_points()
|
||||
.iter()
|
||||
.map(|point| Vector3::new(point[0], point[1], point[2]))
|
||||
.collect();
|
||||
helix.spline.knots = nurbs.knots().to_vec();
|
||||
helix.spline.weights = nurbs.weights().to_vec();
|
||||
|
||||
let once = reversed(&helix).expect("first reversal");
|
||||
assert!((once.radius - curve.base_radius).abs() < 1.0e-12);
|
||||
assert_eq!(once.turns, helix.turns);
|
||||
assert_eq!(once.turn_height, helix.turn_height);
|
||||
assert_eq!(once.handedness, helix.handedness);
|
||||
let original_curve = spline_curve(&helix.spline).unwrap();
|
||||
let once_curve = spline_curve(&once.spline).unwrap();
|
||||
assert!(Vec3::from(once_curve.point_at(0.0))
|
||||
.distance(Vec3::from(original_curve.point_at(1.0))) < 1.0e-9);
|
||||
|
||||
let twice = reversed(&once).expect("second reversal");
|
||||
assert!((twice.radius - helix.radius).abs() < 1.0e-12);
|
||||
let twice_curve = spline_curve(&twice.spline).unwrap();
|
||||
for parameter in [0.0, 0.2, 0.5, 0.8, 1.0] {
|
||||
assert!(Vec3::from(twice_curve.point_at(parameter))
|
||||
.distance(Vec3::from(original_curve.point_at(parameter))) < 1.0e-8);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user