Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions src/cdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

use crate::cdt::ConflictRegionEnd::{EdgeOverlap, Existing};
use crate::delaunay_core::dcel_operations::flip_cw;
use crate::delaunay_core::{bulk_load_cdt, bulk_load_stable};
use crate::delaunay_core::{bulk_load_cdt, bulk_load_stable, try_bulk_load_cdt};
use crate::{
cdt, mitigate_underflow, DelaunayTriangulation, HasPosition, HintGenerator, InsertionError,

Check failure on line 12 in src/cdt.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `cdt`

Check warning on line 12 in src/cdt.rs

View workflow job for this annotation

GitHub Actions / Check

unused import: `cdt`

Check warning on line 12 in src/cdt.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused import: `cdt`
LastUsedVertexHintGenerator, Point2, Triangulation, TriangulationExt,
};
use crate::{
delaunay_core::Dcel, intersection_iterator::LineIntersectionIterator, PositionInTriangulation,
SpadeNum,
};
use crate::{handles::*, intersection_iterator::Intersection};
use crate::{
mitigate_underflow, DelaunayTriangulation, HasPosition, HintGenerator, InsertionError,
LastUsedVertexHintGenerator, Point2, Triangulation, TriangulationExt,
};

/// Undirected edge type of a [ConstrainedDelaunayTriangulation] (CDT).
///
Expand Down Expand Up @@ -410,10 +410,27 @@
vertices: Vec<V>,
edges: Vec<[usize; 2]>,
) -> Result<Self, InsertionError> {
let mut result: Self =
bulk_load_stable(move |vertices| bulk_load_cdt(vertices, edges), vertices)?;
Self::try_bulk_load_cdt_stable(vertices, edges).map(|(cdt, _)| cdt)
}

/// See [Self::bulk_load_cdt_stable]
pub fn try_bulk_load_cdt_stable(
vertices: Vec<V>,
edges: Vec<[usize; 2]>,
) -> Result<(Self, Vec<[usize; 2]>), InsertionError> {
let mut conflicting_edges = Vec::new();
let mut result: Self = bulk_load_stable(
|vertices| match try_bulk_load_cdt(vertices, edges) {
Ok((cdt, new_conflicting_edges)) => {
conflicting_edges = new_conflicting_edges;
Ok(cdt)
}
Err(e) => Err(e),
},
vertices,
)?;
*result.hint_generator_mut() = L::initialize_from_triangulation(&result);
Ok(result)
Ok((result, conflicting_edges))
}

