Code covered by the BSD License  

Highlights from
MESH2D - Automatic Mesh Generation

5.0

5.0 | 21 ratings Rate this file 207 Downloads (last 30 days) File Size: 121 KB File ID: #25555
image thumbnail

MESH2D - Automatic Mesh Generation

by

 

Generates unstructured triangular meshes for general 2D geometry.

| Watch this File

File Information
Description

MESH2D is a toolbox of 2D meshing routines that allows for the automatic generation of unstructured triangular meshes for general 2D geometry.

An iterative algorithm is used that attempts to optimise the mesh topology/vertex position to achieve high quality triangulations.

In addition to the fully automatic settings, MESH2D allows the user to specify sizing information, allowing for varying levels of mesh resolution within the domain.

MESH2D includes a large set of demos/benchmarks. Please see the following:

meshdemo - runs a standard set of demos.
mesh_collection - a collection of user contributed geometries.
facedemo - examples of geometry with connected faces.

MESH2D was previously released under a GNU license. This has been removed to cater for the current restrictions of the MATLAB file exchange. I apologise to the many users who have been unable to access MESH2D due to this license conflict.

Acknowledgements

This file inspired Accelerating Finite Element Analysis (Fea) In Matlab, Fgt Fold Geometry Toolbox, Iges Toolbox, and Remove Unconnected Triangle.

MATLAB release MATLAB 7 (R14)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (41)
02 Jul 2014 Siva Srinivas Kolukula

Excellent ...code..very useful for FEA..

17 Jun 2014 Putra Arri Sandhi  
22 May 2014 liron

you are a genius!!!! awesome addition... thanks very helpful....

10 Apr 2014 Thomas Benson

This is a great submission! A minor change was required for my application. Reason: The triangulation tends to be slightly over-refined on complex boundaries. For some applications this is a bonus, but for hydrodymanic modelling it is not really necessary. I therefore replaced the line in 'quadtree.m' (about line 64) which reads:

L = 2.0*dist2poly(wm,edgexy,2.0*len);

with the following:

L=len;

This helps prevent over refinement and a nice mesh. Excellent work Darren!

26 Feb 2014 Abdulrahman  
03 Feb 2014 Abdulrahman

Great work thank you very much.

I've got a question I appreciate if any one could help, where exactly the changes can take place ? I know it would be most about the geometry but I wasn't able to play with it.

19 Dec 2013 Marcus

I'm doing something that must be pretty standard. I'm needing to mesh a 2d domain with an internal boundary. My approach has been to create 'holes' with zero area, which imposes duplicate nodes. Mesh2d seems able to remove these and gives e.g., the message 'WARNING: 2 duplicate node(s) removed'. However, with some of my examples I get the message: 'Warning: Duplicate data points have been detected. Some data points do not map to vertices in the triangulation. To avoid this behavior, call UNIQUE on the data points prior to calling DELAUNAY.' Now what I would like to know is whether this problem in the geometry is fixed by Mesh2d? I include a simple example of the sort of problem I'm interested in.
PS. Mesh2d has proved invaluable - Thank you!
***********************************
p1 = [0 0; ...
3 0; ...
3 3; ...
0 3];
p2 = [0.5 1.5; ...
1.5 1.5; ...
1.5 2.5; ...
1.5 1.5; ...
0.5 1.5];
n1 = size(p1,1);
n2 = size(p2,1);
c1 = [(1:n1-1)', (2:n1)'; n1, 1];
c2 = [(1:n2-1)', (2:n2)'; n2, 1];
node = [p1; p2];
cnect = [c1;c2+n1];
hdata.hmax = 1;
[p,t] = mesh2d(node,cnect,hdata,[]);
*****************************************

22 Oct 2013 Richard Clarke

Great routine, although I was having some issues removing redundant nodes until I changed the following in line 66 of changegeometry.m

del = nNode-i(end);

-->

del = nNode-length(i);

23 Sep 2013 Mike

This is a powerful and useful tool. It is well written and documented.

Since Mathworks abandoned tsearch, simply calling tsearchn instead works for me. Moving to tsearchn requires two changes that I am aware of:

mytesearch.m Line 68:
i(j)=tsearchn([x y],t,[xi(j) yi(j)]);

meshfaces.m Line 199:
i = tsearchn(ph,th,p);

I do not agree with the solution proposed by Chen and Barnhart. Delaunay triangulations are not unique. Their approach effectively generates a separate triangulation and searches over it. Mapping such results into the original Mesh2D triangulation is ill-advised.

