Skip to content

Commit 9f47c93

Browse files
committed
⚾ Refactor meshbuilder and improve clarity in 2D and 3D mesh generation.
1 parent c2d2a52 commit 9f47c93

1 file changed

Lines changed: 36 additions & 27 deletions

File tree

src/utils.jl

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function get_polygon(points::AbstractMatrix; ratio::Real=0.1, allow_holes::Bool=
4343
end
4444

4545
"""
46-
meshbuilder(x::T, y::T; ϵ::String="FP64") where T <: AbstractRange
46+
meshbuilder(x::AbstractRange, y::AbstractRange)
4747
4848
Description:
4949
---
@@ -54,18 +54,26 @@ Example:
5454
```julia
5555
x = 0:0.1:1
5656
y = 0:0.1:1
57-
mesh = meshbuilder(x, y; ϵ="FP32") # returns a 2xN array of points
57+
mesh = meshbuilder(x, y) # returns a 2xN array of points
5858
```
5959
"""
60-
function meshbuilder(x::T, y::T; ϵ::String="FP64") where T <: AbstractRange
61-
x_tmp = repeat(x', length(y), 1) |> vec
62-
y_tmp = repeat(y , 1, length(x)) |> vec
63-
T1 = ϵ == "FP32" ? Float32 : Float64
64-
return Array{T1}(hcat(x_tmp, y_tmp)')
60+
function meshbuilder(x::AbstractRange, y::AbstractRange)::Array{Float64, 2}
61+
nx, ny = length(x), length(y)
62+
result = Array{Float64, 2}(undef, 2, nx * ny)
63+
idx = 1
64+
@inbounds for i = 1:nx
65+
xi = x[i]
66+
@simd for j = 1:ny
67+
result[1, idx] = xi
68+
result[2, idx] = y[j]
69+
idx += 1
70+
end
71+
end
72+
return result
6573
end
6674

6775
"""
68-
meshbuilder(x::T, y::T, z::T; ϵ::String="FP64") where T <: AbstractRange
76+
meshbuilder(x::AbstractRange, y::AbstractRange, z::AbstractRange)
6977
7078
Description:
7179
---
@@ -77,24 +85,25 @@ Example:
7785
x = 0:0.1:1
7886
y = 0:0.1:1
7987
z = 0:0.1:1
80-
mesh = meshbuilder(x, y, z; ϵ="FP32") # returns a 3xN array of points
88+
mesh = meshbuilder(x, y, z) # returns a 3xN array of points
8189
"""
82-
function meshbuilder(x::T, y::T, z::T; ϵ::String="FP64") where T <: AbstractRange
83-
vx = x |> collect
84-
vy = y |> collect
85-
vz = z |> collect
86-
m, n, o = length(vy), length(vx), length(vz)
87-
vx = reshape(vx, 1, n, 1)
88-
vy = reshape(vy, m, 1, 1)
89-
vz = reshape(vz, 1, 1, o)
90-
om = ones(Int, m)
91-
on = ones(Int, n)
92-
oo = ones(Int, o)
93-
x_tmp = vec(vx[om, :, oo])
94-
y_tmp = vec(vy[:, on, oo])
95-
z_tmp = vec(vz[om, on, :])
96-
T1 = ϵ == "FP32" ? Float32 : Float64
97-
return Array{T1}(hcat(x_tmp, y_tmp, z_tmp)')
90+
function meshbuilder(x::AbstractRange, y::AbstractRange, z::AbstractRange)::Array{Float64, 2}
91+
nx, ny, nz = length(x), length(y), length(z)
92+
result = Array{Float64, 2}(undef, 3, nx * ny * nz)
93+
idx = 1
94+
@inbounds for k = 1:nz
95+
zk = z[k]
96+
for j = 1:nx
97+
xj = x[j]
98+
@simd for i = 1:ny
99+
result[1, idx] = xj
100+
result[2, idx] = y[i]
101+
result[3, idx] = zk
102+
idx += 1
103+
end
104+
end
105+
end
106+
return result
98107
end
99108

100109
"""
@@ -149,8 +158,8 @@ If `xy` is false, sort the points by the z-, y-, and then x-coordinates (3D).
149158
function sortpts(pts::AbstractMatrix; xy::Bool=true)
150159
if size(pts, 1) == 2 || xy
151160
perm = sortperm(axes(pts, 2), by = i -> (pts[2, i], pts[1, i]))
152-
elseif size(pts, 1) == 3
153-
perm = sortperm(axes(pts, 2), by = i -> (pts[3, i], pts[2, i], pts[1, i]))
161+
elseif size(pts, 1) == 3🍻
162+
perm = sortperm(axes(pts, 2), by = i -> (pts[2, i], pts[1, i], pts[3, i]))
154163
else
155164
throw(ArgumentError("The input points should have 2 or 3 rows (2/3D)"))
156165
end

0 commit comments

Comments
 (0)