forked from 1b-agent/opencadstudio-upstream
style: apply cargo fmt across workspace
This commit is contained in:
1 parent
85710e5454
commit
9cf9ff2c5c
531 files changed
+57740
-33906
No files matched your search
+98
-128
@@ -17,11 +17,7 @@ pub struct BoundarySource {
|
||||
/// Boundary welding tolerance for tessellated wires.
|
||||
const WELD_TOLERANCE: f64 = 1.0e-6;
|
||||
|
||||
fn wire_segments_on_plane(
|
||||
wire: &WireModel,
|
||||
plane: WorkingPlane,
|
||||
tolerance: f64,
|
||||
) -> Vec<Line> {
|
||||
fn wire_segments_on_plane(wire: &WireModel, plane: WorkingPlane, tolerance: f64) -> Vec<Line> {
|
||||
let mut segments = Vec::new();
|
||||
let mut previous: Option<glam::DVec3> = None;
|
||||
for (index, high) in wire.points.iter().copied().enumerate() {
|
||||
@@ -54,11 +50,7 @@ fn wire_segments_on_plane(
|
||||
segments
|
||||
}
|
||||
|
||||
fn entity_curves_on_plane(
|
||||
entity: &EntityType,
|
||||
plane: WorkingPlane,
|
||||
tolerance: f64,
|
||||
) -> Vec<Curve> {
|
||||
fn entity_curves_on_plane(entity: &EntityType, plane: WorkingPlane, tolerance: f64) -> Vec<Curve> {
|
||||
let Some(planar) = crate::entities::curve::entity_curve(entity) else {
|
||||
return Vec::new();
|
||||
};
|
||||
@@ -137,47 +129,30 @@ fn live_segments_for_curves(
|
||||
) -> Vec<Line> {
|
||||
curves
|
||||
.iter()
|
||||
.flat_map(|curve| {
|
||||
match curve {
|
||||
Curve::Ray(ray) => clip_bounds
|
||||
.and_then(|(min, max)| {
|
||||
clip_unbounded_to_bounds(
|
||||
ray.origin,
|
||||
ray.direction,
|
||||
true,
|
||||
min,
|
||||
max,
|
||||
tolerance,
|
||||
)
|
||||
})
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>(),
|
||||
Curve::XLine(line) => clip_bounds
|
||||
.and_then(|(min, max)| {
|
||||
clip_unbounded_to_bounds(
|
||||
line.base,
|
||||
line.direction,
|
||||
false,
|
||||
min,
|
||||
max,
|
||||
tolerance,
|
||||
)
|
||||
})
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>(),
|
||||
_ => curve
|
||||
.tessellate_angle(kernel::tessellation::DEFAULT_ANGLE)
|
||||
.windows(2)
|
||||
.filter_map(|pair| {
|
||||
let start = pair[0];
|
||||
let end = pair[1];
|
||||
.flat_map(|curve| match curve {
|
||||
Curve::Ray(ray) => clip_bounds
|
||||
.and_then(|(min, max)| {
|
||||
clip_unbounded_to_bounds(ray.origin, ray.direction, true, min, max, tolerance)
|
||||
})
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>(),
|
||||
Curve::XLine(line) => clip_bounds
|
||||
.and_then(|(min, max)| {
|
||||
clip_unbounded_to_bounds(line.base, line.direction, false, min, max, tolerance)
|
||||
})
|
||||
.into_iter()
|
||||
.collect::<Vec<_>>(),
|
||||
_ => curve
|
||||
.tessellate_angle(kernel::tessellation::DEFAULT_ANGLE)
|
||||
.windows(2)
|
||||
.filter_map(|pair| {
|
||||
let start = pair[0];
|
||||
let end = pair[1];
|
||||
|
||||
(start.iter().chain(&end).all(|value| value.is_finite())
|
||||
&& start != end)
|
||||
.then_some(Line { start, end })
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
}
|
||||
(start.iter().chain(&end).all(|value| value.is_finite()) && start != end)
|
||||
.then_some(Line { start, end })
|
||||
})
|
||||
.collect::<Vec<_>>(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -195,10 +170,7 @@ fn hatch_path_seed(path: &codec::entities::BoundaryPath) -> Option<[f64; 2]> {
|
||||
let (points, triangles) = triangulate(&ring, &[]);
|
||||
if let Some(triangle) = triangles.first() {
|
||||
let [a, b, c] = triangle.map(|vertex| points[vertex]);
|
||||
Some([
|
||||
(a[0] + b[0] + c[0]) / 3.0,
|
||||
(a[1] + b[1] + c[1]) / 3.0,
|
||||
])
|
||||
Some([(a[0] + b[0] + c[0]) / 3.0, (a[1] + b[1] + c[1]) / 3.0])
|
||||
} else {
|
||||
Some([
|
||||
ring.iter().map(|point| point[0]).sum::<f64>() / ring.len() as f64,
|
||||
@@ -310,8 +282,8 @@ fn curve_forward(curve: &Curve, start: [f64; 2], next: [f64; 2]) -> bool {
|
||||
}
|
||||
|
||||
fn stored_arc_angles(start: f64, end: f64, counter_clockwise: bool, whole: bool) -> (f64, f64) {
|
||||
let stored_start = if counter_clockwise { start } else { -start }
|
||||
.rem_euclid(std::f64::consts::TAU);
|
||||
let stored_start =
|
||||
if counter_clockwise { start } else { -start }.rem_euclid(std::f64::consts::TAU);
|
||||
let sweep = if whole {
|
||||
std::f64::consts::TAU
|
||||
} else if counter_clockwise {
|
||||
@@ -329,9 +301,7 @@ fn exact_boundary_edge(
|
||||
next: [f64; 2],
|
||||
whole_curve: bool,
|
||||
) -> codec::entities::BoundaryEdge {
|
||||
use codec::entities::{
|
||||
BoundaryEdge, CircularArcEdge, EllipticArcEdge, LineEdge, SplineEdge,
|
||||
};
|
||||
use codec::entities::{BoundaryEdge, CircularArcEdge, EllipticArcEdge, LineEdge, SplineEdge};
|
||||
use codec::types::{Vector2, Vector3};
|
||||
|
||||
let Some(curve) = curve else {
|
||||
@@ -392,7 +362,11 @@ fn exact_boundary_edge(
|
||||
}
|
||||
Curve::Nurbs(source) => {
|
||||
let trimmed = if whole_curve {
|
||||
Some(if forward { source.clone() } else { source.reversed() })
|
||||
Some(if forward {
|
||||
source.clone()
|
||||
} else {
|
||||
source.reversed()
|
||||
})
|
||||
} else {
|
||||
source.trimmed(source.parameter_at(start), source.parameter_at(end))
|
||||
};
|
||||
@@ -421,12 +395,10 @@ fn exact_boundary_edge(
|
||||
end_tangent: Vector2::new(0.0, 0.0),
|
||||
})
|
||||
}
|
||||
Curve::Polyline(_) | Curve::Ray(_) | Curve::XLine(_) => {
|
||||
BoundaryEdge::Line(LineEdge {
|
||||
start: Vector2::new(start[0], start[1]),
|
||||
end: Vector2::new(end[0], end[1]),
|
||||
})
|
||||
}
|
||||
Curve::Polyline(_) | Curve::Ray(_) | Curve::XLine(_) => BoundaryEdge::Line(LineEdge {
|
||||
start: Vector2::new(start[0], start[1]),
|
||||
end: Vector2::new(end[0], end[1]),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -443,7 +415,7 @@ pub(crate) fn exact_hatch_paths(
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(ring_index, ring)| {
|
||||
let (points, curves) = refined_boundary_ring(ring, sources, tolerance);
|
||||
let (points, curves) = refined_boundary_ring(ring, sources, tolerance);
|
||||
let count = points.len();
|
||||
if count < 3 {
|
||||
return None;
|
||||
@@ -458,17 +430,13 @@ pub(crate) fn exact_hatch_paths(
|
||||
}
|
||||
let mut path = BoundaryPath::with_flags(BoundaryPathFlags::from_bits(bits));
|
||||
|
||||
let all_same = curves.first().is_some_and(|first| {
|
||||
first.is_some() && curves.iter().all(|curve| curve == first)
|
||||
});
|
||||
let all_same = curves
|
||||
.first()
|
||||
.is_some_and(|first| first.is_some() && curves.iter().all(|curve| curve == first));
|
||||
if all_same {
|
||||
let curve = curves[0].as_ref();
|
||||
path.add_edge(exact_boundary_edge(
|
||||
curve,
|
||||
points[0],
|
||||
points[0],
|
||||
points[1],
|
||||
true,
|
||||
curve, points[0], points[0], points[1], true,
|
||||
));
|
||||
} else {
|
||||
let start_index = (0..count)
|
||||
@@ -514,9 +482,7 @@ pub(crate) fn boundary_entities(
|
||||
.filter_map(|ring| {
|
||||
let mut points = Vec::with_capacity(ring.len());
|
||||
for &point in ring {
|
||||
if point.iter().all(|value| value.is_finite())
|
||||
&& points.last() != Some(&point)
|
||||
{
|
||||
if point.iter().all(|value| value.is_finite()) && points.last() != Some(&point) {
|
||||
points.push(point);
|
||||
}
|
||||
}
|
||||
@@ -530,9 +496,7 @@ pub(crate) fn boundary_entities(
|
||||
polyline.is_closed = true;
|
||||
polyline.vertices = points
|
||||
.into_iter()
|
||||
.map(|[x, y]| {
|
||||
codec::entities::LwVertex::new(codec::types::Vector2::new(x, y))
|
||||
})
|
||||
.map(|[x, y]| codec::entities::LwVertex::new(codec::types::Vector2::new(x, y)))
|
||||
.collect();
|
||||
Some(plane.place_entity(codec::EntityType::LwPolyline(polyline)))
|
||||
})
|
||||
@@ -566,17 +530,15 @@ fn edge_curve(
|
||||
.values()
|
||||
.flat_map(|source| source.curves.iter())
|
||||
.filter(|curve| {
|
||||
distance_to(curve, edge.start).min(distance_to(curve, edge.end))
|
||||
<= tolerance * 4.0
|
||||
distance_to(curve, edge.start).min(distance_to(curve, edge.end)) <= tolerance * 4.0
|
||||
})
|
||||
.collect(),
|
||||
};
|
||||
candidates
|
||||
.into_iter()
|
||||
.min_by(|left, right| {
|
||||
let error = |curve: &Curve| {
|
||||
distance_to(curve, edge.start) + distance_to(curve, edge.end)
|
||||
};
|
||||
let error =
|
||||
|curve: &Curve| distance_to(curve, edge.start) + distance_to(curve, edge.end);
|
||||
error(left).total_cmp(&error(right))
|
||||
})
|
||||
.cloned()
|
||||
@@ -586,9 +548,8 @@ fn nearest_crossing(a: &Curve, b: &Curve, point: [f64; 2], tolerance: f64) -> Op
|
||||
intersect(a, b, Tolerance::new(tolerance))
|
||||
.into_iter()
|
||||
.min_by(|left, right| {
|
||||
let distance = |candidate: [f64; 2]| {
|
||||
(candidate[0] - point[0]).hypot(candidate[1] - point[1])
|
||||
};
|
||||
let distance =
|
||||
|candidate: [f64; 2]| (candidate[0] - point[0]).hypot(candidate[1] - point[1]);
|
||||
distance(left.point).total_cmp(&distance(right.point))
|
||||
})
|
||||
.map(|crossing| crossing.point)
|
||||
@@ -636,9 +597,7 @@ fn refined_boundary_ring(
|
||||
}
|
||||
|
||||
fn normalized_delta(from: f64, to: f64) -> f64 {
|
||||
(to - from + std::f64::consts::PI)
|
||||
.rem_euclid(std::f64::consts::TAU)
|
||||
- std::f64::consts::PI
|
||||
(to - from + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU) - std::f64::consts::PI
|
||||
}
|
||||
|
||||
fn curve_edge_bulge(curve: &Curve, start: [f64; 2], end: [f64; 2]) -> f64 {
|
||||
@@ -674,15 +633,12 @@ fn boundary_polyline(
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, point)| {
|
||||
let mut vertex = codec::entities::LwVertex::new(codec::types::Vector2::new(
|
||||
point[0], point[1],
|
||||
));
|
||||
let mut vertex =
|
||||
codec::entities::LwVertex::new(codec::types::Vector2::new(point[0], point[1]));
|
||||
vertex.bulge = edge_curves
|
||||
.get(index)
|
||||
.and_then(|curve| curve.as_ref())
|
||||
.map(|curve| {
|
||||
curve_edge_bulge(curve, *point, points[(index + 1) % points.len()])
|
||||
})
|
||||
.map(|curve| curve_edge_bulge(curve, *point, points[(index + 1) % points.len()]))
|
||||
.unwrap_or(0.0);
|
||||
vertex
|
||||
})
|
||||
@@ -738,7 +694,11 @@ pub(crate) fn remap_hatch_associations(
|
||||
continue;
|
||||
}
|
||||
for path in &mut hatch.paths {
|
||||
if path.boundary_handles.iter().all(|source| handle_map.contains_key(source)) {
|
||||
if path
|
||||
.boundary_handles
|
||||
.iter()
|
||||
.all(|source| handle_map.contains_key(source))
|
||||
{
|
||||
for source in &mut path.boundary_handles {
|
||||
*source = handle_map[source];
|
||||
}
|
||||
@@ -747,17 +707,26 @@ pub(crate) fn remap_hatch_associations(
|
||||
path.flags.set_external(false);
|
||||
}
|
||||
}
|
||||
hatch.is_associative = hatch.paths.iter().any(|path| !path.boundary_handles.is_empty());
|
||||
hatch.is_associative = hatch
|
||||
.paths
|
||||
.iter()
|
||||
.any(|path| !path.boundary_handles.is_empty());
|
||||
touched = true;
|
||||
}
|
||||
touched
|
||||
}
|
||||
|
||||
impl Scene {
|
||||
pub(crate) fn replace_hatch_association(&mut self,handle:Handle,paths:Vec<codec::entities::BoundaryPath>) {
|
||||
let Some(EntityType::Hatch(hatch))=self.document.get_entity_mut(handle) else{return;};
|
||||
hatch.paths=paths;
|
||||
hatch.is_associative=true;
|
||||
pub(crate) fn replace_hatch_association(
|
||||
&mut self,
|
||||
handle: Handle,
|
||||
paths: Vec<codec::entities::BoundaryPath>,
|
||||
) {
|
||||
let Some(EntityType::Hatch(hatch)) = self.document.get_entity_mut(handle) else {
|
||||
return;
|
||||
};
|
||||
hatch.paths = paths;
|
||||
hatch.is_associative = true;
|
||||
self.associative_hatch_source_cache.borrow_mut().take();
|
||||
self.refresh_fill_model(handle);
|
||||
}
|
||||
@@ -791,7 +760,8 @@ impl Scene {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
path.boundary_handles.retain(|handle| !handles.contains(handle));
|
||||
path.boundary_handles
|
||||
.retain(|handle| !handles.contains(handle));
|
||||
}
|
||||
if path.boundary_handles.is_empty() {
|
||||
path.flags.set_external(false);
|
||||
@@ -858,10 +828,7 @@ impl Scene {
|
||||
}
|
||||
}
|
||||
|
||||
fn associative_hatch_dependents(
|
||||
&self,
|
||||
changed: &rustc_hash::FxHashSet<Handle>,
|
||||
) -> Vec<Handle> {
|
||||
fn associative_hatch_dependents(&self, changed: &rustc_hash::FxHashSet<Handle>) -> Vec<Handle> {
|
||||
if self.associative_hatch_source_cache.borrow().is_none() {
|
||||
let mut index: HashMap<Handle, Vec<Handle>> = HashMap::default();
|
||||
for entity in self.document.entities() {
|
||||
@@ -902,8 +869,7 @@ impl Scene {
|
||||
if changes.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
let changed: rustc_hash::FxHashSet<_> =
|
||||
changes.iter().map(|(handle, _)| *handle).collect();
|
||||
let changed: rustc_hash::FxHashSet<_> = changes.iter().map(|(handle, _)| *handle).collect();
|
||||
let candidates: Vec<_> = self
|
||||
.associative_hatch_dependents(&changed)
|
||||
.into_iter()
|
||||
@@ -1144,9 +1110,7 @@ fn clip_unbounded_to_bounds(
|
||||
(length > tolerance).then_some(Line { start, end })
|
||||
}
|
||||
|
||||
fn hatch_path_geometry(
|
||||
path: &codec::entities::BoundaryPath,
|
||||
) -> (Vec<[f64; 2]>, Vec<f64>) {
|
||||
fn hatch_path_geometry(path: &codec::entities::BoundaryPath) -> (Vec<[f64; 2]>, Vec<f64>) {
|
||||
let edges: Vec<_> = path
|
||||
.edges
|
||||
.iter()
|
||||
@@ -1235,17 +1199,11 @@ pub(crate) fn boundary_faces(
|
||||
mod boundary_xline_tests {
|
||||
use super::*;
|
||||
|
||||
fn rect_edge(
|
||||
scene: &mut Scene,
|
||||
start: [f64; 3],
|
||||
end: [f64; 3],
|
||||
) -> codec::Handle {
|
||||
scene.add_entity(EntityType::Line(
|
||||
codec::entities::Line::from_points(
|
||||
codec::types::Vector3::new(start[0], start[1], start[2]),
|
||||
codec::types::Vector3::new(end[0], end[1], end[2]),
|
||||
),
|
||||
))
|
||||
fn rect_edge(scene: &mut Scene, start: [f64; 3], end: [f64; 3]) -> codec::Handle {
|
||||
scene.add_entity(EntityType::Line(codec::entities::Line::from_points(
|
||||
codec::types::Vector3::new(start[0], start[1], start[2]),
|
||||
codec::types::Vector3::new(end[0], end[1], end[2]),
|
||||
)))
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -1309,10 +1267,22 @@ mod boundary_xline_tests {
|
||||
.expect("xline crossing the box must produce a source");
|
||||
assert_eq!(source.segments.len(), 1);
|
||||
let segment = &source.segments[0];
|
||||
assert!((segment.start[0] - 5.0).abs() < 1e-6, "got {:?}", segment.start);
|
||||
assert!((segment.start[1] - 0.0).abs() < 1e-6, "got {:?}", segment.start);
|
||||
assert!(
|
||||
(segment.start[0] - 5.0).abs() < 1e-6,
|
||||
"got {:?}",
|
||||
segment.start
|
||||
);
|
||||
assert!(
|
||||
(segment.start[1] - 0.0).abs() < 1e-6,
|
||||
"got {:?}",
|
||||
segment.start
|
||||
);
|
||||
assert!((segment.end[0] - 5.0).abs() < 1e-6, "got {:?}", segment.end);
|
||||
assert!((segment.end[1] - 10.0).abs() < 1e-6, "got {:?}", segment.end);
|
||||
assert!(
|
||||
(segment.end[1] - 10.0).abs() < 1e-6,
|
||||
"got {:?}",
|
||||
segment.end
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user