Skip to content

Commit 5c9d034

Browse files
committed
Refactor offset functions and examples: rename bench_offset_multiple to bench_offset_polyline_to_polyline, update README with new example, and add offset_arcline example. Clean up unused code and improve documentation clarity.
1 parent 6eed9a4 commit 5c9d034

9 files changed

Lines changed: 300 additions & 239 deletions

File tree

benches/bench_offset_multiple.rs

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33

44
use geom::prelude::*;
5-
use offroad::{offset::pline_01, offset_connect_raw, offset_polyline_raw, offset_prune_invalid, offset_raw::OffsetRaw, offset_split_arcs, poly_to_raws};
5+
use offroad::{offset::{offset_polyline_to_polyline, pline_01, OffsetCfg}};
66

77

88

@@ -11,49 +11,37 @@ fn main() {
1111
divan::main();
1212
}
1313

14-
fn offset_multiple_bench(poly_raws: &Vec<Vec<OffsetRaw>>, off: f64) {
15-
let offset_raw = offset_polyline_raw(&poly_raws, off);
16-
17-
let offset_connect = offset_connect_raw(&offset_raw, off);
18-
19-
let mut offset_split = offset_split_arcs(&offset_raw, &offset_connect);
20-
21-
let _offset_final = offset_prune_invalid(&poly_raws, &mut offset_split, off);
22-
}
2314

