How to detect intersection of 3D rectangles that are rotated?
Show older comments
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.
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');
1 Comment
Michael Ferguson
on 6 Dec 2022
Accepted Answer
More Answers (0)
Categories
Find more on Linear Programming and Mixed-Integer Linear Programming 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!