Skip to content

Commit 90753df

Browse files
committed
Refactor offset configuration: replace debug flags with svg flags for original and final outputs in offset examples and core logic.
1 parent 5c9d034 commit 90753df

6 files changed

Lines changed: 80 additions & 47 deletions

File tree

examples/offset_arcline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ fn main() {
77
let mut svg = SVG::new(300.0, 300.0, "/tmp/arcline.svg");
88
cfg.svg = Some(&mut svg);
99
// Show original arcline in SVG output
10-
cfg.debug_orig = true;
10+
cfg.svg_orig = true;
1111
// Show final offset arclines in SVG output
12-
cfg.debug_final = true;
12+
cfg.svg_final = true;
1313

1414
let arc0 = arc_circle_parametrization(point(0.0, 0.0), point(100.0, 100.0), 0.0);
1515
let arc1 = arc_circle_parametrization(point(100.0, 100.0), point(200.0, 0.0), 0.5);

examples/offset_pline1.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ fn main() {
66
// This will create an SVG file at /tmp/pline1.svg
77
let mut svg = SVG::new(300.0, 300.0, "/tmp/pline1.svg");
88
cfg.svg = Some(&mut svg);
9-
cfg.debug_orig = true;
10-
cfg.debug_prune = true;
9+
cfg.svg_orig = true;
10+
cfg.svg_prune = true;
1111

1212
let poly_orig = pline_01()[0].clone();
1313
// Translate to fit in the SVG viewport

examples/offset_polyline.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ fn main() {
77
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
10-
cfg.debug_orig = true;
10+
cfg.svg_orig = true;
1111
// Show final offset polylines in SVG output
12-
cfg.debug_final = true;
12+
cfg.svg_final = true;
1313

1414
let poly_orig = vec![
1515
pvertex(point(0.0, 0.0), 0.0),
@@ -33,8 +33,5 @@ fn main() {
3333
// Write svg to file
3434
svg.write_stroke_width(0.1);
3535
}
36-
assert!(
37-
offset_polylines.len() == 2,
38-
"Wrong number of offset polylines generated"
39-
);
36+
assert!(offset_polylines.len() == 2);
4037
}

src/lib.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,29 @@
44
//!
55
//! # Examples
66
//!
7-
//! Check "examples" directory for usage examples.
7+
//! ```rust
8+
//! use geom::prelude::*;
9+
//! use offroad::prelude::{OffsetCfg, offset_polyline_to_polyline};
10+
//!
11+
//! // Create a simple L-shaped polyline
12+
//! let poly = vec![
13+
//! pvertex(point(0.0, 0.0), 0.0), // Start point
14+
//! pvertex(point(10.0, 0.0), 0.0), // Horizontal line
15+
//! pvertex(point(10.0, 10.0), 0.0), // Vertical line
16+
//! ];
17+
//!
18+
//! // Create default configuration
19+
//! let mut cfg = OffsetCfg::default();
20+
//!
21+
//! // Offset by 2.0 units to the right
22+
//! let offset_polylines = offset_polyline_to_polyline(&poly, 2.0, &mut cfg);
23+
//!
24+
//! // The function returns a vector of polylines
25+
//! assert!(offset_polylines.len() == 1);
26+
//! assert!(offset_polylines[0].len() == 6);
27+
//! ```
28+
//!
29+
//! Check "examples" directory for more usage examples.
830
931

1032
// Offsetting algorithm components

src/offset.rs

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,42 @@ use crate::{
1212
offset_split_arcs::offset_split_arcs
1313
};
1414

15+
/// Configuration options for offsetting operations.
1516
pub struct OffsetCfg<'a> {
16-
pub svg: Option<&'a mut SVG>, // Optional SVG context for rendering
17-
pub reconnect: bool, // Flag to indicate if reconnecting arcs is needed
18-
pub debug_orig: bool, // Flag to enable debug original polyline
19-
pub debug_row: bool, // Flag to enable debug row offsets
20-
pub debug_connect: bool, // Flag to enable debug connect offsets
21-
pub debug_split: bool, // Flag to enable debug split offsets
22-
pub debug_prune: bool, // Flag to enable debug pruned offsets
23-
pub debug_final: bool, // Flag to enable debug final offsets
17+
/// Optional SVG context for rendering
18+
pub svg: Option<&'a mut SVG>,
19+
/// Flag to indicate if reconnecting arcs is needed
20+
pub reconnect: bool,
21+
/// Flag to enable writing in svg original polyline
22+
pub svg_orig: bool,
23+
/// Flag to enable writing in svg raw offsets
24+
pub svg_raw: bool,
25+
/// Flag to enable writing in svg connect offsets
26+
pub svg_connect: bool,
27+
/// Flag to enable writing in svg split offsets
28+
pub svg_split: bool,
29+
/// Flag to enable writing in svg pruned offsets
30+
pub svg_prune: bool,
31+
/// Flag to enable writing in svg final offsets
32+
pub svg_final: bool,
2433
}
2534

2635
impl<'a> Default for OffsetCfg<'a> {
2736
fn default() -> Self {
2837
OffsetCfg {
29-
svg: None, // SVG context can be set later
30-
reconnect: true, // Default to reconnecting arcs
31-
debug_orig: false, // Debugging flags are off by default
32-
debug_row: false, // Debugging flags are off by default
33-
debug_connect: false, // Debugging flags are off by default
34-
debug_split: false, // Debugging flags are off by default
35-
debug_prune: false, // Debugging flags are off by default
36-
debug_final: false, // Debugging reconnect arcs
38+
svg: None,
39+
reconnect: true,
40+
svg_orig: false,
41+
svg_raw: false,
42+
svg_connect: false,
43+
svg_split: false,
44+
svg_prune: false,
45+
svg_final: false,
3746
}
3847
}
3948
}
4049

41-
/// Computes the offset of a polyline and returns result as simplified polylines.
50+
/// Computes the offset of a polyline and returns result as multiple polylines.
4251
///
4352
/// This is the main entry point for polyline offsetting. It takes an input polyline,
4453
/// applies the specified offset distance, and returns a vector of output polylines.
@@ -98,7 +107,7 @@ pub fn offset_polyline_to_polyline(
98107
cfg: &mut OffsetCfg,
99108
) -> Vec<Polyline> {
100109
if let Some(svg) = cfg.svg.as_deref_mut()
101-
&& cfg.debug_orig
110+
&& cfg.svg_orig
102111
{
103112
svg.polyline(poly, "red");
104113
}
@@ -117,15 +126,15 @@ pub fn offset_polyline_to_polyline(
117126
let final_poly = arcs_to_polylines(&reconnect_arcs);
118127

119128
if let Some(svg) = cfg.svg.as_deref_mut() {
120-
if cfg.debug_final {
129+
if cfg.svg_final {
121130
svg.polylines(&final_poly, "violet");
122131
}
123132
}
124133

125134
final_poly
126135
}
127136

128-
/// Computes the offset of an Arcline and returns result as Arcline-s.
137+
/// Computes the offset of an Arcline and returns result as multiple Arcline-s.
129138
///
130139
/// This function is similar to `offset_polyline_to_polyline` but operates on arclines
131140
/// (sequences of arcs). This is useful when
@@ -185,7 +194,7 @@ pub fn offset_polyline_to_polyline(
185194
///
186195
pub fn offset_arcline_to_arcline(arcs: &Arcline, off: f64, cfg: &mut OffsetCfg) -> Vec<Arcline> {
187196
if let Some(svg) = cfg.svg.as_deref_mut()
188-
&& cfg.debug_orig
197+
&& cfg.svg_orig
189198
{
190199
svg.arcline(arcs, "red");
191200
}
@@ -212,7 +221,7 @@ pub fn offset_arcline_to_arcline(arcs: &Arcline, off: f64, cfg: &mut OffsetCfg)
212221
}
213222

214223
if let Some(svg) = cfg.svg.as_deref_mut() {
215-
if cfg.debug_final {
224+
if cfg.svg_final {
216225
svg.arclines(&final_arcs, "violet");
217226
}
218227
}
@@ -236,6 +245,8 @@ fn offset_arcline_to_arcline_impl(arcs: &Arcline, off: f64, cfg: &mut OffsetCfg)
236245
offset_arcs
237246
}
238247

248+
#[doc(hidden)]
249+
/// Converts a vector of arcs into a vector of polylines.
239250
pub fn arcs_to_polylines(reconnect_arcs: &Vec<Vec<Arc>>) -> Vec<Polyline> {
240251
let mut polylines = Vec::with_capacity(reconnect_arcs.len());
241252
for arcs in reconnect_arcs.iter() {
@@ -245,9 +256,10 @@ pub fn arcs_to_polylines(reconnect_arcs: &Vec<Vec<Arc>>) -> Vec<Polyline> {
245256
polylines
246257
}
247258

248-
// function to convert from Vec<Arc> to Polyline
249-
// Note: arcs is a loop of arcs and when converting to PVertex,
250-
// some Arc can be either "a" to "b" or "b" to "a" oriented
259+
#[doc(hidden)]
260+
/// function to convert from Vec<Arc> to Polyline
261+
/// Note: arcs is a loop of arcs and when converting to PVertex,
262+
/// some Arc can be either "a" to "b" or "b" to "a" oriented
251263
pub fn arcs_to_polylines_single(arcs: &Vec<Arc>) -> Polyline {
252264
let mut polyline = Vec::new();
253265

@@ -384,6 +396,8 @@ mod test_arcs_to_polylines {
384396
}
385397
}
386398

399+
400+
#[doc(hidden)]
387401
pub fn offset_polyline_multiple(
388402
poly: &Polyline,
389403
step: f64,
@@ -404,21 +418,21 @@ pub fn offset_polyline_multiple(
404418
fn offset_single(poly_raws: &Vec<Vec<OffsetRaw>>, off: f64, cfg: &mut OffsetCfg) -> Vec<Arc> {
405419
let offset_raw = offset_polyline_raw::offset_polyline_raw(&poly_raws, off);
406420
if let Some(svg) = cfg.svg.as_deref_mut()
407-
&& cfg.debug_row
421+
&& cfg.svg_raw
408422
{
409423
svg_offset_raws(svg, &offset_raw, "blue");
410424
}
411425

412426
let offset_connect = offset_connect_raw(&offset_raw, off);
413427
if let Some(svg) = cfg.svg.as_deref_mut()
414-
&& cfg.debug_connect
428+
&& cfg.svg_connect
415429
{
416430
svg.arclines(&offset_connect, "violet");
417431
}
418432

419433
let mut offset_split = offset_split_arcs(&offset_raw, &offset_connect);
420434
if let Some(svg) = cfg.svg.as_deref_mut()
421-
&& cfg.debug_split
435+
&& cfg.svg_split
422436
{
423437
svg.arcline(&offset_split, "violet");
424438
svg.offset_segments_single_points(&offset_split, "violet");
@@ -427,14 +441,16 @@ fn offset_single(poly_raws: &Vec<Vec<OffsetRaw>>, off: f64, cfg: &mut OffsetCfg)
427441
let offset_prune = offset_prune_invalid(&poly_raws, &mut offset_split, off);
428442

429443
if let Some(svg) = cfg.svg.as_deref_mut()
430-
&& cfg.debug_prune
444+
&& cfg.svg_prune
431445
{
432446
svg.arcline(&offset_prune, "violet");
433447
svg.offset_segments_single_points(&offset_prune, "violet");
434448
}
435449
offset_prune
436450
}
437451

452+
453+
#[doc(hidden)]
438454
pub fn svg_offset_raws(svg: &mut SVG, offset_raws: &Vec<Vec<OffsetRaw>>, color: &str) {
439455
for raw in offset_raws {
440456
for seg in raw {
@@ -1585,6 +1601,7 @@ pub fn check_if_segments_intersect(off0: OffsetSegment, off1: OffsetSegment) ->
15851601
// }
15861602

15871603
/// Test polyline for offseting.
1604+
/// Has a mix of positive and negative offsets.
15881605
pub fn pline_01() -> Vec<Polyline> {
15891606
let pline = vec![
15901607
pvertex(point(100.0, 100.0), 1.5),
@@ -1617,6 +1634,8 @@ pub fn pline_01() -> Vec<Polyline> {
16171634
return plines;
16181635
}
16191636

1637+
1638+
/// Test polyline for offseting.
16201639
pub fn pline_02() -> Polyline {
16211640
let pline = vec![
16221641
pvertex(point(50.0, 50.0), ZERO),
@@ -2016,6 +2035,7 @@ pub fn pline_04() -> Vec<Polyline> {
20162035

20172036
// }
20182037

2038+
#[doc(hidden)]
20192039
pub fn polyline_to_arcs(plines: &Vec<Polyline>) -> Vec<Vec<Arc>> {
20202040
let mut varcs: Vec<Vec<Arc>> = Vec::new();
20212041
for pline in plines {
@@ -2024,6 +2044,7 @@ pub fn polyline_to_arcs(plines: &Vec<Polyline>) -> Vec<Vec<Arc>> {
20242044
varcs
20252045
}
20262046

2047+
#[doc(hidden)]
20272048
fn polyline_to_arcs_single(pline: &Polyline) -> Vec<Arc> {
20282049
let mut arcs = Vec::with_capacity(pline.len() + 1);
20292050
let last = pline.len() - 1;

src/offset_reconnect_arcs.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -353,13 +353,6 @@ pub fn remove_bridge_arcs(arcs: &mut Vec<Arc>) {
353353
/// - Prefers shortest cycles when multiple cycles share vertices
354354
/// - Reference: "Introduction to Algorithms" by Cormen et al., Chapter 22 (Graph Algorithms)
355355
///
356-
/// # Examples
357-
/// ```rust
358-
/// use offroad::find_connected_components;
359-
/// let graph = vec![(0, 1), (1, 2), (2, 0), (3, 4)];
360-
/// let components = find_connected_components(&graph);
361-
/// // Returns cycles like [[0, 1, 2]] for triangle and potentially isolated vertices
362-
/// ```
363356
pub fn find_connected_components(graph: &[(usize, usize)]) -> Vec<Vec<usize>> {
364357
// Optimized for graphs where each vertex has degree 1-4 only
365358
// This is typical for arc connection graphs in geometric applications

0 commit comments

Comments
 (0)