I am looking to create a mesh within the lines of a 2D contour plot.
For example, with the code:
f = peaks;
contour(f,[2 2])
You achieve the following plot:
I would like to be able to generate a mesh inside and outside of the contour lines (within a domain of say 50x50 for this example).
Could someone suggest the best way to achieve this?
Thank you in advance.

2 comentarios

Ameer Hamza
Ameer Hamza el 14 de Mayo de 2020
Are you trying to create a mesh plot? If you want to plot both inside and outside the contour lines, isn't it the same as a regular mesh plot? Can you show an example image of what is your intended output?
Elliot Bontoft
Elliot Bontoft el 14 de Mayo de 2020
I am trying to develop a finite element mesh, both inside and outside the contour lines, where the nodes align on the contours themselves.
Something like:
but with the inside of the shape meshed too.
Where the shape would be defined by the contour lines from the contour(f,[2 2]) function.
Thanks

Iniciar sesión para comentar.

 Respuesta aceptada

darova
darova el 14 de Mayo de 2020

1 voto

Use initmesh
clc,clear
x1 = [0 5 5 0]; % rectangle
y1 = [0 0 3 3];
[x2,y2] = pol2cart(0:.3:2*pi,1); % circle
gd2 = [2;length(x2);x2(:)+1.5;y2(:)+1.5]; % circle geometry
gd1 = gd2*0;
gd11 = [2;length(x1);x1(:);y1(:)]; % rectangle geometry
gd1(1:length(gd11)) = gd11;
dl = decsg([gd1 gd2],'P1-P2',char('P1','P2')'); % decomposition geometry
[p,e,t] = initmesh(dl); % build a mesh
pdemesh(p,e,t) % display mesh
dl2 = decsg(gd2); % decomposition of circle
[p,e,t] = initmesh(dl2); % build a mesh
hold on
pdemesh(p,e,t) % display emsh
hold off

4 comentarios

Elliot Bontoft
Elliot Bontoft el 19 de Mayo de 2020
Editada: Elliot Bontoft el 19 de Mayo de 2020
Hi Darova,
This is very useful, I have been trying to use this (except with the generateMesh function rather than initmesh - as I believe initmesh is a legacy method https://uk.mathworks.com/help/pde/ug/initmesh.html)
I have used the coordinate data of the contours to build the geometry description matrix for the decsg function. This has been effective for when I have only one contour inside the domain rectangle. However, for more than one contour this creates an error - I have posted this in another forum question as I believe it is a seperate question (https://uk.mathworks.com/matlabcentral/answers/526595-create-mesh-around-more-than-one-shape).
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.
To my knowledge this is the best way to achieve this.
Thank you for your help.
Full code attached.
darova
darova el 19 de Mayo de 2020
Editada: darova el 19 de Mayo de 2020
What about brute force?
x1 = [5 5 0 0 5]; % rectangle
y1 = [0 3 3 0 0];
[x2,y2] = pol2cart(0:.2:2*pi,1); % circle
gd = [2;length(x2)+length(x1)
x2(:)+1.5;x1(:)
y2(:)+1.5;y1(:)];
dl = decsg(gd);
[p,e,t] = initmesh(dl); % build a mesh
pdemesh(p,e,t) % display mesh
axis equal
darova
darova el 19 de Mayo de 2020
Can be connected closer
Elliot Bontoft
Elliot Bontoft el 20 de Mayo de 2020
Hi Darova,
I see what you've done there, very clever work around. However, I will be using this meshed model to run a simulation on, and the boundaries will influence the behaviour of the response.
Thank you for the suggestions

Iniciar sesión para comentar.

Más respuestas (1)

Jiexian Ma
Jiexian Ma el 6 de Abr. de 2025

0 votos

As far as I know, there are two ways to do this.
Personally, I perfer method 2 because you can edit polyshape object easily.
You could check demo14 to demo17 in Im2mesh package. I provide a few examples.

Productos

Versión

R2019b

Preguntada:

el 14 de Mayo de 2020

Respondida:

el 6 de Abr. de 2025

Community Treasure Hunt

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

Start Hunting!

Translated by