在 MATLAB 中用小立方体填充立方体的整个体积

Filing the entire volume of a cube with small cubes in MATLAB

我在 MATLAB 中构建了一个空心立方体,我想用小立方体完全填充它的体积。然后我想找到一种方法来访问这些立方体并通过它们创建路径,即如果当前正在访问立方体 x,应该有一种方法可以知道它的右、左、上、下、前和后最近的邻居是什么(最近的邻居 = 紧邻当前立方体的立方体)。我认为我们有 6 个邻居,因为我们有立方体的 6 个不同面。

通过了解每个方向上最近的立方体,通过立方体的路径可以定义为一系列步骤(例如,右、左、左、上、右、前)。我认为为了能够访问每个小立方体并移动到附近的立方体,我们需要在矩阵(可能是 3D)中表示小立方体,如果一个小立方体的右侧有一个相邻立方体 x,那么在矩阵中,x将出现在小立方体当前列旁边的列中。此外,如果在另一个深度层有一个直接邻居(相同的 x、y 坐标但不同的 z 坐标,例如前后邻居),则应该指出它。有没有更简单的方法来识别邻居?

我通过 rayryeng () 获得了一个代码,可以在大立方体中随机填充许多小立方体并构建一个 3D 矩阵,其中每一片矩阵(深度)代表一个小立方体,每个切片(8 行和 3 列)的行和列代表每个小立方体顶点的 x y z 坐标。请看一下我提供的问题link看代码

我想对代码做两处修改,

1- 有条不紊地用小方块填充大方块,而不是随机填充。

2- 调整 3D 矩阵以表示小立方体如何彼此相邻。

我已尝试调整 linked 问题中的代码,以有组织的方式填充立方体。这是我的试用版(我在for循环中添加了if else),

   clf;
figure(1);
format compact 
h(1) = axes('Position',[0.2 0.2 0.6 0.6]);

%These are the different 8 vertices of the cube, each is defined by its 3 x
%y z coordinates:
vert = [1 1 -1; 
        -1 1 -1; 
        -1 1 1; 
        1 1 1; 
        -1 -1 1;
        1 -1 1; 
        1 -1 -1;
        -1 -1 -1];
%These are the 6 faces of the cube, each is defined by connecting 4 of the
%available vertices:
fac = [1 2 3 4; 
       4 3 5 6; 
       6 7 8 5; 
       1 2 8 7; 
       6 7 1 4; 
       2 3 5 8];

% I defined a new cube whose length is 1 and centers at the origin.
vert2 = vert * .05;  
fac2 = fac;


patch('Faces',fac,'Vertices',vert,'Facecolor', 'w');  % patch function for the first big cube. 
axis([-1, 1, -1, 1, -1, 1]);
axis equal;

hold on;

patch('Faces', fac2, 'Vertices', vert2, 'FaceColor', 'r', 'EdgeColor', 'none');
material metal;
alpha('color');
alphamap('rampdown');
%view(3);


hold on;
rng(123); %// Set seed for reproducibility
num_squares = 1000; %// Set total number of squares

%// New - to store the coordinates
coords = [];

%// For remembering the colours
colors = [];
%// For each square...

for idx = 1 : num_squares

    
    %// Take the base cube and add an offset to each coordinate
    %// Each coordinate will range from [-1,1]
    if (idx==1)
    vert_new = bsxfun(@plus, vert2, [.01 .01 .01]);
    
    else 
    vert_new = bsxfun (@plus, vert_new,[.01 .01 .01] );
    end
    %// New - For the coordinates matrix
    coords = cat(3, coords, vert_new);

    %// Generate a random colour for each cube
    color = rand(1,3);

    %// New - Save the colour
    colors = cat(1, colors, color);

    %// Draw the cube
    patch('Faces', fac, 'Vertices', vert_new, 'FaceColor', color,'EdgeColor', 'none');
end




%// Post processing
material metal;
alpha('color');
alphamap('rampdown');
view(3);

但我得到的结果如下图所示,

谁能告诉我如何解决这个问题并构建 3D 矩阵(或任何其他更简单的方法来表示每个立方体的邻居)?

编辑:详细说明小立方体邻居问题。考虑在大立方体内部的任何位置放置一个立方体 C。设立方体位置为 (.5,.5,.5),我们可以将其视为立方体的中心。然后,这个立方体将有 6 个相邻立方体(直接位于它旁边),每个轴有 2 个相邻立方体。所以,对于立方体 (.5,.5,.5) 我们有,

C 右侧的 x 轴邻居 (.5 +offset, .5, .5)

C 左侧的 x 轴邻居 (.5 -offset, .5, .5)

y 轴邻近 C 的顶部 (.5 , .5+offset, .5)

y 轴与 C 底部相邻(.5,.5-偏移量,.5)

z 轴在 C (.5, .5, .5+offset) 之后相邻一个深度

z 轴相邻 C 之前的深度(.5、.5、.5-偏移量)

其中偏移量可以看作是立方体中心与其任意面之间的距离。这只是为了阐明想法的解释,不需要以相同的方式实现。我希望这是清楚的,我将不胜感激构建这个邻域矩阵的任何帮助。

谢谢。

同样的原理,我们建一个大立方体,然后在角落里建一个小立方体,然后我们用一个小的偏移量重复建小立方体,直到我们装满。与旧代码的主要区别在于,这次每个坐标集的步长变化是受控的(x,y,z小立方体坐标的函数),而不是随机的。

