How to check 2D rectangle and 3D rectangular prism intersect with each other?
Show older comments
When there are 2D rectangle and 3D rectangular prism with their own orientations, how can I check if they intersect with each other in 3D space? I have 4 points for the rectangle and 8 points for the prism. Is there a built-in function I can use in MATLAB for this purpose? Two suitable rectangle and rectangular prism points are given as .mat file.
Answers (2)
You can use intersectionHull() in this File Exchange submission,
load('rectangle.mat')
load('prism.mat')
S=intersectionHull('vert',prism,'vert',rectangle);
For the data you provided, there is indeed an intersection whose vertices are,
>> S.vert
ans =
649.7498 426.2143 507.2920
647.0217 430.5573 507.3727
655.9783 432.4427 512.9273
653.2502 436.7857 513.0080
2 Comments
Matt J
on 6 Dec 2020
If the sides of the prism and rectangle are always parallel/perpendicular to one another, there may be simpler ways.
Bruno Luong
on 6 Dec 2020
Edited: Bruno Luong
on 6 Dec 2020
Quick and dirty code
load('prism.mat')
load('rectangle.mat')
% Normalize the coordinates
M = prism;
minM = min(M,[],1);
maxM = max(M,[],1);
dM = max(maxM-minM);
cfun = @(xyz) (xyz-(minM+maxM)/2)./dM;
M = [cfun(prism); -cfun(rectangle)]';
% Solve for common point
b = [0; 0; 0; 1; 1];
A = [M;
ones(1,8) zeros(1,4);
zeros(1,8) ones(1,4)];
w = lsqnonneg(A,b);
% Check if solution exists
if norm(A*w-b,inf)<1e-10
fprintf('intersected\n')
else
fprintf('not intersected\n')
end
Categories
Find more on Loops and Conditional Statements 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!