Is there a simple way to generate uniform points within a given region?

I’m looking for a way to generate a mesh with given region.
Basically I want things like the Fig 1 in https://doc.freefem.org/tutorials/poisson.html.

What I need are just the vertices. The triangles are not important to me.
Is there a simple way to get this?

Thank you.

2 Likes

You can use rand() to generate a random number between 0 and 1:

julia> rand()
0.45228937995996965

and you can generate a 2-element random vector with rand(2):

julia> rand(2)
2-element Array{Float64,1}:
 0.5299598174026356 
 0.13314274980488228

You can generate a list of random vectors using a list comprehension:

julia> [rand(2) for _ in 1:5]
5-element Array{Array{Float64,1},1}:
 [0.931461404102448, 0.14979258911851256] 
 [0.30356330980471347, 0.3357467250045538]
 [0.5851807158436648, 0.264789888558719]  
 [0.36648327079645826, 0.730164220941981] 
 [0.4670939298076662, 0.9425081883161783] 

Or, if you want a more efficient solution, you can create random SVectors using StaticArrays.jl:

julia> using StaticArrays

julia> rand(SVector{2, Float64}, 5)
5-element Array{SArray{Tuple{2},Float64,1,2},1}:
 [0.926329220599331, 0.48102301661919555]  
 [0.5606673579437782, 0.017517785007929332]
 [0.9727391327916786, 0.27827197467782905] 
 [0.6926174031582435, 0.5357200141460445]  
 [0.9677270267237157, 0.8883903773952777]  
3 Likes

Thank you. I will try this.

This is probably not a good way to generate a uniform mesh, because the separation of random points will have a large variance — some points will be very close together.

One simple procedure would be to generate a uniform cartesian (or hexagonal) grid and then truncate it to the shape of interest, but that may lead to “poor” triangles (closely spaced points) at the boundaries.

In-filling a general polygonal or polyhedral shape with a nearly uniform set of points (nearly equilateral triangles) is a tricky mathematical problem that is solved by various mesh-generation algorithms. I don’t know if there is a native Julia mesh-generation program yet, but there are Julia wrappers around various external libraries that can solve this problem, e.g. Gmsh.jl or TRIANGLE.jl.

(You can also call Matlab, or call Python packages like MeshPy … It would be nice if we had native Julia packages for everything right away, but we’ve worked hard on inter-language calling so that you can gradually transition: take advantage of mature libraries from other languages while still writing your problem-specific code in Julia.)

6 Likes

Instead of TRIANGLE.jl, consider also TriangleMesh.jl. They both wrap the same C library. TRIANGLE has a nicer API(?), but TriangleMesh wraps more methods. In particular, TRIANGLE only offers (Constrained) Delaunay Triangulation, but no mesh refinement.

2 Likes

This is probably not a good way to generate a uniform mesh, because the separation of random points will have a large variance — some points will be very close together.

Let’s say I’m stubborn and want to generate additional points anyway, to be used as the basis of a subsequent Delaunay triangulation. Would it be better to use some specific method of generating the points, e.g. using Sobol.jl, over just uniform sampling for each coordinate?

1 Like

Although I haven’t had time to test these, thanks for all suggestions.