finding intersecting points of two 3D bodies
Show older comments
I have a set of coordinates (stored in vectors Xa, Ya, Za) that describe a 3D body A (including the coordinates "inside"). A second set of coordinates (Xb, Yb, Zb) describes a second 3D body B. I am trying to find the points of A that "intersect" with B, preferably including some margin. I have tried using convhulln and inhull (see code below), but inhull returns false for all elements of A. I also tried delauneyTriangulation and convexHull, but I cannot get that to work either. I would be very grateful for any ideas/comments/corrections!
C = convhulln([Xb Yb Zb]);
W = [Xa, Ya, Za];
in = inhull(W,C);
1 Comment
Ankit
on 6 Mar 2023
function result = checkIntersection(vertices1, faces1, vertices2, faces2)
% vertices1 and vertices2 are N-by-3 arrays of vertex coordinates
% faces1 and faces2 are M-by-3 arrays of indices into vertices1 and vertices2
% Check if any of the edges or faces of the first geometry intersect with the second geometry
for i = 1:size(faces1, 1)
face1 = vertices1(faces1(i, :), :);
for j = 1:size(faces2, 1)
face2 = vertices2(faces2(j, :), :);
[intersect, ~, ~] = intersectLineTriangle(face1, face2);
if intersect
result = true;
return;
end
end
end
% Check if any of the vertices of the first geometry are inside the second geometry
for i = 1:size(vertices1, 1)
vertex1 = vertices1(i, :);
[inside, ~] = isPointInMesh(vertex1, vertices2, faces2);
if inside
result = true;
return;
end
end
% Check if any of the vertices of the second geometry are inside the first geometry
for i = 1:size(vertices2, 1)
vertex2 = vertices2(i, :);
[inside, ~] = isPointInMesh(vertex2, vertices1, faces1);
if inside
result = true;
return;
end
end
result = false;
end
function [intersect, t, u] = intersectLineTriangle(line, triangle)
% line is a 2-by-3 array representing a line segment
% triangle is a 3-by-3 array representing a triangle
% intersect is true if the line intersects the triangle, false otherwise
% t and u are the barycentric coordinates of the intersection point
edge1 = triangle(2,:) - triangle(1,:);
edge2 = triangle(3,:) - triangle(1,:);
normal = cross(edge1, edge2);
if norm(normal) == 0
% triangle is degenerate
intersect = false;
t = NaN;
u = NaN;
return;
end
dir = line(2,:) - line(1,:);
w0 = line(1,:) - triangle(1,:);
a = -dot(normal,w0);
b = dot(normal,dir);
if abs(b) < eps
% line and triangle are parallel
if a == 0
% line lies in triangle plane
intersect = true;
t = 0;
u = 0;
return;
else
% line is outside triangle plane
intersect = false;
t = NaN;
u = NaN;
return;
end
end
t = a/b;
if t < 0 || t > 1
% intersection point is outside line segment
intersect = false;
u = NaN;
return;
end
I = line(1,:) + t*dir;
w = I - triangle(1,:);
uu = dot(w,edge1)/dot(edge1,edge1);
vv = dot(w,edge2)/dot(edge2,edge2);
if uu >= 0 && vv >= 0 && uu + vv <= 1
% intersection point is inside triangle
intersect = true;
Accepted Answer
More Answers (0)
Categories
Find more on Bounding Regions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!