meshpro / dmsh

:spider_web: Simple mesh generator inspired by distmesh.
GNU General Public License v3.0
210 stars 26 forks source link

Could this generate mesh on an arbitrary contour? #72

Closed WenjiaWang0312 closed 2 years ago

WenjiaWang0312 commented 3 years ago

For example, I have a boundary of an arbitrary shape on an image. It could not be parameterized by your function

nschloe commented 3 years ago

How about a polygon?

nschloe commented 2 years ago

Closing for lack of feedback. Feel free to reopen at any time.

oddroj commented 2 years ago

Just wanted to note that you can do this for any contour, first by making an arbitrary function that outputs an array of [x,y] coordinates associated with the edges.

for a quarter ellipse...


import numpy as np

#This is in polar coordinates to generate equi-theta spaced points. Can do it in elliptical or whatever.

def EllipsePol(a,b,Points):
    theta=np.linspace(0,np.pi/2,Points)
    r = a*b/np.sqrt((b**2 -a**2)*np.cos(theta)**2 + a**2)
    theta=np.append(theta,0)
    r=np.append(r,0)
    return np.array([r,theta])

#Converts to Cartesian

def PolToCart(r,theta):
    x=r*np.cos(theta)
    y=r*np.sin(theta)    
    return np.array([x,y])

#Define A and B and resolution (How polygon-esque it is)
A=2
B=5
res=5

Points=PolToCart(EllipsePol(A,B,res)[0],EllipsePol(A,B,res)[1])
geo = dmsh.Polygon(Points.T)
X, cells = dmsh.generate(geo,.3)
dmsh.helpers.show(X, cells, geo)

yields

image

With a high resolution: image

The weird elements are due to the spacing of the poly-points. See with equi-X spacing:

image

nschloe commented 2 years ago

The weird boundary on ellipse meshes are a known bug, see https://github.com/nschloe/dmsh/issues/44.