Exctract specific triangles in a mesh

2 views (last 30 days)
Diego Vargas
Diego Vargas on 1 Sep 2022
Answered: Anurag on 25 Oct 2023
Consider the following mesh
This one was obtained using the PDE modeler app, from PDE toolbox. I need to extract all the triangles inside the square with a red edge, but I am not sure how to do it. Please any help will be appreciated

Answers (1)

Anurag
Anurag on 25 Oct 2023
Hi Diego,
I understand that you want extract specific triangles from a mesh, please refer to the following code which for a sample data does the same. The algorithm followed here is checking each triangle and keeping a track of those triangles which are inside the specified region.
% Sample mesh data
vertices = [
0, 0;
1, 0;
0, 1;
1, 1;
0.3, 0.3;
0.7, 0.3;
0.7, 0.7;
0.3, 0.7
];
faces = [
1, 2, 3;
2, 3, 4;
5, 6, 7;
7, 8, 5
];
% Sample square vertices
squareVertices = [
0.2, 0.2;
0.8, 0.2;
0.8, 0.8;
0.2, 0.8
];
% Initialize an array to store triangles inside the square
trianglesInsideSquare = [];
% Iterate through each triangle
for i = 1:size(faces, 1)
% Get the vertices of the current triangle
currentTriangle = vertices(faces(i, :), :);
% Check if all three vertices of the triangle are inside the square
if all(inpolygon(currentTriangle(:, 1), currentTriangle(:, 2), squareVertices(:, 1), squareVertices(:, 2)))
trianglesInsideSquare = [trianglesInsideSquare; faces(i, :)];
end
end
% Display the indices of triangles inside the square
disp('Indices of triangles inside the square:');
disp(trianglesInsideSquare);
Hope this helped.
Regards,
Anurag

Tags

Community Treasure Hunt

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

Start Hunting!