%%
clf; figure(1); format compact 
h(1) = axes('Position',[0.2 0.2 0.6 0.6]);

%These are the different 8 vertices of the cube, each is defined by its 3 x y z coordinates:
vert = [ 1  1 -1; -1  1 -1; -1  1  1; 1  1  1; -1 -1  1; 1 -1  1; 1 -1 -1; -1 -1 -1];

%These are the 6 faces of the cube, each is defined by connecting 4 of the available vertices:
fac = [1 2 3 4; 4 3 5 6; 6 7 8 5; 1 2 8 7; 6 7 1 4; 2 3 5 8];

%// How many small cube do we want
MainCubeSide = 2 ;              %// dimension of the side of the main cube
nCubeOnSide = 5 ;               %// number of small cube in one "row/column" of the main cube
nCubesTotal = nCubeOnSide^3  ;  %// total number of small cube

% define the Main container cube
MainCube.Vertices = vert *(2/MainCubeSide) ; %// because the cube as defined above has already a side=2
MainCube.Faces = fac ;
MainCube.FaceColor = 'w' ;

hMainCube = patch(MainCube);  %// patch function for the first big cube. 
axis([-1, 1, -1, 1, -1, 1]);
axis equal;
hold on;
material metal;
alpha('color');
alphamap('rampdown');
view(138,24)
%view(3);


%% // generate all the coordinates of each cube first
dstep = MainCubeSide / nCubeOnSide ;                                                 %// step size for small cube vertices
vElem = bsxfun(@plus, vert / nCubeOnSide , -( MainCubeSide/2 - dstep/2)*[1 1 1] )  ; %// elementary cube vertices

%%
hold on;
coords = zeros( size(vElem,1),size(vElem,2), nCubesTotal ) ;  %// To store the coordinates
colors = zeros( nCubesTotal , 3 ) ;                           %// To store the colours
hcube  = zeros( nCubesTotal , 1 ) ;                           %// To store the handles of the patch objects

iNeighbour = zeros( nCubesTotal , 6 ) ;   %// To save the index of the neighbours
idc = permute( reshape(1:nCubesTotal,nCubeOnSide,nCubeOnSide,nCubeOnSide) , [3 2 1] ) ;

%// For each cube ...
iCube = 0 ;
for iline=1:nCubeOnSide         %// Lines
    for icol=1:nCubeOnSide      %// Columns
        for ih=1:nCubeOnSide    %// Slice (height)
            iCube = iCube + 1 ;

            %// Take the base corner coordinates and add an offset to each coordinate
            coords(:,:,iCube) = bsxfun(@plus, vElem , dstep*[(iline-1) (icol-1) (ih-1)]);

            %// Save the colour
            colors(iCube,:) = rand(1,3) ; 

            %// Draw the cube
            hcube(iCube) = patch('Faces', fac, 'Vertices', coords(:,:,iCube), 'FaceColor', colors(iCube,:) ) ;

            drawnow     %// just for intermediate display, you can comment these 2 lines
            pause(0.05) %// just for intermediate display, you can comment these 2 lines

            %// save adjacent cubes indices
            ixAdj = [iline-1 iline+1 icol-1 icol+1 ih-1 ih+1] ;  %// indices of adjacent cubes
            idxFalse = (ixAdj<1) | (ixAdj>nCubeOnSide) ;  %// detect cube which would be "out" of the main cube
            ixAdj(idxFalse) = 1 ;                                %// just to not get an "indexing" error at this stage
            iNeighbour(iCube,:) = [idc(ixAdj(1),icol,ih)    idc(ixAdj(2),icol,ih) ...
                                   idc(iline,ixAdj(3),ih)   idc(iline,ixAdj(4),ih) ...
                                   idc(iline,icol,ixAdj(5)) idc(iline,icol,ixAdj(6)) ] ;
            iNeighbour(iCube,idxFalse) = NaN ;
        end
    end
end

此代码将每个多维数据集的句柄保存在变量 hcube 中,因此您可以根据需要对所有多维数据集批量分配 属性。例如delete(hcube)会一次性删除所有的小立方体,或者set(hcube,'Facealpha',0.5)会让所有的立方体半透明。

您还可以 set/change 仅对其中的一部分属性 hcube(idx_subset) = ...。这是通过索引了解相邻立方体可能有用的地方,但您的邻接问题尚未完全定义。


编辑: 我在主循环中添加了邻居跟踪。这可能不是最有效的方法,但它确实保留了每个基本立方体的所有邻居的索引。 iNeighbour 变量(大小:nCubesx6)保存每个邻居(6 个可能的邻居)的句柄索引。当邻居不存在时,我选择放置 NaN 代替。 为了在没有 NaN 的情况下直接检索邻居的索引,我定义了一个辅助匿名函数:

getNeighbourIndex = @(idx) iNeighbour(idx,~isnan(iNeighbour(idx,:))) ;

现在可以帮助您跟踪给定立方体的所有邻居。例如:

set(hcube,'Visible','off')  %// turn off all small cubes
CubeOfInterest = 111 ;      %// select one cube
%// display the main cube of interest, and it's neighbours in transparency
set(hcube(CubeOfInterest),'Visible','on','FaceColor','r','FaceAlpha',1) 
set(hcube(getNeighbourIndex(CubeOfInterest)),'Visible','on','FaceColor','g','FaceAlpha',.05)

如你所见,无论我们是否靠墙,所有的邻居都在那里。