From d3f931c24f4d6de0d8f746cf27f29372cf8bbb5e Mon Sep 17 00:00:00 2001 From: DanielVandH Date: Fri, 4 Aug 2023 10:24:19 +1000 Subject: [PATCH 1/2] Add method for testing if a graph has a specific vertex, or has boundary vertices, and so fix issue #70 --- src/data_structures/graph.jl | 19 +++++++++++++++++ src/data_structures/triangulation/graph.jl | 17 ++++++++++++++- src/data_structures/triangulation/points.jl | 4 ++-- test/data_structures/graph.jl | 11 ++++++++++ test/data_structures/triangulation.jl | 23 +++++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/data_structures/graph.jl b/src/data_structures/graph.jl index e17201c54..04ce7de63 100644 --- a/src/data_structures/graph.jl +++ b/src/data_structures/graph.jl @@ -45,6 +45,8 @@ is omitted.) - `num_edges(G)` - `num_neighbours(G, u)` - `num_vertices(G)` +- `has_vertex(G, u)` +- `has_boundary_vertices(G)` """ struct Graph{I} graph::UndirectedGraph{I} @@ -129,6 +131,14 @@ Returns the number of vertices in the graph `G`. """ num_vertices(G::Graph) = length(get_vertices(G)) +""" + has_vertex(G::Graph, u) + +Returns `true` if the vertex `u` is in the graph `G`, +and `false` otherwise. +""" +has_vertex(G::Graph, u) = has(get_graph(G), u) + """ add_vertex!(G::Graph, u...) @@ -276,6 +286,15 @@ function delete_boundary_vertices_from_graph!(G::Graph{I}) where {I} return nothing end +""" + has_boundary_vertices(G::Graph{I}) where {I} + +Returns `true` if the graph `G` has any boundary vertices, and `false` otherwise. +""" +function has_boundary_vertices(G::Graph{I}) where {I} + return any(≤(I(BoundaryIndex)), get_vertices(G)) +end + """ clear_empty_points!(G::Graph) diff --git a/src/data_structures/triangulation/graph.jl b/src/data_structures/triangulation/graph.jl index ebdc58d10..0fa9c21e1 100644 --- a/src/data_structures/triangulation/graph.jl +++ b/src/data_structures/triangulation/graph.jl @@ -91,4 +91,19 @@ in the triangulation. Returns `num_vertices(get_graph(tri))`, the number of vertices in the triangulation. """ -@inline num_vertices(tri::Triangulation) = num_vertices(get_graph(tri)) \ No newline at end of file +@inline num_vertices(tri::Triangulation) = num_vertices(get_graph(tri)) + +""" + has_vertex(tri::Triangulation, u) + +Returns `true` if the vertex `u` is in the triangulation, +and `false` otherwise. +""" +@inline has_vertex(tri::Triangulation, u) = has_vertex(get_graph(tri), u) + +""" + has_boundary_vertices(tri::Triangulation) + +Returns `true` if the triangulation has any ghost vertices, and `false` otherwise. +""" +@inline has_boundary_vertices(tri::Triangulation) = has_boundary_vertices(get_graph(tri)) \ No newline at end of file diff --git a/src/data_structures/triangulation/points.jl b/src/data_structures/triangulation/points.jl index 572a3c577..461b5aeca 100644 --- a/src/data_structures/triangulation/points.jl +++ b/src/data_structures/triangulation/points.jl @@ -83,7 +83,7 @@ Sets the `i`th point of `tri` to `p = (x, y)`. Returns the number of ghost vertices of `tri`. """ -num_ghost_vertices(tri::Triangulation) = length(all_boundary_indices(tri)) +num_ghost_vertices(tri::Triangulation) = length(all_boundary_indices(tri)) * has_boundary_vertices(tri) """ num_solid_vertices(tri::Triangulation) @@ -102,7 +102,7 @@ struct EachSolidVertex{V,T} <: AbstractEachVertex{V} vertices::V tri::T end -Base.length(verts::EachSolidVertex) = num_solid_vertices(verts.tri) +Base.length(verts::EachSolidVertex) = num_solid_vertices(verts.tri) struct EachGhostVertex{V,T} <: AbstractEachVertex{V} vertices::V tri::T diff --git a/test/data_structures/graph.jl b/test/data_structures/graph.jl index 085c70888..9aa1acc10 100644 --- a/test/data_structures/graph.jl +++ b/test/data_structures/graph.jl @@ -123,4 +123,15 @@ end @testset "Number of vertices and each_vertex" begin @test num_vertices(g) == 6 @test each_vertex(g) == get_vertices(g) +end + +@testset "has_vertex and has_boundary_vertices" begin + bidx = DT.BoundaryIndex + DT.add_vertex!(g, bidx, bidx - 1, bidx - 2, bidx - 3, bidx - 4) + @test DT.has_vertex(g, bidx) + @test DT.has_vertex(g, bidx - 4) + @test DT.has_vertex(g, 5) + @test DT.has_boundary_vertices(g) + DT.delete_boundary_vertices_from_graph!(g) + @test !DT.has_boundary_vertices(g) end \ No newline at end of file diff --git a/test/data_structures/triangulation.jl b/test/data_structures/triangulation.jl index 864179218..d55a91edc 100644 --- a/test/data_structures/triangulation.jl +++ b/test/data_structures/triangulation.jl @@ -862,4 +862,27 @@ end Base.Threads.@threads for _ in 1:5000 get_adjacent(tri, -5, rand(1:1000)) end +end + +@testset "has_vertex and has_boundary_vertices" begin + tri = triangulate(rand(2, 50), delete_ghosts=false) + @test DT.has_vertex(tri, 1) + @test !DT.has_vertex(tri, 57) + @test DT.has_boundary_vertices(tri) + @test DT.has_vertex(tri, -1) + DT.delete_boundary_vertices_from_graph!(tri) + @test !DT.has_vertex(tri, -1) + @test !DT.has_boundary_vertices(tri) +end + +@testset "Issue #70" begin + points = [(-1.0, -1.0), (1.0, -1.0), (0.0, 1.0)] + tri = triangulate(points) + delete_ghost_triangles!(tri) + DelaunayTriangulation.delete_boundary_vertices_from_graph!(tri) + @test collect(each_solid_vertex(tri)) == collect(each_vertex(tri)) + @test !DelaunayTriangulation.has_boundary_vertices(tri) + @test DelaunayTriangulation.num_ghost_vertices(tri) == 0 + @test DelaunayTriangulation.num_solid_vertices(tri) == 3 + @test isempty(collect(each_ghost_vertex(tri))) end \ No newline at end of file From 10446523ac579aa9f7caa4b0c1fff992607f15f9 Mon Sep 17 00:00:00 2001 From: DanielVandH Date: Fri, 4 Aug 2023 10:27:59 +1000 Subject: [PATCH 2/2] Need to also update the iterator --- src/data_structures/triangulation/points.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data_structures/triangulation/points.jl b/src/data_structures/triangulation/points.jl index 461b5aeca..5cd80c2ab 100644 --- a/src/data_structures/triangulation/points.jl +++ b/src/data_structures/triangulation/points.jl @@ -119,7 +119,7 @@ function Base.iterate(itr::EachSolidVertex, state...) end return vertices, state end -Base.iterate(itr::EachGhostVertex, state...) = Base.iterate(itr.vertices, state...) +Base.iterate(itr::EachGhostVertex, state...) = has_boundary_vertices(itr.tri) ? Base.iterate(itr.vertices, state...) : nothing """ each_solid_vertex(tri::Triangulation)