2415
#[divan::bench(args = [1.0, 2.0, 4.0, 8.0, 16.0, 32.0, 64.0, 128.0, 256.0],
2516
max_time = 1.0, // seconds
2617
sample_size = 200, // 64 × 84 = 5376
2718
)]
28-
fn bench_offset_multiple_complex_polyline(off: f64) {
29-
let mut plines: Vec<Vec<crate::PVertex>> = Vec::new();
19+
fn bench_offset_polyline_to_polyline(off: f64) {
20+
let mut cfg = OffsetCfg::default();
3021

3122
let mut p = pline_01()[0].clone();
3223
p = polyline_translate(&p, point(180.0, -60.0));
3324
p = polyline_scale(&p, 2.5);
34-
plines.push(p.clone());
35-
let poly_raws = poly_to_raws(&plines);
36-
offset_multiple_bench(&poly_raws, off);
25+
offset_polyline_to_polyline(&p, 16.0, &mut cfg);
3726

3827
let p2 = polyline_reverse(&p);
39-
let poly_raws = poly_to_raws(&vec![p2]);
40-
offset_multiple_bench(&poly_raws, off);
28+
offset_polyline_to_polyline(&p2, 16.0, &mut cfg);
4129
}
4230

4331
/*
4432
> cargo bench
4533
4634
Timer precision: 20 ns
47-
offset_multiple fastest │ slowest │ median │ mean │ samples │ iters
48-
╰─ bench_offset_multiple_complex_polyline │ │ │ │ │
49-
├─ 1 222.4 µs │ 233.6 µs │ 223.6 µs │ 224.5 µs │ 234600
50-
├─ 2 222.2 µs │ 229.8 µs │ 222.9 µs │ 223.3 µs │ 234600
51-
├─ 4 222 µs │ 233.6 µs │ 222.6 µs │ 223.4 µs │ 23 │ 4600
52-
├─ 8 222.9 µs │ 315.2 µs │ 225.7 µs │ 247.6 µs │ 21 │ 4200
53-
├─ 16 352.1 µs │ 381.9 µs │ 368.7 µs │ 369.8 µs │ 14 │ 2800
54-
├─ 32 393.8 µs │ 410.4 µs │ 403.3 µs │ 402.3 µs │ 13 │ 2600
55-
├─ 64 566.3 µs │ 634.4 µs │ 586 µs │ 591.9 µs │ 9 │ 1800
56-
├─ 128 542.7 µs │ 582.6 µs │ 571 µs │ 568 µs │ 9 │ 1800
57-
╰─ 256 650.9 µs │ 696.5 µs │ 673.3 µs │ 670.1 µs │ 81600
35+
bench_offset_multiple fastest │ slowest │ median │ mean │ samples │ iters
36+
╰─ bench_offset_polyline_to_polyline │ │ │ │ │
37+
├─ 1 373.1 µs │ 446.2 µs │ 375.3 µs │ 388.5 µs │ 132600
38+
├─ 2 480 µs │ 565.5 µs │ 534.3 µs │ 529 µs │ 102000
39+
├─ 4 545.9 µs │ 565 µs │ 562.8 µs │ 559.9 µs │ 9 │ 1800
40+
├─ 8 566.8 µs │ 628.3 µs │ 581.6 µs │ 584.4 µs │ 9 │ 1800
41+
├─ 16 674.7 µs │ 784.2 µs │ 715.3 µs │ 729.3 µs │ 7 │ 1400
42+
├─ 32 642.5 µs │ 917.4 µs │ 755.8 µs │ 775.4 µs │ 7 │ 1400
43+
├─ 64 568.9 µs │ 623.1 µs │ 589.6 µs │ 595 µs │ 9 │ 1800
44+
├─ 128 566.6 µs │ 585.9 µs │ 568.6 µs │ 571.5 µs │ 9 │ 1800
45+
╰─ 256 565 µs │ 585.2 µs │ 566.5 µs │ 570.5 µs │ 91800
5846
5947
*/

examples/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@
55
```
66
cargo run --example offset_polyline
77
```
8-
98
![](https://raw.githubusercontent.com/radevgit/offroad/refs/heads/main/examples/img/offset_polyline.svg "offset_polyline")
9+
```
10+
cargo run --example offset_arcline
11+
```
12+
13+
14+
15+

examples/offset_arcline.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use geom::prelude::*;
2+
use offroad::{offset::offset_arcline_to_arcline, prelude::OffsetCfg};
3+
4+
fn main() {
5+
// Configuration for offsetting
6+
let mut cfg = OffsetCfg::default();
7+
let mut svg = SVG::new(300.0, 300.0, "/tmp/arcline.svg");
8+
cfg.svg = Some(&mut svg);
9+
// Show original arcline in SVG output
10+
cfg.debug_orig = true;
11+
// Show final offset arclines in SVG output
12+
cfg.debug_final = true;
13+
14+
let arc0 = arc_circle_parametrization(point(0.0, 0.0), point(100.0, 100.0), 0.0);
15+
let arc1 = arc_circle_parametrization(point(100.0, 100.0), point(200.0, 0.0), 0.5);
16+
let arc2 = arc_circle_parametrization(point(200.0, 0.0), point(0.0, 0.0), 1.3);
17+
let arcs_orig = vec![arc0, arc1, arc2];
18+
19+
// Translate to fit in the SVG viewport
20+
//let poly = polyline_translate(&arcs_orig, point(40.0, 100.0));
21+
22+
// Internal offsetting
23+
//let poly = polyline_reverse(&poly);
24+
let offset_arclines = offset_arcline_to_arcline(&arcs_orig, 15.0, &mut cfg);
25+
26+
println!("Input arcline has {} vertices", arcs_orig.len());
27+
println!("Output has {} arclines", offset_arclines.len());
28+
for (i, arcline) in offset_arclines.iter().enumerate() {
29+
println!("Arcline {}: {} vertices", i, arcline.len());
30+
}
31+
32+
if let Some(svg) = cfg.svg.as_deref_mut() {
33+
// Write svg to file
34+
svg.write_stroke_width(0.1);
35+
}
36+
assert!(
37+
offset_arclines.len() == 1,
38+
"Wrong number of offset arclines generated"
39+
);
40+
}

examples/offset_pline1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use geom::prelude::*;
2-
use offroad::prelude::{OffsetCfg, pline_01, offset_polyline_to_polyline};
2+
use offroad::prelude::{OffsetCfg, offset_polyline_to_polyline, pline_01};
33

44
fn main() {
55
let mut cfg = OffsetCfg::default();
@@ -22,7 +22,7 @@ fn main() {
2222
// Internal offsetting
2323
let poly = polyline_reverse(&poly);
2424
let _offset_polylines = offset_polyline_to_polyline(&poly, 15.5600615, &mut cfg);
25-
25+
2626
if let Some(svg) = cfg.svg.as_deref_mut() {
2727
// Write svg to file
2828
svg.write_stroke_width(0.1);

examples/offset_polyline.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use offroad::prelude::{OffsetCfg, offset_polyline_to_polyline};
44
fn main() {
55
// Configuration for offsetting
66
let mut cfg = OffsetCfg::default();
7-
let mut svg = SVG::new(300.0, 300.0, "/tmp/out2.svg");
7+
let mut svg = SVG::new(300.0, 300.0, "/tmp/polyline.svg");
88
cfg.svg = Some(&mut svg);
99
// Show original polyline in SVG output
1010
cfg.debug_orig = true;
@@ -21,17 +21,20 @@ fn main() {
2121

2222
// Internal offsetting
2323
let poly = polyline_reverse(&poly);
24-
let _offset_polylines = offset_polyline_to_polyline(&poly, 15.0, &mut cfg);
25-
24+
let offset_polylines = offset_polyline_to_polyline(&poly, 15.0, &mut cfg);
25+
2626
println!("Input polyline has {} vertices", poly.len());
27-
println!("Output has {} polylines", _offset_polylines.len());
28-
for (i, polyline) in _offset_polylines.iter().enumerate() {
27+
println!("Output has {} polylines", offset_polylines.len());
28+
for (i, polyline) in offset_polylines.iter().enumerate() {
2929
println!("Polyline {}: {} vertices", i, polyline.len());
3030
}
31-
31+
3232
if let Some(svg) = cfg.svg.as_deref_mut() {
3333
// Write svg to file
3434
svg.write_stroke_width(0.1);
3535
}
36-
assert!(_offset_polylines.len() == 2, "Wrong number of offset polylines generated");
36+
assert!(
37+
offset_polylines.len() == 2,
38+
"Wrong number of offset polylines generated"
39+
);
3740
}

src/lib.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,37 @@
99

1010
// Offsetting algorithm components
1111
pub mod offset;
12-
//
12+
#[doc(hidden)]
1313
pub mod offset_raw;
14+
#[doc(hidden)]
1415
// raw offsetting components (lines, arcs)
1516
mod offset_polyline_raw;
17+
#[doc(hidden)]
1618
// connect raw offsets with arcs
1719
mod offset_connect_raw;
20+
#[doc(hidden)]
1821
// split raw offsets into segments in intersection points
1922
mod offset_split_arcs;
23+
#[doc(hidden)]
2024
// prune invalid offsets that are close to original polylines
2125
mod offset_prune_invalid;
26+
#[doc(hidden)]
2227
// resulting soup of arcs is ordered and reconnected
2328
mod offset_reconnect_arcs;
2429

2530

2631
// Re-export main offsetting functions
2732
// For public API
2833
pub mod prelude {
29-
pub use crate::offset::{offset_polyline_to_polyline, offset_arcline_to_arcline, offset_polyline_multiple, OffsetCfg};
34+
pub use crate::offset::{offset_polyline_to_polyline, offset_arcline_to_arcline, OffsetCfg};
3035
pub use crate::offset::{pline_01, pline_02, pline_03, pline_04};
3136
}
3237
// For internal use
33-
pub use crate::offset_polyline_raw::{offset_polyline_raw, poly_to_raws};
34-
pub use crate::offset_connect_raw::offset_connect_raw;
35-
pub use crate::offset_split_arcs::offset_split_arcs;
36-
pub use crate::offset_prune_invalid::offset_prune_invalid;
37-
pub use crate::offset_reconnect_arcs::{offset_reconnect_arcs, find_connected_components, find_middle_points, remove_bridge_arcs};
38+
// pub use crate::offset_polyline_raw::{offset_polyline_raw, poly_to_raws};
39+
// pub use crate::offset_connect_raw::offset_connect_raw;
40+
// pub use crate::offset_split_arcs::offset_split_arcs;
41+
// pub use crate::offset_prune_invalid::offset_prune_invalid;
42+
// pub use crate::offset_reconnect_arcs::{offset_reconnect_arcs, find_connected_components, find_middle_points, remove_bridge_arcs};
3843

3944
#[cfg(test)]
4045
mod tests;

0 commit comments

Comments
 (0)