Create mesh around more than one shape

7 views (last 30 days)
I am trying to create an autonomous meshing code to create a 2D mesh around certain shapes that are created by the MATLAB contour function via the generateMesh function.
I am building the geometry from the contour coordinate data. This is used to create a polygon shape to be defined in the geometry description matrix, then the MATLAB decsg function is used.
I have succesfully got this working for a domain rectangle with one contour shape deducted from it. However, when I have more than one shape removed from the rectangle it fails, I get the following error displayed:
Output argument "dl1" (and maybe others) not assigned during call to "decsg".
If I instead try to add the domain and the contours together in the Set Formula, then again, it works for the domain plus one contour shape, but for multiple shapes I recieve the following error:
Meshing failed to create initial discretization.
Below is the algorithm I am using to build the geometry description matrix from the contour data:
% Domain rectangle
DR = [3,4,x_dom(1),x_dom(2),x_dom(2),x_dom(1),y_dom(2),y_dom(2),y_dom(1),y_dom(1)]';
S(1,:) = DR(:,1)';
% Contours from contour plot
for i = 1:n_contours
x = A{1,i}(1,:); % extract x data
y = A{1,i}(2,:); % extract y data
% Contour geometry description
S(i+1,1) = 2;
S(i+1,2) = numel(x(1,:));
k = 2;
for ii = 1:(numel(x(1,:)))
k = k+1;
S(i+1,k) = x(1,ii);
end
k = numel(x(1,:)) + 2;
for jj = 1:(numel(y(1,:)))
k = k+1;
S(i+1,k) = y(1,jj);
end
% Names each contour formula iteratively
cf_i(i,:) = ['S',num2str(i)];
end
gdm = S'; % geometry description matrix
where, n_contours is the total number of contours produced by the MATLAB contour function, and A is the cell array that hold all the contour data sets. See below the mesh successfully created for a domain with one contour.
Below is how I autonomously create a character string for the Set Formula and Name-Space matrix.
% Set formula
dr = 'R1';
sf = [dr,'-',cf_i(1,:)]; % set formula for when removing contours from domain
% sf = [dr,'+',cf_i(1,:)]; % set formula for when adding contours to domain
for i = 2:n_contours
sf = [sf,'-',cf_i(i,:)]; % set formula for when removing contours from domain
% sf = [sf,'+',cf_i(i,:)]; % set formula for when adding contours to domain
end
% Name geometry
ns = char([dr]);
for i = 1:n_contours
ns = char([ns;cf_i(i,:)]);
end
% Invoke decsg
g = decsg(gdm,sf,ns');
Below is an image of the meshed domain with one shape successfully removed (left) and the same domain and contour shape but added together (right):
Any insight to why this is not working for more than one shape is greatly appriciated.
Thank you.
Full code attached.

Accepted Answer

Elliot Bontoft
Elliot Bontoft on 22 May 2020
To any others facing this problem in the future, I believe I found the solution:
The MATLAB contours function creates coordinate data that (when the shape is closed) has a coordinate point that starts and ends on the same point. The decsg function, however, only asks for the coordinate point at the beginning point of the edge for a polygon, this was causing problems when run iteratively - but was fine when run for one contour line (as I mentioned in the question above).
Removing the final point from the contour line data resolved the problem:
for i = 1:n_contours
x = A{1,i}(1,:);
y = A{1,i}(2,:);
% Contour geometry description
S(i+1,1) = 2;
S(i+1,2) = numel(x(1,:))-1; %introduced this -1
k = 2;
for ii = 1:(numel(x(1,:)))-1 %introduced this -1
k = k+1;
S(i+1,k) = x(1,ii);
end
k = numel(x(1,:)) + 1; %this is now +1 rather than +2
for jj = 1:(numel(y(1,:)))-1 %introduced this -1
k = k+1;
S(i+1,k) = y(1,jj);
end
% Names each contour formula iteratively
cf_i(i,:) = ['S',num2str(i)];
end

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!