/// # Handle invalidation
Expand Down Expand Up @@ -691,7 +708,7 @@
///
/// See also [Self::get_conflicting_edges_between_vertices]
pub fn get_conflicting_edges_between_points(
&self,

Check warning on line 711 in src/cdt.rs

View workflow job for this annotation

GitHub Actions / Fuzz

lifetime flowing from input to output with different syntax can be confusing
from: Point2<<V as HasPosition>::Scalar>,
to: Point2<<V as HasPosition>::Scalar>,
) -> impl Iterator<Item = DirectedEdgeHandle<V, DE, CdtEdge<UE>, F>> {
Expand All @@ -707,7 +724,7 @@
///
/// See also [Self::get_conflicting_edges_between_points]
pub fn get_conflicting_edges_between_vertices(
&self,

Check warning on line 727 in src/cdt.rs

View workflow job for this annotation

GitHub Actions / Fuzz

lifetime flowing from input to output with different syntax can be confusing
from: FixedVertexHandle,
to: FixedVertexHandle,
) -> impl Iterator<Item = DirectedEdgeHandle<V, DE, CdtEdge<UE>, F>> {
Expand Down Expand Up @@ -1995,6 +2012,26 @@
Cdt::bulk_load_cdt_stable(vertices, vec![[3, 2], [5, 4], [7, 6]])
}

#[test]
fn get_try_cdt_for_duplicate_vertices() -> Result<(), InsertionError> {
let vertices = vec![
Point2::new(0.0, -10.0),
Point2::new(76.0, 0.0),
Point2::new(76.0, 0.0), // Duplicated vertex
Point2::new(20.0, -30.0),
Point2::new(45.0, 25.0),
Point2::new(32.0, -35.0),
Point2::new(60.0, 20.0),
Point2::new(60.0, -30.0),
Point2::new(50.0, -34.0),
];
let (_, conflicting_edges) =
Cdt::try_bulk_load_cdt_stable(vertices, vec![[3, 2], [5, 4], [7, 6]])?;
// Hardcoded values, may change if CDT algorithm change
assert_eq!(&conflicting_edges, &[[6, 7,], [4, 5,]]);
Ok(())
}

#[test]
fn test_single_split() -> Result<(), InsertionError> {
let vertices = vec![
Expand Down
44 changes: 37 additions & 7 deletions src/delaunay_core/bulk_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,38 @@
Ok(result)
}

/// Returns the [`ConstrainedDelaunayTriangulation`].
///
/// Conflicting edges are ignored and returned.
/// If any conflicting edge is returned, this means input is incorrect and you may want to look into fixing it.
pub fn bulk_load_cdt<V, DE, UE, F, L>(
elements: Vec<V>,
mut edges: Vec<[usize; 2]>,

Check failure on line 159 in src/delaunay_core/bulk_load.rs

View workflow job for this annotation

GitHub Actions / Clippy

variable does not need to be mutable

Check warning on line 159 in src/delaunay_core/bulk_load.rs

View workflow job for this annotation

GitHub Actions / Check

variable does not need to be mutable

Check warning on line 159 in src/delaunay_core/bulk_load.rs

View workflow job for this annotation

GitHub Actions / Test Suite

variable does not need to be mutable

Check warning on line 159 in src/delaunay_core/bulk_load.rs

View workflow job for this annotation

GitHub Actions / Fuzz

variable does not need to be mutable
) -> Result<ConstrainedDelaunayTriangulation<V, DE, UE, F, L>, InsertionError>
where
V: HasPosition,
DE: Default,
UE: Default,
F: Default,
L: HintGenerator<<V as HasPosition>::Scalar>,
{
try_bulk_load_cdt(elements, edges).map(|(cdt, _)| cdt)
}

/// Returns the [`ConstrainedDelaunayTriangulation`].
///
/// Conflicting edges are ignored and returned.
/// If any conflicting edge is returned, this means input is incorrect and you may want to look into fixing it.
pub fn try_bulk_load_cdt<V, DE, UE, F, L>(
elements: Vec<V>,
mut edges: Vec<[usize; 2]>,
) -> Result<

Check failure on line 178 in src/delaunay_core/bulk_load.rs

View workflow job for this annotation

GitHub Actions / Clippy

very complex type used. Consider factoring parts into `type` definitions
(
ConstrainedDelaunayTriangulation<V, DE, UE, F, L>,
Vec<[usize; 2]>,
),
InsertionError,
>
where
V: HasPosition,
DE: Default,
Expand All @@ -162,11 +190,11 @@
L: HintGenerator<<V as HasPosition>::Scalar>,
{
if elements.is_empty() {
return Ok(ConstrainedDelaunayTriangulation::new());
return Ok((ConstrainedDelaunayTriangulation::new(), Vec::new()));
}

if edges.is_empty() {
return bulk_load(elements);
return bulk_load(elements).map(|cdt| (cdt, Vec::new()));
}

let mut point_sum = Point2::<f64>::new(0.0, 0.0);
Expand Down Expand Up @@ -232,7 +260,7 @@
let mut next_constraint = edges.pop();

let mut buffer = Vec::new();

let mut failed_constraints = Vec::new();
let mut add_constraints_for_new_vertex =
|result: &mut ConstrainedDelaunayTriangulation<V, DE, UE, F, L>, index| {
while let Some([from, to]) = next_constraint {
Expand All @@ -241,7 +269,9 @@
let [new_from, new_to] =
[from, to].map(|v| FixedVertexHandle::new(old_to_new[v]));
// Insert constraint edge
result.add_constraint(new_from, new_to);
if result.try_add_constraint(new_from, new_to).is_empty() {
failed_constraints.push([from, to]);
}
next_constraint = edges.pop();
} else {
break;
Expand All @@ -251,7 +281,7 @@

let mut hull = loop {
let Some((old_index, next)) = elements.pop() else {
return Ok(result);
return Ok((result, failed_constraints));
};
result.insert(next)?;
add_constraints_for_new_vertex(&mut result, old_index);
Expand Down Expand Up @@ -282,7 +312,7 @@
elements.push((old_index, skipped));
hull = loop {
let Some((old_index, next)) = elements.pop() else {
return Ok(result);
return Ok((result, failed_constraints));
};
result.insert(next)?;
add_constraints_for_new_vertex(&mut result, old_index);
Expand All @@ -302,7 +332,7 @@
hull_sanity_check(&result, &hull);
}

Ok(result)
Ok((result, failed_constraints))
}

fn try_get_hull_center<V, T>(result: &T) -> Option<Point2<f64>>
Expand Down
2 changes: 1 addition & 1 deletion src/delaunay_core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod refinement;
pub mod interpolation;
pub mod math;

pub use bulk_load::{bulk_load, bulk_load_cdt, bulk_load_stable};
pub use bulk_load::{bulk_load, bulk_load_cdt, bulk_load_stable, try_bulk_load_cdt};

pub use triangulation_ext::{RemovalResult, TriangulationExt};

Expand Down
Loading