demo15 of Im2mesh package
demo15 - Generate mesh based on 2D contours
Initialize
No need to install any MATLAB toolboxes when running this demo.
Before we start, please set folder "Im2mesh_Matlab" as your current folder of MATLAB.
Set default image size (optional).
x = 250; y = 250; width = 300; height = 300;
set(groot, 'DefaultFigurePosition', [x,y,width,height])
% set(groot, 'DefaultFigurePosition', 'factory')
Function bounds2mesh use a mesh generator called MESH2D (developed by Darren Engwirda). We can use the following command to add the folder 'mesh2d-master' to the path of MATLAB.
addpath(genpath('mesh2d-master'))
2D Matrix
Input is a 2d matrix with height value.
Z = peaks(n); % Z is a 21-by-21 matrix containing the height values of
% the surface with respect to the x-y plane.
Plot surface
2D Contour
Define interested level and plot contour
level = 1.5; % interested level
contour(Z, [level,level]);
Extract countour
Store vertices in the countour
% add (-inf) padding to Z
Z2 = padarray(Z,[1 1],-inf,'both');
C = contourc(Z2, [level,level]);
contourCell = {}; % a cell array for vertices in countours
k = 1; % current column in C
nv = C(2,k); % number of vertices
idx = k+1 : k+nv; % columns that hold the vertices
xy = C(1:2,idx)'; % nv-by-2 array
contourCell{end+1} = xy -1 ; % -1 is used to removed padding
k = k + nv + 1; % jump to next header
Plot contourCell
for i = 1: length(contourCell)
plot( contourCell{i}(:,1), contourCell{i}(:,2) )
Convert to polyshape
Convert contourCell to polyshape object
for i = 1: length(contourCell)
x_temp = [ x_temp; NaN; contourCell{i}(:,1) ];
y_temp = [ y_temp; NaN; contourCell{i}(:,2) ];
psIn = polyshape( x_temp, y_temp );
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
Plot polyshape object
Create outer boundary
Create polyshape for outer boundary
vertex = [xmin ymin; xmax ymin; xmax ymax; xmin ymax; xmin ymin];
psOB = polyshape(vertex); % polyshape for outer boundary
Plot
Boolean operation
psOB = subtract(psOB, psIn);
Plot
Convert to polygonal boundaries
Convert to a nested cell array of polygonal boundaries
psCell = {psOB; psIn}; % a cell array of polyshape
bounds = polyshape2bound(psCell); % a nested cell array of polygonal boundaries
bounds = addIntersectPnts( bounds, tol_intersect );
% plot polygons and show all vertices
plotBounds(bounds,false,'ko-')
Simplify boundaries
We want to reduce the number of vertices.
boundsCtrlP = getCtrlPnts( bounds );
% simplify polyline using Douglas-Peucker algorithm
boundsReduced = simplifyBounds( boundsCtrlP, tolerance );
plotBounds(boundsReduced,false,'ko-')
Generate mesh via MESH2D
opt.disp = inf; % silence verbosity
[vert,tria,tnum,vert2,tria2] = bounds2mesh( boundsReduced, hmax, grad_limit, opt );
Plot mesh
plotMeshes( vert,tria,tnum, color );
We can use function tricost to check mesh quality.
tricost( vert, tria );
See the link below for more examples.
set(groot, 'DefaultFigurePosition', 'factory')