Skip to content

Commit

Permalink
Fix: Calling any locate method with any NAN coordinates would ret…
Browse files Browse the repository at this point in the history
…urn arbitrary results.

It is better to systematically crash instead.
  • Loading branch information
Stoeoef committed Aug 13, 2024
1 parent e51880f commit f818352
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/delaunay_core/triangulation_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ pub trait TriangulationExt: Triangulation {
target_position: Point2<<Self::Vertex as HasPosition>::Scalar>,
start: FixedVertexHandle,
) -> PositionInTriangulation {
let pos = target_position.to_f64();
assert!(!f64::is_nan(pos.x));
assert!(!f64::is_nan(pos.y));

if self.num_vertices() < 2 {
return match self.vertices().next() {
Some(single_vertex) if single_vertex.position() == target_position => {
Expand Down Expand Up @@ -953,6 +957,23 @@ mod test {
Ok(())
}

#[test]
#[should_panic]
fn test_locate_nan_empty() {
let d = DelaunayTriangulation::<Point2<f64>>::default();
d.locate(Point2::new(0.0, f64::NAN));
}

#[test]
#[should_panic]
fn test_locate_nan() {
let points = random_points_with_seed(20, SEED);
let d = DelaunayTriangulation::<Point2<f64>>::bulk_load(points);
if let Ok(d) = d {
d.locate(Point2::new(0.0, f64::NAN));
}
}

#[test]
fn test_iterate_faces() -> Result<(), InsertionError> {
const SIZE: usize = 1000;
Expand Down
16 changes: 16 additions & 0 deletions src/triangulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ pub trait Triangulation: Default {
}

/// Returns information about the location of a point in a triangulation.
///
/// # Panics
///
/// Panics if the target point has any `NAN` coordinate.
fn locate(
&self,
point: Point2<<Self::Vertex as HasPosition>::Scalar>,
Expand All @@ -406,6 +410,10 @@ pub trait Triangulation: Default {
/// Locates a vertex at a given position.
///
/// Returns `None` if the point could not be found.
///
/// # Panics
///
/// Panics if the target point has any `NAN` coordinate.
#[allow(clippy::type_complexity)]
fn locate_vertex(
&self,
Expand Down Expand Up @@ -440,6 +448,10 @@ pub trait Triangulation: Default {
/// The hint should be a vertex close to the position that
/// is being looked up.
///
/// # Panics
///
/// Panics if the target vertex has any `NAN` coordinate.
///
/// *See also [locate](Triangulation::locate), [locate_vertex](Triangulation::locate_vertex)*
fn locate_with_hint(
&self,
Expand Down Expand Up @@ -474,6 +486,10 @@ pub trait Triangulation: Default {
/// This method will invalidate all vertex, edge and face handles
/// upon successful removal.
///
/// # Panics
///
/// Panics if the target position has any `NAN` coordinate.
///
/// *See also [remove](Triangulation::remove)*
fn locate_and_remove(
&mut self,
Expand Down

0 comments on commit f818352

Please sign in to comment.