Skip to content

Commit 5915e0f

Browse files
committed
gmsh for rectangles
1 parent 7f264bf commit 5915e0f

File tree

8 files changed

+406
-138
lines changed

8 files changed

+406
-138
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DelaunayTriangulation"
22
uuid = "927a84f5-c5f4-47a5-9785-b46e178433df"
33
authors = ["Daniel VandenHeuvel <[email protected]>"]
4-
version = "0.3.1"
4+
version = "0.3.2"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ poly!(ax, pts, [collect(T)[i][j] for i in 1:length(T), j in 1:3], color = (:whit
123123

124124
## Gmsh
125125

126-
Support is also added for a simple mesh generator with Gmsh (see https://gmsh.info/), tested up to v4.9.4 on Windows 64. The function for this is `generate_mesh`, and accepts inputs of boundary points that are in counter-clockwise order. This is especially useful for e.g. finite volume codes. Currently I only have code working for simply connected domains - it would be nice to have an alternative, but this is the best I can do with the time I have (the alternative would require me to think a lot more about ghost nodes, boundary edges, etc. when the domain has holes, and the impact this would have on the existing code and existing data structures).
126+
Support is also added for a simple mesh generator with Gmsh (see https://gmsh.info/), tested up to v4.9.4 on Windows 64. The function for this is `generate_mesh`, and accepts inputs of boundary points that are in counter-clockwise order. This is especially useful for e.g. finite volume codes. Currently I only have code working for simply connected domains - it would be nice to have an alternative, but this is the best I can do with the time I have (the alternative would require me to think a lot more about ghost nodes, boundary edges, etc. when the domain has holes, and the impact this would have on the existing code and existing data structures) -- this will eventually be supported.
127127

128128
Let me give an example. In my directory, I have downloaded `gmsh` and saved it as `gmsh-4.9.4-Windows64`, so I define
129129
```julia
@@ -185,6 +185,8 @@ lines!(ax, pts[:, BN[5]], color=:darkgreen, linewidth=4)
185185

186186
As you can see, the triangulation successfully works on this non-convex geometry, and we have been able to clearly identify where in `pts` all the boundary nodes are. This makes it especially useful for example for applying boundary conditions in a finite volume code.
187187

188+
You can also use `generate_mesh(a, b, c, d, ref)` to generate the mesh for a rectangle $[a, b] \times [c, d]$.
189+
188190
# Customisation
189191

190192
The package is built to allow for customisation. For either `triangulate_bowyer` or `triangulate_berg`, we have the following keywords (these are not all of them):

src/data_structures.jl

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,4 +421,64 @@ function Base.iterate(tri::Triangulation, state=1)
421421
elseif state == 5
422422
return (get_points(tri), nothing)
423423
end
424-
end
424+
end
425+
426+
"""
427+
triangulate(triangles,
428+
nodes::AbstractMatrix,
429+
boundary_nodes;
430+
IntegerType::Type{I}=Int64,
431+
EdgeType::Type{E}=NTuple{2,IntegerType},
432+
TriangleType::Type{V}=NTuple{3,IntegerType},
433+
EdgesType::Type{Es}=Set{EdgeType},
434+
TrianglesType::Type{Ts}=Set{TriangleType},
435+
sort_boundary=true) where {I,E,V,Es,Ts}
436+
437+
Given an existing set of `triangles` (all in counter-clockwise order), `nodes`, and `boundary_nodes` (listed in clockwise order),
438+
puts them into a [`Triangulation`](@ref) struct.
439+
"""
440+
function triangulate(triangles,
441+
nodes::AbstractMatrix,
442+
boundary_nodes; # BOUNDARY NODES MUST BE CLOCKWISE
443+
IntegerType::Type{I}=Int64,
444+
EdgeType::Type{E}=NTuple{2,IntegerType},
445+
TriangleType::Type{V}=NTuple{3,IntegerType},
446+
EdgesType::Type{Es}=Set{EdgeType},
447+
TrianglesType::Type{Ts}=Set{TriangleType},
448+
sort_boundary=true) where {I,E,V,Es,Ts}
449+
boundary_nodes = deepcopy(boundary_nodes)
450+
boundary_nodes = reduce(vcat, boundary_nodes)
451+
sort_boundary && unique!(boundary_nodes)
452+
T = Ts()
453+
adj = Adjacent{I,E}()
454+
adj2v = Adjacent2Vertex{I,Es,E}()
455+
DG = DelaunayGraph{I}()
456+
itr = triangles isa AbstractMatrix ? eachcol(triangles) : triangles
457+
for (i, j, k) in itr
458+
add_triangle!(i, j, k, T, adj, adj2v, DG; protect_boundary=true)
459+
end
460+
for i in boundary_nodes
461+
add_neighbour!(DG, I(BoundaryIndex), i)
462+
end
463+
if sort_boundary
464+
cx, cy = sum(nodes; dims=2) / size(nodes, 2)
465+
θ = zeros(length(boundary_nodes))
466+
for (j, i) in pairs(boundary_nodes)
467+
p = @view nodes[:, i]
468+
x, y = p
469+
θ[j] = atan(y - cy, x - cx)
470+
end
471+
sort_idx = sortperm(θ)
472+
permute!(boundary_nodes, sort_idx)
473+
reverse!(boundary_nodes) # cw
474+
end
475+
n = length(boundary_nodes)
476+
push!(boundary_nodes, boundary_nodes[begin])
477+
for i in 1:n
478+
u = boundary_nodes[i]
479+
v = boundary_nodes[i+1]
480+
add_edge!(adj2v, I(BoundaryIndex), u, v)
481+
add_edge!(adj, u, v, I(BoundaryIndex))
482+
end
483+
return T, adj, adj2v, DG
484+
end

0 commit comments

Comments
 (0)