pointLocation return only one ID if a point lies in more than one triangle

2 次查看(过去 30 天)
Hi everyone,
this is my code in which I've two triangles and one point and I want to check in which triangle my point lies (I've to use pointLocation for more complex case like a mesh of STL file).
clear all
points=[0 0;1 0; 0 1; 1 1];
tri=[1 2 3; 1 2 4];
TR=triangulation(tri,points);
triplot(TR);
check=[0.6 0.2];
hold on
plot(check(1,1),check(1,2),'o');
ID = pointLocation(TR,check);
I'd like to be returned both the ID of the triangles in which the point lies. How can I do?

回答(1 个)

VINAYAK LUHA
VINAYAK LUHA 2023-9-27
编辑:VINAYAK LUHA 2023-9-27
Hi Matteo,
I understand that you want to find the IDs of all triangles which enclose a point.The “pointLocation” function returns the triangle ID of only the first enclosing triangle as defined in the “Triangulation connectivity”.
To get triangleIDs of all the enclosing triangles, you can do a linear search over all the triangles and register the ones which enclose the given point.
The code snippet to achieve this can be seen below:
points = [0 0; 1 0; 0 1; 1 1];
tri = [1 2 3; 1 2 4];
check = [0.6 0.2];
plot(check(1,1), check(1,2), 'o');
hold on
enclosingTriangleIDs = [];
for i = 1:size(tri, 1)
TR = triangulation(tri(i,:), points);
ID = pointLocation(TR, check);
if ~isnan(ID)
enclosingTriangleIDs = [enclosingTriangleIDs, i];
end
triplot(TR);
end
disp("IDs of triangles enclosing the point:");
disp(enclosingTriangleIDs);
hold off
To learn more about “traingulation” and “pointLocation”, you may refer to the MathWorks documentation links below:
  1. https://in.mathworks.com/help/matlab/ref/triangulation.html
  2. https://in.mathworks.com/help/matlab/ref/triangulation.pointlocation.html
Hope this helps.
Regards,
Vinayak Luha

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by