Skip to content

Commit 7127e4b

Browse files
committed
Optimize grid rounding and add debug output
1 parent c0e0d1c commit 7127e4b

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

libraries/path-bool/src/path/intersection_path_segment.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::io::Write;
2+
13
use crate::aabb::{bounding_box_max_extent, bounding_boxes_overlap, Aabb};
24
use crate::epsilons::Epsilons;
35
use crate::line_segment::{line_segment_intersection, line_segments_intersect};
@@ -113,7 +115,15 @@ pub fn path_segment_intersection(seg0: &PathSegment, seg1: &PathSegment, endpoin
113115
};
114116

115117
let intersections = path1.cubic_intersections_t(&path2);
118+
let mut file = std::fs::File::options().append(true).create(true).open("/tmp/intersections").unwrap();
119+
let format_curve = |seg0: &PathSegment| seg0.to_cubic().map(|point| format!("{} {}", point.x, point.y)).join(" ");
116120
let intersections: Vec<_> = intersections.into_iter().map(|(s, t)| [s, t]).collect();
121+
let intersections_fmt: Vec<_> = intersections.iter().map(|[s, t]| format!("{},{}", s, t)).collect();
122+
if !intersections.is_empty() {
123+
writeln!(&mut file, "{}\n{}\n{}\n", format_curve(seg0), format_curve(seg1), intersections_fmt.join(" "),).unwrap();
124+
file.flush().unwrap();
125+
}
126+
// let intersections: Vec<_> = intersections.into_iter().map(|(s, t)| [s, t]).collect();
117127
return intersections;
118128
}
119129
(PathSegment::Cubic(_, _, _, _), PathSegment::Line(_, _)) => {
@@ -125,6 +135,7 @@ pub fn path_segment_intersection(seg0: &PathSegment, seg1: &PathSegment, endpoin
125135
_ => (),
126136
};
127137

138+
// Fallback for quadratics and arc segments
128139
// https://math.stackexchange.com/questions/20321/how-can-i-tell-when-two-cubic-b%C3%A9zier-curves-intersect
129140

130141
let mut pairs = vec![(

libraries/path-bool/src/util/grid.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use rustc_hash::FxHashMap;
44
use smallvec::SmallVec;
55

66
pub(crate) struct Grid {
7-
cell_size: f64,
7+
cell_factor: f64,
88
cells: FxHashMap<IVec2, SmallVec<[usize; 6]>>,
99
}
1010

1111
impl Grid {
1212
pub(crate) fn new(cell_size: f64, edges: usize) -> Self {
1313
Grid {
14-
cell_size,
14+
cell_factor: cell_size.recip(),
1515
cells: FxHashMap::with_capacity_and_hasher(edges, Default::default()),
1616
}
1717
}
@@ -45,7 +45,7 @@ impl Grid {
4545
}
4646

4747
fn point_to_cell(&self, point: DVec2) -> IVec2 {
48-
(point / self.cell_size).as_ivec2()
48+
(point * self.cell_factor).as_ivec2()
4949
}
5050
}
5151

0 commit comments

Comments
 (0)