Exctract specific triangles in a mesh

조회 수: 1 (최근 30일)
Diego Vargas
Diego Vargas 2022년 9월 1일
답변: Anurag 2023년 10월 25일
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

답변 (1개)

Anurag
Anurag 2023년 10월 25일
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

카테고리

Help CenterFile Exchange에서 Geometry and Mesh에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by