08 May 2013 Elias Pirayesh

There are some useful examples on meshing a 3D surface on: http://persson.berkeley.edu/distmesh/

07 May 2013 Evgeny

How can I use it on 3D surface?
Is there some algorithm to create unstructured triangular meshes for 3D geometry?

03 Apr 2013 William Barnhart

One more fix. In quadtree.m, near line 528 (right before %Undo rotation, p=rotate(p,-theta)), add:

% remove duplicate nodes
goodid=find(h~=0);
p=p(goodid,:);
h=h(goodid);
for i=1:length(goodid)
t(t==goodid(i))=i;
end

So that's three places to fix with the comments below:
meshfaces.m, line 203
mytsearch.m, line 69
quadtree.m, line 528 (or thereabouts)

02 Apr 2013 William Barnhart

**Correction**

In my tsearch, replace:
i(j) = tsearch(x,y,t,xi(j),yi(j));

with:
for k=1:size(t,1)
temp=inpolygon(xi,yi,x(t(k,:)),y(t(k,:)));
i(temp)=k;
end

Then, no need to worry about delaunay triangulations

02 Apr 2013 William Barnhart

Tsearch fix courtesy of Rowena Lohman.
tsearch.m needs to be replaced in two locations. First:

in meshfaces.m, line 203 replace:
i=tsearch(ph(:,1),ph(:,2),th,p(:,1),p(:,2));

with:

for j=1:size(th,1)
temp=inpolygon(p(:,1),p(:,2),ph(th(j,:),1),ph(th(j,:),2));
i(temp)=j;
end

In mytsearch.m line 69, replace:
i(j) = tsearch(x,y,t,xi(j),yi(j));

with

t = DelaunayTri([x y]);
i(j)= pointLocation(t,[xi(j) yi(j)]);

This uses DelaunayTri which is being fazed out, but delaunayTriangulation can be equally used

20 Mar 2013 Thomas Benson

Hi Darren, thanks for this submission. Is there a version of Mesh2d that works with V2012b ? (TSEARCH.m no longer works and requires the use of DelaunayTri.m). Cheers.

04 Oct 2012 Elliot

I too need an update to replace tsearch function. The suggested replacement is not appropriate for me as performing 'DelaunayTri' within your routine can alter the triangulation depending on initial inputs i.e. I have specified internal boundaries, DelaunayTri does not respect these boundaries when implemented in the suggested way.

Is a modified/updated version in consideration?

thanks

17 Jul 2012 Song Chen

Find the following line:

p = node;
e = edge;
i = tsearch(ph(:,1),ph(:,2),th,p(:,1),p(:,2));
Change it to

p = node;
e = edge;
dt = DelaunayTri(ph);
i = pointLocation(dt,p);

These two codes should be equivalent.

17 Jul 2012 Jody Klymak

tsearch is gone as of 2012a. Any hope of a modification to use DelaunyTri?

17 Jun 2012 cleidinei  
15 Jun 2012 Richard Crozier

I think Maxim is right:

>> A = triarea([0,0; 1,0; 0.5,1],[1,2,3])

A =

1.0000e+000

I think the area of that triangle should clearly be 0.5.

16 Apr 2012 Maxim

Hello.
I think, there are a bug in the function "triarea".
I miss a factor 0.5 in row 14.
Thanks
Maxim

20 Aug 2011 Bryan

Hello-

Thank you very much for this package, it is exactly what I'm looking for. I do have one question/comment. In Matlab 2011a, I get a warning that tsearch is going to be retired and to use delaunayTri/pointLocation isntead. I subbed in this set of commands (seemingly appropriately), and I cannot get the code to work correctly. Could you possibly advise on this? I'm going to use the code as is for the time being and try to implement and update. I'm not sure if you would be willing to use this update as a newer revision or are currently working on this problem.

Thank you,
Bryan

09 Aug 2011 Darren Engwirda

Hi Philippe, you may want to have a look at the utility "connectivity" routine. Amongst other things, it will identify the boundary nodes in a mesh.

02 Aug 2011 Philippe N/A

Hi and ty for the great toolbox.

I'm using this to make a mesh that I want to use in another modeling tool. This other tool requires me to give nodes that are located on the edge of the mesh figure.

So, once the mesh is generated, is there any way to retrive nodes that have been generated on the contour of the figure?

Regards,
Phil

13 Jun 2011 Jose Luis Prego

