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 Center および File ExchangeGeometry and Mesh についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by