Skip to content

Commit bc999fb

Browse files
sbadriankrcools
authored andcommitted
Allow the meshing of 2D/3D curves
Provide a definition of a curve in suitable parameter space, meshcurve function will mesh it. This allows to quickly draw (smooth) 2D meshes directly in CMS.
1 parent 380d982 commit bc999fb

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/CompScienceMeshes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export euclidianbasis, point
2222

2323
# default mesh creation
2424
export mesh, readmesh, writemesh, meshgeo, setminus, load_gmsh_mesh
25-
export meshsegment, meshcircle
25+
export meshsegment, meshcircle, meshcurve
2626
export meshcuboid, meshcylinder, meshdisk, meshicosphere, meshmobius, meshrectangle, meshsphere
2727
export gmshcuboid, gmshrectangle, gmshsphere
2828
export tetmeshsphere, tetmeshcuboid, tetgmshcuboid, meshball

src/primitives/linemeshes/linemeshes.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,55 @@ CT = SVector{2,Int}
5757

5858
return Mesh(vertices, faces)
5959
end
60+
61+
"""
62+
meshcurve(curve, delta::T) where T<:Real
63+
64+
Meshes a curve (2D or 3D) describe by an analytical curve.
65+
In principle, piecewise curves are possible, as long as the parameter
66+
space is continuous.
67+
68+
WARNING: We do not ensure uniform segment length in the physical space!
69+
"""
70+
function meshcurve(curve, delta::T; udim=2, tstart=T(0.0), tend=T(2π), order=1) where T<:Real
71+
72+
PT = SVector{udim,T}
73+
CT = SVector{2,Int}
74+
75+
ξ, w = CompScienceMeshes.legendre(1000, tstart, tend)
76+
77+
curvelength = dot(w, norm.(curve.(ξ)))
78+
79+
@info "The total curve length is $(curvelength)"
80+
81+
num_segments = ceil(Int, curvelength / delta)
82+
83+
tdelta = (tend-tstart) / num_segments
84+
85+
if norm(curve(tstart) - curve(tend)) < eps(T)*1e1
86+
isclosed = true
87+
tdeltas = collect(0 : num_segments-1) * tdelta
88+
vertices = Array{PT}(undef,num_segments)
89+
else
90+
isclosed = false
91+
tdeltas = collect(0 : num_segments) * tdelta
92+
vertices = Array{PT}(undef,num_segments + 1)
93+
end
94+
95+
for i in 1 : (isclosed ? num_segments : num_segments + 1)
96+
vertices[i] = PT(curve(tdeltas[i])[1], curve(tdeltas[i])[2])
97+
end
98+
99+
faces = Array{CT}(undef,num_segments)
100+
for i in 1 : num_segments-1
101+
faces[i] = SVector{2,Int}(i, i+1)
102+
end
103+
104+
if isclosed
105+
faces[end] = SVector{2,Int}(num_segments, 1)
106+
else
107+
faces[end] = SVector{2,Int}(num_segments, num_segments + 1)
108+
end
109+
110+
return Mesh(vertices, faces)
111+
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
using CompScienceMeshes
3+
using StaticArrays
4+
using Test
5+
6+
curve(t) = SVector((9/20-(1/9)cos(5t))*cos(t), (9/20-(1/9)cos(5t))*sin(t))
7+
8+
# Detect closed curve
9+
msh = meshcurve(curve, 0.1; tend=Float64(2π))
10+
@test msh.faces[end][2] == 1
11+
12+
# Detect open curve
13+
msh = meshcurve(curve, 0.1; tend=Float64(π))
14+
@test msh.faces[end][2] != 1

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ include("test_nbd.jl")
4848
include("test_mapper.jl")
4949
include("test_convert.jl")
5050

51+
include("primitives/linemeshes/test_curve.jl")
5152
include("primitives/surfacemeshes/test_cuboid.jl")
5253
include("primitives/surfacemeshes/test_icosphere.jl")
5354
include("primitives/surfacemeshes/test_rectangle.jl")

0 commit comments

Comments
 (0)