Hi, thank you very much for your work. It looks impressive. However allow me to add a small suggestion. It would Very Nice if the Toolbox comes with a small info-note or Manual about the functions and how to start for a beginer. Appart from this you´ve made a Great Work!
Thank you again
:)

23 Mar 2011 Brian Faulkner

Extremely helpful, this is a great library. Well documented and simple to use. Bravo sir, bravo.

26 Dec 2010 Diablo

Thanks so much for your file upload. This was invaluable to me

09 Aug 2010 Philippe N/A

Hi there.
First of all, thank you for that excellent toolbox.
I was wondering if it was possible to specify some point inside a geometric figure. For example, in the Lake Superior demo, I would like to insert a point in a specific location in the middle of the lake so the meshing is finer around that point. However, I can’t find how to do this. I bet I have to construct a new point (let’s say p8[x,y]) and connect it with existing points. But I get lost in that process.

Any help would be greatly appreciated.

With regards,
Phil

09 Jun 2010 Greg

I love your code, but I've recently begun some efforts to see if I can't speed up the runtime a bit.

Is there a particular reason why "mydelaunayn.m" uses the "delaunayn" function instead of the "delaunay" function? As far as I can tell on my machine (with Matlab 7.10), "delaunay" is around four times faster than "delaunayn" and the code seems to function just fine when I make the replacement.

06 Jun 2010 Marios Karaoulis

Why the following fails with meshfaces. I can run it with mesh2d

node = [0.0, 0.0; 1.0,0.0; 1.0,1.0; 0.0,1.0; 2.0,0.0; 2.0,1.0; 3.0,0.0; 3.0,1.0];
edge = [1,2; 2,3; 3,4; 4,1; 2,5; 5,6; 6,3; 5,7; 7,8; 8,6];
faces{1} = [1,2,3,4];
faces{2} = [5,6,7,2];
faces{3} = [8,9,10,6];
hdata.edgeh=[1,0.05 ;7,1];
[p,t]=mesh2d(node,edge,hdata);
%[p,t,fnum]=meshfaces(node,edge,faces,hdata);

running it with meshfaces

??? Index exceeds matrix dimensions.

Error in ==> meshpoly>getedges at 283
bnd = e(~idx,:);
% Boundary edges

Error in ==> meshpoly at 88
e = getedges(t,size(p,1));
% Unique edges

Error in ==> meshfaces at 147
[pnew,tnew] =
meshpoly(node,edge(face{k},:),qtree,pbnd,options);

31 May 2010 Johannes Korsawe

Very nice tool! Thank you a lot for sharing this.

Best regards, Johannes

Very minor irritation:
In line 127 of meshfaces.m, the variable wbar is addressed but never defined.

15 Apr 2010 Canh Le

This is what I was looking for. Many thanks!!

11 Mar 2010 Ralph

Hallo!

Thank you for this great work. Really, really helpful!

I need to ask a question: When using meshfaces, is it possible to use a different 'hfun' for each face?

Thanks,
Ralph

10 Mar 2010 Robert Moucha

Many thanks,

Any plans to bypass MATLAB's tsearch?
In the 2010a release "Warning: TSEARCH will be removed in a future release. Use DelaunayTri/pointLocation instead."

17 Feb 2010 johnus magnus

Just a great collection of functions, thankyou!

19 Jan 2010 Seth  
17 Jan 2010 Wes

I would like to generate a mesh with certain nodes specified on the interior. When I try to do this I end up with areas of my domain that are not filled with elements. For example, I would like node[0.25 0.5] connected to node[0.75 0.5] and the mesh to fill the entire region from [0 0;1 0;1 1;0 1]. The actual meshes I would like to make have more complex node positions.

The following produces a hole in the mesh:

node=[0 0;1 0;1 1;0 1;0.25 0.5;0.75 0.5];
[p,t]=mesh2d(node);

18 Oct 2009 Marios Karaoulis  
18 Oct 2009 Marios Karaoulis

Do you have any thoughts going 3D?

14 Oct 2009 Darren Engwirda

Author comment: v24 was posted to resolve a licence conflict. The Mathworks no longer allows code to be released under a GNU GPL, so this has been removed for v24.

There are some (minor) revisions to the code, based on user feedback. Specifically, v24 may be slightly slower than v23, but should generally produce higher quality meshes, especially for complex geometries.

Comments/feedback is always welcome - d_engwirda@hotmail.com

14 Oct 2009 Marios Karaoulis

Hi, thanks again for your software. What's new in this version ( i am using v23).

Contact us