# How to detect intersection of 3D rectangles that are rotated?

3 views (last 30 days)
Michael Ferguson on 6 Dec 2022
Answered: Jeffrey Clark on 6 Dec 2022
I'm working on a program that detects whether 2, 3D rectangles formed from 8 vertices each have volume that intersects between each other.
You can see in my plot the 8 vertices for each cube colored in red and blue, and by inspection they do not overlap.
However, for whatever reason they are incorrectly being treated as intersecting.
I'm using the method described here in the most upvoted StackOverflow answer.
My code so far:
A = [1.06890761348719 0.228825482643729 6.59315554806020];
B = [1.44834644922958 2.01394485398281 6.59315554806020];
C = [1.88232205180130 1.92170049205148 7.14104345088730];
D = [1.50288321605895 0.136581120712592 7.14104345088730];
E = [1.38125706283994 0.162433557649978 7.24173471345524];
F = [1.76069589858218 1.94755292898839 7.24173471345524];
G = [1.32672029601073 2.03979729091965 6.69384681062850];
H = [0.947281460268735 0.254677919582385 6.69384681062850];
P1 = [A;B;C;D;E;F;G;H];
A = [2.13936288118597 1.24719872848015 6.52288724067451];
B = [1.62823228878787 2.82673229470654 5.76496624830234];
C = [1.90147565323319 3.17763404084656 6.31198638352805];
D = [2.41260624563123 1.59810047462036 7.06990737590013];
E = [2.27208449737978 1.58908440932306 7.14588335127981];
F = [1.76095390498189 3.16861797554878 6.38796235890797];
G = [1.48771054053678 2.81771622940902 5.84094222368265];
H = [1.99884113293429 1.23818266318445 6.59886321605394];
P2 = [A;B;C;D;E;F;G;H];
% check if there is intersection
c1 = max(P1(:,1)) > min(P2(:,1));
c2 = min(P1(:,1)) < max(P2(:,1));
c3 = max(P1(:,2)) > min(P2(:,2));
c4 = min(P1(:,2)) < max(P2(:,2));
c5 = max(P1(:,3)) > min(P2(:,3));
c6 = min(P1(:,3)) < max(P2(:,3));
[X,Y,Z] = deal(nan);
if c1 && c2 && c3 && c4 && c5 && c6
X(1) = max(min(P1(:,1)),min(P2(:,1)));
X(2) = min(max(P1(:,1)),max(P2(:,1)));
Y(1) = max(min(P1(:,2)),min(P2(:,2)));
Y(2) = min(max(P1(:,2)),max(P2(:,2)));
Z(1) = max(min(P1(:,3)),min(P2(:,3)));
Z(2) = min(max(P1(:,3)),max(P2(:,3)));
disp('there is an intersection');
else
disp('there is no intersection');
end
plot3(P1(:,1),P1(:,2),P1(:,3),'.b')
hold on
plot3(P2(:,1),P2(:,2),P2(:,3),'.r')
hold off
h = legend('cube1','cube2','intersection cube');
set(h,'orientation','horizontal','location','north')
axis equal vis3d
xlabel('x');
ylabel('y');
zlabel('z');
Michael Ferguson on 6 Dec 2022
I think potentially the reason the StackOverflow answer isn't working is because "overlapping" and "intersecting" are not the exact same thing

Jeffrey Clark on 6 Dec 2022
@Michael Ferguson, You can try the attached delaunayTriangulationIntersect.m function that I use to look for intersection of solids. This is your code showing your case and my slightly modified intersection case (also see attached figures):
A1 = [1.06890761348719 0.228825482643729 6.59315554806020];
B1 = [1.44834644922958 2.01394485398281 6.59315554806020];
C1 = [1.88232205180130 1.92170049205148 7.14104345088730];
D1 = [1.50288321605895 0.136581120712592 7.14104345088730];
E1 = [1.38125706283994 0.162433557649978 7.24173471345524];
F1 = [1.76069589858218 1.94755292898839 7.24173471345524];
G1 = [1.32672029601073 2.03979729091965 6.69384681062850];
H1 = [0.947281460268735 0.254677919582385 6.69384681062850];
P1 = [A1;B1;C1;D1;E1;F1;G1;H1];
DT1 = delaunayTriangulation(P1);
A2 = [2.13936288118597 1.24719872848015 6.52288724067451];
B2 = [1.62823228878787 2.82673229470654 5.76496624830234];
C2 = [1.90147565323319 3.17763404084656 6.31198638352805];
D2 = [2.41260624563123 1.59810047462036 7.06990737590013];
E2 = [2.27208449737978 1.58908440932306 7.14588335127981];
F2 = [1.76095390498189 3.16861797554878 6.38796235890797];
G2 = [1.48771054053678 2.81771622940902 5.84094222368265];
H2 = [1.99884113293429 1.23818266318445 6.59886321605394];
P2 = [A2;B2;C2;D2;E2;F2;G2;H2];
DT2 = delaunayTriangulation(P2);
figure
tetramesh(DT1,'FaceAlpha',0.05,'FaceColor','r');
hold on
tetramesh(DT2,'FaceAlpha',0.05,'FaceColor','b');
DTint = delaunayTriangulationIntersect(DT1,DT2);
if isempty(DTint.Points)
disp("No intersect of DT1 and DT2")
else
tetramesh(DTint,'FaceColor','g');
end
% Make intersecting case
D2F2m = mean([D2;F2]);
C1a = D2F2m+[0,0,0.1];
F1a = D2F2m-[0,0,0.1];
P1a = [A1;B1;C1a;D1;E1;F1a;G1;H1];
DT1a = delaunayTriangulation(P1a);
figure
tetramesh(DT1a,'FaceAlpha',0.05,'FaceColor','r');
hold on
tetramesh(DT2,'FaceAlpha',0.05,'FaceColor','b');
DTint = delaunayTriangulationIntersect(DT1a,DT2);
if isempty(DTint.Points)
disp("No intersect of DT1a and DT2")
else
tetramesh(DTint,'FaceColor','g');
end