How to check 2D rectangle and 3D rectangular prism intersect with each other?

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

Hello, thanks for help. Nevertheless, I can't use it in Simulink because it says "Function cellfun is not supported for code generation."
Actually, I don't need the intersection area, I just need to know if they intersect with each other. Maybe cellfun part might be used for finding intersection area and we can exclude that part..
If the sides of the prism and rectangle are always parallel/perpendicular to one another, there may be simpler ways.

Sign in to comment.

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

Tags

Asked:

on 5 Dec 2020

Edited:

on 6 Dec 2020

Community Treasure Hunt

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

Start Hunting!