简体   繁体   中英

Performing 2D cut on 3D cube in MATLAB

I have a 3D cube that's filled with small cubes. After filling it with small cubes, I can see its faces. I want to perform a cut through x, y, or z axis, so that I can see the internal structure at some points. Here is an image of the cube,

在此处输入图片说明

I want to see the internal structure of this cube. After search I found that slice can be used. I used this code,

figure
  [x,y,z] = meshgrid(1:100);
 v = repmat(magic(100),[1 1 100]);
% 
% % Define the slice plane
 [xi, yi] = meshgrid(1:100);
 zi = xi;
% 
% % Slice it
slice(x,y,z,v,xi,yi,zi);
 drawnow

But I get a result with completely different colors. Here is the result,

在此处输入图片说明

Can you please tell me how to fix the code above to produce a cut through an axis? Also, I would really appreciate it if you could explain how this process is done as I don't completely understand the code above.

Re. the first question - I don't know of a straightforward way to slice your multicube off-hand with Hoki's code (but see below). polyxpoly might be helpful, but still requires projection onto the plane of the slice.

Re. the second question - slice is cutting "volumetric data," meaning something like density that has a value at each point in the volume. The repmat(magic(...)) line is making a 100x100x100 (3-d) array that has a number at each of the 100*100*100=1,000,000 grid points in the array. It so happens that those points have different values between 1 and 100^2, so you're getting different colors.

Hoki's code doesn't play well with slice because it uses patch to make polygons instead of using volumetric data. An alternative would be to make your cubes volumetrically. This code should run by itself but - caution - is not tested. it is independent of Hoki's code you mentioned.

face=ones(10,10);        %make a small cube, 10x10x10
middle=zeros(10,10);
middle(1,1:10)=1;
middle(10,1:10)=1;
middle(1:10,1)=1;
middle(1:10,10)=1;
small_cube=cat(3,face,repmat(middle,1,1,8),face);

% Now make an array of them - ten in each direction, so 100x100x100
v=repmat(small_cube,10,10,10);

% Now define the slice plane and slice as above.
[xi, yi] = meshgrid(1:100);
zi = xi;

figure;
slice(x,y,z,v,xi,yi,zi);
drawnow

Edit : In the above, 0 is for points that are not part of the cube and 1 is for points that are. This is a voxel representation of a cube. face is for the top and bottom, and middle is for a slice of the edges between the top and bottom. The first repmat stacks up eight slices of the middle like a burger patty. The cat puts a face on the top and bottom of that stack like the two halves of the bun. Then the second repmat makes the 999 other small cubes in the big cube.

Edit 2 : Replacing zeros with NaN in the above code should make the insides of the cube transparent. (Also not tested!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM