Search code examples
matlab3dgridcubesbrep

Generation of cubic grid


I am trying to generate a cubic grid in Matlab so that I can produce a grid of M x N x Q cubes with M, N and Q being integer numbers. I don't need to plot it but rather to produce a B-Rep of the grid (vertex matrix and faces matrix - with no duplicate internal faces). I have tried two approaches:

  1. Copy and translate points in the X, Y, Z direction, eliminate duplicate points and try to generate the new topology (I have no idea how).
  2. Use the Matlab Neuronal Neural Network toolbox, specifically the gridplot function that produces a 3D grid of points but the faces matrix cannot be generated from this function.

Any suggestions?

Thank you.

Update The vertex matrix contains all 8 points of each cube and the faces matrix all 6 faces of each cube. I can generate that with the following code: clc clear

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];      
vert_total = [];
face_total = fac;

for x = 0 : 1
for y = 0 : 1
for z = 0 : 1
  vert = [1 1 0; 
         0 1 0; 
         0 1 1; 
         1 1 1; 
         0 0 1;
         1 0 1; 
         1 0 0;
         0 0 0];
 vert(:,1) = vert(:,1) + x;
 vert(:,2) = vert(:,2) + y;
 vert(:,3) = vert(:,3) + z;
 vert_total = [vert_total; vert];

 face = face_total(end-5:end,:);
 face_total = [face_total; face+8];

end
end
end

The problem with this code is that it contains duplicate vertex and duplicate faces. Eliminating the repeated vertex is pretty straightforward using the unique function, but I don't know how to handle the topology (faces matrix) when I eliminate the repeated points (obviously, some of the faces should be eliminated as well).

Any sugestions with that?


Solution

  • You can create the 3D grid, and then keep only those at 6 faces. Someone else may point a better way than this.

    M = 5; N = 6; Q = 7;
    [X, Y, Z] = ndgrid(1:M, 1:N, 1:Q); % 3D
    faces = X==1 | X==M | Y==1 | Y==N | Z==1 | Z==Q;
    X = X(faces);
    Y = Y(faces);
    Z = Z(faces);
    

    Now [X Y Z] are coordinates for faces.