MATLAB: How can i check a 3D object is located inside or outside of another 3D object

check inside or ouside

I have two 3D objects. One is a small object and another is a big one. Both of them have the same dimension as I attached. I would like to check the small object is located inside, outside or attached boundary of the big object. Please kindly advise me to solve it. Thank you.

Best Answer

Because your data is two logical arrays, you can easily test if the smaller is inside the larger. Finding out if the smaller object is touching the edge is slightly more tricky: we reduce the big object to only its shell, and then we do the same thing again.
%2D test cases
%case 1: completely inside
SmallObj=[0 0 0;0 1 0;0 0 0];
BigObj=true(3,3);
%case 2: edge
SmallObj=[0 0 0;0 1 0;0 1 0];
BigObj=true(3,3);
%case 3: outside
SmallObj=[0 0 0;0 1 0;0 1 0];
BigObj=[0 0 0;0 1 0;0 0 0];
%convert to logical
BigObj=logical(BigObj);
SmallObj=logical(SmallObj);
clc
s=load('data.mat');SmallObj=s.SmallObj;BigObj=s.BigObj;
temp_small=SmallObj;
temp_small=temp_small(~BigObj);
if any(temp_small(:))
disp('some part of SmallObj is outside BigObj')
else
temp_big = bwperim(BigObj);%get the shell voxels
temp_small=SmallObj;
temp_small=temp_small(temp_big);
if any(temp_small(:))
disp('some part of SmallObj along the edge of BigObj')
else
disp('SmallObj is completely inside BigObj')
end
end